summaryrefslogtreecommitdiff
path: root/tests/test_tidal_extras.py
blob: 916b88b7e50a4a175aff5e1eb10fa716a7a48349 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import mutagen
import pytest

from tidal_dl_ng.helper.tidal import (
    _normalize_contributors,
    extract_contributor_names,
    parse_track_and_album_extras,
)
from tidal_dl_ng.metadata import Metadata


def test_normalize_contributors_dict_shape():
    raw = {
        "producer": [{"name": "Prod A"}, {"name": "Prod B", "foo": "bar"}],
        "composer": [{"name": "Comp 1"}],
        "invalid": ["x", {"no_name": True}],
    }

    result = _normalize_contributors(raw)

    assert set(result.keys()) == {"producer", "composer"}
    assert result["producer"] == ["Prod A", "Prod B"]
    assert result["composer"] == ["Comp 1"]


def test_normalize_contributors_list_shape():
    raw = [
        {"name": "Prod A", "role": "producer"},
        {"name": "Comp 1", "role": "composer"},
        {"name": "Bad", "role": ""},
        "oops",
    ]

    result = _normalize_contributors(raw)

    assert set(result.keys()) == {"producer", "composer"}
    assert result["producer"] == ["Prod A"]
    assert result["composer"] == ["Comp 1"]


def test_parse_track_and_album_extras_full():
    track_json = {
        "bpm": 123,
        "contributors": {
            "producer": [{"name": "Prod A"}],
            "composer": [{"name": "Comp 1"}],
            "lyricist": [{"name": "Writer"}],
        },
    }
    album_json = {
        "label": "Test Label",
        "genres": [{"name": "Rock"}, {"name": "Indie"}],
    }

    extras = parse_track_and_album_extras(track_json, album_json)

    assert extras["bpm"] == 123
    assert extras["label"] == "Test Label"
    assert extras["genres"] == ["Rock", "Indie"]
    assert extras["contributors_by_role"]["producer"] == ["Prod A"]


def test_parse_track_and_album_extras_missing_fields():
    extras = parse_track_and_album_extras({}, {})

    assert extras["bpm"] is None
    assert extras["label"] == ""
    assert extras["genres"] == []
    assert extras["contributors_by_role"] == {}


@pytest.mark.parametrize(
    "role,expected",
    [
        ("producer", "Prod A, Prod B"),
        ("composer", "Comp 1"),
        ("lyricist", ""),
    ],
)
def test_extract_contributor_names(role, expected):
    contributors = {
        "producer": ["Prod A", "Prod B"],
        "composer": ["Comp 1"],
    }

    names = extract_contributor_names(contributors, role, delimiter=", ")

    assert names == expected


def test_metadata_enriched_tags_flac(tmp_path):
    # Create a valid minimal FLAC file using soundfile
    path = tmp_path / "test.flac"

    # Import numpy and soundfile to create a minimal valid FLAC
    try:
        import numpy as np
        import soundfile as sf

        # Create 1 second of silence at 44100 Hz
        silence = np.zeros((44100, 2), dtype=np.float32)
        sf.write(str(path), silence, 44100, format="FLAC")
    except ImportError:
        # Fallback: skip this test if soundfile not available
        import pytest

        pytest.skip("soundfile required for FLAC test")

    m = Metadata(
        path_file=path,
        target_upc={"FLAC": "UPC", "MP3": "UPC", "MP4": "UPC"},
        title="Title",
        album="Album",
        artists="Artist1, Artist2",
        albumartist="AlbumArtist",
        genre="Rock, Indie",
        label="Test Label",
        bpm=120,
        producers="Prod A, Prod B",
        composers_detailed="Comp 1, Comp 2",
        lyricists="Writer 1, Writer 2",
    )

    m.save()

    audio_out = mutagen.flac.FLAC(path)

    assert audio_out["TITLE"][0] == "Title"
    assert audio_out["ALBUM"][0] == "Album"
    assert audio_out["ARTIST"][0] == "Artist1, Artist2"
    assert audio_out["ALBUMARTIST"][0] == "AlbumArtist"
    assert audio_out["GENRE"][0] == "Rock, Indie"
    assert audio_out["LABEL"][0] == "Test Label"
    assert audio_out["BPM"][0] == "120"
    assert audio_out["PRODUCER"][0] == "Prod A, Prod B"
    assert audio_out["LYRICIST"][0] == "Writer 1, Writer 2"
    assert audio_out["COMPOSER"][0] == "Comp 1, Comp 2"