diff --git a/src/components/git/AnnounceRepositoryDialog.test.tsx b/src/components/git/AnnounceRepositoryDialog.test.tsx new file mode 100644 index 00000000..38fa8bd3 --- /dev/null +++ b/src/components/git/AnnounceRepositoryDialog.test.tsx @@ -0,0 +1,124 @@ +import { describe, it, expect } from 'vitest'; + +describe('AnnounceRepositoryDialog - NIP-34 Tag Structure', () => { + it('should create tags with multiple values in single tag per NIP-34', () => { + // Test the tag structure that would be created + const webUrls = ['https://example.com/browse', 'https://mirror.example.com']; + const cloneUrls = [ + 'https://git.shakespeare.diy/npub1.../repo.git', + 'https://relay.ngit.dev/npub1.../repo.git', + ]; + const relays = ['wss://git.shakespeare.diy', 'wss://relay.ngit.dev']; + + const tags: string[][] = [['d', 'test-repo']]; + + // Simulate the tag creation logic from AnnounceRepositoryDialog + if (webUrls.length > 0) { + const trimmedUrls = webUrls.map((url) => url.trim()).filter((url) => url); + if (trimmedUrls.length > 0) { + tags.push(['web', ...trimmedUrls]); + } + } + + if (cloneUrls.length > 0) { + const trimmedUrls = cloneUrls.map((url) => url.trim()).filter((url) => url); + if (trimmedUrls.length > 0) { + tags.push(['clone', ...trimmedUrls]); + } + } + + if (relays.length > 0) { + const trimmedRelays = relays.map((relay) => relay.trim()).filter((relay) => relay); + if (trimmedRelays.length > 0) { + tags.push(['relays', ...trimmedRelays]); + } + } + + // Verify the structure matches NIP-34 spec + expect(tags).toEqual([ + ['d', 'test-repo'], + ['web', 'https://example.com/browse', 'https://mirror.example.com'], + [ + 'clone', + 'https://git.shakespeare.diy/npub1.../repo.git', + 'https://relay.ngit.dev/npub1.../repo.git', + ], + ['relays', 'wss://git.shakespeare.diy', 'wss://relay.ngit.dev'], + ]); + + // Verify we have single tags with multiple values, not multiple tags + const cloneTags = tags.filter((t) => t[0] === 'clone'); + expect(cloneTags.length).toBe(1); + expect(cloneTags[0].length).toBe(3); // ['clone', url1, url2] + + const relayTags = tags.filter((t) => t[0] === 'relays'); + expect(relayTags.length).toBe(1); + expect(relayTags[0].length).toBe(3); // ['relays', relay1, relay2] + }); + + it('should handle reading relays from single tag with multiple values', () => { + // Simulate the event structure from Nostr + const repoEvent = { + tags: [ + ['d', 'hello-shakespeare'], + [ + 'clone', + 'https://git.shakespeare.diy/npub1.../hello-shakespeare.git', + 'https://relay.ngit.dev/npub1.../hello-shakespeare.git', + ], + ['relays', 'wss://git.shakespeare.diy', 'wss://relay.ngit.dev'], + ], + }; + + // Simulate the reading logic from PullRequestDialog + const relayTag = repoEvent.tags.find((t: string[]) => t[0] === 'relays'); + let publishRelays: string[] = []; + + if (relayTag && relayTag.length > 1) { + publishRelays = relayTag.slice(1); + } + + // Verify we extracted all relay URLs correctly + expect(publishRelays).toEqual(['wss://git.shakespeare.diy', 'wss://relay.ngit.dev']); + }); + + it('should handle reading clone URLs from single tag with multiple values', () => { + const repoEvent = { + tags: [ + ['d', 'hello-shakespeare'], + [ + 'clone', + 'https://git.shakespeare.diy/npub1.../hello-shakespeare.git', + 'https://relay.ngit.dev/npub1.../hello-shakespeare.git', + ], + ], + }; + + const cloneTag = repoEvent.tags.find((t: string[]) => t[0] === 'clone'); + let cloneUrls: string[] = []; + + if (cloneTag && cloneTag.length > 1) { + cloneUrls = cloneTag.slice(1); + } + + expect(cloneUrls).toEqual([ + 'https://git.shakespeare.diy/npub1.../hello-shakespeare.git', + 'https://relay.ngit.dev/npub1.../hello-shakespeare.git', + ]); + }); + + it('should handle reading maintainers from single tag with multiple values', () => { + const repoEvent = { + tags: [ + ['d', 'hello-shakespeare'], + ['maintainers', 'pubkey1', 'pubkey2', 'pubkey3'], + ], + }; + + // Simulate the reading logic from NostrGitProvider + const maintainerTag = repoEvent.tags.find((t: string[]) => t[0] === 'maintainers'); + const maintainers = maintainerTag ? maintainerTag.slice(1) : []; + + expect(maintainers).toEqual(['pubkey1', 'pubkey2', 'pubkey3']); + }); +}); diff --git a/src/components/git/AnnounceRepositoryDialog.tsx b/src/components/git/AnnounceRepositoryDialog.tsx index e59c2606..627ba7e7 100644 --- a/src/components/git/AnnounceRepositoryDialog.tsx +++ b/src/components/git/AnnounceRepositoryDialog.tsx @@ -194,20 +194,29 @@ export function AnnounceRepositoryDialog({ tags.push(['description', description.trim()]); } - // Add web URLs - webUrls.forEach(url => { - if (url.trim()) tags.push(['web', url.trim()]); - }); + // Add web URLs (all in a single tag per NIP-34) + if (webUrls.length > 0) { + const trimmedUrls = webUrls.map(url => url.trim()).filter(url => url); + if (trimmedUrls.length > 0) { + tags.push(['web', ...trimmedUrls]); + } + } - // Add clone URLs (required) - cloneUrls.forEach(url => { - if (url.trim()) tags.push(['clone', url.trim()]); - }); + // Add clone URLs (required, all in a single tag per NIP-34) + if (cloneUrls.length > 0) { + const trimmedUrls = cloneUrls.map(url => url.trim()).filter(url => url); + if (trimmedUrls.length > 0) { + tags.push(['clone', ...trimmedUrls]); + } + } - // Add relays for patch submissions - relays.forEach(relay => { - if (relay.trim()) tags.push(['relays', relay.trim()]); - }); + // Add relays for patch submissions (all in a single tag per NIP-34) + if (relays.length > 0) { + const trimmedRelays = relays.map(relay => relay.trim()).filter(relay => relay); + if (trimmedRelays.length > 0) { + tags.push(['relays', ...trimmedRelays]); + } + } // Add earliest unique commit (euc) for repository identification if (earliestCommit.trim()) { diff --git a/src/components/git/PullRequestDialog.tsx b/src/components/git/PullRequestDialog.tsx index c0484e6f..5711041c 100644 --- a/src/components/git/PullRequestDialog.tsx +++ b/src/components/git/PullRequestDialog.tsx @@ -724,9 +724,11 @@ export function PullRequestDialog({ } // Get relay hints from repository announcement - const relayTags = repoEvent.tags.filter((t: string[]) => t[0] === 'relays'); - if (relayTags.length > 0) { - publishRelays = relayTags.map((t: string[]) => t[1]); + // Per NIP-34, all relays are in a single tag: ["relays", "wss://...", "wss://...", ...] + const relayTag = repoEvent.tags.find((t: string[]) => t[0] === 'relays'); + if (relayTag && relayTag.length > 1) { + // Extract all relay URLs from the tag (skip the first element which is "relays") + publishRelays = relayTag.slice(1); console.log('Publishing to repository relays:', publishRelays); } else { console.log('No relay hints in repository announcement, using configured relay'); diff --git a/src/lib/git-hosts/NostrGitProvider.ts b/src/lib/git-hosts/NostrGitProvider.ts index 4b814d63..22d8fbaf 100644 --- a/src/lib/git-hosts/NostrGitProvider.ts +++ b/src/lib/git-hosts/NostrGitProvider.ts @@ -62,7 +62,11 @@ export class NostrGitProvider implements GitHostProvider { const getDescription = () => event.tags.find((t: string[]) => t[0] === 'description')?.[1]; const getWebUrl = () => event.tags.find((t: string[]) => t[0] === 'web')?.[1] || ''; const getCloneUrl = () => event.tags.find((t: string[]) => t[0] === 'clone')?.[1] || ''; - const getRelays = () => event.tags.filter((t: string[]) => t[0] === 'relays').map((t: string[]) => t[1]); + // Per NIP-34, all relays are in a single tag: ["relays", "wss://...", "wss://...", ...] + const getRelays = () => { + const relayTag = event.tags.find((t: string[]) => t[0] === 'relays'); + return relayTag ? relayTag.slice(1) : []; + }; // Update relay to use repository's designated relays const repoRelays = getRelays(); @@ -88,9 +92,9 @@ export class NostrGitProvider implements GitHostProvider { // But only maintainers can apply them // Check if current user is listed as a maintainer const event = await this.queryRepo(owner, repo); - const maintainers = event.tags - .filter((t: string[]) => t[0] === 'maintainers') - .map((t: string[]) => t[1]); + // Per NIP-34, all maintainers are in a single tag: ["maintainers", "pubkey1", "pubkey2", ...] + const maintainerTag = event.tags.find((t: string[]) => t[0] === 'maintainers'); + const maintainers = maintainerTag ? maintainerTag.slice(1) : []; // Repo owner is always a maintainer const isMaintainer = owner === this.pubkey || maintainers.includes(this.pubkey); diff --git a/src/lib/git.test.ts b/src/lib/git.test.ts index 4e02a78e..ea147e3d 100644 --- a/src/lib/git.test.ts +++ b/src/lib/git.test.ts @@ -203,8 +203,7 @@ describe('Git', () => { content: '', tags: [ ['d', 'my-repo'], - ['clone', 'https://github.com/user/repo.git'], - ['clone', 'https://gitlab.com/user/repo.git'] + ['clone', 'https://github.com/user/repo.git', 'https://gitlab.com/user/repo.git'], ], created_at: Date.now(), sig: 'signature'