// Integration: resolveRoster against REAL atproto records — list + listitem records written by
// RepoManager and read back through the SAME read-view (getRecord/listRecords) that favela's DidPeer
// uses over P2P. Proves the resolver works with genuinely-encoded records, not just a hand-mocked
// shape. Offline, no hpp/network. Run: `bare test/roster-integration.test.mjs`
import '../lib/bare-globals.js'
import { storeDir } from './_store-dir.mjs'
import { RepoStore } from 'atproto-store/lib/repo-store.js'
import { RepoManager } from 'atproto-store/lib/repo-manager.js'
import { getRecord, listRecords } from 'atproto-store/lib/read-view.js'
import { resolveRoster, LIST_COLLECTION, LISTITEM_COLLECTION } from '../lib/roster.js'
const assert = (cond, msg) => { if (!cond) throw new Error('FAIL: ' + msg); console.log(' ok -', msg) }
const OWNER = 'did:plc:realowner'
const PRIV = new Uint8Array(32).fill(5)
const dir = storeDir('roster-int')
const store = await new RepoStore(dir).ready()
let micros = 1_700_000_000_000_000n
const mgr = new RepoManager(store, { did: OWNER, privateKey: PRIV, clock: () => (micros += 1_000_000n) })
await mgr.init()
const listRkey = '3list'
const listUri = `at://${OWNER}/app.bsky.graph.list/${listRkey}`
const otherUri = `at://${OWNER}/app.bsky.graph.list/3other`
const iso = '2026-07-01T00:00:00.000Z'
await mgr.applyWrite({ action: 'create', collection: LIST_COLLECTION, rkey: listRkey, record: { $type: LIST_COLLECTION, name: 'Real Community', purpose: 'app.bsky.graph.defs#curatelist', createdAt: iso } })
await mgr.applyWrite({ action: 'create', collection: LISTITEM_COLLECTION, rkey: 'i1', record: { $type: LISTITEM_COLLECTION, subject: 'did:plc:alice', list: listUri, createdAt: iso } })
await mgr.applyWrite({ action: 'create', collection: LISTITEM_COLLECTION, rkey: 'i2', record: { $type: LISTITEM_COLLECTION, subject: 'did:plc:bob', list: listUri, createdAt: iso } })
await mgr.applyWrite({ action: 'create', collection: LISTITEM_COLLECTION, rkey: 'i3', record: { $type: LISTITEM_COLLECTION, subject: 'did:plc:carol', list: otherUri, createdAt: iso } })
// Wrap the real store in the peer-like surface resolveRoster expects — the SAME read-view favela's
// DidPeer exposes over P2P (getRecord/listRecords -> { uri, cid, value }).
const ownerPeer = {
did: OWNER,
getRecord: (c, r) => getRecord(store, c, r, { did: OWNER }),
listRecords: (c) => listRecords(store, c, { did: OWNER })
}
const res = await resolveRoster(ownerPeer, listUri)
assert(res.name === 'Real Community', 'reads the real list record name via read-view')
assert(res.purpose === 'app.bsky.graph.defs#curatelist', 'reads the real list purpose')
assert(res.members.length === 2, 'resolves exactly 2 members from real listitem records')
assert(res.members.includes('did:plc:alice') && res.members.includes('did:plc:bob'), 'members are the real listitem subjects')
assert(!res.members.includes('did:plc:carol'), 'excludes a listitem pointing at a different list')
console.log('\nROSTER-INTEGRATION OK: resolveRoster over REAL atproto list/listitem records (RepoManager -> read-view)')
await store.close()