Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Schedule.test.ts

115 lines4.0 KB

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("skips to the next matching fixed-time weekday", () => {
27 const next = nextScheduledRunAt(
28 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2, 3, 4, 5] },
29 DateTime.makeZonedUnsafe(
30 {
31 year: 2026,
32 month: 7,
33 day: 3,
34 hour: 10,
35 minute: 0,
36 second: 0,
37 millisecond: 0,
38 },
39 { timeZone: "America/Los_Angeles", adjustForTimeZone: true },
40 ),
41 );
42 const parts = next ? DateTime.toParts(next) : null;
43 expect(parts?.weekDay).toBe(1);
44 expect(parts?.hour).toBe(9);
45 expect(parts?.minute).toBe(0);
46 });
47​
48 it("skips fixed-time runs missed by more than the grace window", () => {
49 const fixedTime = { type: "fixed_time", timeOfDay: "09:00" } as const;
50 const dueAt = DateTime.makeUnsafe("2026-07-01T09:00:00.000Z");
51 const withinGrace = DateTime.makeUnsafe("2026-07-01T09:05:00.000Z");
52 const pastGrace = DateTime.makeUnsafe("2026-07-01T15:00:00.000Z");
53 // A run only slightly late (poll jitter, short sleep) still fires.
54 expect(isMissedFixedTimeRun(fixedTime, dueAt, withinGrace)).toBe(false);
55 // A run hours past its slot is skipped and rescheduled instead.
56 expect(isMissedFixedTimeRun(fixedTime, dueAt, pastGrace)).toBe(true);
57 // Interval schedules always catch up with a single run, never skip.
58 expect(isMissedFixedTimeRun({ type: "interval", everyMs: 60_000 }, dueAt, pastGrace)).toBe(
59 false,
60 );
61 });
62​
63 it("compares schedules structurally", () => {
64 expect(
65 isSameSchedule({ type: "interval", everyMs: 60_000 }, { type: "interval", everyMs: 60_000 }),
66 ).toBe(true);
67 expect(
68 isSameSchedule({ type: "interval", everyMs: 60_000 }, { type: "interval", everyMs: 30_000 }),
69 ).toBe(false);
70 expect(
71 isSameSchedule(
72 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
73 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
74 ),
75 ).toBe(true);
76 // Weekday masks are sets: order and duplicates do not change firing.
77 expect(
78 isSameSchedule(
79 { type: "fixed_time", timeOfDay: "09:00", weekdays: [5, 1] },
80 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 5, 5] },
81 ),
82 ).toBe(true);
83 // Omitted, empty, and all-seven masks all mean daily.
84 expect(
85 isSameSchedule(
86 { type: "fixed_time", timeOfDay: "09:00" },
87 { type: "fixed_time", timeOfDay: "09:00", weekdays: [0, 1, 2, 3, 4, 5, 6] },
88 ),
89 ).toBe(true);
90 expect(
91 isSameSchedule(
92 { type: "fixed_time", timeOfDay: "09:00", weekdays: [] },
93 { type: "fixed_time", timeOfDay: "09:00" },
94 ),
95 ).toBe(true);
96 expect(
97 isSameSchedule(
98 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
99 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 3] },
100 ),
101 ).toBe(false);
102 expect(
103 isSameSchedule(
104 { type: "fixed_time", timeOfDay: "09:00" },
105 { type: "fixed_time", timeOfDay: "09:30" },
106 ),
107 ).toBe(false);
108 expect(
109 isSameSchedule(
110 { type: "interval", everyMs: 60_000 },
111 { type: "fixed_time", timeOfDay: "09:00" },
112 ),
113 ).toBe(false);
114 });
115});