Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { resolveSnoozePresets, snoozeWakeDescription, snoozeWakeLabel } from "./Sidebar.snooze";
4
5// Local-time constructor so preset math is timezone-stable in tests.
6function localDate(year: number, month: number, day: number, hour: number, minute = 0): Date {
7 return new Date(year, month - 1, day, hour, minute, 0, 0);
8}
9
10describe("resolveSnoozePresets", () => {
11 it("offers hour, evening, tomorrow, next week in the morning", () => {
12 // Wednesday 2026-04-08 10:00 local.
13 const presets = resolveSnoozePresets(localDate(2026, 4, 8, 10));
14 expect(presets.map((preset) => preset.id)).toEqual([
15 "hour",
16 "evening",
17 "tomorrow",
18 "next-week",
19 ]);
20 const evening = presets.find((preset) => preset.id === "evening");
21 expect(new Date(evening!.snoozedUntil).getHours()).toBe(18);
22 const tomorrow = presets.find((preset) => preset.id === "tomorrow");
23 const tomorrowDate = new Date(tomorrow!.snoozedUntil);
24 expect(tomorrowDate.getDate()).toBe(9);
25 expect(tomorrowDate.getHours()).toBe(9);
26 const nextWeek = presets.find((preset) => preset.id === "next-week");
27 const nextWeekDate = new Date(nextWeek!.snoozedUntil);
28 expect(nextWeekDate.getDay()).toBe(1);
29 expect(nextWeekDate.getDate()).toBe(13);
30 });
31
32 it("whenLabel complements the label instead of repeating it", () => {
33 const presets = resolveSnoozePresets(localDate(2026, 4, 8, 10));
34 for (const preset of presets) {
35 // Day words live in the label column; the time column is time-only
36 // (plus a weekday for next week, which names a different day).
37 expect(preset.whenLabel.toLowerCase()).not.toContain("tomorrow");
38 }
39 const tomorrow = presets.find((preset) => preset.id === "tomorrow");
40 expect(tomorrow!.whenLabel).toMatch(/9/);
41 const nextWeek = presets.find((preset) => preset.id === "next-week");
42 expect(nextWeek!.whenLabel).toMatch(/Mon/);
43 });
44
45 it("drops the evening preset once evening is near or past", () => {
46 expect(resolveSnoozePresets(localDate(2026, 4, 8, 17, 30)).map((preset) => preset.id)).toEqual([
47 "hour",
48 "tomorrow",
49 "next-week",
50 ]);
51 expect(resolveSnoozePresets(localDate(2026, 4, 8, 21)).map((preset) => preset.id)).toEqual([
52 "hour",
53 "tomorrow",
54 "next-week",
55 ]);
56 });
57
58 it("puts next week a full week out when today is Monday", () => {
59 // Monday 2026-04-06.
60 const presets = resolveSnoozePresets(localDate(2026, 4, 6, 10));
61 const nextWeek = new Date(presets.find((preset) => preset.id === "next-week")!.snoozedUntil);
62 expect(nextWeek.getDay()).toBe(1);
63 expect(nextWeek.getDate()).toBe(13);
64 });
65});
66
67describe("snoozeWakeLabel", () => {
68 const now = localDate(2026, 4, 8, 10);
69
70 it("formats minutes, hours, and days, rounding up", () => {
71 expect(snoozeWakeLabel(new Date(now.getTime() + 30 * 60_000).toISOString(), now)).toBe("30m");
72 expect(snoozeWakeLabel(new Date(now.getTime() + 90 * 60_000).toISOString(), now)).toBe("2h");
73 expect(snoozeWakeLabel(new Date(now.getTime() + 26 * 3_600_000).toISOString(), now)).toBe("2d");
74 });
75
76 it("reports now for past and malformed wake times", () => {
77 expect(snoozeWakeLabel(new Date(now.getTime() - 1000).toISOString(), now)).toBe("now");
78 expect(snoozeWakeLabel("not-a-date", now)).toBe("now");
79 });
80});
81
82describe("snoozeWakeDescription", () => {
83 const now = localDate(2026, 4, 8, 10);
84
85 it("uses bare time today, 'tomorrow' next day, weekday within the week", () => {
86 expect(snoozeWakeDescription(localDate(2026, 4, 8, 18).toISOString(), now)).not.toContain(
87 "tomorrow",
88 );
89 expect(snoozeWakeDescription(localDate(2026, 4, 9, 9).toISOString(), now)).toContain(
90 "tomorrow",
91 );
92 expect(snoozeWakeDescription(localDate(2026, 4, 13, 9).toISOString(), now)).toMatch(/Mon/);
93 });
94});