Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

test(gateway): isolate lifecycle fixtures

Rahmane55b5d94bc7d
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 4b94e473..eef50ad9 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -41,6 +41,7 @@ this is the *what*, and it is what Settings → About shows as “What's new”.
**Tests**
+- `gateway` isolate lifecycle fixtures
- `cli` lock offline update dispatch
- `gateway` harden hosted security fixtures
diff --git a/scripts/gateway-stop-identity.test.ts b/scripts/gateway-stop-identity.test.ts
index 5a2eebcd..53d0c284 100644
--- a/scripts/gateway-stop-identity.test.ts
+++ b/scripts/gateway-stop-identity.test.ts
@@ -2,38 +2,17 @@ import { spawnSync } from "node:child_process";
import path from "node:path";
import { describe, expect, it } from "vitest";
-const COMMON = path.join(process.cwd(), "scripts/lib/gateway-common.sh");
+const HARNESS = path.join(process.cwd(), "scripts/test-fixtures/gateway-stop-identity.sh");
describe("gateway_stop_identity", () => {
it("fails closed when the same process identity survives SIGKILL", () => {
- const result = spawnSync("bash", [
- "-c",
- `set -euo pipefail
-source "$1"
-gateway_identity_matches() { return 0; }
-kill() { return 0; }
-sleep() { :; }
-gateway_stop_identity '{"pid":4242}'`,
- "gateway-stop-test",
- COMMON,
- ], { encoding: "utf8" });
+ const result = spawnSync(HARNESS, ["survives"], { encoding: "utf8" });
expect(result.status).not.toBe(0);
expect(result.stderr).toContain("survived SIGKILL; preserving lifecycle state");
});
it("succeeds once the recorded identity disappears", () => {
- const result = spawnSync("bash", [
- "-c",
- `set -euo pipefail
-source "$1"
-checks=0
-gateway_identity_matches() { checks=$((checks+1)); [ "$checks" -lt 3 ]; }
-kill() { return 0; }
-sleep() { :; }
-gateway_stop_identity '{"pid":4242}'`,
- "gateway-stop-test",
- COMMON,
- ], { encoding: "utf8" });
+ const result = spawnSync(HARNESS, ["disappears"], { encoding: "utf8" });
expect(result.status).toBe(0);
});
});
diff --git a/scripts/mso-gateway-active-recovery.test.ts b/scripts/mso-gateway-active-recovery.test.ts
index 29589e87..442edddf 100644
--- a/scripts/mso-gateway-active-recovery.test.ts
+++ b/scripts/mso-gateway-active-recovery.test.ts
@@ -1,10 +1,9 @@
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
-import { afterEach, describe, expect, it } from "vitest";
-import { GATEWAY, ROOT, VERSION, cleanupGatewayFixtures, fixture } from "./mso-gateway-test-fixture";
+import { describe, expect, it } from "vitest";
+import { GATEWAY, ROOT, VERSION, fixture } from "./mso-gateway-test-fixture";
-afterEach(() => cleanupGatewayFixtures());
describe("mso active tunnel runtime recovery", () => {
it("restarts a dead local runtime while preserving the already-live tunnel", () => {
diff --git a/scripts/mso-gateway-boundaries.test.ts b/scripts/mso-gateway-boundaries.test.ts
index ad37a5e0..e17a2478 100644
--- a/scripts/mso-gateway-boundaries.test.ts
+++ b/scripts/mso-gateway-boundaries.test.ts
@@ -1,10 +1,9 @@
import { execFileSync, spawn, spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
-import { afterEach, describe, expect, it } from "vitest";
-import { GATEWAY, ROOT, VERSION, cleanupGatewayFixtures, fixture, runGateway as run } from "./mso-gateway-test-fixture";
+import { describe, expect, it } from "vitest";
+import { GATEWAY, ROOT, VERSION, fixture, runGateway as run } from "./mso-gateway-test-fixture";
-afterEach(() => cleanupGatewayFixtures());
describe("mso gateway scope and public identity boundaries", () => {
it("isolates default gateway state by checkout and selected loopback origin", () => {
diff --git a/scripts/mso-gateway-runtime-signal.test.ts b/scripts/mso-gateway-runtime-signal.test.ts
index be43bc6e..8c4bbca8 100644
--- a/scripts/mso-gateway-runtime-signal.test.ts
+++ b/scripts/mso-gateway-runtime-signal.test.ts
@@ -2,13 +2,13 @@ import { spawn } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
-import { GATEWAY, ROOT, VERSION, cleanupGatewayFixtures, fixture } from "./mso-gateway-test-fixture";
+import { GATEWAY, ROOT, VERSION, fixture } from "./mso-gateway-test-fixture";
const pids = new Set<number>();
function alive(pid: number) { try { process.kill(pid, 0); return true; } catch { return false; } }
afterEach(() => {
for (const pid of pids) if (alive(pid)) try { process.kill(pid, "SIGTERM"); } catch {}
- pids.clear(); cleanupGatewayFixtures();
+ pids.clear();
});
describe("mso fallback runtime signal safety", () => {
diff --git a/scripts/mso-gateway-test-fixture.ts b/scripts/mso-gateway-test-fixture.ts
index b09afbda..40fa22f2 100644
--- a/scripts/mso-gateway-test-fixture.ts
+++ b/scripts/mso-gateway-test-fixture.ts
@@ -2,18 +2,46 @@ import { execFileSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
+import { onTestFinished } from "vitest";
export const ROOT = path.resolve(__dirname, "..");
export const GATEWAY = path.join(__dirname, "mso-gateway");
export const CLI = path.join(ROOT, "bin/mso");
export const VERSION = JSON.parse(fs.readFileSync(path.join(ROOT, "package.json"), "utf8")).version as string;
-const tempRoots: string[] = [];
-const tunnelPids = new Set<number>();
+function collectRecordedPids(root: string): number[] {
+ const pids = new Set<number>();
+ const visit = (dir: string) => {
+ let entries: fs.Dirent[] = [];
+ try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
+ for (const entry of entries) {
+ const file = path.join(dir, entry.name);
+ if (entry.isDirectory()) { visit(file); continue; }
+ if (entry.name !== "state.json") continue;
+ try {
+ const state = JSON.parse(fs.readFileSync(file, "utf8")) as {
+ tunnelIdentity?: { pid?: number }; runtimeIdentity?: { pid?: number };
+ };
+ for (const pid of [state.tunnelIdentity?.pid, state.runtimeIdentity?.pid]) {
+ if (typeof pid === "number" && pid > 1) pids.add(pid);
+ }
+ } catch { /* corrupt/partial fixture state is still safe to delete */ }
+ }
+ };
+ visit(root);
+ return [...pids];
+}
+
+function cleanupGatewayFixture(dir: string) {
+ for (const pid of collectRecordedPids(dir)) {
+ try { process.kill(pid, "SIGTERM"); } catch { /* already stopped */ }
+ }
+ fs.rmSync(dir, { recursive: true, force: true });
+}
export function fixture() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "mso-gateway-"));
- tempRoots.push(dir);
+ onTestFinished(() => cleanupGatewayFixture(dir));
const bin = path.join(dir, "bin"), state = path.join(dir, "state"), envFile = path.join(dir, ".env.local");
fs.mkdirSync(bin, { mode: 0o700 });
fs.writeFileSync(envFile, "OS_SESSION_SECRET=fixture-only-not-a-real-secret\n", { mode: 0o600 });
@@ -80,14 +108,5 @@ export function readState(stateDir: string) {
tunnelIdentity: { pid: number };
runtimeOwned: boolean;
};
- tunnelPids.add(value.tunnelIdentity.pid);
return { path: statePath, snapshot, value };
}
-
-export function cleanupGatewayFixtures() {
- for (const pid of tunnelPids) {
- try { process.kill(pid, "SIGTERM"); } catch { /* already stopped */ }
- }
- tunnelPids.clear();
- for (const dir of tempRoots.splice(0)) fs.rmSync(dir, { recursive: true, force: true });
-}
diff --git a/scripts/mso-gateway-update-safety.test.ts b/scripts/mso-gateway-update-safety.test.ts
index 06c3b1b0..4b44c65b 100644
--- a/scripts/mso-gateway-update-safety.test.ts
+++ b/scripts/mso-gateway-update-safety.test.ts
@@ -1,8 +1,7 @@
import { spawnSync } from "node:child_process";
-import { afterEach, describe, expect, it } from "vitest";
-import { GATEWAY, cleanupGatewayFixtures, fixture } from "./mso-gateway-test-fixture";
+import { describe, expect, it } from "vitest";
+import { GATEWAY, fixture } from "./mso-gateway-test-fixture";
-afterEach(cleanupGatewayFixtures);
describe("gateway offline-update safety assertion", () => {
it("rejects a healthy selected MSO responder that has no gateway ownership state", () => {
diff --git a/scripts/mso-gateway-web.test.ts b/scripts/mso-gateway-web.test.ts
index 20673980..340b5ec0 100644
--- a/scripts/mso-gateway-web.test.ts
+++ b/scripts/mso-gateway-web.test.ts
@@ -1,17 +1,13 @@
import { describe, expect, it } from "vitest";
-import { cleanupGatewayFixtures, fixture, runGateway } from "./mso-gateway-test-fixture";
+import { fixture, runGateway } from "./mso-gateway-test-fixture";
describe("mso web routing", () => {
it("falls back to loopback when a public origin is configured but no gateway is active", () => {
const f = fixture();
- try {
- const out = runGateway(["web", "--print"], {
- ...f.baseEnv,
- OS_PUBLIC_ORIGIN: "https://mso.example.test",
- });
- expect(out.trim()).toBe("http://127.0.0.1:4005");
- } finally {
- cleanupGatewayFixtures();
- }
+ const out = runGateway(["web", "--print"], {
+ ...f.baseEnv,
+ OS_PUBLIC_ORIGIN: "https://mso.example.test",
+ });
+ expect(out.trim()).toBe("http://127.0.0.1:4005");
});
});
diff --git a/scripts/mso-gateway.test.ts b/scripts/mso-gateway.test.ts
index 5e91c99b..b00c3110 100644
--- a/scripts/mso-gateway.test.ts
+++ b/scripts/mso-gateway.test.ts
@@ -1,12 +1,11 @@
import { execFileSync, spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
-import { afterEach, describe, expect, it } from "vitest";
+import { describe, expect, it } from "vitest";
import {
- CLI, GATEWAY, ROOT, VERSION, cleanupGatewayFixtures, fixture, readFileSnapshot, readState, runGateway as run,
+ CLI, GATEWAY, ROOT, VERSION, fixture, readFileSnapshot, readState, runGateway as run,
} from "./mso-gateway-test-fixture";
-afterEach(cleanupGatewayFixtures);
describe("mso public gateway", () => {
it("runs a temporary HTTPS gateway without publishing the MSO app port", () => {
diff --git a/scripts/test-fixtures/gateway-stop-identity.sh b/scripts/test-fixtures/gateway-stop-identity.sh
new file mode 100755
index 00000000..78f71187
--- /dev/null
+++ b/scripts/test-fixtures/gateway-stop-identity.sh
@@ -0,0 +1,22 @@
+#!/usr/bin/env bash
+set -euo pipefail
+ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
+# shellcheck source=scripts/lib/gateway-common.sh
+. "$ROOT/scripts/lib/gateway-common.sh"
+
+case "${1:-}" in
+ survives)
+ gateway_identity_matches() { return 0; }
+ kill() { return 0; }
+ sleep() { :; }
+ gateway_stop_identity '{"pid":4242}'
+ ;;
+ disappears)
+ checks=0
+ gateway_identity_matches() { checks=$((checks+1)); [ "$checks" -lt 3 ]; }
+ kill() { return 0; }
+ sleep() { :; }
+ gateway_stop_identity '{"pid":4242}'
+ ;;
+ *) exit 2 ;;
+esac