"""Tests on Lab 06's recorded runs (28 October 2026, Apple M4 Max, Docker Desktop with 16 vCPU):
02_pagination.py (50 pages of 100 from a 20,000-row copy of the newest orders, while orders are
added on top and already-read orders are removed) and 06_contracts.py (which schema changes break
old clients). The Protobuf bytes were written by the lab's own code for the order with id 9876543
and a total of 12,345 cents.
Run: pytest test_page_practice.py"""
from page_practice import (breaking_json, cursor_page, duplicates_and_skips, expected_errors, offset_page,
                           paging_run, read_varint, read_whole_number)

STACK = list(range(50, 35, -1))  # the chapter's small stack: #50 down to #36, newest first


def test_offset_and_cursor_pages():
    assert offset_page(STACK, 0, 5) == [50, 49, 48, 47, 46]
    assert offset_page(STACK, 1, 5) == [45, 44, 43, 42, 41]
    assert cursor_page(STACK, None, 5) == [50, 49, 48, 47, 46]
    assert cursor_page(STACK, 46, 5) == [45, 44, 43, 42, 41]
    assert cursor_page(STACK, 37, 5) == [36]  # the last page can be short


def test_the_small_stack_changes():
    # two new receipts on top: offset repeats #47 and #46, the cursor doesn't
    grown = [52, 51] + STACK
    assert offset_page(grown, 1, 5) == [47, 46, 45, 44, 43]
    assert cursor_page(grown, 46, 5) == [45, 44, 43, 42, 41]
    # two already-read receipts removed: offset never shows #45 and #44
    shrunk = [i for i in STACK if i not in (50, 49)]
    assert offset_page(shrunk, 1, 5) == [43, 42, 41, 40, 39]
    assert cursor_page(shrunk, 46, 5) == [45, 44, 43, 42, 41]


def test_duplicates_and_skips():
    present = set(range(36, 51))
    clean = duplicates_and_skips([[50, 49, 48, 47, 46], [45, 44, 43, 42, 41]], present, 41, 50)
    assert clean == {"rows_returned": 10, "duplicates": 0, "skips": 0}
    again = duplicates_and_skips([[50, 49, 48, 47, 46], [47, 46, 45, 44, 43]], present, 43, 50)
    assert again == {"rows_returned": 10, "duplicates": 2, "skips": 0}
    # #50 and #49 were removed, so they don't count as skips; #45 and #44 do
    lost = duplicates_and_skips([[50, 49, 48, 47, 46], [43, 42, 41, 40, 39]], present - {50, 49}, 39, 50)
    assert lost == {"rows_returned": 10, "duplicates": 0, "skips": 2}


def test_paging_run_matches_the_lab():
    # the lab's four settings, as recorded: offset, then cursor
    assert paging_run("offset", 0, 0) == {"rows_returned": 5000, "duplicates": 0, "skips": 0}
    assert paging_run("offset", 5, 0) == {"rows_returned": 5000, "duplicates": 245, "skips": 0}
    assert paging_run("offset", 0, 2) == {"rows_returned": 5000, "duplicates": 0, "skips": 98}
    assert paging_run("offset", 5, 2) == {"rows_returned": 5000, "duplicates": 147, "skips": 0}
    for ins, dels in ((0, 0), (5, 0), (0, 2), (5, 2)):
        assert paging_run("cursor", ins, dels) == {"rows_returned": 5000, "duplicates": 0, "skips": 0}


def test_which_rows_are_removed_doesnt_matter():
    assert paging_run("offset", 0, 2, seed=1)["skips"] == 98
    assert paging_run("offset", 5, 2, seed=7)["duplicates"] == 147


def test_expected_errors():
    assert expected_errors(50, 5, 0) == (245, 0)  # 49 gaps x 5
    assert expected_errors(50, 0, 2) == (0, 98)  # 49 gaps x 2
    assert expected_errors(50, 5, 2) == (147, 0)  # 49 x (5 - 2): nothing lost
    assert expected_errors(50, 3, 3) == (0, 0)  # offset looks clean, by luck
    run = paging_run("offset", 2, 5)
    assert expected_errors(50, 2, 5) == (run["duplicates"], run["skips"]) == (0, 147)


def test_read_varint():
    assert read_varint(bytes([0x2A]), 0) == (42, 1)
    assert read_varint(bytes([0xB9, 0x60]), 0) == (12345, 2)
    assert read_varint(bytes([0x28, 0xB9, 0x60]), 1) == (12345, 3)


V1 = bytes.fromhex("08bfe8da04102a1a0773686970706564208084aef29cd08d0328b9603211"
                   "6c656176652061742074686520646f6f72")
RENUMBERED = bytes.fromhex("08bfe8da04102a1a0773686970706564208084aef29cd08d03"
                           "32116c656176652061742074686520646f6f7238b960")
AS_TEXT = bytes.fromhex("08bfe8da04102a1a0773686970706564208084aef29cd08d032a053132333435"
                        "32116c656176652061742074686520646f6f72")
WITH_CURRENCY = V1 + bytes.fromhex("3a03455552")


def test_old_reader_on_new_bytes():
    assert read_whole_number(V1, 1) == 9876543  # the id, box 1
    assert read_whole_number(V1, 5) == 12345  # the total, box 5
    assert read_whole_number(WITH_CURRENCY, 5) == 12345  # a new box 7 is skipped
    assert read_whole_number(RENUMBERED, 5) == 0  # moved to box 7: no error, a total of 0
    assert read_whole_number(RENUMBERED, 7) == 12345  # it's still there, in a box the old form doesn't know
    assert read_whole_number(AS_TEXT, 5) == 0  # box 5 now holds text: skipped, 0


def test_breaking_json():
    v1 = {"id": {"integer"}, "customer_id": {"integer"}, "status": {"string"},
          "created_at": {"string"}, "total_cents": {"integer"}, "note": {"string"}}
    assert breaking_json(v1, dict(v1, currency={"string"})) == []
    renamed = {k: v for k, v in v1.items() if k != "total_cents"} | {"amount_cents": {"integer"}}
    assert breaking_json(v1, renamed) == ["removed: total_cents"]  # a rename is a removal
    assert breaking_json(v1, dict(v1, id={"string"})) == ["type changed: id"]
    assert breaking_json(v1, dict(v1, note={"string", "null"})) == ["type changed: note"]
    assert breaking_json(v1, {k: v for k, v in v1.items() if k != "note"}) == ["removed: note"]
