mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 02:28:27 +00:00
test(logging): dedupe stream and state-dir env assertions
This commit is contained in:
@@ -8,10 +8,30 @@ import {
|
||||
withStateDirEnv,
|
||||
} from "./state-dir-env.js";
|
||||
|
||||
type EnvSnapshot = {
|
||||
openclaw?: string;
|
||||
legacy?: string;
|
||||
};
|
||||
|
||||
function snapshotCurrentStateDirVars(): EnvSnapshot {
|
||||
return {
|
||||
openclaw: process.env.OPENCLAW_STATE_DIR,
|
||||
legacy: process.env.CLAWDBOT_STATE_DIR,
|
||||
};
|
||||
}
|
||||
|
||||
function expectStateDirVars(snapshot: EnvSnapshot) {
|
||||
expect(process.env.OPENCLAW_STATE_DIR).toBe(snapshot.openclaw);
|
||||
expect(process.env.CLAWDBOT_STATE_DIR).toBe(snapshot.legacy);
|
||||
}
|
||||
|
||||
async function expectPathMissing(filePath: string) {
|
||||
await expect(fs.stat(filePath)).rejects.toThrow();
|
||||
}
|
||||
|
||||
describe("state-dir-env helpers", () => {
|
||||
it("set/snapshot/restore round-trips OPENCLAW_STATE_DIR", () => {
|
||||
const prevOpenClaw = process.env.OPENCLAW_STATE_DIR;
|
||||
const prevLegacy = process.env.CLAWDBOT_STATE_DIR;
|
||||
const prev = snapshotCurrentStateDirVars();
|
||||
const snapshot = snapshotStateDirEnv();
|
||||
|
||||
setStateDirEnv("/tmp/openclaw-state-dir-test");
|
||||
@@ -19,13 +39,11 @@ describe("state-dir-env helpers", () => {
|
||||
expect(process.env.CLAWDBOT_STATE_DIR).toBeUndefined();
|
||||
|
||||
restoreStateDirEnv(snapshot);
|
||||
expect(process.env.OPENCLAW_STATE_DIR).toBe(prevOpenClaw);
|
||||
expect(process.env.CLAWDBOT_STATE_DIR).toBe(prevLegacy);
|
||||
expectStateDirVars(prev);
|
||||
});
|
||||
|
||||
it("withStateDirEnv sets env for callback and cleans up temp root", async () => {
|
||||
const prevOpenClaw = process.env.OPENCLAW_STATE_DIR;
|
||||
const prevLegacy = process.env.CLAWDBOT_STATE_DIR;
|
||||
const prev = snapshotCurrentStateDirVars();
|
||||
|
||||
let capturedTempRoot = "";
|
||||
let capturedStateDir = "";
|
||||
@@ -37,15 +55,13 @@ describe("state-dir-env helpers", () => {
|
||||
await fs.writeFile(path.join(stateDir, "probe.txt"), "ok", "utf8");
|
||||
});
|
||||
|
||||
expect(process.env.OPENCLAW_STATE_DIR).toBe(prevOpenClaw);
|
||||
expect(process.env.CLAWDBOT_STATE_DIR).toBe(prevLegacy);
|
||||
await expect(fs.stat(capturedStateDir)).rejects.toThrow();
|
||||
await expect(fs.stat(capturedTempRoot)).rejects.toThrow();
|
||||
expectStateDirVars(prev);
|
||||
await expectPathMissing(capturedStateDir);
|
||||
await expectPathMissing(capturedTempRoot);
|
||||
});
|
||||
|
||||
it("withStateDirEnv restores env and cleans temp root when callback throws", async () => {
|
||||
const prevOpenClaw = process.env.OPENCLAW_STATE_DIR;
|
||||
const prevLegacy = process.env.CLAWDBOT_STATE_DIR;
|
||||
const prev = snapshotCurrentStateDirVars();
|
||||
|
||||
let capturedTempRoot = "";
|
||||
let capturedStateDir = "";
|
||||
@@ -57,9 +73,8 @@ describe("state-dir-env helpers", () => {
|
||||
}),
|
||||
).rejects.toThrow("boom");
|
||||
|
||||
expect(process.env.OPENCLAW_STATE_DIR).toBe(prevOpenClaw);
|
||||
expect(process.env.CLAWDBOT_STATE_DIR).toBe(prevLegacy);
|
||||
await expect(fs.stat(capturedStateDir)).rejects.toThrow();
|
||||
await expect(fs.stat(capturedTempRoot)).rejects.toThrow();
|
||||
expectStateDirVars(prev);
|
||||
await expectPathMissing(capturedStateDir);
|
||||
await expectPathMissing(capturedTempRoot);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user