OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-base / test / base.test.mjs

91 lines · javascript ref:
// AtprotoBase slice 1 — the engine (autobase record view) wired to the deterministic projector.
// Proves: records written through the typed op API produce a relay-valid, deterministic, advancing
// atproto commit chain — and two independent bases with the same records project the IDENTICAL head.

import test from 'brittle'
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { deriveAtprotoKey, createAtprotoIdentity } from 'atproto-identity'
import { verifyCommit } from 'atproto-repo/lib/commit.js'
import { readCar } from 'atproto-repo/lib/car.js'
import { verifyRepo } from 'atproto-repo'
import { decodeCommitFrame } from 'atproto-store/lib/firehose.js'
import { AtprotoBase } from '../lib/base.js'

const MNEMONIC = 'test test test test test test test test test test test junk'
const ID = createAtprotoIdentity(MNEMONIC, { handle: 'x.7rnx.net', pdsUrl: 'https://pds.7rnx.net' })
const KEY = deriveAtprotoKey(MNEMONIC)

function mkBase () {
  const dir = mkdtempSync(join(tmpdir(), 'atbase-'))
  const base = new AtprotoBase({ path: dir, did: ID.did, privateKey: KEY.privateKey, ackInterval: 100 })
  return { base, dir, close: async () => { await base.close(); rmSync(dir, { recursive: true, force: true }) } }
}
const head = async (bs) => { await bs.base.settled(); return bs.base.head() } // finalized head
const post = (text) => ({ $type: 'app.bsky.feed.post', text, createdAt: '2026-07-23T00:00:00.000Z', langs: ['en'] })

test('AtprotoBase: typed record writes → relay-valid, deterministic, advancing atproto chain', async (t) => {
  const A = mkBase(); await A.base.ready()
  t.teardown(() => A.close())
  t.ok(A.base.writable, 'creator base is writable')

  await A.base.putRecord('app.bsky.feed.post', '3aaa', post('one'))
  await A.base.putRecord('app.bsky.feed.post', '3bbb', post('two'))

  const h1 = await head(A)
  const v = await verifyRepo(await readCar(h1.carBytes))
  t.ok(v.ok, `head CAR is relay-valid (verifyRepo), records=${v.records}`)
  t.ok(verifyCommit(h1.signedCommit, KEY.publicKey), 'head commit verifies against #atproto key')
  t.is(v.records, 2, 'both records present in the projected repo')

  // Advances on a new write.
  await A.base.putRecord('app.bsky.feed.post', '3ccc', post('three'))
  const h2 = await head(A)
  t.not(h1.commitCid.toString(), h2.commitCid.toString(), 'head advances when a record is added')
  t.is((await verifyRepo(await readCar(h2.carBytes))).records, 3, 'now 3 records')

  // Delete is reflected.
  await A.base.delRecord('app.bsky.feed.post', '3aaa')
  const h3 = await head(A)
  t.is((await verifyRepo(await readCar(h3.carBytes))).records, 2, 'record deleted → 2 records in projection')
})

test('AtprotoBase: two independent bases with the same records project the IDENTICAL head (engine-level determinism)', async (t) => {
  const A = mkBase(); const B = mkBase()
  await A.base.ready(); await B.base.ready()
  t.teardown(async () => { await A.close(); await B.close() })

  // Same records, written in DIFFERENT order on two separate co-owner bases.
  await A.base.putRecord('app.bsky.feed.post', '3aaa', post('one'))
  await A.base.putRecord('app.bsky.feed.post', '3bbb', post('two'))
  await B.base.putRecord('app.bsky.feed.post', '3bbb', post('two'))
  await B.base.putRecord('app.bsky.feed.post', '3aaa', post('one'))

  const ha = await head(A)
  const hb = await head(B)
  t.is(ha.commitCid.toString(), hb.commitCid.toString(), 'identical head commit CID across co-owners')
  t.is(Buffer.compare(Buffer.from(ha.signedCommit.sig), Buffer.from(hb.signedCommit.sig)), 0, 'identical signature (no signer election needed)')
})

test('AtprotoBase firehose: finalized head advances emit #commit frames; subscribeRepos replays them', async (t) => {
  const A = mkBase(); await A.base.ready()
  t.teardown(() => A.close())

  await A.base.putRecord('app.bsky.feed.post', '3a', post('one'))
  const w1 = await A.base.emitFirehose()
  t.ok(w1 && w1.ops.length === 1, 'first emit: one create op for the finalized head')

  await A.base.putRecord('app.bsky.feed.post', '3b', post('two'))
  const w2 = await A.base.emitFirehose()
  t.ok(w2 && w2.ops.some((o) => o.action === 'create' && o.path.endsWith('3b')), 'second emit: create 3b')

  t.is(await A.base.emitFirehose(), null, 'no finalized change → no new frame emitted')
  t.is(A.base.firehoseLength, 2, 'firehose core has exactly 2 #commit frames')

  const frames = []
  for await (const { seq, bytes } of A.base.subscribeRepos(0)) { frames.push(bytes); if (frames.length >= 2) break }
  t.is(frames.length, 2, 'subscribeRepos replayed both frames')
  t.is(decodeCommitFrame(frames[0]).body.commit.toString(), w1.commitCid.toString(), 'replayed frame 1 = the first finalized head')
})