Skip to content

Commit cfc84a9

Browse files
committed
test: updated tests for refactor
1 parent 835a516 commit cfc84a9

File tree

2 files changed

+95
-29
lines changed

2 files changed

+95
-29
lines changed

tests/test_join_and_id3.py

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,81 @@
1+
# tests/test_join_and_id3.py
2+
13
from pathlib import Path
24
import workbench_core as core
35

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)
421

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
930
)
1031

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+
1638

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`.
2141

2242

2343
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

workbench_core.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,30 @@ def get_album_name(files: list[Path]) -> str:
565565
# -----------------------
566566

567567

568+
def prepare_cookies(
569+
cookies_file: Path | None,
570+
cookies_browser: str | None,
571+
working_dir: Path,
572+
log: Callable[[str], None] | None = None,
573+
) -> Path | None:
574+
"""
575+
If a JSON from Cookie-Editor is provided, convert to Netscape text and return that path.
576+
If a browser name is provided, return None (yt-dlp will use --cookies-from-browser).
577+
Otherwise, return the original cookies_file.
578+
"""
579+
if cookies_file and cookies_file.suffix.lower() == ".json":
580+
out = working_dir / "cookies.txt"
581+
converted_path = convert_cookie_editor_json_to_netscape(cookies_file, out, log=log)
582+
return converted_path
583+
584+
if cookies_browser and cookies_browser.lower() != "none":
585+
if log:
586+
log(f"[cookies] using cookies from browser: {cookies_browser}")
587+
return None
588+
589+
return cookies_file
590+
591+
568592
def convert_cookie_editor_json_to_netscape(json_path: Path, out_txt: Path, log) -> Path:
569593
"""Convert Cookie-Editor/EditThisCookie JSON into a Netscape cookies.txt with header."""
570594
try:

0 commit comments

Comments
 (0)