"""Tests on Lab 18's script 01, from its recorded run (October 2026): the similarity of each of 988
real learner messages to its closest starting test, the reviewer's verdicts on the 161 tests the
writer kept, and the fine-tuning tests in the final suite. Run: pytest test_testgen_practice.py"""
from testgen_practice import kappa, reviewed, threshold_rates, to_recheck

# Similarity to the closest of the 40 starting tests (rounded to 3 places). 316 messages ask something
# a starting test covers; 672 ask something new.
COVERED = [
    0.817, 0.74, 0.696, 0.712, 0.883, 0.762, 0.934, 0.688, 0.84, 0.786, 0.727, 0.896, 0.958, 0.614, 0.661,
    0.822, 0.88, 0.588, 0.631, 0.781, 0.868, 0.574, 0.688, 0.643, 0.737, 0.895, 0.88, 0.745, 0.772, 0.718,
    0.745, 0.718, 0.667, 0.903, 0.745, 0.742, 0.778, 0.789, 0.87, 0.804, 0.711, 0.958, 0.673, 0.643, 0.806,
    0.806, 0.868, 0.822, 0.88, 0.784, 0.875, 0.88, 0.802, 0.787, 0.896, 0.781, 0.464, 0.922, 0.614, 0.745,
    0.742, 0.757, 0.82, 0.867, 0.573, 0.762, 0.835, 0.806, 0.875, 0.913, 0.464, 0.867, 0.958, 0.704, 0.725,
    0.867, 0.816, 0.804, 0.712, 0.888, 0.81, 0.895, 0.804, 0.789, 0.547, 0.911, 0.778, 0.84, 0.864, 0.464,
    0.867, 0.696, 0.592, 0.896, 0.804, 0.614, 0.631, 0.882, 0.789, 0.789, 0.868, 0.774, 0.547, 0.547, 0.778,
    0.464, 0.806, 0.464, 0.774, 0.88, 0.789, 0.689, 0.552, 0.822, 0.757, 0.875, 0.835, 0.896, 0.804, 0.868,
    0.759, 0.911, 0.712, 0.712, 0.718, 0.958, 0.835, 0.718, 0.835, 0.744, 0.778, 0.789, 0.829, 0.808, 0.464,
    0.592, 0.879, 0.592, 0.944, 0.781, 0.879, 0.824, 0.867, 0.863, 0.786, 0.643, 0.88, 0.934, 0.712, 0.753,
    0.718, 0.804, 0.661, 0.718, 0.804, 0.789, 0.649, 0.688, 0.958, 0.778, 0.837, 0.887, 0.802, 0.753, 0.718,
    0.709, 0.868, 0.813, 0.793, 0.744, 0.464, 0.802, 0.883, 0.547, 0.896, 0.547, 0.822, 0.896, 0.712, 0.678,
    0.958, 0.661, 0.789, 0.643, 0.745, 0.592, 0.804, 0.934, 0.734, 0.837, 0.718, 0.737, 0.678, 0.888, 0.592,
    0.757, 0.681, 0.804, 0.937, 0.887, 0.834, 0.784, 0.737, 0.552, 0.85, 0.888, 0.718, 0.795, 0.726, 0.88,
    0.958, 0.837, 0.829, 0.718, 0.804, 0.547, 0.768, 0.724, 0.824, 0.891, 0.934, 0.718, 0.936, 0.789, 0.718,
    0.789, 0.806, 0.822, 0.688, 0.929, 0.864, 0.847, 0.661, 0.745, 0.84, 0.726, 0.867, 0.819, 0.631, 0.681,
    0.786, 0.723, 0.712, 0.631, 0.762, 0.671, 0.82, 0.775, 0.896, 0.958, 0.697, 0.614, 0.724, 0.753, 0.753,
    0.644, 0.753, 0.745, 0.822, 0.882, 0.88, 0.718, 0.802, 0.868, 0.958, 0.828, 0.614, 0.768, 0.547, 0.958,
    0.922, 0.725, 0.741, 0.659, 0.802, 0.774, 0.868, 0.734, 0.745, 0.958, 0.592, 0.745, 0.789, 0.958, 0.742,
    0.697, 0.797, 0.778, 0.84, 0.789, 0.843, 0.696, 0.737, 0.835, 0.456, 0.745, 0.643, 0.643, 0.888, 0.922,
    0.718, 0.804, 0.868, 0.757, 0.697, 0.588, 0.727, 0.694, 0.678, 0.778, 0.804, 0.807, 0.778, 0.804, 0.614,
    0.806,
]

