import assert from "node:assert/strict" import { test } from "node:test" import { assertDatabaseRestoreQuarantineOpen, beginDatabaseRestoreQuarantine, completeDatabaseRestoreQuarantine, readDatabaseRestoreJournalHead, } from "./database-restore-quarantine.js" const id = "cce2a99d-bd85-4311-9d0a-6e7022fab650" const otherId = "6a8ad2ec-dfcd-4d44-8f93-7234e3853b78" const refusal = { code: "DATABASE_RESTORE_QUARANTINE_UNAVAILABLE", statusCode: 503 } const clientReturning = (...rows: Record[]) => ({ query: async () => ({ rows }) }) test("admission requires exactly one open row and hides connection details", async () => { const open = { operation_id: null } await assertDatabaseRestoreQuarantineOpen(clientReturning(open)) for (const rows of [[], [{}], [{ operation_id: id }], [{ ...open, unexpected: true }], [open, open]]) { await assert.rejects(assertDatabaseRestoreQuarantineOpen(clientReturning(...rows)), refusal) } await assert.rejects(assertDatabaseRestoreQuarantineOpen({ query: async () => { throw new Error("private SQL detail") } }), error => { assert.deepEqual({ code: (error as { code?: string }).code, statusCode: (error as { statusCode?: number }).statusCode }, refusal) assert.doesNotMatch(String(error), /private SQL detail/) return true }) }) test("begin and completion compare against the previous claim and require an exact acknowledgement", async () => { const calls: (string | null)[][] = [] const client = { query: async (sql: string, values: (string | null)[]) => { assert.match(sql, /operation_id IS NOT DISTINCT FROM \$2::pg_catalog.uuid/) calls.push(values) return { rows: [{ operation_id: values[0] }] } } } await beginDatabaseRestoreQuarantine(client, id) await completeDatabaseRestoreQuarantine(client, id) await assert.rejects(beginDatabaseRestoreQuarantine(client, "invalid"), refusal) await assert.rejects(completeDatabaseRestoreQuarantine(client, "invalid"), refusal) assert.deepEqual(calls, [[id, null], [null, id]]) for (const rows of [[], [{ operation_id: null }], [{ operation_id: otherId }], [{ operation_id: id }, { operation_id: id }]]) { await assert.rejects(beginDatabaseRestoreQuarantine(clientReturning(...rows), id), refusal) } for (const rows of [[], [{ operation_id: id }], [{ operation_id: null }, { operation_id: null }]]) { await assert.rejects(completeDatabaseRestoreQuarantine(clientReturning(...rows), id), refusal) } }) test("journal head distinguishes the old schema from a provisioned empty head", async () => { const read = (columns: Record) => readDatabaseRestoreJournalHead(clientReturning({ quarantine: { id: 1, ...columns } })) const sha256 = "a".repeat(64) assert.equal(await read({ operation_id: null }), undefined) assert.equal(await read({ journal_prefix_bytes: null, journal_prefix_sha256: null }), null) assert.deepEqual(await read({ journal_prefix_bytes: 42, journal_prefix_sha256: sha256 }), { bytes: 42, sha256 }) for (const columns of [ { journal_prefix_bytes: null }, { journal_prefix_bytes: null, journal_prefix_sha256: sha256 }, { journal_prefix_bytes: 42, journal_prefix_sha256: null }, { journal_prefix_bytes: 0, journal_prefix_sha256: sha256 }, { journal_prefix_bytes: 8_388_609, journal_prefix_sha256: sha256 }, { journal_prefix_bytes: "42", journal_prefix_sha256: sha256 }, { journal_prefix_bytes: 42, journal_prefix_sha256: sha256 + "\n" }, ]) await assert.rejects(read(columns), refusal) await assert.rejects(readDatabaseRestoreJournalHead(clientReturning()), refusal) const client = { query: async () => { throw Object.assign(new Error("missing relation"), { code: "42P01" }) } } await assert.rejects(readDatabaseRestoreJournalHead(client), refusal) }) test("signed transitions compare and acknowledge both the operation and journal head", async () => { const first = { bytes: 42, sha256: "a".repeat(64) } const applied = { bytes: 76, sha256: "b".repeat(64) } const calls: (string | null)[][] = [] const client = { query: async (sql: string, values: (string | null)[]) => { assert.match(sql, /operation_id IS NOT DISTINCT FROM \$2::pg_catalog.uuid/) assert.match(sql, /journal_prefix_bytes IS NOT DISTINCT FROM \$5::pg_catalog.int4/) assert.match(sql, /journal_prefix_sha256 IS NOT DISTINCT FROM \$6::pg_catalog.text/) calls.push(values) return { rows: [{ operation_id: values[0], journal_prefix_bytes: Number(values[2]), journal_prefix_sha256: values[3] }] } } } await beginDatabaseRestoreQuarantine(client, id, { expected: null, next: first }) await completeDatabaseRestoreQuarantine(client, id, { expected: first, next: applied }) await assert.rejects(completeDatabaseRestoreQuarantine(client, id, { expected: applied, next: first }), refusal) assert.deepEqual(calls, [[id, null, "42", first.sha256, null, null], [null, id, "76", applied.sha256, "42", first.sha256]]) await assert.rejects(completeDatabaseRestoreQuarantine(clientReturning(), id, { expected: first, next: applied }), refusal) await assert.rejects(completeDatabaseRestoreQuarantine(clientReturning({ operation_id: null, journal_prefix_bytes: applied.bytes, journal_prefix_sha256: first.sha256, }), id, { expected: first, next: applied }), refusal) })