|
| 1 | +# tests/test_join_and_id3.py |
| 2 | + |
1 | 3 | from pathlib import Path
|
2 | 4 | import workbench_core as core
|
3 | 5 |
|
| 6 | +# We need to import mutagen to verify the results of the tagging function |
| 7 | +from mutagen.id3 import ID3 |
| 8 | + |
| 9 | +# --- Test for the new mutagen-based tag writer --- |
| 10 | +# The old test was for an ffmpeg function that no longer exists in that form. |
| 11 | +# This new test correctly checks the behavior of write_id3_tags_mutagen. |
| 12 | + |
| 13 | + |
| 14 | +def test_write_id3_tags_mutagen_mock(monkeypatch, tmp_path): |
| 15 | + """ |
| 16 | + Tests the mutagen-based tag writer. |
| 17 | + It calls the function and then uses mutagen to verify the tags were written. |
| 18 | + """ |
| 19 | + # We don't need to mock any subprocesses, just the dependency check. |
| 20 | + monkeypatch.setattr(core, "ensure_mutagen_installed", lambda log: True) |
4 | 21 |
|
5 |
| -def test_write_id3_tags_mock(monkeypatch, tmp_path): |
6 |
| - # Pretend ffmpeg exists and _run_capture succeeds |
7 |
| - monkeypatch.setattr( |
8 |
| - core, "resolve_tool_path", lambda cmd: "/bin/ffmpeg" if cmd == "ffmpeg" else None |
| 22 | + mp3_file = tmp_path / "Artist Name - Song Title.mp3" |
| 23 | + mp3_file.write_bytes(b"dummy mp3 data") # Create a dummy file |
| 24 | + |
| 25 | + # Call the function with the correct signature (list of files, album name) |
| 26 | + core.write_id3_tags_mutagen( |
| 27 | + files=[mp3_file], |
| 28 | + album="My Test Album", |
| 29 | + log=print, # Use print for logging during the test |
9 | 30 | )
|
10 | 31 |
|
11 |
| - def ok_run(args, cwd=None): |
12 |
| - # Simulate ffmpeg writing tmp file |
13 |
| - out = Path(args[-1]) |
14 |
| - out.write_bytes(b"dummy") |
15 |
| - return 0, "ok", "" |
| 32 | + # Verify the results by reading the tags back from the file |
| 33 | + tags = ID3(mp3_file) |
| 34 | + assert str(tags["TALB"].text[0]) == "My Test Album" |
| 35 | + assert str(tags["TPE1"].text[0]) == "Artist Name" |
| 36 | + assert str(tags["TIT2"].text[0]) == "Song Title" |
| 37 | + |
16 | 38 |
|
17 |
| - monkeypatch.setattr(core, "_run_capture", ok_run) |
18 |
| - mp3 = tmp_path / "a.mp3" |
19 |
| - mp3.write_bytes(b"src") |
20 |
| - assert core.write_id3_tags(mp3, {"artist": "X"}, log=None) is True |
| 39 | +# --- Test for the new ffmpeg-based joiner --- |
| 40 | +# This test is updated to use the new function signature and mock `run_quiet`. |
21 | 41 |
|
22 | 42 |
|
23 | 43 | def test_join_via_wav_then_lame_mock(monkeypatch, tmp_path):
|
24 |
| - # Pretend tools exist and run capture OK twice |
25 |
| - monkeypatch.setattr(core, "resolve_tool_path", lambda cmd: "/bin/" + cmd) |
26 |
| - seq = {"calls": 0} |
27 |
| - |
28 |
| - def ok_run(args, cwd=None): |
29 |
| - seq["calls"] += 1 |
30 |
| - # On second run we expect output MP3 path at the end |
31 |
| - return 0, "ok", "" |
32 |
| - |
33 |
| - monkeypatch.setattr(core, "_run_capture", ok_run) |
34 |
| - wavs = [tmp_path / "1.wav", tmp_path / "2.wav"] |
35 |
| - for w in wavs: |
36 |
| - w.write_bytes(b"w") |
37 |
| - out = tmp_path / "joined.mp3" |
38 |
| - assert core.join_via_wav_then_lame(wavs, out, lame_bitrate_kbps=192, log=None) is True |
39 |
| - assert seq["calls"] >= 2 |
| 44 | + """ |
| 45 | + Tests the ffmpeg-based joiner. |
| 46 | + It mocks `run_quiet` and checks that the function is called with the |
| 47 | + correct arguments and returns the expected final path. |
| 48 | + """ |
| 49 | + # This list will track calls to our mock function |
| 50 | + call_log = [] |
| 51 | + |
| 52 | + def mock_run_quiet(cmd, cwd=None, env=None): |
| 53 | + # Log the command that was run |
| 54 | + call_log.append(cmd) |
| 55 | + # The last argument is the output file; simulate its creation |
| 56 | + Path(cmd[-1]).touch() |
| 57 | + return 0 # Return success code |
| 58 | + |
| 59 | + monkeypatch.setattr(core, "run_quiet", mock_run_quiet) |
| 60 | + |
| 61 | + # Create dummy source files for the function to join |
| 62 | + source_files = [tmp_path / "1.mp3", tmp_path / "2.mp3"] |
| 63 | + for f in source_files: |
| 64 | + f.touch() |
| 65 | + |
| 66 | + # Call the function with the new, correct arguments |
| 67 | + result_path = core.join_via_wav_then_lame( |
| 68 | + files=source_files, |
| 69 | + outdir=tmp_path, |
| 70 | + sr=44100, |
| 71 | + br_kbps=192, |
| 72 | + join_name="final_album", |
| 73 | + log=print, |
| 74 | + ) |
| 75 | + |
| 76 | + # 1. Assert that the function returned the correct final path |
| 77 | + assert result_path == tmp_path / "final_album.mp3" |
| 78 | + |
| 79 | + # 2. Assert that ffmpeg was called the correct number of times |
| 80 | + # (once for each source file + once for concat + once for final encode) |
| 81 | + assert len(call_log) == len(source_files) + 2 |
0 commit comments