From e33868fbe95efe9f500a9b88221c97e5ad91339b Mon Sep 17 00:00:00 2001 From: OmikronApex Date: Wed, 8 Jul 2026 16:57:38 +0200 Subject: [PATCH] test: add pytest coverage for LRC formatting, status, and export routes Covers _format_lrc/_format_lrc_word_level (line + inline word- timestamp formatting, empty-input edge case), /status (no server configured, healthy/error status codes, connection exceptions via mocked requests), and /export (missing-segments 400, header field presence/absence, filename sanitization). /align and /save additionally require a host-provided sloppak module not importable standalone in this repo, so route-level testing for those is left for a follow-up (same situation as lyrics_karaoke#12). Co-Authored-By: Claude Fable 5 --- tests/conftest.py | 25 ++++++++ tests/test_lrc_and_routes.py | 119 +++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 tests/conftest.py create mode 100644 tests/test_lrc_and_routes.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..5d2ff4b --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,25 @@ +import sys +from pathlib import Path + +import pytest +from fastapi import FastAPI +from fastapi.testclient import TestClient + +sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) + +import routes # noqa: E402 + + +@pytest.fixture +def config_dir(tmp_path): + d = tmp_path / "config" + d.mkdir() + return d + + +@pytest.fixture +def client(config_dir): + app = FastAPI() + routes.setup(app, {"config_dir": config_dir, "get_dlc_dir": lambda: None}) + with TestClient(app) as c: + yield c diff --git a/tests/test_lrc_and_routes.py b/tests/test_lrc_and_routes.py new file mode 100644 index 0000000..3a6fb49 --- /dev/null +++ b/tests/test_lrc_and_routes.py @@ -0,0 +1,119 @@ +"""LRC formatting (pure) + /status and /export routes.""" + +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) + +import routes # noqa: E402 + +STATUS = "/api/plugins/lyrics_sync/status" +EXPORT = "/api/plugins/lyrics_sync/export" + + +# ── _format_lrc / _format_lrc_word_level (pure) ──────────────────────────────── + +def test_format_lrc_basic_timestamps(): + segments = [{"start": 0.0, "text": "Hello"}, {"start": 65.5, "text": "World"}] + assert routes._format_lrc(segments) == "[00:00.00]Hello\n[01:05.50]World\n" + + +def test_format_lrc_empty_segments(): + assert routes._format_lrc([]) == "\n" + + +def test_format_lrc_word_level_includes_inline_word_timestamps(): + segments = [{ + "start": 1.0, "text": "hi there", + "words": [{"start": 1.0, "text": "hi"}, {"start": 1.5, "text": "there"}], + }] + lrc = routes._format_lrc_word_level(segments) + assert lrc == "[00:01.00]<00:01.00>hi <00:01.50>there\n" + + +def test_format_lrc_word_level_falls_back_to_line_text_without_words(): + segments = [{"start": 2.0, "text": "no word timing"}] + assert routes._format_lrc_word_level(segments) == "[00:02.00]no word timing\n" + + +# ── /status ───────────────────────────────────────────────────────────────── + +def test_status_no_server_configured(client): + r = client.get(STATUS) + assert r.status_code == 200 + assert r.json() == {"available": False, "reason": "No demucs server configured"} + + +def test_status_reports_available_server(client, config_dir, monkeypatch): + (config_dir / "config.json").write_text('{"demucs_server_url": "http://localhost:9000"}') + + import requests + + class FakeResp: + status_code = 200 + + monkeypatch.setattr(requests, "get", lambda url, timeout=5: FakeResp()) + r = client.get(STATUS) + assert r.json() == {"available": True, "server_url": "http://localhost:9000"} + + +def test_status_reports_server_error_code(client, config_dir, monkeypatch): + (config_dir / "config.json").write_text('{"demucs_server_url": "http://localhost:9000"}') + import requests + + class FakeResp: + status_code = 500 + + monkeypatch.setattr(requests, "get", lambda url, timeout=5: FakeResp()) + r = client.get(STATUS) + assert r.json() == {"available": False, "reason": "Server returned 500"} + + +def test_status_reports_connection_exception(client, config_dir, monkeypatch): + (config_dir / "config.json").write_text('{"demucs_server_url": "http://localhost:9000"}') + import requests + + def raise_conn(url, timeout=5): + raise ConnectionError("boom") + monkeypatch.setattr(requests, "get", raise_conn) + r = client.get(STATUS) + assert r.json()["available"] is False + assert "boom" in r.json()["reason"] + + +# ── /export ───────────────────────────────────────────────────────────────── + +def test_export_requires_segments(client): + r = client.post(EXPORT, json={"segments": []}) + assert r.status_code == 400 + + +def test_export_produces_lrc_with_header(client): + r = client.post(EXPORT, json={ + "segments": [{"start": 0.0, "text": "Hello"}], + "title": "My Song", "artist": "My Band", + }) + assert r.status_code == 200 + body = r.text + assert "[ti:My Song]" in body + assert "[ar:My Band]" in body + assert "[by:Slopsmith Lyrics Sync]" in body + assert "[00:00.00]Hello" in body + assert "attachment" in r.headers["content-disposition"] + assert "My Band - My Song.lrc" in r.headers["content-disposition"] + + +def test_export_omits_header_fields_when_absent(client): + r = client.post(EXPORT, json={"segments": [{"start": 0.0, "text": "Hi"}]}) + body = r.text + assert "[ti:" not in body + assert "[ar:" not in body + assert "lyrics.lrc" in r.headers["content-disposition"] + + +def test_export_sanitizes_slashes_in_filename(client): + r = client.post(EXPORT, json={ + "segments": [{"start": 0.0, "text": "Hi"}], + "title": "A/B\\C", + }) + assert "A_B_C.lrc" in r.headers["content-disposition"]