"""Tests on Lab 01's recorded run (code-review-labs, October 2026): change c20's batch-size line, the
734 mutants of 03_what_ci_catches.py (results/mutants.jsonl), and 04_corpus_budget.py's intervals for
173 planted bugs. Run: pytest test_mutants_practice.py"""
from mutants_practice import batch_sizes, bugs_needed, caught, survival_by, wilson

CRM_DEFAULT = 100  # the CRM destination's default batch size


def correct(setting):
    return setting or CRM_DEFAULT  # c20's line: the sync's own setting, or the default


def or_to_and(setting):
    return setting and CRM_DEFAULT  # the mutant "or -> and"


def deleted(setting):
    raise NameError("size")  # the mutant "delete statement": size is never assigned


def planted(setting):
    return min(setting, CRM_DEFAULT) if setting else CRM_DEFAULT  # planted bug c20-d1


def test_batch_sizes():
    assert batch_sizes(13, 4) == [4, 4, 4, 1]
    assert batch_sizes(13, 100) == [13]  # one big batch: what the or -> and mutant sends
    assert batch_sizes(200, 100) == [100, 100]
    assert batch_sizes(0, 4) == []
    assert max(batch_sizes(13, correct(4))) <= 4  # the PR's own test passes on the correct line


def test_caught_mutants_vs_planted_bug():
    own_test = [4]  # the PR's own test only tries a batch size of 4
    assert caught(correct, or_to_and, own_test)  # 4 and 100 is 100: CI turns red
    assert caught(correct, deleted, own_test)  # a crash counts as caught
    assert not caught(correct, planted, own_test)  # min(4, 100) is 4: CI stays green
    assert not caught(correct, planted, [None, 4, 50, 100])  # wrong only above the default
    assert caught(correct, planted, [4, 200])  # a test above the default would catch it
    assert planted(200) == 100 and correct(200) == 200
    assert not caught(correct, correct, [None, 4, 200])


def _rows():
    """The 734 mutants by package and outcome, as 03_what_ci_catches.py recorded them."""
    counts = {
        "api": {"own tests": 150, "rest of CI (typecheck)": 11, "rest of CI (test:api)": 9, None: 30},
        "web": {"own tests": 50, "rest of CI (typecheck)": 1, None: 33},
        "worker": {"own tests": 355, "rest of CI (test:worker)": 29, None: 66},
    }
    return [{"package": p, "caught_by": c} for p, by in counts.items() for c, n in by.items() for _ in range(n)]


def test_survival_by_package():
    rows = _rows()
    assert len(rows) == 734
    by = survival_by(rows, "package")
    assert by == {"api": (30, 200), "web": (33, 84), "worker": (66, 450)}
    assert sum(s for s, _ in by.values()) == 129  # almost 1 in 5 survive CI
    assert survival_by([], "package") == {}
    sizes = [{"size": "XS", "caught_by": "own tests"}] * 13 + [{"size": "S", "caught_by": None}] * 2
    assert survival_by(sizes, "size") == {"XS": (0, 13), "S": (2, 2)}


def _pct(interval):
    return tuple(round(x * 100) for x in interval)


def test_wilson():
    assert _pct(wilson(129, 734)) == (15, 20)  # mutants that survive CI
    assert _pct(wilson(33, 84)) == (30, 50)  # the web app
    assert _pct(wilson(30, 200)) == (11, 21)  # the API
    assert _pct(wilson(104, 173)) == (53, 67)  # a reviewer who finds 60% of 173 bugs
    assert _pct(wilson(7, 11)) == (35, 85)  # 60% of the 11 weakened-test bugs: 50 points wide
    assert _pct(wilson(19, 31)) == (44, 76)  # the biggest kind, logic
    assert wilson(0, 10)[0] == 0.0 and wilson(10, 10)[1] == 1.0
    assert wilson(0, 0) == (0.0, 1.0)


def test_bugs_needed():
    assert bugs_needed(0.6, 0.10) == 89
    assert bugs_needed(0.6, 0.07) == 185  # 173 gives ±7.2 points, just over
    assert bugs_needed(0.6, 0.05) == 366  # halving the range takes about four times the bugs
    assert bugs_needed(0.6, 0.03) == 1021
