"""Tests on Lab 03's recorded run (October 2026): one model (gpt-5.4-mini, low effort) in three
harnesses, each reviewing the same 40 PRs three times. 30 of the PRs have a planted bug. Each row
below is one planted bug, in the lab's order: did run 1, 2, 3 find it? (Matched by line and confirmed
by an AI judge; the answer key includes the 4 issues the lab proved.)
Run: pytest test_harness_practice.py"""
from harness_practice import only_by, paired_bootstrap, pooled, precision, proven, recall_per_run

SINGLE = [
    [1, 1, 1],  # n36-d2 reliability
    [1, 1, 1],  # c18-d2 logic
    [1, 1, 1],  # n17-d2 security
    [1, 1, 1],  # n22-d1 ui
    [1, 1, 1],  # n05-d2 security
    [1, 1, 1],  # n27-d2 ai-shaped
    [1, 0, 0],  # c16-d2 performance
    [1, 1, 1],  # n32-d4 tests
    [1, 1, 1],  # worker-release-d2 logic
    [1, 1, 1],  # n31-d3 convention
    [1, 1, 1],  # c38-d2 security
    [1, 1, 1],  # n26-d2 convention
    [1, 1, 0],  # c20-d2 tests
    [0, 0, 0],  # n33-d1 contract
    [0, 0, 1],  # n29-d3 ui
    [0, 0, 1],  # n02-d2 performance
    [0, 1, 1],  # n09-d2 ai-shaped
    [1, 1, 0],  # c02-d1 ai-shaped
    [1, 1, 1],  # c28-d1 reliability
    [1, 1, 1],  # n24-d2 security
    [0, 0, 1],  # n30-d2 convention
    [1, 1, 1],  # n34-d2 contract
    [0, 1, 1],  # n19-d3 performance
    [1, 1, 1],  # n10-d2 logic
    [0, 0, 1],  # n16-d1 contract
    [1, 1, 0],  # n28-d3 convention
    [1, 1, 1],  # n23-d2 ui
    [0, 0, 0],  # c35-d3 security
    [1, 1, 1],  # n25-d2 ui
    [1, 1, 1],  # c27-d3 reliability
]

AGENT = [
    [1, 1, 1],  # n36-d2 reliability
    [1, 1, 1],  # c18-d2 logic
    [1, 1, 1],  # n17-d2 security
    [1, 1, 1],  # n22-d1 ui
    [1, 1, 1],  # n05-d2 security
    [1, 1, 1],  # n27-d2 ai-shaped
    [0, 0, 0],  # c16-d2 performance
    [1, 1, 1],  # n32-d4 tests
    [1, 1, 1],  # worker-release-d2 logic
    [1, 1, 1],  # n31-d3 convention
    [1, 1, 1],  # c38-d2 security
    [0, 1, 1],  # n26-d2 convention
    [1, 0, 1],  # c20-d2 tests
    [1, 1, 1],  # n33-d1 contract
    [0, 0, 0],  # n29-d3 ui
    [0, 0, 1],  # n02-d2 performance
    [1, 1, 0],  # n09-d2 ai-shaped
    [1, 1, 1],  # c02-d1 ai-shaped
    [1, 0, 1],  # c28-d1 reliability
    [1, 1, 1],  # n24-d2 security
    [1, 0, 0],  # n30-d2 convention
    [0, 0, 1],  # n34-d2 contract
    [1, 1, 1],  # n19-d3 performance
    [1, 1, 1],  # n10-d2 logic
    [1, 1, 1],  # n16-d1 contract
    [0, 1, 1],  # n28-d3 convention
    [1, 1, 1],  # n23-d2 ui
    [1, 0, 0],  # c35-d3 security
    [1, 1, 1],  # n25-d2 ui
    [1, 1, 1],  # c27-d3 reliability
]

PR_AGENT = [
    [1, 1, 1],  # n36-d2 reliability
    [1, 0, 1],  # c18-d2 logic
    [1, 1, 1],  # n17-d2 security
    [1, 1, 1],  # n22-d1 ui
    [1, 1, 1],  # n05-d2 security
    [1, 1, 1],  # n27-d2 ai-shaped
    [0, 0, 0],  # c16-d2 performance
    [1, 1, 1],  # n32-d4 tests
    [1, 1, 0],  # worker-release-d2 logic
    [1, 1, 1],  # n31-d3 convention
    [1, 1, 1],  # c38-d2 security
    [0, 0, 0],  # n26-d2 convention
    [1, 1, 0],  # c20-d2 tests
    [0, 0, 0],  # n33-d1 contract
    [1, 1, 1],  # n29-d3 ui
    [0, 0, 0],  # n02-d2 performance
    [1, 1, 1],  # n09-d2 ai-shaped
    [0, 0, 0],  # c02-d1 ai-shaped
    [1, 1, 1],  # c28-d1 reliability
    [1, 1, 1],  # n24-d2 security
    [0, 0, 0],  # n30-d2 convention
    [0, 0, 0],  # n34-d2 contract
    [0, 0, 1],  # n19-d3 performance
    [1, 1, 1],  # n10-d2 logic
    [1, 1, 1],  # n16-d1 contract
    [0, 0, 0],  # n28-d3 convention
    [1, 1, 1],  # n23-d2 ui
    [0, 0, 1],  # c35-d3 security
    [1, 1, 1],  # n25-d2 ui
    [0, 0, 0],  # c27-d3 reliability
]


def test_recall_per_run():
    assert recall_per_run(SINGLE) == [22, 23, 24]
    assert recall_per_run(AGENT) == [24, 22, 25]
    assert recall_per_run(PR_AGENT) == [19, 18, 19]


def test_pooling_three_runs():
    # Single and agent: about 77% per run, 93% pooled. PR-Agent's misses are consistent.
    assert pooled(SINGLE) == 28
    assert pooled(AGENT) == 28
    assert pooled(PR_AGENT) == 21


def test_found_only_by_one():
    assert only_by(SINGLE, AGENT) == (2, 2)
    assert only_by(SINGLE, PR_AGENT) == (8, 1)
    # The contract bug n33-d1 (row 14): the agent found it in every run, the others never did.
    assert AGENT[13] == [1, 1, 1] and SINGLE[13] == [0, 0, 0] and PR_AGENT[13] == [0, 0, 0]


def test_paired_bootstrap_agent_vs_single():
    point, lo, hi = paired_bootstrap(SINGLE, AGENT)
    assert round(point * 100) == 2
    assert (round(lo * 100), round(hi * 100)) == (-8, 13)
    assert lo < 0 < hi  # zero is inside: on 30 bugs, can't tell them apart


def test_paired_bootstrap_pr_agent_vs_single():
    point, lo, hi = paired_bootstrap(SINGLE, PR_AGENT)
    assert round(point * 100) == -14
    assert (round(lo * 100), round(hi * 100)) == (-29, -1)
    assert hi < 0  # only just excludes zero


def test_precision_run_1():
    # Right bug comments / all bug comments, run 1: single 24/35, agent 27/38, PR-Agent 27/33.
    assert round(precision(24, 11) * 100) == 69
    assert round(precision(27, 11) * 100) == 71
    assert round(precision(27, 6) * 100) == 82
    assert precision(0, 0) == 0.0


def test_proof_needs_both_runs():
    assert proven("fails", "passes")          # f1–f4 on all 12 variant PRs
    assert not proven("fails", "fails")       # n34-d1's first SSRF test: failed for the planted reason too
    assert not proven("passes", "passes")     # the test doesn't show the bug at all
