

























@@ -7,6 +7,7 @@ import fs from "node:fs/promises";
77import os from "node:os";
88import path from "node:path";
99import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
10+import { withEnvAsync } from "../test-utils/env.js";
10111112type CapturedEditOperations = {
1213readFile: (absolutePath: string) => Promise<Buffer>;
@@ -94,7 +95,6 @@ describe("host tool tilde expansion (non-workspace mode)", () => {
9495});
95969697afterEach(async () => {
97-vi.unstubAllEnvs();
9898mocks.editOps = undefined;
9999mocks.writeOps = undefined;
100100while (tempDirs.length > 0) {
@@ -147,52 +147,56 @@ describe("host tool tilde expansion (non-workspace mode)", () => {
147147const openclawHome = await createTempDir("openclaw-home-override-", os.tmpdir());
148148const dir = await createTempDir("openclaw-tilde-test-write-");
149149const testFile = path.join(dir, "os-home-write.txt");
150-vi.stubEnv("OPENCLAW_HOME", openclawHome);
151150152-createHostWorkspaceWriteTool(openclawHome, { workspaceOnly: false });
153-await readWriteOps().writeFile(toTildePath(testFile), "written via os home");
151+await withEnvAsync({ OPENCLAW_HOME: openclawHome }, async () => {
152+createHostWorkspaceWriteTool(openclawHome, { workspaceOnly: false });
153+await readWriteOps().writeFile(toTildePath(testFile), "written via os home");
154154155-expect(await fs.readFile(testFile, "utf8")).toBe("written via os home");
156-await expectMissingPath(fs.access(path.join(openclawHome, path.basename(testFile))));
155+expect(await fs.readFile(testFile, "utf8")).toBe("written via os home");
156+await expectMissingPath(fs.access(path.join(openclawHome, path.basename(testFile))));
157+});
157158});
158159159160it("ignores OPENCLAW_HOME for mkdir operations", async () => {
160161const openclawHome = await createTempDir("openclaw-home-override-", os.tmpdir());
161162const dir = await createTempDir("openclaw-tilde-test-mkdir-");
162163const newDir = path.join(dir, "os-home-subdir");
163-vi.stubEnv("OPENCLAW_HOME", openclawHome);
164164165-createHostWorkspaceWriteTool(openclawHome, { workspaceOnly: false });
166-await readWriteOps().mkdir(toTildePath(newDir));
165+await withEnvAsync({ OPENCLAW_HOME: openclawHome }, async () => {
166+createHostWorkspaceWriteTool(openclawHome, { workspaceOnly: false });
167+await readWriteOps().mkdir(toTildePath(newDir));
167168168-expect((await fs.stat(newDir)).isDirectory()).toBe(true);
169-await expectMissingPath(fs.access(path.join(openclawHome, path.basename(newDir))));
169+expect((await fs.stat(newDir)).isDirectory()).toBe(true);
170+await expectMissingPath(fs.access(path.join(openclawHome, path.basename(newDir))));
171+});
170172});
171173172174it("ignores OPENCLAW_HOME for readFile operations", async () => {
173175const openclawHome = await createTempDir("openclaw-home-override-", os.tmpdir());
174176const dir = await createTempDir("openclaw-tilde-test-edit-");
175177const testFile = path.join(dir, "os-home-read.txt");
176178await fs.writeFile(testFile, "OS home content", "utf8");
177-vi.stubEnv("OPENCLAW_HOME", openclawHome);
178179179-createHostWorkspaceEditTool(openclawHome, { workspaceOnly: false });
180-const content = await readEditOps().readFile(toTildePath(testFile));
180+await withEnvAsync({ OPENCLAW_HOME: openclawHome }, async () => {
181+createHostWorkspaceEditTool(openclawHome, { workspaceOnly: false });
182+const content = await readEditOps().readFile(toTildePath(testFile));
181183182-expect(content.toString("utf8")).toBe("OS home content");
183-await expectMissingPath(fs.access(path.join(openclawHome, path.basename(testFile))));
184+expect(content.toString("utf8")).toBe("OS home content");
185+await expectMissingPath(fs.access(path.join(openclawHome, path.basename(testFile))));
186+});
184187});
185188186189it("ignores OPENCLAW_HOME for access operations", async () => {
187190const openclawHome = await createTempDir("openclaw-home-override-", os.tmpdir());
188191const dir = await createTempDir("openclaw-tilde-test-edit-");
189192const testFile = path.join(dir, "os-home-access.txt");
190193await fs.writeFile(testFile, "exists", "utf8");
191-vi.stubEnv("OPENCLAW_HOME", openclawHome);
192194193-createHostWorkspaceEditTool(openclawHome, { workspaceOnly: false });
195+await withEnvAsync({ OPENCLAW_HOME: openclawHome }, async () => {
196+createHostWorkspaceEditTool(openclawHome, { workspaceOnly: false });
194197195-await expect(readEditOps().access(toTildePath(testFile))).resolves.toBeUndefined();
196-await expectMissingPath(fs.access(path.join(openclawHome, path.basename(testFile))));
198+await expect(readEditOps().access(toTildePath(testFile))).resolves.toBeUndefined();
199+await expectMissingPath(fs.access(path.join(openclawHome, path.basename(testFile))));
200+});
197201});
198202});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。