Release 0.1.3
This commit is contained in:
@@ -1,10 +1,22 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
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 { setVerbose } from "./globals.js";
|
||||
import { logDebug, logError, logInfo, logSuccess, logWarn } from "./logger.js";
|
||||
import { resetLogger, setLoggerOverride } from "./logging.js";
|
||||
import type { RuntimeEnv } from "./runtime.js";
|
||||
|
||||
describe("logger helpers", () => {
|
||||
afterEach(() => {
|
||||
resetLogger();
|
||||
setLoggerOverride(null);
|
||||
setVerbose(false);
|
||||
});
|
||||
|
||||
it("formats messages through runtime log/error", () => {
|
||||
const log = vi.fn();
|
||||
const error = vi.fn();
|
||||
@@ -31,4 +43,40 @@ describe("logger helpers", () => {
|
||||
expect(logVerbose).toHaveBeenCalled();
|
||||
logVerbose.mockRestore();
|
||||
});
|
||||
|
||||
it("writes to configured log file at configured level", () => {
|
||||
const logPath = pathForTest();
|
||||
cleanup(logPath);
|
||||
setLoggerOverride({ level: "debug", file: logPath });
|
||||
logInfo("hello");
|
||||
logDebug("debug-only");
|
||||
const content = fs.readFileSync(logPath, "utf-8");
|
||||
expect(content).toContain("hello");
|
||||
expect(content).toContain("debug-only");
|
||||
cleanup(logPath);
|
||||
});
|
||||
|
||||
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).not.toContain("info-only");
|
||||
expect(content).toContain("warn-only");
|
||||
cleanup(logPath);
|
||||
});
|
||||
});
|
||||
|
||||
function pathForTest() {
|
||||
return path.join(os.tmpdir(), `warelay-log-${crypto.randomUUID()}.log`);
|
||||
}
|
||||
|
||||
function cleanup(file: string) {
|
||||
try {
|
||||
fs.rmSync(file, { force: true });
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user