File size: 2,919 Bytes
9d4abaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a4837b
 
 
 
 
9d4abaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
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