Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187import type { Agent } from '@atproto/api'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { createSubscription, deleteSubscription, rkeysForPublication, summarizePublication,} from './subscription'
let session = new Map<string, unknown>()
function area(backing: Map<string, unknown>) { return { get: async (keys: string | null) => { if (keys === null) return Object.fromEntries(backing) return backing.has(keys) ? { [keys]: backing.get(keys) } : {} }, set: async (items: Record<string, unknown>) => { for (const [k, v] of Object.entries(items)) backing.set(k, v) }, remove: async (keys: string | string[]) => { for (const k of [keys].flat()) backing.delete(k) }, }}
beforeEach(() => { session = new Map() vi.stubGlobal('chrome', { storage: { session: area(session), local: area(new Map()) } })})
afterEach(() => { vi.unstubAllGlobals()})
function fakeAgent(uri = 'at://did:plc:me/site.standard.graph.subscription/3knew') { const createRecord = vi.fn(async () => ({ data: { uri, cid: 'cid' } })) const deleteRecord = vi.fn(async () => ({})) return { agent: { com: { atproto: { repo: { createRecord, deleteRecord } } } } as unknown as Agent, createRecord, deleteRecord, }}
const SUB_KEY = 'own:did:plc:me/subs'
describe('createSubscription', () => { it('writes the popup-compatible shape and returns the new rkey', async () => { const { agent, createRecord } = fakeAgent() const entry = await createSubscription( agent, 'did:plc:me', 'at://did:plc:pub/site.standard.publication/self', '2026-02-03T04:05:06.000Z', )
expect(createRecord).toHaveBeenCalledWith({ repo: 'did:plc:me', collection: 'site.standard.graph.subscription', record: { $type: 'site.standard.graph.subscription', publication: 'at://did:plc:pub/site.standard.publication/self', createdAt: '2026-02-03T04:05:06.000Z', }, }) expect(entry).toEqual({ uri: 'at://did:plc:me/site.standard.graph.subscription/3knew', rkey: '3knew', publication: 'at://did:plc:pub/site.standard.publication/self', createdAt: '2026-02-03T04:05:06.000Z', }) })
it('defaults createdAt to now, so Undo can pass the original date', async () => { const { agent } = fakeAgent() const before = Date.now() const entry = await createSubscription( agent, 'did:plc:me', 'at://did:plc:pub/site.standard.publication/self', ) expect(Date.parse(entry.createdAt!)).toBeGreaterThanOrEqual(before) })
it('drops the worker’s own subscription cache after the write', async () => { session.set(SUB_KEY, { at: Date.now(), value: { byPub: {} } }) const { agent } = fakeAgent() await createSubscription(agent, 'did:plc:me', 'at://did:plc:pub/site.standard.publication/self') expect(session.has(SUB_KEY)).toBe(false) })
it('surfaces an unexpected created uri rather than returning a fake rkey', async () => { const { agent } = fakeAgent('https://not-an-at-uri') await expect( createSubscription(agent, 'did:plc:me', 'at://did:plc:pub/site.standard.publication/self'), ).rejects.toThrow(/Unexpected subscription uri/) })})
describe('deleteSubscription', () => { it('deletes by rkey and drops the worker’s own subscription cache', async () => { const { agent, deleteRecord } = fakeAgent() session.set(SUB_KEY, { at: Date.now(), value: { byPub: {} } })
await deleteSubscription(agent, 'did:plc:me', '3kold')
expect(deleteRecord).toHaveBeenCalledWith({ repo: 'did:plc:me', collection: 'site.standard.graph.subscription', rkey: '3kold', }) expect(session.has(SUB_KEY)).toBe(false) })})
describe('rkeysForPublication', () => { it('returns every rkey that points at the publication, including duplicates', () => { const publication = 'at://did:plc:pub/site.standard.publication/self' expect( rkeysForPublication( [ { uri: 'at://did:plc:me/site.standard.graph.subscription/3ka', rkey: '3ka', publication }, { uri: 'at://did:plc:me/site.standard.graph.subscription/3kb', rkey: '3kb', publication }, { uri: 'at://did:plc:me/site.standard.graph.subscription/3kc', rkey: '3kc', publication: 'at://did:plc:other/site.standard.publication/other', }, ], publication, ), ).toEqual(['3ka', '3kb']) })
it('returns nothing when the publication has no record', () => { expect(rkeysForPublication([], 'at://did:plc:pub/site.standard.publication/self')).toEqual([]) })})
describe('summarizePublication', () => { it('keeps the linkable site, host, description and blob icon', () => { const summary = summarizePublication( 'at://did:plc:pub/site.standard.publication/self', 'https://pds.example.com', 'did:plc:pub', { $type: 'site.standard.publication', url: 'https://blog.example.com/', name: 'Example Blog', description: 'Notes', icon: { $type: 'blob', ref: { $link: 'bafyicon' }, mimeType: 'image/png', size: 10, }, }, ) expect(summary).toEqual({ uri: 'at://did:plc:pub/site.standard.publication/self', name: 'Example Blog', description: 'Notes', url: 'https://blog.example.com/', host: 'blog.example.com', iconUrl: 'https://pds.example.com/xrpc/com.atproto.sync.getBlob?did=did%3Aplc%3Apub&cid=bafyicon', }) })
it('refuses a non-http site and falls back from a missing name', () => { const summary = summarizePublication( 'at://did:plc:pub/site.standard.publication/self', 'https://pds.example.com', 'did:plc:pub', { $type: 'site.standard.publication', url: 'javascript:alert(1)', name: ' ', }, ) expect(summary.url).toBeUndefined() expect(summary.host).toBeUndefined() expect(summary.name).toBe('did:plc:pub') })})