"""Tests on the recorded gpt-5.4-nano runs from the video (three runs of Lab 02, Scripts 4-5).
Run: pytest test_routing_practice.py"""
from routing_practice import check_labels, easy_share, majority_vote, route

LABELS = {"digit_sum_9": "hard", "digits_3_40": "hard", "count_s": "hard", "days_between": "hard",
          "div_7_11": "hard", "pen_notebook": "easy", "printers": "easy", "capital_france": "easy",
          "add_simple": "easy"}
# The instant answer (thinking off, answer immediately): right or wrong, per question. The same
# four questions were right in all three runs.
RIGHT = {"days_between", "printers", "capital_france", "add_simple"}
INSTANT_OK = [{q: q in RIGHT for q in LABELS} for _ in range(3)]
# Run 1, thinking (medium) on all nine: the cost of each call, in dollars, at the lab's prices.
THINK_COSTS_RUN_1 = {"digit_sum_9": 0.0004917, "digits_3_40": 0.0020194, "count_s": 0.0006892,
                     "days_between": 0.0013043, "div_7_11": 0.0003612, "pen_notebook": 0.0002495,
                     "printers": 0.0004666, "capital_france": 0.0000276, "add_simple": 0.0000278}


def test_route_reads_one_word():
    assert route("HARD") == "hard"
    assert route("Hard.") == "hard"
    assert route("EASY") == "easy"
    assert route("easy") == "easy"


def test_vote_with_a_majority():
    # run 2, days between two dates: one answer said 316, the vote went to 265
    assert majority_vote(["265", "265", "265", "316", "265"]) == ("265", 4)
    assert majority_vote(["Paris", "Paris", "Paris", "Paris", "Paris"]) == ("paris", 5)


def test_a_repeated_mistake_wins():
    # run 1, the letter s (15): the single answer said 15, but 14 came up three times
    assert majority_vote(["14", "14", "14", "15", "15"]) == ("14", 3)


def test_no_majority_means_the_first_answer_wins():
    # 3^40's digit sum (90), runs 1 and 3: five different answers each time
    assert majority_vote(["90", "92", "89", "78", "87"]) == ("90", 1)
    assert majority_vote(["87", "77", "91", "90", "Unable to determine reliably without accurate computation of \\(3^{40}\\)."]) == ("87", 1)
    # "8" and "8 minutes" count as different answers, so it's a tie and "8" was seen first
    assert majority_vote(["8", "8", "8 minutes", "8 minutes", "1.6 minutes"]) == ("8", 2)


def test_your_labels():
    assert check_labels(INSTANT_OK, LABELS) == {"should_be_hard": ["pen_notebook"],
                                                 "could_be_easy": ["days_between"]}
    one_bad_run = [dict(INSTANT_OK[0]), dict(INSTANT_OK[1]), {**INSTANT_OK[2], "add_simple": False}]
    assert check_labels(one_bad_run, LABELS)["should_be_hard"] == ["add_simple", "pen_notebook"]


def test_the_most_a_router_could_save():
    assert round(easy_share(THINK_COSTS_RUN_1, LABELS) * 100, 1) == 13.7
    assert easy_share({"a": 0.0}, {"a": "easy"}) == 0.0
