/ opencodehub / tests / unit / digest-scheduler.test.ts
swadhinbiswas Harden notification digests with timezone settings and cron tests · 2/18/2026
8424830
75 lines · 3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { describe, expect, it } from "vitest";
import { shouldSendDigestNow } from "@/lib/chat-notifications";

describe("digest scheduler logic", () => {
  it("sends daily digest when due time passed and never sent", () => {
    const now = new Date("2026-02-18T10:00:00.000Z");
    const shouldSend = shouldSendDigestNow("daily", "09:00", 1, null, now);
    expect(shouldSend).toBe(true);
  });

  it("does not send daily digest before due time", () => {
    const now = new Date("2026-02-18T08:59:00.000Z");
    const shouldSend = shouldSendDigestNow("daily", "09:00", 1, null, now);
    expect(shouldSend).toBe(false);
  });

  it("does not re-send daily digest on same UTC day", () => {
    const now = new Date("2026-02-18T12:00:00.000Z");
    const lastSentAt = new Date("2026-02-18T09:10:00.000Z");
    const shouldSend = shouldSendDigestNow("daily", "09:00", 1, lastSentAt, now);
    expect(shouldSend).toBe(false);
  });

  it("sends weekly digest on configured day after due time", () => {
    // Monday
    const now = new Date("2026-02-16T10:00:00.000Z");
    const shouldSend = shouldSendDigestNow("weekly", "09:00", 1, null, now);
    expect(shouldSend).toBe(true);
  });

  it("does not send weekly digest on non-configured day", () => {
    // Tuesday
    const now = new Date("2026-02-17T10:00:00.000Z");
    const shouldSend = shouldSendDigestNow("weekly", "09:00", 1, null, now);
    expect(shouldSend).toBe(false);
  });

  it("does not re-send weekly digest in same week", () => {
    // Monday this week
    const now = new Date("2026-02-16T12:00:00.000Z");
    // Also Monday this week
    const lastSentAt = new Date("2026-02-16T09:15:00.000Z");
    const shouldSend = shouldSendDigestNow("weekly", "09:00", 1, lastSentAt, now);
    expect(shouldSend).toBe(false);
  });

  it("uses timezone for daily due-time checks", () => {
    // 08:30 in America/New_York (UTC-5 in February)
    const beforeDue = new Date("2026-02-18T13:30:00.000Z");
    const shouldSendBeforeDue = shouldSendDigestNow("daily", "09:00", 1, null, beforeDue, "America/New_York");
    expect(shouldSendBeforeDue).toBe(false);

    // 09:30 in America/New_York
    const afterDue = new Date("2026-02-18T14:30:00.000Z");
    const shouldSendAfterDue = shouldSendDigestNow("daily", "09:00", 1, null, afterDue, "America/New_York");
    expect(shouldSendAfterDue).toBe(true);
  });

  it("normalizes invalid weekly day to Monday", () => {
    // Monday UTC
    const now = new Date("2026-02-16T10:00:00.000Z");
    const shouldSend = shouldSendDigestNow("weekly", "09:00", 99, null, now);
    expect(shouldSend).toBe(true);
  });

  it("does not re-send daily digest on same local date in timezone", () => {
    // 00:30 JST on Feb 18, 2026
    const now = new Date("2026-02-17T15:30:00.000Z");
    // 00:05 JST on the same local date
    const lastSentAt = new Date("2026-02-17T15:05:00.000Z");
    const shouldSend = shouldSendDigestNow("daily", "00:00", 1, lastSentAt, now, "Asia/Tokyo");
    expect(shouldSend).toBe(false);
  });
});