From c394c5fa998b858afa671bf26980d926d5676839 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 19:33:46 +0000 Subject: [PATCH] test(daemon): dedupe schtasks install fixture and cover empty env omission --- src/daemon/schtasks.install.test.ts | 38 ++++++++++++++++------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/daemon/schtasks.install.test.ts b/src/daemon/schtasks.install.test.ts index c7bfb41710f..36051aff200 100644 --- a/src/daemon/schtasks.install.test.ts +++ b/src/daemon/schtasks.install.test.ts @@ -19,13 +19,23 @@ beforeEach(() => { }); describe("installScheduledTask", () => { - it("writes quoted set assignments and escapes metacharacters", async () => { + async function withUserProfileDir( + run: (tmpDir: string, env: Record) => Promise, + ) { const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-schtasks-install-")); + const env = { + USERPROFILE: tmpDir, + OPENCLAW_PROFILE: "default", + }; try { - const env = { - USERPROFILE: tmpDir, - OPENCLAW_PROFILE: "default", - }; + await run(tmpDir, env); + } finally { + await fs.rm(tmpDir, { recursive: true, force: true }); + } + } + + it("writes quoted set assignments and escapes metacharacters", async () => { + await withUserProfileDir(async (_tmpDir, env) => { const { scriptPath } = await installScheduledTask({ env, stdout: new PassThrough(), @@ -46,6 +56,7 @@ describe("installScheduledTask", () => { OC_PERCENT: "%TEMP%", OC_BANG: "!token!", OC_QUOTE: 'he said "hi"', + OC_EMPTY: "", }, }); @@ -59,6 +70,7 @@ describe("installScheduledTask", () => { expect(script).toContain('set "OC_PERCENT=%%TEMP%%"'); expect(script).toContain('set "OC_BANG=^!token^!"'); expect(script).toContain('set "OC_QUOTE=he said ^"hi^""'); + expect(script).not.toContain('set "OC_EMPTY='); expect(script).not.toContain("set OC_INJECT="); const parsed = await readScheduledTaskCommand(env); @@ -82,22 +94,16 @@ describe("installScheduledTask", () => { OC_BANG: "!token!", OC_QUOTE: 'he said "hi"', }); + expect(parsed?.environment).not.toHaveProperty("OC_EMPTY"); expect(schtasksCalls[0]).toEqual(["/Query"]); expect(schtasksCalls[1]?.[0]).toBe("/Create"); expect(schtasksCalls[2]).toEqual(["/Run", "/TN", "OpenClaw Gateway"]); - } finally { - await fs.rm(tmpDir, { recursive: true, force: true }); - } + }); }); it("rejects line breaks in command arguments, env vars, and descriptions", async () => { - const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-schtasks-install-")); - const env = { - USERPROFILE: tmpDir, - OPENCLAW_PROFILE: "default", - }; - try { + await withUserProfileDir(async (_tmpDir, env) => { await expect( installScheduledTask({ env, @@ -125,8 +131,6 @@ describe("installScheduledTask", () => { environment: {}, }), ).rejects.toThrow(/Task description cannot contain CR or LF/); - } finally { - await fs.rm(tmpDir, { recursive: true, force: true }); - } + }); }); });