OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-proto-peer / test / pointer-substrate.test.mjs

58 lines · javascript ref:
// Step 2a — PointerSubstrate: the record SET converges symmetrically as tiny {$oid,size} POINTERS in
// the StateBase autobase, while the record BYTES live as deduped overlay files. Proves: a write stores
// a POINTER (not the record) in the autobase + the bytes as an overlay file; RepoBase resolves the
// pointer back to the byte-exact record; delete tombstones the pointer + drops the file. This restores
// the symmetric multi-writer convergence the files-only path dropped, with ~50 bytes/record in the log.
import '../lib/bare-globals.js'
import test from 'brittle'
import crypto from 'hypercore-crypto'
import fs from 'bare-fs'
import { StateBaseV3 } from 'atproto-room/lib/state-base.js'
import { RepoBase } from 'atproto-room/lib/repo-base.js'
import { OverlayRepoStore } from 'atproto-store/lib/overlay-repo-store.js'
import { PointerSubstrate } from 'atproto-store/lib/pointer-substrate.js'
import { openCorestore } from 'atproto-store/lib/corestore.js'
import { HyperOverlay } from 'hyper-overlay'

const tmp = () => { const d = '/tmp/hpp-ptr-' + Math.floor(Math.random() * 1e9); fs.mkdirSync(d, { recursive: true }); return d }

test('PointerSubstrate: pointers in the autobase, bytes in the overlay, byte-exact records', async (t) => {
  const sb = new StateBaseV3(tmp(), crypto.randomBytes(32), { roomSeed: crypto.randomBytes(32), network: false })
  await sb.ready(); t.teardown(() => sb.close())
  const ovStore = openCorestore(tmp())
  const overlay = new HyperOverlay(ovStore, { namespace: 'ptr-ov', destDir: tmp() })
  await overlay.ready(); t.teardown(() => overlay.close())
  const root = tmp()
  const store = await new OverlayRepoStore(root, { overlay }).ready()
  const repo = new RepoBase(new PointerSubstrate(sb, store), { did: 'did:plc:ptrtest' })

  const rec = { $type: 'app.bsky.feed.post', text: 'hello world', createdAt: '2026-07-19T00:00:00.000Z' }
  await repo.putRecord('app.bsky.feed.post', '1', rec)

  // (1) the AUTOBASE holds a tiny {$oid,size} POINTER — the symmetric-convergence unit — NOT the record
  const ptr = await sb.get('app.bsky.feed.post/1')
  t.ok(ptr && ptr.$oid && typeof ptr.size === 'number', 'autobase holds a {$oid,size} pointer')
  t.absent(ptr.text, 'the pointer is NOT the record (record bytes never enter the autobase)')
  t.ok(JSON.stringify(ptr).length < 120, `pointer is tiny (${JSON.stringify(ptr).length} bytes) — no balloon`)

  // (2) the record BYTES are a deduped overlay file on disk
  t.ok(fs.existsSync(root + '/records/app.bsky.feed.post/1.dagcbor'), 'record bytes are an overlay file')

  // (3) RepoBase resolves the pointer to the byte-exact record
  t.alike(await repo.getRecord('app.bsky.feed.post', '1'), rec, 'getRecord resolves the pointer to the exact record')

  await repo.putRecord('app.bsky.graph.follow', 'f1', { $type: 'app.bsky.graph.follow', subject: 'did:plc:other', createdAt: rec.createdAt })
  const list = await repo.listRecords()
  t.is(list.length, 2, 'listRecords returns the converged record set (2)')
  t.alike(list.find((r) => r.rkey === '1').record, rec, 'listRecords resolves bytes byte-exactly')

  // (4) listRecords skips reserved __ keys even though they share the autobase
  await sb.set('__prefs__', { preferences: [] })
  t.is((await repo.listRecords()).length, 2, 'reserved __ keys are not resolved as records')

  // (5) delete tombstones the pointer + drops the file
  await repo.deleteRecord('app.bsky.feed.post', '1')
  t.is((await repo.listRecords()).length, 1, 'delete removes the record from the converged set')
  t.absent(fs.existsSync(root + '/records/app.bsky.feed.post/1.dagcbor'), 'delete drops the overlay file')
})