singularity-forge/web/lib/__tests__/shutdown-gate.test.ts
NilsR0711 1ad4137892 fix(web): skip shutdown in daemon mode so server survives tab close (#2842)
When GSD_WEB_DAEMON_MODE=1 is set, scheduleShutdown() becomes a no-op.
The /api/shutdown endpoint still returns { ok: true } so the client
beacon fires without a network error, but process.exit() is never
called. This allows gsd --web to run as a persistent daemon behind a
reverse proxy without exiting on every browser tab close or refresh.

Closes #2835
2026-03-27 18:07:44 -06:00

83 lines
2.3 KiB
TypeScript

import { describe, test, beforeEach, afterEach } from "node:test";
import assert from "node:assert/strict";
import {
scheduleShutdown,
cancelShutdown,
isShutdownPending,
isDaemonMode,
} from "../shutdown-gate.ts";
describe("shutdown-gate", () => {
afterEach(() => {
// Always clean up any pending timers between tests
cancelShutdown();
delete process.env.GSD_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 (GSD_WEB_DAEMON_MODE=1)", () => {
beforeEach(() => {
process.env.GSD_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("GSD_WEB_DAEMON_MODE=0 does not enable daemon mode", () => {
process.env.GSD_WEB_DAEMON_MODE = "0";
assert.equal(isDaemonMode(), false);
scheduleShutdown();
assert.equal(isShutdownPending(), true);
});
test("GSD_WEB_DAEMON_MODE=true does not enable daemon mode", () => {
process.env.GSD_WEB_DAEMON_MODE = "true";
assert.equal(isDaemonMode(), false);
scheduleShutdown();
assert.equal(isShutdownPending(), true);
});
test("unset GSD_WEB_DAEMON_MODE does not enable daemon mode", () => {
delete process.env.GSD_WEB_DAEMON_MODE;
assert.equal(isDaemonMode(), false);
scheduleShutdown();
assert.equal(isShutdownPending(), true);
});
});
});