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_RequestCompletedLinksToWhatArrived(t *testing.T) { require.Equal(t, "/albums/al-1", render(KindRequestCompleted, Payload{Name: "WWW", AlbumID: "al-1", ArtistID: "ar-1"}).Link) require.Equal(t, "/artists/ar-1", render(KindRequestCompleted, Payload{Name: "Moe Shop", ArtistID: "ar-1"}).Link) require.Equal(t, "/requests", render(KindRequestCompleted, Payload{Name: "WWW"}).Link) require.Equal(t, "WWW has arrived.", render(KindRequestCompleted, Payload{Name: "WWW"}).Body) } func TestRender_RejectionCarriesTheReasonWhenThereIsOne(t *testing.T) { require.Equal(t, "WWW was declined.", render(KindRequestRejected, Payload{Name: "WWW"}).Body) require.Equal(t, "WWW was declined: already owned", render(KindRequestRejected, Payload{Name: "WWW", Reason: "already owned"}).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) } // M542: 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(KindRequestApproved, []byte(`not json`)) require.Equal(t, "Request approved", r.Title) require.Equal(t, "Your request is on its way.", 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()) }