"""Regression tests for task 023-FIX: UI/UX follow-up after visual review.""" import re import shutil from datetime import date from decimal import Decimal 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, System, Section, WorkItem from app.models.agreement import Agreement from app.models.ks2 import KS2 from app.models.ks3 import KS3 from app.models.payment import Payment from app.routers import ( projects as project_router, boq as boq_router, agreements as agreement_router, daily_reports, ks2 as ks2_router, ks3 as ks3_router, payments as payments_router, ) def _make_session(tmp_path): db_path = tmp_path / "t023fix-copy.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(bind=engine) return engine, sessionmaker(bind=engine, autoflush=False, autocommit=False) ALL_DEPS = ( project_router.get_db, boq_router.get_db, agreement_router.get_db, daily_reports.get_db, ks2_router.get_db, ks3_router.get_db, payments_router.get_db, ) def _bind_session(Session, *deps): def override_db(): s = Session() try: yield s finally: s.close() for dep in deps: app.dependency_overrides[dep] = override_db def _clear(*deps): for dep in deps: app.dependency_overrides.pop(dep, None) # 1. Document tree has both entry points def test_document_tree_has_boq_and_agreements_buttons(tmp_path): engine, Session = _make_session(tmp_path) _bind_session(Session, *ALL_DEPS) try: with Session() as s: p = Project(name="Tree buttons") s.add(p) s.flush() doc = BoqDocument(project_id=p.id, name="Ведомость") s.add(doc) s.flush() agr = Agreement(project_id=p.id, number="A-1", date=date(2026, 1, 1), title="ДС", status="draft") s.add(agr) s.commit() pid, doc_id, agr_id = p.id, doc.id, agr.id with TestClient(app) as client: html = client.get(f"/projects/{pid}").text # Both buttons present in the document-tree card header tree_block = html.split("Дерево документов", 1)[-1].split("", 1)[0] assert f'href="/boq/{pid}"' in tree_block assert f'href="/projects/{pid}/agreements"' in tree_block assert "Дополнительные соглашения" in tree_block assert "Ведомости" in tree_block finally: _clear(*ALL_DEPS) engine.dispose() # 2. Import requisites are on the initial import form and pass through def test_import_form_shows_all_four_requisites(tmp_path): engine, Session = _make_session(tmp_path) _bind_session(Session, *ALL_DEPS) try: with Session() as s: p = Project(name="Import form", contract_number="PC-1") s.add(p) s.commit() pid = p.id with TestClient(app) as client: html = client.get(f"/projects/{pid}/import").text assert 'name="document_name"' in html assert 'name="document_number"' in html assert 'name="document_date"' in html assert 'name="contract"' in html assert "Реквизиты ведомости" in html finally: _clear(*ALL_DEPS) engine.dispose() def test_import_requisites_pass_upload_to_confirm(tmp_path): import openpyxl wb = openpyxl.Workbook() ws = wb.active ws.title = "Лист А" for _ in range(4): ws.append([None]) ws.append(["№", "Наименование", "Модель", "Производитель", "Ед", "Кол-во", "Цена", "Сумма"]) ws.append([1, "Работа", None, None, "шт", 1, 5, 5]) upload_dir = tmp_path / "uploads" upload_dir.mkdir() wb.save(upload_dir / "file.xlsx") engine, Session = _make_session(tmp_path) _bind_session(Session, *ALL_DEPS) try: with Session() as s: p = Project(name="Flow") s.add(p) s.commit() pid = p.id import app.routers.boq as boq_mod old_upload = boq_mod.UPLOAD_DIR boq_mod.UPLOAD_DIR = upload_dir try: with TestClient(app) as client: # upload carries requisites into sheet selection with open(upload_dir / "file.xlsx", "rb") as fh: up = client.post( f"/boq/{pid}/import/upload", files={"file": ("file.xlsx", fh, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")}, data={ "document_name": "Ведомость А", "document_number": "N-1", "document_date": "2026-04-01", "contract": "Д-9", }, ) assert up.status_code == 200 assert 'value="Ведомость А"' in up.text assert 'value="N-1"' in up.text assert 'value="Д-9"' in up.text # preview shows requisites prev = client.post(f"/boq/{pid}/import/preview", data={ "filename": "file.xlsx", "sheet_name": "Лист А", "header_row": "5", "start_row": "6", "document_name": "Ведомость А", "document_number": "N-1", "document_date": "2026-04-01", "contract": "Д-9", }) assert prev.status_code == 200 assert "Реквизиты ведомости" in prev.text assert "Ведомость А" in prev.text assert "N-1" in prev.text # confirm saves conf = client.post(f"/boq/{pid}/import/confirm", data={ "filename": "file.xlsx", "sheet_name": "Лист А", "header_row": "5", "start_row": "6", "document_name": "Ведомость А", "document_number": "N-1", "document_date": "2026-04-01", "contract": "Д-9", }) assert conf.status_code == 200 assert "Ведомость А" in conf.text finally: boq_mod.UPLOAD_DIR = old_upload with Session() as s: doc = s.query(BoqDocument).filter_by(project_id=pid).one() assert doc.name == "Ведомость А" assert doc.document_number == "N-1" assert doc.document_date == date(2026, 4, 1) assert doc.contract == "Д-9" assert doc.display_name == "Ведомость А" finally: _clear(*ALL_DEPS) engine.dispose() # 3. display_name used in UI def test_display_name_used_in_agreement_scope(tmp_path): engine, Session = _make_session(tmp_path) _bind_session(Session, *ALL_DEPS) try: with Session() as s: p = Project(name="DN") s.add(p) s.flush() doc = BoqDocument(project_id=p.id, name="BOQ Import - Ведомость ОВ") s.add(doc) s.commit() pid = p.id with TestClient(app) as client: html = client.get(f"/projects/{pid}/agreements/new").text assert "Ведомость ОВ" in html assert "BOQ Import" not in html finally: _clear(*ALL_DEPS) engine.dispose() # 4-6. Status in KS-2 / KS-3 / Payments lists def _seed_ks_and_payments(Session): with Session() as s: p = Project(name="Status lists") 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", quantity=Decimal("1"), amount=Decimal("10")) s.add(wi) s.flush() ks2_draft = KS2(project_id=p.id, number="K2-1", date=date(2026, 1, 1), status="draft") ks2_issued = KS2(project_id=p.id, number="K2-2", date=date(2026, 1, 2), status="issued") ks2_cancelled = KS2(project_id=p.id, number="K2-3", date=date(2026, 1, 3), status="cancelled") s.add_all([ks2_draft, ks2_issued, ks2_cancelled]) s.flush() ks3_draft = KS3(project_id=p.id, number="K3-1", date=date(2026, 2, 1), status="draft") ks3_issued = KS3(project_id=p.id, number="K3-2", date=date(2026, 2, 2), status="issued") ks3_cancelled = KS3(project_id=p.id, number="K3-3", date=date(2026, 2, 3), status="cancelled") s.add_all([ks3_draft, ks3_issued, ks3_cancelled]) s.flush() pay_active = Payment(project_id=p.id, date=date(2026, 3, 1), amount=Decimal("5"), currency="RUB", status="active") pay_cancelled = Payment(project_id=p.id, date=date(2026, 3, 2), amount=Decimal("5"), currency="RUB", status="cancelled") s.add_all([pay_active, pay_cancelled]) s.commit() return p.id def test_ks2_list_shows_all_statuses(tmp_path): engine, Session = _make_session(tmp_path) _bind_session(Session, *ALL_DEPS) try: pid = _seed_ks_and_payments(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}/ks2").text assert "Черновик" in html assert "Проведён" in html assert "Сторнирован" in html assert "badge" in html assert "bg-danger" in html assert "bg-success" in html assert "bg-secondary" in html # row-click preserved assert "clickable-row" in html finally: _clear(*ALL_DEPS) engine.dispose() def test_ks3_list_shows_all_statuses(tmp_path): engine, Session = _make_session(tmp_path) _bind_session(Session, *ALL_DEPS) try: pid = _seed_ks_and_payments(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}/ks3").text assert "Черновик" in html assert "Проведён" in html assert "Сторнирован" in html assert "badge" in html assert "clickable-row" in html finally: _clear(*ALL_DEPS) engine.dispose() def test_payments_list_shows_status(tmp_path): engine, Session = _make_session(tmp_path) _bind_session(Session, *ALL_DEPS) try: pid = _seed_ks_and_payments(Session) with TestClient(app) as client: html = client.get(f"/projects/{pid}/payments").text assert "Сторнирован" in html assert "Активный" in html assert "Статус" in html assert "badge" in html assert "clickable-row" in html finally: _clear(*ALL_DEPS) engine.dispose() # 7. Existing tests + regressions: header layout, package button def test_project_header_layout_and_package_button(tmp_path): engine, Session = _make_session(tmp_path) _bind_session(Session, *ALL_DEPS) try: with Session() as s: p = Project(name="Header", status="active", contract_number="D-1", customer="Cust") s.add(p) s.commit() pid = p.id with TestClient(app) as client: html = client.get(f"/projects/{pid}").text # Top row: project name and primary actions assert "Дневной отчёт" in html assert "Редактировать" in html # Requisites readable assert "D-1" in html assert "Cust" in html assert "Активный" in html # Package button is not full-width / not stretched match = re.search(r'', html) assert match, "package button not found" assert "w-100" not in match.group(1) assert "w-auto" in match.group(1) assert "text-nowrap" in match.group(1) finally: _clear(*ALL_DEPS) engine.dispose() def test_eye_toggle_and_agreement_create_not_broken(tmp_path): engine, Session = _make_session(tmp_path) _bind_session(Session, *ALL_DEPS) try: with Session() as s: p = Project(name="Regressions") s.add(p) s.flush() BoqDocument(project_id=p.id, name="BOQ Import - Ведомость ОВ") s.commit() pid = p.id with TestClient(app) as client: page = client.get(f"/projects/{pid}") assert 'id="toggleMoney"' in page.text assert "money-hidden" in page.text form = client.get(f"/projects/{pid}/agreements/new") assert form.status_code == 200 created = client.post(f"/projects/{pid}/agreements", data={ "number": "A-1", "agreement_date": "2026-01-01", "title": "Тест", "description": "", "scope": "project", }, follow_redirects=False) assert created.status_code == 303 # agreement list row-click listing = client.get(f"/projects/{pid}/agreements") assert "clickable-row" in listing.text finally: _clear(*ALL_DEPS) engine.dispose()