"""Tests on Lab 07's recorded run (October 2026). The sets below come from the lab's results through its own
code (plans.decisions, 03_plan_vs_code.plan_found / code_found, scoring.score), read-only. The decision labels are
by the AI that built the lab, and every reviewer is an AI model. Run: pytest test_plan_review_practice.py"""
from plan_review_practice import flag_changes, paired_split, points, score_plan

# The 81 planted bugs labelled decisions (a plan could state them).
DECISIONS = {
    "api-release-d1", "c02-d1", "c03-d1", "c04-d1", "c10-d2", "c11-d3", "c12-d1", "c12-d2", "c13-d3",
    "c14-d1", "c16-d1", "c16-d3", "c18-d1", "c19-d1", "c20-d1", "c23-d1", "c24-d1", "c24-d2", "c24-d4",
    "c26-d1", "c27-d1", "c27-d3", "c33-d1", "c35-d3", "c36-d1", "c38-d2", "c40-d1", "c40-d2", "n01-d1",
    "n01-d3", "n02-d1", "n04-d2", "n05-d1", "n06-d1", "n06-d2", "n07-d2", "n08-d1", "n08-d3", "n09-d1",
    "n10-d1", "n10-d3", "n11-d2", "n12-d1", "n13-d3", "n15-d1", "n15-d2", "n15-d3", "n16-d1", "n16-d2",
    "n17-d1", "n18-d1", "n18-d2", "n19-d1", "n19-d2", "n19-d3", "n20-d3", "n23-d1", "n23-d3", "n24-d1",
    "n24-d4", "n26-d1", "n27-d1", "n27-d3", "n28-d2", "n29-d1", "n29-d4", "n30-d1", "n31-d1", "n31-d4",
    "n32-d2", "n32-d4", "n34-d2", "n35-d1", "n36-d1", "n37-d1", "n37-d2", "n37-d4", "n38-d1", "n38-d2",
    "worker-delivery-d1", "worker-release-d1",
}

# Standard caught the wrong plan, reading the plan alone (49).
STD_PLAN = {
    "api-release-d1", "c11-d3", "c13-d3", "c14-d1", "c16-d3", "c18-d1", "c19-d1", "c24-d1", "c24-d4",
    "c26-d1", "c33-d1", "c38-d2", "c40-d1", "c40-d2", "n01-d1", "n01-d3", "n04-d2", "n05-d1", "n06-d2",
    "n07-d2", "n08-d1", "n08-d3", "n09-d1", "n10-d1", "n13-d3", "n15-d1", "n15-d3", "n17-d1", "n18-d1",
    "n19-d1", "n19-d2", "n20-d3", "n23-d1", "n24-d1", "n24-d4", "n26-d1", "n27-d1", "n27-d3", "n29-d4",
    "n30-d1", "n31-d4", "n32-d2", "n32-d4", "n36-d1", "n37-d2", "n38-d1", "n38-d2",
    "worker-delivery-d1", "worker-release-d1",
}

# Standard caught the wrong plan, reading the task + the plan (62).
STD_TASK = {
    "c02-d1", "c03-d1", "c10-d2", "c12-d1", "c12-d2", "c13-d3", "c14-d1", "c16-d3", "c18-d1", "c19-d1",
    "c20-d1", "c23-d1", "c24-d1", "c24-d2", "c24-d4", "c26-d1", "c27-d3", "c33-d1", "c38-d2", "c40-d1",
    "c40-d2", "n01-d3", "n05-d1", "n06-d1", "n06-d2", "n07-d2", "n08-d1", "n08-d3", "n09-d1", "n10-d1",
    "n11-d2", "n12-d1", "n13-d3", "n15-d1", "n15-d3", "n16-d2", "n17-d1", "n18-d1", "n18-d2", "n19-d1",
    "n19-d2", "n20-d3", "n23-d1", "n24-d1", "n24-d4", "n26-d1", "n27-d1", "n27-d3", "n28-d2", "n29-d1",
    "n29-d4", "n30-d1", "n32-d2", "n32-d4", "n35-d1", "n36-d1", "n37-d2", "n37-d4", "n38-d1", "n38-d2",
    "worker-delivery-d1", "worker-release-d1",
}

