From 29a48ab129023f5d488160c9bff305f32a037f48 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 18:32:11 +0100 Subject: [PATCH] test: share systemd stage fixture --- src/daemon/systemd.test.ts | 82 +++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/daemon/systemd.test.ts b/src/daemon/systemd.test.ts index ae50ea1c9d3..e9f9ec68e1e 100644 --- a/src/daemon/systemd.test.ts +++ b/src/daemon/systemd.test.ts @@ -643,12 +643,14 @@ describe("readSystemdServiceExecStart", () => { }); describe("stageSystemdService", () => { - beforeEach(() => { - vi.restoreAllMocks(); - execFileMock.mockReset(); - }); - - it("writes dotenv-backed values to a separate env file and keeps inline env minimal", async () => { + async function withStageFixture( + run: (context: { + env: Record; + stateDir: string; + unitPath: string; + envFilePath: string; + }) => Promise, + ): Promise { const tempHomeRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-systemd-stage-")); const home = path.join(tempHomeRoot, "home"); const stateDir = path.join(home, ".openclaw"); @@ -660,19 +662,36 @@ describe("stageSystemdService", () => { const unitPath = resolveSystemdUserUnitPath(env); const envFilePath = path.join(stateDir, "gateway.systemd.env"); - await fs.mkdir(stateDir, { recursive: true }); - await fs.writeFile( - path.join(stateDir, ".env"), - ["OPENCLAW_GATEWAY_TOKEN=dotenv-token", "LLM_API_KEY=dotenv-key"].join("\n"), - "utf8", - ); + try { + await fs.mkdir(stateDir, { recursive: true }); + await run({ env, stateDir, unitPath, envFilePath }); + } finally { + await fs.rm(tempHomeRoot, { recursive: true, force: true }); + } + } + function mockSystemctlStatusOk(): void { execFileMock.mockImplementationOnce((_cmd, args, _opts, cb) => { assertUserSystemctlArgs(args, "status"); cb(null, "", ""); }); + } + + beforeEach(() => { + vi.restoreAllMocks(); + execFileMock.mockReset(); + }); + + it("writes dotenv-backed values to a separate env file and keeps inline env minimal", async () => { + await withStageFixture(async ({ env, stateDir, unitPath, envFilePath }) => { + await fs.writeFile( + path.join(stateDir, ".env"), + ["OPENCLAW_GATEWAY_TOKEN=dotenv-token", "LLM_API_KEY=dotenv-key"].join("\n"), + "utf8", + ); + + mockSystemctlStatusOk(); - try { await stageSystemdService({ env, stdout: { write: vi.fn() } as unknown as NodeJS.WritableStream, @@ -697,36 +716,19 @@ describe("stageSystemdService", () => { expect(unit).not.toContain("Environment=LLM_API_KEY=dotenv-key"); expect(envFile).toBe("OPENCLAW_GATEWAY_TOKEN=dotenv-token\nLLM_API_KEY=dotenv-key\n"); expect(envFileStat.mode & 0o777).toBe(0o600); - } finally { - await fs.rm(tempHomeRoot, { recursive: true, force: true }); - } + }); }); it("keeps inline overrides out of the generated env file", async () => { - const tempHomeRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-systemd-stage-")); - const home = path.join(tempHomeRoot, "home"); - const stateDir = path.join(home, ".openclaw"); - const env = { - HOME: home, - OPENCLAW_STATE_DIR: stateDir, - OPENCLAW_SYSTEMD_UNIT: "openclaw-gateway-stage-test", - }; - const unitPath = resolveSystemdUserUnitPath(env); - const envFilePath = path.join(stateDir, "gateway.systemd.env"); + await withStageFixture(async ({ env, stateDir, unitPath, envFilePath }) => { + await fs.writeFile( + path.join(stateDir, ".env"), + ["OPENCLAW_GATEWAY_TOKEN=stale-token", "LLM_API_KEY=dotenv-key"].join("\n"), + "utf8", + ); - await fs.mkdir(stateDir, { recursive: true }); - await fs.writeFile( - path.join(stateDir, ".env"), - ["OPENCLAW_GATEWAY_TOKEN=stale-token", "LLM_API_KEY=dotenv-key"].join("\n"), - "utf8", - ); + mockSystemctlStatusOk(); - execFileMock.mockImplementationOnce((_cmd, args, _opts, cb) => { - assertUserSystemctlArgs(args, "status"); - cb(null, "", ""); - }); - - try { await stageSystemdService({ env, stdout: { write: vi.fn() } as unknown as NodeJS.WritableStream, @@ -746,9 +748,7 @@ describe("stageSystemdService", () => { expect(unit).toContain(`EnvironmentFile=-${envFilePath}`); expect(unit).toContain("Environment=OPENCLAW_GATEWAY_TOKEN=fresh-token"); expect(envFile).toBe("LLM_API_KEY=dotenv-key\n"); - } finally { - await fs.rm(tempHomeRoot, { recursive: true, force: true }); - } + }); }); });