diff --git a/src/daemon/schtasks.startup-fallback.test.ts b/src/daemon/schtasks.startup-fallback.test.ts index 4fdadd2f110..4d8d6325366 100644 --- a/src/daemon/schtasks.startup-fallback.test.ts +++ b/src/daemon/schtasks.startup-fallback.test.ts @@ -10,6 +10,7 @@ import { resetSchtasksBaseMocks, schtasksResponses, withWindowsEnv, + writeGatewayScript, } from "./test-helpers/schtasks-fixtures.js"; const childUnref = vi.hoisted(() => vi.fn()); const spawn = vi.hoisted(() => vi.fn(() => ({ unref: childUnref }))); @@ -43,20 +44,6 @@ function resolveStartupEntryPath(env: Record) { ); } -async function writeGatewayScript(env: Record, port = 18789) { - const scriptPath = resolveTaskScriptPath(env); - await fs.mkdir(path.dirname(scriptPath), { recursive: true }); - await fs.writeFile( - scriptPath, - [ - "@echo off", - `set "OPENCLAW_GATEWAY_PORT=${port}"`, - `"C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\steipete\\AppData\\Roaming\\npm\\node_modules\\openclaw\\dist\\index.js" gateway --port ${port}`, - "", - ].join("\r\n"), - "utf8", - ); -} async function writeStartupFallbackEntry(env: Record) { const startupEntryPath = resolveStartupEntryPath(env); await fs.mkdir(path.dirname(startupEntryPath), { recursive: true }); diff --git a/src/daemon/schtasks.stop.test.ts b/src/daemon/schtasks.stop.test.ts index caa9b99c4b8..2844196e5ad 100644 --- a/src/daemon/schtasks.stop.test.ts +++ b/src/daemon/schtasks.stop.test.ts @@ -1,5 +1,3 @@ -import fs from "node:fs/promises"; -import path from "node:path"; import { PassThrough } from "node:stream"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import "./test-helpers/schtasks-base-mocks.js"; @@ -10,6 +8,7 @@ import { schtasksCalls, schtasksResponses, withWindowsEnv, + writeGatewayScript, } from "./test-helpers/schtasks-fixtures.js"; const findVerifiedGatewayListenerPidsOnPortSync = vi.hoisted(() => vi.fn<(port: number) => number[]>(() => []), @@ -20,26 +19,10 @@ vi.mock("../infra/gateway-processes.js", () => ({ findVerifiedGatewayListenerPidsOnPortSync(port), })); -const { restartScheduledTask, resolveTaskScriptPath, stopScheduledTask } = - await import("./schtasks.js"); +const { restartScheduledTask, stopScheduledTask } = await import("./schtasks.js"); const GATEWAY_PORT = 18789; const SUCCESS_RESPONSE = { code: 0, stdout: "", stderr: "" } as const; -async function writeGatewayScript(env: Record, port = GATEWAY_PORT) { - const scriptPath = resolveTaskScriptPath(env); - await fs.mkdir(path.dirname(scriptPath), { recursive: true }); - await fs.writeFile( - scriptPath, - [ - "@echo off", - `set "OPENCLAW_GATEWAY_PORT=${port}"`, - `"C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\steipete\\AppData\\Roaming\\npm\\node_modules\\openclaw\\dist\\index.js" gateway --port ${port}`, - "", - ].join("\r\n"), - "utf8", - ); -} - function pushSuccessfulSchtasksResponses(count: number) { for (let i = 0; i < count; i += 1) { schtasksResponses.push({ ...SUCCESS_RESPONSE }); @@ -80,7 +63,7 @@ async function withPreparedGatewayTask( run: (context: { env: Record; stdout: PassThrough }) => Promise, ) { await withWindowsEnv("openclaw-win-stop-", async ({ env }) => { - await writeGatewayScript(env); + await writeGatewayScript(env, GATEWAY_PORT); const stdout = new PassThrough(); await run({ env, stdout }); }); diff --git a/src/daemon/test-helpers/schtasks-fixtures.ts b/src/daemon/test-helpers/schtasks-fixtures.ts index 4762b7543eb..9755acefae7 100644 --- a/src/daemon/test-helpers/schtasks-fixtures.ts +++ b/src/daemon/test-helpers/schtasks-fixtures.ts @@ -5,6 +5,7 @@ import { vi } from "vitest"; import type { PortUsage } from "../../infra/ports-types.js"; import type { killProcessTree as killProcessTreeImpl } from "../../process/kill-tree.js"; import type { MockFn } from "../../test-utils/vitest-mock-fn.js"; +import { resolveTaskScriptPath } from "../schtasks.js"; export const schtasksResponses: Array<{ code: number; stdout: string; stderr: string }> = []; export const schtasksCalls: string[][] = []; @@ -36,3 +37,21 @@ export function resetSchtasksBaseMocks() { inspectPortUsage.mockReset(); killProcessTree.mockReset(); } + +export async function writeGatewayScript( + env: Record, + port = Number(env.OPENCLAW_GATEWAY_PORT || "18789"), +) { + const scriptPath = resolveTaskScriptPath(env); + await fs.mkdir(path.dirname(scriptPath), { recursive: true }); + await fs.writeFile( + scriptPath, + [ + "@echo off", + `set "OPENCLAW_GATEWAY_PORT=${port}"`, + `"C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\steipete\\AppData\\Roaming\\npm\\node_modules\\openclaw\\dist\\index.js" gateway --port ${port}`, + "", + ].join("\r\n"), + "utf8", + ); +}