|
| 1 | +// Restart catch-up must include a missed cron slot throughout its first second. |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { CronService } from "./service.js"; |
| 4 | +import { setupCronServiceSuite, writeCronStoreSnapshot } from "./service.test-harness.js"; |
| 5 | + |
| 6 | +const { logger, makeStorePath } = setupCronServiceSuite({ |
| 7 | + prefix: "openclaw-cron-subsecond-", |
| 8 | + baseTimeIso: "2025-12-13T04:02:00.500Z", |
| 9 | +}); |
| 10 | + |
| 11 | +describe("CronService restart catch-up within a cron slot's first second", () => { |
| 12 | + it("replays the missed slot when the persisted next run already advanced", async () => { |
| 13 | + const restartAtMs = Date.parse("2025-12-13T04:02:00.500Z"); |
| 14 | + const store = await makeStorePath(); |
| 15 | + const runCommandJob = vi.fn(async () => ({ status: "ok" as const, summary: "done" })); |
| 16 | + |
| 17 | + await writeCronStoreSnapshot({ |
| 18 | + storePath: store.storePath, |
| 19 | + jobs: [ |
| 20 | + { |
| 21 | + id: "restart-missed-subsecond-slot", |
| 22 | + name: "every minute", |
| 23 | + enabled: true, |
| 24 | + createdAtMs: Date.parse("2025-12-10T12:00:00.000Z"), |
| 25 | + updatedAtMs: Date.parse("2025-12-13T04:01:00.000Z"), |
| 26 | + schedule: { kind: "cron", expr: "* * * * *", tz: "UTC" }, |
| 27 | + sessionTarget: "isolated", |
| 28 | + wakeMode: "now", |
| 29 | + payload: { kind: "command", argv: ["echo", "FIRED"] }, |
| 30 | + state: { |
| 31 | + nextRunAtMs: Date.parse("2025-12-13T04:03:00.000Z"), |
| 32 | + lastRunAtMs: Date.parse("2025-12-13T04:01:00.000Z"), |
| 33 | + lastStatus: "ok", |
| 34 | + }, |
| 35 | + }, |
| 36 | + ], |
| 37 | + }); |
| 38 | + |
| 39 | + const cron = new CronService({ |
| 40 | + storePath: store.storePath, |
| 41 | + cronEnabled: true, |
| 42 | + log: logger, |
| 43 | + enqueueSystemEvent: vi.fn(), |
| 44 | + requestHeartbeat: vi.fn(), |
| 45 | + runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" as const })), |
| 46 | + runCommandJob: runCommandJob as never, |
| 47 | + }); |
| 48 | + |
| 49 | + try { |
| 50 | + await cron.start(); |
| 51 | + expect(runCommandJob).toHaveBeenCalledTimes(1); |
| 52 | + expect(cron.getJob("restart-missed-subsecond-slot")?.state.lastRunAtMs).toBe(restartAtMs); |
| 53 | + } finally { |
| 54 | + cron.stop(); |
| 55 | + await store.cleanup(); |
| 56 | + } |
| 57 | + }); |
| 58 | +}); |
0 commit comments