test: snapshot diagnostic state env

This commit is contained in:
Shakker
2026-06-05 01:14:47 +01:00
parent 8ede9e0e07
commit dc4c9030fc

View File

@@ -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<typeof captureEnv> | 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 });
}