Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto
Something went wrong. Try again.
7.8 kB · 237 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238// Labels as the popup has to read them: Bluesky's interpretation for the// global values, the labeler's own for its own values, retractions actually// retracting, and an unreachable labeler costing nothing but a log.
import { afterEach, describe, expect, it, vi } from 'vitest'import type { Agent } from '@atproto/api'import { DEFAULT_LABELER_DID, type LabelerInfo, fetchLabels, labelerDids, labelsFor, queryLabelsUrl, subscribedLabelerDids, viewLabel, withDefaultLabeler,} from './labels'
const LABELER = 'did:plc:labeler0000000000000000'const ENDPOINT = 'https://mod.example.com'const PUB_DID = 'did:plc:publisher00000000000000'const PUB = `at://${PUB_DID}/site.standard.publication/self`
const bare: LabelerInfo = { did: LABELER, handle: 'mod.example.com', definitions: {} }
function label(val: string, uri = PUB_DID, extra: Record<string, unknown> = {}) { return { src: LABELER, uri, val, cts: '2026-08-12T00:00:00.000Z', ...extra }}
function stubChromeStorage(items: Record<string, unknown> = {}) { vi.stubGlobal('chrome', { storage: { local: { get: async (key: string) => (key in items ? { [key]: items[key] } : {}) } }, })}
afterEach(() => { vi.unstubAllGlobals()})
describe('labelerDids', () => { it('listens to Bluesky moderation until told otherwise', async () => { stubChromeStorage() expect(await labelerDids()).toEqual([DEFAULT_LABELER_DID]) })
it('lets the stored list win, including an empty one', async () => { stubChromeStorage({ labelers: [] }) expect(await labelerDids()).toEqual([]) })
it('drops anything in storage that is not a did', async () => { stubChromeStorage({ labelers: [LABELER, 'mod.example.com', 42] }) expect(await labelerDids()).toEqual([LABELER]) })})
describe('queryLabelsUrl', () => { it('asks one labeler about every subject at once', () => { const u = new URL(queryLabelsUrl(ENDPOINT, [PUB_DID, PUB])) expect(u.pathname).toBe('/xrpc/com.atproto.label.queryLabels') expect(u.searchParams.getAll('uriPatterns')).toEqual([PUB_DID, PUB]) })})
describe('viewLabel', () => { it('takes severity and blur for a global value from Bluesky, not from us', () => { const view = viewLabel(label('porn'), bare) expect(view.name).toBe('Adult Content') expect(view.severity).toBe('none') expect(view.hides).toBe(true) })
it('treats a value the labeler never defined as informational, not invisible', () => { const view = viewLabel(label('rumor-mill'), bare) expect(view.name).toBe('rumor-mill') expect(view.severity).toBe('inform') expect(view.hides).toBe(false) })
it('prefers the labeler’s own words for its own value', () => { const labeler: LabelerInfo = { ...bare, definitions: { 'rumor-mill': { identifier: 'rumor-mill', severity: 'alert', blurs: 'content', defaultSetting: 'warn', adultOnly: false, locales: [{ lang: 'en', name: 'Rumor Mill', description: 'Unsourced reporting.' }], // The rest of the interpreted shape is unused here. } as unknown as LabelerInfo['definitions'][string], }, } const view = viewLabel(label('rumor-mill'), labeler) expect(view.name).toBe('Rumor Mill') expect(view.description).toBe('Unsourced reporting.') expect(view.severity).toBe('alert') expect(view.hides).toBe(true) })})
describe('fetchLabels', () => { it('drops a label that the same labeler has since retracted', async () => { vi.stubGlobal( 'fetch', vi.fn( async () => new Response( JSON.stringify({ labels: [label('spam'), label('spam', PUB_DID, { neg: true }), label('nudity')], }), ), ), ) const labels = await fetchLabels(ENDPOINT, [PUB_DID]) expect(labels.map((l) => l.val)).toEqual(['nudity']) })})
describe('labelsFor', () => { it('orders alerts ahead of everything else', async () => { stubNetwork() const views = await labelsFor([PUB_DID, PUB], [LABELER]) expect(views.map((v) => v.value)).toEqual(['!hide', 'spam']) expect(views[0]?.hides).toBe(true) })
it('costs nothing when the labeler cannot be reached', async () => { vi.stubGlobal( 'fetch', vi.fn(async () => new Response('down', { status: 503, statusText: 'Unavailable' })), ) expect(await labelsFor([PUB_DID], [LABELER])).toEqual([]) })
it('asks nobody when the user listens to nobody', async () => { const fetchMock = vi.fn() vi.stubGlobal('fetch', fetchMock) expect(await labelsFor([PUB_DID], [])).toEqual([]) expect(fetchMock).not.toHaveBeenCalled() })})
/** A labeler whose DID document names both a PDS and a labeler service. */function stubNetwork() { vi.stubGlobal( 'fetch', vi.fn(async (input: string) => { const url = String(input) if (url === `https://plc.directory/${LABELER}`) { return new Response( JSON.stringify({ alsoKnownAs: ['at://mod.example.com'], service: [ { id: '#atproto_pds', type: 'AtprotoPersonalDataServer', serviceEndpoint: 'https://pds.example.com', }, { id: '#atproto_labeler', type: 'AtprotoLabeler', serviceEndpoint: ENDPOINT, }, ], }), ) } if (url.includes('com.atproto.repo.getRecord')) { return new Response( JSON.stringify({ uri: `at://${LABELER}/app.bsky.labeler.service/self`, cid: 'bafcid', value: { policies: { labelValues: ['spam'] } }, }), ) } if (url.includes('com.atproto.label.queryLabels')) { return new Response(JSON.stringify({ labels: [label('spam'), label('!hide')] })) } return new Response('not found', { status: 404, statusText: 'Not Found' }) }), )}
/** Just the one call subscribedLabelerDids makes. */function agentWith(preferences: unknown[]): Agent { return { app: { bsky: { actor: { getPreferences: async () => ({ data: { preferences } }) } } }, } as unknown as Agent}
describe('subscribedLabelerDids', () => { it('reads the account’s own labeler subscriptions out of its preferences', async () => { const dids = await subscribedLabelerDids( agentWith([ { $type: 'app.bsky.actor.defs#savedFeedsPrefV2', items: [] }, { $type: 'app.bsky.actor.defs#labelersPref', labelers: [{ did: LABELER }, { did: 'did:plc:another0000000000000000' }], }, ]), ) expect(dids).toEqual([LABELER, 'did:plc:another0000000000000000']) })
it('is empty for an account that has subscribed to none', async () => { expect(await subscribedLabelerDids(agentWith([]))).toEqual([]) })
it('drops entries that are not dids, since preferences are user-written', async () => { const dids = await subscribedLabelerDids( agentWith([ { $type: 'app.bsky.actor.defs#labelersPref', labelers: [{ did: LABELER }, { did: 'mod.example.com' }, {}, { did: 42 }], }, ]), ) expect(dids).toEqual([LABELER]) })})
describe('withDefaultLabeler', () => { it('always asks Bluesky moderation, which cannot be unsubscribed there', () => { expect(withDefaultLabeler([LABELER])).toEqual([DEFAULT_LABELER_DID, LABELER]) expect(withDefaultLabeler([])).toEqual([DEFAULT_LABELER_DID]) })
it('does not ask it twice when it is in the subscriptions too', () => { expect(withDefaultLabeler([DEFAULT_LABELER_DID, LABELER])).toEqual([ DEFAULT_LABELER_DID, LABELER, ]) })})