

























@@ -2,9 +2,10 @@
22import fs from "node:fs";
33import os from "node:os";
44import path from "node:path";
5-import { afterEach, describe, expect, it, vi } from "vitest";
5+import { describe, expect, it } from "vitest";
66import type { OpenClawConfig } from "../config/config.js";
77import type { SessionEntry } from "../config/sessions.js";
8+import { withEnv } from "../test-utils/env.js";
89import {
910clearAutoFallbackPrimaryProbeSelection,
1011hasLegacyAutoFallbackWithoutOrigin,
@@ -30,10 +31,6 @@ import {
3031setAgentEffectiveModelPrimary,
3132} from "./agent-scope.js";
323333-afterEach(() => {
34-vi.unstubAllEnvs();
35-});
36-3734describe("resolveAgentConfig", () => {
3835it("should return undefined when no agents config exists", () => {
3936const cfg: OpenClawConfig = {};
@@ -1154,41 +1151,43 @@ describe("resolveAgentConfig", () => {
1154115111551152it("uses OPENCLAW_HOME for default agent workspace", () => {
11561153const home = path.join(path.sep, "srv", "openclaw-home");
1157-vi.stubEnv("OPENCLAW_HOME", home);
1158-1159-const workspace = resolveAgentWorkspaceDir({} as OpenClawConfig, "main");
1160-expect(workspace).toBe(path.join(path.resolve(home), ".openclaw", "workspace"));
1154+withEnv({ OPENCLAW_HOME: home }, () => {
1155+ const workspace = resolveAgentWorkspaceDir({} as OpenClawConfig, "main");
1156+ expect(workspace).toBe(path.join(path.resolve(home), ".openclaw", "workspace"));
1157+});
11611158});
1162115911631160it("uses OPENCLAW_WORKSPACE_DIR for default agent workspace", () => {
11641161const workspaceDir = path.join(path.sep, "srv", "openclaw-workspace");
1165-vi.stubEnv("OPENCLAW_WORKSPACE_DIR", workspaceDir);
1166-vi.stubEnv("OPENCLAW_HOME", path.join(path.sep, "srv", "openclaw-home"));
1167-1168-const workspace = resolveAgentWorkspaceDir({} as OpenClawConfig, "main");
1169-expect(workspace).toBe(path.resolve(workspaceDir));
1162+withEnv(
1163+{
1164+OPENCLAW_WORKSPACE_DIR: workspaceDir,
1165+OPENCLAW_HOME: path.join(path.sep, "srv", "openclaw-home"),
1166+},
1167+() => {
1168+const workspace = resolveAgentWorkspaceDir({} as OpenClawConfig, "main");
1169+expect(workspace).toBe(path.resolve(workspaceDir));
1170+},
1171+);
11701172});
1171117311721174it("uses OPENCLAW_HOME for default agentDir", () => {
11731175const home = path.join(path.sep, "srv", "openclaw-home");
1174-vi.stubEnv("OPENCLAW_HOME", home);
1175-// Clear state dir so it falls back to OPENCLAW_HOME
1176-vi.stubEnv("OPENCLAW_STATE_DIR", "");
1177-1178-const agentDir = resolveAgentDir({} as OpenClawConfig, "main");
1179-expect(agentDir).toBe(path.join(path.resolve(home), ".openclaw", "agents", "main", "agent"));
1176+withEnv({ OPENCLAW_HOME: home, OPENCLAW_STATE_DIR: "" }, () => {
1177+const agentDir = resolveAgentDir({} as OpenClawConfig, "main");
1178+expect(agentDir).toBe(path.join(path.resolve(home), ".openclaw", "agents", "main", "agent"));
1179+});
11801180});
1181118111821182it("resolves default agentDir from the configured default agent", () => {
11831183const stateDir = path.join(path.sep, "tmp", "test-state");
1184-vi.stubEnv("OPENCLAW_STATE_DIR", stateDir);
11851184const cfg: OpenClawConfig = {
11861185agents: {
11871186list: [{ id: "main" }, { id: "ops", default: true }],
11881187},
11891188};
119011891191-const agentDir = resolveDefaultAgentDir(cfg);
1190+const agentDir = withEnv({ OPENCLAW_STATE_DIR: stateDir }, () => resolveDefaultAgentDir(cfg));
1192119111931192expect(agentDir).toBe(path.resolve(stateDir, "agents", "ops", "agent"));
11941193});
@@ -1217,13 +1216,14 @@ describe("resolveAgentConfig", () => {
1217121612181217it("non-default agent without defaults.workspace falls back to stateDir", () => {
12191218const stateDir = path.join(path.sep, "tmp", "test-state");
1220-vi.stubEnv("OPENCLAW_STATE_DIR", stateDir);
12211219const cfg: OpenClawConfig = {
12221220agents: {
12231221list: [{ id: "main" }, { id: "work", default: true, workspace: "/work-ws" }],
12241222},
12251223};
1226-const workspace = resolveAgentWorkspaceDir(cfg, "main");
1224+const workspace = withEnv({ OPENCLAW_STATE_DIR: stateDir }, () =>
1225+resolveAgentWorkspaceDir(cfg, "main"),
1226+);
12271227expect(workspace).toBe(path.resolve(stateDir, "workspace-main"));
12281228});
12291229});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。