Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as DateTime from "effect/DateTime";
3
4import {
5 isMissedFixedTimeRun,
6 isSameSchedule,
7 nextScheduledRunAt,
8 parseTimeOfDay,
9} from "./Schedule.ts";
10
11describe("scheduled task schedule calculation", () => {
12 it("parses 24-hour times", () => {
13 expect(parseTimeOfDay("09:30")).toEqual({ hour: 9, minute: 30 });
14 expect(parseTimeOfDay("23:59")).toEqual({ hour: 23, minute: 59 });
15 expect(parseTimeOfDay("25:00")).toBeNull();
16 });
17
18 it("calculates interval schedules from the supplied instant", () => {
19 const next = nextScheduledRunAt(
20 { type: "interval", everyMs: 5 * 60_000 },
21 DateTime.makeUnsafe("2026-07-01T16:00:00.000Z"),
22 );
23 expect(next ? DateTime.formatIso(DateTime.toUtc(next)) : null).toBe("2026-07-01T16:05:00.000Z");
24 });
25
26 it("clamps legacy sub-minute intervals to the one-minute execution floor", () => {
27 const next = nextScheduledRunAt(
28 { type: "interval", everyMs: 1_000 },
29 DateTime.makeUnsafe("2026-07-01T16:00:00.000Z"),
30 );
31 expect(next ? DateTime.formatIso(DateTime.toUtc(next)) : null).toBe("2026-07-01T16:01:00.000Z");
32 });
33
34 it("skips to the next matching fixed-time weekday", () => {
35 const next = nextScheduledRunAt(
36 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2, 3, 4, 5] },
37 DateTime.makeZonedUnsafe(
38 {
39 year: 2026,
40 month: 7,
41 day: 3,
42 hour: 10,
43 minute: 0,
44 second: 0,
45 millisecond: 0,
46 },
47 { timeZone: "America/Los_Angeles", adjustForTimeZone: true },
48 ),
49 );
50 const parts = next ? DateTime.toParts(next) : null;
51 expect(parts?.weekDay).toBe(1);
52 expect(parts?.hour).toBe(9);
53 expect(parts?.minute).toBe(0);
54 });
55
56 it("skips fixed-time runs missed by more than the grace window", () => {
57 const fixedTime = { type: "fixed_time", timeOfDay: "09:00" } as const;
58 const dueAt = DateTime.makeUnsafe("2026-07-01T09:00:00.000Z");
59 const withinGrace = DateTime.makeUnsafe("2026-07-01T09:05:00.000Z");
60 const pastGrace = DateTime.makeUnsafe("2026-07-01T15:00:00.000Z");
61 expect(isMissedFixedTimeRun(fixedTime, dueAt, withinGrace)).toBe(false);
62 expect(isMissedFixedTimeRun(fixedTime, dueAt, pastGrace)).toBe(true);
63 expect(isMissedFixedTimeRun({ type: "interval", everyMs: 60_000 }, dueAt, pastGrace)).toBe(
64 false,
65 );
66 });
67
68 it("compares schedules structurally", () => {
69 expect(
70 isSameSchedule({ type: "interval", everyMs: 60_000 }, { type: "interval", everyMs: 60_000 }),
71 ).toBe(true);
72 expect(
73 isSameSchedule({ type: "interval", everyMs: 60_000 }, { type: "interval", everyMs: 30_000 }),
74 ).toBe(false);
75 expect(
76 isSameSchedule(
77 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
78 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
79 ),
80 ).toBe(true);
81 expect(
82 isSameSchedule(
83 { type: "fixed_time", timeOfDay: "09:00", weekdays: [5, 1] },
84 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 5, 5] },
85 ),
86 ).toBe(true);
87 expect(
88 isSameSchedule(
89 { type: "fixed_time", timeOfDay: "09:00" },
90 { type: "fixed_time", timeOfDay: "09:00", weekdays: [0, 1, 2, 3, 4, 5, 6] },
91 ),
92 ).toBe(true);
93 expect(
94 isSameSchedule(
95 { type: "fixed_time", timeOfDay: "09:00", weekdays: [] },
96 { type: "fixed_time", timeOfDay: "09:00" },
97 ),
98 ).toBe(true);
99 expect(
100 isSameSchedule(
101 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
102 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 3] },
103 ),
104 ).toBe(false);
105 expect(
106 isSameSchedule(
107 { type: "fixed_time", timeOfDay: "09:00" },
108 { type: "fixed_time", timeOfDay: "09:30" },
109 ),
110 ).toBe(false);
111 expect(
112 isSameSchedule(
113 { type: "interval", everyMs: 60_000 },
114 { type: "fixed_time", timeOfDay: "09:00" },
115 ),
116 ).toBe(false);
117 });
118
119 it.each([
120 ["9:00", "09:00"],
121 ["09:00", "9:00"],
122 ["0:30", "00:30"],
123 ])("treats %s and %s as the same fixed-time schedule", (before, after) => {
124 expect(
125 isSameSchedule(
126 { type: "fixed_time", timeOfDay: before },
127 { type: "fixed_time", timeOfDay: after },
128 ),
129 ).toBe(true);
130 });
131});