import assert from 'node:assert/strict' import { describe, it } from 'node:test' import { Secp256k1Keypair } from '@atproto/crypto' import { LABEL_VALUES, didDocument, diffLabels, labelsFromDataset, matchesUri, queryLabels, serviceRecord, signLabel, verifyLabel, } from '../src/labels.mjs' import { LabelStore } from '../src/store.mjs' const DATASET = { generatedAt: '2026-10-26T00:00:00.000Z', accounts: [ { did: 'did:plc:one', labels: ['multiple-blogs', 'blogged-recently'], bestPublicationPercentile: 0.5, publisherPercentile: 3, earlyAdopter: true, }, { did: 'did:plc:two', labels: ['stopped-blogging', 'not-a-label'], bestPublicationPercentile: 4, }, { did: 'did:plc:one', labels: ['multiple-blogs'] }, { labels: ['prolific'] }, ], } describe('labelsFromDataset', () => { it('deduplicates account values, derives percentile labels and uses the dataset timestamp', () => { const labels = labelsFromDataset(DATASET) assert.deepEqual( labels.map((label) => `${label.uri}|${label.val}`), [ 'did:plc:one|multiple-blogs', 'did:plc:one|blogged-recently', 'did:plc:one|early-adopter', 'did:plc:one|top-one-percent-publication', 'did:plc:one|top-five-percent-publisher', 'did:plc:two|stopped-blogging', 'did:plc:two|top-five-percent-publication', ], ) assert.equal(labels[0].cts, DATASET.generatedAt) }) it('ignores a value the service does not declare', () => { assert.ok(labelsFromDataset(DATASET).every((label) => LABEL_VALUES.includes(label.val))) }) it('answers an empty dataset with no labels', () => { assert.deepEqual(labelsFromDataset({}), []) assert.deepEqual(labelsFromDataset(null), []) }) }) describe('signing', () => { it('signs a label that verifies against the key', async () => { const keypair = await Secp256k1Keypair.create() const label = await signLabel( { uri: 'did:plc:one', val: 'multiple-blogs', cts: DATASET.generatedAt }, { src: 'did:web:labeler.test', keypair }, ) assert.equal(label.ver, 1) assert.equal(label.src, 'did:web:labeler.test') assert.equal(typeof label.sig, 'string') assert.equal(await verifyLabel(label, keypair.did()), true) }) it('does not verify after the value is changed', async () => { const keypair = await Secp256k1Keypair.create() const label = await signLabel( { uri: 'did:plc:one', val: 'multiple-blogs', cts: DATASET.generatedAt }, { src: 'did:web:labeler.test', keypair }, ) assert.equal(await verifyLabel({ ...label, val: 'prolific' }, keypair.did()), false) }) it('signs a negation with neg set', async () => { const keypair = await Secp256k1Keypair.create() const label = await signLabel( { uri: 'did:plc:one', val: 'multiple-blogs', cts: DATASET.generatedAt, neg: true }, { src: 'did:web:labeler.test', keypair }, ) assert.equal(label.neg, true) assert.equal(await verifyLabel(label, keypair.did()), true) }) }) describe('diffLabels', () => { it('reports additions and removes the values that disappeared as negations', () => { const previous = [ { uri: 'did:plc:one', val: 'blogged-recently', cts: 'old' }, { uri: 'did:plc:one', val: 'multiple-blogs', cts: 'old' }, ] const next = [{ uri: 'did:plc:one', val: 'multiple-blogs', cts: 'new' }] const { added, removed } = diffLabels(previous, next) assert.deepEqual(added, []) assert.deepEqual(removed, [ { uri: 'did:plc:one', val: 'blogged-recently', cts: 'old', neg: true }, ]) }) it('reports a new value', () => { const { added, removed } = diffLabels([], [{ uri: 'did:plc:one', val: 'prolific', cts: 'new' }]) assert.equal(added.length, 1) assert.deepEqual(removed, []) }) }) describe('matchesUri', () => { it('matches exact, prefix and wildcard patterns', () => { assert.equal(matchesUri('did:plc:abc', 'did:plc:abc'), true) assert.equal(matchesUri('did:plc:abc', 'did:plc:ab*'), true) assert.equal(matchesUri('did:plc:abc', 'did:plc:def*'), false) assert.equal(matchesUri('did:plc:abc', '*'), true) }) }) describe('queryLabels', () => { const labels = [ { src: 'did:web:labeler', uri: 'did:plc:one', val: 'a' }, { src: 'did:web:labeler', uri: 'did:plc:two', val: 'b' }, { src: 'did:web:other', uri: 'did:plc:one', val: 'c' }, ] it('filters by URI pattern and source', () => { assert.deepEqual( queryLabels(labels, { uriPatterns: ['did:plc:one'] }).labels.map((label) => label.val), ['a', 'c'], ) assert.deepEqual( queryLabels(labels, { uriPatterns: ['*'], sources: ['did:web:labeler'] }).labels.map( (label) => label.val, ), ['a', 'b'], ) }) it('paginates and returns the next cursor only when there is more', () => { const first = queryLabels(labels, { limit: 2 }) assert.equal(first.labels.length, 2) assert.equal(first.cursor, '2') const second = queryLabels(labels, { limit: 2, cursor: first.cursor }) assert.equal(second.labels.length, 1) assert.equal(second.cursor, undefined) }) it('clamps a silly limit', () => { assert.equal(queryLabels(labels, { limit: 10_000 }).labels.length, labels.length) }) }) describe('didDocument', () => { it('names the labeler endpoint and the repository server', () => { const doc = didDocument({ did: 'did:web:labeler.test', publicKeyMultibase: 'zKey', pds: 'https://pds.test', endpoint: 'https://labeler.test', }) assert.equal(doc.id, 'did:web:labeler.test') assert.equal(doc.verificationMethod[0].id, 'did:web:labeler.test#atproto') assert.ok(doc.service.some((service) => service.id === '#atproto_labeler')) assert.ok(doc.service.some((service) => service.id === '#atproto_pds')) }) it('omits the PDS when the deployment does not name one', () => { const doc = didDocument({ did: 'did:web:labeler.test', publicKeyMultibase: 'zKey', endpoint: 'https://labeler.test', }) assert.ok(!doc.service.some((service) => service.id === '#atproto_pds')) }) }) describe('serviceRecord', () => { it('declares every value the service emits', () => { const record = serviceRecord('2026-10-26T00:00:00.000Z') assert.equal(record.$type, 'app.bsky.labeler.service') assert.deepEqual(record.policies.labelValues, LABEL_VALUES) assert.equal(record.policies.labelValueDefinitions.length, LABEL_VALUES.length) }) }) describe('LabelStore', () => { it('numbers frames and replays the ones after a cursor', () => { const store = new LabelStore() store.append([{ uri: 'did:plc:one', val: 'a' }]) store.append([{ uri: 'did:plc:two', val: 'b' }]) assert.equal(store.seq, 2) assert.deepEqual( store.since(1).map((frame) => frame.seq), [2], ) }) it('wakes a subscriber with each new frame', () => { const store = new LabelStore() const seen = [] const unsubscribe = store.subscribe((frame) => seen.push(frame.seq)) store.append([{ uri: 'did:plc:one', val: 'a' }]) unsubscribe() store.append([{ uri: 'did:plc:two', val: 'b' }]) assert.deepEqual(seen, [1]) }) it('says nothing when there is nothing to append', () => { const store = new LabelStore() assert.equal(store.append([]), undefined) assert.equal(store.seq, 0) }) })