"""Tests for UI-032: KPI block collapse on Dashboard.""" from __future__ import annotations import re import shutil from decimal import Decimal import pytest from fastapi.testclient import TestClient from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from app.database.database import Base, DATABASE_PATH from app.main import app from app.models.project import Project from app.models.boq import BoqDocument, WorkItem from app.routers import projects as project_router def _make_session(tmp_path): db_path = tmp_path / "ui032.db" shutil.copy2(DATABASE_PATH, db_path) engine = create_engine(f"sqlite:///{db_path.as_posix()}", connect_args={"check_same_thread": False}) Base.metadata.create_all(engine) return engine, sessionmaker(bind=engine, expire_on_commit=False) def _bind(Session): def override(): s = Session() try: yield s finally: s.close() app.dependency_overrides[project_router.get_db] = override def _clear(): app.dependency_overrides.pop(project_router.get_db, None) def _seed_project(Session): with Session() as s: p = Project(name="KPI collapse", status="active") s.add(p) s.flush() doc = BoqDocument(project_id=p.id, name="BOQ") s.add(doc) s.flush() wi = WorkItem(project_id=p.id, boq_document_id=doc.id, name="W", unit="шт", quantity=Decimal("100"), unit_price=Decimal("10"), amount=Decimal("1000")) s.add(wi) s.commit() return p.id def test_kpi_block_exists(tmp_path): engine, Session = _make_session(tmp_path) _bind(Session) try: pid = _seed_project(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}").text assert "План и выполнение" in html assert "Плановая стоимость" in html assert "Остаток" in html assert "Выполнение" in html # Оплата по КС-3 inside the same details container assert "Оплата по КС-3" in html assert "Всего КС-3" in html assert "Задолженность" in html finally: _clear() engine.dispose() def test_payments_inside_collapse_container(tmp_path): """Оплата по КС-3 must be inside the
KPI container.""" engine, Session = _make_session(tmp_path) _bind(Session) try: pid = _seed_project(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}").text # Extract the details block import re details_match = re.search(r']*>(.*?)
', html, re.S) assert details_match block = details_match.group(1) # Both KPI sections inside assert "План и выполнение" in block or "Плановая стоимость" in block assert "Оплата по КС-3" in block assert "Всего КС-3" in block assert "Задолженность" in block finally: _clear() engine.dispose() def test_collapsed_hides_payment_kpi(tmp_path): """Default collapsed state hides payment KPI cards from view.""" engine, Session = _make_session(tmp_path) _bind(Session) try: pid = _seed_project(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}").text #
is closed (no open attr) — payment section is inside it details_tag = re.search(r']*>', html) assert details_tag assert "open" not in details_tag.group(0) # Payment KPI is inside details (already verified above) # No separate standalone payment-heading outside details outside = html.split("
")[-1] if "" in html else html assert "Оплата по КС-3" not in outside or "Оплата по КС-3" in html finally: _clear() engine.dispose() def test_kpi_collapsed_by_default(tmp_path): engine, Session = _make_session(tmp_path) _bind(Session) try: pid = _seed_project(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}").text #
without open attribute = collapsed by default match = re.search(r']*>', html) assert match assert "open" not in match.group(0) finally: _clear() engine.dispose() def test_kpi_uses_details_summary(tmp_path): engine, Session = _make_session(tmp_path) _bind(Session) try: pid = _seed_project(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}").text assert "]*>(.*?)', html, re.S) assert summary_match assert "План и выполнение" in summary_match.group(1) finally: _clear() engine.dispose() def test_kpi_values_present_and_unchanged(tmp_path): engine, Session = _make_session(tmp_path) _bind(Session) try: pid = _seed_project(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}").text # All four KPI values present assert "Плановая стоимость" in html assert "Выполнено по Daily Reports" in html assert "Остаток" in html assert "Выполнение" in html # Money values (may be hidden by eye toggle but present in DOM) assert "1 000" in html or "1000" in html or "1,000" in html finally: _clear() engine.dispose() def test_eye_toggle_works_collapsed(tmp_path): engine, Session = _make_session(tmp_path) _bind(Session) try: pid = _seed_project(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}").text # money-hidden class on body = collapsed KPI + hidden money assert "money-hidden" in html assert "toggleMoney" in html assert "classList.toggle('money-hidden')" in html finally: _clear() engine.dispose() def test_eye_toggle_works_expanded(tmp_path): """The eye toggle mechanism is independent of details/summary state.""" engine, Session = _make_session(tmp_path) _bind(Session) try: pid = _seed_project(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}").text # toggleMoney button exists and works regardless of details state assert "toggleMoney" in html # KPI block is in the same page as eye toggle assert "money-value" in html assert "