






















@@ -6,6 +6,7 @@ import path from "node:path";
66import { afterEach, describe, expect, it, vi } from "vitest";
77import * as fsSafe from "../infra/fs-safe.js";
88import { withTempDir } from "../test-helpers/temp-dir.js";
9+import { captureEnv, setTestEnvValue } from "../test-utils/env.js";
910import { withFetchPreconnect } from "../test-utils/fetch-mock.js";
1011import { MediaAttachmentCache } from "./attachments.js";
1112import { normalizeMediaUnderstandingChatType, resolveMediaUnderstandingScope } from "./scope.js";
@@ -27,14 +28,10 @@ describe("media understanding scope", () => {
2728});
28292930const originalFetch = globalThis.fetch;
30-const originalStateDirEnv = process.env.OPENCLAW_STATE_DIR;
31+const stateDirEnvSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
31323233function restoreProcessState() {
33-if (originalStateDirEnv === undefined) {
34-delete process.env.OPENCLAW_STATE_DIR;
35-} else {
36-process.env.OPENCLAW_STATE_DIR = originalStateDirEnv;
37-}
34+stateDirEnvSnapshot.restore();
3835}
39364037async function withLocalAttachmentCache(
@@ -174,7 +171,7 @@ describe("media understanding attachments SSRF", () => {
174171await fs.mkdir(path.dirname(attachmentPath), { recursive: true });
175172await fs.mkdir(cwd, { recursive: true });
176173await fs.writeFile(attachmentPath, "state-media");
177-process.env.OPENCLAW_STATE_DIR = stateDir;
174+setTestEnvValue("OPENCLAW_STATE_DIR", stateDir);
178175vi.spyOn(process, "cwd").mockReturnValue(cwd);
179176180177const cache = new MediaAttachmentCache([{ index: 0, path: relativePath }], {
@@ -196,7 +193,7 @@ describe("media understanding attachments SSRF", () => {
196193await fs.mkdir(stateDir, { recursive: true });
197194await fs.mkdir(path.dirname(attachmentPath), { recursive: true });
198195await fs.writeFile(attachmentPath, "cwd-media");
199-process.env.OPENCLAW_STATE_DIR = stateDir;
196+setTestEnvValue("OPENCLAW_STATE_DIR", stateDir);
200197vi.spyOn(process, "cwd").mockReturnValue(cwd);
201198202199const cache = new MediaAttachmentCache([{ index: 0, path: relativePath }], {
@@ -220,7 +217,7 @@ describe("media understanding attachments SSRF", () => {
220217await fs.mkdir(path.dirname(statePath), { recursive: true });
221218await fs.writeFile(cwdPath, "cwd-media");
222219await fs.writeFile(statePath, "state-media");
223-process.env.OPENCLAW_STATE_DIR = stateDir;
220+setTestEnvValue("OPENCLAW_STATE_DIR", stateDir);
224221vi.spyOn(process, "cwd").mockReturnValue(cwd);
225222226223const cache = new MediaAttachmentCache([{ index: 0, path: relativePath }], {
@@ -243,7 +240,7 @@ describe("media understanding attachments SSRF", () => {
243240await fs.mkdir(path.dirname(statePath), { recursive: true });
244241await fs.writeFile(cwdPath, "blocked-cwd-media");
245242await fs.writeFile(statePath, "state-media");
246-process.env.OPENCLAW_STATE_DIR = stateDir;
243+setTestEnvValue("OPENCLAW_STATE_DIR", stateDir);
247244vi.spyOn(process, "cwd").mockReturnValue(cwd);
248245249246const cache = new MediaAttachmentCache([{ index: 0, path: relativePath }], {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。