Add update/1 and subscribe/0 to ListeningStats

This commit is contained in:
Claudio Ortolina
2026-03-27 23:03:23 +00:00
parent 401e034549
commit 9643e2141c
2 changed files with 88 additions and 0 deletions
@@ -9,6 +9,55 @@ defmodule MusicLibrary.ListeningStatsTest do
alias MusicLibrary.ListeningStats
alias MusicLibrary.Records.Record
describe "update/1 and subscribe/0" do
test "stores tracks and broadcasts the updated track count" do
:ok = ListeningStats.subscribe()
track_one = %Track{
musicbrainz_id: "5689211e-9afa-3c3e-8e34-63dc0de45ef1",
title: "The Flow",
artist: %LastFm.Artist{
musicbrainz_id: "0cf0af1f-20ca-4863-9b24-5f52772f7715",
name: "Anekdoten"
},
album: %LastFm.Album{
musicbrainz_id: "08237599-8fdf-4e2b-a7c9-eb5336f60346",
title: "Vemod"
},
cover_url:
"https://lastfm.freetls.fastly.net/i/u/64s/9741e297b9884a4294624f0f90e14749.jpg",
scrobbled_at_uts: 1_731_318_211,
scrobbled_at_label: "11 Nov 2024, 09:43",
last_fm_data: %{}
}
track_two = %Track{
musicbrainz_id: "619cb295-b155-3e35-b65a-396a7cd1fc47",
title: "Wheel",
artist: %LastFm.Artist{
musicbrainz_id: "0cf0af1f-20ca-4863-9b24-5f52772f7715",
name: "Anekdoten"
},
album: %LastFm.Album{
musicbrainz_id: "08237599-8fdf-4e2b-a7c9-eb5336f60346",
title: "Vemod"
},
cover_url:
"https://lastfm.freetls.fastly.net/i/u/64s/9741e297b9884a4294624f0f90e14749.jpg",
scrobbled_at_uts: 1_731_318_945,
scrobbled_at_label: "11 Nov 2024, 09:55",
last_fm_data: %{}
}
assert {:ok, 2} == ListeningStats.update([track_two, track_one])
assert_receive %{track_count: 2}
# Tracks have already been inserted, count of new tracks is 0
assert {:ok, 0} == ListeningStats.update([track_two, track_one])
assert_receive %{track_count: 0}
end
end
describe "scrobble_count/0" do
test "returns correct count" do
initial_count = ListeningStats.scrobble_count()