NEW = [
    0.471, 0.626, 0.086, 0.291, 0.291, 0.285, 0.554, 0.285, 0.29, 0.386, 0.456, 0.336, 0.122, 0.366, 0.527,
    0.475, 0.658, 0.422, 0.417, 0.536, 0.609, 0.703, 0.465, 0.532, 0.346, 0.553, 0.732, 0.233, 0.122, 0.223,
    0.665, 0.771, 0.465, 0.452, 0.316, 0.31, 0.713, 0.417, 0.56, 0.143, 0.336, 0.542, 0.542, 0.404, 0.56,
    0.666, 0.53, 0.517, 0.529, 0.502, 0.692, 0.527, 0.452, 0.291, 0.336, 0.56, 0.317, 0.705, 0.612, 0.488,
    0.502, 0.488, 0.089, 0.522, 0.417, 0.588, 0.573, 0.186, 0.542, 0.456, 0.682, 0.451, 0.703, 0.703, 0.086,
    0.468, 0.726, 0.553, 0.525, 0.71, 0.633, 0.724, 0.539, 0.471, 0.502, 0.417, 0.469, 0.262, 0.536, 0.549,
    0.63, 0.465, 0.336, 0.681, 0.657, 0.143, 0.459, 0.33, 0.557, 0.608, 0.44, 0.652, 0.471, 0.67, 0.186,
    0.53, 0.419, 0.63, 0.467, 0.45, 0.68, 0.514, 0.502, 0.285, 0.465, 0.223, 0.404, 0.545, 0.801, 0.101,
    0.378, 0.654, 0.129, 0.105, 0.63, 0.592, 0.249, 0.651, 0.503, 0.29, 0.486, 0.267, 0.192, 0.317, 0.789,
    0.558, 0.346, 0.315, 0.582, 0.497, 0.728, 0.285, 0.716, 0.452, 0.539, 0.629, 0.482, 0.651, 0.57, 0.611,
    0.238, 0.291, 0.726, 0.57, 0.629, 0.262, 0.291, 0.56, 0.393, 0.45, 0.475, 0.393, 0.465, 0.336, 0.331,
    0.703, 0.316, 0.475, 0.117, 0.489, 0.086, 0.57, 0.465, 0.291, 0.469, 0.531, 0.203, 0.626, 0.377, 0.422,
    0.518, 0.285, 0.664, 0.422, 0.525, 0.465, 0.192, 0.553, 0.19, 0.652, 0.336, 0.488, 0.336, 0.465, 0.132,
    0.419, 0.567, 0.65, 0.417, 0.249, 0.351, 0.387, 0.673, 0.31, 0.488, 0.336, 0.354, 0.769, 0.192, 0.596,
    0.45, 0.489, 0.488, 0.527, 0.302, 0.159, 0.599, 0.713, 0.291, 0.734, 0.724, 0.665, 0.56, 0.665, 0.722,
    0.31, 0.703, 0.417, 0.267, 0.561, 0.538, 0.378, 0.703, 0.58, 0.349, 0.769, 0.315, 0.349, 0.31, 0.669,
    0.572, 0.417, 0.423, 0.447, 0.336, 0.515, 0.734, 0.654, 0.634, 0.159, 0.188, 0.518, 0.285, 0.285, 0.554,
    0.186, 0.492, 0.477, 0.652, 0.159, 0.681, 0.465, 0.309, 0.554, 0.502, 0.515, 0.086, 0.238, 0.29, 0.608,
    0.291, 0.549, 0.527, 0.317, 0.694, 0.654, 0.105, 0.285, 0.311, 0.712, 0.548, 0.465, 0.597, 0.336, 0.665,
    0.417, 0.186, 0.748, 0.654, 0.596, 0.481, 0.713, 0.453, 0.422, 0.522, 0.703, 0.285, 0.574, 0.499, 0.485,
    0.692, 0.488, 0.323, 0.606, 0.419, 0.291, 0.417, 0.598, 0.614, 0.705, 0.326, 0.444, 0.55, 0.465, 0.268,
    0.315, 0.489, 0.705, 0.455, 0.409, 0.614, 0.482, 0.377, 0.471, 0.33, 0.369, 0.55, 0.801, 0.453, 0.488,
    0.545, 0.249, 0.369, 0.463, 0.186, 0.682, 0.322, 0.485, 0.615, 0.741, 0.582, 0.341, 0.291, 0.286, 0.417,
    0.338, 0.086, 0.387, 0.234, 0.275, 0.341, 0.089, 0.336, 0.305, 0.463, 0.592, 0.387, 0.665, 0.357, 0.43,
    0.303, 0.31, 0.331, 0.634, 0.44, 0.349, 0.467, 0.571, 0.414, 0.317, 0.297, 0.378, 0.413, 0.336, 0.455,
    0.386, 0.609, 0.766, 0.609, 0.467, 0.354, 0.383, 0.422, 0.256, 0.22, 0.465, 0.304, 0.38, 0.298, 0.344,
    0.344, 0.332, 0.341, 0.554, 0.467, 0.663, 0.357, 0.42, 0.608, 0.681, 0.345, 0.328, 0.703, 0.322, 0.341,
    0.192, 0.518, 0.495, 0.349, 0.249, 0.289, 0.56, 0.341, 0.414, 0.56, 0.666, 0.353, 0.31, 0.465, 0.347,
    0.287, 0.606, 0.159, 0.336, 0.345, 0.387, 0.306, 0.463, 0.159, 0.322, 0.31, 0.465, 0.122, 0.529, 0.439,
    0.692, 0.654, 0.262, 0.548, 0.285, 0.409, 0.363, 0.285, 0.291, 0.593, 0.322, 0.766, 0.564, 0.488, 0.447,
    0.253, 0.592, 0.422, 0.308, 0.527, 0.684, 0.673, 0.295, 0.192, 0.652, 0.387, 0.597, 0.465, 0.507, 0.732,
    0.345, 0.303, 0.423, 0.293, 0.469, 0.236, 0.313, 0.467, 0.588, 0.203, 0.584, 0.456, 0.409, 0.322, 0.606,
    0.611, 0.399, 0.387, 0.347, 0.43, 0.336, 0.423, 0.194, 0.581, 0.527, 0.305, 0.317, 0.402, 0.628, 0.706,
    0.48, 0.302, 0.359, 0.684, 0.422, 0.465, 0.683, 0.381, 0.463, 0.726, 0.423, 0.365, 0.356, 0.278, 0.53,
    0.138, 0.531, 0.223, 0.363, 0.089, 0.567, 0.455, 0.291, 0.316, 0.42, 0.341, 0.606, 0.413, 0.122, 0.467,
    0.465, 0.444, 0.469, 0.45, 0.584, 0.705, 0.325, 0.465, 0.736, 0.522, 0.332, 0.548, 0.592, 0.724, 0.53,
    0.268, 0.285, 0.532, 0.291, 0.788, 0.089, 0.425, 0.53, 0.53, 0.291, 0.584, 0.422, 0.291, 0.322, 0.447,
    0.336, 0.417, 0.477, 0.591, 0.192, 0.262, 0.422, 0.302, 0.609, 0.368, 0.665, 0.309, 0.368, 0.369, 0.598,
    0.695, 0.336, 0.669, 0.385, 0.285, 0.258, 0.422, 0.769, 0.243, 0.433, 0.262, 0.285, 0.364, 0.539, 0.65,
    0.665, 0.34, 0.654, 0.089, 0.45, 0.502, 0.455, 0.555, 0.517, 0.302, 0.548, 0.354, 0.336, 0.456, 0.303,
    0.5, 0.447, 0.465, 0.545, 0.349, 0.347, 0.341, 0.275, 0.574, 0.609, 0.281, 0.53, 0.55, 0.122, 0.553,
    0.456, 0.344, 0.466, 0.549, 0.485, 0.406, 0.356, 0.339, 0.297, 0.425, 0.186, 0.422, 0.308, 0.309, 0.634,
    0.424, 0.734, 0.413, 0.347, 0.606, 0.673, 0.346, 0.477, 0.476, 0.192, 0.423, 0.574, 0.57, 0.285, 0.241,
    0.334, 0.462, 0.422, 0.529, 0.285, 0.31, 0.223, 0.801, 0.105, 0.203, 0.488, 0.262, 0.322, 0.385, 0.387,
    0.326, 0.539, 0.626, 0.31, 0.275, 0.519, 0.275, 0.353, 0.291, 0.425, 0.285, 0.33,
]

