import sys import types from pathlib import Path import gradio as gr import pytest # app.py loads YuE2 on import; the helpers under test never render, so a stub engine is enough. sys.modules.setdefault("song_engine", types.SimpleNamespace(render_song=None)) import app # noqa: E402 SETTINGS = {"style": "Rock", "cot": "off", "steps": 16, "seed": 5} LAST_SONG = {"style": "Lo-fi", "cot": "melody", "steps": 32, "seed": 7, "lyrics": "[Verse]\nla", "abc": "X:1"} def test_text_download_writes_named_file_with_current_text(): update = app.text_download(" [Verse]\nRain on the glass ", app.LYRICS_FILENAME) path = Path(update["value"]) assert update["interactive"] is True assert path.name == "lyrics.txt" assert path.read_text(encoding="utf-8") == "[Verse]\nRain on the glass\n" def test_text_download_follows_edits_and_uses_score_filename(): first = Path(app.text_download("X:1\nK:C\nCDEF|", app.SCORE_FILENAME)["value"]) edited = Path(app.text_download("X:1\nK:G\nGABc|", app.SCORE_FILENAME)["value"]) assert first.name == edited.name == "abc_score.txt" assert edited.read_text(encoding="utf-8") == "X:1\nK:G\nGABc|\n" assert first.read_text(encoding="utf-8") == "X:1\nK:C\nCDEF|\n" def test_text_download_is_disabled_for_empty_text(): assert app.text_download(" ", app.LYRICS_FILENAME) == gr.update(value=None, interactive=False) def test_score_song_reuses_last_song_style_and_seed(): assert app.score_song(LAST_SONG, SETTINGS) == {"style": "Lo-fi", "cot": "melody", "steps": 32, "seed": 7} def test_score_song_without_last_song_uses_settings_with_score_planning(): assert app.score_song(None, SETTINGS) == {"style": "Rock", "cot": "full", "steps": 16, "seed": 5} def test_score_song_switches_no_score_planning_to_full(): assert app.score_song({**LAST_SONG, "cot": "off"}, SETTINGS)["cot"] == "full" @pytest.mark.parametrize("lyrics", ["", " ", "Just some words without tags"]) def test_require_lyrics_rejects_empty_or_untagged_lyrics(lyrics): with pytest.raises(gr.Error): app.require_lyrics(lyrics) def test_require_lyrics_accepts_typed_lyrics_with_tags(): assert app.require_lyrics(" [verse]\nmy own words\n[Chorus]\nhey ") == "[verse]\nmy own words\n[Chorus]\nhey" def test_require_score_rejects_empty_score(): with pytest.raises(gr.Error): app.require_score(" ")