"""Tests on Lab 05's recorded run (October 2026): ten checks on 77 bases and 250 PRs, 173 of them with a
planted bug and 77 clean. The per-PR rows below come from the lab's results/runs/*.jsonl and
results/catch_labels.jsonl (the "exposes" labels are by an AI). Run: pytest test_tools_practice.py"""
from tools_practice import diff_ratio, flags_near, gate_price, new_findings

GATES = {"tsc", "eslint", "ruff", "mypy", "semgrep", "secrets", "property", "contract", "browser"}

# Clean PR -> the checks that turn it red, with each tool's default rules. The 14 clean PRs not
# listed pass every check. "mutation" is a surviving mutant (no test pins a line the PR adds).
CLEAN_DEFAULT = {
    "api-cleanup-clean": ["tsc", "eslint"],
    "api-release-clean": ["tsc", "eslint", "mutation"],
    "c01-clean": ["eslint"],
    "c02-clean": ["eslint"],
    "c03-clean": ["tsc", "eslint"],
    "c04-clean": ["eslint"],
    "c06-clean": ["ruff"],
    "c07-clean": ["ruff"],
    "c11-clean": ["eslint"],
    "c12-clean": ["eslint"],
    "c16-clean": ["ruff", "property"],
    "c18-clean": ["eslint", "mutation"],
    "c19-clean": ["eslint", "mutation"],
    "c20-clean": ["eslint"],
    "c21-clean": ["eslint"],
    "c22-clean": ["eslint", "ruff", "mutation"],
    "c23-clean": ["eslint", "mutation"],
    "c24-clean": ["eslint", "mutation"],
    "c25-clean": ["eslint", "property", "mutation"],
    "c26-clean": ["eslint", "mutation"],
    "c27-clean": ["mutation"],
    "c28-clean": ["mypy", "mutation"],
    "c33-clean": ["mutation"],
    "c35-clean": ["ruff", "mutation"],
    "c36-clean": ["eslint", "mutation"],
    "c40-clean": ["tsc", "eslint", "mutation"],
    "n01-clean": ["eslint", "mutation"],
    "n02-clean": ["eslint"],
    "n03-clean": ["eslint"],
    "n04-clean": ["eslint", "mutation"],
    "n05-clean": ["tsc", "eslint", "mutation"],
    "n06-clean": ["eslint"],
    "n07-clean": ["eslint", "mutation"],
    "n08-clean": ["eslint", "mutation"],
    "n09-clean": ["eslint", "mutation"],
    "n10-clean": ["eslint", "mutation"],
    "n11-clean": ["mypy", "mutation"],
    "n13-clean": ["ruff", "mutation"],
    "n14-clean": ["ruff", "mutation"],
    "n15-clean": ["mypy", "mutation"],
    "n16-clean": ["ruff", "mutation"],
    "n17-clean": ["ruff", "semgrep", "mutation"],
    "n18-clean": ["mutation"],
    "n19-clean": ["mutation"],
    "n20-clean": ["mutation"],
    "n21-clean": ["mutation"],
    "n23-clean": ["mutation"],
    "n24-clean": ["mutation"],
    "n25-clean": ["mutation"],
    "n27-clean": ["browser", "mutation"],
    "n28-clean": ["mutation"],
    "n29-clean": ["eslint", "mutation"],
    "n30-clean": ["eslint"],
    "n31-clean": ["eslint", "mutation"],
    "n32-clean": ["eslint", "mutation"],
    "n33-clean": ["eslint", "mutation"],
    "n34-clean": ["eslint", "secrets", "mutation"],
    "n35-clean": ["eslint", "mutation"],
    "n36-clean": ["eslint", "ruff", "mutation"],
    "n37-clean": ["eslint", "mypy", "mutation"],
    "n38-clean": ["eslint", "ruff", "semgrep", "mutation"],
    "worker-delivery-clean": ["ruff"],
    "worker-release-clean": ["ruff"],
}

