"""Tests on Lab 05's recorded runs (October 2026, Apple M4 Max, Docker Desktop with 16 vCPU,
Postgres 18.6): 01_lost_updates.py (the naive transfer with 1 and 32 workers, and six ways to
write it, 3 runs each), 03_isolation.py (the step-by-step plays and the doctors) and
04_hot_row.py (1,000 buyers for 100 seats).
Run: pytest test_race_practice.py"""

from race_practice import (booking_check, change, gained_and_lost, median_and_range, play_doctors,
                           play_withdrawals, violations)


def test_violations_clean():
    v = violations({1: 90, 2: 110}, [(1, 2, 10)], initial=100)
    assert v == {"money_created": 0, "negative": 0, "ledger_mismatch": 0, "misplaced": 0, "ok": True}


def test_a_lost_update_that_keeps_the_total_is_still_caught():
    # Two transfers of 10 from account 1 to 2; one overwrote the other on both rows.
    # The total is right; the ledger says 20 moved, the balances say 10.
    v = violations({1: 90, 2: 110}, [(1, 2, 10), (1, 2, 10)], initial=100)
    assert v["money_created"] == 0 and v["ledger_mismatch"] == 2 and v["misplaced"] == 20
    assert not v["ok"]


def test_money_created_and_negative():
    v = violations({1: -5, 2: 220}, [], initial=100)
    assert v["money_created"] == 15 and v["negative"] == 1 and not v["ok"]


def test_the_default_start_is_1000():
    v = violations({1: 990, 2: 1010}, [(1, 2, 10)])
    assert v["ok"]


def test_gained_and_lost():
    # 32 workers, 10,000 naive transfers: 14,277 misplaced, the total off by -793
    assert gained_and_lost(14_277, -793) == (6_742, 7_535)
    # Exercise 2's three naive runs: the total moved by only 48, 187 and 1,198
    assert gained_and_lost(13_832, -48) == (6_892, 6_940)
    assert gained_and_lost(13_307, -187) == (6_560, 6_747)
    assert gained_and_lost(12_708, -1_198) == (5_755, 6_953)
    assert gained_and_lost(0, 0) == (0, 0)  # one worker: perfect


def test_the_lost_update_played():
    # 03_isolation.py, Exercise 1, read committed: "balance 80, should be 70"
    r = play_withdrawals(100, 10, 20, "naive")
    assert r == {"balance": 80, "answers": ["ok", "ok"], "retries": 0}


def test_each_fix_ends_at_70():
    assert play_withdrawals(100, 10, 20, "atomic") == {"balance": 70, "answers": ["ok", "ok"], "retries": 0}
    assert play_withdrawals(100, 10, 20, "lock") == {"balance": 70, "answers": ["ok", "ok"], "retries": 0}
    assert play_withdrawals(100, 10, 20, "version") == {"balance": 70, "answers": ["ok", "ok"], "retries": 1}


def test_not_enough_for_both():
    # The naive code says ok twice and pays out 110 from 100 (the balance shows 50).
    assert play_withdrawals(100, 60, 50, "naive") == {"balance": 50, "answers": ["ok", "ok"], "retries": 0}
    for mode in ("atomic", "lock"):
        assert play_withdrawals(100, 60, 50, mode) == {"balance": 40, "answers": ["ok", "insufficient"], "retries": 0}
    assert play_withdrawals(100, 60, 50, "version") == {"balance": 40, "answers": ["ok", "insufficient"], "retries": 1}


def test_write_skew_played():
    # 03_isolation.py, Exercise 1: 0 doctors on call under read committed AND repeatable read
    assert play_doctors("read committed") == (0, 0)
    assert play_doctors("repeatable read") == (0, 0)
    assert play_doctors("serializable") == (1, 1)
    assert play_doctors("lock") == (1, 0)


def test_the_last_seat():
    # Check, then book: 1,000 told "booked", only seats 1-74 taken
    seats = list(range(1, 75)) + [1 + i % 74 for i in range(926)]
    v = booking_check(100, seats)
    assert v == {"bookings": 1000, "oversold": 900, "distinct": 74, "on_taken": 926, "ok": False}
    # The unique rule and the counter: exactly 100, each seat once
    assert booking_check(100, list(range(1, 101)))["ok"]
    assert not booking_check(3, [0, 4])["ok"]  # seat numbers out of range


def test_median_and_range():
    # transfers a second, 3 interleaved runs each, 1,000 accounts
    naive = [7_414.0, 7_033.0, 7_140.1]
    atomic = [8_626.0, 8_749.0, 8_428.0]
    assert median_and_range(naive) == (7_140.1, 7_033.0, 7_414.0)
    assert median_and_range(atomic) == (8_626.0, 8_428.0, 8_749.0)


def test_change_against_the_broken_code():
    naive = 7_140.06
    assert round(change(8_625.97, naive), 2) == 0.21  # the atomic update: 21% faster than the broken code
    assert round(change(5_755.16, naive), 2) == -0.19  # serializable: about 20% slower
    assert round(change(6_755.54, naive), 2) == -0.05  # repeatable read
    assert round(change(7_414.93, naive), 2) == 0.04  # the row lock
