Files
57_WolveandClaude Opus 5.5 64a72903d8
Security / Vulnerability check (push) Successful in 47s
Test / Lint and test (push) Successful in 2m6s
Give each ntfy digest a reference of its own
Every digest went to the print queue under the fixed reference
"digest", so after a crash the queue could not tell a digest it had
queued from a new one. Once queued jobs survive a restart, a crash in
the second between queueing a digest and saving the subscription's
state would print it twice; the same window exists today for a digest
that printed in that second.

A digest now gets a reference of its own when it is made, "digest_"
and a TypeID, and is saved with it in the subscription's state, apart
from the held messages. It is offered to the queue only once that save
has succeeded, and dropped once the queue takes it; while the state
file cannot be written, digests wait. A restart with a saved digest
offers that digest again first, with the same rows and reference, and
the queue's duplicate check answers with the earlier job while it holds
it. Messages held after a digest is made wait for the next one, also
while quiet hours delay the saved digest after a restart: added to a
digest the queue already holds, they would never print. The digest is
submitted as unique, like a card.

A digest can cost one more state save, two fsyncs. The reference shows
in the log lines and Sentry reports where "digest" did.

This completes stage 1c of docs/plans/job-store.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 13:50:57 -06:00

94 lines
2.9 KiB
Go

package ntfy
import (
"fmt"
"strings"
"testing"
"time"
)
// The budget allows limit receipts in any window, not limit per refill: a
// sixth receipt waits until the first is a whole window old.
func TestBudget(t *testing.T) {
t0 := time.Date(2026, 11, 30, 21, 0, 0, 0, time.UTC)
b := budget{limit: 5, window: 10 * time.Minute}
for i := range 5 {
now := t0.Add(time.Duration(i) * time.Minute)
if !b.room(now) {
t.Fatalf("no room for receipt %d", i+1)
}
b.spend(now)
}
at := t0.Add(5 * time.Minute)
if b.room(at) {
t.Fatal("room for a sixth receipt within the window")
}
if next := b.next(at); !next.Equal(t0.Add(10 * time.Minute)) {
t.Errorf("next = %s, want when the first receipt is 10 minutes old", next)
}
if !b.room(t0.Add(10 * time.Minute)) {
t.Error("no room once the first receipt left the window")
}
// A receipt dated in the future, as after the clock went back, counts
// as printed now rather than blocking for longer than a window.
b = budget{limit: 1, window: time.Minute, prints: []time.Time{t0.Add(time.Hour)}}
if b.room(t0) {
t.Error("room with a receipt just counted")
}
if !b.room(t0.Add(time.Minute)) {
t.Error("a receipt dated in the future blocked for more than a window")
}
}
func TestRing(t *testing.T) {
var items []string
for i := range ringSize + 10 {
items = append(items, fmt.Sprint(i))
}
r := newRing(items)
if r.has("9") || !r.has("10") || !r.has(fmt.Sprint(ringSize+9)) {
t.Error("the ring kept the wrong items")
}
if got := r.list(); len(got) != ringSize || got[0] != "10" {
t.Errorf("list has %d items from %q", len(got), got[0])
}
// An item added twice stays until both copies are forgotten.
r = newRing([]string{"a", "a"})
for i := range ringSize - 1 {
r.add(fmt.Sprint(i))
}
if !r.has("a") {
t.Error("forgot a while one copy is left")
}
r.add("x")
if r.has("a") {
t.Error("remembers a after both copies left")
}
}
func TestHeldRows(t *testing.T) {
now := time.Date(2026, 11, 30, 21, 5, 0, 0, time.UTC)
var h held
h.add(Message{ID: "a", Time: now.Unix(), Title: strings.Repeat("é", 200), Tags: []string{"warning"}, RawPriority: 4}, false, false, now)
h.add(Message{ID: "b", Message: "\n \nfirst line\nsecond"}, true, true, now.Add(time.Minute))
if h.Count != 2 || !h.Overnight || h.Since != now.Unix() {
t.Errorf("held %+v", h)
}
if row := h.Rows[0]; len(row.Title) > maxRowText || !strings.HasPrefix(row.Title, "é") || row.FirstLine != "" || row.Priority != 4 {
t.Errorf("row %+v", row)
}
if row := h.Rows[1]; row.FirstLine != "first line" || !row.Updated || row.Priority != 3 {
t.Errorf("row %+v", row)
}
made := h.makeDigest(now.Add(9 * time.Minute))
d := made.digest()
if d.Count != 2 || !d.Since.Equal(now) || !d.Until.Equal(now.Add(9*time.Minute)) || len(d.Rows) != 2 ||
!strings.HasPrefix(d.Ref, "digest_") {
t.Errorf("digest %+v", d)
}
if other := h.makeDigest(now.Add(9 * time.Minute)); other.Ref == made.Ref {
t.Error("two digests got the same reference")
}
}