Files
uberbringer/internal/pkg/ntfy/message_test.go
T
57_WolveandClaude Opus 5.5 792ee26af5 Store the ntfy notification a card came from
A card from an ntfy subscription now carries its notification,
"<topic>/<sequence ID>", in its Meta, and the queue stores it with the
job. ntfy's sequence IDs are per topic and one subscription reads
several topics, so the topic is part of the key; a message without a
sequence_id is its own sequence. Digests and jobs from the routes carry
none.

Nothing reads it yet: it lets an ntfy message_delete find the waiting
job of its notification, also after a restart, once cancelling comes.

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

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

68 lines
3.1 KiB
Go

package ntfy_test
import (
"encoding/json"
"testing"
"uberbringer/internal/pkg/ntfy"
)
func TestPriority(t *testing.T) {
for raw, want := range map[int]int{0: 3, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 3, -1: 3} {
if got := (ntfy.Message{RawPriority: raw}).Priority(); got != want {
t.Errorf("priority %d: Priority() = %d, want %d", raw, got, want)
}
}
}
// A message as ntfy sends it decodes into every field, and unknown fields
// are ignored.
func TestDecodeMessage(t *testing.T) {
raw := `{"id":"sPs71M8A2Tq9","sequence_id":"seq1","time":1764450245,"expires":1764493445,"event":"message",
"topic":"alerts","title":"Disk almost full","message":"**/var** is at *94%*","priority":4,"tags":["warning","nas01"],
"click":"https://grafana.lan/d/disk","icon":"https://example.com/i.png","content_type":"text/markdown","encoding":"",
"poll_id":"p1","future":true,
"actions":[{"id":"a1","action":"view","label":"Open","url":"https://grafana.lan/d/disk","clear":true},
{"id":"a2","action":"http","label":"Close","url":"https://h/","method":"PUT","headers":{"X":"1"},"body":"b"},
{"id":"a3","action":"broadcast","label":"B","intent":"i","extras":{"k":"v"}},
{"id":"a4","action":"copy","label":"C","value":"123456"}],
"attachment":{"name":"graph.png","type":"image/png","size":33848,"expires":1764461045,"url":"https://ntfy.example.com/f.png"}}`
var m ntfy.Message
if err := json.Unmarshal([]byte(raw), &m); err != nil {
t.Fatal(err)
}
if m.ID != "sPs71M8A2Tq9" || m.SequenceID != "seq1" || m.Time != 1764450245 || m.Expires != 1764493445 ||
m.Event != ntfy.EventMessage || m.Topic != "alerts" || m.Title != "Disk almost full" ||
m.Message != "**/var** is at *94%*" || m.Priority() != ntfy.PriorityHigh || len(m.Tags) != 2 ||
m.Click != "https://grafana.lan/d/disk" || m.Icon == "" || m.ContentType != ntfy.ContentTypeMarkdown || m.PollID != "p1" {
t.Errorf("message %+v", m)
}
if len(m.Actions) != 4 || !m.Actions[0].Clear || m.Actions[1].Method != "PUT" || m.Actions[1].Headers["X"] != "1" ||
m.Actions[1].Body != "b" || m.Actions[2].Intent != "i" || m.Actions[2].Extras["k"] != "v" ||
m.Actions[3].Action != ntfy.ActionCopy || m.Actions[3].Value != "123456" {
t.Errorf("actions %+v", m.Actions)
}
if a := m.Attachment; a == nil || a.Name != "graph.png" || a.Type != "image/png" || a.Size != 33848 ||
a.Expires != 1764461045 || a.URL != "https://ntfy.example.com/f.png" {
t.Errorf("attachment %+v", m.Attachment)
}
}
// Notification joins the topic and the sequence, which falls back to the
// ID, and is empty for an event with neither.
func TestNotification(t *testing.T) {
for _, tt := range []struct {
m ntfy.Message
want string
}{
{ntfy.Message{ID: "m1", SequenceID: "backup", Topic: "alerts"}, "alerts/backup"},
{ntfy.Message{ID: "m1", Topic: "alerts"}, "alerts/m1"},
{ntfy.Message{Event: ntfy.EventMessageDelete, SequenceID: "backup", Topic: "jobs"}, "jobs/backup"},
{ntfy.Message{Event: ntfy.EventKeepalive, Topic: "alerts"}, ""},
} {
if got := tt.m.Notification(); got != tt.want {
t.Errorf("%+v: Notification() = %q, want %q", tt.m, got, tt.want)
}
}
}