[[ntfy.subscriptions]] tables make the daemon subscribe to ntfy topics itself and print each message as a notification card, so the phone's ntfy app is the only client. Each subscription is one ntfy.Subscriber in the daemon's errgroup: one event loop owns its state, and one goroutine per connection reads the JSON stream. internal/app hands its messages to modules.FromNtfy, Card, Validate and Submit with the source ntfy:<name> and the message's ID as ref, so ntfy never imports modules. The cursor is ntfy server time, never an ID. ntfy sends open before the replay, so only keepalives and messages dealt with move it, and a reconnect asks for two minutes before it, since a message is dated before its attachment is stored. The IDs of the messages a replay could send again are kept, at least the last 512 and 20,000 at most, so a flood in the overlap never reprints; when the cap forgets IDs a replay could send, the floor rises past them. A saved floor bounds replays: a first start sets it to the open event's time, and max_backlog (default 1h, at most 12h), measured by the server's clock as each connection tells it (its open event, else its Date, then the monotonic clock), never the host's, so replays are requested unclamped and filtered as they arrive, raises it when it clamps, so what it left out stays out. The state, with the held messages and the receipt times, is written atomically at most once a second and at shutdown. Redirects are never followed, so credentials never go to another address or over plain http. A stalled stream reconnects after twice the keepalive plus 15 s, failures back off 5 to 120 s with jitter, a 429 waits for Retry-After, and refused credentials, checked at startup through /auth, show as unauthorized and retry every 5 minutes. A full print queue pauses reading and retries the message from 1 s to 30 s, and the cursor moves past it only once it is queued. The paper budget is a strict window: at most rate_limit receipts (5) in any rate_window (10m), digests included; the rest are held and print on one digest card once there is room, and outside quiet hours bypass_priority (5) and above print at once. Quiet hours hold everything below their min_priority for one Overnight digest when the window ends, or drop it; windows cross midnight and keep their times of day when clocks change, also where the change skips midnight or a window's end. A message whose sequence printed before, on a card or a digest row, is marked (updated), or skipped with ntfy.format.updates = "skip"; the first of a sequence its publisher named prints plain. /healthz lists the subscriptions without changing how its status code is decided, and no credential, topic or message text reaches it, the logs or a config error. Subscriptions come from the file only, with strict keys and per-entry defaults; credentials are optional, at most one kind. ntfy.state_dir is required with a subscription: the unit gains StateDirectory=uberbringer, install.sh writes the path into a new config, and the Docker image sets it for a volume and runs as the fixed user 10001. README explains how to lock a self-hosted ntfy server down so that only your publishers can print. Breaking: printer.cut_feed (default 3) feeds blank lines before every cut, so the last line no longer sits right at the cut. They count toward format.max_lines once per copy, so a job within 3 lines a copy of the limit now fails. The renderer's option is 0 unless set, so the pinned receipts keep their bytes. testing/synctest drives the subscriber on a virtual clock, including a 24-hour soak with floods, repeats, refusals, a full queue, quiet hours and a restart that fails on any leaked goroutine; it replaces a 24-hour run on hardware. Tests over real sockets cover net/http's goroutines and redirects to plain http and to another port. Checked on the TM-T88IV against a live ntfy server: a phone message, quiet hours with priorities 3 and 5 and the Overnight digest, a stale cursor, a 30-minute outage with an update, a wrong token, and the three-line cut feed. The digest and the cut-feed card are pinned in TestPaperReceipts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
287 lines
10 KiB
Go
287 lines
10 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|