diff --git a/src/logging/diagnostic-session-context.test.ts b/src/logging/diagnostic-session-context.test.ts index b0f50932a6f6..5c03fea1f816 100644 --- a/src/logging/diagnostic-session-context.test.ts +++ b/src/logging/diagnostic-session-context.test.ts @@ -4,6 +4,7 @@ import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { saveCronStore } from "../cron/store.js"; +import { captureEnv } from "../test-utils/env.js"; import { formatCronSessionDiagnosticFields, formatStoppedCronSessionDiagnosticFields, @@ -13,7 +14,7 @@ import { } from "./diagnostic-session-context.js"; let tempDir: string | undefined; -let previousStateDir: string | undefined; +let envSnapshot: ReturnType | undefined; function writeJsonl(filePath: string, rows: unknown[]) { fs.mkdirSync(path.dirname(filePath), { recursive: true }); @@ -22,17 +23,14 @@ function writeJsonl(filePath: string, rows: unknown[]) { describe("diagnostic session context", () => { beforeEach(() => { - previousStateDir = process.env.OPENCLAW_STATE_DIR; + envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]); tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-diagnostic-session-")); process.env.OPENCLAW_STATE_DIR = tempDir; }); afterEach(() => { - if (previousStateDir === undefined) { - delete process.env.OPENCLAW_STATE_DIR; - } else { - process.env.OPENCLAW_STATE_DIR = previousStateDir; - } + envSnapshot?.restore(); + envSnapshot = undefined; if (tempDir) { fs.rmSync(tempDir, { recursive: true, force: true }); }