Files
Nexus/packages/db/src/destructive-db-guard.test.ts
T
Hartmut 19aeb2ba04
CI / Lint (push) Successful in 3m4s
CI / Typecheck (push) Successful in 3m6s
CI / Architecture Guardrails (push) Successful in 3m8s
CI / Assistant Split Regression (push) Successful in 3m48s
CI / Build (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
CI / Fresh-Linux Docker Deploy (push) Has been cancelled
CI / Release Images (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
rename(phase 3): compose/DB/infra + stray code refs capakraken → nexus (#62)
rename(phase 3): compose/DB/infra + stray code refs capakraken → nexus (#62)

Co-authored-by: Hartmut Nörenberg <hn@hartmut-noerenberg.com>
Co-committed-by: Hartmut Nörenberg <hn@hartmut-noerenberg.com>
2026-05-21 20:07:18 +02:00

130 lines
3.6 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { assertDestructiveDbAllowed } from "./destructive-db-guard.js";
import { assertNexusDbTarget, assertSafeSeedTarget } from "./safe-destructive-env.js";
const ORIGINAL_ENV = { ...process.env };
function setEnv(values: Record<string, string | undefined>) {
process.env = { ...ORIGINAL_ENV };
for (const [key, value] of Object.entries(values)) {
if (value === undefined) {
delete process.env[key];
continue;
}
process.env[key] = value;
}
}
test.afterEach(() => {
process.env = { ...ORIGINAL_ENV };
});
test("assertDestructiveDbAllowed allows an explicitly confirmed disposable nexus test database", () => {
setEnv({
DATABASE_URL: "postgresql://tester:secret@localhost:5432/nexus_test",
ALLOW_DESTRUCTIVE_DB_TOOLS: "true",
CONFIRM_DESTRUCTIVE_DB_NAME: "nexus_test",
});
const target = assertDestructiveDbAllowed({
commandName: "db:test",
allowedDatabaseNames: ["nexus_test"],
});
assert.equal(target.databaseName, "nexus_test");
assert.equal(target.hostname, "localhost");
});
test("assertDestructiveDbAllowed rejects protected live database names even if allowlisted", () => {
setEnv({
DATABASE_URL: "postgresql://tester:secret@localhost:5432/nexus",
ALLOW_DESTRUCTIVE_DB_TOOLS: "true",
CONFIRM_DESTRUCTIVE_DB_NAME: "nexus",
});
assert.throws(
() =>
assertDestructiveDbAllowed({
commandName: "db:test",
allowedDatabaseNames: ["nexus"],
}),
/explicitly protected/u,
);
});
test("assertDestructiveDbAllowed rejects missing confirmation", () => {
setEnv({
DATABASE_URL: "postgresql://tester:secret@localhost:5432/nexus_e2e",
ALLOW_DESTRUCTIVE_DB_TOOLS: "true",
CONFIRM_DESTRUCTIVE_DB_NAME: "wrong_db",
});
assert.throws(
() =>
assertDestructiveDbAllowed({
commandName: "db:test",
allowedDatabaseNames: ["nexus_e2e"],
}),
/CONFIRM_DESTRUCTIVE_DB_NAME=nexus_e2e/u,
);
});
test("assertDestructiveDbAllowed rejects missing destructive allow flag", () => {
setEnv({
DATABASE_URL: "postgresql://tester:secret@localhost:5432/nexus_ci",
ALLOW_DESTRUCTIVE_DB_TOOLS: undefined,
CONFIRM_DESTRUCTIVE_DB_NAME: "nexus_ci",
});
assert.throws(
() =>
assertDestructiveDbAllowed({
commandName: "db:test",
allowedDatabaseNames: ["nexus_ci"],
}),
/ALLOW_DESTRUCTIVE_DB_TOOLS=true/u,
);
});
test("assertSafeSeedTarget rejects unexpected legacy disposable databases", () => {
setEnv({
DATABASE_URL: "postgresql://tester:secret@localhost:5432/legacy_test",
ALLOW_DESTRUCTIVE_DB_TOOLS: "true",
CONFIRM_DESTRUCTIVE_DB_NAME: "legacy_test",
});
assert.throws(() => assertSafeSeedTarget("db:seed"), /not in the destructive-tool allowlist/u);
});
test("assertNexusDbTarget accepts non-destructive nexus targets", () => {
setEnv({
DATABASE_URL: "postgresql://tester:secret@localhost:5432/nexus_dev",
});
const target = assertNexusDbTarget("db:seed:holidays");
assert.equal(target.databaseName, "nexus_dev");
});
test("assertNexusDbTarget rejects legacy non-nexus targets", () => {
setEnv({
DATABASE_URL: "postgresql://tester:secret@localhost:5432/legacy_non_nexus",
});
assert.throws(() => assertNexusDbTarget("db:seed:holidays"), /not a valid Nexus target/u);
});
test("assertNexusDbTarget explains missing env loading clearly", () => {
setEnv({
DATABASE_URL: undefined,
});
assert.throws(
() => assertNexusDbTarget("db:update:blueprints"),
/Run the command through the Nexus env wrappers/u,
);
});