Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475// End-to-end detection against a stubbed network, using the shapes real// publications are served with: a well-known endpoint that answers with a// handle-form at-uri, and a publication record whose `icon` is a path string// rather than a blob ref. Both shapes are real and neither is what the lexicon// suggests, which is why they are pinned here rather than in a live capture.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
// The profile read is the one path that talks to an account's repo rather than// to a page: keep the identity and record reads controllable without stubbing// the whole network shape for every test here. Everything else stays real, so// detectPage keeps exercising the same fetch paths it always did.vi.mock('./atproto', async (importOriginal) => { const actual = await importOriginal<typeof import('./atproto')>() return { ...actual, resolveHandleToDid: vi.fn(actual.resolveHandleToDid), listRecords: vi.fn(actual.listRecords), }})
import { listRecords, resolveHandleToDid } from './atproto'import { detectPage, dropProbes, publicationForProfile, wellKnownUrl } from './detection'
const DID = 'did:plc:detectiontest0000000000'const URI = `at://${DID}/site.standard.publication/self`
/** The backing map of the current chrome.storage.session stub, for assertions. */let sessionStore = new Map<string, unknown>()
/** chrome.storage.session, which probeWellKnown uses as its probe cache. */function stubChromeStorage() { const store = new Map<string, unknown>() sessionStore = store vi.stubGlobal('chrome', { storage: { session: { get: async (key: string | null) => key === null ? Object.fromEntries(store) : store.has(key) ? { [key]: store.get(key) } : {}, set: async (items: Record<string, unknown>) => { for (const [k, v] of Object.entries(items)) store.set(k, v) }, remove: async (keys: string | string[]) => { for (const k of [keys].flat()) store.delete(k) }, }, }, })}
function stubNetwork(record: Record<string, unknown>) { const fetchMock = vi.fn(async (input: string) => { const url = String(input) if (url === 'https://pub.example/.well-known/site.standard.publication') { return new Response(`at://pub.example/site.standard.publication/self\n`) } if (url.includes('com.atproto.identity.resolveHandle')) { return new Response(JSON.stringify({ did: DID })) } if (url === `https://plc.directory/${DID}`) { return new Response( JSON.stringify({ alsoKnownAs: ['at://pub.example'], service: [ { id: '#atproto_pds', type: 'AtprotoPersonalDataServer', serviceEndpoint: 'https://pds.example.com', }, ], }), ) } if (url.includes('com.atproto.repo.getRecord')) { return new Response(JSON.stringify({ uri: URI, cid: 'bafcid', value: record })) } return new Response('not found', { status: 404, statusText: 'Not Found' }) }) vi.stubGlobal('fetch', fetchMock) return fetchMock}
const baseRecord = { $type: 'site.standard.publication', url: 'https://pub.example', name: 'Detection Fixture', description: 'wobbegong',}
beforeEach(() => { stubChromeStorage() // Calls, not implementations: every test starts from the real atproto // functions the factory wrapped, and any `...Once` left unconsumed was a // mistake in that test rather than state to carry here. vi.mocked(resolveHandleToDid).mockClear() vi.mocked(listRecords).mockClear()})afterEach(() => { vi.unstubAllGlobals()})
describe('detectPage', () => { it('verifies a publication whose well-known returns a handle-form at-uri', async () => { stubNetwork(baseRecord) const { pub } = await detectPage('https://pub.example/') expect(pub?.uri).toBe(URI) expect(pub?.verified).toBe(true) expect(pub?.handle).toBe('pub.example') expect(pub?.iconUrl).toBeUndefined() })
it('builds an icon url from a real blob ref', async () => { stubNetwork({ ...baseRecord, icon: { $type: 'blob', ref: { $link: 'bafkreiicon' }, mimeType: 'image/png', size: 9 }, }) const { pub } = await detectPage('https://pub.example/') expect(pub?.iconUrl).toBe( `https://pds.example.com/xrpc/com.atproto.sync.getBlob?did=${encodeURIComponent(DID)}&cid=bafkreiicon`, ) })
it('still detects when the record puts a path string in `icon`', async () => { // Regression: reading blob.ref.$link off a string threw, and the throw // surfaced as a fetch-failed error instead of a detected publication. stubNetwork({ ...baseRecord, icon: '/assets/images/favicon/apple-touch-icon.png' }) const { pub } = await detectPage('https://pub.example/') expect(pub?.verified).toBe(true) expect(pub?.iconUrl).toBeUndefined() })
it('carries the publication’s declared theme', async () => { stubNetwork({ ...baseRecord, basicTheme: { background: { $type: 'site.standard.theme.color#rgb', r: 253, g: 252, b: 249 }, foreground: { $type: 'site.standard.theme.color#rgb', r: 38, g: 40, b: 43 }, accent: { $type: 'site.standard.theme.color#rgb', r: 217, g: 72, b: 43 }, accentForeground: { $type: 'site.standard.theme.color#rgb', r: 255, g: 255, b: 255 }, }, }) const { pub } = await detectPage('https://pub.example/') expect(pub?.theme?.accent).toEqual({ r: 217, g: 72, b: 43 }) })
it('leaves an unreadable theme off the publication', async () => { stubNetwork({ ...baseRecord, basicTheme: { background: { $type: 'site.standard.theme.color#rgb', r: 255, g: 255, b: 255 }, foreground: { $type: 'site.standard.theme.color#rgb', r: 250, g: 250, b: 250 }, accent: { $type: 'site.standard.theme.color#rgb', r: 0, g: 0, b: 0 }, accentForeground: { $type: 'site.standard.theme.color#rgb', r: 255, g: 255, b: 255 }, }, }) const { pub } = await detectPage('https://pub.example/') expect(pub?.verified).toBe(true) expect(pub?.theme).toBeUndefined() })})
/** Counts well-known probes and answers every one of them with a miss. */function missingWellKnown() { const fetchSpy = vi.fn( async (_input: string) => new Response('', { status: 404, statusText: 'Not Found' }), ) vi.stubGlobal('fetch', fetchSpy) return fetchSpy}
describe('wellKnownUrl', () => { it('probes the origin for a root publication', () => { expect(wellKnownUrl('https://example.test')).toBe( 'https://example.test/.well-known/site.standard.publication', ) })
it('ignores a trailing slash', () => { expect(wellKnownUrl('https://example.test/')).toBe( 'https://example.test/.well-known/site.standard.publication', ) })
// atproto.com/blog is a real publication of this shape: the origin 404s and // only the path-suffixed probe answers. it('appends the base path for a publication below the root', () => { expect(wellKnownUrl('https://atproto.test/blog')).toBe( 'https://atproto.test/.well-known/site.standard.publication/blog', ) })
it('drops query and fragment', () => { expect(wellKnownUrl('https://example.test/blog?x=1#y')).toBe( 'https://example.test/.well-known/site.standard.publication/blog', ) })})
describe('well-known probe caching', () => { it('asks an origin once and serves later pages under it from cache', async () => { const fetchSpy = missingWellKnown() await detectPage('https://example.test/blog/one') const afterFirst = fetchSpy.mock.calls.length await detectPage('https://example.test/blog/two') await detectPage('https://example.test/blog/three') expect(fetchSpy.mock.calls.length).toBe(afterFirst) })
// The Refresh button existed but could not do the one thing it is for: a // publisher fixing their well-known kept seeing the cached miss. it('re-asks the origin when the caller forces a refresh', async () => { const fetchSpy = missingWellKnown() await detectPage('https://example.test/one') const afterFirst = fetchSpy.mock.calls.length await detectPage('https://example.test/one', undefined, undefined, true) expect(fetchSpy.mock.calls.length).toBeGreaterThan(afterFirst) })
it('collapses concurrent probes of one origin into a single request', async () => { const fetchSpy = missingWellKnown() await Promise.all([ detectPage('https://example.test/a'), detectPage('https://example.test/b'), detectPage('https://example.test/c'), ]) // Origin probe plus the one path-level probe each page triggers on a miss; // without dedupe this would be three times as many. expect(fetchSpy.mock.calls.length).toBeLessThanOrEqual(4) })
it('records a miss so a second visit does not re-ask', async () => { missingWellKnown() await detectPage('https://example.test/one') const keys = [...sessionStore.keys()].filter((k) => k.startsWith('wk:')) expect(keys.length).toBeGreaterThan(0) expect(sessionStore.get(keys[0]!)).toMatchObject({ uri: null }) })
// The dev build's Drop cache: this cache is the one nothing else empties, // since Refresh only bypasses it for the page it is walking. it('re-asks every origin after the probe answers are dropped', async () => { const fetchSpy = missingWellKnown() await detectPage('https://example.test/one') const afterFirst = fetchSpy.mock.calls.length
expect(await dropProbes()).toBeGreaterThan(0) expect([...sessionStore.keys()].filter((k) => k.startsWith('wk:'))).toEqual([])
await detectPage('https://example.test/one') expect(fetchSpy.mock.calls.length).toBeGreaterThan(afterFirst) })
it('leaves everything that is not a probe answer alone', async () => { missingWellKnown() // A per-tab detection state: the worker's, dropped by the worker, and not // something a sweep of this cache may take with it. sessionStore.set('tab:7', { url: 'https://example.test/one' }) await detectPage('https://example.test/one') await dropProbes() expect(sessionStore.get('tab:7')).toEqual({ url: 'https://example.test/one' }) })
it('reports no publication rather than throwing when every probe misses', async () => { missingWellKnown() await expect(detectPage('https://example.test/one')).resolves.toEqual({ pub: undefined, doc: undefined, }) })
/** * Documents a real cost rather than asserting a wish: on an origin with no * publication, each new *first path segment* earns its own probe, because a * publication may legitimately live under one path and not another. See * TODO.md — narrowing this needs a heuristic with false negatives. */ it('probes each distinct first path segment on an origin with no publication', async () => { const fetchSpy = missingWellKnown() await detectPage('https://example.test/one') const afterFirst = fetchSpy.mock.calls.length await detectPage('https://example.test/two') expect(fetchSpy.mock.calls.length).toBe(afterFirst + 1) })})
describe('path-prefix probes', () => { const deepUrl = 'https://pub.example/journal/2026' const deepUri = `at://${DID}/site.standard.publication/deep` const docUri = `at://${DID}/site.standard.document/doc`
function json(value: unknown) { return new Response(JSON.stringify(value)) }
function stubDeepPublication() { const fetchMock = vi.fn(async (input: string) => { const url = String(input) if (url.includes('com.atproto.identity.resolveHandle')) { return json({ did: DID }) } if (url === `https://plc.directory/${DID}`) { return json({ alsoKnownAs: ['at://pub.example'], service: [ { id: '#atproto_pds', type: 'AtprotoPersonalDataServer', serviceEndpoint: 'https://pds.example.com', }, ], }) } if (url.includes('com.atproto.repo.getRecord')) { if (url.includes(encodeURIComponent('site.standard.document'))) { return json({ uri: docUri, cid: 'bafdoc', value: { $type: 'site.standard.document', site: deepUrl, title: 'Deep', publishedAt: '2026-01-01T00:00:00.000Z', }, }) } return json({ uri: deepUri, cid: 'bafcid', value: { ...baseRecord, url: deepUrl } }) } if ( url === 'https://pub.example/.well-known/site.standard.publication/journal/2026' ) { return new Response(`${deepUri}\n`) } return new Response('', { status: 404, statusText: 'Not Found' }) }) vi.stubGlobal('fetch', fetchMock) return fetchMock }
it('follows a hinted page down to a publication below one path segment', async () => { const fetchMock = stubDeepPublication() const { pub } = await detectPage( 'https://pub.example/journal/2026/a-post', undefined, docUri, ) expect(pub?.uri).toBe(deepUri) expect(pub?.verified).toBe(true) const probed = fetchMock.mock.calls.map((call) => String(call[0])) expect(probed).toContain( 'https://pub.example/.well-known/site.standard.publication/journal/2026', ) })
it('stops below the first segment when the page carries no hint', async () => { const fetchSpy = missingWellKnown() await detectPage('https://example.test/one/two/three') const probed = fetchSpy.mock.calls.map((call) => String(call[0])) expect(probed).toContain( 'https://example.test/.well-known/site.standard.publication/one', ) expect(probed).not.toContain( 'https://example.test/.well-known/site.standard.publication/one/two', ) })})
describe('publicationForProfile', () => { const profileRecord = { ...baseRecord, createdAt: '2026-01-01T00:00:00.000Z', }
it('reads the account’s publication from its own repo, unverified', async () => { stubNetwork(baseRecord) vi.mocked(resolveHandleToDid).mockResolvedValueOnce(DID) vi.mocked(listRecords).mockResolvedValueOnce([ { uri: URI, cid: 'bafcid', value: profileRecord }, ])
const pub = await publicationForProfile('pub.example')
expect(pub?.uri).toBe(URI) expect(pub?.did).toBe(DID) expect(pub?.handle).toBe('pub.example') expect(pub?.pds).toBe('https://pds.example.com') // The profile is not the publication's verified home, even though every // read succeeded. expect(pub?.verified).toBe(false) expect(listRecords).toHaveBeenCalledWith( 'https://pds.example.com', DID, 'site.standard.publication', 10, ) })
it('builds the icon from a blob ref the same way detection does', async () => { stubNetwork(baseRecord) vi.mocked(listRecords).mockResolvedValueOnce([ { uri: URI, cid: 'bafcid', value: { ...profileRecord, icon: { $type: 'blob', ref: { $link: 'bafkreiicon' }, mimeType: 'image/png', size: 9 }, }, }, ])
const pub = await publicationForProfile(DID) expect(pub?.iconUrl).toBe( `https://pds.example.com/xrpc/com.atproto.sync.getBlob?did=${encodeURIComponent(DID)}&cid=bafkreiicon`, ) })
it('picks the newest record when the repo holds more than one', async () => { stubNetwork(baseRecord) const older = `at://${DID}/site.standard.publication/older` const newer = `at://${DID}/site.standard.publication/newer` vi.mocked(listRecords).mockResolvedValueOnce([ { uri: older, cid: 'bafold', value: { ...profileRecord, createdAt: '2025-01-01T00:00:00.000Z' }, }, { uri: newer, cid: 'bafnew', value: { ...profileRecord, createdAt: '2026-02-01T00:00:00.000Z' }, }, ])
const pub = await publicationForProfile(DID) expect(pub?.uri).toBe(newer) })
it('returns null when the account has no publication', async () => { stubNetwork(baseRecord) vi.mocked(listRecords).mockResolvedValueOnce([])
await expect(publicationForProfile(DID)).resolves.toBeNull() })
it('returns null when a read fails, without throwing', async () => { stubNetwork(baseRecord) vi.mocked(listRecords).mockRejectedValueOnce(new Error('pds down'))
await expect(publicationForProfile(DID)).resolves.toBeNull() })
it('returns null when the handle does not resolve', async () => { vi.mocked(resolveHandleToDid).mockRejectedValueOnce(new Error('no such handle'))
await expect(publicationForProfile('missing.example')).resolves.toBeNull() })
it('serves a second visit from the cache, not another repo read', async () => { stubNetwork(baseRecord) vi.mocked(listRecords).mockResolvedValueOnce([ { uri: URI, cid: 'bafcid', value: profileRecord }, ])
const first = await publicationForProfile(DID) const second = await publicationForProfile(DID)
expect(second).toEqual(first) expect(listRecords).toHaveBeenCalledTimes(1) })})