"""Tests on Lab 18's script 03, from its recorded run (October 2026): every test's pass/fail in 6 real
runs of the unchanged assistant on the 195-test suite, and 3 runs of each planted change. Runs 1-3 set the
rules; runs 4-6 play unchanged nights. Run: pytest test_qa_practice.py"""
from qa_practice import (alarm_line, down_up, mcnemar_p, paired_alarm, pass_rates, per_test_alarm,
                         reference, slip_rate, stable)

# One string per real run: "1" = the test passed, "0" = it failed.
RAW = {
    'baseline': [
        "111011111111111110011111110110111101111111111110101110111111111111101111111111111111111111111111111111011111111111011111111111111111111101111111011111001111011111111011111111111011111110101111111",
        "111011111111111111011111111111110111111111111111111110111111111111101111011111111111111111111111111111101111111111111111111111101111111111111111111111101111111111111111111111111011111110111111111",
        "111011111111111111111111111110101111111111111111111110111101111111101111111111111111111111111111111111111111111111110111101111111111110101111101111111101111111111111111111111111011111110111111111",
        "111001111111111111011111111111110111111111011111111110111111111111101111111111111101101111111111111111111110111111110111111111111111111111111111111111101111111111111111111111011011111110111111111",
        "111011111111110111011111111011111111111111111111111110111111111110100101111111111111101111111111110111111111111111111111111111111111101111111111111111111111111111111110101111001011110110101111110",
        "011011111111111111011111111111111001111111111111111110111111111111101111111111111101101001111111111111111111111111111111111111111111111111111111111111101111111111111111111111111011011110111111110",
    ],
    'corrupt 3%': [
        "111000111111111111011111111111010111111111011111111110111111111111101111111111111101101011101111111111111110111111110111111111111111111111111111111111111111111111111111111111011011111110111011111",
        "111010111111110111011111111011011111111111111111111110111111111110100101111111111111101011101111110111111111111111111111111111111111101111111111111111111111111111111110101111001011110110101111111",
        "011010111111111111011111111111011001111111111111111110111111111111101111111111111101101101101111111111111111111111111111111111111111111111111111111111111111111111111111111111111011011110111011111",
    ],
    'corrupt 10%': [
        "111000111111010111011111111011010111111111011111101110111111111110100111111111111101101001101111110111111110011111111011111111111111111111111111111111111111111111111111111111011011011110111011111",
        "110010111111010111011111111011011111111111011111101110111111111110100101111111111111110001101111110111101101011111101011111111111111111111111111111111111111111111111110111111011011011110101111111",
        "011010111111010111011111111011001111111111011111101110111111111110100111111111111111101101101111110111111111010111111011101111111111111111111111111111111111111111111111111111011011010110111011111",
    ],
    'corrupt 30%': [
        "010000101010010111011010111011011111111111011111101110111111111110100110111111111110101001100111110110111111011111111111111011101111111111111101111111111111111011101001101011001011111110111011110",
        "011010101010010111011110111011001111111111011111101110111111111110100110011111111100111001100111100111101111011111101111111011111111111111111101111111111111111011101000111011001011111110111011110",
        "011010101100010111011010111011010111111111111111101010111111011110100111111011111110101001100111110111111111011101110111101011111111111101111101111111111111111011101001111011011011110110111011110",
    ],
    'k = 1': [
        "011010011011111111010111011111010111111110111111111110011111101111101111100111111100111110110011111011111111011111110111101101111011110101101111111101101000111011111011111111111011111110111011111",
        "011010011111111111011101011111011111111111011111111110011111111111101110100111111110101110111011111011111111111111110111101101111111110101111111101100101000111011111111111111011011111110111011111",
        "011010011111111111011111011111011111111111111111111100010111111111101110101111111110111110111011111011101110110111001111111001111111110101111111111101111000111011111001111111011011111110111011111",
    ],
    'concise': [
        "000000000000000000000000000000000000111100000100100000010101010110100000000001001100100000100011000010001100000001000001101000100010100000100000000000000000001001101010001100001010000110101011000",
        "000000000000000000001000000000000000111100000100100000010101010110100000000001001100100000100001000010001100000001000001101000100010100000100000000000000000001001101000001000001010000110101011000",
        "000000000001000000000000000000000000111100000100100000010101010110100000000001001100100000100001000010001100000001000001101000100010100000100000000000000000001001101000001000001010000110101011000",
    ],
    'gpt-4o-mini': [
        "111011111111111111011111111111111111111111111111111111011111111111101111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111110111111110",
        "111011111111111111011111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111110111111111",
        "111011111111111111011111111111111111111111111111111111111111111111101111111111111101111111111111111111111110111111111111111111111111111111111111111111111111111011111111111111111011111110111111111",
    ],
}
RUNS = {k: [[c == "1" for c in r] for r in v] for k, v in RAW.items()}
CAL, LATER = RUNS["baseline"][:3], RUNS["baseline"][3:]
REAL_BREAKAGES = ["corrupt 10%", "corrupt 30%", "k = 1", "concise"]


