// Step 2 of the DidPeer cutover: the bridge's PeerRouter must front a peer that ONLY announces
// (atproto/did-announce/v1) — NO get-coords responder — proving the corrected push-discovery works
// end-to-end through the real PeerRouter/PeerFront, with no serverKey directionality.
import test from 'brittle'
import { mkdtempSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import Hyperswarm from 'hyperswarm'
import Protomux from 'protomux'
import c from 'compact-encoding'
import b4a from 'b4a'
import createTestnet from '@hyperswarm/testnet'
import { openCorestore } from 'atproto-store/lib/corestore.js'
import { RepoStore } from 'atproto-store/lib/repo-store.js'
import { RepoManager } from 'atproto-store/lib/repo-manager.js'
import { FirehoseSequencer } from 'atproto-store/lib/sequencer.js'
import { readCar, loadCommit } from 'atproto-repo/lib/repo.js'
import { verifyCommit } from 'atproto-repo/lib/commit.js'
import { keyFromPrivate } from 'atproto-identity'
import { discoveryTopic } from 'atproto-identity/lib/discovery.js'
import { mkDidRouter } from './mk-did-router.js'
const DID = 'did:plc:announceonly2222222222'
const PRIV = new Uint8Array(32).fill(6)
const NOW = '2026-07-13T00:00:00.000Z'
const tmp = (p) => mkdtempSync(join(tmpdir(), p))
async function waitFor (fn, ms = 20000) { const e = Date.now() + ms; while (Date.now() < e) { if (await fn()) return true; await new Promise(r => setTimeout(r, 80)) } return false }
// An ANNOUNCE-ONLY peer: pushes {repoKey,firehoseKey,lengths} over atproto/did-announce/v1. It does
// NOT open a get-coords RPC responder — so the router can only front it via the pushed announce.
async function announceOnlyPeer (testnet, did = DID, priv = PRIV) {
const store = openCorestore(tmp('ao-peer-'))
const repo = await new RepoStore(store, { name: `atproto-repo:${did}` }).ready()
let micros = 1_700_000_000_000_000n
const mgr = await new RepoManager(repo, { did, privateKey: priv, clock: () => (micros += 1_000_000n) }).init()
const seq = await new FirehoseSequencer(store.get({ name: 'firehose' })).ready()
const w = await mgr.applyWrite({ action: 'create', collection: 'app.bsky.feed.post', rkey: '3a', record: { $type: 'app.bsky.feed.post', text: 'announce-only', createdAt: NOW } })
await seq.sequenceCommit(w, { time: NOW })
const sw = new Hyperswarm({ bootstrap: testnet.bootstrap })
const repoKey = b4a.toString(repo.core.key, 'hex')
const firehoseKey = b4a.toString(seq.core.key, 'hex')
sw.on('connection', (conn) => {
store.replicate(conn)
const chan = Protomux.from(conn).createChannel({ protocol: 'atproto/did-announce/v1' })
if (!chan) return
const msg = chan.addMessage({ encoding: c.json, onmessage: () => {} })
chan.open()
try { msg.send({ did, handle: did.slice(-6) + '.test', repoKey, firehoseKey, repoLength: repo.core.length, firehoseLength: seq.core.length }) } catch {}
// deliberately NO ProtomuxRPC get-coords responder.
})
await sw.join(discoveryTopic(did), { server: true, client: true }).flushed()
return { store, sw, close: async () => { await sw.destroy(); await seq.close(); await repo.close(); await store.close() } }
}
test('PeerRouter fronts an ANNOUNCE-ONLY peer (no get-coords) — corrected push-discovery', async (t) => {
const testnet = await createTestnet(3)
const peer = await announceOnlyPeer(testnet)
const store = openCorestore(tmp('ao-bridge-'))
const swarm = new Hyperswarm({ bootstrap: testnet.bootstrap })
const key = keyFromPrivate(PRIV).publicKey
const router = mkDidRouter(swarm, store, { fronted: [DID], resolveVerifyKey: async () => key })
t.teardown(async () => { await peer.close(); await router.close(); await swarm.destroy(); await store.close(); await testnet.destroy() })
// the router joined the DID topic in its constructor; it must IDENTIFY the peer purely from the
// pushed announce (no get-coords), then front + serve a DID-key-verified CAR.
t.ok(await waitFor(() => router._byDid && router._byDid.has(DID)), 'router identified the peer from the ANNOUNCE (no get-coords)')
const front = await router.front(DID)
t.is(front.did, DID, 'fronted the announce-only peer')
const { carBytes } = await front.getRepo()
t.ok(verifyCommit(loadCommit(await readCar(carBytes)).value, key), 'served CAR verifies against the DID key — keyless, announce-discovered')
})
test('PeerRouter fronts MULTIPLE announce-only DIDs over ONE swarm (the real wj62+ashatron deployment, no get-coords)', async (t) => {
const testnet = await createTestnet(3)
const DID_A = 'did:plc:announcemulti1111111'
const DID_B = 'did:plc:announcemulti2222222'
const PRIV_A = new Uint8Array(32).fill(3)
const PRIV_B = new Uint8Array(32).fill(4)
const keyA = keyFromPrivate(PRIV_A).publicKey
const keyB = keyFromPrivate(PRIV_B).publicKey
const A = await announceOnlyPeer(testnet, DID_A, PRIV_A)
const B = await announceOnlyPeer(testnet, DID_B, PRIV_B)
const store = openCorestore(tmp('aom-bridge-'))
const swarm = new Hyperswarm({ bootstrap: testnet.bootstrap })
const router = mkDidRouter(swarm, store, { fronted: [DID_A, DID_B], resolveVerifyKey: async (d) => (d === DID_A ? keyA : keyB) })
t.teardown(async () => { await A.close(); await B.close(); await router.close(); await swarm.destroy(); await store.close(); await testnet.destroy() })
// both DIDs must be identified purely from their pushed announces (no get-coords), over ONE swarm.
t.ok(await waitFor(() => router._byDid.has(DID_A) && router._byDid.has(DID_B)), 'both DIDs identified from their announces (no get-coords)')
const fa = await router.front(DID_A)
const fb = await router.front(DID_B)
t.is(fa.did, DID_A, 'fronted DID_A via announce')
t.is(fb.did, DID_B, 'fronted DID_B via announce over the SAME swarm')
t.ok(verifyCommit(loadCommit(await readCar((await fa.getRepo()).carBytes)).value, keyA), 'DID_A CAR verifies against DID_A key')
t.ok(verifyCommit(loadCommit(await readCar((await fb.getRepo()).carBytes)).value, keyB), 'DID_B CAR verifies against DID_B key')
})