Files
boss/tests/boss-state-store.test.ts

47 lines
1.8 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
const schemaPath = new URL("../scripts/postgres-state-schema.sql", import.meta.url);
test("state store defaults to file mode", async () => {
delete process.env.BOSS_STATE_STORE;
const { describeBossStateStore } = await import("../src/lib/boss-state-store.ts");
const summary = describeBossStateStore();
assert.equal(summary.mode, "file");
assert.equal(summary.ready, true);
});
test("postgres state store fails closed without a database url", async () => {
process.env.BOSS_STATE_STORE = "postgres";
delete process.env.BOSS_DATABASE_URL;
const { describeBossStateStore, createBossStateStore } = await import("../src/lib/boss-state-store.ts");
const summary = describeBossStateStore();
assert.equal(summary.mode, "postgres");
assert.equal(summary.ready, false);
assert.match(summary.reason ?? "", /BOSS_DATABASE_URL/);
assert.throws(
() => createBossStateStore({ dataFile: "/tmp/boss-state.json", backupFile: "/tmp/boss-state.json.bak" }),
/BOSS_DATABASE_URL_REQUIRED/,
);
delete process.env.BOSS_STATE_STORE;
});
test("postgres state schema stores a single jsonb state snapshot", async () => {
const schema = await readFile(schemaPath, "utf8");
assert.match(schema, /CREATE TABLE IF NOT EXISTS boss_state_snapshots/);
assert.match(schema, /state JSONB NOT NULL/);
assert.match(schema, /PRIMARY KEY/);
});
test("state store loads postgres lazily so file mode works in standalone builds", async () => {
const source = await readFile(new URL("../src/lib/boss-state-store.ts", import.meta.url), "utf8");
assert.doesNotMatch(source, /import\s+\{\s*Client\s*\}\s+from\s+["']pg["']/);
assert.match(source, /await import\(["']pg["']\)/);
});