# Quick caught the wrong plan, reading the plan alone (46).
QUICK_PLAN = {
    "api-release-d1", "c02-d1", "c04-d1", "c12-d1", "c14-d1", "c16-d1", "c16-d3", "c18-d1", "c23-d1",
    "c24-d1", "c24-d4", "c26-d1", "c27-d3", "c33-d1", "c38-d2", "c40-d1", "c40-d2", "n01-d1", "n01-d3",
    "n05-d1", "n07-d2", "n08-d1", "n08-d3", "n10-d1", "n13-d3", "n15-d3", "n16-d2", "n17-d1", "n19-d1",
    "n19-d2", "n23-d1", "n24-d1", "n24-d4", "n26-d1", "n27-d1", "n27-d3", "n29-d4", "n31-d1", "n34-d2",
    "n36-d1", "n37-d2", "n37-d4", "n38-d1", "n38-d2", "worker-delivery-d1", "worker-release-d1",
}

# Quick caught the wrong plan, reading the task + the plan (56).
QUICK_TASK = {
    "api-release-d1", "c11-d3", "c12-d1", "c12-d2", "c14-d1", "c16-d3", "c18-d1", "c20-d1", "c23-d1",
    "c24-d1", "c24-d2", "c24-d4", "c26-d1", "c27-d3", "c36-d1", "c38-d2", "c40-d1", "c40-d2", "n01-d1",
    "n01-d3", "n02-d1", "n05-d1", "n06-d1", "n07-d2", "n08-d1", "n08-d3", "n09-d1", "n10-d1", "n11-d2",
    "n12-d1", "n13-d3", "n15-d2", "n15-d3", "n17-d1", "n18-d1", "n18-d2", "n19-d1", "n19-d2", "n20-d3",
    "n23-d1", "n24-d4", "n26-d1", "n27-d1", "n27-d3", "n29-d4", "n30-d1", "n32-d2", "n32-d4", "n34-d2",
    "n35-d1", "n36-d1", "n37-d2", "n38-d1", "n38-d2", "worker-delivery-d1", "worker-release-d1",
}

# Standard caught the same bug in the PR's diff (Lab 02, run 1: 68).
STD_DIFF = {
    "api-release-d1", "c02-d1", "c03-d1", "c04-d1", "c10-d2", "c12-d1", "c12-d2", "c13-d3", "c14-d1",
    "c16-d1", "c16-d3", "c18-d1", "c19-d1", "c20-d1", "c23-d1", "c24-d1", "c24-d4", "c26-d1", "c27-d3",
    "c33-d1", "c38-d2", "c40-d1", "c40-d2", "n01-d3", "n02-d1", "n04-d2", "n05-d1", "n06-d1", "n06-d2",
    "n07-d2", "n08-d3", "n09-d1", "n10-d1", "n10-d3", "n11-d2", "n12-d1", "n13-d3", "n15-d1", "n15-d2",
    "n15-d3", "n16-d2", "n17-d1", "n18-d1", "n18-d2", "n19-d1", "n19-d2", "n20-d3", "n23-d1", "n23-d3",
    "n24-d1", "n24-d4", "n26-d1", "n27-d1", "n27-d3", "n28-d2", "n29-d1", "n29-d4", "n30-d1", "n31-d1",
    "n32-d2", "n32-d4", "n34-d2", "n35-d1", "n36-d1", "n37-d4", "n38-d1", "n38-d2", "worker-delivery-d1",
}

# Quick caught it in the diff (53).
QUICK_DIFF = {
    "api-release-d1", "c04-d1", "c12-d1", "c12-d2", "c13-d3", "c14-d1", "c16-d3", "c18-d1", "c19-d1",
    "c20-d1", "c23-d1", "c24-d1", "c24-d2", "c24-d4", "c26-d1", "c33-d1", "c38-d2", "c40-d1", "c40-d2",
    "n01-d3", "n02-d1", "n04-d2", "n05-d1", "n06-d2", "n07-d2", "n08-d3", "n09-d1", "n10-d1", "n12-d1",
    "n13-d3", "n18-d1", "n19-d1", "n19-d3", "n20-d3", "n23-d1", "n24-d1", "n24-d4", "n27-d1", "n27-d3",
    "n28-d2", "n29-d4", "n30-d1", "n31-d1", "n31-d4", "n32-d2", "n32-d4", "n34-d2", "n35-d1", "n36-d1",
    "n37-d4", "n38-d1", "n38-d2", "worker-delivery-d1",
}
# Clean PRs (no planted bug) that standard flagged as buggy: code review alone, and with the approved plan above
# the diff (04_plan_as_spec.py).
FLAGGED_WITHOUT_PLAN = {
    "api-cleanup-clean", "api-release-clean", "c05-clean", "c07-clean", "c12-clean", "c14-clean", "c15-clean",
    "c23-clean", "c24-clean", "c26-clean", "c28-clean", "c33-clean", "c34-clean", "c35-clean", "c40-clean",
    "n01-clean", "n02-clean", "n05-clean", "n09-clean", "n10-clean", "n11-clean", "n13-clean", "n14-clean",
    "n15-clean", "n17-clean", "n19-clean", "n24-clean", "n25-clean", "n26-clean", "n29-clean", "n31-clean",
    "n32-clean", "n34-clean", "n36-clean", "n38-clean", "worker-delivery-clean", "worker-release-clean",
}
FLAGGED_WITH_PLAN = {
    "api-cleanup-clean", "api-release-clean", "c04-clean", "c05-clean", "c07-clean", "c10-clean", "c11-clean",
    "c12-clean", "c13-clean", "c17-clean", "c19-clean", "c20-clean", "c22-clean", "c23-clean", "c24-clean",
    "c26-clean", "c27-clean", "c33-clean", "c34-clean", "c35-clean", "c36-clean", "n01-clean", "n02-clean",
    "n03-clean", "n05-clean", "n08-clean", "n09-clean", "n10-clean", "n11-clean", "n13-clean", "n14-clean",
    "n15-clean", "n16-clean", "n17-clean", "n19-clean", "n20-clean", "n22-clean", "n23-clean", "n25-clean",
    "n26-clean", "n27-clean", "n28-clean", "n29-clean", "n30-clean", "n31-clean", "n33-clean", "n35-clean",
    "n36-clean", "n37-clean", "n38-clean", "worker-delivery-clean", "worker-release-clean",
}


