Fix repo announcement event structure

This commit is contained in:
DanConwayDev committed 2025-10-30 15:11:47 +00:00
1 parent 18456f2293
commit 430806fbf9
5 files changed
+159 -21

No files matched your search

@@ -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']);
});
});
+21 -12
View File
@@ -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()) {
+5 -3
View File
@@ -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');