"""Tests for ``mgf-fed ledger-seed``.

The property that matters most is a NEGATIVE one: the seeder must never claim
conformance it did not assess. Marking the new rows ``N/A`` would go green
everywhere and be worse than the gap it closes — a false claim about 33 rules
across 43 repos, asserted by a script that assessed nothing.
"""

from __future__ import annotations

from mgf.fed.cli._ledger_seed import _SECTION, append_rows, missing_musts

_LEDGER = """\
# Conformance

## §Z Per-MUST evidenced audit

| Rule | Status | Evidence | Notes |
|---|---|---|---|
| AG-01 | ✅ | evidence | authored note |
"""


def test_missing_musts_names_only_what_the_ledger_lacks() -> None:
    assert missing_musts(_LEDGER, {"AG-01", "SEAT-01", "CRT-01"}) == ["CRT-01", "SEAT-01"]


def test_a_complete_ledger_has_nothing_missing() -> None:
    assert missing_musts(_LEDGER, {"AG-01"}) == []


def test_seeded_rows_default_to_pending_never_a_conformance_claim() -> None:
    """THE property. ⏳ is the only honest verdict a script can give."""
    out = append_rows(_LEDGER, ["SEAT-01"], "⏳")
    row = next(line for line in out.splitlines() if line.startswith("| SEAT-01 "))
    assert "⏳" in row
    assert "✅" not in row
    assert "not yet assessed" in row
    assert "not a conformance" in out  # the section says so in prose too


def test_the_authored_ledger_is_preserved_byte_for_byte() -> None:
    """Appended, never spliced. A rewriter that reaches into authored prose is how
    a tool destroys 295 lines it did not understand."""
    out = append_rows(_LEDGER, ["SEAT-01"], "⏳")
    assert out.startswith(_LEDGER)


def test_seeding_twice_reuses_the_section_instead_of_duplicating_it() -> None:
    once = append_rows(_LEDGER, ["SEAT-01"], "⏳")
    twice = append_rows(once, ["CRT-01"], "⏳")
    assert twice.count(_SECTION) == 1
    assert "| CRT-01 " in twice and "| SEAT-01 " in twice


def test_a_seeded_ledger_accounts_for_the_ids_it_was_given() -> None:
    """The point of the exercise: after seeding, DOC-15's question is answered."""
    ids = {"AG-01", "SEAT-01", "CRT-01"}
    out = append_rows(_LEDGER, missing_musts(_LEDGER, ids), "⏳")
    assert missing_musts(out, ids) == []


def test_an_explicit_reviewed_status_is_honoured() -> None:
    # --status exists for the case where a human actually decided.
    out = append_rows(_LEDGER, ["GUI-01"], "N/A")
    assert "| GUI-01 | N/A |" in out