# The same, with lint and type rules cut to the 11 whose catches exposed a bug. The test checks
# don't change; the secret scanner's rule isn't among the 11.
CLEAN_CUT = {
    "api-release-clean": ["mutation"],
    "c16-clean": ["property"],
    "c18-clean": ["mutation"],
    "c19-clean": ["mutation"],
    "c22-clean": ["mutation"],
    "c23-clean": ["mutation"],
    "c24-clean": ["mutation"],
    "c25-clean": ["property", "mutation"],
    "c26-clean": ["mutation"],
    "c27-clean": ["mutation"],
    "c28-clean": ["mutation"],
    "c33-clean": ["mutation"],
    "c35-clean": ["mutation"],
    "c36-clean": ["mutation"],
    "c40-clean": ["tsc", "mutation"],
    "n01-clean": ["mutation"],
    "n02-clean": ["eslint"],
    "n04-clean": ["mutation"],
    "n05-clean": ["tsc", "eslint", "mutation"],
    "n07-clean": ["mutation"],
    "n08-clean": ["mutation"],
    "n09-clean": ["mutation"],
    "n10-clean": ["eslint", "mutation"],
    "n11-clean": ["mutation"],
    "n13-clean": ["mutation"],
    "n14-clean": ["mutation"],
    "n15-clean": ["mutation"],
    "n16-clean": ["mutation"],
    "n17-clean": ["mutation"],
    "n18-clean": ["mutation"],
    "n19-clean": ["mutation"],
    "n20-clean": ["mutation"],
    "n21-clean": ["mutation"],
    "n23-clean": ["mutation"],
    "n24-clean": ["mutation"],
    "n25-clean": ["mutation"],
    "n27-clean": ["browser", "mutation"],
    "n28-clean": ["mutation"],
    "n29-clean": ["eslint", "mutation"],
    "n31-clean": ["mutation"],
    "n32-clean": ["mutation"],
    "n33-clean": ["mutation"],
    "n34-clean": ["mutation"],
    "n35-clean": ["mutation"],
    "n36-clean": ["mutation"],
    "n37-clean": ["mutation"],
    "n38-clean": ["eslint", "mutation"],
}

# Buggy PR -> the checks whose catch exposes its bug. The other 154 buggy PRs: none.
EXPOSED_BY = {
    "c07-d1": ["property"],
    "c07-d2": ["property"],
    "c08-d1": ["property"],
    "c11-d2": ["eslint"],
    "c13-d1": ["mypy"],
    "c25-d1": ["ruff", "mypy"],
    "c27-d2": ["ruff"],
    "n01-d1": ["eslint"],
    "n03-d1": ["tsc", "eslint"],
    "n09-d2": ["eslint"],
    "n10-d1": ["tsc", "eslint"],
    "n24-d4": ["semgrep"],
    "n26-d2": ["eslint"],
    "n27-d3": ["tsc"],
    "n31-d3": ["eslint"],
    "n32-d1": ["browser"],
    "n32-d2": ["browser"],
    "n32-d3": ["eslint"],
    "worker-delivery-d2": ["property"],
}


# ---------------------------------------------------------------------------------------------
# new_findings: only what the PR adds

# Findings on a base. Line numbers in this block are made up; the messages are the tools' own.
BASE = [
    ("eslint", "@typescript-eslint/no-unsafe-member-access", "api/test/destinations.test.ts", 10, "Unsafe member access .destinations on an `any` value."),
    ("eslint", "@typescript-eslint/no-unsafe-member-access", "api/test/destinations.test.ts", 18, "Unsafe member access .destinations on an `any` value."),
    ("tsc", "TS2769", "api/test/run_order.test.ts", 5, "No overload matches this call."),
]


def test_a_moved_finding_is_not_new():
    pr = [("tsc", "TS2769", "api/test/run_order.test.ts", 9, "No overload matches this call.")]
    assert new_findings(pr, BASE) == []