def test_the_lab_line():
    r = threshold_rates(COVERED, NEW, 0.70)
    assert (len(COVERED), len(NEW)) == (316, 672)
    assert r == {"repeats": 246, "fresh": 626, "sent_on": 696}  # 78% and 93%


def test_a_higher_line_sends_on_mostly_repeats():
    r = threshold_rates(COVERED, NEW, 0.80)
    assert r == {"repeats": 138, "fresh": 669, "sent_on": 847}  # 44% and 100%
    assert r["sent_on"] - 696 == 151  # 108 of the 151 extra are repeats


# A small replay: the Monday and Wednesday fine-tuning questions from the video, and two more.
STARTING = {"what is rag"}
MESSAGES = [
    (0, "what is rag"),                          # a starting test covers it
    (1, "why is full fine-tuning expensive"),     # Monday, week 1
    (3, "why is full fine-tuning expensive"),     # Wednesday: same meaning, review not done yet
    (5, "how does pq compress vectors"),
    (8, "why is full fine-tuning expensive"),     # week 2: approved by now
    (9, "how does pq compress vectors"),
    (10, "what is mcp"),
    (12, "what is mcp"),
]


def test_remember_nothing():
    assert reviewed(MESSAGES, STARTING, "nothing") == 8


def test_approved_only_reviews_a_repeat_asked_before_the_review():
    assert reviewed(MESSAGES, STARTING, "approved") == 5


