From 24a3cc9265785a94e1ce81f47d678cd35791d4a6 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 13 Apr 2026 10:08:01 +0100 Subject: [PATCH] Add Collection.collection_summary/0 --- lib/music_library/collection.ex | 28 ++++++++++ test/music_library/collection_test.exs | 74 ++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/lib/music_library/collection.ex b/lib/music_library/collection.ex index 08ac6fb7..fa48763c 100644 --- a/lib/music_library/collection.ex +++ b/lib/music_library/collection.ex @@ -191,6 +191,34 @@ defmodule MusicLibrary.Collection do |> MapSet.new() end + @spec collection_summary() :: String.t() + def collection_summary do + records = + from(r in Record, + where: not is_nil(r.purchased_at), + order_by: [order_alphabetically()], + select: ^Records.essential_fields() + ) + |> Repo.all() + + records + |> Enum.map_join("\n", &format_record_line/1) + end + + defp format_record_line(record) do + artist_names = Record.artist_names(record) + genres = record.genres || [] + + base = + "#{artist_names} - #{record.title} (#{record.release_date || "Unknown"}, #{record.format}, #{record.type})" + + if genres == [] do + base + else + base <> " [#{Enum.join(genres, ", ")}]" + end + end + defp base_search do from r in SearchIndex, where: not is_nil(r.purchased_at) diff --git a/test/music_library/collection_test.exs b/test/music_library/collection_test.exs index 7599d5d0..5eb12aaf 100644 --- a/test/music_library/collection_test.exs +++ b/test/music_library/collection_test.exs @@ -296,4 +296,78 @@ defmodule MusicLibrary.CollectionTest do assert random_record.id in collection_ids end end + + describe "collection_summary/0" do + test "returns empty string when collection is empty" do + assert Collection.collection_summary() == "" + end + + test "returns one line per collected record" do + record_with_artist("Radiohead", %{ + title: "OK Computer", + format: :cd, + type: :album, + genres: ["alternative rock", "art rock"], + release_date: "1997-06-16", + purchased_at: ~U[2024-01-01 00:00:00Z] + }) + + record_with_artist("Pink Floyd", %{ + title: "The Dark Side of the Moon", + format: :vinyl, + type: :album, + genres: ["progressive rock"], + release_date: "1973-03-01", + purchased_at: ~U[2024-01-02 00:00:00Z] + }) + + summary = Collection.collection_summary() + lines = String.split(summary, "\n") + + assert length(lines) == 2 + + assert Enum.any?(lines, fn line -> + line =~ "Radiohead" and line =~ "OK Computer" and + line =~ "1997-06-16" and line =~ "cd" and line =~ "album" and + line =~ "alternative rock, art rock" + end) + + assert Enum.any?(lines, fn line -> + line =~ "Pink Floyd" and line =~ "The Dark Side of the Moon" and + line =~ "1973-03-01" and line =~ "vinyl" and line =~ "album" and + line =~ "progressive rock" + end) + end + + test "omits genre brackets when genres are empty" do + record_with_artist("Radiohead", %{ + title: "OK Computer", + format: :cd, + type: :album, + genres: [], + release_date: "1997-06-16", + purchased_at: ~U[2024-01-01 00:00:00Z] + }) + + summary = Collection.collection_summary() + refute summary =~ "[" + refute summary =~ "]" + end + + test "excludes wishlist records" do + record_with_artist("Radiohead", %{ + title: "OK Computer", + purchased_at: ~U[2024-01-01 00:00:00Z] + }) + + record_with_artist("Pink Floyd", %{ + title: "Wish You Were Here", + purchased_at: nil + }) + + summary = Collection.collection_summary() + assert summary =~ "OK Computer" + refute summary =~ "Wish You Were Here" + end + end end