package ntfy import ( "slices" "testing" "testing/synctest" "time" // Embeds the zone database, so that IANA names load on any host. _ "time/tzdata" ) func london(t *testing.T) *time.Location { t.Helper() loc, err := time.LoadLocation("Europe/London") if err != nil { t.Fatal(err) } return loc } // nightly is 22:00 to 07:00 every day in loc. func nightly(loc *time.Location) *Quiet { return &Quiet{Start: Clock{22, 0}, End: Clock{7, 0}, Days: allDays, Location: loc, MinPriority: 5} } func TestQuietWindow(t *testing.T) { loc := london(t) at := func(y int, m time.Month, d, hh, mm int) time.Time { return time.Date(y, m, d, hh, mm, 0, 0, loc) } tests := []struct { name string q *Quiet t time.Time in bool wantEnd time.Time }{ {"evening before", nightly(loc), at(2026, 11, 30, 21, 59), false, time.Time{}}, {"start", nightly(loc), at(2026, 11, 30, 22, 0), true, at(2026, 12, 1, 7, 0)}, {"after midnight", nightly(loc), at(2026, 12, 1, 3, 0), true, at(2026, 12, 1, 7, 0)}, {"end", nightly(loc), at(2026, 12, 1, 7, 0), false, time.Time{}}, {"daytime window", &Quiet{Start: Clock{13, 0}, End: Clock{14, 30}, Days: allDays, Location: loc}, at(2026, 12, 1, 14, 0), true, at(2026, 12, 1, 14, 30)}, {"daytime window over", &Quiet{Start: Clock{13, 0}, End: Clock{14, 30}, Days: allDays, Location: loc}, at(2026, 12, 1, 14, 30), false, time.Time{}}, // Friday's window runs into Saturday morning; Saturday's does not // start. {"friday night", &Quiet{Start: Clock{22, 0}, End: Clock{7, 0}, Days: []time.Weekday{time.Friday}, Location: loc}, at(2026, 12, 5, 3, 0), true, at(2026, 12, 5, 7, 0)}, {"saturday night", &Quiet{Start: Clock{22, 0}, End: Clock{7, 0}, Days: []time.Weekday{time.Friday}, Location: loc}, at(2026, 12, 5, 23, 0), false, time.Time{}}, {"no quiet hours", nil, at(2026, 12, 1, 3, 0), false, time.Time{}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { end, in := tt.q.window(tt.t) if in != tt.in || !end.Equal(tt.wantEnd) { t.Errorf("window(%s) = %s, %v; want %s, %v", tt.t, end, in, tt.wantEnd, tt.in) } }) } } // On the nights the clocks change, the window keeps its times of day: it // lasts eight hours when they go forward and ten when they go back. func TestQuietWindowAcrossClockChanges(t *testing.T) { loc := london(t) q := nightly(loc) for _, tt := range []struct { night time.Time hours time.Duration }{ {time.Date(2026, 3, 28, 23, 0, 0, 0, loc), 8}, {time.Date(2026, 10, 24, 23, 0, 0, 0, loc), 10}, } { end, in := q.window(tt.night) if !in { t.Fatalf("%s is not in the window", tt.night) } start := time.Date(tt.night.Year(), tt.night.Month(), tt.night.Day(), 22, 0, 0, 0, loc) if got := end.Sub(start); got != tt.hours*time.Hour || end.In(loc).Hour() != 7 { t.Errorf("night of %s: window %s to %s, %s long; want %dh ending 07:00", tt.night.Format("2 Jan"), start, end, got, tt.hours) } // Just after 07:00 local time is outside the window. if _, in := q.window(end.Add(time.Second)); in { t.Errorf("%s is still in the window", end.Add(time.Second)) } if _, in := q.window(end.Add(-time.Second)); !in { t.Errorf("%s is not in the window", end.Add(-time.Second)) } } } // In zones whose clocks go forward at midnight, the evening of that day // still has its window. func TestQuietWindowWhenMidnightIsSkipped(t *testing.T) { for _, tt := range []struct { zone string y int m time.Month d int }{ {"America/Santiago", 2026, time.September, 6}, {"America/Havana", 2026, time.March, 8}, } { t.Run(tt.zone, func(t *testing.T) { loc, err := time.LoadLocation(tt.zone) if err != nil { t.Fatal(err) } if mid := time.Date(tt.y, tt.m, tt.d, 0, 0, 0, 0, loc); mid.Day() == tt.d && mid.Hour() == 0 { t.Skipf("midnight of %d-%02d-%02d exists in this zone database", tt.y, tt.m, tt.d) } q := nightly(loc) evening := time.Date(tt.y, tt.m, tt.d, 23, 0, 0, 0, loc) end, in := q.window(evening) if want := time.Date(tt.y, tt.m, tt.d+1, 7, 0, 0, 0, loc); !in || !end.Equal(want) { t.Errorf("window(%s) = %s, %v; want the window ending %s", evening, end, in, want) } // The night before, which runs into the gap, is in its window too. early := time.Date(tt.y, tt.m, tt.d, 3, 0, 0, 0, loc) if _, in := q.window(early); !in { t.Errorf("%s is not in the window", early) } // A window ending at the skipped midnight ends when the clocks // show 01:00, the first instant after the gap, not at 23:00 // the evening before; one starting then starts at 01:00. toMidnight := &Quiet{Start: Clock{22, 0}, End: Clock{0, 0}, Days: allDays, Location: loc} lateEvening := time.Date(tt.y, tt.m, tt.d-1, 23, 30, 0, 0, loc) end, in = toMidnight.window(lateEvening) if want := time.Date(tt.y, tt.m, tt.d, 1, 0, 0, 0, loc); !in || !end.Equal(want) { t.Errorf("window(%s) = %s, %v; want the window ending %s", lateEvening, end, in, want) } fromMidnight := &Quiet{Start: Clock{0, 0}, End: Clock{6, 0}, Days: allDays, Location: loc} gapEnd := time.Date(tt.y, tt.m, tt.d, 1, 0, 0, 0, loc) if _, in := fromMidnight.window(gapEnd.Add(-time.Second)); in { t.Errorf("%s, before the gap ends, is in a window starting at the skipped midnight", gapEnd.Add(-time.Second)) } if _, in := fromMidnight.window(gapEnd); !in { t.Errorf("%s is not in a window starting at the skipped midnight", gapEnd) } }) } } func TestWallTime(t *testing.T) { loc := london(t) tests := []struct { y int m time.Month d int c Clock want time.Time }{ {2026, time.November, 30, Clock{22, 0}, time.Date(2026, 11, 30, 22, 0, 0, 0, loc)}, {2026, time.November, 31, Clock{7, 0}, time.Date(2026, 12, 1, 7, 0, 0, 0, loc)}, // 01:30 on the night clocks go forward does not exist in London. {2026, time.March, 29, Clock{1, 30}, time.Date(2026, 3, 29, 1, 0, 0, 0, time.UTC)}, // 01:30 on the night they go back exists twice; time.Date's // choice, the second, stands. {2026, time.October, 25, Clock{1, 30}, time.Date(2026, 10, 25, 1, 30, 0, 0, time.UTC)}, } for _, tt := range tests { if got := wallTime(tt.y, tt.m, tt.d, tt.c, loc); !got.Equal(tt.want) { t.Errorf("wallTime(%d-%02d-%02d %02d:%02d) = %s, want %s", tt.y, tt.m, tt.d, tt.c.Hour, tt.c.Minute, got, tt.want) } } } // sleepUntil advances the bubble's clock to t. func sleepUntil(t time.Time) { time.Sleep(time.Until(t)) synctest.Wait() } // During quiet hours a priority 3 message is held, a priority 5 message // prints at once, and the held one prints on an overnight digest when the // window ends, on the night the clocks go forward too. func TestQuietHoursHoldForTheMorning(t *testing.T) { synctest.Test(t, func(t *testing.T) { loc := london(t) sleepUntil(time.Date(2026, 3, 28, 21, 0, 0, 0, loc)) f := newFakeServer() h := &recorder{} o := testOptions(f, t.TempDir()) o.Quiet = nightly(loc) s, stop := start(t, o, h) defer stop() synctest.Wait() sleepUntil(time.Date(2026, 3, 28, 23, 0, 0, 0, loc)) held := f.publish(Message{Title: "backup finished", RawPriority: 3}) sleepUntil(time.Date(2026, 3, 28, 23, 30, 0, 0, loc)) urgent := f.publish(Message{Title: "grafana is DOWN", RawPriority: 5}) synctest.Wait() if got := h.printed(); !slices.Equal(got, []string{urgent.ID}) { t.Fatalf("printed %v during quiet hours, want only the urgent %v", got, urgent.ID) } if st := s.Status(); st.Held != 1 || st.Digested != 1 { t.Errorf("status %+v", st) } // 07:00 BST is 06:00 UTC. end := time.Date(2026, 3, 29, 7, 0, 0, 0, loc) sleepUntil(end.Add(-time.Second)) if len(h.digestList()) != 0 { t.Fatal("the digest printed before the window ended") } sleepUntil(end.Add(time.Second)) ds := h.digestList() if len(ds) != 1 { t.Fatalf("%d digests at the end of the window, want 1", len(ds)) } if d := ds[0]; !d.Overnight || d.Count != 1 || d.Rows[0].ID != held.ID { t.Errorf("digest %+v", d) } after := f.publish(Message{Title: "morning", RawPriority: 3}) synctest.Wait() if got := h.printed(); !slices.Equal(got, []string{urgent.ID, after.ID}) { t.Errorf("printed %v after the window", got) } }) } // With action drop, messages below the window's priority are left out. func TestQuietHoursDrop(t *testing.T) { synctest.Test(t, func(t *testing.T) { loc := london(t) sleepUntil(time.Date(2026, 11, 30, 23, 0, 0, 0, loc)) f := newFakeServer() h := &recorder{} o := testOptions(f, t.TempDir()) o.Quiet = nightly(loc) o.Quiet.Drop = true o.Quiet.MinPriority = 4 s, stop := start(t, o, h) defer stop() synctest.Wait() f.publish(Message{Title: "dropped", RawPriority: 3}) high := f.publish(Message{Title: "high", RawPriority: 4}) sleepUntil(time.Date(2026, 12, 1, 8, 0, 0, 0, loc)) if got := h.printed(); !slices.Equal(got, []string{high.ID}) { t.Errorf("printed %v, want only %v", got, high.ID) } if ds := h.digestList(); len(ds) != 0 { t.Errorf("digests %+v, want none", ds) } if st := s.Status(); st.Held != 0 || st.Digested != 0 { t.Errorf("status %+v", st) } }) } // Messages held overnight are saved with the state, so a restart during // the night still prints them in the morning. func TestQuietHoursHeldSurviveRestart(t *testing.T) { synctest.Test(t, func(t *testing.T) { loc := london(t) sleepUntil(time.Date(2026, 11, 30, 23, 0, 0, 0, loc)) dir := t.TempDir() f := newFakeServer() o := testOptions(f, dir) o.Quiet = nightly(loc) _, stop := start(t, o, &recorder{}) synctest.Wait() held := f.publish(Message{Title: "held", RawPriority: 2, SequenceID: "nightly-backup"}) synctest.Wait() stop() sleepUntil(time.Date(2026, 12, 1, 2, 0, 0, 0, loc)) h := &recorder{} s, stop := start(t, o, h) defer stop() synctest.Wait() if st := s.Status(); st.Held != 1 { t.Errorf("status %+v after the restart", st) } update := f.publish(Message{Title: "held again", RawPriority: 2, SequenceID: "nightly-backup"}) sleepUntil(time.Date(2026, 12, 1, 7, 0, 1, 0, loc)) ds := h.digestList() if len(ds) != 1 || ds[0].Count != 2 || ds[0].Rows[0].ID != held.ID || ds[0].Rows[1].ID != update.ID { t.Fatalf("digests %+v", ds) } if ds[0].Rows[0].Updated || !ds[0].Rows[1].Updated { t.Errorf("rows %+v: want the second marked as an update", ds[0].Rows) } }) }