"""Pure-logic tests for The Bridge perf module (no sudo, hardware-tolerant).

    cd tools/bridge && python3 -m pytest tests/ -q
"""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import perf as P


def test_temp_state_thresholds():
    assert P.temp_state(None) == "unknown"
    assert P.temp_state(40) == "cool"
    assert P.temp_state(54) == "cool"
    assert P.temp_state(60) == "warm"
    assert P.temp_state(75) == "hot"
    assert P.temp_state(90) == "critical"


def test_modes_shape():
    assert set(P.MODES) == {"cool", "standard", "extreme", "normal"}
    assert P.MODES["extreme"][1] == "--extreme"
    assert P.MODES["normal"][1] == "--stop"


def test_set_mode_rejects_unknown():
    ok, msg = P.set_mode("turbo")
    assert ok is False and "unknown mode" in msg


def test_set_mode_guards_missing_script(monkeypatch):
    monkeypatch.setattr(P, "script_available", lambda: False)
    ok, msg = P.set_mode("cool")
    assert ok is False and "not deployed" in msg


def test_snapshot_shape():
    s = P.snapshot()
    for k in ("mode", "package_c", "temp_state", "cores_c", "fans_rpm",
              "freq_max_mhz", "throttle_delta", "script_available", "modes"):
        assert k in s
    assert s["mode"] in P.MODES
    assert isinstance(s["cores_c"], list)
    assert isinstance(s["script_available"], bool)


def test_detect_mode_returns_known():
    assert P.detect_mode() in P.MODES
