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

Schedule.test.ts

79 lines2.4 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as DateTime from "effect/DateTime";
3​
4import { isSameSchedule, nextScheduledRunAt } from "./Schedule.ts";
5​
6describe("scheduled task schedule calculation", () => {
7 it("skips to the next matching fixed-time weekday", () => {
8 const next = nextScheduledRunAt(
9 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2, 3, 4, 5] },
10 DateTime.makeZonedUnsafe(
11 {
12 year: 2026,
13 month: 7,
14 day: 3,
15 hour: 10,
16 minute: 0,
17 second: 0,
18 millisecond: 0,
19 },
20 { timeZone: "America/Los_Angeles", adjustForTimeZone: true },
21 ),
22 );
23 const parts = next ? DateTime.toParts(next) : null;
24 expect(parts?.weekDay).toBe(1);
25 expect(parts?.hour).toBe(9);
26 expect(parts?.minute).toBe(0);
27 });
28​
29 it("compares schedules structurally", () => {
30 expect(
31 isSameSchedule({ type: "interval", everyMs: 60_000 }, { type: "interval", everyMs: 60_000 }),
32 ).toBe(true);
33 expect(
34 isSameSchedule({ type: "interval", everyMs: 60_000 }, { type: "interval", everyMs: 30_000 }),
35 ).toBe(false);
36 expect(
37 isSameSchedule(
38 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
39 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
40 ),
41 ).toBe(true);
42 expect(
43 isSameSchedule(
44 { type: "fixed_time", timeOfDay: "09:00", weekdays: [5, 1] },
45 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 5, 5] },
46 ),
47 ).toBe(true);
48 expect(
49 isSameSchedule(
50 { type: "fixed_time", timeOfDay: "09:00" },
51 { type: "fixed_time", timeOfDay: "09:00", weekdays: [0, 1, 2, 3, 4, 5, 6] },
52 ),
53 ).toBe(true);
54 expect(
55 isSameSchedule(
56 { type: "fixed_time", timeOfDay: "09:00", weekdays: [] },
57 { type: "fixed_time", timeOfDay: "09:00" },
58 ),
59 ).toBe(true);
60 expect(
61 isSameSchedule(
62 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
63 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 3] },
64 ),
65 ).toBe(false);
66 expect(
67 isSameSchedule(
68 { type: "fixed_time", timeOfDay: "09:00" },
69 { type: "fixed_time", timeOfDay: "09:30" },
70 ),
71 ).toBe(false);
72 expect(
73 isSameSchedule(
74 { type: "interval", everyMs: 60_000 },
75 { type: "fixed_time", timeOfDay: "09:00" },
76 ),
77 ).toBe(false);
78 });
79});