"""Tests for the input modes: AI backup vs. ground truth in vocals.json, pitch cache invalidation.""" import json from unmix.pipeline.source_modes import ( invalidate_pitch_cache, mode_label, prepare_ai_vocals, previous_vocals_source, ) from unmix.schema.vocals import VocalLine, VocalsDocument, VocalSyllable def _doc(text: str, start: float = 1.0) -> VocalsDocument: syl = VocalSyllable(text=text, start=start, end=start + 0.5, midi=60.0, word_end=True) return VocalsDocument(lines=[VocalLine(start=start, end=start + 0.5, syllables=[syl])]) def _text(doc: VocalsDocument) -> str: return doc.lines[0].syllables[0].text def test_first_run_returns_ai_and_backs_it_up(tmp_path): out = tmp_path / "out" out.mkdir() (out / "vocals.json").write_text(_doc("ai").model_dump_json(), encoding="utf-8") ai = prepare_ai_vocals(out, out / ".cache", "whisper") assert _text(ai) == "ai" assert (out / ".cache" / "vocals_ai.json").exists() def test_run_after_ground_truth_write_uses_backup_not_reference(tmp_path): out = tmp_path / "out" cache = out / ".cache" out.mkdir() (out / "vocals.json").write_text(_doc("ai").model_dump_json(), encoding="utf-8") prepare_ai_vocals(out, cache, "whisper") # first --ultrastar run backs up the AI result (out / "vocals.json").write_text(_doc("ground-truth").model_dump_json(), encoding="utf-8") # second run in the same folder: vocals.json now holds the reference ai = prepare_ai_vocals(out, cache, "reference_ultrastar") assert _text(ai) == "ai" def test_reference_source_without_backup_gives_no_ai(tmp_path): out = tmp_path / "out" out.mkdir() (out / "vocals.json").write_text(_doc("ground-truth").model_dump_json(), encoding="utf-8") assert prepare_ai_vocals(out, out / ".cache", "reference_ultrastar") is None def test_invalidate_pitch_cache_removes_only_pitch_stage(tmp_path): (tmp_path / "s4_pitch.json").write_text("{}", encoding="utf-8") (tmp_path / "s3_lyrics.json").write_text("{}", encoding="utf-8") invalidate_pitch_cache(tmp_path) invalidate_pitch_cache(tmp_path) # no error when already gone assert not (tmp_path / "s4_pitch.json").exists() assert (tmp_path / "s3_lyrics.json").exists() def test_previous_vocals_source_and_mode_label(tmp_path): meta = tmp_path / "meta.json" assert previous_vocals_source(meta) is None meta.write_text(json.dumps({"vocals_source": "reference_ultrastar"}), encoding="utf-8") assert previous_vocals_source(meta) == "reference_ultrastar" meta.write_text("not json", encoding="utf-8") assert previous_vocals_source(meta) is None assert mode_label(False) == "whisper" assert mode_label(True) == "lyrics_aligned"