"""``mgf-fed app-doctor`` — the supervisor-coverage ↔ ``patch = ["subprocess"]``
coupling check (PAPER-122).

The install-state checklist gains one FAIL: a repo whose ``.woodpecker.yml`` runs
``coverage run … -m mgf.test_supervisor`` while its ``pyproject.toml`` lacks
``[tool.coverage.run] patch = ["subprocess"]`` reds CI with EVERY TEST PASSING.
The check is exercised directly (not through ``doctor()``) so a failure points at
the right line and the ambient environment's own extras can't colour the result.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from mgf.fed.cli._doctor import _FAIL, _check_supervisor_coverage_patch

if TYPE_CHECKING:
    from pathlib import Path

_PIPELINE = (
    "steps:\n"
    "  - name: test-3.12\n"
    "    commands:\n"
    "      - coverage run --source=src/mgf -m mgf.test_supervisor --tier 0-2\n"
    "      - coverage combine\n"
)

_PYPROJECT = '[project]\nname = "myapp"\nversion = "1.0.0"\n'
_PATCH_KEY = '[tool.coverage.run]\npatch = ["subprocess"]\n'


def _project(root: Path, *, pipeline: str | None, pyproject: str | None) -> None:
    if pyproject is not None:
        (root / "pyproject.toml").write_text(pyproject, encoding="utf-8")
    if pipeline is not None:
        (root / ".woodpecker.yml").write_text(pipeline, encoding="utf-8")


def test_fails_when_the_coupling_is_broken(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    _project(tmp_path, pipeline=_PIPELINE, pyproject=_PYPROJECT)
    monkeypatch.chdir(tmp_path)
    findings = list(_check_supervisor_coverage_patch())
    assert len(findings) == 1
    marker, msg, action = findings[0]
    assert marker == _FAIL
    assert "coverage coupling BROKEN" in msg
    assert action is not None
    assert 'patch = ["subprocess"]' in action


def test_silent_when_the_patch_key_is_present(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    _project(tmp_path, pipeline=_PIPELINE, pyproject=_PYPROJECT + _PATCH_KEY)
    monkeypatch.chdir(tmp_path)
    assert list(_check_supervisor_coverage_patch()) == []


def test_silent_when_the_pipeline_does_not_run_the_supervisor(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    _project(
        tmp_path,
        pipeline="steps:\n  - name: test\n    commands:\n      - pytest -q\n",
        pyproject=_PYPROJECT,
    )
    monkeypatch.chdir(tmp_path)
    assert list(_check_supervisor_coverage_patch()) == []


def test_silent_when_there_is_no_pipeline(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    _project(tmp_path, pipeline=None, pyproject=_PYPROJECT)
    monkeypatch.chdir(tmp_path)
    assert list(_check_supervisor_coverage_patch()) == []


def test_silent_on_an_unparseable_pyproject(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    # Never invent a failure from a file we could not read.
    _project(tmp_path, pipeline=_PIPELINE, pyproject="[project\nname = broken")
    monkeypatch.chdir(tmp_path)
    assert list(_check_supervisor_coverage_patch()) == []
