"""Tests on Lab 06's recorded results (eval labs, Repeated Runs). The AI is simulated: 2,000 tasks, each with
its own true chance of passing; the matrices below are the exact runs the lab's scripts print with their seeds.
Each string is one task's runs: 1 = pass, 0 = fail.
Run: pytest test_repeats_practice.py"""
from repeats_practice import (all_k_pass, effective_tasks, flakiness, flips, naive_ci, pass_at_k, pass_hat_k,
                              task_level_ci)

# 01_flakiness.py, Exercise 1: 20 tasks x 10 runs, temperature 0.7
GRID = [
    "1111110111", "1111111111", "1111111111", "0101111011", "1111101011",
    "1111111111", "1111111111", "1011011011", "1111111111", "1111111111",
    "1101111111", "1101110101", "1111101111", "1000000000", "1110111111",
    "1011111011", "1111111111", "1111011101", "1111111111", "1111111111",
]

# 01_flakiness.py, Exercise 3: the same 100 tasks run twice, one run each, nothing changed
RUN_A = "1111111110111010110111110101111111101101111111111001100111110011101100111100101011011111011111111011"
RUN_B = "1111111010111011111111000101111111101101011111111111100111111011111110111110101111011111111111111111"

# 02_pass_at_k.py: 200 tasks x 10 runs, temperature 0.7 (the module-level matrix M)
MATRIX = [
    "1111111111", "1111111111", "0111111111", "0110111010", "0000000000",
    "1100000100", "1111111111", "0010000100", "1111111111", "1111111111",
    "1111111101", "1111111111", "1111111111", "1111111111", "1111111110",
    "0100110011", "1111111111", "1111111111", "1111111111", "1111111111",
    "1111111111", "0000000001", "1111111111", "1111111111", "0000000000",
    "1111111111", "1111111111", "1111111111", "1111111111", "1111111111",
    "1000100000", "1111111111", "1111111111", "0111111111", "1111111111",
    "1111111111", "1111111111", "1111110111", "1111111111", "0000000000",
    "1111111111", "0000000000", "0010010000", "0010000000", "1101010111",
    "0000001100", "1111111111", "0111111111", "0000000000", "1111111101",
    "1111110111", "0100000001", "1111111111", "1111110111", "1111101111",
    "1111111111", "1100111011", "1111111111", "1111111111", "0000000000",
    "0111011111", "1111111111", "1110111101", "1111111111", "1111111111",
    "1000010000", "1011000010", "0001000000", "0000010010", "0000000000",
    "1111111111", "1111111111", "1111111111", "1111111111", "1111111111",
    "1111111111", "1111111111", "1111111111", "1111111111", "1111111111",
    "1111111111", "1101101100", "1111111111", "1111111111", "1111000011",
    "1111111111", "1111111111", "1111111111", "1111111111", "1111111111",
    "1111111111", "0000000000", "1111111111", "1101111111", "1111011111",
    "1111111111", "1111111111", "1111111111", "0111010100", "1010010101",
    "1101100111", "0000000000", "1111111111", "1111111111", "1111111011",
    "0000010000", "1111111111", "0101000000", "1110111011", "1110111000",
    "1111111111", "1111111111", "1111111110", "1111111111", "0000000000",
    "1111111111", "1111111111", "1011110101", "1111111111", "1111101111",
    "0100010010", "1111111111", "0000000000", "1111111111", "1111111111",
    "0111010111", "1101111111", "1011000100", "1111111111", "1111111111",
    "1011100100", "0000000000", "1111111111", "1111111010", "0000000000",
    "1111110111", "1111111111", "1111111111", "1111111111", "0000000000",
    "1111111110", "0000000000", "1101011110", "1111111111", "0110110000",
    "1111111111", "0010010000", "0000000010", "1111111111", "0000000000",
    "1111111110", "1111111111", "1111111111", "1111111111", "1111111111",
    "1110111111", "1111111111", "1111111110", "0000000000", "1111111111",
    "1111111111", "1111111111", "1101111111", "1111111111", "0000000000",
    "0000000000", "1111111111", "1100010100", "1111111111", "1111111111",
    "1111011101", "1001111010", "1011111111", "0000010000", "0100011000",
    "0010000011", "0010101010", "0000000100", "1111111111", "0100010010",
    "0000000000", "0000000000", "1111111111", "1111111111", "0000010000",
    "1111111111", "0000000000", "1111111111", "1110111111", "1111111111",
    "1111111111", "1111111111", "1110111111", "1111111111", "0111111111",
    "1111111101", "1111111111", "1111111111", "1000000000", "1111111111",
]


