""" Teste pentru app/web/labels.py — modul de etichete umane (US-001, PRD 3.4). Ordinea: RED (scrise inainte de implementare), apoi GREEN dupa creare labels.py. """ import re import pytest from pathlib import Path # --------------------------------------------------------------------------- # Utilitara: extrage starile din CHECK constraint in schema.sql # --------------------------------------------------------------------------- def _starile_din_schema() -> list[str]: """ Parseaza `app/schema.sql` si returneaza lista starilor din CHECK constraint al coloanei `status` din tabela `submissions`. Linia relevanta (schema.sql, tabela submissions): CHECK (status IN ('queued','sending','sent','needs_mapping','needs_data','error')) Testul devine automat RED daca cineva adauga o stare noua in schema fara s-o mapeze in labels.py. """ schema_path = Path(__file__).parent.parent / "app" / "schema.sql" sql = schema_path.read_text(encoding="utf-8") # Cauta blocul CHECK aferent coloanei status din CREATE TABLE submissions. # Nota (5.5): de cand exista si `accounts.status` cu propriul CHECK, ancoram pe blocul # tabelei submissions (`CREATE TABLE ... submissions`) ca sa nu prindem starile de cont. tbl = re.search( r"CREATE TABLE[^;]*?submissions\b(.*?);", sql, re.IGNORECASE | re.DOTALL ) assert tbl, "Nu am gasit CREATE TABLE submissions in schema.sql — schema s-a schimbat?" match = re.search( r"CHECK\s*\(\s*status\s+IN\s*\(([^)]+)\)\s*\)", tbl.group(1), ) assert match, "Nu am gasit CHECK (status IN (...)) in submissions — schema s-a schimbat?" raw = match.group(1) # Extrage valorile dintre ghilimele simple stari = re.findall(r"'([^']+)'", raw) assert stari, "Lista de stari din CHECK este goala — ceva s-a stricat la parsare." return stari # --------------------------------------------------------------------------- # Import modulul de etichete (va esua la RED, inainte de implementare) # --------------------------------------------------------------------------- from app.web.labels import ( # noqa: E402 eticheta_stare, eticheta_worker, eticheta_rar, format_data_rar, ) # --------------------------------------------------------------------------- # Teste worker # --------------------------------------------------------------------------- def test_eticheta_worker_viu(): text, subtext, css_class = eticheta_worker(viu=True) assert "Trimitere automata" in text, ( f"Textul pentru worker viu trebuie sa contina 'Trimitere automata', got: {text!r}" ) assert "activa" in text.lower() or "activa" in subtext.lower(), ( f"Starea 'activa' trebuie sa apara in text sau subtext, got: text={text!r}, subtext={subtext!r}" ) # Nu trebuie sa afiseze cuvintele tehnice brute assert "viu" not in text.lower(), f"Textul nu trebuie sa contina 'viu': {text!r}" # Clasa CSS trebuie sa fie definita (non-vida) assert css_class, "css_class nu trebuie sa fie vida pentru worker viu" def test_eticheta_worker_mort(): text, subtext, css_class = eticheta_worker(viu=False) assert "Trimitere automata" in text, ( f"Textul pentru worker mort trebuie sa contina 'Trimitere automata', got: {text!r}" ) assert "oprita" in text.lower() or "oprita" in subtext.lower(), ( f"Starea 'oprita' trebuie sa apara in text sau subtext, got: text={text!r}, subtext={subtext!r}" ) assert "mort" not in text.lower(), f"Textul nu trebuie sa contina 'mort': {text!r}" assert css_class, "css_class nu trebuie sa fie vida pentru worker mort" # --------------------------------------------------------------------------- # Teste eticheta_rar # --------------------------------------------------------------------------- def test_eticheta_rar_ok(): text, subtext, css_class = eticheta_rar(stare="ok") assert "Legatura cu RAR" in text, f"got: {text!r}" assert "functionala" in text.lower() or "functionala" in subtext.lower() assert css_class def test_eticheta_rar_indisponibil(): text, subtext, css_class = eticheta_rar(stare="indisponibil") assert "Legatura cu RAR" in text, f"got: {text!r}" assert "indisponibila" in text.lower() or "indisponibila" in subtext.lower() assert css_class # --------------------------------------------------------------------------- # Test eticheta_stare pentru fiecare stare de submission # --------------------------------------------------------------------------- def test_eticheta_stare_submission(): """Verifica textele umane concrete pentru fiecare stare cunoscuta.""" cazuri = { "queued": ("In asteptare", "s-queued"), "sent": ("Declarate la RAR", "s-sent"), "sending": ("Se trimite", "s-sending"), "needs_mapping": ("Lipseste codul", "s-needs_mapping"), "needs_data": ("Date incomplete", "s-needs_data"), "error": ("Eroare", "s-error"), } for status, (fragment_text, clasa) in cazuri.items(): text, subtext, css_class = eticheta_stare(status) assert fragment_text.lower() in text.lower(), ( f"Status {status!r}: asteptam '{fragment_text}' in text, got {text!r}" ) assert css_class == clasa, ( f"Status {status!r}: asteptam css_class={clasa!r}, got {css_class!r}" ) # --------------------------------------------------------------------------- # Test parametrizat: TOATE starile din schema au eticheta (anti-drift) # --------------------------------------------------------------------------- _STARI_SCHEMA = _starile_din_schema() # --------------------------------------------------------------------------- # Test format_data_rar (US-001, PRD 3.5) # --------------------------------------------------------------------------- def test_format_data_rar(): """`2026-06-18T14:30:22` -> `18.06.2026 14:30:22`.""" assert format_data_rar("2026-06-18T14:30:22") == "18.06.2026 14:30:22" def test_format_data_rar_cu_timezone(): """Timezone si microsecunde nu strica formatarea; fractiunile cad.""" assert format_data_rar("2026-06-18T14:30:22.123456+00:00") == "18.06.2026 14:30:22" assert format_data_rar("2026-06-18T14:30:22Z") == "18.06.2026 14:30:22" def test_format_data_rar_lipsa(): """Valoare lipsa -> em-dash, nu exceptie.""" assert format_data_rar(None) == "—" assert format_data_rar("") == "—" assert format_data_rar(" ") == "—" def test_format_data_rar_invalid(): """Format invalid -> fallback grijuliu (intoarce brutul, nu arunca).""" # Nu trebuie sa arunce assert format_data_rar("nu-e-data") == "nu-e-data" assert format_data_rar(12345) == "12345" @pytest.mark.parametrize("status", _STARI_SCHEMA) def test_toate_starile_au_eticheta(status: str): """ Fiecare stare din CHECK constraint (schema.sql) trebuie sa aiba o eticheta non-vida si o clasa CSS non-vida in labels.py. Daca cineva adauga o stare noua in schema fara s-o mapeze, acest test devine RED. """ text, subtext, css_class = eticheta_stare(status) assert text, f"Status {status!r}: textul etichetei este vid." assert css_class, f"Status {status!r}: clasa CSS este vida." # Textul nu trebuie sa fie chiar statusul tehnic brut (ex. "queued" afisat ca atare) assert text.lower() != status.lower(), ( f"Status {status!r}: eticheta umana este identica cu statusul tehnic — nu e o eticheta umana." )