OverviewFilesCommitsIssuesDocsReleasesPeers

favela-web / test / integration-p2p-index.test.mjs

48 lines · javascript ref:
// Full-chain integration UNDER BARE: atproto-did-peer reads wj62 over the real swarm -> appview.indexPeer
// folds its posts into the Hyperbee -> getTimeline. Proves the P2P read + index run Bare-native end to end.
// Run: `bare test/integration-p2p-index.test.mjs`.
import '../lib/bare-globals.js'
import { storeDir } from './_store-dir.mjs'
import Hyperswarm from 'hyperswarm'
import Corestore from 'corestore'
import { DidPeer } from 'atproto-did-peer'
import { openCorestore } from 'atproto-store/lib/corestore.js'
import { discoveryTopic } from 'atproto-identity/lib/discovery.js'
import { Appview } from '../lib/appview.js'
import { mkGetRecordStore } from './mk-record-store.mjs'

const assert = (cond, msg) => { if (!cond) throw new Error('FAIL: ' + msg); console.log('  ok -', msg) }

const DID = 'did:plc:wj62ebbusln6gxo5azo6ogzo'
// wj62's #atproto signing pubkey, resolved from PLC via Node (this Bare test needs no fetch).
const VERIFY_KEY = Buffer.from('03d401804233e71bc5728c4428d5ef4d33c8ef636b934762bfe1b378d43c7d56d8', 'hex')

const stamp = Date.now()
const swarm = new Hyperswarm()
const didStore = openCorestore(storeDir('int-did')          // atproto-store copy (P2P read)
const peer = new DidPeer(didStore, { did: DID, verifyKey: VERIFY_KEY, onError: (e) => console.error('  [err]', e.message) })
swarm.on('connection', (c) => peer.attach(c))
swarm.join(discoveryTopic(DID), { client: true, server: true })
await swarm.flush()
console.log('joined wj62 topic; replicating under Bare...')

const head = await peer.ready({ timeout: 45000 })
assert(head, 'DidPeer reached a verified head over P2P (Bare-native)')

const indexStore = new Corestore(storeDir('int-idx')        // favela copy (the index)
const av = await new Appview(indexStore, { getRecordStore: mkGetRecordStore(storeDir('int-rec') }).ready()
const n = await av.indexPeer(peer)
assert(n > 0, `indexed ${n} of wj62's posts into the Hyperbee timeline`)

const tl = await av.getTimeline({ limit: 5 })
assert(tl.posts.length > 0, 'getTimeline returns posts derived from the P2P firehose')
console.log('  newest:', JSON.stringify(tl.posts[0].record?.text || '').slice(0, 60))

const feed = await av.getAuthorFeed(DID, { limit: 3 })
assert(feed.posts.length > 0 && feed.posts.every(p => p.did === DID), 'author feed scoped to wj62')

console.log('\nINTEGRATION OK (Bare): P2P read (atproto-did-peer) -> appview index -> timeline, end to end.')
await swarm.destroy(); await peer.close(); await av.close()
try { await didStore.close() } catch {}
try { await indexStore.close() } catch {}