package notifications import ( "encoding/json" "testing" "github.com/stretchr/testify/require" ) // render stores p the way Notify does (through Map) and reads it back. func render(k Kind, p Payload) Rendered { b, err := json.Marshal(p.Map()) if err != nil { panic(err) } return Render(k, b) } func TestRender_EveryKindHasATitleAndALink(t *testing.T) { for _, k := range Kinds() { r := Render(k, []byte(`{}`)) require.NotEmpty(t, r.Title, k) require.NotEqual(t, "Notification", r.Title, "%s falls through to the default", k) require.NotEmpty(t, r.Link, k) } } func TestRender_NewMusicOpensWhatArrived(t *testing.T) { one := render(KindNewMusic, Payload{Count: 1, Albums: []Arrival{{Artist: "Moe Shop", Title: "WWW", AlbumID: "al-1"}}}) require.Equal(t, "New album", one.Title) require.Equal(t, "Moe Shop – WWW is in the library.", one.Body) require.Equal(t, "/albums/al-1", one.Link) // Several open Home, whose Recently added row starts with them; the // count, not only the albums carried, is what "more" counts. many := render(KindNewMusic, Payload{Count: 60, Albums: []Arrival{ {Artist: "A", Title: "1"}, {Artist: "B", Title: "2"}, {Artist: "C", Title: "3"}, {Artist: "D", Title: "4"}, }}) require.Equal(t, "60 new albums", many.Title) require.Equal(t, "Now in the library: A – 1, B – 2, C – 3 and 57 more.", many.Body) require.Equal(t, "/", many.Link) } func TestRender_FlagCarriesTheReasonWhenThereIsOne(t *testing.T) { require.Equal(t, "Ann flagged WWW.", render(KindQuarantineFlagged, Payload{Name: "WWW", Actor: "Ann"}).Body) require.Equal(t, "Ann flagged WWW: bad rip", render(KindQuarantineFlagged, Payload{Name: "WWW", Actor: "Ann", Reason: "bad rip"}).Body) } func TestRender_CoalescedCountsReadNaturally(t *testing.T) { require.Equal(t, "1 track went missing", render(KindTracksMissing, Payload{Count: 1}).Title) require.Equal(t, "14 tracks went missing", render(KindTracksMissing, Payload{Count: 14}).Title) require.Equal(t, "Library scan failed", render(KindScanFailed, Payload{Count: 1, Detail: "x"}).Title) require.Equal(t, "Library scan failed 3 times", render(KindScanFailed, Payload{Count: 3}).Title) require.Equal(t, "Some tracks went missing", Render(KindTracksMissing, []byte(`{}`)).Title) } // A notice says which, not only how many. func TestRender_NamesWhatChanged(t *testing.T) { require.Equal(t, "No longer on disk: Weezer – Buddy Holly.", render(KindTracksMissing, Payload{Count: 1, Items: []string{"Weezer – Buddy Holly"}}).Body) require.Equal(t, "No longer on disk: A – 1 and B – 2.", render(KindTracksMissing, Payload{Count: 2, Items: []string{"A – 1", "B – 2"}}).Body) // Three are named; the rest of the count, not only the rest of the // stored names, is "more". require.Equal(t, "Held more than once: A, B, C and 9 more.", render(KindDuplicatesFound, Payload{Count: 12, Items: []string{"A", "B", "C", "D", "E"}}).Body) require.Equal(t, "12 new duplicates to review", render(KindDuplicatesFound, Payload{Count: 12}).Title) require.Equal(t, "Extra copies removed, the right one kept: A and B.", render(KindDuplicatesResolved, Payload{Count: 2, Items: []string{"A", "B"}}).Body) require.Equal(t, "Failed to play: Weezer – Buddy Holly (alice).", render(KindPlaybackErrors, Payload{Count: 1, Items: []string{"Weezer – Buddy Holly (alice)"}}).Body) } func TestRender_RowsFromBeforeItemsReadAsTheyDid(t *testing.T) { require.Equal(t, "Their files are no longer where the library expects them.", render(KindTracksMissing, Payload{Count: 4}).Body) require.Equal(t, "Merged 8 duplicate copies.", render(KindDuplicatesResolved, Payload{Count: 716, Detail: "Merged 8 duplicate copies."}).Body) } func TestRender_AScanFailureSaysWhatStopped(t *testing.T) { r := render(KindScanFailed, Payload{Count: 1, Detail: "scan root \"/music\" unavailable"}) require.Equal(t, `The last scan stopped: scan root "/music" unavailable`, r.Body) require.Equal(t, "/admin/library", r.Link) } func TestRender_AnUnreadablePayloadStillRenders(t *testing.T) { r := Render(KindQuarantineFlagged, []byte(`not json`)) require.Equal(t, "Track flagged", r.Title) require.Equal(t, "Someone flagged A track.", r.Body) } func TestPayloadMap_DropsEmptyFields(t *testing.T) { require.Equal(t, map[string]any{"name": "WWW", "count": float64(2)}, Payload{Name: "WWW", Count: 2}.Map()) }