mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-03 07:57:22 +00:00
The nsite manifest tag table has `path`, `x`, `d`, `a`, `A`, `server`, `title`, `description`, and `source`, and nothing for saying which relays the event went to. Ours went out with one per configured relay anyway, apparently by symmetry with `server` — but a `server` tag answers a question the host server actually asks when fetching a blob, and nothing in the spec or in this codebase ever read the relay hints back. So they are gone from the deploy path, and gone from the two places the npanel migration re-signs somebody else's manifest: republishing no longer strips the archive's relay tags only to write its own in their place, and retitling no longer passes through whatever an older deploy of ours left behind. Re-signing an event is the moment to stop carrying a tag that does not exist. Where to find the manifest stays the reader's problem and the author's kind 10002, which is where NIP-5A leaves it.
233 lines
9.2 KiB
TypeScript
233 lines
9.2 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
||
import type { NostrEvent } from '@nostrify/nostrify';
|
||
import type { NpanelClaim } from './npanelApi';
|
||
import {
|
||
NAMED_SITE_KIND,
|
||
ROOT_SITE_KIND,
|
||
hasArchiveTitle,
|
||
isServableHostname,
|
||
nsitePreviewUrl,
|
||
planClaim,
|
||
republishTags,
|
||
retitleTags,
|
||
sitePreviewUrl,
|
||
} from './npanelMigration';
|
||
|
||
const PUBKEY = 'a'.repeat(64);
|
||
const ARCHIVE = 'b'.repeat(64);
|
||
const DOMAIN = 'shakespeare.wtf';
|
||
|
||
function claim(overrides: Partial<NpanelClaim> = {}): NpanelClaim {
|
||
return {
|
||
hostname: 'mysite.shakespeare.wtf',
|
||
source: 'picohost',
|
||
live: true,
|
||
address: `${NAMED_SITE_KIND}:${ARCHIVE}:mysite`,
|
||
mine: false,
|
||
manifest: {
|
||
kind: NAMED_SITE_KIND,
|
||
content: '',
|
||
tags: [
|
||
['d', 'mysite'],
|
||
['title', 'mysite.shakespeare.wtf'],
|
||
['alt', 'mysite.shakespeare.wtf — archived so that it keeps working.'],
|
||
['path', '/index.html', 'c'.repeat(64)],
|
||
['server', 'https://blossom.ditto.pub/'],
|
||
],
|
||
},
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
describe('planClaim', () => {
|
||
it('only changes the record for a site already on the owner’s key', () => {
|
||
const address = `${NAMED_SITE_KIND}:${PUBKEY}:mysite`;
|
||
|
||
expect(
|
||
planClaim(claim({ mine: true, address, manifest: null }), PUBKEY, new Set(), DOMAIN),
|
||
).toEqual({ kind: 'claim', address, reason: 'mine' });
|
||
});
|
||
|
||
it('points a name at a site the owner already publishes, rather than republishing over it', () => {
|
||
// The destructive case: kind 35128 is addressable, so publishing the
|
||
// archive's stale copy at this `d` tag would replace the live site.
|
||
expect(planClaim(claim(), PUBKEY, new Set(['mysite']), DOMAIN)).toEqual({
|
||
kind: 'claim',
|
||
address: `${NAMED_SITE_KIND}:${PUBKEY}:mysite`,
|
||
reason: 'published',
|
||
});
|
||
});
|
||
|
||
it('republishes the archived manifest when nothing is published under that name', () => {
|
||
const step = planClaim(claim(), PUBKEY, new Set(['something-else']), DOMAIN);
|
||
|
||
expect(step.kind).toBe('republish');
|
||
expect(step).toMatchObject({ identifier: 'mysite' });
|
||
});
|
||
|
||
it('refuses a root site, which would replace the owner’s main site', () => {
|
||
const rooted = claim({ manifest: { kind: 15128, content: '', tags: [] } });
|
||
|
||
expect(planClaim(rooted, PUBKEY, new Set(), DOMAIN).kind).toBe('blocked');
|
||
});
|
||
|
||
it('has nothing to do for a reservation whose site never made it across', () => {
|
||
const empty = claim({ live: false, address: null, manifest: null });
|
||
|
||
expect(planClaim(empty, PUBKEY, new Set(), DOMAIN).kind).toBe('blocked');
|
||
});
|
||
|
||
it('refuses a name no certificate can cover, however it got on the list', () => {
|
||
// A wildcard covers one label. The host this migration inherits from took
|
||
// these anyway, so five of them are on file and none can serve TLS.
|
||
const deep = claim({ hostname: 'webvoice.shakespeare.wtf.shakespeare.wtf', mine: true });
|
||
|
||
const step = planClaim(deep, PUBKEY, new Set(), DOMAIN);
|
||
expect(step.kind).toBe('blocked');
|
||
expect(step).toMatchObject({ reason: expect.stringContaining('certificate') });
|
||
});
|
||
});
|
||
|
||
describe('isServableHostname', () => {
|
||
it('accepts exactly one label under the domain', () => {
|
||
expect(isServableHostname('mysite.shakespeare.wtf', DOMAIN)).toBe(true);
|
||
expect(isServableHostname('My-Site.Shakespeare.WTF', DOMAIN)).toBe(true);
|
||
expect(isServableHostname('a.b.shakespeare.wtf', DOMAIN)).toBe(false);
|
||
expect(isServableHostname('shakespeare.wtf', DOMAIN)).toBe(false);
|
||
expect(isServableHostname('mysite.example.com', DOMAIN)).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe('nsitePreviewUrl', () => {
|
||
it('addresses a named site by its base36 pubkey and identifier', () => {
|
||
expect(nsitePreviewUrl(`${NAMED_SITE_KIND}:${ARCHIVE}:mysite`)).toMatch(/^https:\/\/[0-9a-z]{50}mysite\.nsite\.lol$/);
|
||
});
|
||
|
||
it('addresses a root site by its npub', () => {
|
||
expect(nsitePreviewUrl(`${ROOT_SITE_KIND}:${ARCHIVE}:`)).toMatch(/^https:\/\/npub1[0-9a-z]+\.nsite\.lol$/);
|
||
});
|
||
|
||
it('has nowhere to send an identifier that will not fit a DNS label', () => {
|
||
// About a quarter of what this migration carries: the host it came from
|
||
// named sites after projects and never had to fit one in a subdomain.
|
||
expect(nsitePreviewUrl(`${NAMED_SITE_KIND}:${ARCHIVE}:nostr-pro-file-manager`)).toBeUndefined();
|
||
// Uppercase resolves as a DNS label and then matches no `d` tag, which is
|
||
// worse than no link at all.
|
||
expect(nsitePreviewUrl(`${NAMED_SITE_KIND}:${ARCHIVE}:ErrorTime`)).toBeUndefined();
|
||
});
|
||
|
||
it('says nothing about an address it cannot read', () => {
|
||
expect(nsitePreviewUrl(null)).toBeUndefined();
|
||
expect(nsitePreviewUrl(undefined)).toBeUndefined();
|
||
expect(nsitePreviewUrl(`${NAMED_SITE_KIND}:not-a-pubkey:mysite`)).toBeUndefined();
|
||
expect(nsitePreviewUrl(`${NAMED_SITE_KIND}:${ARCHIVE}:`)).toBeUndefined();
|
||
});
|
||
});
|
||
|
||
describe('sitePreviewUrl', () => {
|
||
it('prefers the address, which outlives both the name and its certificate', () => {
|
||
expect(sitePreviewUrl(`${NAMED_SITE_KIND}:${ARCHIVE}:mysite`, 'mysite.shakespeare.wtf', DOMAIN)).toMatch(
|
||
/\.nsite\.lol$/,
|
||
);
|
||
});
|
||
|
||
it('falls back to the hostname when no gateway label can hold the identifier', () => {
|
||
expect(
|
||
sitePreviewUrl(`${NAMED_SITE_KIND}:${ARCHIVE}:nostr-pro-file-manager`, 'long.shakespeare.wtf', DOMAIN),
|
||
).toBe('https://long.shakespeare.wtf');
|
||
});
|
||
|
||
it('offers nothing for a name that is neither servable nor addressable', () => {
|
||
// A link to a certificate error is worse than no button.
|
||
expect(sitePreviewUrl(null, 'a.b.shakespeare.wtf', DOMAIN)).toBeUndefined();
|
||
expect(
|
||
sitePreviewUrl(`${NAMED_SITE_KIND}:${ARCHIVE}:nostr-pro-file-manager`, 'a.b.shakespeare.wtf', DOMAIN),
|
||
).toBeUndefined();
|
||
});
|
||
});
|
||
|
||
describe('republishTags', () => {
|
||
it('keeps the files and where to fetch them, and takes the archive’s words', () => {
|
||
const manifest = claim().manifest;
|
||
if (!manifest) throw new Error('fixture has a manifest');
|
||
|
||
const tags = republishTags(
|
||
{ ...manifest, tags: [...manifest.tags, ['relay', 'wss://gone.example']] },
|
||
'mysite',
|
||
{ title: 'My Site', description: 'What it is for.' },
|
||
);
|
||
|
||
expect(tags[0]).toEqual(['d', 'mysite']);
|
||
expect(tags).toContainEqual(['path', '/index.html', 'c'.repeat(64)]);
|
||
expect(tags).toContainEqual(['server', 'https://blossom.ditto.pub/']);
|
||
// NIP-5A has no relay-hint tag, so the archive's are dropped and none of
|
||
// ours take their place.
|
||
expect(tags.some(([name]) => name === 'relay' || name === 'r')).toBe(false);
|
||
expect(tags.filter(([name]) => name === 'd')).toHaveLength(1);
|
||
|
||
// The archive's title was the hostname and its `alt` says the site is
|
||
// archived, which stops being true the moment it comes home.
|
||
expect(tags).toContainEqual(['title', 'My Site']);
|
||
expect(tags).toContainEqual(['description', 'What it is for.']);
|
||
expect(tags.filter(([name]) => name === 'title')).toHaveLength(1);
|
||
expect(tags.some(([name]) => name === 'alt')).toBe(false);
|
||
});
|
||
|
||
it('leaves a site that never said what it is called untitled', () => {
|
||
const manifest = claim().manifest;
|
||
if (!manifest) throw new Error('fixture has a manifest');
|
||
|
||
// Roughly a third of these are shells that set their title from JavaScript.
|
||
// No title is what an nsite manifest without one has always meant, and is
|
||
// better than a domain wearing a name's clothes.
|
||
const tags = republishTags(manifest, 'mysite', {});
|
||
|
||
expect(tags.some(([name]) => name === 'title')).toBe(false);
|
||
expect(tags).toContainEqual(['path', '/index.html', 'c'.repeat(64)]);
|
||
});
|
||
});
|
||
|
||
describe('retitleTags', () => {
|
||
it('swaps the archive’s words, drops the tags NIP-5A never had, and passes the rest through', () => {
|
||
const event = {
|
||
id: 'e'.repeat(64),
|
||
pubkey: PUBKEY,
|
||
kind: NAMED_SITE_KIND,
|
||
created_at: 1,
|
||
content: '',
|
||
sig: '0'.repeat(128),
|
||
tags: [
|
||
['d', 'mysite'],
|
||
['title', 'mysite.shakespeare.wtf'],
|
||
['path', '/index.html', 'c'.repeat(64)],
|
||
['server', 'https://blossom.ditto.pub/'],
|
||
['relay', 'wss://relay.ditto.pub'],
|
||
],
|
||
} as NostrEvent;
|
||
|
||
const tags = retitleTags(event, { title: 'My Site' });
|
||
|
||
expect(tags).toContainEqual(['d', 'mysite']);
|
||
// Built from the event as relays hold it, so a site redeployed since this
|
||
// migration ran keeps whatever it has become.
|
||
expect(tags).toContainEqual(['path', '/index.html', 'c'.repeat(64)]);
|
||
expect(tags).toContainEqual(['server', 'https://blossom.ditto.pub/']);
|
||
// Except the relay hints an older deploy wrote, which re-signing is the
|
||
// chance to stop carrying.
|
||
expect(tags.some(([name]) => name === 'relay')).toBe(false);
|
||
expect(tags).toContainEqual(['title', 'My Site']);
|
||
expect(tags.filter(([name]) => name === 'title')).toHaveLength(1);
|
||
});
|
||
});
|
||
|
||
describe('hasArchiveTitle', () => {
|
||
const event = (title: string) =>
|
||
({ tags: [['d', 'mysite'], ['title', title]] }) as NostrEvent;
|
||
|
||
it('recognises a title that is really an address', () => {
|
||
expect(hasArchiveTitle(event('mysite.shakespeare.wtf'), 'mysite.shakespeare.wtf')).toBe(true);
|
||
expect(hasArchiveTitle(event('My Site'), 'mysite.shakespeare.wtf')).toBe(false);
|
||
expect(hasArchiveTitle({ tags: [] } as unknown as NostrEvent, 'mysite.shakespeare.wtf')).toBe(false);
|
||
});
|
||
});
|