"""Teste US-002 (PRD 3.6): buton "Editeaza" pe rand in tabelul de preview. Mod editare pe rand (form propriu, NU #confirm-form). Swap pe rand + OOB contoare, NU pe #import-section (D-3.1). La eroare de validare randul ramane in editare cu valorile pastrate (D-2.1/D-2.2). Enter intr-un camp salveaza randul, nu confirma (D-3.3). """ from __future__ import annotations import io import os import pathlib import re import tempfile import pytest from fastapi.testclient import TestClient _FIXTURES = pathlib.Path(__file__).parent / "fixtures" @pytest.fixture() def client(monkeypatch): tmp = tempfile.mkdtemp() monkeypatch.setenv("AUTOPASS_DB_PATH", os.path.join(tmp, "edit_ui.db")) monkeypatch.setenv("AUTOPASS_WEB_AUTH_REQUIRED", "false") from app.config import get_settings get_settings.cache_clear() from app.crypto import reset_cache reset_cache() from app.main import app with TestClient(app) as c: yield c get_settings.cache_clear() reset_cache() def _seed_op1() -> None: from app.db import get_connection conn = get_connection() try: conn.execute( "INSERT OR REPLACE INTO nomenclator_rar (cod_prestatie, nume_prestatie) " "VALUES ('R-FRANE','Reparatie frane')" ) conn.execute( "INSERT OR IGNORE INTO operations_mapping (account_id, cod_op_service, cod_prestatie, auto_send) " "VALUES (1, 'OP-1', 'R-FRANE', 1)" ) conn.commit() finally: conn.close() def _upload_and_preview(client: TestClient) -> int: """Upload fixture necanonic + salveaza maparea -> preview. Intoarce import_id.""" data = (_FIXTURES / "import_antet_necanonic.csv").read_bytes() r = client.post( "/_import/upload", files={"file": ("import_antet_necanonic.csv", io.BytesIO(data), "text/csv")}, ) assert r.status_code == 200, r.text m = re.search(r"/_import/(\d+)/mapare-coloane", r.text) assert m, "import_id negasit in formularul de mapare" iid = int(m.group(1)) r = client.post(f"/_import/{iid}/mapare-coloane", data={ "colname": ["Serie sasiu", "Nr", "Data", "KM", "Operatie"], "canon": ["vin", "nr_inmatriculare", "data_prestatie", "odometru_final", "operatie"], "format_data": "YYYY-MM-DD", }) assert r.status_code == 200, r.text return iid def test_preview_are_buton_editeaza_pe_rand(client): """Tabelul de preview are buton 'Editeaza' pe rand (coloana de actiuni).""" _seed_op1() iid = _upload_and_preview(client) r = client.get(f"/_import/{iid}/preview") assert r.status_code == 200 assert "Editeaza" in r.text assert f"/_import/{iid}/rand/0/editare" in r.text def test_editeaza_intra_in_mod_editare_form_propriu(client): """GET editare: randul devine un FORM separat care posteaza la .../editeaza, NU #confirm-form.""" _seed_op1() iid = _upload_and_preview(client) r = client.get(f"/_import/{iid}/rand/0/editare") assert r.status_code == 200 html = r.text assert f'hx-post="/_import/{iid}/rand/0/editeaza"' in html # Inputurile de editare NU stau in #confirm-form (form propriu). assert 'id="confirm-form"' not in html assert 'name="data_prestatie"' in html and 'name="vin"' in html def test_salveaza_cere_reincarcare_si_toast(client): """POST editeaza: raspuns minimal + HX-Trigger(reincarcaPreview + randSalvat). Contractul nou (dogfood 5.13): nu mai facem OOB swap pe