OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-repo / test / car-read.test.js

52 lines · javascript ref:
// Slice 1: prove our CAR + DAG-CBOR + CID + MST-read stack is byte-exact against a
// REAL, relay-accepted repo (getRepo output from our live PDS). The killer check:
// every block re-encodes to identical bytes and the same CID.

import test from 'brittle'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import { readCar, loadCommit, listRecords, verifyRepo } from '../lib/repo.js'
import { cidForValue } from '../lib/cid.js'

const here = dirname(fileURLToPath(import.meta.url))
const carBytes = readFileSync(join(here, 'vectors', 'bridgetest-repo.car'))

const DID = 'did:plc:bh2hiuoeb34g4lif62zj2vx7'
const ROOT = 'bafyreigiu7fhjqrqo3cxvg5ymdjazlat6n3go4h6akm4yh47iwodnebr2a'
const POST_KEY = 'app.bsky.feed.post/3mpmksfzaqm7k'

const car = await readCar(carBytes)

test('CAR structure decodes to the signed commit', (t) => {
  t.ok(car.roots.length === 1 && car.roots[0].toString() === ROOT,
    'CAR has exactly one root (the commit)')

  const c = loadCommit(car)
  t.ok(c.value.did === DID && c.value.rev === '3mpmksfzaqm7k' && c.value.sig && c.value.data,
    'root decodes to our signed commit')
})

// THE fidelity proof: every block re-encodes to identical bytes + same CID.
test('every block is byte-exact (re-encode -> same CID)', async (t) => {
  const r = await verifyRepo(car)
  if (!r.ok) t.comment('mismatches: ' + JSON.stringify(r.mismatches))
  t.comment(`verified ${r.blocks} blocks, ${r.records} record(s)`)
  t.ok(r.ok, 'every block is byte-exact (re-encode -> same CID)')
})

test('MST traversal and record fidelity', async (t) => {
  const recs = listRecords(car)
  t.ok(recs.length === 1 && recs[0].key === POST_KEY, 'MST traversal yields the post key')

  const rec = listRecords(car)[0].value
  t.ok(rec.$type === 'app.bsky.feed.post' && typeof rec.text === 'string' && rec.text.includes('pds.7rnx.net'),
    'the record is the expected app.bsky.feed.post')

  // Independent CID check: recompute the record's CID from its value, compare to the
  // CID the MST points at (proves cidForValue matches the on-wire addressing).
  const rec2 = listRecords(car)[0]
  const { cid } = await cidForValue(rec2.value)
  t.ok(cid.equals(rec2.cid), 'recomputed record CID matches the MST pointer')
})