fix(test): stop autostart tests from disabling the developer's real systemd service (#8900)

Validated in local merge-train (devbox-vm-06-dev002) @ combined-tip (FAST gates green: static + changed tests + vitest — only pre-existing audit.test.ts flake). Evidence: /home/diegosouzapw/dev/proxys/OmniRoute/.claude/worktrees/merge-train-20260805-213228-suite.log
This commit is contained in:
NoSoloSoft
2026-08-06 02:41:26 +02:00
committed by GitHub
parent e4d1108ad3
commit b4d7e86521
2 changed files with 52 additions and 2 deletions

View File

@@ -1,22 +1,41 @@
import test from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
let tmpDir: string;
let origHome: string | undefined;
let origPath: string | undefined;
test.before(() => {
tmpDir = mkdtempSync(join(tmpdir(), "omniroute-tray-test-"));
origHome = process.env.HOME;
// Redirecionar HOME para tmpDir para isolar testes de autostart
process.env.HOME = tmpDir;
// HOME alone does NOT isolate this test: `enable()`/`disable()` shell out to
// `systemctl --user enable|start` and `systemctl --user disable --now
// omniroute.service`, which reach the caller's real systemd bus (XDG_RUNTIME_DIR,
// not HOME) and therefore stopped and disabled the developer's REAL omniroute
// service every time this suite ran. Shadow systemctl/loginctl with failing
// stubs so `isSystemdUserAvailable()` is false and the systemd branch is skipped;
// the XDG desktop-file branch still runs and is properly isolated by HOME.
// Same rationale documented at length in tests/unit/cli/autostart-linux.test.ts.
origPath = process.env.PATH;
const stubBin = join(tmpDir, "stub-bin");
mkdirSync(stubBin, { recursive: true });
for (const name of ["systemctl", "loginctl"]) {
writeFileSync(join(stubBin, name), "#!/bin/sh\nexit 1\n", { mode: 0o755 });
}
process.env.PATH = `${stubBin}:${origPath ?? ""}`;
});
test.after(() => {
if (origHome === undefined) delete process.env.HOME;
else process.env.HOME = origHome;
if (origPath === undefined) delete process.env.PATH;
else process.env.PATH = origPath;
try {
rmSync(tmpDir, { recursive: true, force: true });
} catch {}

View File

@@ -1,21 +1,52 @@
import test from "node:test";
import assert from "node:assert/strict";
import { existsSync, readFileSync, mkdtempSync, rmSync } from "node:fs";
import { existsSync, readFileSync, mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
let tmpDir: string;
let origHome: string | undefined;
let origPath: string | undefined;
/**
* Redirecting HOME is NOT enough to isolate this test.
*
* `disableLinux()` runs `systemctl --user disable --now omniroute.service` and
* `enableLinux()` runs `systemctl --user enable` + `start`. `systemctl --user`
* talks to the caller's systemd bus via XDG_RUNTIME_DIR and does not care about
* HOME, so on any Linux developer machine that actually runs omniroute as a user
* service these tests stopped and disabled the REAL service — repeatedly, since
* the pair enable()/disable() ping-pongs it. Symptom: an ordered `Stopped` that
* `Restart=always` will not recover from, plus a silently `disabled` unit.
*
* Fix: shadow `systemctl` and `loginctl` with stubs that always fail, so
* `isSystemdUserAvailable()` returns false and the whole systemd branch is
* skipped. The XDG desktop-file branch still runs and IS isolated by HOME, so
* the test keeps its coverage.
*/
function installSystemctlStubs(binDir: string): void {
mkdirSync(binDir, { recursive: true });
for (const name of ["systemctl", "loginctl"]) {
const stub = join(binDir, name);
writeFileSync(stub, "#!/bin/sh\nexit 1\n", { mode: 0o755 });
}
}
test.before(() => {
tmpDir = mkdtempSync(join(tmpdir(), "omniroute-autostart-linux-"));
origHome = process.env.HOME;
process.env.HOME = tmpDir;
origPath = process.env.PATH;
const stubBin = join(tmpDir, "stub-bin");
installSystemctlStubs(stubBin);
process.env.PATH = `${stubBin}:${origPath ?? ""}`;
});
test.after(() => {
if (origHome === undefined) delete process.env.HOME;
else process.env.HOME = origHome;
if (origPath === undefined) delete process.env.PATH;
else process.env.PATH = origPath;
try {
rmSync(tmpDir, { recursive: true, force: true });
} catch {}