"""Tests on Lab 06's recorded run (October 2026). The per-PR finds below were read from the lab's
results/reviews and results/judgments.jsonl through its own scoring code; every reviewer is the AI
stand-in (quick: gpt-5.4-nano; standard: gpt-5.4-mini, low effort), not a person.
Run: pytest test_reviewable_practice.py"""
from reviewable_practice import (
    break_even_seconds,
    defect_parts,
    defects_per_arm,
    noise_floor,
    paired_change,
    plan_problems,
)

# The noise floor: 30 buggy PRs whose text is identical as the original and as "ordered".
# Columns: quick original run 1, run 2, quick ordered run 1, run 2, standard original run 1, standard ordered run 1.
FLOOR = {
    "api-cleanup-d1": (0, 0, 0, 0, 0, 0),
    "api-cleanup-d2": (0, 0, 0, 0, 1, 1),
    "c25-d1": (0, 1, 1, 1, 1, 1),
    "c26-d1": (1, 1, 1, 0, 1, 1),
    "c27-d1": (0, 1, 0, 0, 0, 1),
    "c27-d2": (1, 1, 1, 1, 1, 1),
    "c27-d3": (0, 0, 1, 0, 1, 1),
    "c28-d1": (1, 1, 0, 0, 1, 1),
    "n13-d1": (0, 0, 0, 0, 1, 0),
    "n13-d2": (0, 0, 1, 1, 0, 0),
    "n13-d3": (1, 0, 1, 1, 1, 1),
    "n15-d1": (0, 0, 0, 1, 1, 1),
    "n15-d2": (0, 0, 1, 1, 1, 1),
    "n15-d3": (0, 0, 0, 0, 1, 1),
    "n19-d1": (1, 0, 1, 1, 1, 0),
    "n19-d2": (0, 0, 0, 1, 1, 1),
    "n19-d3": (1, 1, 1, 1, 0, 0),
    "n19-d4": (1, 1, 1, 1, 1, 1),
    "n20-d1": (1, 1, 1, 1, 1, 1),
    "n20-d2": (1, 1, 1, 1, 1, 1),
    "n20-d3": (1, 1, 1, 1, 1, 1),
    "n27-d1": (1, 1, 1, 1, 1, 1),
    "n27-d2": (1, 1, 1, 1, 1, 1),
    "n27-d3": (1, 1, 1, 1, 1, 1),
    "web-polish-d1": (1, 1, 1, 1, 1, 1),
    "web-polish-d2": (1, 1, 1, 1, 1, 1),
    "worker-delivery-d1": (1, 1, 1, 1, 1, 1),
    "worker-delivery-d2": (1, 0, 1, 1, 1, 0),
    "worker-release-d1": (0, 0, 0, 0, 0, 1),
    "worker-release-d2": (0, 1, 1, 1, 1, 1),
}

# The standard reviewer on the 26 bugs in large PRs (200+ changed lines).
# Columns: original run 1, original run 2, split run 1, split run 2.
STANDARD_L = {
    "api-release-d1": (1, 1, 1, 1),
    "api-release-d2": (0, 0, 1, 0),
    "n05-d2": (1, 1, 1, 1),
    "n09-d1": (1, 1, 1, 1),
    "n09-d2": (0, 1, 1, 1),
    "n15-d1": (1, 1, 1, 1),
    "n15-d2": (1, 1, 1, 1),
    "n19-d1": (1, 1, 1, 0),
    "n19-d2": (1, 1, 1, 1),
    "n19-d3": (0, 1, 1, 1),
    "n19-d4": (1, 1, 1, 1),
    "n29-d1": (1, 1, 1, 1),
    "n29-d2": (0, 0, 0, 0),
    "n29-d3": (0, 0, 1, 0),
    "n29-d4": (1, 1, 1, 1),
    "n34-d1": (0, 0, 0, 0),
    "n34-d2": (1, 1, 1, 1),
    "n34-d3": (0, 0, 0, 0),
    "n36-d1": (1, 1, 1, 1),
    "n36-d2": (1, 1, 1, 1),
    "n37-d1": (0, 0, 0, 0),
    "n37-d2": (0, 0, 1, 1),
    "n37-d3": (1, 0, 1, 1),
    "n37-d4": (1, 1, 1, 1),
    "worker-release-d1": (0, 0, 1, 1),
    "worker-release-d2": (1, 1, 1, 1),
}

