Files
openclaw/src/cron/store.test.ts
zerone0x a5f0a9240f fix(cron): retry rename on EBUSY and fall back to copyFile on Windows
Landed from contributor PR #16932 with additional changelog alignment and verification.
2026-03-01 20:02:24 -06:00

148 lines
4.5 KiB
TypeScript

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { loadCronStore, resolveCronStorePath, saveCronStore } from "./store.js";
import type { CronStoreFile } from "./types.js";
async function makeStorePath() {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cron-store-"));
return {
dir,
storePath: path.join(dir, "jobs.json"),
cleanup: async () => {
await fs.rm(dir, { recursive: true, force: true });
},
};
}
function makeStore(jobId: string, enabled: boolean): CronStoreFile {
const now = Date.now();
return {
version: 1,
jobs: [
{
id: jobId,
name: `Job ${jobId}`,
enabled,
createdAtMs: now,
updatedAtMs: now,
schedule: { kind: "every", everyMs: 60_000 },
sessionTarget: "main",
wakeMode: "next-heartbeat",
payload: { kind: "systemEvent", text: `tick-${jobId}` },
state: {},
},
],
};
}
describe("resolveCronStorePath", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it("uses OPENCLAW_HOME for tilde expansion", () => {
vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home");
vi.stubEnv("HOME", "/home/other");
const result = resolveCronStorePath("~/cron/jobs.json");
expect(result).toBe(path.resolve("/srv/openclaw-home", "cron", "jobs.json"));
});
});
describe("cron store", () => {
it("returns empty store when file does not exist", async () => {
const store = await makeStorePath();
const loaded = await loadCronStore(store.storePath);
expect(loaded).toEqual({ version: 1, jobs: [] });
await store.cleanup();
});
it("throws when store contains invalid JSON", async () => {
const store = await makeStorePath();
await fs.writeFile(store.storePath, "{ not json", "utf-8");
await expect(loadCronStore(store.storePath)).rejects.toThrow(/Failed to parse cron store/i);
await store.cleanup();
});
it("does not create a backup file when saving unchanged content", async () => {
const store = await makeStorePath();
const payload = makeStore("job-1", true);
await saveCronStore(store.storePath, payload);
await saveCronStore(store.storePath, payload);
await expect(fs.stat(`${store.storePath}.bak`)).rejects.toThrow();
await store.cleanup();
});
it("backs up previous content before replacing the store", async () => {
const store = await makeStorePath();
const first = makeStore("job-1", true);
const second = makeStore("job-2", false);
await saveCronStore(store.storePath, first);
await saveCronStore(store.storePath, second);
const currentRaw = await fs.readFile(store.storePath, "utf-8");
const backupRaw = await fs.readFile(`${store.storePath}.bak`, "utf-8");
expect(JSON.parse(currentRaw)).toEqual(second);
expect(JSON.parse(backupRaw)).toEqual(first);
await store.cleanup();
});
});
describe("saveCronStore", () => {
const dummyStore: CronStoreFile = { version: 1, jobs: [] };
it("persists and round-trips a store file", async () => {
const { storePath, cleanup } = await makeStorePath();
await saveCronStore(storePath, dummyStore);
const loaded = await loadCronStore(storePath);
expect(loaded).toEqual(dummyStore);
await cleanup();
});
it("retries rename on EBUSY then succeeds", async () => {
const { storePath, cleanup } = await makeStorePath();
const origRename = fs.rename.bind(fs);
let ebusyCount = 0;
const spy = vi.spyOn(fs, "rename").mockImplementation(async (src, dest) => {
if (ebusyCount < 2) {
ebusyCount++;
const err = new Error("EBUSY") as NodeJS.ErrnoException;
err.code = "EBUSY";
throw err;
}
return origRename(src, dest);
});
await saveCronStore(storePath, dummyStore);
expect(ebusyCount).toBe(2);
const loaded = await loadCronStore(storePath);
expect(loaded).toEqual(dummyStore);
spy.mockRestore();
await cleanup();
});
it("falls back to copyFile on EPERM (Windows)", async () => {
const { storePath, cleanup } = await makeStorePath();
const spy = vi.spyOn(fs, "rename").mockImplementation(async () => {
const err = new Error("EPERM") as NodeJS.ErrnoException;
err.code = "EPERM";
throw err;
});
await saveCronStore(storePath, dummyStore);
const loaded = await loadCronStore(storePath);
expect(loaded).toEqual(dummyStore);
spy.mockRestore();
await cleanup();
});
});