# ---------------------------------------------------------------------------------------------
# score_plan

def test_wrong_plan_caught_when_a_comment_is_judged_the_same():
    assert score_plan("wrong", [{"severity": "bug", "same": False}, {"severity": "bug", "same": True}]) is True


def test_wrong_plan_caught_even_by_a_nit():
    assert score_plan("wrong", [{"severity": "nit", "same": True}]) is True


def test_wrong_plan_missed_when_no_comment_matches():
    assert score_plan("wrong", [{"severity": "bug", "same": False}]) is False
    assert score_plan("wrong", []) is False


def test_right_plan_sent_back_by_any_bug_comment():
    # The task asks for omitted or null `enabled`; the plan's tests skip null (the right plan for
    # "Fix syncs(enabled: false) returning every sync", c01). No planted bug, but it's sent back.
    assert score_plan("right", [{"severity": "bug", "same": False}]) is True


def test_right_plan_with_only_nits_passes():
    assert score_plan("right", [{"severity": "nit", "same": False}]) is False
    assert score_plan("right", []) is False


# ---------------------------------------------------------------------------------------------
# paired_split and points, on the 81 decisions

def test_the_sets_are_the_lab_counts():
    assert len(DECISIONS) == 81
    assert [len(s) for s in (STD_PLAN, STD_TASK, QUICK_PLAN, QUICK_TASK, STD_DIFF, QUICK_DIFF)] == [49, 62, 46, 56, 68, 53]


def test_standard_with_the_task():
    assert paired_split(DECISIONS, STD_TASK, STD_DIFF) == {"both": 58, "only_plan": 4, "only_diff": 10, "neither": 9}


def test_standard_plan_alone():
    assert paired_split(DECISIONS, STD_PLAN, STD_DIFF) == {"both": 43, "only_plan": 6, "only_diff": 25, "neither": 7}


def test_split_adds_up():
    for plan, diff in ((QUICK_PLAN, QUICK_DIFF), (QUICK_TASK, QUICK_DIFF)):
        assert sum(paired_split(DECISIONS, plan, diff).values()) == 81


def test_points_match_the_lab():
    # The lab's table: standard −23 (plan alone) and −7 (task + plan); quick −9 and +4.
    assert points(DECISIONS, STD_PLAN, STD_DIFF) == -23
    assert points(DECISIONS, STD_TASK, STD_DIFF) == -7
    assert points(DECISIONS, QUICK_PLAN, QUICK_DIFF) == -9
    assert points(DECISIONS, QUICK_TASK, QUICK_DIFF) == 4


def test_points_ignore_bugs_outside_the_set():
    assert points({"a", "b"}, {"a", "x", "y"}, set()) == 50


# ---------------------------------------------------------------------------------------------
# flag_changes: the approved plan as the spec

def test_the_approved_plan_flags_more_clean_prs():
    assert flag_changes(FLAGGED_WITHOUT_PLAN, FLAGGED_WITH_PLAN) == {"before": 37, "after": 52, "new": 22, "no_longer": 7}


def test_no_change():
    assert flag_changes({"a"}, {"a"}) == {"before": 1, "after": 1, "new": 0, "no_longer": 0}