def test_reference_is_mostly_sure_passes():
    ref = reference(CAL)
    assert len(ref) == 195
    assert sum(ref) == 186  # 186 sure passes, 9 sure fails


def test_25_tests_flipped_in_runs_1_to_3():
    ref, st = reference(CAL), stable(CAL)
    assert sum(not s for s in st) == 25
    assert sum(s and r for s, r in zip(st, ref)) == 164  # passed all three
    assert sum(s and not r for s, r in zip(st, ref)) == 6  # failed all three


def test_unchanged_run_5_goes_down_far_more_than_up():
    ref = reference(CAL)
    assert down_up(ref, LATER[1]) == (15, 3)
    assert down_up(ref, LATER[1], stable(CAL)) == (14, 1)  # flaky tests left out


def test_mcnemar():
    assert mcnemar_p(0, 0) == 1.0
    assert mcnemar_p(3, 3) == 1.0
    assert round(mcnemar_p(15, 3), 4) == 0.0075
    assert round(mcnemar_p(14, 1), 4) == 0.001


def test_paired_test_cries_wolf_and_leaving_out_flaky_tests_makes_it_worse():
    ref, st = reference(CAL), stable(CAL)
    assert [paired_alarm(ref, night) for night in LATER] == [False, True, False]
    assert [paired_alarm(ref, night, st) for night in LATER] == [False, True, True]


def test_sure_passes_still_slip_about_one_run_in_18():
    assert round(slip_rate(CAL, LATER), 3) == 0.057


def test_each_tests_own_pass_rate():
    rates = pass_rates(CAL)
    passed = [sum(run[i] for run in CAL) for i in range(195)]
    by_count = {c: round(rates[passed.index(c)], 3) for c in (3, 2, 0)}
    assert by_count == {3: 0.961, 2: 0.794, 0: 0.461}


def test_expected_failures_and_the_line():
    expected, line = alarm_line(pass_rates(CAL))
    assert round(expected, 2) == 15.33
    assert round(line, 2) == 25.72


def test_per_test_rule_is_quiet_on_unchanged_nights_and_loud_on_breakages():
    rates = pass_rates(CAL)
    assert not any(per_test_alarm(rates, night) for night in LATER)  # 15, 21, 16 failed
    assert all(per_test_alarm(rates, night) for b in REAL_BREAKAGES for night in RUNS[b])
    assert not any(per_test_alarm(rates, night) for night in RUNS["gpt-4o-mini"])  # the score went up


def test_the_price_the_two_point_change_slips_by():
    rates = pass_rates(CAL)
    assert [sum(not x for x in night) for night in RUNS["corrupt 3%"]] == [19, 24, 17]
    assert not any(per_test_alarm(rates, night) for night in RUNS["corrupt 3%"])
