Files
boss/tests/state-store-maintenance-script.test.ts
2026-05-17 02:20:08 +08:00

149 lines
4.4 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import os from "node:os";
import path from "node:path";
import { execFile } from "node:child_process";
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
const scriptPath = new URL("../scripts/boss-state-store-maintenance.mjs", import.meta.url);
test("state store maintenance script exposes migration, backup, export, and rollback commands", async () => {
const source = await readFile(scriptPath, "utf8");
for (const command of [
"describe",
"validate-schema",
"backup-file",
"export-file",
"migrate-file-to-postgres",
"export-postgres-backup",
"restore-postgres-backup",
"rollback-postgres-to-file",
]) {
assert.match(source, new RegExp(command));
}
assert.match(source, /BOSS_DATABASE_URL/);
assert.match(source, /boss_state_snapshots/);
});
test("backup-file dry run validates local state without requiring postgres", async () => {
const root = await mkdtemp(path.join(os.tmpdir(), "boss-state-maintenance-"));
try {
const stateFile = path.join(root, "boss-state.json");
await writeFile(stateFile, JSON.stringify({ schemaVersion: 1, migratedAt: "2026-04-27T00:00:00.000Z" }), "utf8");
const { stdout } = await execFileAsync(process.execPath, [
scriptPath.pathname,
"backup-file",
"--input",
stateFile,
"--dry-run",
], {
env: {
...process.env,
BOSS_STATE_FILE: stateFile,
},
});
const payload = JSON.parse(stdout);
assert.equal(payload.ok, true);
assert.equal(payload.action, "backup-file");
assert.equal(payload.dryRun, true);
assert.equal(payload.source, stateFile);
assert.equal(payload.bytes > 0, true);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("migrate-file-to-postgres dry run requires explicit postgres mode and database url without connecting", async () => {
const root = await mkdtemp(path.join(os.tmpdir(), "boss-state-maintenance-"));
try {
const stateFile = path.join(root, "boss-state.json");
await writeFile(stateFile, JSON.stringify({ schemaVersion: 1, migratedAt: "2026-04-27T00:00:00.000Z" }), "utf8");
await assert.rejects(
execFileAsync(process.execPath, [
scriptPath.pathname,
"migrate-file-to-postgres",
"--input",
stateFile,
"--dry-run",
], {
env: {
...process.env,
BOSS_STATE_FILE: stateFile,
BOSS_STATE_STORE: "file",
BOSS_DATABASE_URL: "",
},
}),
/BOSS_STATE_STORE_POSTGRES_REQUIRED/,
);
await assert.rejects(
execFileAsync(process.execPath, [
scriptPath.pathname,
"migrate-file-to-postgres",
"--input",
stateFile,
"--dry-run",
], {
env: {
...process.env,
BOSS_STATE_FILE: stateFile,
BOSS_STATE_STORE: "postgres",
BOSS_DATABASE_URL: "",
},
}),
/BOSS_DATABASE_URL_REQUIRED/,
);
const { stdout } = await execFileAsync(process.execPath, [
scriptPath.pathname,
"migrate-file-to-postgres",
"--input",
stateFile,
"--dry-run",
], {
env: {
...process.env,
BOSS_STATE_FILE: stateFile,
BOSS_STATE_STORE: "postgres",
BOSS_DATABASE_URL: "postgres://boss@example.invalid/boss",
},
});
const payload = JSON.parse(stdout);
assert.equal(payload.ok, true);
assert.equal(payload.action, "migrate-file-to-postgres");
assert.equal(payload.dryRun, true);
assert.equal(payload.postgresConfigured, true);
assert.equal(payload.wouldConnect, false);
assert.equal(payload.schemaValid, true);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("validate-schema rejects an incomplete postgres schema", async () => {
const root = await mkdtemp(path.join(os.tmpdir(), "boss-state-maintenance-"));
try {
const schemaFile = path.join(root, "postgres-state-schema.sql");
await writeFile(schemaFile, "CREATE TABLE boss_state_snapshots (state JSONB NOT NULL);", "utf8");
await assert.rejects(
execFileAsync(process.execPath, [
scriptPath.pathname,
"validate-schema",
"--schema",
schemaFile,
]),
/POSTGRES_SCHEMA_INVALID/,
);
} finally {
await rm(root, { recursive: true, force: true });
}
});