# The batch-size PR where the setting is never loaded (c20-d2), as the agent split it: 53 and 34 changed lines.
C20_FILES = [
    "api/src/generated/schema.ts", "api/src/repo/syncs.ts", "api/src/validation.ts", "api/test/batch_size.test.ts",
    "db/migrations/0004_sync_batch_size.sql", "schema/schema.graphql",
    "worker/harbor_worker/destinations/__init__.py", "worker/harbor_worker/generated/models.py",
    "worker/harbor_worker/models.py", "worker/harbor_worker/runner.py", "worker/tests/test_batch_size.py",
    "worker/tests/test_dry_run.py",
]
C20_STACK = [
    {"title": "Add batch size to the schema and persistence", "files": [
        "schema/schema.graphql", "db/migrations/0004_sync_batch_size.sql", "api/src/generated/schema.ts",
        "api/src/repo/syncs.ts", "api/src/validation.ts", "api/test/batch_size.test.ts",
        "worker/harbor_worker/generated/models.py", "worker/tests/test_dry_run.py"]},
    {"title": "Use the sync batch size in the worker", "files": [
        "worker/harbor_worker/destinations/__init__.py", "worker/harbor_worker/models.py",
        "worker/harbor_worker/runner.py", "worker/tests/test_batch_size.py"]},
]
C20_D2_BUG = ["worker/harbor_worker/models.py", "worker/tests/test_batch_size.py"]


def column(table: dict, i: int) -> dict[str, int]:
    return {pr: row[i] for pr, row in table.items()}


def test_noise_floor_quick():
    got = noise_floor([column(FLOOR, i) for i in range(4)])
    assert got["counts"] == [17, 17, 21, 21]
    assert got["flips"] == 13
    assert got["spread"] == 13


def test_noise_floor_standard():
    got = noise_floor([column(FLOOR, 4), column(FLOOR, 5)])
    assert got["counts"] == [25, 24]
    assert got["flips"] == 5
    assert got["spread"] == 3


def test_paired_change_standard_large_two_runs():
    base = [column(STANDARD_L, 0), column(STANDARD_L, 1)]
    split = [column(STANDARD_L, 2), column(STANDARD_L, 3)]
    assert paired_change(base, split) == (63, 79, 15)


def test_paired_change_first_run_alone_says_more():
    assert paired_change([column(STANDARD_L, 0)], [column(STANDARD_L, 2)]) == (62, 85, 23)


def test_paired_change_uses_bugs_in_both_presentations():
    base = [{"a": 1, "b": 0}, {"a": 0, "b": 0}]
    new = [{"a": 1, "c": 1}]
    assert paired_change(base, new) == (50, 100, 50)


def test_plan_problems_c20_stack_is_fine():
    assert plan_problems(C20_STACK, C20_FILES) == []


def test_plan_problems_catches_doubled_unknown_and_missing_files():
    parts = [
        {"title": "one", "files": ["a.py", "b.py", "z.py"]},
        {"title": "two", "files": ["b.py"]},
        {"title": "three", "files": []},
    ]
    assert plan_problems(parts, ["a.py", "b.py", "c.py", "d.py"]) == [
        "part 1 lists z.py, which the PR doesn't change",
        "b.py is in part 1 and part 2",
        "part 3 has no files",
        "not in any part: c.py, d.py",
    ]


def test_plan_problems_part_count():
    six = [{"title": str(i), "files": [f"{i}.py"]} for i in range(6)]
    assert plan_problems(six, [f"{i}.py" for i in range(6)]) == ["6 parts (use 1 to 5)"]


def test_defect_parts_c20_bug_is_whole_in_part_2():
    assert defect_parts(C20_D2_BUG, C20_STACK) == [2]


def test_defect_parts_a_cut_bug():
    assert defect_parts(["worker/harbor_worker/models.py", "api/src/repo/syncs.ts"], C20_STACK) == [1, 2]


def test_defects_per_arm_matches_the_lab():
    assert defects_per_arm(0.60, 0.75) == 149
    assert defects_per_arm(0.70, 0.80) == 291
    assert defects_per_arm(0.70, 0.75) == 1247
    assert defects_per_arm(0.80, 0.85) == 902
    try:
        defects_per_arm(0.7, 0.7)
    except ValueError:
        pass
    else:
        raise AssertionError("p1 == p2 should raise ValueError")


def test_break_even_seconds():
    # split 1.08¢ + walkthrough 0.45¢ a PR, at $100 an hour (2.78¢ a second)
    assert round(break_even_seconds(1.08 + 0.45, 100), 2) == 0.55
    assert round(break_even_seconds(1.53, 150), 2) == 0.37
