OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-base / test / relay-acceptance.test.mjs

41 lines · javascript ref:
// TEST 6 — the projected chain is RELAY-VALID: each CAR is byte-exact (verifyRepo, the same check a
// real relay's CID validation performs), each commit verifies against the #atproto key, and the chain
// is prev-linked with strictly-increasing rev (the relay's forward-only cursor). Plus: chain commits
// are deterministic across co-owners (extends test 1 to a prev-linked chain).

import test from 'brittle'
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 { projectCommit, nextRev } from '../lib/project.js'

const MNEMONIC = 'test test test test test test test test test test test junk'
const post = (rk, text) => ({ collection: 'app.bsky.feed.post', rkey: rk, record: { $type: 'app.bsky.feed.post', text, createdAt: '2026-07-23T00:00:00.000Z', langs: ['en'] } })

test('projected chain is relay-valid (verifyRepo byte-exact + verifyCommit + forward-only prev-linked rev)', async (t) => {
  const id = createAtprotoIdentity(MNEMONIC, { handle: 'x.7rnx.net', pdsUrl: 'https://pds.7rnx.net' })
  const key = deriveAtprotoKey(MNEMONIC)

  const R1 = [post('3aaa', 'one')]
  const R2 = [post('3aaa', 'one'), post('3bbb', 'two')]

  const rev1 = nextRev(null)
  const c1 = await projectCommit({ records: R1, did: id.did, rev: rev1, prev: null, privateKey: key.privateKey })
  const rev2 = nextRev(rev1)
  const c2 = await projectCommit({ records: R2, did: id.did, rev: rev2, prev: c1.commitCid, privateKey: key.privateKey })

  for (const [name, c] of [['genesis', c1], ['commit2', c2]]) {
    const v = await verifyRepo(await readCar(c.carBytes))
    t.ok(v.ok, `${name} CAR is byte-exact (verifyRepo — relay CID validation passes), mismatches=${JSON.stringify(v.mismatches)}`)
    t.ok(verifyCommit(c.signedCommit, key.publicKey), `${name} commit verifies against the #atproto key`)
  }

  t.is(c2.signedCommit.prev.toString(), c1.commitCid.toString(), 'commit2.prev links to the genesis commit CID (append-only chain)')
  t.ok(rev2 > rev1, 'rev strictly increasing (relay forward-only cursor accepts)')

  // Chain-level determinism: a co-owner projecting commit2 with shuffled records gets the identical CID.
  const c2b = await projectCommit({ records: [...R2].reverse(), did: id.did, rev: rev2, prev: c1.commitCid, privateKey: key.privateKey })
  t.is(c2.commitCid.toString(), c2b.commitCid.toString(), 'prev-linked commit is deterministic across co-owners')
})