|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { CronService } from "./service.js"; |
| 3 | +import { |
| 4 | + createCronStoreHarness, |
| 5 | + createFinishedBarrier, |
| 6 | + createNoopLogger, |
| 7 | + installCronTestHooks, |
| 8 | +} from "./service.test-harness.js"; |
| 9 | + |
| 10 | +const noopLogger = createNoopLogger(); |
| 11 | +const { makeStorePath } = createCronStoreHarness(); |
| 12 | +installCronTestHooks({ logger: noopLogger }); |
| 13 | + |
| 14 | +describe("update() must not drop a due every-job's pending run", () => { |
| 15 | + it("preserves a due every-job nextRunAtMs on an idempotent schedule re-save", async () => { |
| 16 | + const store = await makeStorePath(); |
| 17 | + const base = Date.parse("2025-12-13T00:00:00.000Z"); |
| 18 | + |
| 19 | + const finished = createFinishedBarrier(); |
| 20 | + const cron = new CronService({ |
| 21 | + storePath: store.storePath, |
| 22 | + cronEnabled: true, |
| 23 | + log: noopLogger, |
| 24 | + enqueueSystemEvent: vi.fn(), |
| 25 | + requestHeartbeat: vi.fn(), |
| 26 | + runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" as const })), |
| 27 | + onEvent: finished.onEvent, |
| 28 | + }); |
| 29 | + |
| 30 | + await cron.start(); |
| 31 | + |
| 32 | + const job = await cron.add({ |
| 33 | + name: "every 10s", |
| 34 | + enabled: true, |
| 35 | + schedule: { kind: "every", everyMs: 10_000 }, |
| 36 | + sessionTarget: "isolated", |
| 37 | + wakeMode: "next-heartbeat", |
| 38 | + payload: { kind: "agentTurn", message: "tick" }, |
| 39 | + }); |
| 40 | + const jobId = job.id; |
| 41 | + expect(job.state.nextRunAtMs).toBe(base + 10_000); |
| 42 | + |
| 43 | + // Fire once so the job carries lastRunAtMs and a real next due slot. |
| 44 | + vi.setSystemTime(new Date(base + 10_000 + 5)); |
| 45 | + const firstRun = finished.waitForOk(jobId); |
| 46 | + await vi.runOnlyPendingTimersAsync(); |
| 47 | + await firstRun; |
| 48 | + |
| 49 | + let current = (await cron.list({ includeDisabled: true })).find((j) => j.id === jobId)!; |
| 50 | + const lastRunAtMs = current.state.lastRunAtMs!; |
| 51 | + const dueSlot = current.state.nextRunAtMs!; |
| 52 | + expect(dueSlot).toBe(lastRunAtMs + 10_000); |
| 53 | + |
| 54 | + // Advance past the next slot so it is now due, before the timer services it. |
| 55 | + vi.setSystemTime(new Date(dueSlot + 50)); |
| 56 | + const nowDue = dueSlot + 50; |
| 57 | + |
| 58 | + // User edits the job and the control UI resubmits the unchanged schedule |
| 59 | + // (a normal idempotent re-save, e.g. while changing the message). This must |
| 60 | + // not advance the already-due slot. |
| 61 | + await cron.update(jobId, { schedule: { kind: "every", everyMs: 10_000 } }); |
| 62 | + |
| 63 | + current = (await cron.list({ includeDisabled: true })).find((j) => j.id === jobId)!; |
| 64 | + // Correct: the due slot is preserved so the pending run still fires. |
| 65 | + // Buggy (current main): nextRunAtMs jumps to dueSlot + 10_000 (> nowDue), |
| 66 | + // silently dropping this slot's run. |
| 67 | + expect(current.state.lastRunAtMs).toBe(lastRunAtMs); |
| 68 | + expect(current.state.nextRunAtMs).toBe(dueSlot); |
| 69 | + expect(current.state.nextRunAtMs).toBeLessThanOrEqual(nowDue); |
| 70 | + // The cadence anchor must not re-phase to "now" on an idempotent re-save. |
| 71 | + expect(current.schedule).toMatchObject({ kind: "every", anchorMs: base }); |
| 72 | + |
| 73 | + cron.stop(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("re-anchors an every-job to the edit time when the interval actually changes", async () => { |
| 77 | + const store = await makeStorePath(); |
| 78 | + const base = Date.parse("2025-12-13T00:00:00.000Z"); |
| 79 | + |
| 80 | + const finished = createFinishedBarrier(); |
| 81 | + const cron = new CronService({ |
| 82 | + storePath: store.storePath, |
| 83 | + cronEnabled: true, |
| 84 | + log: noopLogger, |
| 85 | + enqueueSystemEvent: vi.fn(), |
| 86 | + requestHeartbeat: vi.fn(), |
| 87 | + runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" as const })), |
| 88 | + onEvent: finished.onEvent, |
| 89 | + }); |
| 90 | + |
| 91 | + await cron.start(); |
| 92 | + |
| 93 | + const job = await cron.add({ |
| 94 | + name: "every 10s", |
| 95 | + enabled: true, |
| 96 | + schedule: { kind: "every", everyMs: 10_000 }, |
| 97 | + sessionTarget: "isolated", |
| 98 | + wakeMode: "next-heartbeat", |
| 99 | + payload: { kind: "agentTurn", message: "tick" }, |
| 100 | + }); |
| 101 | + const jobId = job.id; |
| 102 | + expect(job.schedule).toMatchObject({ kind: "every", anchorMs: base }); |
| 103 | + |
| 104 | + // User edits the interval from 10s to 1h. The control UI omits the internal |
| 105 | + // anchorMs, so the new cadence must start from the edit time rather than |
| 106 | + // keeping the old phase; nextRunAtMs is one new interval from now. |
| 107 | + const editTime = base + 3_000; |
| 108 | + vi.setSystemTime(new Date(editTime)); |
| 109 | + await cron.update(jobId, { schedule: { kind: "every", everyMs: 3_600_000 } }); |
| 110 | + |
| 111 | + const current = (await cron.list({ includeDisabled: true })).find((j) => j.id === jobId)!; |
| 112 | + expect(current.schedule).toMatchObject({ |
| 113 | + kind: "every", |
| 114 | + everyMs: 3_600_000, |
| 115 | + anchorMs: editTime, |
| 116 | + }); |
| 117 | + expect(current.state.nextRunAtMs).toBe(editTime + 3_600_000); |
| 118 | + |
| 119 | + cron.stop(); |
| 120 | + }); |
| 121 | + |
| 122 | + it("preserves a due cron-job nextRunAtMs on an idempotent schedule re-save", async () => { |
| 123 | + const store = await makeStorePath(); |
| 124 | + vi.setSystemTime(new Date("2025-12-13T08:59:00.000Z")); |
| 125 | + |
| 126 | + const finished = createFinishedBarrier(); |
| 127 | + const cron = new CronService({ |
| 128 | + storePath: store.storePath, |
| 129 | + cronEnabled: true, |
| 130 | + log: noopLogger, |
| 131 | + enqueueSystemEvent: vi.fn(), |
| 132 | + requestHeartbeat: vi.fn(), |
| 133 | + runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" as const })), |
| 134 | + onEvent: finished.onEvent, |
| 135 | + }); |
| 136 | + |
| 137 | + await cron.start(); |
| 138 | + |
| 139 | + const job = await cron.add({ |
| 140 | + name: "daily 9am", |
| 141 | + enabled: true, |
| 142 | + schedule: { kind: "cron", expr: "0 9 * * *" }, |
| 143 | + sessionTarget: "isolated", |
| 144 | + wakeMode: "next-heartbeat", |
| 145 | + payload: { kind: "agentTurn", message: "report" }, |
| 146 | + }); |
| 147 | + const jobId = job.id; |
| 148 | + const dueSlot = job.state.nextRunAtMs!; |
| 149 | + |
| 150 | + // Advance past the 09:00 slot so it is now due, before the timer fires it. |
| 151 | + vi.setSystemTime(new Date(dueSlot + 50)); |
| 152 | + const nowDue = dueSlot + 50; |
| 153 | + |
| 154 | + await cron.update(jobId, { schedule: { kind: "cron", expr: "0 9 * * *" } }); |
| 155 | + |
| 156 | + const current = (await cron.list({ includeDisabled: true })).find((j) => j.id === jobId)!; |
| 157 | + // Correct: the due slot is preserved. Buggy main: nextRunAtMs jumps to the |
| 158 | + // next day, dropping today's run. |
| 159 | + expect(current.state.nextRunAtMs).toBe(dueSlot); |
| 160 | + expect(current.state.nextRunAtMs).toBeLessThanOrEqual(nowDue); |
| 161 | + |
| 162 | + cron.stop(); |
| 163 | + }); |
| 164 | +}); |
0 commit comments