def test_an_extra_copy_is_new():
    pr = BASE[:2] + [
        ("eslint", "@typescript-eslint/no-unsafe-member-access", "api/test/destinations.test.ts", 25, "Unsafe member access .destinations on an `any` value."),
    ]
    assert new_findings(pr, BASE) == [pr[2]]


def test_the_unused_now_on_n03_d1_is_new():
    now = ("tsc", "TS6133", "api/src/repo/runs.ts", 58, "'now' is declared but its value is never read.")
    assert new_findings(BASE + [now], BASE) == [now]


# ---------------------------------------------------------------------------------------------
# flags_near: within 3 lines of the bug, and not on the clean twin

N03_LINES = {"api/src/repo/runs.ts": [61]}
NOW = ("tsc", "TS6133", "api/src/repo/runs.ts", 58, "'now' is declared but its value is never read.")


def test_flags_near_the_bug():
    assert flags_near(N03_LINES, [NOW], []) == [NOW]


def test_not_when_the_clean_twin_has_it_too():
    assert flags_near(N03_LINES, [NOW], [NOW]) == []


def test_not_four_lines_away_or_in_another_file():
    far = ("tsc", "TS6133", "api/src/repo/runs.ts", 57, "'now' is declared but its value is never read.")
    other = ("tsc", "TS6133", "api/src/repo/syncs.ts", 61, "'x' is declared but its value is never read.")
    assert flags_near(N03_LINES, [far, other], []) == []


def test_an_empty_line_list_means_the_whole_file():
    assert flags_near({"api/src/repo/runs.ts": []}, [NOW], []) == [NOW]


# ---------------------------------------------------------------------------------------------
# gate_price: what a set of required checks buys and costs

def test_every_gate_with_default_rules():
    assert gate_price(GATES, CLEAN_DEFAULT, EXPOSED_BY) == {"exposed": 19, "blocked": 53, "blocked_pct": 69}


def test_eslint_alone_blocks_half_the_clean_prs():
    assert gate_price({"eslint"}, CLEAN_DEFAULT, EXPOSED_BY) == {"exposed": 8, "blocked": 39, "blocked_pct": 51}


def test_cut_to_eleven_rules_keeps_every_exposed_bug():
    assert gate_price(GATES, CLEAN_CUT, EXPOSED_BY) == {"exposed": 19, "blocked": 9, "blocked_pct": 12}


def test_mutation_testing_as_a_gate_blocks_most_clean_prs():
    assert gate_price({"mutation"}, CLEAN_DEFAULT, EXPOSED_BY)["blocked"] == 45
    assert gate_price(GATES | {"mutation"}, CLEAN_DEFAULT, EXPOSED_BY)["blocked_pct"] == 82


def test_tests_written_on_main():
    assert gate_price({"property"}, CLEAN_DEFAULT, EXPOSED_BY) == {"exposed": 4, "blocked": 2, "blocked_pct": 3}
    assert gate_price({"contract"}, CLEAN_DEFAULT, EXPOSED_BY) == {"exposed": 0, "blocked": 0, "blocked_pct": 0}


# ---------------------------------------------------------------------------------------------
# diff_ratio: a masked cell that changes width still moves the page

W, PINK, BLACK = (255, 255, 255), (255, 0, 255), (0, 0, 0)


def row(mask_width, total=10):
    """A one-row table: a pink mask over the time, then a column border right after it."""
    return [PINK] * mask_width + [BLACK] + [W] * (total - mask_width - 1)


def test_identical_screenshots():
    assert diff_ratio([row(4)], [row(4)]) == 0.0


def test_anti_aliasing_within_tolerance_is_ignored():
    assert diff_ratio([[(200, 200, 200)]], [[(210, 190, 200)]]) == 0.0


def test_a_mask_that_shrinks_still_flags():
    # Masked to 5 pixels, then 3: the text under the mask set the width, so the border moves.
    assert diff_ratio([row(5)], [row(3)]) == 0.3


def test_different_sizes_count_as_fully_changed():
    assert diff_ratio([row(4)], [row(4), row(4)]) == 1.0
