// Co-hosted path: a noRepo peer has NO repo core (listRecords -> []); the appview must source records
// from the SHARED overlay store (authoritative, one copy). Run: bare test/appview-norepo.test.mjs
import '../lib/bare-globals.js'
import { storeDir } from './_store-dir.mjs'
import Corestore from 'corestore'
import { Appview } from '../lib/appview.js'
import { mkGetRecordStore } from './mk-record-store.mjs'
const assert = (c, m) => { if (!c) throw new Error('FAIL: ' + m); console.log(' ok -', m) }
const DID = 'did:plc:norepocohost00000000000'
const dir = storeDir('appview-norepo')
const store = new Corestore(dir)
const getRecordStore = mkGetRecordStore(dir + '/rec')
const av = await new Appview(store, { getRecordStore }).ready()
// Pre-populate the SHARED store exactly as hpp would (records-as-files), then reconcile a noRepo peer.
const rs = await getRecordStore(DID)
await rs.put('app.bsky.feed.post', 'p1', { $type: 'app.bsky.feed.post', text: 'lives once in the shared store', createdAt: '2026-07-02T00:00:00.000Z' })
await rs.put('app.bsky.feed.post', 'p2', { $type: 'app.bsky.feed.post', text: 'second', createdAt: '2026-07-01T00:00:00.000Z' })
// A noRepo peer: no repo core → listRecords is empty; the store is the source.
const peer = { did: DID, _noRepo: true, listRecords: async () => [] }
const n = await av.indexPeer(peer)
assert(n === 2, 'indexPeer sourced BOTH records from the shared store (noRepo peer, empty listRecords)')
const tl = await av.getTimeline({ limit: 10 })
assert(tl.posts.length === 2, 'timeline shows both store-sourced records')
assert(tl.posts[0].record.text === 'lives once in the shared store', 'newest-first, record hydrated from the shared store')
// Delete-correctness: remove from the store (as hpp would), reconcile → the appview drops it.
await rs.del('app.bsky.feed.post', 'p2')
await av.indexPeer(peer)
const tl2 = await av.getTimeline({ limit: 10 })
assert(tl2.posts.length === 1 && tl2.posts[0].record.text === 'lives once in the shared store', 'reconcile to the shared store removes the deleted record')
console.log('\nAPPVIEW-NOREPO OK: co-hosted DID sources records from the shared overlay store (no repo-core replica)')
await av.close()
try { await store.close() } catch {}