"""Tests from Lab 01's recorded runs (gpt-5.4-nano; judge gpt-5.4-mini). Run: pytest test_llm_practice.py"""
from llm_practice import alike, compare, position_bias, unfair_cases, verdict, was_cut

OCEAN_T0 = [
    "The ocean stretches endlessly, a vast blue mirror that breathes with tides and whispers secrets to the shore.",
    "The ocean stretches endlessly, a living ribbon of blue that breathes with tides, flashes with sunlight, and keeps its secrets deep beneath the waves.",
    "The ocean stretches endlessly, a restless ribbon of blue that glitters in the sun and murmurs like a secret to the shore.",
]
OCEAN_T1 = [
    "The ocean stretched endlessly, a living sheet of blue that shimmered with sunlight, swallowed the horizon in its quiet breath, and carried the salt-scented weight of storms far beyond sight.",
    "The ocean stretched endlessly beneath a bruised sky, its restless waves glittering like moving glass as they breathed in and out against the shore.",
    "The ocean sprawls endlessly, a restless ribbon of blue that mirrors the sky by day and swallows stars by night.",
]

# The lab's own A/B test: max_completion_tokens=300, one order
LAB = [
    {"input": "My payment failed but I was still charged. What do I do?", "a_cut": True, "b_cut": False},
    {"input": "I can't log into my account. I've tried resetting my password but the email never arrives.", "a_cut": True, "b_cut": False},
    {"input": "How do I export all my data? I want to cancel my subscription.", "a_cut": True, "b_cut": False},
    {"input": "Your app crashed and I lost 2 hours of work.", "a_cut": True, "b_cut": False},
]

# The fairer test: limit 1,200, every pair judged twice ([A shown first, B shown first])
FAIR = [
    {"input": "payment failed", "a": [6, 4], "b": [8, 7]},
    {"input": "can't log in", "a": [8, 9], "b": [5, 6]},
    {"input": "export my data", "a": [4, 3], "b": [8, 8]},
    {"input": "app crashed", "a": [6, 4], "b": [3, 3]},
    {"input": "charged for 5 seats", "a": [7, 7], "b": [9, 8]},
    {"input": "is my data safe?", "a": [8, 6], "b": [6, 7]},
    {"input": "add a teammate", "a": [8, 6], "b": [9, 8]},
    {"input": "mobile app logs me out", "a": [8, 8], "b": [6, 7]},
]


def test_cut_off_answers_are_detectable():
    cut = {"choices": [{"finish_reason": "length", "message": {"content": "…the foundation for many modern AI"}}]}
    done = {"choices": [{"finish_reason": "stop", "message": {"content": "The cat sat on the mat."}}]}
    assert was_cut(cut) is True
    assert was_cut(done) is False


def test_temperature_zero_is_less_varied_not_identical():
    assert alike(OCEAN_T0) < 1.0               # three different sentences at temperature 0
    assert alike(OCEAN_T0) > alike(OCEAN_T1)   # but more alike than at temperature 1
    assert alike(["same words", "same words"]) == 1.0
    assert alike(["only one run"]) == 1.0


def test_the_lab_test_compared_the_limit():
    assert unfair_cases(LAB) == [c["input"] for c in LAB]
    assert unfair_cases([{"input": "x", "a_cut": True, "b_cut": True}]) == []


def test_judging_both_orders():
    assert verdict([6, 4], [8, 7]) == "B"
    assert verdict([8, 9], [5, 6]) == "A"
    assert verdict([8, 6], [6, 7]) == "flips"     # the security question
    assert verdict([7, 6], [7, 5]) == "tie"
    assert compare(FAIR) == {"A": 3, "B": 4, "flips": 1, "tie": 0}


def test_the_answer_shown_first_scores_higher():
    first, second = position_bias(FAIR)
    assert (first, second) == (6.81, 6.31)
    assert first - second > 0.4
