| … | … | @@ -0,0 +1,43 @@ |
| 1 | // Offline unit test for DidPeer.commitOps — the race-free CAR extraction. Build a REAL #commit frame |
| 2 | // (RepoManager.applyWrite -> commitFrame), decode it (firehose.decodeFrame), and assert commitOps |
| 3 | // yields normalized ops with the changed record decoded FROM THE FRAME'S OWN CAR. Run: |
| 4 | // node test/commitops.test.mjs |
| 5 | import { mkdtempSync, rmSync } from 'node:fs' |
| 6 | import { join } from 'node:path' |
| 7 | import { tmpdir } from 'node:os' |
| 8 | import { RepoStore } from 'atproto-store/lib/repo-store.js' |
| 9 | import { RepoManager } from 'atproto-store/lib/repo-manager.js' |
| 10 | import { commitFrame, decodeFrame } from 'atproto-store/lib/firehose.js' |
| 11 | import { DidPeer } from '../lib/did-peer.js' |
| 12 | |
| 13 | const assert = (c, m) => { if (!c) { console.error('FAIL:', m); process.exit(1) } console.log(' ok -', m) } |
| 14 | |
| 15 | const DID = 'did:plc:commitops' |
| 16 | const PRIV = new Uint8Array(32).fill(9) |
| 17 | const iso = '2026-07-01T00:00:00.000Z' |
| 18 | |
| 19 | const dir = mkdtempSync(join(tmpdir(), 'commitops-')) |
| 20 | const store = await new RepoStore(dir).ready() |
| 21 | let micros = 1_700_000_000_000_000n |
| 22 | const mgr = new RepoManager(store, { did: DID, privateKey: PRIV, clock: () => (micros += 1_000_000n) }) |
| 23 | await mgr.init() |
| 24 | |
| 25 | // CREATE -> the record must be recoverable from the frame CAR alone (no repo/store access) |
| 26 | const wc = await mgr.applyWrite({ action: 'create', collection: 'app.bsky.feed.post', rkey: '3zzz', record: { $type: 'app.bsky.feed.post', text: 'from-car', createdAt: iso } }) |
| 27 | const dc = decodeFrame(await commitFrame(wc, { seq: 1, time: iso })) |
| 28 | assert(dc && dc.t === '#commit', 'create frame decodes as #commit') |
| 29 | const opsC = await DidPeer.commitOps(dc.body) |
| 30 | assert(opsC.length === 1, 'commitOps returns 1 op for a single create') |
| 31 | assert(opsC[0].action === 'create' && opsC[0].collection === 'app.bsky.feed.post' && opsC[0].rkey === '3zzz', 'op action/collection/rkey parsed from path') |
| 32 | assert(opsC[0].record && opsC[0].record.text === 'from-car', 'RECORD decoded from the frame CAR (race-free)') |
| 33 | |
| 34 | // DELETE -> a delete op carries no record |
| 35 | const wd = await mgr.applyWrite({ action: 'delete', collection: 'app.bsky.feed.post', rkey: '3zzz' }) |
| 36 | const dd = decodeFrame(await commitFrame(wd, { seq: 2, time: iso })) |
| 37 | const opsD = await DidPeer.commitOps(dd.body) |
| 38 | assert(opsD.length === 1 && opsD[0].action === 'delete' && opsD[0].rkey === '3zzz', 'commitOps yields a delete op') |
| 39 | assert(opsD[0].record === undefined, 'delete op carries no record') |
| 40 | |
| 41 | console.log('\nCOMMITOPS OK: record extracted from the #commit CAR; create/delete normalized') |
| 42 | await store.close() |
| 43 | rmSync(dir, { recursive: true, force: true }) |