"""The `Console` wrapper's contract, driven against a trivial static site (tier-2).

`Console` is the API a console's UI spec actually writes against — ``open()`` then
``assert_layout_clean()`` / ``assert_healthy()`` / ``snapshot()``. Proving it here
against a stdlib static server (rather than only inside a real console's specs)
means the library's own CI gate covers its own ergonomic surface, and a refactor
that blunts a gate fails here instead of in a consumer.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

if TYPE_CHECKING:
    from mgf.uitest_web import Console

pytestmark = pytest.mark.integration


def test_console_opens_and_reads_text(console: Console) -> None:
    response = console.open("/")
    assert response is not None
    console.assert_status(200)
    assert "Static console" in console.text()
    assert "Nothing spills here." in console.text("#hello")


def test_console_gates_pass_on_a_clean_page(console: Console) -> None:
    console.open("/")
    report = console.assert_layout_clean()
    assert report.ok
    console.assert_healthy()


def test_console_assert_ok_runs_every_gate(console: Console) -> None:
    console.open("/")
    console.assert_ok()  # status + layout + a11y + health, in one call


def test_console_audit_and_scan_return_reports(console: Console) -> None:
    console.open("/")
    assert console.audit_layout().ok
    assert console.scan_a11y() == []


def test_console_snapshot_writes_a_labelled_png(console: Console) -> None:
    console.open("/")
    path = console.snapshot("static overview")
    assert path.exists()
    assert path.stat().st_size > 0
    # name__viewport__theme.png — the gallery's matrix-cell label.
    assert path.name == "static-overview__desktop__light.png"


def test_console_assert_status_catches_a_wrong_code(console: Console) -> None:
    console.open("/nope-does-not-exist")
    with pytest.raises(AssertionError, match="expected HTTP 200"):
        console.assert_status(200)
