|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { CronServiceState } from "./service/state.js"; |
| 3 | +import type { CronJob } from "./types.js"; |
| 4 | +import { recomputeNextRunsForMaintenance } from "./service/jobs.js"; |
| 5 | + |
| 6 | +describe("issue #13992 regression - cron jobs skip execution", () => { |
| 7 | + function createMockState(jobs: CronJob[]): CronServiceState { |
| 8 | + return { |
| 9 | + store: { version: 1, jobs }, |
| 10 | + running: false, |
| 11 | + timer: null, |
| 12 | + storeLoadedAtMs: Date.now(), |
| 13 | + deps: { |
| 14 | + storePath: "/mock/path", |
| 15 | + cronEnabled: true, |
| 16 | + nowMs: () => Date.now(), |
| 17 | + log: { |
| 18 | + debug: () => {}, |
| 19 | + info: () => {}, |
| 20 | + warn: () => {}, |
| 21 | + error: () => {}, |
| 22 | + } as never, |
| 23 | + }, |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + it("should NOT recompute nextRunAtMs for past-due jobs during maintenance", () => { |
| 28 | + const now = Date.now(); |
| 29 | + const pastDue = now - 60_000; // 1 minute ago |
| 30 | + |
| 31 | + const job: CronJob = { |
| 32 | + id: "test-job", |
| 33 | + name: "test job", |
| 34 | + enabled: true, |
| 35 | + schedule: { kind: "cron", expr: "0 8 * * *", tz: "UTC" }, |
| 36 | + payload: { kind: "systemEvent", text: "test" }, |
| 37 | + sessionTarget: "main", |
| 38 | + createdAtMs: now - 3600_000, |
| 39 | + updatedAtMs: now - 3600_000, |
| 40 | + state: { |
| 41 | + nextRunAtMs: pastDue, // This is in the past and should NOT be recomputed |
| 42 | + }, |
| 43 | + }; |
| 44 | + |
| 45 | + const state = createMockState([job]); |
| 46 | + recomputeNextRunsForMaintenance(state); |
| 47 | + |
| 48 | + // Should not have changed the past-due nextRunAtMs |
| 49 | + expect(job.state.nextRunAtMs).toBe(pastDue); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should compute missing nextRunAtMs during maintenance", () => { |
| 53 | + const now = Date.now(); |
| 54 | + |
| 55 | + const job: CronJob = { |
| 56 | + id: "test-job", |
| 57 | + name: "test job", |
| 58 | + enabled: true, |
| 59 | + schedule: { kind: "cron", expr: "0 8 * * *", tz: "UTC" }, |
| 60 | + payload: { kind: "systemEvent", text: "test" }, |
| 61 | + sessionTarget: "main", |
| 62 | + createdAtMs: now, |
| 63 | + updatedAtMs: now, |
| 64 | + state: { |
| 65 | + // nextRunAtMs is missing |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + const state = createMockState([job]); |
| 70 | + recomputeNextRunsForMaintenance(state); |
| 71 | + |
| 72 | + // Should have computed a nextRunAtMs |
| 73 | + expect(typeof job.state.nextRunAtMs).toBe("number"); |
| 74 | + expect(job.state.nextRunAtMs).toBeGreaterThan(now); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should clear nextRunAtMs for disabled jobs during maintenance", () => { |
| 78 | + const now = Date.now(); |
| 79 | + const futureTime = now + 3600_000; |
| 80 | + |
| 81 | + const job: CronJob = { |
| 82 | + id: "test-job", |
| 83 | + name: "test job", |
| 84 | + enabled: false, // Disabled |
| 85 | + schedule: { kind: "cron", expr: "0 8 * * *", tz: "UTC" }, |
| 86 | + payload: { kind: "systemEvent", text: "test" }, |
| 87 | + sessionTarget: "main", |
| 88 | + createdAtMs: now, |
| 89 | + updatedAtMs: now, |
| 90 | + state: { |
| 91 | + nextRunAtMs: futureTime, |
| 92 | + }, |
| 93 | + }; |
| 94 | + |
| 95 | + const state = createMockState([job]); |
| 96 | + recomputeNextRunsForMaintenance(state); |
| 97 | + |
| 98 | + // Should have cleared nextRunAtMs for disabled job |
| 99 | + expect(job.state.nextRunAtMs).toBeUndefined(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should clear stuck running markers during maintenance", () => { |
| 103 | + const now = Date.now(); |
| 104 | + const stuckTime = now - 3 * 60 * 60_000; // 3 hours ago (> 2 hour threshold) |
| 105 | + const futureTime = now + 3600_000; |
| 106 | + |
| 107 | + const job: CronJob = { |
| 108 | + id: "test-job", |
| 109 | + name: "test job", |
| 110 | + enabled: true, |
| 111 | + schedule: { kind: "cron", expr: "0 8 * * *", tz: "UTC" }, |
| 112 | + payload: { kind: "systemEvent", text: "test" }, |
| 113 | + sessionTarget: "main", |
| 114 | + createdAtMs: now, |
| 115 | + updatedAtMs: now, |
| 116 | + state: { |
| 117 | + nextRunAtMs: futureTime, |
| 118 | + runningAtMs: stuckTime, // Stuck running marker |
| 119 | + }, |
| 120 | + }; |
| 121 | + |
| 122 | + const state = createMockState([job]); |
| 123 | + recomputeNextRunsForMaintenance(state); |
| 124 | + |
| 125 | + // Should have cleared stuck running marker |
| 126 | + expect(job.state.runningAtMs).toBeUndefined(); |
| 127 | + // But should NOT have changed nextRunAtMs (it's still future) |
| 128 | + expect(job.state.nextRunAtMs).toBe(futureTime); |
| 129 | + }); |
| 130 | +}); |
0 commit comments