test: speed up focused unit tests

This commit is contained in:
Peter Steinberger
2026-04-27 12:52:49 +01:00
parent a041ea7ca7
commit 10257114ac
4 changed files with 27 additions and 52 deletions

View File

@@ -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,

View File

@@ -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"]);

View File

@@ -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

View File

@@ -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<typeof import("./tts.js")> | 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);