mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 13:44:32 +00:00
test(daemon): dedupe schtasks fixtures and cover state-dir override
This commit is contained in:
@@ -65,147 +65,150 @@ describe("resolveTaskScriptPath", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("readScheduledTaskCommand", () => {
|
describe("readScheduledTaskCommand", () => {
|
||||||
it("parses script with quoted arguments containing spaces", async () => {
|
async function withScheduledTaskScript(
|
||||||
|
options: {
|
||||||
|
scriptLines?: string[];
|
||||||
|
env?:
|
||||||
|
| Record<string, string | undefined>
|
||||||
|
| ((tmpDir: string) => Record<string, string | undefined>);
|
||||||
|
},
|
||||||
|
run: (env: Record<string, string | undefined>) => Promise<void>,
|
||||||
|
) {
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-schtasks-test-"));
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-schtasks-test-"));
|
||||||
try {
|
try {
|
||||||
const scriptPath = path.join(tmpDir, ".openclaw", "gateway.cmd");
|
const extraEnv = typeof options.env === "function" ? options.env(tmpDir) : options.env;
|
||||||
await fs.mkdir(path.dirname(scriptPath), { recursive: true });
|
const env = {
|
||||||
// Use forward slashes which work in Windows cmd and avoid escape parsing issues
|
USERPROFILE: tmpDir,
|
||||||
await fs.writeFile(
|
OPENCLAW_PROFILE: "default",
|
||||||
scriptPath,
|
...extraEnv,
|
||||||
["@echo off", '"C:/Program Files/Node/node.exe" gateway.js'].join("\r\n"),
|
};
|
||||||
"utf8",
|
if (options.scriptLines) {
|
||||||
);
|
const scriptPath = resolveTaskScriptPath(env);
|
||||||
|
await fs.mkdir(path.dirname(scriptPath), { recursive: true });
|
||||||
const env = { USERPROFILE: tmpDir, OPENCLAW_PROFILE: "default" };
|
await fs.writeFile(scriptPath, options.scriptLines.join("\r\n"), "utf8");
|
||||||
const result = await readScheduledTaskCommand(env);
|
}
|
||||||
expect(result).toEqual({
|
await run(env);
|
||||||
programArguments: ["C:/Program Files/Node/node.exe", "gateway.js"],
|
|
||||||
});
|
|
||||||
} finally {
|
} finally {
|
||||||
await fs.rm(tmpDir, { recursive: true, force: true });
|
await fs.rm(tmpDir, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it("parses script with quoted arguments containing spaces", async () => {
|
||||||
|
await withScheduledTaskScript(
|
||||||
|
{
|
||||||
|
// Use forward slashes which work in Windows cmd and avoid escape parsing issues.
|
||||||
|
scriptLines: ["@echo off", '"C:/Program Files/Node/node.exe" gateway.js'],
|
||||||
|
},
|
||||||
|
async (env) => {
|
||||||
|
const result = await readScheduledTaskCommand(env);
|
||||||
|
expect(result).toEqual({
|
||||||
|
programArguments: ["C:/Program Files/Node/node.exe", "gateway.js"],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns null when script does not exist", async () => {
|
it("returns null when script does not exist", async () => {
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-schtasks-test-"));
|
await withScheduledTaskScript({}, async (env) => {
|
||||||
try {
|
|
||||||
const env = { USERPROFILE: tmpDir, OPENCLAW_PROFILE: "default" };
|
|
||||||
const result = await readScheduledTaskCommand(env);
|
const result = await readScheduledTaskCommand(env);
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
} finally {
|
});
|
||||||
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns null when script has no command", async () => {
|
it("returns null when script has no command", async () => {
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-schtasks-test-"));
|
await withScheduledTaskScript(
|
||||||
try {
|
{ scriptLines: ["@echo off", "rem This is just a comment"] },
|
||||||
const scriptPath = path.join(tmpDir, ".openclaw", "gateway.cmd");
|
async (env) => {
|
||||||
await fs.mkdir(path.dirname(scriptPath), { recursive: true });
|
const result = await readScheduledTaskCommand(env);
|
||||||
await fs.writeFile(
|
expect(result).toBeNull();
|
||||||
scriptPath,
|
},
|
||||||
["@echo off", "rem This is just a comment"].join("\r\n"),
|
);
|
||||||
"utf8",
|
|
||||||
);
|
|
||||||
|
|
||||||
const env = { USERPROFILE: tmpDir, OPENCLAW_PROFILE: "default" };
|
|
||||||
const result = await readScheduledTaskCommand(env);
|
|
||||||
expect(result).toBeNull();
|
|
||||||
} finally {
|
|
||||||
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("parses full script with all components", async () => {
|
it("parses full script with all components", async () => {
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-schtasks-test-"));
|
await withScheduledTaskScript(
|
||||||
try {
|
{
|
||||||
const scriptPath = path.join(tmpDir, ".openclaw", "gateway.cmd");
|
scriptLines: [
|
||||||
await fs.mkdir(path.dirname(scriptPath), { recursive: true });
|
|
||||||
await fs.writeFile(
|
|
||||||
scriptPath,
|
|
||||||
[
|
|
||||||
"@echo off",
|
"@echo off",
|
||||||
"rem OpenClaw Gateway",
|
"rem OpenClaw Gateway",
|
||||||
"cd /d C:\\Projects\\openclaw",
|
"cd /d C:\\Projects\\openclaw",
|
||||||
"set NODE_ENV=production",
|
"set NODE_ENV=production",
|
||||||
"set OPENCLAW_PORT=18789",
|
"set OPENCLAW_PORT=18789",
|
||||||
"node gateway.js --verbose",
|
"node gateway.js --verbose",
|
||||||
].join("\r\n"),
|
],
|
||||||
"utf8",
|
},
|
||||||
);
|
async (env) => {
|
||||||
|
const result = await readScheduledTaskCommand(env);
|
||||||
const env = { USERPROFILE: tmpDir, OPENCLAW_PROFILE: "default" };
|
expect(result).toEqual({
|
||||||
const result = await readScheduledTaskCommand(env);
|
programArguments: ["node", "gateway.js", "--verbose"],
|
||||||
expect(result).toEqual({
|
workingDirectory: "C:\\Projects\\openclaw",
|
||||||
programArguments: ["node", "gateway.js", "--verbose"],
|
environment: {
|
||||||
workingDirectory: "C:\\Projects\\openclaw",
|
NODE_ENV: "production",
|
||||||
environment: {
|
OPENCLAW_PORT: "18789",
|
||||||
NODE_ENV: "production",
|
},
|
||||||
OPENCLAW_PORT: "18789",
|
});
|
||||||
},
|
},
|
||||||
});
|
);
|
||||||
} finally {
|
|
||||||
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("parses command with Windows backslash paths", async () => {
|
it("parses command with Windows backslash paths", async () => {
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-schtasks-test-"));
|
await withScheduledTaskScript(
|
||||||
try {
|
{
|
||||||
const scriptPath = path.join(tmpDir, ".openclaw", "gateway.cmd");
|
scriptLines: [
|
||||||
await fs.mkdir(path.dirname(scriptPath), { recursive: true });
|
|
||||||
await fs.writeFile(
|
|
||||||
scriptPath,
|
|
||||||
[
|
|
||||||
"@echo off",
|
"@echo off",
|
||||||
'"C:\\Program Files\\nodejs\\node.exe" C:\\Users\\test\\AppData\\Roaming\\npm\\node_modules\\openclaw\\dist\\index.js gateway --port 18789',
|
'"C:\\Program Files\\nodejs\\node.exe" C:\\Users\\test\\AppData\\Roaming\\npm\\node_modules\\openclaw\\dist\\index.js gateway --port 18789',
|
||||||
].join("\r\n"),
|
|
||||||
"utf8",
|
|
||||||
);
|
|
||||||
|
|
||||||
const env = { USERPROFILE: tmpDir, OPENCLAW_PROFILE: "default" };
|
|
||||||
const result = await readScheduledTaskCommand(env);
|
|
||||||
expect(result).toEqual({
|
|
||||||
programArguments: [
|
|
||||||
"C:\\Program Files\\nodejs\\node.exe",
|
|
||||||
"C:\\Users\\test\\AppData\\Roaming\\npm\\node_modules\\openclaw\\dist\\index.js",
|
|
||||||
"gateway",
|
|
||||||
"--port",
|
|
||||||
"18789",
|
|
||||||
],
|
],
|
||||||
});
|
},
|
||||||
} finally {
|
async (env) => {
|
||||||
await fs.rm(tmpDir, { recursive: true, force: true });
|
const result = await readScheduledTaskCommand(env);
|
||||||
}
|
expect(result).toEqual({
|
||||||
|
programArguments: [
|
||||||
|
"C:\\Program Files\\nodejs\\node.exe",
|
||||||
|
"C:\\Users\\test\\AppData\\Roaming\\npm\\node_modules\\openclaw\\dist\\index.js",
|
||||||
|
"gateway",
|
||||||
|
"--port",
|
||||||
|
"18789",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("preserves UNC paths in command arguments", async () => {
|
it("preserves UNC paths in command arguments", async () => {
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-schtasks-test-"));
|
await withScheduledTaskScript(
|
||||||
try {
|
{
|
||||||
const scriptPath = path.join(tmpDir, ".openclaw", "gateway.cmd");
|
scriptLines: [
|
||||||
await fs.mkdir(path.dirname(scriptPath), { recursive: true });
|
|
||||||
await fs.writeFile(
|
|
||||||
scriptPath,
|
|
||||||
[
|
|
||||||
"@echo off",
|
"@echo off",
|
||||||
'"\\\\fileserver\\OpenClaw Share\\node.exe" "\\\\fileserver\\OpenClaw Share\\dist\\index.js" gateway --port 18789',
|
'"\\\\fileserver\\OpenClaw Share\\node.exe" "\\\\fileserver\\OpenClaw Share\\dist\\index.js" gateway --port 18789',
|
||||||
].join("\r\n"),
|
|
||||||
"utf8",
|
|
||||||
);
|
|
||||||
|
|
||||||
const env = { USERPROFILE: tmpDir, OPENCLAW_PROFILE: "default" };
|
|
||||||
const result = await readScheduledTaskCommand(env);
|
|
||||||
expect(result).toEqual({
|
|
||||||
programArguments: [
|
|
||||||
"\\\\fileserver\\OpenClaw Share\\node.exe",
|
|
||||||
"\\\\fileserver\\OpenClaw Share\\dist\\index.js",
|
|
||||||
"gateway",
|
|
||||||
"--port",
|
|
||||||
"18789",
|
|
||||||
],
|
],
|
||||||
});
|
},
|
||||||
} finally {
|
async (env) => {
|
||||||
await fs.rm(tmpDir, { recursive: true, force: true });
|
const result = await readScheduledTaskCommand(env);
|
||||||
}
|
expect(result).toEqual({
|
||||||
|
programArguments: [
|
||||||
|
"\\\\fileserver\\OpenClaw Share\\node.exe",
|
||||||
|
"\\\\fileserver\\OpenClaw Share\\dist\\index.js",
|
||||||
|
"gateway",
|
||||||
|
"--port",
|
||||||
|
"18789",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reads script from OPENCLAW_STATE_DIR override", async () => {
|
||||||
|
await withScheduledTaskScript(
|
||||||
|
{
|
||||||
|
env: (tmpDir) => ({ OPENCLAW_STATE_DIR: path.join(tmpDir, "custom-state") }),
|
||||||
|
scriptLines: ["@echo off", "node gateway.js --from-state-dir"],
|
||||||
|
},
|
||||||
|
async (env) => {
|
||||||
|
const result = await readScheduledTaskCommand(env);
|
||||||
|
expect(result).toEqual({
|
||||||
|
programArguments: ["node", "gateway.js", "--from-state-dir"],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user