anbot

root / test / store-conformance.mjs

5843 bytes

// Conformance checks for the store interface: the same assertions run
// against the in-memory store and the filesystem store. The browser store
// cannot run under Node; test/browser.sh covers it in a real browser.
import assert from 'node:assert/strict'
import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { base64Encode, recordPath } from '../core.mjs'
import { memoryStore } from '../store.mjs'
import { createFsStore, initializeStore } from '../storage.mjs'

// IDs whose standard Base64 contains '+' and '/', which must never reach a
// file name: record keys use the Base64url form.
const recordId = base64Encode(new Uint8Array(32).fill(0xfb))
const otherId = base64Encode(new Uint8Array(32).fill(0x01))
const postId = base64Encode(new Uint8Array(32).fill(0x04))
const blockHash = base64Encode(new Uint8Array(32).fill(0x02), true)
const encode = text => new TextEncoder().encode(text)

assert.match(recordPath(recordId), /^[A-Za-z0-9_-]+$/)

async function checkStore(store, label) {
  assert.equal(await store.getRecord(recordId), undefined, `${label}: missing record`)
  assert.equal(await store.getBlock(blockHash, 10), undefined, `${label}: missing block`)
  assert.equal(await store.getPost(postId), undefined, `${label}: missing post`)
  assert.equal(await store.getSelection(), undefined, `${label}: missing selection`)
  await assert.rejects(store.getRecord('bogus'), /Invalid encoding/, `${label}: bad record id`)
  await assert.rejects(store.getBlock('bogus', 10), /Invalid encoding/, `${label}: bad block hash`)
  await assert.rejects(store.getPost('bogus'), /Invalid encoding/, `${label}: bad post id`)

  const recordBytes = encode('{"id":"x"}')
  assert.equal(await store.putRecord(recordId, recordBytes), true, `${label}: first record put`)
  const read = await store.getRecord(recordId)
  assert.deepEqual(read, recordBytes, `${label}: record round-trip`)
  read[0] = 0
  assert.deepEqual(await store.getRecord(recordId), recordBytes, `${label}: record get copies`)
  recordBytes[0] = 0
  assert.notEqual((await store.getRecord(recordId))[0], 0, `${label}: record put copies`)
  assert.equal(await store.putRecord(recordId, encode('other')), false, `${label}: record no-overwrite`)
  assert.notEqual((await store.getRecord(recordId))[0], 0, `${label}: record kept`)

  const blockBytes = new Uint8Array([1, 2, 3, 4, 5])
  assert.equal(await store.putBlock(blockHash, blockBytes), true, `${label}: first block put`)
  assert.deepEqual(await store.getBlock(blockHash, 5), blockBytes, `${label}: block round-trip`)
  await assert.rejects(store.getBlock(blockHash, 4), /Block exceeds size limit/, `${label}: size cap`)
  assert.equal(await store.putBlock(blockHash, new Uint8Array([9])), false, `${label}: block no-overwrite`)
  assert.deepEqual(await store.getBlock(blockHash, 5), blockBytes, `${label}: block kept`)

  const postBytes = encode('{"id":"post"}')
  assert.equal(await store.putPost(postId, postBytes), true, `${label}: first post put`)
  const postRead = await store.getPost(postId)
  assert.deepEqual(postRead, postBytes, `${label}: post round-trip`)
  postRead[0] = 0
  assert.deepEqual(await store.getPost(postId), postBytes, `${label}: post get copies`)
  postBytes[0] = 0
  assert.notEqual((await store.getPost(postId))[0], 0, `${label}: post put copies`)
  assert.equal(await store.putPost(postId, encode('other')), false, `${label}: post no-overwrite`)
  assert.notEqual((await store.getPost(postId))[0], 0, `${label}: post kept`)
  // Posts live in their own namespace: the same ID as a record is separate.
  assert.equal(await store.putPost(recordId, encode('post-bytes')), true, `${label}: post shares id space`)
  assert.deepEqual(await store.getPost(recordId), encode('post-bytes'), `${label}: post separate from record`)
  assert.notDeepEqual(await store.getRecord(recordId), encode('post-bytes'), `${label}: record untouched by post`)

  const selection = { repoId: recordId, ref: 'refs/heads/main', updateId: otherId }
  await store.putSelection(selection)
  assert.deepEqual(await store.getSelection(), selection, `${label}: selection round-trip`)
  const got = await store.getSelection()
  got.ref = 'refs/heads/other'
  assert.equal((await store.getSelection()).ref, 'refs/heads/main', `${label}: selection get copies`)
  const replaced = { repoId: otherId, ref: 'refs/heads/main', updateId: recordId }
  await store.putSelection(replaced)
  assert.deepEqual(await store.getSelection(), replaced, `${label}: selection replace`)
}

await checkStore(memoryStore(), 'memory')

const root = mkdtempSync(join(tmpdir(), 'anbot-store-'))
try {
  initializeStore(root, tmpdir())
  const fsStore = createFsStore(root)
  await checkStore(fsStore, 'fs')

  // The filesystem store keeps the existing on-disk layout and exact bytes.
  const diskBytes = encode('on-disk-bytes')
  await fsStore.putRecord(otherId, diskBytes)
  assert.deepEqual(readFileSync(join(root, 'records', recordPath(otherId))), Buffer.from(diskBytes))
  const diskBlock = new Uint8Array([7, 7, 7])
  const diskHash = base64Encode(new Uint8Array(32).fill(0x03), true)
  await fsStore.putBlock(diskHash, diskBlock)
  assert.deepEqual(readFileSync(join(root, 'blocks', diskHash)), Buffer.from(diskBlock))
  const diskPost = encode('on-disk-post')
  const diskPostId = base64Encode(new Uint8Array(32).fill(0x05))
  await fsStore.putPost(diskPostId, diskPost)
  assert.deepEqual(readFileSync(join(root, 'posts', recordPath(diskPostId))), Buffer.from(diskPost))
  const diskSelection = { repoId: otherId, ref: 'refs/heads/main', updateId: recordId }
  await fsStore.putSelection(diskSelection)
  assert.equal(readFileSync(join(root, 'selected.json'), 'utf8'), JSON.stringify(diskSelection))
} finally {
  rmSync(root, { recursive: true, force: true })
}

Git ยท Social feed