singularity-forge/web/lib/__tests__/shutdown-gate.test.ts
2026-05-05 14:46:18 +02:00

83 lines
2.1 KiB
TypeScript

import assert from "node:assert/strict";
import { afterEach, beforeEach, describe, test } from "vitest";
import {
cancelShutdown,
isDaemonMode,
isShutdownPending,
scheduleShutdown,
} from "../shutdown-gate.ts";
describe("shutdown-gate", () => {
afterEach(() => {
// Always clean up any pending timers between tests
cancelShutdown();
delete process.env.SF_WEB_DAEMON_MODE;
});
describe("default mode (no daemon)", () => {
test("scheduleShutdown() sets a pending timer", () => {
scheduleShutdown();
assert.equal(isShutdownPending(), true);
});
test("cancelShutdown() clears the pending timer", () => {
scheduleShutdown();
cancelShutdown();
assert.equal(isShutdownPending(), false);
});
test("isDaemonMode() returns false", () => {
assert.equal(isDaemonMode(), false);
});
});
describe("daemon mode (SF_WEB_DAEMON_MODE=1)", () => {
beforeEach(() => {
process.env.SF_WEB_DAEMON_MODE = "1";
});
test("isDaemonMode() returns true", () => {
assert.equal(isDaemonMode(), true);
});
test("scheduleShutdown() does not schedule a timer", () => {
scheduleShutdown();
assert.equal(
isShutdownPending(),
false,
"shutdown timer must not be set in daemon mode",
);
});
test("scheduleShutdown() is safe to call multiple times", () => {
scheduleShutdown();
scheduleShutdown();
scheduleShutdown();
assert.equal(isShutdownPending(), false);
});
});
describe("daemon mode is not activated by other values", () => {
test("SF_WEB_DAEMON_MODE=0 does not enable daemon mode", () => {
process.env.SF_WEB_DAEMON_MODE = "0";
assert.equal(isDaemonMode(), false);
scheduleShutdown();
assert.equal(isShutdownPending(), true);
});
test("SF_WEB_DAEMON_MODE=true does not enable daemon mode", () => {
process.env.SF_WEB_DAEMON_MODE = "true";
assert.equal(isDaemonMode(), false);
scheduleShutdown();
assert.equal(isShutdownPending(), true);
});
test("unset SF_WEB_DAEMON_MODE does not enable daemon mode", () => {
delete process.env.SF_WEB_DAEMON_MODE;
assert.equal(isDaemonMode(), false);
scheduleShutdown();
assert.equal(isShutdownPending(), true);
});
});
});