// The loser-club attempt-2 topology WITH storeBackend:'overlay' — production's actual backend.
//
// test/joiner-resync-multidid.test.js reproduced the full topology (two hosted DIDs, one swarm and one
// shared store, primary listed first, resync side store, joiner key sorting below the incumbent's) and it
// CONVERGED. The remaining untested delta against production was the record backend: both stortron DIDs
// run storeBackend:'overlay', neither repro did.
//
// That is not a cosmetic difference. Under the overlay backend a record is split in two:
// - a {$oid,size} POINTER that converges through the StateBase autobase, like any control key
// - the record BYTES, which live as content-addressed files and move over a SEPARATE per-connection
// content protocol (overlay.attachProtocol), wired up independently of the autobase
// So there are two ways to half-fail, and they look different: no pointers at all (base flat — what
// hyper-social#16 P1 reported), versus pointers converged but bytes unresolved (a repo that looks healthy
// from every count-based surface, which is the wj62 class of bug pointerDiag was built to catch).
//
// This test measures BOTH halves, because asserting only the record set would call a byte-starved joiner
// converged.
//
// hyper-social#16 P1. Run: bare test/joiner-resync-overlay.test.js
import '../lib/bare-globals.js'
import test from 'brittle'
import fs from 'bare-fs'
import b4a from 'b4a'
import createTestnet from '@hyperswarm/testnet'
import { bootMultiDevice, provisionDevice, resyncDid } from '../lib/device-node.js'
import { deviceKeetKeyPair } from 'atproto-room/lib/membership.js'
const ROOT_ROOM = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art'
const ROOT_OTHER = 'legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth title'
const DEV_A = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo vote'
const DEV_B = 'legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth useful legal will'
const DEV_C = 'letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic bless'
const DEV_D = 'letter advice cage absurd amount doctor acoustic avoid letter advice cage above'
const tmp = () => { const d = '/tmp/hpp-ovl-' + Math.floor(Math.random() * 1e9); fs.mkdirSync(d, { recursive: true }); return d }
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
const post = (rk, text) => ({ action: 'create', collection: 'app.bsky.feed.post', rkey: rk, record: { $type: 'app.bsky.feed.post', text, createdAt: '2026-07-27T00:00:00.000Z' } })
const keys = async (n) => (await n.device.state()).records.map((r) => r.collection + '/' + r.rkey).sort()
const waitFor = async (fn, ms = 60000) => { const end = Date.now() + ms; while (Date.now() < end) { if (await fn()) return true; await sleep(500) } return false }
async function bundle (root, devMnem, handle) {
const dk = await deviceKeetKeyPair(devMnem)
const b = await provisionDevice({ rootMnemonic: root, devicePublicKey: dk.publicKey, handle, pdsUrl: 'https://pds.local' })
return { entry: { deviceMnemonic: devMnem, did: b.did, signingKey: b.signingKey, deviceProof: b.deviceProof, rootIdentityKey: b.rootIdentityKey, handle }, keetPub: b4a.toString(dk.publicKey, 'hex'), did: b.did }
}
async function pair (root, handle) {
const a = await bundle(root, DEV_A, handle)
const b = await bundle(root, DEV_B, handle)
const joinerIsA = a.keetPub < b.keetPub
return { INC: joinerIsA ? b : a, JOIN: joinerIsA ? a : b, did: a.did }
}
test('overlay backend: a resync joiner on the second DID converges POINTERS and BYTES', async (t) => {
const testnet = await createTestnet(3)
const swarmOpts = { bootstrap: testnet.bootstrap }
const room = await pair(ROOT_ROOM, 'club.local')
const oInc = await bundle(ROOT_OTHER, DEV_C, 'other.local')
const oJoin = await bundle(ROOT_OTHER, DEV_D, 'other.local')
const roomMembers = [{ key: room.INC.keetPub, role: 1 }, { key: room.JOIN.keetPub, role: 1 }]
const otherMembers = [{ key: oInc.keetPub, role: 1 }, { key: oJoin.keetPub, role: 1 }]
// Incumbent: both DIDs on the OVERLAY backend, primary first — production's config shape.
const sInc = tmp()
const nInc = await bootMultiDevice({
storage: sInc,
dids: [
{ ...oInc.entry, members: otherMembers, forceCreator: true, primary: true, storeBackend: 'overlay' },
{ ...room.INC.entry, members: roomMembers, forceCreator: true, storeBackend: 'overlay' }
],
swarmOpts,
signIntervalMs: 1000
})
t.teardown(async () => { try { await nInc.close() } catch {} ; await testnet.destroy() })
const inc = nInc.byDid.get(room.did)
t.ok(inc.overlay, 'the incumbent room really is on the overlay backend')
await inc.device.applyWrite(post('3aaa', 'incumbent one'))
await inc.device.applyWrite(post('3bbb', 'incumbent two'))
await waitFor(async () => (await inc.device.state()).recordCount >= 2)
const incKeys = await keys(inc)
// Joiner: resync flag written before boot, both DIDs on overlay, primary first.
const sJoin = tmp()
resyncDid(sJoin, room.did)
const nJoin = await bootMultiDevice({
storage: sJoin,
dids: [
{ ...oJoin.entry, members: otherMembers, joiner: true, primary: true, storeBackend: 'overlay' },
{ ...room.JOIN.entry, members: roomMembers, joiner: true, storeBackend: 'overlay' }
],
swarmOpts,
signIntervalMs: 1000
})
t.teardown(async () => { try { await nJoin.close() } catch {} })
const join = nJoin.byDid.get(room.did)
t.ok(join && join._sideStore, 'the room opened from a side store (resync), as production did')
t.ok(join.overlay, 'and on the overlay backend, as production does')
// HALF ONE: do the POINTERS converge? This is the half production reported as dead (base=0/0).
const converged = await waitFor(async () => {
const s = await join.device.state()
return s.records.some((r) => r.rkey === '3aaa') && s.records.some((r) => r.rkey === '3bbb')
}, 90000)
const d = await join.device.membersDiag()
const st = await join.device.state()
t.ok(converged, `POINTERS converged (base=${d.indexedLength}/${d.baseLength}, records=${st.recordCount})`)
t.alike(await keys(join), incKeys, 'and the record set is EXACTLY the incumbent\'s — no rival line')
// HALF TWO: do the BYTES resolve? A pointer set that converged while the bytes never moved is the
// failure mode every count-based surface calls healthy — assert it explicitly rather than inferring it.
const bytes = await waitFor(async () => {
const pd = await join.device.pointerDiag({})
return pd && pd.checked > 0 && pd.missing === 0 && pd.stale === 0
}, 90000)
const pd = await join.device.pointerDiag({})
t.ok(bytes, `BYTES resolved too (checked=${pd.checked} ok=${pd.ok} missing=${pd.missing} stale=${pd.stale} noOid=${pd.unresolvable})`)
// And the room stays live afterwards.
await inc.device.applyWrite(post('3ccc', 'after the join'))
t.ok(await waitFor(async () => (await join.device.state()).records.some((r) => r.rkey === '3ccc'), 90000),
'a post-join write on the incumbent reaches the overlay-backed joiner')
})