"""Layout tests for task 024-FIX: payment card adaptive layout.""" import re import shutil 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.payment import Payment, PaymentAllocation from app.routers import payments as payments_router def _make_session(tmp_path): db_path = tmp_path / "t024fix.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) def _bind(Session): def override_db(): s = Session() try: yield s finally: s.close() app.dependency_overrides[payments_router.get_db] = override_db def _clear(): app.dependency_overrides.pop(payments_router.get_db, None) def test_cancelled_payment_card_layout(tmp_path): from datetime import date from decimal import Decimal engine, Session = _make_session(tmp_path) _bind(Session) try: with Session() as s: p = Project(name="Fix layout") s.add(p) s.flush() pay = Payment(project_id=p.id, date=date(2026, 1, 1), amount=Decimal("100"), currency="RUB", status="cancelled") s.add(pay) s.flush() s.add(PaymentAllocation(payment_id=pay.id, ks3_id=1, amount=Decimal("40"))) s.commit() pid, pay_id = p.id, pay.id with TestClient(app) as client: html = client.get(f"/projects/{pid}/payments/{pay_id}").text # Alert wraps and cannot overflow assert "payment-storno-alert" in html assert "overflow-wrap: anywhere" in html assert "white-space: normal" in html # Requisites use adaptive Bootstrap grid assert re.search(r'class="col-12 col-sm-6 col-lg-4"', html) assert "text-break" in html # Labels as separate blocks for label in ("Сумма", "Распределено", "Не распределено"): assert f'>{label}' in html # History table unchanged assert "Исторические распределения по КС-3" in html # Back button is FIRST in the actions row (left-aligned) actions = re.search(r'class="d-flex flex-wrap gap-2 payment-actions"(.*?)', html, re.S) assert actions, "payment-actions block missing" block = actions.group(1) assert "Назад к списку" in block assert block.index("Назад к списку") < block.index("недоступны") assert "justify-content-between" not in block assert "justify-content: flex-start" in html or "justify-content-flex-start" in html # Alert structure: paragraphs + container constraints assert "payment-storno-alert" in html assert "min-width: 0" in html assert "max-width: 100%" in html assert "white-space: normal" in html assert 'class="payment-view"' in html finally: _clear() engine.dispose() def test_active_payment_card_uses_same_layout(tmp_path): from datetime import date from decimal import Decimal engine, Session = _make_session(tmp_path) _bind(Session) try: with Session() as s: p = Project(name="Active layout") s.add(p) s.flush() s.add(Payment(project_id=p.id, date=date(2026, 1, 1), amount=Decimal("100"), currency="RUB", status="active")) s.commit() pid = p.id with TestClient(app) as client: rows = client.get(f"/projects/{pid}/payments").text # find payment id from DB with Session() as s: pay_id = s.query(Payment).filter_by(project_id=pid).one().id html = client.get(f"/projects/{pid}/payments/{pay_id}").text assert re.search(r'class="col-12 col-sm-6 col-lg-4"', html) assert "Сторнировать платёж" in html assert "Редактировать" in html assert "Назад к списку" in html finally: _clear() engine.dispose()