"""Unit tests for the pillar-check result types — no browser.

The browser-driving halves (``audit_layout``, ``scan_a11y``, ``watch_health``,
``capture``) are exercised by the tier-3 e2e suite; here we pin the pure result
types and their assertion-message formatting.
"""

from __future__ import annotations

import dataclasses

import pytest

from mgf.uitest_web import HealthWatch, LayoutReport
from mgf.uitest_web.checks import DEFAULT_A11Y_IMPACTS, describe_a11y


def test_layout_report_ok_when_clean() -> None:
    r = LayoutReport(
        viewport_width=1280, document_scroll_width=1280, horizontal_scroll=False, violations=[]
    )
    assert r.ok
    assert "layout ok" in r.describe()


def test_layout_report_not_ok_and_describes_the_violation() -> None:
    r = LayoutReport(
        viewport_width=390,
        document_scroll_width=572,
        horizontal_scroll=True,
        violations=[
            {
                "kind": "viewport-overflow-x",
                "tag": "div",
                "cls": "topbar",
                "right": 572,
                "viewport": 390,
            }
        ],
    )
    assert not r.ok
    desc = r.describe()
    assert "FAILED" in desc
    assert "topbar" in desc


def test_layout_report_describes_a_text_overflow() -> None:
    r = LayoutReport(
        viewport_width=390,
        document_scroll_width=390,
        horizontal_scroll=False,
        violations=[
            {
                "kind": "text-overflow",
                "tag": "span",
                "cls": "label",
                "text": "toolong",
                "scrollWidth": 200,
                "clientWidth": 40,
                "scrollHeight": 20,
                "clientHeight": 20,
            }
        ],
    )
    assert "overflows its box" in r.describe()


def test_layout_report_is_frozen() -> None:
    r = LayoutReport(
        viewport_width=1, document_scroll_width=1, horizontal_scroll=False, violations=[]
    )
    with pytest.raises(dataclasses.FrozenInstanceError):
        r.viewport_width = 2  # type: ignore[misc]


def test_health_watch_ok_then_flags_an_http_error() -> None:
    w = HealthWatch()
    assert w.ok
    assert "ok" in w.describe()
    w.error_responses.append({"url": "/global-tokens.css", "status": 404})
    assert not w.ok
    assert "404" in w.describe()


def test_describe_a11y_empty_and_populated() -> None:
    assert "no accessibility" in describe_a11y([])
    violations = [
        {
            "impact": "serious",
            "id": "color-contrast",
            "help": "fix contrast",
            "nodes": [{"target": ["summary"]}],
        }
    ]
    out = describe_a11y(violations)
    assert "color-contrast" in out
    assert "serious" in out


def test_default_a11y_impacts() -> None:
    assert DEFAULT_A11Y_IMPACTS == ("serious", "critical")
