OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-room / test / label-room.test.mjs

81 lines · javascript ref:
// LabelRoom — co-authored labeler assembly over a shared (mock) StateBase: N mods append intents; the
// LEASE-holder signs them into the shared __label__ log; a standby signs nothing while the lease is
// live, and takes over (no re-emit) after it lapses. Node. Run: node test/label-room.test.mjs
import { mkdtempSync, rmSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import Corestore from 'corestore'
import { keyFromPrivate } from 'atproto-identity/lib/atproto-identity.js'
import { verifyLabel, decodeLabel } from 'atproto-identity/lib/labels.js'
import { LabelRoom } from '../lib/label-room.js'

const assert = (c, m) => { if (!c) { console.error('FAIL:', m); process.exit(1) } console.log('  ok -', m) }

// mock StateBase shared by all writers (models autobase replication — one linearized view)
function mockSb () {
  const m = new Map()
  return {
    async set (k, v) { m.set(k, v) },
    async del (k) { m.delete(k) },
    async get (k) { return m.has(k) ? m.get(k) : null },
    async list (prefix) { const o = {}; for (const [k, v] of m) if (k.startsWith(prefix)) o[k] = v; return o }
  }
}

const labeler = keyFromPrivate(new Uint8Array(32).fill(33))
const SRC = 'did:plc:communitylabeler'
const KP = { src: SRC, privateKey: labeler.privateKey }
const U1 = 'at://did:plc:a/app.bsky.feed.post/1'
const U2 = 'at://did:plc:a/app.bsky.feed.post/2'

const sb = mockSb()
const modA = new LabelRoom(sb, { ...KP, self: 'mod-a', ttlMs: 10000 })
const modB = new LabelRoom(sb, { ...KP, self: 'mod-b', ttlMs: 10000 })

// two DIFFERENT mods each record an intent (multi-writer)
await modA.label(U1, 'spam', { cts: '2026-07-01T00:00:00.000Z' })
await modB.label(U2, 'nsfw', { cts: '2026-07-02T00:00:00.000Z' })

// modA ticks first -> claims the free lease -> signs BOTH intents into the shared log
let t0 = 1000000
assert(await modA.tick(t0) === 2, 'lease-holder (modA) signs both intents into the shared log')
let log = await modA.signedLog()
assert(log.length === 2, 'shared __label__ log has 2 signed labels')
assert(log.every((l) => l.src === SRC && verifyLabel(l, labeler.didKey)), 'both signed under ONE labeler DID + verify against the labeler key')

// modB ticks while modA's lease is LIVE -> signs nothing (single active signer)
assert(await modB.tick(t0 + 1000) === 0, 'standby (modB) signs nothing while modA holds the lease')
assert((await modB.signedLog()).length === 2, 'shared log unchanged by the standby')

// a mod negates U1/spam; the holder emits the negation (retraction, not deletion)
await modB.negate(U1, 'spam', { cts: '2026-07-05T00:00:00.000Z' })
assert(await modA.tick(t0 + 2000) === 1, 'holder signs the new negation intent')
log = await modA.signedLog()
assert(log.some((l) => l.uri === U1 && l.neg === true && verifyLabel(l, labeler.didKey)), 'a signed negation label is in the log')

// FAILOVER: modA goes away; after the lease lapses (past TTL), modB takes over and does NOT re-sign
// the already-emitted labels (seeded from the shared log)
const later = t0 + 2000 + 20000 // > ttlMs after modA's last renew
assert(await modB.tick(later) === 0, 'modB takes over the lapsed lease but re-signs nothing (seeded from the log)')
assert((await modB.signedLog()).length === 3, 'no duplicate re-emit on failover (log still 3: 2 labels + 1 negation)')

// a NEW intent after failover is signed by the new holder
await modA.label(U2, '!hide', { cts: '2026-07-10T00:00:00.000Z' })
assert(await modB.tick(later + 1000) === 1, 'new holder (modB) signs a fresh intent')
assert((await modB.signedLog()).length === 4, 'shared log grew by exactly one')

// --- projection: shared __label__ log -> a served feed core (what favela readLabelFeed consumes) ---
const dir = mkdtempSync(join(tmpdir(), 'labelfeed-'))
const store = new Corestore(dir)
const feed = store.get({ name: 'label-feed', valueEncoding: 'binary' })
const n = await modB.projectTo(feed)
assert(n === 4, 'projectTo appended all 4 shared-log labels to the feed core')
assert(feed.length === 4, 'feed core length matches the shared log')
const blocks = []
for (let i = 0; i < feed.length; i++) blocks.push(decodeLabel(await feed.get(i)))
assert(blocks.every((l) => l.src === SRC && verifyLabel(l, labeler.didKey)), 'every projected feed block decodes + verifies under the labeler key')
assert(await modB.projectTo(feed) === 0, 'projectTo is idempotent (nothing new to append)')
await feed.close(); try { await store.close() } catch {}; rmSync(dir, { recursive: true, force: true })

console.log('\nLABEL-ROOM OK: multi-writer intents + lease-gated signing + failover + projection to a served feed')