""" Tests for Phonetic Engine & Rhyme Meter Analyzer in Moldovan AI Media Studio. """ import pytest import sys from pathlib import Path from fastapi.testclient import TestClient REPO_ROOT = Path(__file__).resolve().parent.parent.parent MOLDOVAN_QWEN = REPO_ROOT / "moldovan-qwen" if str(MOLDOVAN_QWEN) not in sys.path: sys.path.insert(0, str(MOLDOVAN_QWEN)) from engine.phonetic_composer import PhoneticComposer, PhoneticDictionary from studio_server import app @pytest.fixture def phonetic_composer(): return PhoneticComposer() @pytest.fixture def client(): import os headers = {} if os.environ.get("FLOW_API_KEY"): headers["Authorization"] = f"Bearer {os.environ['FLOW_API_KEY']}" return TestClient(app, headers=headers) def test_phonetic_composer_transform(phonetic_composer): text = "koroce suka frate mergem la Chișinău" transformed = phonetic_composer.phonetize_text(text) assert "ka-ro-ce" in transformed assert "suuuka" in transformed assert "fraaate" in transformed def test_rhyme_and_syllable_meter_analysis(phonetic_composer): lyrics = [ "Mergem noaptea pe carare", "Pana cand rasare soare", "Frate dam volumul tare", "Fara nicio ezitare" ] analysis = phonetic_composer.analyze_meter_and_rhymes(lyrics) assert analysis["rhyme_tightness_score"] >= 70 assert len(analysis["line_details"]) == 4 for line in analysis["line_details"]: assert line["syllables"] > 0 assert line["last_word"] != "" def test_phonetic_endpoints(client): # Test GET phonetics dict res = client.get("/api/media/lyrics/phonetics") assert res.status_code == 200 data = res.json() assert "dictionary" in data or "phonetics" in data dict_map = data.get("dictionary") or data.get("phonetics") assert "koroce" in dict_map # Test POST phonetics update post_res = client.post("/api/media/lyrics/phonetics", json={ "word": "testcuvant", "phonetic": "test-cu-vaaant" }) assert post_res.status_code == 200 assert post_res.json()["phonetic"] == "test-cu-vaaant" # Test Phonetize endpoint phone_res = client.post("/api/media/lyrics/phonetize", json={ "text": "testcuvant koroce" }) assert phone_res.status_code == 200 assert "test-cu-vaaant" in phone_res.json()["phonetic_text"] assert "ka-ro-ce" in phone_res.json()["phonetic_text"] # Test DELETE rule del_res = client.delete("/api/media/lyrics/phonetics/testcuvant") assert del_res.status_code == 200 # Test Analyze Rhymes endpoint rhyme_res = client.post("/api/media/lyrics/analyze-rhymes", json={ "lyrics": "Linie una cu rima mare\nLinie doua cu rima tare" }) assert rhyme_res.status_code == 200 r_data = rhyme_res.json() assert "rhyme_tightness_score" in r_data assert len(r_data["analyzed_lines"]) == 2