"""The ``[tool.mgf-fed]`` CI carve-out table reader — pure-dict checks, no sibling
templates required (unlike ``test_woodpecker``, these run in a single-repo CI clone
too — deliberately, since they include the §3.3 tombstone that keeps deleted config
knobs dead).
"""

from __future__ import annotations

import dataclasses

import pytest

from mgf.fed._woodpecker import WoodpeckerParams, _mgf_ci


def test_mgf_ci_reads_only_tool_mgf_fed(capsys: pytest.CaptureFixture[str]) -> None:
    """Carve-outs come from [tool.mgf-fed] only. The transitional [tool.mgf.ci]
    fallback was dropped once the fleet finished migrating — a legacy [tool.mgf.ci]
    is no longer read (and MUST NOT be, since it breaks MgfSettings)."""
    assert _mgf_ci({"tool": {"mgf-fed": {"coverage_floor": 90}}}) == {"coverage_floor": 90}
    # legacy [tool.mgf.ci] is NO LONGER read
    assert _mgf_ci({"tool": {"mgf": {"ci": {"coverage_floor": 80}}}}) == {}
    assert _mgf_ci({"tool": {}}) == {}
    # known keys (and mgf-standard's `policy` subtable, which belongs to
    # _template.load_policy) draw no unknown-key note.
    _mgf_ci({"tool": {"mgf-fed": {"vuln_ignore": [], "extra_index": True, "policy": {}}}})
    assert "not read by the CI renderer" not in capsys.readouterr().err


def test_mgf_ci_notes_dead_skip_editable(capsys: pytest.CaptureFixture[str]) -> None:
    """§3.3 tombstone: `skip_editable` was parsed-but-never-read and is DELETED —
    a repo still declaring it (or any unknown key) draws the stderr note naming the
    key, so the dead knob can never silently return. Never a hard failure (the
    render must stay usable): the known keys are still read."""
    table = _mgf_ci({"tool": {"mgf-fed": {"skip_editable": True, "coverage_floor": 90}}})
    assert table["coverage_floor"] == 90  # known keys still read
    err = capsys.readouterr().err
    assert "skip_editable" in err and "not read by the CI renderer" in err
    assert "--skip-editable is intrinsic" in err


def test_mgf_ci_notes_any_unknown_key_without_skip_editable_tail(
    capsys: pytest.CaptureFixture[str],
) -> None:
    """A typo'd key draws the generic note (no skip_editable tail)."""
    _mgf_ci({"tool": {"mgf-fed": {"vuln_ignores": ["CVE-1"]}}})
    err = capsys.readouterr().err
    assert "vuln_ignores" in err and "not read by the CI renderer" in err
    assert "intrinsic" not in err


def test_woodpecker_params_has_no_skip_editable_field() -> None:
    """The dead knob is gone from the params surface too (grep-proof companion)."""
    assert "skip_editable" not in {f.name for f in dataclasses.fields(WoodpeckerParams)}
