diff --git a/test/music_library_web/live/collection_live/index_test.exs b/test/music_library_web/live/collection_live/index_test.exs index 3b3dda3b..3b4702e8 100644 --- a/test/music_library_web/live/collection_live/index_test.exs +++ b/test/music_library_web/live/collection_live/index_test.exs @@ -3,10 +3,11 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do import MusicLibrary.Fixtures.Records import MusicBrainz.Fixtures.ReleaseGroup + import MusicBrainz.Fixtures.Release import MusicLibraryWeb.RecordComponents, only: [format_label: 1, type_label: 1] import Mox + alias MusicBrainz.{APIMock, ReleaseSearchResult} alias MusicLibrary.Records.{Cover, Record} - alias MusicBrainz.APIMock setup :verify_on_exit! @@ -354,4 +355,85 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do |> assert_has("video#camera-preview") end end + + test "it adds a record after scanning", %{conn: conn} do + barcode = "5037300650128" + releases = releases(:marbles) + + expect(APIMock, :search_release_by_barcode, fn ^barcode, _config -> + {:ok, Enum.map(releases, &ReleaseSearchResult.from_api_response/1)} + end) + + release = release(:marbles) + release_id = release_id(:marbles) + + expect(APIMock, :get_release, fn ^release_id, _config -> + {:ok, release} + end) + + release_group = release_group(:marbles) + release_group_id = release_group["id"] + + expect(APIMock, :get_release_group, fn ^release_group_id, _config -> + {:ok, release_group} + end) + + expect(APIMock, :get_releases, fn ^release_group_id, _opts, _config -> + {:ok, %{"releases" => release_group["releases"]}} + end) + + cover_data = File.read!(marbles_cover_fixture()) + + expect(APIMock, :get_cover_art, fn {:musicbrainz_id, ^release_group_id}, _config -> + {:ok, cover_data} + end) + + conn + |> visit(~p"/collection/scan") + |> unwrap(fn view -> + view + |> element("#barcode-scanner") + |> render_hook("barcode_scanned", %{"number" => barcode}) + end) + |> assert_has("h2", text: "Marbles") + |> click_button("Import releases") + + [record] = MusicLibrary.Repo.all(MusicLibrary.Records.Record) + + assert record.musicbrainz_id == release_group_id + assert record.title == "Marbles" + assert record.release == "2004-05-03" + assert record.format == :cd + assert record.musicbrainz_data == release_group + + assert record.genres == [ + "alternative rock", + "art rock", + "baroque pop", + "pop rock", + "progressive rock", + "psychedelic pop", + "rock" + ] + + assert record.cover_hash == + "0ED79C93C5BECC7B28FE05CAA3E49B924A3377EA3219CA8FFAE3B2B0960F2AC8" + + {:ok, resized_cover_data} = Cover.resize(cover_data) + + assert record.cover_data == resized_cover_data + + assert record.inserted_at !== nil + assert record.updated_at !== nil + assert record.purchased_at !== nil + + [marillion] = record.artists + + assert %MusicLibrary.Records.Artist{ + name: "Marillion", + sort_name: "Marillion", + disambiguation: "British progressive rock band", + musicbrainz_id: "1932f5b6-0b7b-4050-b1df-833ca89e5f44" + } = marillion + end end diff --git a/test/support/fixtures/music_brainz/release.ex b/test/support/fixtures/music_brainz/release.ex index 55a2b05b..c67cbe93 100644 --- a/test/support/fixtures/music_brainz/release.ex +++ b/test/support/fixtures/music_brainz/release.ex @@ -8,6 +8,13 @@ defmodule MusicBrainz.Fixtures.Release do |> Map.get("releases") end + def releases(:marbles) do + Path.join([@fixtures_folder, "releases - marillion - marbles.json"]) + |> File.read!() + |> Jason.decode!() + |> Map.get("releases") + end + def release(:mystery_of_time) do Path.join([@fixtures_folder, "release - avantasia - the mystery of time.json"]) |> File.read!() diff --git a/test/support/fixtures/music_brainz/releases - marillion - marbles.json b/test/support/fixtures/music_brainz/releases - marillion - marbles.json new file mode 100644 index 00000000..15893a2c --- /dev/null +++ b/test/support/fixtures/music_brainz/releases - marillion - marbles.json @@ -0,0 +1,73 @@ +{ + "created": "2025-02-18T19:48:53.894Z", + "count": 1, + "offset": 0, + "releases": [ + { + "id": "d3f9b9e2-73f5-4b47-a2a7-2c2199aad608", + "score": 100, + "status-id": "4e304316-386d-3409-af2e-78857eec5cfe", + "packaging-id": "ec27701a-4a22-37f4-bfac-6616e0f9750a", + "count": 1, + "title": "Marbles", + "status": "Official", + "packaging": "Jewel Case", + "text-representation": { + "language": "eng", + "script": "Latn" + }, + "artist-credit": [ + { + "name": "Marillion", + "artist": { + "id": "1932f5b6-0b7b-4050-b1df-833ca89e5f44", + "name": "Marillion", + "sort-name": "Marillion", + "disambiguation": "British progressive rock band" + } + } + ], + "release-group": { + "id": "20790e26-98e4-3ad3-a67f-b674758b942d", + "type-id": "f529b476-6e62-324f-b0aa-1f3e33d313fc", + "primary-type-id": "f529b476-6e62-324f-b0aa-1f3e33d313fc", + "title": "Marbles", + "primary-type": "Album" + }, + "date": "2004-05-03", + "country": "GB", + "release-events": [ + { + "date": "2004-05-03", + "area": { + "id": "8a754a16-0027-3a29-b6d7-2b40ea0481ed", + "name": "United Kingdom", + "sort-name": "United Kingdom", + "iso-3166-1-codes": [ + "GB" + ] + } + } + ], + "barcode": "5037300650128", + "asin": "B0002Y4TM4", + "label-info": [ + { + "catalog-number": "INTACTCD1", + "label": { + "id": "983e8198-6c5a-4353-8ac1-3d41a9336481", + "name": "Intact Records" + } + } + ], + "track-count": 12, + "media": [ + { + "format": "CD", + "disc-count": 2, + "track-count": 12 + } + ] + } + ] +}