Open History to see the last commit
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.each([
30 { from: "2026-11-01T05:00:00.000Z", expected: "2026-11-01T05:30:00.000Z" },
31 { from: "2026-11-01T05:31:00.000Z", expected: "2026-11-08T06:30:00.000Z" },
32 { from: "2026-11-01T06:00:00.000Z", expected: "2026-11-08T06:30:00.000Z" },
33 ])("schedules a repeated fall-back time once from $from", ({ from, expected }) => {
34 const next = nextScheduledRunAt(
35 { type: "fixed_time", timeOfDay: "01:30", weekdays: [0] },
36 DateTime.makeZonedUnsafe(from, { timeZone: "America/New_York" }),
37 );
38 expect(next ? DateTime.formatIso(next) : null).toBe(expected);
39 });
40
41 it("compares schedules structurally", () => {
42 expect(
43 isSameSchedule(
44 { type: "once", runAt: "2026-11-01T03:00:00-05:00" },
45 { type: "once", runAt: "2026-11-01T08:00:00.000Z" },
46 ),
47 ).toBe(true);
48 expect(
49 isSameSchedule({ type: "interval", everyMs: 60_000 }, { type: "interval", everyMs: 60_000 }),
50 ).toBe(true);
51 expect(
52 isSameSchedule({ type: "interval", everyMs: 60_000 }, { type: "interval", everyMs: 30_000 }),
53 ).toBe(true);
54 expect(
55 isSameSchedule({ type: "interval", everyMs: 60_000 }, { type: "interval", everyMs: 120_000 }),
56 ).toBe(false);
57 expect(
58 isSameSchedule(
59 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
60 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
61 ),
62 ).toBe(true);
63 expect(
64 isSameSchedule(
65 { type: "fixed_time", timeOfDay: "09:00", weekdays: [5, 1] },
66 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 5, 5] },
67 ),
68 ).toBe(true);
69 expect(
70 isSameSchedule(
71 { type: "fixed_time", timeOfDay: "09:00" },
72 { type: "fixed_time", timeOfDay: "09:00", weekdays: [0, 1, 2, 3, 4, 5, 6] },
73 ),
74 ).toBe(true);
75 expect(
76 isSameSchedule(
77 { type: "fixed_time", timeOfDay: "09:00", weekdays: [] },
78 { type: "fixed_time", timeOfDay: "09:00" },
79 ),
80 ).toBe(true);
81 expect(
82 isSameSchedule(
83 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2] },
84 { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 3] },
85 ),
86 ).toBe(false);
87 expect(
88 isSameSchedule(
89 { type: "fixed_time", timeOfDay: "09:00" },
90 { type: "fixed_time", timeOfDay: "09:30" },
91 ),
92 ).toBe(false);
93 expect(
94 isSameSchedule(
95 { type: "interval", everyMs: 60_000 },
96 { type: "fixed_time", timeOfDay: "09:00" },
97 ),
98 ).toBe(false);
99 });
100});