From ec91dce0b86300a477703b36a180ebea1d13aede Mon Sep 17 00:00:00 2001 From: Shakker Date: Fri, 5 Jun 2026 17:08:30 +0100 Subject: [PATCH] test: scope internal transcript state env --- src/agents/internal-session-effects.test.ts | 80 ++++++++++----------- 1 file changed, 36 insertions(+), 44 deletions(-) diff --git a/src/agents/internal-session-effects.test.ts b/src/agents/internal-session-effects.test.ts index 4e81e5f72681..666cc6299b97 100644 --- a/src/agents/internal-session-effects.test.ts +++ b/src/agents/internal-session-effects.test.ts @@ -1,75 +1,67 @@ // Covers private transcript preparation for internal agent side effects. import fs from "node:fs/promises"; import path from "node:path"; -import { afterEach, describe, expect, it } from "vitest"; +import { describe, expect, it } from "vitest"; import { withTempDir } from "../test-helpers/temp-dir.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { prepareInternalSessionEffectsTranscript, removeInternalSessionEffectsTranscript, } from "./internal-session-effects.js"; -const ORIGINAL_STATE_DIR = process.env.OPENCLAW_STATE_DIR; - -afterEach(() => { - if (ORIGINAL_STATE_DIR === undefined) { - delete process.env.OPENCLAW_STATE_DIR; - } else { - process.env.OPENCLAW_STATE_DIR = ORIGINAL_STATE_DIR; - } -}); - describe("prepareInternalSessionEffectsTranscript", () => { it("creates a private transcript even without a visible source file", async () => { await withTempDir({ prefix: "openclaw-internal-session-effects-" }, async (dir) => { - process.env.OPENCLAW_STATE_DIR = dir; + await withEnvAsync({ OPENCLAW_STATE_DIR: dir }, async () => { + const sessionFile = await prepareInternalSessionEffectsTranscript({ + runId: "run/with space", + }); - const sessionFile = await prepareInternalSessionEffectsTranscript({ - runId: "run/with space", + // The run id is filesystem-normalized and the transcript is private + // because internal side effects may contain hidden agent context. + expect(sessionFile).toBe(path.join(dir, "internal-agent-runs", "run_with_space.jsonl")); + expect(await fs.readFile(sessionFile, "utf8")).toBe(""); + expect((await fs.stat(sessionFile)).mode & 0o777).toBe(0o600); + + await removeInternalSessionEffectsTranscript(sessionFile); + + await expect(fs.stat(sessionFile)).rejects.toMatchObject({ code: "ENOENT" }); }); - - // The run id is filesystem-normalized and the transcript is private - // because internal side effects may contain hidden agent context. - expect(sessionFile).toBe(path.join(dir, "internal-agent-runs", "run_with_space.jsonl")); - expect(await fs.readFile(sessionFile, "utf8")).toBe(""); - expect((await fs.stat(sessionFile)).mode & 0o777).toBe(0o600); - - await removeInternalSessionEffectsTranscript(sessionFile); - - await expect(fs.stat(sessionFile)).rejects.toMatchObject({ code: "ENOENT" }); }); }); it("copies a visible source transcript into a private transcript", async () => { await withTempDir({ prefix: "openclaw-internal-session-effects-" }, async (dir) => { - process.env.OPENCLAW_STATE_DIR = dir; - const sourceFile = path.join(dir, "visible-session.jsonl"); - await fs.writeFile(sourceFile, '{"role":"assistant","content":"done"}\n', { - mode: 0o644, - }); + await withEnvAsync({ OPENCLAW_STATE_DIR: dir }, async () => { + const sourceFile = path.join(dir, "visible-session.jsonl"); + await fs.writeFile(sourceFile, '{"role":"assistant","content":"done"}\n', { + mode: 0o644, + }); - const sessionFile = await prepareInternalSessionEffectsTranscript({ - sessionFile: sourceFile, - runId: "run-copy", - }); + const sessionFile = await prepareInternalSessionEffectsTranscript({ + sessionFile: sourceFile, + runId: "run-copy", + }); - expect(await fs.readFile(sessionFile, "utf8")).toBe( - '{"role":"assistant","content":"done"}\n', - ); - expect((await fs.stat(sessionFile)).mode & 0o777).toBe(0o600); + expect(await fs.readFile(sessionFile, "utf8")).toBe( + '{"role":"assistant","content":"done"}\n', + ); + expect((await fs.stat(sessionFile)).mode & 0o777).toBe(0o600); + }); }); }); it("creates an empty private transcript when the visible source is missing", async () => { await withTempDir({ prefix: "openclaw-internal-session-effects-" }, async (dir) => { - process.env.OPENCLAW_STATE_DIR = dir; + await withEnvAsync({ OPENCLAW_STATE_DIR: dir }, async () => { + const sessionFile = await prepareInternalSessionEffectsTranscript({ + sessionFile: path.join(dir, "missing-session.jsonl"), + runId: "run-missing-source", + }); - const sessionFile = await prepareInternalSessionEffectsTranscript({ - sessionFile: path.join(dir, "missing-session.jsonl"), - runId: "run-missing-source", + expect(await fs.readFile(sessionFile, "utf8")).toBe(""); + expect((await fs.stat(sessionFile)).mode & 0o777).toBe(0o600); }); - - expect(await fs.readFile(sessionFile, "utf8")).toBe(""); - expect((await fs.stat(sessionFile)).mode & 0o777).toBe(0o600); }); }); });