"""Tests on Lab 10's recorded run (October 2026). The revert counts come from three public repos
(results/02_metrics.out.txt); the weekly series are the simulated team on the lab's dashboard
(simteam seed 0, results/dashboard.html), where half the reviewers start rubber-stamping in week 13.
Every reviewer in that team is an AI stand-in. Run: pytest test_metrics_practice.py"""
from metrics_practice import first_notice, revert_target, shifted, silent_approval, wilson

SWITCH = 13
# One simulated team, weeks 0-25. Counts are out of 60 PRs a week.
TTM_H = [5.42, 5.85, 6.29, 7.3, 5.56, 4.8, 6.33, 5.89, 5.48, 5.28, 8.18, 5.48, 5.33,
         5.44, 3.76, 5.5, 3.01, 3.89, 3.93, 3.68, 4.33, 3.97, 3.82, 3.25, 4.19, 3.58]
TTFR_H = [2.59, 2.03, 2.33, 3.07, 2.62, 2.51, 3.64, 2.97, 2.88, 2.27, 3.58, 2.12, 3.2,
          3.29, 2.81, 3.62, 2.75, 3.33, 1.93, 2.56, 2.83, 2.37, 2.7, 2.84, 3.54, 2.43]
ROUNDS = [x / 60 for x in [96, 95, 98, 96, 95, 95, 93, 101, 99, 90, 100, 93, 88,
                           85, 78, 81, 76, 74, 82, 78, 83, 76, 79, 71, 78, 82]]
SILENT = [x / 60 for x in [24, 24, 21, 22, 24, 25, 26, 19, 21, 30, 20, 27, 30,
                           34, 42, 39, 44, 46, 37, 42, 37, 43, 41, 48, 41, 37]]
ACTED = [0.432, 0.424, 0.359, 0.314, 0.387, 0.378, 0.485, 0.4, 0.529, 0.375, 0.294, 0.297, 0.281,
         0.231, 0.419, 0.25, 0.243, 0.275, 0.25, 0.417, 0.286, 0.216, 0.214, 0.286, 0.474, 0.35]
# The truth no dashboard shows: PRs whose planted bug got past everything, per week.
ESCAPED = [0, 1, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 2,
           1, 0, 0, 2, 1, 0, 0, 2, 3, 1, 3, 1, 0]


def test_silent_approval():
    ben_ok = ("ben", "human", "APPROVED")
    assert silent_approval([ben_ok], "ana")
    assert not silent_approval([("ben", "human", "comment"), ben_ok], "ana")
    assert silent_approval([("coderabbitai", "ai", "thread"), ben_ok], "ana"), "an AI thread doesn't count"
    assert silent_approval([("ana", "human", "comment"), ben_ok], "ana"), "the author talking doesn't count"
    assert not silent_approval([("ben", "human", "COMMENTED")], "ana"), "no approval, nothing silent"
    assert not silent_approval([("cy", "human", "thread"), ben_ok], "ana")


def test_revert_target_by_reference():
    for body in ("Reverts o/r#12", "Reverts #12", "Standard `git revert` of #12", "revert of #12",
                 "Reverts https://github.com/o/r/pull/12"):
        assert revert_target("Undo the change", body, {}) == 12, body
    assert revert_target("fix: revert #8365", "", {}) == 8365


def test_revert_target_by_title():
    titles = {"Add x": 1, "Add y": 2}
    assert revert_target('Revert "Add y"', "", titles) == 2
    assert revert_target("chore: revert stripe whitelisting", "", titles) is None, "a revert, target unknown"
    assert revert_target("stop search reverting to a stale value", "", titles) is None, "not a revert"


def test_wilson_on_the_real_reverts():
    lo, hi = wilson(6, 1762)                 # infisical: 6 reverts in 1,762 merged PRs
    assert (round(lo * 100, 1), round(hi * 100, 1)) == (0.2, 0.7)
    lo, hi = wilson(0, 1578)                 # formbricks: none
    assert lo == 0.0 and round(hi * 100, 1) == 0.2
    lo, hi = wilson(7, 4213)                 # all three repos
    assert (round(lo * 100, 1), round(hi * 100, 1)) == (0.1, 0.3)
    # PRs with no AI thread vs AI threads mostly acted on: 1 of 1,530 vs 2 of 1,316. The intervals overlap.
    a, b = wilson(1, 1530), wilson(2, 1316)
    assert a[1] > b[0] and b[1] > a[0]
    assert wilson(0, 0) == (0.0, 1.0)


def test_shifted():
    assert shifted(TTM_H[:SWITCH], TTM_H[SWITCH:]) == -1
    assert shifted(SILENT[:SWITCH], SILENT[SWITCH:]) == 1
    assert shifted(TTFR_H[:SWITCH], TTFR_H[SWITCH:]) == 0
    assert shifted([1.0, 1.0], [1.0]) == 0 and shifted([1.0, 1.0], [2.0]) == 1
    try:
        shifted([1.0], [2.0])
    except ValueError:
        pass
    else:
        raise AssertionError("one before-week should raise ValueError")


def test_first_notice_on_the_rubber_stamping_team():
    assert first_notice(ROUNDS, SWITCH) == 1
    assert first_notice(SILENT, SWITCH) == 1
    assert first_notice(TTM_H, SWITCH) == 4
    assert first_notice(ACTED, SWITCH) == 5
    assert first_notice(TTFR_H, SWITCH) is None, "rubber-stampers wait as long to pick a PR up"
    # Even the truth, 7 escaped bugs before and 14 after, doesn't clear the rule for one team.
    assert sum(ESCAPED[:SWITCH]) == 7 and sum(ESCAPED[SWITCH:]) == 14
    assert first_notice(ESCAPED, SWITCH) is None
