diff --git a/src/lib/git.test.ts b/src/lib/git.test.ts index 4958c69e..8c8fe4cf 100644 --- a/src/lib/git.test.ts +++ b/src/lib/git.test.ts @@ -3,10 +3,21 @@ import { Git } from './git'; import git from 'isomorphic-git'; import type { NPool } from '@nostrify/nostrify'; import type { JSRuntimeFS } from './JSRuntime'; +import { nip19 } from 'nostr-tools'; // Mock isomorphic-git vi.mock('isomorphic-git'); vi.mock('isomorphic-git/http/web'); +vi.mock('nostr-tools', () => ({ + nip19: { + decode: vi.fn(), + }, +})); +vi.mock('@nostrify/nostrify', () => ({ + NIP05: { + lookup: vi.fn(), + }, +})); const mockGit = vi.mocked(git); @@ -174,4 +185,120 @@ describe('Git', () => { }); }); }); + + describe('Nostr URI cloning', () => { + beforeEach(() => { + mockGit.clone = vi.fn().mockResolvedValue(undefined); + mockGit.deleteRemote = vi.fn().mockResolvedValue(undefined); + mockGit.addRemote = vi.fn().mockResolvedValue(undefined); + + // Mock nip19 decode + vi.mocked(nip19.decode).mockReturnValue({ + type: 'npub', + data: 'abcd1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab' + }); + + // Mock Nostr query to return a repository event + const mockRelay = { + query: vi.fn().mockResolvedValue([{ + id: 'event123', + pubkey: 'abcd1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab', + kind: 30617, + content: '', + tags: [ + ['d', 'my-repo'], + ['clone', 'https://github.com/user/repo.git'], + ['clone', 'https://gitlab.com/user/repo.git'] + ], + created_at: Date.now(), + sig: 'signature' + }]) + }; + + nostr.group = vi.fn().mockReturnValue(mockRelay); + nostr.relay = vi.fn().mockReturnValue(mockRelay); + }); + + it('successfully clones from Nostr URI and updates origin remote', async () => { + const nostrUrl = 'nostr://npub1abc123/my-repo'; + + await gitInstance.clone({ + dir: '/test', + url: nostrUrl, + }); + + // Should attempt to clone from the first URL + expect(mockGit.clone).toHaveBeenCalledWith({ + fs, + http: expect.any(Object), + corsProxy: 'https://cors.example.com', + dir: '/test', + url: 'https://github.com/user/repo.git', + }); + + // Should delete the existing origin remote + expect(mockGit.deleteRemote).toHaveBeenCalledWith({ + fs, + dir: '/test', + remote: 'origin', + }); + + // Should add the Nostr URI as the new origin + expect(mockGit.addRemote).toHaveBeenCalledWith({ + fs, + dir: '/test', + remote: 'origin', + url: nostrUrl, + }); + }); + }); + + describe('setRemoteURL', () => { + beforeEach(() => { + mockGit.deleteRemote = vi.fn().mockResolvedValue(undefined); + mockGit.addRemote = vi.fn().mockResolvedValue(undefined); + }); + + it('updates remote URL by deleting and re-adding remote', async () => { + await gitInstance.setRemoteURL({ + dir: '/test', + remote: 'origin', + url: 'https://new-repo.com/user/repo.git', + }); + + expect(mockGit.deleteRemote).toHaveBeenCalledWith({ + fs, + dir: '/test', + remote: 'origin', + }); + + expect(mockGit.addRemote).toHaveBeenCalledWith({ + fs, + dir: '/test', + remote: 'origin', + url: 'https://new-repo.com/user/repo.git', + }); + }); + + it('works with any remote name', async () => { + await gitInstance.setRemoteURL({ + dir: '/test', + remote: 'upstream', + url: 'nostr://npub1abc123/my-repo', + }); + + expect(mockGit.deleteRemote).toHaveBeenCalledWith({ + fs, + dir: '/test', + remote: 'upstream', + }); + + expect(mockGit.addRemote).toHaveBeenCalledWith({ + fs, + dir: '/test', + remote: 'upstream', + url: 'nostr://npub1abc123/my-repo', + }); + }); + }); }); \ No newline at end of file diff --git a/src/lib/git.ts b/src/lib/git.ts index ae8debfb..ea9cb139 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -646,6 +646,13 @@ export class Git { ) ]); + // After successful clone, update the origin remote to point to the Nostr URI + await this.setRemoteURL({ + dir: originalOptions.dir, + remote: 'origin', + url: originalOptions.url, + }); + return; // Success! } catch (error) { console.warn(`Failed to clone from ${cloneUrl}:`, error); @@ -657,4 +664,21 @@ export class Git { // If we get here, all clone attempts failed throw lastError || new Error('All clone attempts failed'); } + + async setRemoteURL(options: { dir: string; remote: string; url: string }): Promise { + // Remove the existing remote + await git.deleteRemote({ + fs: this.fs, + dir: options.dir, + remote: options.remote, + }); + + // Add the remote with the new URL + await git.addRemote({ + fs: this.fs, + dir: options.dir, + remote: options.remote, + url: options.url, + }); + } } \ No newline at end of file