|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +import { CronService } from "./service.js"; |
| 8 | +import * as storeModule from "./store.js"; |
| 9 | + |
| 10 | +const noopLogger = { |
| 11 | + debug: vi.fn(), |
| 12 | + info: vi.fn(), |
| 13 | + warn: vi.fn(), |
| 14 | + error: vi.fn(), |
| 15 | +}; |
| 16 | + |
| 17 | +async function makeStorePath() { |
| 18 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "zee-cron-timer-")); |
| 19 | + return { |
| 20 | + storePath: path.join(dir, "cron", "jobs.json"), |
| 21 | + cleanup: async () => { |
| 22 | + await fs.rm(dir, { recursive: true, force: true }); |
| 23 | + }, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +describe("CronService timer resilience", () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.useFakeTimers(); |
| 30 | + vi.setSystemTime(new Date("2025-12-13T00:00:00.000Z")); |
| 31 | + noopLogger.debug.mockClear(); |
| 32 | + noopLogger.info.mockClear(); |
| 33 | + noopLogger.warn.mockClear(); |
| 34 | + noopLogger.error.mockClear(); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + vi.useRealTimers(); |
| 39 | + vi.restoreAllMocks(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("re-arms timer after persist failure", async () => { |
| 43 | + const store = await makeStorePath(); |
| 44 | + const enqueueSystemEvent = vi.fn(); |
| 45 | + const requestHeartbeatNow = vi.fn(); |
| 46 | + |
| 47 | + const cron = new CronService({ |
| 48 | + storePath: store.storePath, |
| 49 | + cronEnabled: true, |
| 50 | + log: noopLogger, |
| 51 | + enqueueSystemEvent, |
| 52 | + requestHeartbeatNow, |
| 53 | + runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" })), |
| 54 | + }); |
| 55 | + |
| 56 | + await cron.start(); |
| 57 | + |
| 58 | + // Add a recurring job that fires every second. |
| 59 | + await cron.add({ |
| 60 | + name: "recurring job", |
| 61 | + enabled: true, |
| 62 | + schedule: { kind: "every", everyMs: 1_000 }, |
| 63 | + sessionTarget: "main", |
| 64 | + wakeMode: "next-heartbeat", |
| 65 | + payload: { kind: "systemEvent", text: "tick" }, |
| 66 | + }); |
| 67 | + |
| 68 | + // Make saveCronStore throw on the next call to simulate a persist failure. |
| 69 | + const saveSpy = vi |
| 70 | + .spyOn(storeModule, "saveCronStore") |
| 71 | + .mockRejectedValueOnce(new Error("ENOSPC: no space left on device")); |
| 72 | + |
| 73 | + // Advance past the first due time -- this tick should fail on persist. |
| 74 | + vi.setSystemTime(new Date("2025-12-13T00:00:01.000Z")); |
| 75 | + await vi.runOnlyPendingTimersAsync(); |
| 76 | + |
| 77 | + // The persist error should have been logged via the timer catch handler. |
| 78 | + expect(noopLogger.error).toHaveBeenCalled(); |
| 79 | + |
| 80 | + // Restore normal saveCronStore behavior for the next tick. |
| 81 | + saveSpy.mockRestore(); |
| 82 | + |
| 83 | + // Advance time again -- the timer should still be alive and fire. |
| 84 | + vi.setSystemTime(new Date("2025-12-13T00:00:02.000Z")); |
| 85 | + await vi.runOnlyPendingTimersAsync(); |
| 86 | + |
| 87 | + // The job should have been enqueued on the second tick despite the |
| 88 | + // first tick's persist failure. |
| 89 | + expect(enqueueSystemEvent).toHaveBeenCalledWith("tick", { |
| 90 | + agentId: undefined, |
| 91 | + }); |
| 92 | + |
| 93 | + cron.stop(); |
| 94 | + await store.cleanup(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("clamps timer delay to 60 seconds (drift guard)", async () => { |
| 98 | + const store = await makeStorePath(); |
| 99 | + |
| 100 | + const cron = new CronService({ |
| 101 | + storePath: store.storePath, |
| 102 | + cronEnabled: true, |
| 103 | + log: noopLogger, |
| 104 | + enqueueSystemEvent: vi.fn(), |
| 105 | + requestHeartbeatNow: vi.fn(), |
| 106 | + runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" })), |
| 107 | + }); |
| 108 | + |
| 109 | + await cron.start(); |
| 110 | + |
| 111 | + // Add a job 10 minutes in the future. |
| 112 | + const tenMinutesLater = Date.parse("2025-12-13T00:10:00.000Z"); |
| 113 | + await cron.add({ |
| 114 | + name: "far future job", |
| 115 | + enabled: true, |
| 116 | + schedule: { kind: "at", atMs: tenMinutesLater }, |
| 117 | + sessionTarget: "main", |
| 118 | + wakeMode: "next-heartbeat", |
| 119 | + payload: { kind: "systemEvent", text: "hello" }, |
| 120 | + }); |
| 121 | + |
| 122 | + // The timer should fire after at most 60 seconds, not 10 minutes. |
| 123 | + // Advance 61 seconds and verify the timer fires (even though the |
| 124 | + // job isn't due yet, the scheduler re-evaluates). |
| 125 | + vi.advanceTimersByTime(61_000); |
| 126 | + // Allow async callbacks to flush. |
| 127 | + await vi.runOnlyPendingTimersAsync(); |
| 128 | + |
| 129 | + // The job should not have run yet (not due until T+10min), but the |
| 130 | + // timer should have ticked (verified by no timeout at 10 min). |
| 131 | + // Advance to 10 minutes -- the drift guard means the timer will |
| 132 | + // have re-armed many times by now, eventually catching the due job. |
| 133 | + vi.setSystemTime(new Date("2025-12-13T00:10:00.000Z")); |
| 134 | + await vi.runOnlyPendingTimersAsync(); |
| 135 | + |
| 136 | + cron.stop(); |
| 137 | + await store.cleanup(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments