OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-did-peer / test / emit-sync.test.mjs

36 lines · javascript ref:
// emit-sync: an OPERATOR re-announce must reach the relay even for a rev that was #synced before.
//
// 2026-10-13: bsky's relay held loser-club at a July rev (3mrnruepxss22) newer than the head we serve
// (3mrfgnyiurk22), so it dropped every commit. The fix is one #sync at our head — and `emit-sync` answered
// emitted:false, because the relay dedupes frames by key `sync:<did>:<rev>` and a #sync for 3mrfgnyiurk22
// had been emitted on 2026-07-26. The lever could never re-announce a head it had announced once, which is
// precisely the case it exists for. Automatic emits stay deduplicated; an operator request is new each time.
// Run: node test/emit-sync.test.mjs
import { DidPeerRouter } from '../index.js'

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

// A relay that dedupes by key exactly as FleetRelay.emit does.
function fakeRelay () {
  const keys = new Set()
  return { keys, async emit (key) { if (keys.has(key)) return false; keys.add(key); return true } }
}
const front = { head: { signed: { did: 'did:plc:lc', rev: '3mrfgnyiurk22', version: 3, sig: new Uint8Array(64) } } }

const r = Object.create(DidPeerRouter.prototype)
r.relay = fakeRelay()
r.front = async () => front

const auto1 = await r._emitSync('did:plc:lc', front)
const auto2 = await r._emitSync('did:plc:lc', front)
assert(auto1.emitted === true, 'the first automatic #sync for a head is emitted')
assert(auto2.emitted === false, 'a second AUTOMATIC #sync for the same head is deduplicated (re-anchors do not spam the relay)')

const op1 = await r.emitSync('did:plc:lc')
assert(op1.emitted === true, '⛔ an OPERATOR emit-sync is emitted although that head was #synced before')
assert(op1.rev === '3mrfgnyiurk22', 'at the served head')
await new Promise((resolve) => setTimeout(resolve, 2))
const op2 = await r.emitSync('did:plc:lc')
assert(op2.emitted === true, 'and again on the next request — each operator request is its own announcement')
console.log('emit-sync: all ok')