refactor(test): reuse env helper in gateway tool e2e

This commit is contained in:
Peter Steinberger
2026-02-21 18:19:20 +00:00
parent bc037dfe01
commit 8fd8988ff7

View File

@@ -2,7 +2,7 @@ import fs from "node:fs/promises";
import os from "node:os"; import os from "node:os";
import path from "node:path"; import path from "node:path";
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from "vitest";
import { captureEnv } from "../test-utils/env.js"; import { withEnvAsync } from "../test-utils/env.js";
import "./test-helpers/fast-core-tools.js"; import "./test-helpers/fast-core-tools.js";
import { createOpenClawTools } from "./openclaw-tools.js"; import { createOpenClawTools } from "./openclaw-tools.js";
@@ -31,48 +31,49 @@ describe("gateway tool", () => {
it("schedules SIGUSR1 restart", async () => { it("schedules SIGUSR1 restart", async () => {
vi.useFakeTimers(); vi.useFakeTimers();
const kill = vi.spyOn(process, "kill").mockImplementation(() => true); const kill = vi.spyOn(process, "kill").mockImplementation(() => true);
const envSnapshot = captureEnv(["OPENCLAW_STATE_DIR", "OPENCLAW_PROFILE"]);
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-test-")); const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-test-"));
process.env.OPENCLAW_STATE_DIR = stateDir;
process.env.OPENCLAW_PROFILE = "isolated";
try { try {
const tool = createOpenClawTools({ await withEnvAsync(
config: { commands: { restart: true } }, { OPENCLAW_STATE_DIR: stateDir, OPENCLAW_PROFILE: "isolated" },
}).find((candidate) => candidate.name === "gateway"); async () => {
expect(tool).toBeDefined(); const tool = createOpenClawTools({
if (!tool) { config: { commands: { restart: true } },
throw new Error("missing gateway tool"); }).find((candidate) => candidate.name === "gateway");
} expect(tool).toBeDefined();
if (!tool) {
throw new Error("missing gateway tool");
}
const result = await tool.execute("call1", { const result = await tool.execute("call1", {
action: "restart", action: "restart",
delayMs: 0, delayMs: 0,
}); });
expect(result.details).toMatchObject({ expect(result.details).toMatchObject({
ok: true, ok: true,
pid: process.pid, pid: process.pid,
signal: "SIGUSR1", signal: "SIGUSR1",
delayMs: 0, delayMs: 0,
}); });
const sentinelPath = path.join(stateDir, "restart-sentinel.json"); const sentinelPath = path.join(stateDir, "restart-sentinel.json");
const raw = await fs.readFile(sentinelPath, "utf-8"); const raw = await fs.readFile(sentinelPath, "utf-8");
const parsed = JSON.parse(raw) as { const parsed = JSON.parse(raw) as {
payload?: { kind?: string; doctorHint?: string | null }; payload?: { kind?: string; doctorHint?: string | null };
}; };
expect(parsed.payload?.kind).toBe("restart"); expect(parsed.payload?.kind).toBe("restart");
expect(parsed.payload?.doctorHint).toBe( expect(parsed.payload?.doctorHint).toBe(
"Run: openclaw --profile isolated doctor --non-interactive", "Run: openclaw --profile isolated doctor --non-interactive",
);
expect(kill).not.toHaveBeenCalled();
await vi.runAllTimersAsync();
expect(kill).toHaveBeenCalledWith(process.pid, "SIGUSR1");
},
); );
expect(kill).not.toHaveBeenCalled();
await vi.runAllTimersAsync();
expect(kill).toHaveBeenCalledWith(process.pid, "SIGUSR1");
} finally { } finally {
kill.mockRestore(); kill.mockRestore();
vi.useRealTimers(); vi.useRealTimers();
envSnapshot.restore();
await fs.rm(stateDir, { recursive: true, force: true }); await fs.rm(stateDir, { recursive: true, force: true });
} }
}); });