import { describe, expect, it, vi } from 'vitest' import { verifyPublication } from './verify' import type { PubInfo } from './types' const pub: PubInfo = { uri: 'at://did:plc:publisher/site.standard.publication/self', did: 'did:plc:publisher', handle: 'pub.example.com', pds: 'https://pds.example.com', record: { $type: 'site.standard.publication', url: 'https://pub.example.com/blog', name: 'A publication', }, verified: true, } const probe = (answer: string | null) => vi.fn(async () => answer) describe('verifyPublication', () => { it('passes every step when the site names the record', async () => { const report = await verifyPublication(pub, 'https://pub.example.com/blog/a-post', { probe: probe(pub.uri), }) expect(report.steps.map((s) => [s.id, s.status])).toEqual([ ['record', 'pass'], ['home', 'pass'], ['well-known', 'pass'], ['match', 'pass'], ]) expect(report.verified).toBe(true) expect(report.steps[2]?.href).toBe( 'https://pub.example.com/.well-known/site.standard.publication/blog', ) }) it('fails the home step when the record points at another origin', async () => { const report = await verifyPublication(pub, 'https://reader.example/a-post', { probe: probe(pub.uri), }) expect(report.steps.find((s) => s.id === 'home')?.status).toBe('fail') expect(report.verified).toBe(false) }) it('fails the match step when the site names a different record', async () => { const report = await verifyPublication(pub, 'https://pub.example.com/blog/a-post', { probe: probe('at://did:plc:other/site.standard.publication/self'), }) expect(report.steps.find((s) => s.id === 'match')?.status).toBe('fail') expect(report.steps.find((s) => s.id === 'match')?.detail).toContain('at://did:plc:other') }) it('fails the well-known and match steps when the site is silent', async () => { const report = await verifyPublication(pub, 'https://pub.example.com/blog/a-post', { probe: probe(null), }) expect(report.steps.find((s) => s.id === 'well-known')?.status).toBe('fail') expect(report.steps.find((s) => s.id === 'match')?.status).toBe('fail') expect(report.verified).toBe(false) }) it('fails the home step and asks nothing for an unusable url', async () => { const ask = probe(pub.uri) const report = await verifyPublication( { ...pub, record: { ...pub.record, url: 'javascript:alert(1)' } }, 'https://pub.example.com/blog/a-post', { probe: ask }, ) expect(report.steps.find((s) => s.id === 'home')?.status).toBe('fail') expect(report.steps.find((s) => s.id === 'well-known')?.status).toBe('fail') expect(ask).not.toHaveBeenCalled() }) it('passes force through to the probe', async () => { const ask = probe(pub.uri) await verifyPublication(pub, 'https://pub.example.com/blog/a-post', { probe: ask, force: true }) expect(ask).toHaveBeenCalledWith('https://pub.example.com/blog', true) }) it('treats a probe that throws as no answer', async () => { const ask = vi.fn(async () => { throw new Error('down') }) const report = await verifyPublication(pub, 'https://pub.example.com/blog/a-post', { probe: ask }) expect(report.steps.find((s) => s.id === 'well-known')?.status).toBe('fail') }) })