perf(test): remove resetModules from config/sandbox/message suites

This commit is contained in:
Peter Steinberger
2026-02-13 15:58:08 +00:00
parent 02fe0c840e
commit de7d94d9e2
3 changed files with 114 additions and 184 deletions

View File

@@ -1,20 +1,9 @@
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { ensureSandboxWorkspaceForSession, resolveSandboxContext } from "./sandbox.js";
describe("resolveSandboxContext", () => {
it("does not sandbox the agent main session in non-main mode", async () => {
vi.resetModules();
const spawn = vi.fn(() => {
throw new Error("spawn should not be called");
});
vi.doMock("node:child_process", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:child_process")>();
return { ...actual, spawn };
});
const { resolveSandboxContext } = await import("./sandbox.js");
const cfg: OpenClawConfig = {
agents: {
defaults: {
@@ -31,24 +20,9 @@ describe("resolveSandboxContext", () => {
});
expect(result).toBeNull();
expect(spawn).not.toHaveBeenCalled();
vi.doUnmock("node:child_process");
}, 15_000);
it("does not create a sandbox workspace for the agent main session in non-main mode", async () => {
vi.resetModules();
const spawn = vi.fn(() => {
throw new Error("spawn should not be called");
});
vi.doMock("node:child_process", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:child_process")>();
return { ...actual, spawn };
});
const { ensureSandboxWorkspaceForSession } = await import("./sandbox.js");
const cfg: OpenClawConfig = {
agents: {
defaults: {
@@ -65,25 +39,9 @@ describe("resolveSandboxContext", () => {
});
expect(result).toBeNull();
expect(spawn).not.toHaveBeenCalled();
vi.doUnmock("node:child_process");
}, 15_000);
it("treats main session aliases as main in non-main mode", async () => {
vi.resetModules();
const spawn = vi.fn(() => {
throw new Error("spawn should not be called");
});
vi.doMock("node:child_process", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:child_process")>();
return { ...actual, spawn };
});
const { ensureSandboxWorkspaceForSession, resolveSandboxContext } =
await import("./sandbox.js");
const cfg: OpenClawConfig = {
session: { mainKey: "work" },
agents: {
@@ -125,9 +83,5 @@ describe("resolveSandboxContext", () => {
workspaceDir: "/tmp/openclaw-test",
}),
).toBeNull();
expect(spawn).not.toHaveBeenCalled();
vi.doUnmock("node:child_process");
}, 15_000);
});

View File

@@ -1,118 +1,107 @@
import fs from "node:fs/promises";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { withEnvOverride, withTempHome } from "./test-helpers.js";
import { describe, expect, it } from "vitest";
import {
createConfigIO,
DEFAULT_GATEWAY_PORT,
resolveConfigPathCandidate,
resolveGatewayPort,
resolveIsNixMode,
resolveStateDir,
} from "./config.js";
import { withTempHome } from "./test-helpers.js";
function envWith(overrides: Record<string, string | undefined>): NodeJS.ProcessEnv {
return { ...process.env, ...overrides };
}
function loadConfigForHome(home: string) {
return createConfigIO({
env: envWith({ OPENCLAW_HOME: home }),
homedir: () => home,
}).loadConfig();
}
describe("Nix integration (U3, U5, U9)", () => {
describe("U3: isNixMode env var detection", () => {
it("isNixMode is false when OPENCLAW_NIX_MODE is not set", async () => {
await withEnvOverride({ OPENCLAW_NIX_MODE: undefined }, async () => {
const { isNixMode } = await import("./config.js");
expect(isNixMode).toBe(false);
});
it("isNixMode is false when OPENCLAW_NIX_MODE is not set", () => {
expect(resolveIsNixMode(envWith({ OPENCLAW_NIX_MODE: undefined }))).toBe(false);
});
it("isNixMode is false when OPENCLAW_NIX_MODE is empty", async () => {
await withEnvOverride({ OPENCLAW_NIX_MODE: "" }, async () => {
const { isNixMode } = await import("./config.js");
expect(isNixMode).toBe(false);
});
it("isNixMode is false when OPENCLAW_NIX_MODE is empty", () => {
expect(resolveIsNixMode(envWith({ OPENCLAW_NIX_MODE: "" }))).toBe(false);
});
it("isNixMode is false when OPENCLAW_NIX_MODE is not '1'", async () => {
await withEnvOverride({ OPENCLAW_NIX_MODE: "true" }, async () => {
const { isNixMode } = await import("./config.js");
expect(isNixMode).toBe(false);
});
it("isNixMode is false when OPENCLAW_NIX_MODE is not '1'", () => {
expect(resolveIsNixMode(envWith({ OPENCLAW_NIX_MODE: "true" }))).toBe(false);
});
it("isNixMode is true when OPENCLAW_NIX_MODE=1", async () => {
await withEnvOverride({ OPENCLAW_NIX_MODE: "1" }, async () => {
const { isNixMode } = await import("./config.js");
expect(isNixMode).toBe(true);
});
it("isNixMode is true when OPENCLAW_NIX_MODE=1", () => {
expect(resolveIsNixMode(envWith({ OPENCLAW_NIX_MODE: "1" }))).toBe(true);
});
});
describe("U5: CONFIG_PATH and STATE_DIR env var overrides", () => {
it("STATE_DIR defaults to ~/.openclaw when env not set", async () => {
await withEnvOverride({ OPENCLAW_STATE_DIR: undefined }, async () => {
const { STATE_DIR } = await import("./config.js");
expect(STATE_DIR).toMatch(/\.openclaw$/);
});
it("STATE_DIR defaults to ~/.openclaw when env not set", () => {
expect(resolveStateDir(envWith({ OPENCLAW_STATE_DIR: undefined }))).toMatch(/\.openclaw$/);
});
it("STATE_DIR respects OPENCLAW_STATE_DIR override", async () => {
await withEnvOverride({ OPENCLAW_STATE_DIR: "/custom/state/dir" }, async () => {
const { STATE_DIR } = await import("./config.js");
expect(STATE_DIR).toBe(path.resolve("/custom/state/dir"));
});
it("STATE_DIR respects OPENCLAW_STATE_DIR override", () => {
expect(resolveStateDir(envWith({ OPENCLAW_STATE_DIR: "/custom/state/dir" }))).toBe(
path.resolve("/custom/state/dir"),
);
});
it("STATE_DIR respects OPENCLAW_HOME when state override is unset", async () => {
it("STATE_DIR respects OPENCLAW_HOME when state override is unset", () => {
const customHome = path.join(path.sep, "custom", "home");
await withEnvOverride(
{ OPENCLAW_HOME: customHome, OPENCLAW_STATE_DIR: undefined },
async () => {
const { STATE_DIR } = await import("./config.js");
expect(STATE_DIR).toBe(path.join(path.resolve(customHome), ".openclaw"));
},
);
expect(
resolveStateDir(envWith({ OPENCLAW_HOME: customHome, OPENCLAW_STATE_DIR: undefined })),
).toBe(path.join(path.resolve(customHome), ".openclaw"));
});
it("CONFIG_PATH defaults to OPENCLAW_HOME/.openclaw/openclaw.json", async () => {
it("CONFIG_PATH defaults to OPENCLAW_HOME/.openclaw/openclaw.json", () => {
const customHome = path.join(path.sep, "custom", "home");
await withEnvOverride(
{
OPENCLAW_HOME: customHome,
OPENCLAW_CONFIG_PATH: undefined,
OPENCLAW_STATE_DIR: undefined,
},
async () => {
const { CONFIG_PATH } = await import("./config.js");
expect(CONFIG_PATH).toBe(
path.join(path.resolve(customHome), ".openclaw", "openclaw.json"),
);
},
);
expect(
resolveConfigPathCandidate(
envWith({
OPENCLAW_HOME: customHome,
OPENCLAW_CONFIG_PATH: undefined,
OPENCLAW_STATE_DIR: undefined,
}),
),
).toBe(path.join(path.resolve(customHome), ".openclaw", "openclaw.json"));
});
it("CONFIG_PATH defaults to ~/.openclaw/openclaw.json when env not set", async () => {
await withEnvOverride(
{ OPENCLAW_CONFIG_PATH: undefined, OPENCLAW_STATE_DIR: undefined },
async () => {
const { CONFIG_PATH } = await import("./config.js");
expect(CONFIG_PATH).toMatch(/\.openclaw[\\/]openclaw\.json$/);
},
);
it("CONFIG_PATH defaults to ~/.openclaw/openclaw.json when env not set", () => {
expect(
resolveConfigPathCandidate(
envWith({ OPENCLAW_CONFIG_PATH: undefined, OPENCLAW_STATE_DIR: undefined }),
),
).toMatch(/\.openclaw[\\/]openclaw\.json$/);
});
it("CONFIG_PATH respects OPENCLAW_CONFIG_PATH override", async () => {
await withEnvOverride({ OPENCLAW_CONFIG_PATH: "/nix/store/abc/openclaw.json" }, async () => {
const { CONFIG_PATH } = await import("./config.js");
expect(CONFIG_PATH).toBe(path.resolve("/nix/store/abc/openclaw.json"));
});
it("CONFIG_PATH respects OPENCLAW_CONFIG_PATH override", () => {
expect(
resolveConfigPathCandidate(
envWith({ OPENCLAW_CONFIG_PATH: "/nix/store/abc/openclaw.json" }),
),
).toBe(path.resolve("/nix/store/abc/openclaw.json"));
});
it("CONFIG_PATH expands ~ in OPENCLAW_CONFIG_PATH override", async () => {
await withTempHome(async (home) => {
await withEnvOverride({ OPENCLAW_CONFIG_PATH: "~/.openclaw/custom.json" }, async () => {
const { CONFIG_PATH } = await import("./config.js");
expect(CONFIG_PATH).toBe(path.join(home, ".openclaw", "custom.json"));
});
expect(
resolveConfigPathCandidate(
envWith({ OPENCLAW_HOME: home, OPENCLAW_CONFIG_PATH: "~/.openclaw/custom.json" }),
() => home,
),
).toBe(path.join(home, ".openclaw", "custom.json"));
});
});
it("CONFIG_PATH uses STATE_DIR when only state dir is overridden", async () => {
await withEnvOverride(
{
OPENCLAW_CONFIG_PATH: undefined,
OPENCLAW_STATE_DIR: "/custom/state",
},
async () => {
const { CONFIG_PATH } = await import("./config.js");
expect(CONFIG_PATH).toBe(path.join(path.resolve("/custom/state"), "openclaw.json"));
},
it("CONFIG_PATH uses STATE_DIR when only state dir is overridden", () => {
expect(resolveConfigPathCandidate(envWith({ OPENCLAW_STATE_DIR: "/custom/state" }))).toBe(
path.join(path.resolve("/custom/state"), "openclaw.json"),
);
});
});
@@ -177,9 +166,7 @@ describe("Nix integration (U3, U5, U9)", () => {
"utf-8",
);
vi.resetModules();
const { loadConfig } = await import("./config.js");
const cfg = loadConfig();
const cfg = loadConfigForHome(home);
expect(cfg.plugins?.load?.paths?.[0]).toBe(path.join(home, "plugins", "demo-plugin"));
expect(cfg.agents?.defaults?.workspace).toBe(path.join(home, "ws-default"));
@@ -196,25 +183,28 @@ describe("Nix integration (U3, U5, U9)", () => {
});
describe("U6: gateway port resolution", () => {
it("uses default when env and config are unset", async () => {
await withEnvOverride({ OPENCLAW_GATEWAY_PORT: undefined }, async () => {
const { DEFAULT_GATEWAY_PORT, resolveGatewayPort } = await import("./config.js");
expect(resolveGatewayPort({})).toBe(DEFAULT_GATEWAY_PORT);
});
it("uses default when env and config are unset", () => {
expect(resolveGatewayPort({}, envWith({ OPENCLAW_GATEWAY_PORT: undefined }))).toBe(
DEFAULT_GATEWAY_PORT,
);
});
it("prefers OPENCLAW_GATEWAY_PORT over config", async () => {
await withEnvOverride({ OPENCLAW_GATEWAY_PORT: "19001" }, async () => {
const { resolveGatewayPort } = await import("./config.js");
expect(resolveGatewayPort({ gateway: { port: 19002 } })).toBe(19001);
});
it("prefers OPENCLAW_GATEWAY_PORT over config", () => {
expect(
resolveGatewayPort(
{ gateway: { port: 19002 } },
envWith({ OPENCLAW_GATEWAY_PORT: "19001" }),
),
).toBe(19001);
});
it("falls back to config when env is invalid", async () => {
await withEnvOverride({ OPENCLAW_GATEWAY_PORT: "nope" }, async () => {
const { resolveGatewayPort } = await import("./config.js");
expect(resolveGatewayPort({ gateway: { port: 19003 } })).toBe(19003);
});
it("falls back to config when env is invalid", () => {
expect(
resolveGatewayPort(
{ gateway: { port: 19003 } },
envWith({ OPENCLAW_GATEWAY_PORT: "nope" }),
),
).toBe(19003);
});
});
@@ -231,9 +221,7 @@ describe("Nix integration (U3, U5, U9)", () => {
"utf-8",
);
vi.resetModules();
const { loadConfig } = await import("./config.js");
const cfg = loadConfig();
const cfg = loadConfigForHome(home);
expect(cfg.channels?.telegram?.botToken).toBe("123:ABC");
expect(cfg.channels?.telegram?.tokenFile).toBeUndefined();
});
@@ -251,9 +239,7 @@ describe("Nix integration (U3, U5, U9)", () => {
"utf-8",
);
vi.resetModules();
const { loadConfig } = await import("./config.js");
const cfg = loadConfig();
const cfg = loadConfigForHome(home);
expect(cfg.channels?.telegram?.tokenFile).toBe("/run/agenix/telegram-token");
expect(cfg.channels?.telegram?.botToken).toBeUndefined();
});
@@ -276,9 +262,7 @@ describe("Nix integration (U3, U5, U9)", () => {
"utf-8",
);
vi.resetModules();
const { loadConfig } = await import("./config.js");
const cfg = loadConfig();
const cfg = loadConfigForHome(home);
expect(cfg.channels?.telegram?.botToken).toBe("fallback:token");
expect(cfg.channels?.telegram?.tokenFile).toBe("/run/agenix/telegram-token");
});

View File

@@ -1,10 +1,10 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ChannelOutboundAdapter, ChannelPlugin } from "../../channels/plugins/types.js";
import { setActivePluginRegistry } from "../../plugins/runtime.js";
import { createIMessageTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
const loadMessage = async () => await import("./message.js");
import { sendMessage, sendPoll } from "./message.js";
const setRegistry = async (registry: ReturnType<typeof createTestRegistry>) => {
const { setActivePluginRegistry } = await import("../../plugins/runtime.js");
const setRegistry = (registry: ReturnType<typeof createTestRegistry>) => {
setActivePluginRegistry(registry);
};
@@ -15,23 +15,21 @@ vi.mock("../../gateway/call.js", () => ({
}));
describe("sendMessage channel normalization", () => {
beforeEach(async () => {
beforeEach(() => {
callGatewayMock.mockReset();
vi.resetModules();
await setRegistry(emptyRegistry);
setRegistry(emptyRegistry);
});
afterEach(async () => {
await setRegistry(emptyRegistry);
afterEach(() => {
setRegistry(emptyRegistry);
});
it("normalizes Teams alias", async () => {
const { sendMessage } = await loadMessage();
const sendMSTeams = vi.fn(async () => ({
messageId: "m1",
conversationId: "c1",
}));
await setRegistry(
setRegistry(
createTestRegistry([
{
pluginId: "msteams",
@@ -56,9 +54,8 @@ describe("sendMessage channel normalization", () => {
});
it("normalizes iMessage alias", async () => {
const { sendMessage } = await loadMessage();
const sendIMessage = vi.fn(async () => ({ messageId: "i1" }));
await setRegistry(
setRegistry(
createTestRegistry([
{
pluginId: "imessage",
@@ -81,25 +78,23 @@ describe("sendMessage channel normalization", () => {
});
describe("sendMessage replyToId threading", () => {
beforeEach(async () => {
beforeEach(() => {
callGatewayMock.mockReset();
vi.resetModules();
await setRegistry(emptyRegistry);
setRegistry(emptyRegistry);
});
afterEach(async () => {
await setRegistry(emptyRegistry);
afterEach(() => {
setRegistry(emptyRegistry);
});
it("passes replyToId through to the outbound adapter", async () => {
const { sendMessage } = await loadMessage();
const capturedCtx: Record<string, unknown>[] = [];
const plugin = createMattermostLikePlugin({
onSendText: (ctx) => {
capturedCtx.push(ctx);
},
});
await setRegistry(createTestRegistry([{ pluginId: "mattermost", source: "test", plugin }]));
setRegistry(createTestRegistry([{ pluginId: "mattermost", source: "test", plugin }]));
await sendMessage({
cfg: {},
@@ -114,14 +109,13 @@ describe("sendMessage replyToId threading", () => {
});
it("passes threadId through to the outbound adapter", async () => {
const { sendMessage } = await loadMessage();
const capturedCtx: Record<string, unknown>[] = [];
const plugin = createMattermostLikePlugin({
onSendText: (ctx) => {
capturedCtx.push(ctx);
},
});
await setRegistry(createTestRegistry([{ pluginId: "mattermost", source: "test", plugin }]));
setRegistry(createTestRegistry([{ pluginId: "mattermost", source: "test", plugin }]));
await sendMessage({
cfg: {},
@@ -137,20 +131,18 @@ describe("sendMessage replyToId threading", () => {
});
describe("sendPoll channel normalization", () => {
beforeEach(async () => {
beforeEach(() => {
callGatewayMock.mockReset();
vi.resetModules();
await setRegistry(emptyRegistry);
setRegistry(emptyRegistry);
});
afterEach(async () => {
await setRegistry(emptyRegistry);
afterEach(() => {
setRegistry(emptyRegistry);
});
it("normalizes Teams alias for polls", async () => {
const { sendPoll } = await loadMessage();
callGatewayMock.mockResolvedValueOnce({ messageId: "p1" });
await setRegistry(
setRegistry(
createTestRegistry([
{
pluginId: "msteams",