diff --git a/tests/unit/cli-tray.test.ts b/tests/unit/cli-tray.test.ts index ec816ee5b9..e67022bbd2 100644 --- a/tests/unit/cli-tray.test.ts +++ b/tests/unit/cli-tray.test.ts @@ -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 {} diff --git a/tests/unit/cli/autostart-linux.test.ts b/tests/unit/cli/autostart-linux.test.ts index fff06e803a..4e337652f9 100644 --- a/tests/unit/cli/autostart-linux.test.ts +++ b/tests/unit/cli/autostart-linux.test.ts @@ -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 {}