test(logging): dedupe stream and state-dir env assertions
This commit is contained in:
@@ -125,20 +125,15 @@ describe("enableConsoleCapture", () => {
|
||||
expect(log).toHaveBeenCalledWith(payload);
|
||||
});
|
||||
|
||||
it("swallows async EPIPE on stdout", () => {
|
||||
it.each([
|
||||
{ name: "stdout", stream: process.stdout },
|
||||
{ name: "stderr", stream: process.stderr },
|
||||
])("swallows async EPIPE on $name", ({ stream }) => {
|
||||
setLoggerOverride({ level: "info", file: tempLogPath() });
|
||||
enableConsoleCapture();
|
||||
const epipe = new Error("write EPIPE") as NodeJS.ErrnoException;
|
||||
epipe.code = "EPIPE";
|
||||
expect(() => process.stdout.emit("error", epipe)).not.toThrow();
|
||||
});
|
||||
|
||||
it("swallows async EPIPE on stderr", () => {
|
||||
setLoggerOverride({ level: "info", file: tempLogPath() });
|
||||
enableConsoleCapture();
|
||||
const epipe = new Error("write EPIPE") as NodeJS.ErrnoException;
|
||||
epipe.code = "EPIPE";
|
||||
expect(() => process.stderr.emit("error", epipe)).not.toThrow();
|
||||
expect(() => stream.emit("error", epipe)).not.toThrow();
|
||||
});
|
||||
|
||||
it("rethrows non-EPIPE errors on stdout", () => {
|
||||
|
||||
@@ -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