mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Three things the migration got wrong, all of them the same shape: something was known and not said. The archive titled every site it carried after the site's hostname, because an address was all it knew about a site it had only ever seen at one. Handing that back hands back an address where a name should be — a site called "Arguable" comes home titled "arguable.shakespeare.wtf". The gateway can now read the name the site was carrying all along, out of its webmanifest or the metadata it publishes for link previews, and that is what gets signed. A site that never said what it is called gets no title, which is what a manifest without one has always meant. Its `alt` goes too: it says the site is archived so that it keeps working, which is a true sentence about the archive and stops being true here. Names already taken back are listed and fixed the same way, rebuilt from the event as relays hold it rather than from the gateway's copy — a site redeployed since the migration ran has moved on, and rebuilding it from what the gateway fetched would quietly restore an older version of it. Publishing to relays went through a pool that resolves on the first acceptance and discards every other answer, so a manifest that reached one relay out of five was reported exactly like one that reached all five, and the reasons the other four refused were received and thrown away. Of 102 manifests published this way in one sitting, one relay held every one and another held none, and nothing anywhere said so. Every relay is now published to on its own and reported on by name. One acceptance is still a success — the event exists and whoever reads that relay will find it — so this is a note, not a failure. The gaps that leaves are closed without signing anything: the events go out again exactly as they are, ids and signatures unchanged, to the relays whose own answer says they are missing them. A relay that already has one says duplicate and nothing happens; a relay that refused before finally says why, which is the only way to hear a reason that was discarded at the time. Ordinary nsite deploys published the same blind way, so they use the same helper and carry the same coverage. Lastly, names more than one label under the gateway's domain are refused rather than published for. A wildcard certificate covers one label, so `a.b.example.com` can be claimed, published for, and will still fail its handshake for everyone forever. Five are on file, inherited from a host that accepted them; one already has a manifest published for it, which can now be retracted.
179 lines
6.8 KiB
TypeScript
179 lines
6.8 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
||
import type { NostrEvent } from '@nostrify/nostrify';
|
||
import type { NpanelClaim } from './npanelApi';
|
||
import {
|
||
NAMED_SITE_KIND,
|
||
hasArchiveTitle,
|
||
isServableHostname,
|
||
planClaim,
|
||
republishTags,
|
||
retitleTags,
|
||
} 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('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',
|
||
['wss://relay.ditto.pub'],
|
||
{ 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/']);
|
||
expect(tags).toContainEqual(['relay', 'wss://relay.ditto.pub']);
|
||
expect(tags).not.toContainEqual(['relay', 'wss://gone.example']);
|
||
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 and passes everything else 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']);
|
||
expect(tags).toContainEqual(['path', '/index.html', 'c'.repeat(64)]);
|
||
// Built from the event as relays hold it, so a site redeployed since this
|
||
// migration ran keeps whatever it has become.
|
||
expect(tags).toContainEqual(['relay', 'wss://relay.ditto.pub']);
|
||
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);
|
||
});
|
||
});
|