43 lines
4.1 KiB
TypeScript
43 lines
4.1 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { readFile } from "node:fs/promises"
|
|
import { runInNewContext } from "node:vm"
|
|
import ts from "typescript"
|
|
|
|
const source = await readFile(new URL("./traffic-flow-facts-rebuild.ts", import.meta.url), "utf8")
|
|
const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText
|
|
type Authority = { serverIds: number[]; authorize: (client?: object) => Promise<void>; drainExisting: unknown }
|
|
async function fixture() {
|
|
let rows: Array<{ id: number; revision: string }> = [{ id: 2, revision: "11" }, { id: 8, revision: "12" }], fail: unknown, invoke = 0, poolReads = 0, clientReads = 0, authority!: Authority
|
|
const inspect = (text: string) => { assert.equal(text, "SELECT id,xmin::text AS revision FROM servers ORDER BY id LIMIT 1001"); return { rows: structuredClone(rows) } }
|
|
const pool = { query: async (text: string) => { poolReads++; return inspect(text) } }, owned = { query: async (text: string) => { clientReads++; return inspect(text) } }
|
|
const drain = async () => {}
|
|
const modules: Record<string, unknown> = {
|
|
"../db/index.js": { pool },
|
|
"./traffic-flow-ingest.js": { drainExistingTrafficFlowWriters: drain },
|
|
"./traffic-flow-facts-rebuild-service.js": { rebuildFlowFactsSafely: async (input: Authority) => { authority = input; invoke++; await input.authorize(); await input.authorize(owned); if (fail) throw fail; return { ok: true, buckets: 4, facts: 2, days: ["2026-10-25"], jobId: "private-job", configurationProof: "private-config", classifierProof: "private-classifier", resultProof: "private-result" } } },
|
|
}
|
|
const exports = {} as { FACT_REBUILD_LOCK_KEY: number; rebuildFlowFactsFromBuckets: () => Promise<unknown> }
|
|
runInNewContext(compiled, { exports, require: (name: string) => { assert(name in modules, "Unapproved legacy dependency: " + name); return modules[name] }, Error, Number, JSON }, { filename: "traffic-flow-facts-rebuild.ts" })
|
|
return { run: exports.rebuildFlowFactsFromBuckets, key: exports.FACT_REBUILD_LOCK_KEY, drain, owned, authority: () => authority, setRows: (value: typeof rows) => { rows = value }, fail: (value: unknown) => { fail = value }, counts: () => ({ invoke, poolReads, clientReads }) }
|
|
}
|
|
const f = await fixture()
|
|
assert.equal(f.key, 8723104)
|
|
assert.deepEqual(JSON.parse(JSON.stringify(await f.run())), { ok: true, buckets: 4, facts: 2, days: ["2026-10-25"] })
|
|
assert.deepEqual(Array.from(f.authority().serverIds), [2, 8]); assert.equal(f.authority().drainExisting, f.drain)
|
|
assert.deepEqual(f.counts(), { invoke: 1, poolReads: 2, clientReads: 1 })
|
|
const before = f.counts(); await f.authority().authorize(f.owned)
|
|
assert.equal(f.counts().poolReads, before.poolReads); assert.equal(f.counts().clientReads, before.clientReads + 1)
|
|
for (const changed of [[{ id: 2, revision: "99" }, { id: 8, revision: "12" }], [{ id: 2, revision: "11" }], [{ id: 2, revision: "11" }, { id: 8, revision: "12" }, { id: 9, revision: "1" }]]) {
|
|
f.setRows(changed); await assert.rejects(f.authority().authorize(f.owned), /catalog changed/)
|
|
}
|
|
for (const invalid of [Array.from({ length: 1001 }, (_, index) => ({ id: index + 1, revision: "1" })), [{ id: 1, revision: "x" }], [{ id: 0, revision: "1" }], [{ id: 2, revision: "1" }, { id: 2, revision: "1" }], [{ id: 8, revision: "1" }, { id: 2, revision: "1" }]]) {
|
|
const f = await fixture(); f.setRows(invalid); await assert.rejects(f.run(), /exact bounded/); assert.equal(f.counts().invoke, 0)
|
|
}
|
|
const unknown = Object.assign(new Error("Unconfirmed publication; reconcile by jobId"), { code: "FLOW_COMMIT_UNKNOWN", jobId: "owned-job", configurationProof: "owned-config" })
|
|
const uncertain = await fixture(); uncertain.fail(unknown)
|
|
await assert.rejects(uncertain.run(), error => error === unknown)
|
|
assert.equal(uncertain.counts().invoke, 1)
|
|
const denied = await fixture(), protectedObject = Error("Protected future partition refused")
|
|
denied.fail(protectedObject); await assert.rejects(denied.run(), error => error === protectedObject); assert.equal(denied.counts().invoke, 1)
|
|
console.log("Legacy fact rebuild CLI: 13 inert exact-catalog/owned-client/service/drain/no-replay assertions passed")
|