test(logger): isolate rolling file cleanup

This commit is contained in:
Peter Steinberger
2026-04-25 12:04:02 +01:00
parent 4d00c47072
commit 4484772e7d

View File

@@ -4,7 +4,6 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { isVerbose, isYes, logVerbose, setVerbose, setYes } from "./globals.js";
import { logDebug, logError, logInfo, logSuccess, logWarn } from "./logger.js";
import {
DEFAULT_LOG_DIR,
resetLogger,
setLoggerOverride,
stripRedundantSubsystemPrefixForConsole,
@@ -35,7 +34,7 @@ describe("logger helpers", () => {
});
it("only logs debug when verbose is enabled", () => {
const logVerbose = vi.spyOn(console, "log");
const logVerbose = vi.spyOn(console, "log").mockImplementation(() => {});
setVerbose(false);
logDebug("quiet");
expect(logVerbose).not.toHaveBeenCalled();
@@ -70,26 +69,24 @@ describe("logger helpers", () => {
});
});
it("uses daily rolling default log file and prunes old ones", () => {
resetLogger();
setLoggerOverride({ level: "info" }); // force default file path with enabled file logging
const today = localDateString(new Date());
const todayPath = path.join(DEFAULT_LOG_DIR, `openclaw-${today}.log`);
it("uses daily rolling log files and prunes old ones", () => {
withTempDirSync({ prefix: "openclaw-log-test-" }, (dir) => {
resetLogger();
const today = localDateString(new Date());
const todayPath = path.join(dir, `openclaw-${today}.log`);
setLoggerOverride({ level: "info", file: todayPath });
// create an old file to be pruned
const oldPath = path.join(DEFAULT_LOG_DIR, "openclaw-2000-01-01.log");
fs.mkdirSync(DEFAULT_LOG_DIR, { recursive: true });
fs.writeFileSync(oldPath, "old");
fs.utimesSync(oldPath, new Date(0), new Date(0));
fs.rmSync(todayPath, { force: true });
// create an old file to be pruned
const oldPath = path.join(dir, "openclaw-2000-01-01.log");
fs.writeFileSync(oldPath, "old");
fs.utimesSync(oldPath, new Date(0), new Date(0));
logInfo("roll-me");
logInfo("roll-me");
expect(fs.existsSync(todayPath)).toBe(true);
expect(fs.readFileSync(todayPath, "utf-8")).toContain("roll-me");
expect(fs.existsSync(oldPath)).toBe(false);
fs.rmSync(todayPath, { force: true });
expect(fs.existsSync(todayPath)).toBe(true);
expect(fs.readFileSync(todayPath, "utf-8")).toContain("roll-me");
expect(fs.existsSync(oldPath)).toBe(false);
});
});
});