|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { computeJobNextRunAtMs } from "./service/jobs.js"; |
| 3 | +import type { CronJob } from "./types.js"; |
| 4 | + |
| 5 | +const ORIGINAL_AT_MS = Date.parse("2026-02-22T10:00:00.000Z"); |
| 6 | +const LAST_RUN_AT_MS = Date.parse("2026-02-22T10:00:05.000Z"); // ran shortly after scheduled time |
| 7 | +const RESCHEDULED_AT_MS = Date.parse("2026-02-22T12:00:00.000Z"); // rescheduled to 2 hours later |
| 8 | + |
| 9 | +function createAtJob( |
| 10 | + overrides: { state?: CronJob["state"]; schedule?: CronJob["schedule"] } = {}, |
| 11 | +): CronJob { |
| 12 | + return { |
| 13 | + id: "issue-19676", |
| 14 | + name: "one-shot-reminder", |
| 15 | + enabled: true, |
| 16 | + createdAtMs: ORIGINAL_AT_MS - 60_000, |
| 17 | + updatedAtMs: ORIGINAL_AT_MS - 60_000, |
| 18 | + schedule: overrides.schedule ?? { kind: "at", at: new Date(ORIGINAL_AT_MS).toISOString() }, |
| 19 | + sessionTarget: "isolated", |
| 20 | + wakeMode: "next-heartbeat", |
| 21 | + payload: { kind: "agentTurn", message: "reminder" }, |
| 22 | + delivery: { mode: "none" }, |
| 23 | + state: { ...overrides.state }, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +describe("Cron issue #19676 at-job reschedule", () => { |
| 28 | + it("returns undefined for a completed one-shot job that has not been rescheduled", () => { |
| 29 | + const job = createAtJob({ |
| 30 | + state: { lastStatus: "ok", lastRunAtMs: LAST_RUN_AT_MS }, |
| 31 | + }); |
| 32 | + const nowMs = LAST_RUN_AT_MS + 1_000; |
| 33 | + expect(computeJobNextRunAtMs(job, nowMs)).toBeUndefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("returns the new atMs when a completed one-shot job is rescheduled to a future time", () => { |
| 37 | + const job = createAtJob({ |
| 38 | + schedule: { kind: "at", at: new Date(RESCHEDULED_AT_MS).toISOString() }, |
| 39 | + state: { lastStatus: "ok", lastRunAtMs: LAST_RUN_AT_MS }, |
| 40 | + }); |
| 41 | + const nowMs = LAST_RUN_AT_MS + 1_000; |
| 42 | + expect(computeJobNextRunAtMs(job, nowMs)).toBe(RESCHEDULED_AT_MS); |
| 43 | + }); |
| 44 | + |
| 45 | + it("returns the new atMs when rescheduled via legacy numeric atMs field", () => { |
| 46 | + const job = createAtJob({ |
| 47 | + state: { lastStatus: "ok", lastRunAtMs: LAST_RUN_AT_MS }, |
| 48 | + }); |
| 49 | + // Simulate legacy numeric atMs field on the schedule object. |
| 50 | + const schedule = job.schedule as { kind: "at"; atMs?: number }; |
| 51 | + schedule.atMs = RESCHEDULED_AT_MS; |
| 52 | + const nowMs = LAST_RUN_AT_MS + 1_000; |
| 53 | + expect(computeJobNextRunAtMs(job, nowMs)).toBe(RESCHEDULED_AT_MS); |
| 54 | + }); |
| 55 | + |
| 56 | + it("returns undefined when rescheduled to a time before the last run", () => { |
| 57 | + const beforeLastRun = LAST_RUN_AT_MS - 60_000; |
| 58 | + const job = createAtJob({ |
| 59 | + schedule: { kind: "at", at: new Date(beforeLastRun).toISOString() }, |
| 60 | + state: { lastStatus: "ok", lastRunAtMs: LAST_RUN_AT_MS }, |
| 61 | + }); |
| 62 | + const nowMs = LAST_RUN_AT_MS + 1_000; |
| 63 | + expect(computeJobNextRunAtMs(job, nowMs)).toBeUndefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("still returns atMs for a job that has never run", () => { |
| 67 | + const job = createAtJob(); |
| 68 | + const nowMs = ORIGINAL_AT_MS - 60_000; |
| 69 | + expect(computeJobNextRunAtMs(job, nowMs)).toBe(ORIGINAL_AT_MS); |
| 70 | + }); |
| 71 | + |
| 72 | + it("still returns atMs for a job whose last status is error", () => { |
| 73 | + const job = createAtJob({ |
| 74 | + state: { lastStatus: "error", lastRunAtMs: LAST_RUN_AT_MS }, |
| 75 | + }); |
| 76 | + const nowMs = LAST_RUN_AT_MS + 1_000; |
| 77 | + expect(computeJobNextRunAtMs(job, nowMs)).toBe(ORIGINAL_AT_MS); |
| 78 | + }); |
| 79 | + |
| 80 | + it("returns undefined for a disabled job even if rescheduled", () => { |
| 81 | + const job = createAtJob({ |
| 82 | + schedule: { kind: "at", at: new Date(RESCHEDULED_AT_MS).toISOString() }, |
| 83 | + state: { lastStatus: "ok", lastRunAtMs: LAST_RUN_AT_MS }, |
| 84 | + }); |
| 85 | + job.enabled = false; |
| 86 | + const nowMs = LAST_RUN_AT_MS + 1_000; |
| 87 | + expect(computeJobNextRunAtMs(job, nowMs)).toBeUndefined(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments