From 12a59b0a18e177c6bbe22fed2ebdd9a8749061ba Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 17 Apr 2026 02:45:54 +0100 Subject: [PATCH] test: trim hotspot wait overhead --- src/canvas-host/server.test.ts | 21 ++--------- src/node-host/invoke-system-run.test.ts | 46 ++++++++++++++++++------- ui/src/ui/views/chat.test.ts | 2 +- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/canvas-host/server.test.ts b/src/canvas-host/server.test.ts index ec95a74e731..6cb6894668b 100644 --- a/src/canvas-host/server.test.ts +++ b/src/canvas-host/server.test.ts @@ -16,7 +16,6 @@ type MockWatcher = { __emit: (event: string, ...args: unknown[]) => void; }; -const CANVAS_RELOAD_TIMEOUT_MS = 10_000; const CANVAS_RELOAD_TEST_TIMEOUT_MS = 20_000; type TrackingWebSocket = { @@ -358,26 +357,10 @@ describe("canvas host", () => { const ws = TrackingWebSocketServerClass.latestSocket; expect(ws).toBeTruthy(); - const msg = new Promise((resolve, reject) => { - const deadline = Date.now() + CANVAS_RELOAD_TIMEOUT_MS; - const poll = () => { - const value = ws?.sent[0]; - if (value) { - resolve(value); - return; - } - if (Date.now() >= deadline) { - reject(new Error("reload timeout")); - return; - } - void sleep(10).then(poll, reject); - }; - poll(); - }); - await fs.writeFile(index, "v2", "utf8"); watcher.__emit("all", "change", index); - expect(await msg).toBe("reload"); + await sleep(15); + expect(ws?.sent[0]).toBe("reload"); } finally { await handler.close(); } diff --git a/src/node-host/invoke-system-run.test.ts b/src/node-host/invoke-system-run.test.ts index 36fdff4ce6c..21dd8cb1d97 100644 --- a/src/node-host/invoke-system-run.test.ts +++ b/src/node-host/invoke-system-run.test.ts @@ -2,7 +2,17 @@ import crypto from "node:crypto"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; -import { afterEach, beforeEach, describe, expect, it, type Mock, vi } from "vitest"; +import { + afterAll, + afterEach, + beforeAll, + beforeEach, + describe, + expect, + it, + type Mock, + vi, +} from "vitest"; import { clearRuntimeConfigSnapshot, setRuntimeConfigSnapshot } from "../config/config.js"; import type { SystemRunApprovalPlan } from "../infra/exec-approvals.js"; import { loadExecApprovals, saveExecApprovals } from "../infra/exec-approvals.js"; @@ -32,12 +42,30 @@ describe("formatSystemRunAllowlistMissMessage", () => { }); describe("handleSystemRunInvoke mac app exec host routing", () => { + let sharedFixtureRoot = ""; + let sharedFixtureId = 0; let testOpenClawHome = ""; let previousOpenClawHome: string | undefined; + beforeAll(() => { + sharedFixtureRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-node-host-fixtures-")); + }); + + afterAll(() => { + if (sharedFixtureRoot) { + fs.rmSync(sharedFixtureRoot, { recursive: true, force: true }); + } + }); + + function createFixtureDir(prefix: string): string { + const dir = path.join(sharedFixtureRoot, `${prefix}${sharedFixtureId++}`); + fs.mkdirSync(dir, { recursive: true }); + return dir; + } + beforeEach(() => { previousOpenClawHome = process.env.OPENCLAW_HOME; - testOpenClawHome = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-node-host-home-")); + testOpenClawHome = createFixtureDir("openclaw-node-host-home-"); process.env.OPENCLAW_HOME = testOpenClawHome; clearRuntimeConfigSnapshot(); }); @@ -49,10 +77,7 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { } else { process.env.OPENCLAW_HOME = previousOpenClawHome; } - if (testOpenClawHome) { - fs.rmSync(testOpenClawHome, { recursive: true, force: true }); - testOpenClawHome = ""; - } + testOpenClawHome = ""; }); function createLocalRunResult(stdout = "local-ok") { @@ -238,7 +263,7 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { approvals: Parameters[0]; run: (ctx: { tempHome: string }) => Promise; }): Promise { - const tempHome = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-exec-approvals-")); + const tempHome = createFixtureDir("openclaw-exec-approvals-"); const previousOpenClawHome = process.env.OPENCLAW_HOME; process.env.OPENCLAW_HOME = tempHome; saveExecApprovals(params.approvals); @@ -250,7 +275,6 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { } else { process.env.OPENCLAW_HOME = previousOpenClawHome; } - fs.rmSync(tempHome, { recursive: true, force: true }); } } @@ -258,7 +282,7 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { tmpPrefix: string; run: (ctx: { link: string; expected: string }) => Promise; }): Promise { - const tmp = fs.mkdtempSync(path.join(os.tmpdir(), params.tmpPrefix)); + const tmp = createFixtureDir(params.tmpPrefix); const binDir = path.join(tmp, "bin"); fs.mkdirSync(binDir, { recursive: true }); const link = path.join(binDir, "poccmd"); @@ -274,7 +298,6 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { } else { process.env.PATH = oldPath; } - fs.rmSync(tmp, { recursive: true, force: true }); } } @@ -282,7 +305,7 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { runtime: "bun" | "deno" | "jiti" | "tsx"; run: () => Promise; }): Promise { - const tmp = fs.mkdtempSync(path.join(os.tmpdir(), `openclaw-${params.runtime}-path-`)); + const tmp = createFixtureDir(`openclaw-${params.runtime}-path-`); const binDir = path.join(tmp, "bin"); fs.mkdirSync(binDir, { recursive: true }); const runtimePath = @@ -305,7 +328,6 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { } else { process.env.PATH = oldPath; } - fs.rmSync(tmp, { recursive: true, force: true }); } } diff --git a/ui/src/ui/views/chat.test.ts b/ui/src/ui/views/chat.test.ts index 12506801320..83f038942a1 100644 --- a/ui/src/ui/views/chat.test.ts +++ b/ui/src/ui/views/chat.test.ts @@ -147,7 +147,7 @@ function createChatHeaderState( } function flushTasks() { - return new Promise((resolve) => setTimeout(resolve, 0)); + return new Promise((resolve) => queueMicrotask(resolve)); } function createProps(overrides: Partial = {}): ChatProps {