From 10257114ac2daf6fa92a4a3cf34c994391ae8e98 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 27 Apr 2026 12:52:49 +0100 Subject: [PATCH] test: speed up focused unit tests --- src/acp/translator.session-rate-limit.test.ts | 4 ++ src/install-sh-version.test.ts | 44 ++++--------------- .../audit-config-include-perms.test.ts | 14 +++--- src/tts/tts.test.ts | 17 +++---- 4 files changed, 27 insertions(+), 52 deletions(-) diff --git a/src/acp/translator.session-rate-limit.test.ts b/src/acp/translator.session-rate-limit.test.ts index 6d45a518e83..f4ed08c5ea3 100644 --- a/src/acp/translator.session-rate-limit.test.ts +++ b/src/acp/translator.session-rate-limit.test.ts @@ -13,6 +13,10 @@ import { createInMemorySessionStore } from "./session.js"; import { AcpGatewayAgent } from "./translator.js"; import { createAcpConnection, createAcpGateway } from "./translator.test-helpers.js"; +vi.mock("./commands.js", () => ({ + getAvailableCommands: () => [], +})); + function createNewSessionRequest(cwd = "/tmp"): NewSessionRequest { return { cwd, diff --git a/src/install-sh-version.test.ts b/src/install-sh-version.test.ts index 7bf574d9b82..442ff041ddc 100644 --- a/src/install-sh-version.test.ts +++ b/src/install-sh-version.test.ts @@ -6,26 +6,7 @@ import { cleanupTempDirs, makeTempDir } from "../test/helpers/temp-dir.js"; const tempRoots: string[] = []; -function withFakeCli(versionOutput: string): { root: string; cliPath: string } { - const root = makeTempDir(tempRoots, "openclaw-install-sh-"); - const cliPath = path.join(root, "openclaw"); - const escapedOutput = versionOutput.replace(/'/g, "'\\''"); - fs.writeFileSync( - cliPath, - `#!/bin/sh -printf '%s\n' '${escapedOutput}' -`, - "utf-8", - ); - fs.chmodSync(cliPath, 0o755); - return { root, cliPath }; -} - -function resolveInstallerVersionCases(params: { - cliPaths: string[]; - stdinCliPath: string; - stdinCwd: string; -}): string[] { +function resolveInstallerVersionCases(params: { stdinCwd: string }): string[] { const installerPath = path.join(process.cwd(), "scripts", "install.sh"); const installerSource = fs.readFileSync(installerPath, "utf-8"); const versionHelperStart = installerSource.indexOf("load_install_version_helpers() {"); @@ -39,22 +20,21 @@ function resolveInstallerVersionCases(params: { [ "-c", `${versionHelperSource} -for openclaw_bin in "\${@:3}"; do - OPENCLAW_BIN="$openclaw_bin" - resolve_openclaw_version -done +fake_openclaw_decorated() { printf '%s\\n' 'OpenClaw 2026.3.10 (abcdef0)'; } +fake_openclaw_raw() { printf '%s\\n' "OpenClaw dev's build"; } +OPENCLAW_BIN=fake_openclaw_decorated resolve_openclaw_version +OPENCLAW_BIN=fake_openclaw_raw resolve_openclaw_version ( - cd "$2" - FAKE_OPENCLAW_BIN="\${@:1:1}" bash -s <<'OPENCLAW_STDIN_INSTALLER' + cd "$1" + bash -s <<'OPENCLAW_STDIN_INSTALLER' ${versionHelperSource} -OPENCLAW_BIN="$FAKE_OPENCLAW_BIN" +fake_openclaw_stdin() { printf '%s\\n' 'OpenClaw 2026.3.10 (abcdef0)'; } +OPENCLAW_BIN=fake_openclaw_stdin resolve_openclaw_version OPENCLAW_STDIN_INSTALLER )`, "openclaw-version-test", - params.stdinCliPath, params.stdinCwd, - ...params.cliPaths, ], { cwd: process.cwd(), @@ -76,10 +56,6 @@ describe("install.sh version resolution", () => { it.runIf(process.platform !== "win32")( "parses CLI versions and keeps stdin helpers isolated from cwd", () => { - const decorated = withFakeCli("OpenClaw 2026.3.10 (abcdef0)"); - const raw = withFakeCli("OpenClaw dev's build"); - const stdinFixture = withFakeCli("OpenClaw 2026.3.10 (abcdef0)"); - const hostileCwd = makeTempDir(tempRoots, "openclaw-install-stdin-"); const hostileHelper = path.join( hostileCwd, @@ -100,8 +76,6 @@ extract_openclaw_semver() { expect( resolveInstallerVersionCases({ - cliPaths: [decorated.cliPath, raw.cliPath], - stdinCliPath: stdinFixture.cliPath, stdinCwd: hostileCwd, }), ).toEqual(["2026.3.10", "OpenClaw dev's build", "2026.3.10"]); diff --git a/src/security/audit-config-include-perms.test.ts b/src/security/audit-config-include-perms.test.ts index 649cce7472f..c55a67989e5 100644 --- a/src/security/audit-config-include-perms.test.ts +++ b/src/security/audit-config-include-perms.test.ts @@ -1,4 +1,4 @@ -import fs from "node:fs/promises"; +import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { describe, expect, it } from "vitest"; @@ -9,22 +9,22 @@ const isWindows = process.platform === "win32"; describe("security audit config include permissions", () => { it("flags group/world-readable config include files", async () => { - const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-include-perms-")); + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-include-perms-")); const stateDir = path.join(tmp, "state"); - await fs.mkdir(stateDir, { recursive: true, mode: 0o700 }); + fs.mkdirSync(stateDir, { recursive: true, mode: 0o700 }); const includePath = path.join(stateDir, "extra.json5"); - await fs.writeFile(includePath, "{ logging: { redactSensitive: 'off' } }\n", "utf-8"); + fs.writeFileSync(includePath, "{ logging: { redactSensitive: 'off' } }\n", "utf-8"); if (isWindows) { const { execSync } = await import("node:child_process"); execSync(`icacls "${includePath}" /grant Everyone:W`, { stdio: "ignore" }); } else { - await fs.chmod(includePath, 0o644); + fs.chmodSync(includePath, 0o644); } const configPath = path.join(stateDir, "openclaw.json"); - await fs.writeFile(configPath, `{ "$include": "./extra.json5" }\n`, "utf-8"); - await fs.chmod(configPath, 0o600); + fs.writeFileSync(configPath, `{ "$include": "./extra.json5" }\n`, "utf-8"); + fs.chmodSync(configPath, 0o600); const user = "DESKTOP-TEST\\Tester"; const execIcacls = isWindows diff --git a/src/tts/tts.test.ts b/src/tts/tts.test.ts index c9e3a93b70d..891a3626378 100644 --- a/src/tts/tts.test.ts +++ b/src/tts/tts.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; const loadBundledPluginPublicSurfaceModuleSync = vi.hoisted(() => vi.fn()); const loadActivatedBundledPluginPublicSurfaceModuleSync = vi.hoisted(() => vi.fn()); @@ -32,26 +32,23 @@ vi.mock("../plugin-sdk/facade-runtime.js", () => ({ })); describe("tts runtime facade", () => { - let ttsModulePromise: Promise | undefined; + let tts: typeof import("./tts.js"); + + beforeAll(async () => { + tts = await import("./tts.js"); + }); beforeEach(() => { loadActivatedBundledPluginPublicSurfaceModuleSync.mockReset(); loadBundledPluginPublicSurfaceModuleSync.mockReset(); }); - function importTtsModule() { - ttsModulePromise ??= import("./tts.js"); - return ttsModulePromise; - } - - it("loads speech-core lazily after module import", async () => { + it("loads speech-core lazily after module import", () => { const buildTtsSystemPromptHint = vi.fn().mockReturnValue("hint"); loadActivatedBundledPluginPublicSurfaceModuleSync.mockReturnValue({ buildTtsSystemPromptHint, }); - const tts = await importTtsModule(); - expect(loadActivatedBundledPluginPublicSurfaceModuleSync).not.toHaveBeenCalled(); expect(tts.buildTtsSystemPromptHint({} as never)).toBe("hint"); expect(loadActivatedBundledPluginPublicSurfaceModuleSync).toHaveBeenCalledTimes(1);