import httpx import pytest import lyrics as lyrics_module from lyrics import ( STRUCTURES, HfLyricsWriter, LyricsError, build_messages, build_rewrite_messages, clean_lyrics, section_tags, structure_sections, ) ORIGINAL = "[Verse 1]\nRain on the glass\nCity in grey\n\n[Chorus]\nStay, stay\n[Outro]\nGone" def test_clean_lyrics_strips_reasoning_and_code_fences(): raw = "plan the chorus\n```text\n[Verse]\nRain on the glass\n[Chorus]\nStay\n```" assert clean_lyrics(raw) == "[Verse]\nRain on the glass\n[Chorus]\nStay" def test_clean_lyrics_rejects_text_without_section_tags(): with pytest.raises(LyricsError): clean_lyrics("Here are some lyrics about rain.") def _response(status: int, body: dict | None = None) -> httpx.Response: return httpx.Response(status, json=body or {}, request=httpx.Request("POST", "https://x")) @pytest.mark.parametrize( ("status", "expected"), [ (401, "Hugging Face rejected your token. Update it in Settings."), (403, "This token cannot use Inference Providers. Check its permissions and credits."), (500, "The lyrics service returned an error. Please try again."), ], ) def test_provider_errors_map_to_user_messages(monkeypatch, status, expected): monkeypatch.setattr(lyrics_module.httpx, "post", lambda *a, **k: _response(status)) with pytest.raises(LyricsError) as info: HfLyricsWriter().write("rain", "hf_token") assert str(info.value) == expected def test_successful_completion_returns_cleaned_lyrics(monkeypatch): body = {"choices": [{"message": {"content": "[Intro]\nla\n[Chorus]\nhey"}}]} seen = {} def fake_post(url, headers, json, timeout): seen.update(url=url, headers=headers, json=json) return _response(200, body) monkeypatch.setattr(lyrics_module.httpx, "post", fake_post) assert HfLyricsWriter().write("rain", "hf_token") == "[Intro]\nla\n[Chorus]\nhey" assert seen["url"] == "https://router.huggingface.co/v1/chat/completions" assert seen["headers"] == {"Authorization": "Bearer hf_token"} assert seen["json"]["model"] == "deepseek-ai/DeepSeek-V4.1-Flash:deepinfra" def test_section_tags_ignore_numbering_case_and_lyric_lines(): assert section_tags(ORIGINAL) == ["verse", "chorus", "outro"] assert section_tags("[verse]\na\n[CHORUS]\nb\n[Outro 2]\nc") == ["verse", "chorus", "outro"] def _completion(content: str) -> httpx.Response: return _response(200, {"choices": [{"message": {"content": content}}]}) def test_rewrite_sends_original_lyrics_and_direction(monkeypatch): new = "[Verse]\nSun on the glass\nCity in gold\n[Chorus]\nGo, go\n[Outro]\nHome" seen = {} def fake_post(url, headers, json, timeout): seen.update(json=json) return _completion(new) monkeypatch.setattr(lyrics_module.httpx, "post", fake_post) result = HfLyricsWriter().rewrite("rain", ORIGINAL, "more hopeful", "hf_token") assert result == new prompt = seen["json"]["messages"][-1]["content"] assert ORIGINAL in prompt assert "more hopeful" in prompt assert "same number of lines" in prompt def test_rewrite_without_direction_still_asks_for_better_lyrics(monkeypatch): seen = {} def fake_post(url, headers, json, timeout): seen.update(json=json) return _completion(ORIGINAL) monkeypatch.setattr(lyrics_module.httpx, "post", fake_post) HfLyricsWriter().rewrite("rain", ORIGINAL, " ", "hf_token") assert "Direction: Make the lyrics more vivid" in seen["json"]["messages"][-1]["content"] def test_rewrite_rejects_lyrics_that_change_the_song_structure(monkeypatch): changed = "[Verse]\nSun\n[Bridge]\nNew part\n[Chorus]\nGo\n[Outro]\nHome" monkeypatch.setattr(lyrics_module.httpx, "post", lambda *a, **k: _completion(changed)) with pytest.raises(LyricsError, match="original song structure"): HfLyricsWriter().rewrite("rain", ORIGINAL, "", "hf_token") def test_write_and_rewrite_prompts_forbid_filler_choruses(): for messages in (build_messages("rain"), build_rewrite_messages("rain", ORIGINAL, "")): prompt = messages[-1]["content"] assert "'hey hey'" in prompt and "'oh oh'" in prompt assert "meaningful words" in prompt @pytest.mark.parametrize("structure", STRUCTURES) def test_new_lyrics_prompt_lists_the_chosen_structure_in_order(structure): prompt = build_messages("rain", structure)[-1]["content"] expected = "\n".join(f"[{section}]" for section in structure_sections(structure)) assert expected in prompt def test_only_structures_with_an_instrumental_explain_it(): with_break = next(s for s in STRUCTURES if "Instrumental" in s) without_break = next(s for s in STRUCTURES if "Instrumental" not in s) assert "Instrumental sections keep their tag" in build_messages("rain", with_break)[-1]["content"] assert "Instrumental sections" not in build_messages("rain", without_break)[-1]["content"] def test_write_sends_the_chosen_structure(monkeypatch): seen = {} def fake_post(url, headers, json, timeout): seen.update(json=json) return _completion("[Verse]\na\n[Pre-Chorus]\nb\n[Chorus]\nc") monkeypatch.setattr(lyrics_module.httpx, "post", fake_post) HfLyricsWriter().write("rain", "hf_token", STRUCTURES[1]) assert "[Pre-Chorus]" in seen["json"]["messages"][-1]["content"]