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) } }