def matrix(rows):
    return [[int(ch) for ch in r] for r in rows]


def test_flaky_tasks():
    # 20 tasks x 10 runs: 9 passed every run, none failed every run, 11 flipped
    assert flakiness(matrix(GRID)) == {"always_pass": 9, "always_fail": 0, "flaky": 11}
    # 4 of the flaky ones failed exactly one run in ten
    assert sum(1 for r in matrix(GRID) if sum(r) == 9) == 4


def test_same_eval_twice():
    a, b = [int(c) for c in RUN_A], [int(c) for c in RUN_B]
    assert (sum(a), sum(b)) == (76, 83)  # 76% then 83%, nothing changed
    assert flips(a, b) == (4, 11)  # 4 passed then failed, 11 failed then passed


def test_free_throws():
    assert round(all_k_pass(0.9, 5), 2) == 0.59  # a 90% shooter makes 5 in a row 59% of the time
    assert round(1 - all_k_pass(0.97, 10), 2) == 0.26  # a 97% task fails at least once in 10 runs: 26%


def test_pass_at_k_and_pass_hat_k():
    m = matrix(MATRIX)  # 200 tasks x 10 runs
    assert round(pass_at_k(m, 1), 4) == round(pass_hat_k(m, 1), 4) == 0.7335  # both are the pass rate
    assert round(pass_at_k(m, 5), 3) == 0.855 and round(pass_hat_k(m, 5), 3) == 0.591
    assert round(pass_at_k(m, 10), 3) == 0.890  # at least one of 10 passes
    assert round(pass_hat_k(m, 10), 3) == 0.515  # all 10 pass
    first100 = m[:100]  # Script 2, Exercise 3
    assert round(pass_at_k(first100, 5), 3) == 0.888 and round(pass_hat_k(first100, 5), 3) == 0.647
    try:
        pass_at_k(m, 11)
    except ValueError:
        pass
    else:
        raise AssertionError("k = 11 needs at least 11 runs per task: raise ValueError")


def test_the_shortcut_is_too_gloomy():
    m = matrix(MATRIX)
    p1 = pass_at_k(m, 1)
    # the shortcut, pass@1 to the power 5, says about 21%; measured from the repeats, all 5 pass 59%
    assert round(all_k_pass(p1, 5), 3) == 0.212
    assert pass_hat_k(m, 5) > 2.5 * all_k_pass(p1, 5)


def test_runs_are_not_tasks():
    m = [r[:5] for r in matrix(MATRIX)[:100]]  # 100 tasks x 5 runs
    lo, hi = naive_ci(m)
    assert (round(lo, 3), round(hi, 3)) == (0.742, 0.814)
    lo2, hi2 = task_level_ci(m)
    assert (round(lo2, 3), round(hi2, 3)) == (0.708, 0.848)
    assert (hi2 - lo2) > 1.8 * (hi - lo)  # about twice as wide


def test_effective_tasks():
    assert round(effective_tasks(100, 5, 0.6746)) == 135  # temperature 0.7
    assert round(effective_tasks(100, 5, 0.9234)) == 107  # temperature 0: runs agree even more
    assert round(effective_tasks(50, 10, 0.1132)) == 248  # 50 documents x 10 questions