def test_every_candidate_reviews_each_question_once():
    assert reviewed(MESSAGES, STARTING, "seen") == 3


# The reviewer on the 161 kept tests: the answer key says 155 are right, 6 wrong. It approved 160:
# all 155 right ones, and 5 of the 6 wrong ones.
KEY = [True] * 155 + [False] * 6
REVIEWER = [True] * 155 + [True, False, True, True, True, True]


def test_agreement_is_high_but_kappa_is_low():
    agree = sum(a == b for a, b in zip(REVIEWER, KEY))
    assert agree == 156  # 97%
    assert round(kappa(REVIEWER, KEY), 2) == 0.28


def test_approving_everything_is_no_better_than_luck():
    assert sum(KEY) / len(KEY) > 0.96  # 96% agreement for free
    assert kappa([True] * 161, KEY) == 0.0


def test_a_perfect_reviewer():
    assert kappa(KEY, KEY) == 1.0


# The fine-tuning tests in the final suite. On day 28 the article was revised, changing the answers
# to four questions.
FT_TESTS = [
    {"id": "t003", "intent": "fine_tuning-2-2", "added_day": 0},
    {"id": "t009", "intent": "fine_tuning-0-2", "added_day": 0},
    {"id": "t017", "intent": "fine_tuning-3-1", "added_day": 0},
    {"id": "t018", "intent": "fine_tuning-1-2", "added_day": 0},
    {"id": "t028", "intent": "fine_tuning-0-0", "added_day": 0},
    {"id": "t031", "intent": "fine_tuning-1-0", "added_day": 0},
    {"id": "t034", "intent": "fine_tuning-3-0", "added_day": 0},
    {"id": "s0130", "intent": "fine_tuning-1-1", "added_day": 4},
    {"id": "s0152", "intent": "fine_tuning-3-2", "added_day": 5},
    {"id": "s0217", "intent": "fine_tuning-2-1", "added_day": 8},
    {"id": "s0239", "intent": "fine_tuning-1-1", "added_day": 9},
    {"id": "s0311", "intent": "fine_tuning-1-0", "added_day": 11},
    {"id": "s0317", "intent": "fine_tuning-0-1", "added_day": 11},
    {"id": "s0346", "intent": "fine_tuning-2-1", "added_day": 13},
    {"id": "s0528", "intent": "fine_tuning-3-2", "added_day": 20},
    {"id": "s0785", "intent": "fine_tuning-1-0", "added_day": 30},
    {"id": "s0879", "intent": "fine_tuning-2-0", "added_day": 34},
    {"id": "s0908", "intent": "fine_tuning-1-2", "added_day": 35},
]
REVISED = {"fine_tuning-1-0", "fine_tuning-1-1", "fine_tuning-2-0", "fine_tuning-2-2"}


def test_tests_to_recheck_after_the_revision():
    ids = to_recheck(FT_TESTS, REVISED, 28)
    assert ids == ["t003", "t031", "s0130", "s0239", "s0311"]  # 4 of these 5 now expect the old answer
    assert "s0785" not in ids  # written after the revision, from the new article
