test(core): reuse shared temp dir helper in logger tests

This commit is contained in:
Vincent Koc
2026-04-06 06:27:34 +01:00
parent 0b1496f876
commit 6f2c258011

View File

@@ -1,6 +1,4 @@
import crypto from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { isVerbose, isYes, logVerbose, setVerbose, setYes } from "./globals.js";
@@ -12,6 +10,7 @@ import {
stripRedundantSubsystemPrefixForConsole,
} from "./logging.js";
import type { RuntimeEnv } from "./runtime.js";
import { withTempDirSync } from "./test-helpers/temp-dir.js";
describe("logger helpers", () => {
afterEach(() => {
@@ -49,26 +48,26 @@ describe("logger helpers", () => {
});
it("writes to configured log file at configured level", () => {
const logPath = pathForTest();
cleanup(logPath);
setLoggerOverride({ level: "info", file: logPath });
fs.writeFileSync(logPath, "");
logInfo("hello");
logDebug("debug-only"); // may be filtered depending on level mapping
const content = fs.readFileSync(logPath, "utf-8");
expect(content.length).toBeGreaterThan(0);
cleanup(logPath);
withTempDirSync({ prefix: "openclaw-log-test-" }, (dir) => {
const logPath = path.join(dir, "openclaw.log");
setLoggerOverride({ level: "info", file: logPath });
fs.writeFileSync(logPath, "");
logInfo("hello");
logDebug("debug-only"); // may be filtered depending on level mapping
const content = fs.readFileSync(logPath, "utf-8");
expect(content.length).toBeGreaterThan(0);
});
});
it("filters messages below configured level", () => {
const logPath = pathForTest();
cleanup(logPath);
setLoggerOverride({ level: "warn", file: logPath });
logInfo("info-only");
logWarn("warn-only");
const content = fs.readFileSync(logPath, "utf-8");
expect(content).toContain("warn-only");
cleanup(logPath);
withTempDirSync({ prefix: "openclaw-log-test-" }, (dir) => {
const logPath = path.join(dir, "openclaw.log");
setLoggerOverride({ level: "warn", file: logPath });
logInfo("info-only");
logWarn("warn-only");
const content = fs.readFileSync(logPath, "utf-8");
expect(content).toContain("warn-only");
});
});
it("uses daily rolling default log file and prunes old ones", () => {
@@ -82,7 +81,7 @@ describe("logger helpers", () => {
fs.mkdirSync(DEFAULT_LOG_DIR, { recursive: true });
fs.writeFileSync(oldPath, "old");
fs.utimesSync(oldPath, new Date(0), new Date(0));
cleanup(todayPath);
fs.rmSync(todayPath, { force: true });
logInfo("roll-me");
@@ -90,7 +89,7 @@ describe("logger helpers", () => {
expect(fs.readFileSync(todayPath, "utf-8")).toContain("roll-me");
expect(fs.existsSync(oldPath)).toBe(false);
cleanup(todayPath);
fs.rmSync(todayPath, { force: true });
});
});
@@ -142,20 +141,6 @@ describe("stripRedundantSubsystemPrefixForConsole", () => {
});
});
function pathForTest() {
const file = path.join(os.tmpdir(), `openclaw-log-${crypto.randomUUID()}.log`);
fs.mkdirSync(path.dirname(file), { recursive: true });
return file;
}
function cleanup(file: string) {
try {
fs.rmSync(file, { force: true });
} catch {
// ignore
}
}
function localDateString(date: Date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");