| import { beforeEach, describe, expect, it } from "vitest"; |
| import { |
| coerceFiniteScheduleNumber, |
| clearCronScheduleCacheForTest, |
| computeNextRunAtMs, |
| computePreviousRunAtMs, |
| getCronScheduleCacheSizeForTest, |
| } from "./schedule.js"; |
|
|
| describe("cron schedule", () => { |
| beforeEach(() => { |
| clearCronScheduleCacheForTest(); |
| }); |
|
|
| it("computes next run for cron expression with timezone", () => { |
| |
| const nowMs = Date.parse("2025-12-13T00:00:00.000Z"); |
| const next = computeNextRunAtMs( |
| { kind: "cron", expr: "0 9 * * 3", tz: "America/Los_Angeles" }, |
| nowMs, |
| ); |
| |
| expect(next).toBe(Date.parse("2025-12-17T17:00:00.000Z")); |
| }); |
|
|
| it("does not roll back year for Asia/Shanghai daily cron schedules (#30351)", () => { |
| |
| const nowMs = Date.parse("2026-03-01T00:00:00.000Z"); |
| const next = computeNextRunAtMs( |
| { kind: "cron", expr: "0 8 * * *", tz: "Asia/Shanghai" }, |
| nowMs, |
| ); |
|
|
| |
| expect(next).toBe(Date.parse("2026-03-02T00:00:00.000Z")); |
| expect(next).toBeGreaterThan(nowMs); |
| expect(new Date(next ?? 0).getUTCFullYear()).toBe(2026); |
| }); |
|
|
| it("throws a clear error when cron expr is missing at runtime", () => { |
| const nowMs = Date.parse("2025-12-13T00:00:00.000Z"); |
| expect(() => |
| computeNextRunAtMs( |
| { |
| kind: "cron", |
| } as unknown as { kind: "cron"; expr: string; tz?: string }, |
| nowMs, |
| ), |
| ).toThrow("invalid cron schedule: expr is required"); |
| }); |
|
|
| it("supports legacy cron field when expr is missing", () => { |
| const nowMs = Date.parse("2025-12-13T00:00:00.000Z"); |
| const next = computeNextRunAtMs( |
| { |
| kind: "cron", |
| cron: "0 9 * * 3", |
| tz: "America/Los_Angeles", |
| } as unknown as { kind: "cron"; expr: string; tz?: string }, |
| nowMs, |
| ); |
| expect(next).toBe(Date.parse("2025-12-17T17:00:00.000Z")); |
| }); |
|
|
| it("computes next run for every schedule", () => { |
| const anchor = Date.parse("2025-12-13T00:00:00.000Z"); |
| const now = anchor + 10_000; |
| const next = computeNextRunAtMs({ kind: "every", everyMs: 30_000, anchorMs: anchor }, now); |
| expect(next).toBe(anchor + 30_000); |
| }); |
|
|
| it("computes next run for every schedule when anchorMs is not provided", () => { |
| const now = Date.parse("2025-12-13T00:00:00.000Z"); |
| const next = computeNextRunAtMs({ kind: "every", everyMs: 30_000 }, now); |
|
|
| |
| expect(next).toBe(now + 30_000); |
| }); |
|
|
| it("handles string-typed everyMs and anchorMs from legacy persisted data", () => { |
| const anchor = Date.parse("2025-12-13T00:00:00.000Z"); |
| const now = anchor + 10_000; |
| const next = computeNextRunAtMs( |
| { |
| kind: "every", |
| everyMs: "30000" as unknown as number, |
| anchorMs: `${anchor}` as unknown as number, |
| }, |
| now, |
| ); |
| expect(next).toBe(anchor + 30_000); |
| }); |
|
|
| it("returns undefined for non-numeric string everyMs", () => { |
| const now = Date.now(); |
| const next = computeNextRunAtMs({ kind: "every", everyMs: "abc" as unknown as number }, now); |
| expect(next).toBeUndefined(); |
| }); |
|
|
| it("advances when now matches anchor for every schedule", () => { |
| const anchor = Date.parse("2025-12-13T00:00:00.000Z"); |
| const next = computeNextRunAtMs({ kind: "every", everyMs: 30_000, anchorMs: anchor }, anchor); |
| expect(next).toBe(anchor + 30_000); |
| }); |
|
|
| it("never returns a past timestamp for Asia/Shanghai daily schedule (#30351)", () => { |
| const nowMs = Date.parse("2026-03-01T00:00:00.000Z"); |
| const next = computeNextRunAtMs( |
| { kind: "cron", expr: "0 8 * * *", tz: "Asia/Shanghai" }, |
| nowMs, |
| ); |
| expect(next).toBeDefined(); |
| expect(next!).toBeGreaterThan(nowMs); |
| }); |
|
|
| it("never returns a previous run that is at-or-after now", () => { |
| const nowMs = Date.parse("2026-03-01T00:00:00.000Z"); |
| const previous = computePreviousRunAtMs( |
| { kind: "cron", expr: "0 8 * * *", tz: "Asia/Shanghai" }, |
| nowMs, |
| ); |
| if (previous !== undefined) { |
| expect(previous).toBeLessThan(nowMs); |
| } |
| }); |
|
|
| it("reuses compiled cron evaluators for the same expression/timezone", () => { |
| const nowMs = Date.parse("2026-03-01T00:00:00.000Z"); |
| expect(getCronScheduleCacheSizeForTest()).toBe(0); |
|
|
| const first = computeNextRunAtMs( |
| { kind: "cron", expr: "0 8 * * *", tz: "Asia/Shanghai" }, |
| nowMs, |
| ); |
| const second = computeNextRunAtMs( |
| { kind: "cron", expr: "0 8 * * *", tz: "Asia/Shanghai" }, |
| nowMs + 1_000, |
| ); |
| const third = computeNextRunAtMs({ kind: "cron", expr: "0 8 * * *", tz: "UTC" }, nowMs); |
|
|
| expect(first).toBeDefined(); |
| expect(second).toBeDefined(); |
| expect(third).toBeDefined(); |
| expect(getCronScheduleCacheSizeForTest()).toBe(2); |
| }); |
|
|
| describe("cron with specific seconds (6-field pattern)", () => { |
| |
| const dailyNoon = { kind: "cron" as const, expr: "0 0 12 * * *", tz: "UTC" }; |
| const noonMs = Date.parse("2026-02-08T12:00:00.000Z"); |
|
|
| it("advances past current second when nowMs is exactly at the match", () => { |
| |
| |
| const next = computeNextRunAtMs(dailyNoon, noonMs); |
| expect(next).toBe(noonMs + 86_400_000); |
| }); |
|
|
| it("advances past current second when nowMs is mid-second (.500) within the match", () => { |
| |
| const next = computeNextRunAtMs(dailyNoon, noonMs + 500); |
| expect(next).toBe(noonMs + 86_400_000); |
| }); |
|
|
| it("advances past current second when nowMs is late in the matching second (.999)", () => { |
| const next = computeNextRunAtMs(dailyNoon, noonMs + 999); |
| expect(next).toBe(noonMs + 86_400_000); |
| }); |
|
|
| it("advances to next day once the matching second is fully past", () => { |
| const next = computeNextRunAtMs(dailyNoon, noonMs + 1000); |
| expect(next).toBe(noonMs + 86_400_000); |
| }); |
|
|
| it("returns today when nowMs is before the match", () => { |
| const next = computeNextRunAtMs(dailyNoon, noonMs - 500); |
| expect(next).toBe(noonMs); |
| }); |
|
|
| it("advances to next day when job completes within same second it fired (#17821)", () => { |
| |
| |
| |
| |
| |
| const completedAtMs = noonMs + 21; |
| const next = computeNextRunAtMs(dailyNoon, completedAtMs); |
| expect(next).toBe(noonMs + 86_400_000); |
| }); |
|
|
| it("advances to next day when job completes just before second boundary (#17821)", () => { |
| |
| const completedAtMs = noonMs + 999; |
| const next = computeNextRunAtMs(dailyNoon, completedAtMs); |
| expect(next).toBe(noonMs + 86_400_000); |
| }); |
| }); |
| }); |
|
|
| describe("coerceFiniteScheduleNumber", () => { |
| it("returns finite numbers directly", () => { |
| expect(coerceFiniteScheduleNumber(60_000)).toBe(60_000); |
| }); |
|
|
| it("parses numeric strings", () => { |
| expect(coerceFiniteScheduleNumber("60000")).toBe(60_000); |
| expect(coerceFiniteScheduleNumber(" 60000 ")).toBe(60_000); |
| }); |
|
|
| it("returns undefined for invalid inputs", () => { |
| expect(coerceFiniteScheduleNumber("")).toBeUndefined(); |
| expect(coerceFiniteScheduleNumber("abc")).toBeUndefined(); |
| expect(coerceFiniteScheduleNumber(NaN)).toBeUndefined(); |
| expect(coerceFiniteScheduleNumber(Infinity)).toBeUndefined(); |
| expect(coerceFiniteScheduleNumber(null)).toBeUndefined(); |
| expect(coerceFiniteScheduleNumber(undefined)).toBeUndefined(); |
| }); |
| }); |
|
|