"""Tests on the recorded gpt-5.4-nano runs from the video (three runs of Lab 02, Scripts 1-3).
Run: pytest test_effort_practice.py"""
from effort_practice import call_cost, lowest_effort, per_thousand, split_output

# "Count the letter s ... give only the answer", thinking on (medium). The reply was "FINAL: 15".
LETTER_S = [
    {"prompt_tokens": 56, "completion_tokens": 477, "completion_tokens_details": {"reasoning_tokens": 464}},
    {"prompt_tokens": 56, "completion_tokens": 533, "completion_tokens_details": {"reasoning_tokens": 520}},
    {"prompt_tokens": 56, "completion_tokens": 331, "completion_tokens_details": {"reasoning_tokens": 318}},
]
# The nine-question harness: right answers in runs 1, 2 and 3, per effort level.
HARNESS = {"none": [6, 9, 7], "low": [8, 9, 9], "medium": [9, 9, 9], "high": [9, 9, 9], "xhigh": [9, 9, 9]}


def test_the_hidden_bill():
    visible, hidden, share = split_output(LETTER_S[0])
    assert (visible, hidden) == (13, 464)
    assert round(share * 100) == 97
    assert [round(split_output(u)[2] * 100) for u in LETTER_S] == [97, 98, 96]


def test_no_thinking_means_no_hidden_tokens():
    assert split_output({"prompt_tokens": 38, "completion_tokens": 16}) == (16, 0, 0.0)
    assert split_output({"prompt_tokens": 38, "completion_tokens": 16,
                         "completion_tokens_details": {"reasoning_tokens": None}}) == (16, 0, 0.0)
    assert split_output({"prompt_tokens": 5, "completion_tokens": 0}) == (0, 0, 0.0)


def test_hidden_tokens_are_billed_at_the_output_price():
    assert round(call_cost(LETTER_S[0]), 6) == 0.000607
    # the 464 hidden tokens are 95.5% of the cost, input included
    hidden_cost = 464 * 1.25 / 1_000_000
    assert round(hidden_cost / call_cost(LETTER_S[0]) * 100, 1) == 95.5


def test_cost_per_thousand_questions():
    # run 1's medium level cost $0.005357 for the nine questions; effort none, $0.001845
    assert round(per_thousand(0.005357, 9), 2) == 0.60
    assert round(per_thousand(0.001845, 9), 2) == 0.20


def test_lowest_level_that_clears_the_bar_in_every_run():
    assert lowest_effort(HARNESS, 9) == "medium"
    assert lowest_effort(HARNESS, 8) == "low"
    assert lowest_effort(HARNESS, 6) == "none"
    assert lowest_effort(HARNESS, 10) is None


def test_one_run_can_fool_you():
    run_2_only = {e: [runs[1]] for e, runs in HARNESS.items()}
    assert lowest_effort(run_2_only, 9) == "none"   # run 2 alone says: turn thinking off
    assert lowest_effort({"medium": [9, 9, 9], "low": [8, 9, 9]}, 9) == "medium"
