- base.html: listener htmx:beforeRequest scopat la #submissions-wrap care anuleaza (preventDefault) DOAR poll-ul periodic (fara requestConfig.triggeringEvent) cat timp modalul de detaliu e deschis SAU exista checkbox de bulk bifat. - F5/R6: trimiteriChanged si submit-ul de filtru au triggeringEvent -> trec mereu, deci pauza nu ramane lipita permanent daca randul bifat paraseste filtrul. - Resume automat (anularea nu opreste timer-ul htmx) + resume explicit pe checkbox change via delegare pe body -> trimiteriChanged from:body (pastreaza filtrul). - Vechea pauza pe „rand expandat" (5.8) era deja inlocuita de modalul global (US-003). - 3 teste noi in tests/test_web_modal.py; suita 843 passed, 1 deselected. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
219 lines
9.1 KiB
Python
219 lines
9.1 KiB
Python
"""Teste PRD 5.9 US-003: detaliul trimiterii se deschide intr-un MODAL global
|
|
(#modal-detaliu), in afara zonei de poll (#submissions-wrap).
|
|
|
|
Verificam markup-ul server-side: containerul modal e global si plasat IN AFARA
|
|
#submissions-wrap (de fapt sibling al <main>, ca `inert` pe <main> sa nu-l prinda),
|
|
corpul #detaliu-modal-body e tinta de swap, iar fragmentul de detaliu (forme corectie/
|
|
mapare/lifecycle) tinteste corpul modalului — NU vechiul #detaliu-{id} / #trimitere-detaliu.
|
|
Focus-trap / scroll-lock / inert sunt logica JS in base.html (verificam hook-urile).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
import tempfile
|
|
|
|
import pytest
|
|
from starlette.testclient import TestClient
|
|
|
|
|
|
def _create_account_user(email: str, name: str = "Service", password: str = "parolasecreta10"):
|
|
from app.accounts import create_account
|
|
from app.users import create_user
|
|
from app.db import get_connection
|
|
|
|
conn = get_connection()
|
|
try:
|
|
acct_id = create_account(conn, name, active=True)
|
|
create_user(conn, acct_id, email, password)
|
|
return acct_id
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def _login(client, email: str, password: str = "parolasecreta10") -> None:
|
|
resp = client.get("/login")
|
|
m = re.search(r'name="csrf_token"\s+value="([^"]+)"', resp.text) or \
|
|
re.search(r'value="([^"]+)"\s+name="csrf_token"', resp.text)
|
|
assert m
|
|
resp = client.post("/login", data={"email": email, "parola": password, "csrf_token": m.group(1)})
|
|
assert resp.status_code == 303
|
|
|
|
|
|
def _insert_submission(acct: int, status: str = "needs_data") -> int:
|
|
from app.db import get_connection
|
|
conn = get_connection()
|
|
try:
|
|
p = {
|
|
"vin": "WVWZZZ1JZXW000777",
|
|
"nr_inmatriculare": "B777ZZZ",
|
|
"data_prestatie": "2026-06-18",
|
|
"odometru_final": "55000",
|
|
"prestatii": [{"cod_prestatie": "R-FRANE", "denumire": "Reparatie frane"}],
|
|
}
|
|
cur = conn.execute(
|
|
"INSERT INTO submissions (idempotency_key, account_id, status, payload_json) "
|
|
"VALUES (?, ?, ?, ?)",
|
|
(f"k-{status}-{os.urandom(4).hex()}", acct, status, json.dumps(p)),
|
|
)
|
|
conn.commit()
|
|
return int(cur.lastrowid)
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
@pytest.fixture()
|
|
def client(monkeypatch):
|
|
tmp = tempfile.mkdtemp()
|
|
monkeypatch.setenv("AUTOPASS_DB_PATH", os.path.join(tmp, "modal.db"))
|
|
monkeypatch.setenv("AUTOPASS_WEB_AUTH_REQUIRED", "true")
|
|
from app.config import get_settings
|
|
get_settings.cache_clear()
|
|
from app.web import ratelimit
|
|
ratelimit._hits.clear()
|
|
from app.main import app
|
|
with TestClient(app, follow_redirects=False) as c:
|
|
yield c
|
|
ratelimit._hits.clear()
|
|
get_settings.cache_clear()
|
|
|
|
|
|
def test_modal_container_in_afara_submissions_wrap(client):
|
|
"""Containerul modal global exista, e dialog a11y si e plasat IN AFARA
|
|
#submissions-wrap (sibling al <main>, dupa </main>)."""
|
|
acct = _create_account_user("modal@test.com")
|
|
_insert_submission(acct, "needs_data") # sectiunea Trimiteri (wrap) apare doar cu randuri
|
|
_login(client, "modal@test.com")
|
|
|
|
html = client.get("/?tab=acasa").text
|
|
# containerul modal + corpul de swap
|
|
assert 'id="modal-detaliu"' in html, "lipseste containerul modal global"
|
|
assert 'id="detaliu-modal-body"' in html, "lipseste corpul de swap al modalului"
|
|
# rol de dialog modal + heading legat prin aria-labelledby
|
|
assert 'role="dialog"' in html
|
|
assert 'aria-modal="true"' in html
|
|
assert 'aria-labelledby="detaliu-modal-titlu"' in html
|
|
# buton de inchidere cu aria-label (R7)
|
|
assert "modal-close" in html
|
|
assert 'aria-label="Inchide detaliul"' in html
|
|
|
|
# Plasament: modalul e DUPA </main>, deci in afara <main> si a #submissions-wrap
|
|
# (care traieste in panoul din <main>). inert pe <main> nu-l prinde (R7).
|
|
idx_wrap = html.find('id="submissions-wrap"')
|
|
idx_main_close = html.find("</main>")
|
|
idx_modal = html.find('id="modal-detaliu"')
|
|
assert idx_wrap != -1 and idx_main_close != -1 and idx_modal != -1
|
|
assert idx_wrap < idx_main_close < idx_modal, "modalul trebuie sa fie in afara <main>/#submissions-wrap"
|
|
|
|
# Vechiul panou inert eliminat; fara mecanismul inline 5.8 in pagina.
|
|
assert 'id="trimitere-detaliu"' not in html
|
|
assert 'class="detaliu-rand"' not in html
|
|
assert "marcheazaDetaliuDeschis" not in html
|
|
|
|
|
|
def test_fragment_detaliu_tinteste_modalul(client):
|
|
"""Randul declanseaza modalul (hx-target=#detaliu-modal-body) si fragmentul de
|
|
detaliu (forme corectie/mapare/lifecycle) tinteste tot corpul modalului — NU
|
|
vechiul container per-rand #detaliu-{id} sau #trimitere-detaliu."""
|
|
acct = _create_account_user("frag@test.com")
|
|
sid = _insert_submission(acct, "needs_data")
|
|
_login(client, "frag@test.com")
|
|
|
|
# 1. Randul din tabel tinteste corpul modalului; fara rand-sibling / chevron / aria-expanded.
|
|
lista = client.get("/_fragments/submissions")
|
|
assert lista.status_code == 200
|
|
h = lista.text
|
|
assert 'hx-target="#detaliu-modal-body"' in h, "randul trebuie sa tinteasca corpul modalului"
|
|
assert 'hx-target="#detaliu-%d"' % sid not in h
|
|
assert 'class="detaliu-rand"' not in h
|
|
assert 'aria-expanded' not in h
|
|
assert "chevron" not in h
|
|
assert 'aria-haspopup="dialog"' in h
|
|
assert 'role="button"' in h and 'tabindex="0"' in h
|
|
|
|
# 2. Fragmentul de detaliu: formele tintesc corpul modalului, nu containerul vechi.
|
|
det = client.get(f"/_fragments/trimitere/{sid}")
|
|
assert det.status_code == 200
|
|
d = det.text
|
|
assert 'hx-target="#detaliu-modal-body"' in d
|
|
assert 'hx-target="#detaliu-%d"' % sid not in d
|
|
assert 'hx-target="#trimitere-detaliu"' not in d
|
|
# heading legat de aria-labelledby al dialogului
|
|
assert 'id="detaliu-modal-titlu"' in d
|
|
|
|
|
|
def test_modal_hookuri_js_prezente(client):
|
|
"""Logica de modal (focus-trap, scroll-lock, inert pe <main>, inchidere pe succes)
|
|
e prezenta in base.html — hook-urile cheie exista."""
|
|
_create_account_user("hook@test.com")
|
|
_login(client, "hook@test.com")
|
|
js = client.get("/?tab=acasa").text
|
|
assert "modal-detaliu" in js
|
|
# focus-trap + scroll-lock + inert pe ancestor stabil
|
|
assert "trapFocus" in js or "Tab" in js
|
|
assert "modal-open" in js
|
|
assert "inert" in js
|
|
# inchidere pe succes corectie/sterge (listener pe evenimentul HX-Trigger)
|
|
assert "inchideModal" in js
|
|
# API public pastrat (butoanele/rutele pot inchide modalul)
|
|
assert "window.inchideDetaliu" in js
|
|
|
|
|
|
# --- PRD 5.9 US-005 (R6): poll-guard ---------------------------------------
|
|
# Modalul + selectia trebuie sa supravietuiasca poll-ului de 15s. Logica e JS in
|
|
# base.html: testam la nivel de markup/handler ca guard-ul exista si distinge corect
|
|
# sursa trigger-ului (periodic vs trimiteriChanged/filtru). Comportamentul runtime
|
|
# efectiv (anularea propriu-zisa) e validat E2E (requiresBrowserCheck) — aici asertam
|
|
# codul/atributele care il implementeaza.
|
|
|
|
|
|
def test_poll_pauzat_cat_modal_deschis(client):
|
|
"""Guard-ul de poll exista si, cat modalul de detaliu e deschis, anuleaza
|
|
reincarcarea periodica a listei (#submissions-wrap), nu pe restul."""
|
|
_create_account_user("poll1@test.com")
|
|
_login(client, "poll1@test.com")
|
|
js = client.get("/?tab=acasa").text
|
|
|
|
# Guard scopat la poll-ul listei, declansat pe htmx:beforeRequest.
|
|
assert "htmx:beforeRequest" in js
|
|
assert "d.elt.id !== 'submissions-wrap'" in js, "guard-ul trebuie scopat la #submissions-wrap"
|
|
# Conditia (a): modal deschis -> pauza (preventDefault).
|
|
assert "modalDeschis" in js
|
|
assert "modal-detaliu" in js and "hidden" in js
|
|
assert "evt.preventDefault()" in js, "pauza scopata se face prin preventDefault"
|
|
|
|
|
|
def test_poll_pauzat_cat_exista_bifa(client):
|
|
"""Conditia (b): macar un checkbox de bulk bifat -> poll-ul periodic e pus pe
|
|
pauza. Resume pe checkbox `change` prin delegare pe body (prinde si bifele
|
|
randate dupa swap)."""
|
|
_create_account_user("poll2@test.com")
|
|
_login(client, "poll2@test.com")
|
|
js = client.get("/?tab=acasa").text
|
|
|
|
# Detecteaza bifa de bulk in interiorul #submissions-wrap.
|
|
assert "existaBifa" in js
|
|
assert 'input[name="submission_id"]:checked' in js
|
|
# Resume: delegare pe body pe evenimentul `change` al checkbox-ului de bulk.
|
|
assert "addEventListener('change'" in js
|
|
assert "t.name === 'submission_id'" in js
|
|
|
|
|
|
def test_trimiteriChanged_inca_reincarca_cu_bifa(client):
|
|
"""R6 (F5): guard-ul NU anuleaza request-urile cu `triggeringEvent`
|
|
(trimiteriChanged / submit filtru) — acelea TREC MEREU, ca pauza sa nu ramana
|
|
lipita permanent daca randul bifat paraseste filtrul."""
|
|
_create_account_user("poll3@test.com")
|
|
_login(client, "poll3@test.com")
|
|
js = client.get("/?tab=acasa").text
|
|
|
|
# Numai trigger-ul periodic (fara triggeringEvent) e candidat la pauza;
|
|
# orice request cu triggeringEvent iese devreme din guard.
|
|
assert "triggeringEvent" in js
|
|
assert "rc.triggeringEvent) return" in js, \
|
|
"request-urile cu triggeringEvent (trimiteriChanged/filtru) trebuie sa treaca mereu"
|
|
# Resume explicit reutilizeaza acelasi canal `trimiteriChanged` (pastreaza filtrul).
|
|
assert "trimiteriChanged" in js
|