// End-to-end: start the real service with a real key and a fixture dataset, // then use the two endpoints a labeler must serve. The subscription is read // with the same frame decoder a client uses. import assert from 'node:assert/strict' import { spawn } from 'node:child_process' import { mkdtempSync, writeFileSync } from 'node:fs' import { createServer as createNetServer } from 'node:net' import { tmpdir } from 'node:os' import { join } from 'node:path' import { after, before, describe, it } from 'node:test' import { Secp256k1Keypair } from '@atproto/crypto' import { Frame } from '@atproto/xrpc-server' import { verifyLabel } from '../src/labels.mjs' const DID = 'did:web:labeler.test' let child let keypair let datasetDir let PORT let BASE /** Ask the kernel for an unused port, then hand it to the child. */ async function freePort() { const server = createNetServer() await new Promise((resolve) => server.listen(0, resolve)) const { port } = server.address() await new Promise((resolve) => server.close(resolve)) return port } before(async () => { PORT = await freePort() BASE = `http://127.0.0.1:${PORT}` keypair = await Secp256k1Keypair.create({ exportable: true }) const hex = Buffer.from(await keypair.export()).toString('hex') datasetDir = mkdtempSync(join(tmpdir(), 'stansite-labeler-')) const dataset = join(datasetDir, 'data.json') writeFileSync( dataset, JSON.stringify({ generatedAt: '2026-10-26T00:00:00.000Z', accounts: [ { did: 'did:plc:one', labels: ['multiple-blogs', 'blogged-recently'] }, { did: 'did:plc:two', labels: ['stopped-blogging'] }, ], }), ) child = spawn(process.execPath, ['src/server.mjs'], { cwd: join(import.meta.dirname, '..'), env: { ...process.env, LABELER_DID: DID, LABELER_SIGNING_KEY: hex, LABELER_PDS: 'https://pds.test', PANOPTIBLOG_DATA_FILE: dataset, PORT: String(PORT), }, stdio: ['ignore', 'pipe', 'inherit'], }) await waitForHealth() }) after(() => { child?.kill('SIGTERM') }) async function waitForHealth() { for (let i = 0; i < 100; i++) { try { const res = await fetch(`${BASE}/health`) if (res.ok) return } catch { // not up yet } await new Promise((resolve) => setTimeout(resolve, 50)) } throw new Error('labeler did not start') } describe('the labeler service', () => { it('serves a DID document naming the labeler and its PDS', async () => { const doc = await (await fetch(`${BASE}/.well-known/did.json`)).json() assert.equal(doc.id, DID) assert.equal(doc.verificationMethod[0].publicKeyMultibase, keypair.did().slice('did:key:'.length)) assert.ok(doc.service.some((service) => service.id === '#atproto_labeler')) assert.ok(doc.service.some((service) => service.id === '#atproto_pds')) }) it('serves signed labels that verify against the key', async () => { const body = await ( await fetch(`${BASE}/xrpc/com.atproto.label.queryLabels?uriPatterns=*&limit=50`) ).json() assert.equal(body.labels.length, 3) for (const label of body.labels) { assert.equal(await verifyLabel(label, keypair.did()), true, label.val) } }) it('filters by account and paginates', async () => { const first = await ( await fetch( `${BASE}/xrpc/com.atproto.label.queryLabels?uriPatterns=did:plc:one&limit=1`, ) ).json() assert.equal(first.labels.length, 1) assert.equal(first.cursor, '1') const second = await ( await fetch( `${BASE}/xrpc/com.atproto.label.queryLabels?uriPatterns=did:plc:one&limit=1&cursor=1`, ) ).json() assert.equal(second.labels.length, 1) assert.equal(second.cursor, undefined) }) it('streams the labels to a subscriber', async () => { const frame = await new Promise((resolve, reject) => { const ws = new WebSocket(`${BASE.replace('http', 'ws')}/xrpc/com.atproto.label.subscribeLabels`) ws.binaryType = 'arraybuffer' const timer = setTimeout(() => reject(new Error('subscription timed out')), 5_000) ws.addEventListener('message', (event) => { clearTimeout(timer) ws.close() resolve(Frame.fromBytes(new Uint8Array(event.data))) }) ws.addEventListener('error', () => reject(new Error('subscription failed'))) }) assert.equal(frame.isMessage(), true) assert.equal(frame.type, '#labels') assert.equal(frame.body.seq, 1) assert.equal(frame.body.labels.length, 3) }) it('rejects a subscription cursor from the future', async () => { const frame = await new Promise((resolve, reject) => { const ws = new WebSocket( `${BASE.replace('http', 'ws')}/xrpc/com.atproto.label.subscribeLabels?cursor=999`, ) ws.binaryType = 'arraybuffer' const timer = setTimeout(() => reject(new Error('subscription timed out')), 5_000) ws.addEventListener('message', (event) => { clearTimeout(timer) ws.close() resolve(Frame.fromBytes(new Uint8Array(event.data))) }) ws.addEventListener('error', () => reject(new Error('subscription failed'))) }) assert.equal(frame.isError(), true) assert.equal(frame.code, 'FutureCursor') }) })