/
/
/
1"""Tests for external media identifier helpers."""
2
3from music_assistant_models.enums import ExternalID
4
5from music_assistant.helpers.external_ids import (
6 barcode_to_upc,
7 external_id_lookup_values,
8 external_id_lookup_values_untyped,
9 is_valid_barcode,
10 normalize_external_id,
11 normalize_external_ids,
12)
13
14
15def test_normalize_gtin_variants() -> None:
16 """Equivalent UPC/EAN/GTIN representations normalize to GTIN-14."""
17 canonical = "00724354283857"
18
19 assert normalize_external_id(ExternalID.BARCODE, "724354283857") == canonical
20 assert normalize_external_id(ExternalID.BARCODE, "0724354283857") == canonical
21 assert normalize_external_id(ExternalID.BARCODE, canonical) == canonical
22 assert normalize_external_id(ExternalID.BARCODE, "000724354283857") == canonical
23
24
25def test_invalid_gtin_is_preserved() -> None:
26 """Invalid provider barcode data remains available for exact matching."""
27 assert normalize_external_id(ExternalID.BARCODE, "602445790392") == "602445790392"
28 assert normalize_external_id(ExternalID.BARCODE, "catalog-123") == "catalog-123"
29
30
31def test_truncated_gtin14_recovers_check_digit() -> None:
32 """A GTIN-14 with its check digit chopped off (Qobuz) recovers the full value."""
33 assert normalize_external_id(ExternalID.BARCODE, "0060252758365") == "00602527583655"
34 # a structurally valid EAN-13 keeps taking the regular normalization path
35 assert normalize_external_id(ExternalID.BARCODE, "0724354283857") == "00724354283857"
36
37
38def test_legacy_gtin_lookup_values() -> None:
39 """A canonical barcode lookup includes legacy zero-padded storage forms."""
40 values = external_id_lookup_values(ExternalID.BARCODE, "0724354283857")
41
42 assert "724354283857" in values
43 assert "0724354283857" in values
44 assert "00724354283857" in values
45 assert "000724354283857" in values
46
47
48def test_untyped_lookup_values_and_itunes_upc() -> None:
49 """Untyped lookups normalize formatted IDs and canonical GTINs convert to UPC."""
50 assert "USRC17607839" in external_id_lookup_values_untyped("US-RC1-76-07839")
51 assert barcode_to_upc("00724354283857") == "724354283857"
52
53
54def test_normalize_isrc_and_mbid() -> None:
55 """Formatted ISRCs and MusicBrainz UUIDs use canonical representations."""
56 mbid = "B1A9C0E9-D987-4042-AE91-78D6A3267D69"
57
58 assert normalize_external_id(ExternalID.ISRC, "us-rc1-76-07839") == "USRC17607839"
59 assert normalize_external_id(ExternalID.ISRC, "invalid-isrc") == "invalid-isrc"
60 assert normalize_external_id(ExternalID.MB_RECORDING, f"{{{mbid}}}") == mbid.lower()
61
62
63def test_mbid_lookup_values_include_braced_form() -> None:
64 """A canonical MBID lookup also covers the legacy braced storage form."""
65 mbid = "b1a9c0e9-d987-4042-ae91-78d6a3267d69"
66 values = external_id_lookup_values(ExternalID.MB_RECORDING, mbid)
67
68 assert mbid in values
69 assert f"{{{mbid}}}" in values
70
71
72def test_normalize_external_ids_deduplicates_values() -> None:
73 """Equivalent identifier forms collapse to one canonical pair."""
74 result = normalize_external_ids(
75 {
76 (ExternalID.BARCODE, "0724354283857"),
77 (ExternalID.BARCODE, "000724354283857"),
78 }
79 )
80
81 assert result == {(ExternalID.BARCODE, "00724354283857")}
82
83
84def test_is_valid_barcode() -> None:
85 """A structurally valid GTIN is recognized regardless of UPC/EAN notation."""
86 assert is_valid_barcode("888072439412") is True
87 assert is_valid_barcode("0888072439412") is True
88 assert is_valid_barcode("00888072439412") is True
89 # bad check digit, wrong length or non-numeric data is not a usable barcode
90 assert is_valid_barcode("602445790392") is False
91 assert is_valid_barcode("12345") is False
92 assert is_valid_barcode("catalog-123") is False
93