test(cron): dedupe run-log temp fixtures and cover invalid line filtering

This commit is contained in:
Peter Steinberger
2026-02-21 19:33:08 +00:00
parent 7036352d94
commit d015dc9216

View File

@@ -5,6 +5,15 @@ import { describe, expect, it } from "vitest";
import { appendCronRunLog, readCronRunLogEntries, resolveCronRunLogPath } from "./run-log.js"; import { appendCronRunLog, readCronRunLogEntries, resolveCronRunLogPath } from "./run-log.js";
describe("cron run log", () => { describe("cron run log", () => {
async function withRunLogDir(prefix: string, run: (dir: string) => Promise<void>) {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
try {
await run(dir);
} finally {
await fs.rm(dir, { recursive: true, force: true });
}
}
it("resolves store path to per-job runs/<jobId>.jsonl", () => { it("resolves store path to per-job runs/<jobId>.jsonl", () => {
const storePath = path.join(os.tmpdir(), "cron", "jobs.json"); const storePath = path.join(os.tmpdir(), "cron", "jobs.json");
const p = resolveCronRunLogPath({ storePath, jobId: "job-1" }); const p = resolveCronRunLogPath({ storePath, jobId: "job-1" });
@@ -12,7 +21,7 @@ describe("cron run log", () => {
}); });
it("appends JSONL and prunes by line count", async () => { it("appends JSONL and prunes by line count", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cron-log-")); await withRunLogDir("openclaw-cron-log-", async (dir) => {
const logPath = path.join(dir, "runs", "job-1.jsonl"); const logPath = path.join(dir, "runs", "job-1.jsonl");
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
@@ -37,12 +46,11 @@ describe("cron run log", () => {
expect(lines.length).toBe(3); expect(lines.length).toBe(3);
const last = JSON.parse(lines[2] ?? "{}") as { ts?: number }; const last = JSON.parse(lines[2] ?? "{}") as { ts?: number };
expect(last.ts).toBe(1009); expect(last.ts).toBe(1009);
});
await fs.rm(dir, { recursive: true, force: true });
}); });
it("reads newest entries and filters by jobId", async () => { it("reads newest entries and filters by jobId", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cron-log-read-")); await withRunLogDir("openclaw-cron-log-read-", async (dir) => {
const logPathA = path.join(dir, "runs", "a.jsonl"); const logPathA = path.join(dir, "runs", "a.jsonl");
const logPathB = path.join(dir, "runs", "b.jsonl"); const logPathB = path.join(dir, "runs", "b.jsonl");
@@ -94,12 +102,38 @@ describe("cron run log", () => {
jobId: "b", jobId: "b",
}); });
expect(wrongFilter).toEqual([]); expect(wrongFilter).toEqual([]);
});
});
await fs.rm(dir, { recursive: true, force: true }); it("ignores invalid and non-finished lines while preserving delivered flag", async () => {
await withRunLogDir("openclaw-cron-log-filter-", async (dir) => {
const logPath = path.join(dir, "runs", "job-1.jsonl");
await fs.mkdir(path.dirname(logPath), { recursive: true });
await fs.writeFile(
logPath,
[
'{"bad":',
JSON.stringify({ ts: 1, jobId: "job-1", action: "started", status: "ok" }),
JSON.stringify({
ts: 2,
jobId: "job-1",
action: "finished",
status: "ok",
delivered: true,
}),
].join("\n") + "\n",
"utf-8",
);
const entries = await readCronRunLogEntries(logPath, { limit: 10, jobId: "job-1" });
expect(entries).toHaveLength(1);
expect(entries[0]?.ts).toBe(2);
expect(entries[0]?.delivered).toBe(true);
});
}); });
it("reads telemetry fields", async () => { it("reads telemetry fields", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cron-log-telemetry-")); await withRunLogDir("openclaw-cron-log-telemetry-", async (dir) => {
const logPath = path.join(dir, "runs", "job-1.jsonl"); const logPath = path.join(dir, "runs", "job-1.jsonl");
await appendCronRunLog(logPath, { await appendCronRunLog(logPath, {
@@ -145,7 +179,6 @@ describe("cron run log", () => {
expect(entries[1]?.model).toBeUndefined(); expect(entries[1]?.model).toBeUndefined();
expect(entries[1]?.provider).toBeUndefined(); expect(entries[1]?.provider).toBeUndefined();
expect(entries[1]?.usage?.input_tokens).toBeUndefined(); expect(entries[1]?.usage?.input_tokens).toBeUndefined();
});
await fs.rm(dir, { recursive: true, force: true });
}); });
}); });