"""Tests for UI-033: BOQ progress states, filter, and overflow.""" from __future__ import annotations from datetime import date from decimal import Decimal import pytest from fastapi.testclient import TestClient from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.pool import StaticPool from app.database.database import Base from app.main import app from app.models import ( # noqa: F401 agreement, boq, daily_report, document_artifact, ks2, ks3, payment, project, work_journal, ) from app.models.boq import BoqDocument, Section, System, WorkItem from app.models.project import Project from app.routers import boq as boq_router, projects as project_router @pytest.fixture def db_session(): engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine, expire_on_commit=False) session = Session() try: yield session finally: session.close() engine.dispose() @pytest.fixture def ctx(db_session): """Create a BOQ with items in every progress state.""" p = Project(name="UI-033") db_session.add(p) db_session.flush() doc = BoqDocument(project_id=p.id, name="BOQ") db_session.add(doc) db_session.flush() sys1 = System(project_id=p.id, name="Система А", sort_order=1) db_session.add(sys1) db_session.flush() sec1 = Section(system_id=sys1.id, name="Раздел 1", sort_order=1) db_session.add(sec1) db_session.flush() # not-started: qty=100, done=0 wi1 = WorkItem( project_id=p.id, boq_document_id=doc.id, system_id=sys1.id, section_id=sec1.id, source_number="1", name="Не начато", unit="шт", quantity=Decimal("100"), unit_price=Decimal("10"), amount=Decimal("1000"), ) # in-progress: qty=100, done=50 wi2 = WorkItem( project_id=p.id, boq_document_id=doc.id, system_id=sys1.id, section_id=sec1.id, source_number="2", name="В работе", unit="шт", quantity=Decimal("100"), unit_price=Decimal("10"), amount=Decimal("1000"), ) # completed: qty=100, done=100 wi3 = WorkItem( project_id=p.id, boq_document_id=doc.id, system_id=sys1.id, section_id=sec1.id, source_number="3", name="Выполнено", unit="шт", quantity=Decimal("100"), unit_price=Decimal("10"), amount=Decimal("1000"), ) # overspend: qty=100, done=150 wi4 = WorkItem( project_id=p.id, boq_document_id=doc.id, system_id=sys1.id, section_id=sec1.id, source_number="4", name="Перерасход", unit="шт", quantity=Decimal("100"), unit_price=Decimal("10"), amount=Decimal("1000"), ) # plan=0: qty=0 wi5 = WorkItem( project_id=p.id, boq_document_id=doc.id, system_id=sys1.id, section_id=sec1.id, source_number="5", name="План 0", unit="шт", quantity=Decimal("0"), unit_price=Decimal("10"), amount=Decimal("0"), ) db_session.add_all([wi1, wi2, wi3, wi4, wi5]) db_session.commit() return { "pid": p.id, "doc_id": doc.id, "wi1": wi1.id, "wi2": wi2.id, "wi3": wi3.id, "wi4": wi4.id, "wi5": wi5.id, } def _override(db_session): def override(): yield db_session app.dependency_overrides[boq_router.get_db] = override app.dependency_overrides[project_router.get_db] = override def _clear(): app.dependency_overrides.clear() def _get_html(db_session, ctx): _override(db_session) try: client = TestClient(app) resp = client.get(f"/boq/{ctx['pid']}/documents/{ctx['doc_id']}") assert resp.status_code == 200 return resp.text finally: _clear() # --------------------------------------------------------------------------- # 1. Four progress states # --------------------------------------------------------------------------- def test_four_progress_states_css(db_session, ctx, monkeypatch): """Inject completed quantities so all four states appear.""" import app.routers.boq as boq_mod monkeypatch.setattr(boq_mod, "_completed_quantities", lambda db, pid, items: { ctx["wi1"]: Decimal("0"), ctx["wi2"]: Decimal("50"), ctx["wi3"]: Decimal("100"), ctx["wi4"]: Decimal("150"), ctx["wi5"]: Decimal("0"), }) html = _get_html(db_session, ctx) # data-progress-state attributes assert 'data-progress-state="not-started"' in html assert 'data-progress-state="in-progress"' in html assert 'data-progress-state="completed"' in html assert 'data-progress-state="overspend"' in html # CSS classes on progress bars assert "bg-secondary" in html # not-started assert "bg-primary" in html # in-progress assert "bg-success" in html # completed assert "bg-danger" in html # overspend # Overspend text assert "Перерасход" in html def test_no_warning_state(db_session, ctx, monkeypatch): """The old bg-warning (50% threshold) should be gone.""" import app.routers.boq as boq_mod monkeypatch.setattr(boq_mod, "_completed_quantities", lambda db, pid, items: { ctx["wi2"]: Decimal("50"), }) html = _get_html(db_session, ctx) assert "bg-warning" not in html # --------------------------------------------------------------------------- # 2. Legend # --------------------------------------------------------------------------- def test_legend_present(db_session, ctx): html = _get_html(db_session, ctx) assert "Выполнение:" in html assert "Не начато" in html assert "В работе" in html assert "Выполнено" in html assert "Перерасход" in html # CSS dots assert "dot-not-started" in html assert "dot-in-progress" in html assert "dot-completed" in html assert "dot-overspend" in html # --------------------------------------------------------------------------- # 3. Filter buttons # --------------------------------------------------------------------------- def test_filter_buttons_present(db_session, ctx): html = _get_html(db_session, ctx) assert 'data-filter="all"' in html assert 'data-filter="not-started"' in html assert 'data-filter="in-progress"' in html assert 'data-filter="completed"' in html assert 'data-filter="overspend"' in html # --------------------------------------------------------------------------- # 4. plan=0 # --------------------------------------------------------------------------- def test_plan_zero_no_division_error(db_session, ctx, monkeypatch): """plan=0 must render without error and without progress bar.""" import app.routers.boq as boq_mod monkeypatch.setattr(boq_mod, "_completed_quantities", lambda db, pid, items: {}) html = _get_html(db_session, ctx) # Page renders successfully (already asserted in _get_html) # plan=0 item has no progress bar (empty td) assert "План 0" in html # --------------------------------------------------------------------------- # 5. Expand/collapse preserved # --------------------------------------------------------------------------- def test_expand_collapse_buttons(db_session, ctx): html = _get_html(db_session, ctx) assert 'id="expandAll"' in html assert 'id="collapseAll"' in html assert "data-toggle=" in html assert "data-parent=" in html # --------------------------------------------------------------------------- # 6. Eye toggle preserved # --------------------------------------------------------------------------- def test_eye_toggle_present(db_session, ctx): html = _get_html(db_session, ctx) assert 'id="toggleMoney"' in html assert "money-hidden" in html assert "money-value" in html # --------------------------------------------------------------------------- # 7. Sticky header preserved # --------------------------------------------------------------------------- def test_sticky_header_css(db_session, ctx): html = _get_html(db_session, ctx) assert "position: sticky" in html assert "boq-table-wrap" in html # --------------------------------------------------------------------------- # UI-033-FIX: overflow regression # --------------------------------------------------------------------------- def test_header_buttons_wrap_on_narrow(db_session, ctx): """Header buttons must wrap (flex-wrap) to prevent page overflow on narrow viewports.""" html = _get_html(db_session, ctx) # Parent flex container must have flex-wrap assert 'd-flex flex-wrap justify-content-between align-items-start' in html # Button container must have flex-wrap assert 'd-flex flex-wrap gap-2' in html def test_no_body_overflow_x_hidden(db_session, ctx): """Must NOT use overflow-x: hidden to mask overflow.""" html = _get_html(db_session, ctx) assert "overflow-x: hidden" not in html assert "overflow-x:hidden" not in html def test_table_wrap_has_overflow_auto(db_session, ctx): """Table overflow must be localized in the wrap container.""" html = _get_html(db_session, ctx) assert "overflow: auto" in html # --------------------------------------------------------------------------- # 5. All five filter modes (JS infrastructure) # --------------------------------------------------------------------------- def test_filter_js_handles_all_states(db_session, ctx): """applyFilter must handle 'all' and four progress states via data-progress-state.""" html = _get_html(db_session, ctx) assert "applyFilter" in html assert "data-progress-state" in html assert "filtered-out" in html # The five button data-filter values must match state names used in Jinja for f in ("all", "not-started", "in-progress", "completed", "overspend"): assert f'data-filter="{f}"' in html # Jinja produces all four state values assert "not-started" in html assert "in-progress" in html assert "completed" in html assert "overspend" in html # --------------------------------------------------------------------------- # 6. Hide empty Section # --------------------------------------------------------------------------- def test_hide_empty_section_logic(db_session, ctx): """JS must hide section headers when no items match the filter.""" html = _get_html(db_session, ctx) assert "group-hidden" in html # Section headers have data-toggle^="section-" and items use data-parent assert 'data-toggle="section-' in html assert 'data-parent="section-' in html # JS queries sections and toggles group-hidden assert '[data-toggle^="section-"]' in html assert "group-hidden" in html # --------------------------------------------------------------------------- # 7. Hide empty System # --------------------------------------------------------------------------- def test_hide_empty_system_logic(db_session, ctx): """JS must hide system headers when no visible sections remain.""" html = _get_html(db_session, ctx) # System headers have data-toggle^="system-" assert 'data-toggle="system-' in html assert '[data-toggle^="system-"]' in html # Systems are hidden when all their sections are group-hidden assert "group-hidden" in html # --------------------------------------------------------------------------- # 10. Eye toggle independence # --------------------------------------------------------------------------- def test_filter_independent_of_eye_toggle(db_session, ctx): """Filter JS must not depend on money-hidden / toggleMoney state.""" html = _get_html(db_session, ctx) # Extract the filter IIFE section (after applyFilter definition) filter_start = html.find("applyFilter") assert filter_start > 0 filter_js = html[filter_start:] # Filter logic must NOT reference financial privacy classes assert "money-hidden" not in filter_js assert "toggleMoney" not in filter_js assert "money-value" not in filter_js # --------------------------------------------------------------------------- # UI-033-FIX2: table width regression # --------------------------------------------------------------------------- def test_microstat_label_allows_wrap(db_session, ctx): """microstat-label must NOT use white-space: nowrap — it caused table overflow.""" html = _get_html(db_session, ctx) # Find the microstat-label CSS rule import re m = re.search(r'\.microstat-label\s*\{([^}]+)\}', html) assert m, "microstat-label CSS rule not found" rule = m.group(1) assert "nowrap" not in rule, "microstat-label must not have white-space: nowrap" def test_table_wrap_structure_preserved(db_session, ctx): """Table wrap structure: overflow auto, table-layout fixed, sticky thead.""" html = _get_html(db_session, ctx) assert "boq-table-wrap" in html assert "overflow: auto" in html assert "table-layout: fixed" in html assert "position: sticky" in html def test_long_text_wraps(db_session, ctx): """Table cells must allow text wrapping (overflow-wrap, no nowrap on text cells).""" html = _get_html(db_session, ctx) import re # td/th overflow-wrap: anywhere allows long text to break m = re.search(r'\.boq-table td,\s*\.boq-table th\s*\{([^}]+)\}', html) assert m, "boq-table td/th rule not found" assert "anywhere" in m.group(1) # money-value uses nowrap (acceptable for short currency strings) assert "white-space: nowrap" in html def test_microstat_bar_min_width_reasonable(db_session, ctx): """microstat-bar min-width must not force column wider than allocated (11rem = 176px).""" html = _get_html(db_session, ctx) import re m = re.search(r'\.microstat-bar\s*\{([^}]+)\}', html) assert m, "microstat-bar CSS rule not found" rule = m.group(1) # min-width must be less than 11rem (176px) — 6rem = 96px is fine assert "min-width: 6rem" in rule or "min-width:0" in rule.replace(" ", "")