from pathlib import Path
import pytest
from categorizer import FixtureClass, Speed, auto_classify

def _write(tmp_path: Path, name: str, content: str) -> Path:
 p = tmp_path / name
 p.write_text(content, encoding="utf-8")
 return p

def test_auto_classify_clean_install_filename(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_clean_install.py", "def test_x(): pass\n")
 r = auto_classify(p)
 assert r.fixture_class == FixtureClass.OPT_IN

def test_auto_classify_docker_build_filename(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_docker_build.py", "def test_x(): pass\n")
 r = auto_classify(p)
 assert r.fixture_class == FixtureClass.OPT_IN

def test_auto_classify_live_gui_fixture_in_source(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_x.py", "def test_x(live_gui): pass\n")
 r = auto_classify(p)
 assert r.fixture_class == FixtureClass.LIVE_GUI

def test_auto_classify_mock_app_fixture_in_source(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_x.py", "def test_x(mock_app): pass\n")
 r = auto_classify(p)
 assert r.fixture_class == FixtureClass.MOCK_APP

def test_auto_classify_perf_keyword_in_filename(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_xyz_stress.py", "def test_x(): pass\n")
 r = auto_classify(p)
 assert r.fixture_class == FixtureClass.PERFORMANCE

def test_auto_classify_default_to_unit(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_command_palette.py", "def test_x(): pass\n")
 r = auto_classify(p)
 assert r.fixture_class == FixtureClass.UNIT

def test_subsystem_inference_known_prefix(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_mcp_client_foo.py", "def test_x(): pass\n")
 r = auto_classify(p)
 assert "mcp" in r.subsystems

def test_speed_inference_from_durations_fast(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_x.py", "def test_x(): pass\n")
 durations = {f"{p.name}::test_x": 0.05}
 r = auto_classify(p, durations=durations)
 assert r.speed == Speed.FAST

def test_speed_default_medium_without_durations(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_x.py", "def test_x(): pass\n")
 r = auto_classify(p)
 assert r.speed == Speed.MEDIUM

def test_batch_group_inference_gui_subsystem(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_gui_layout_foo.py", "def test_x(): pass\n")
 r = auto_classify(p)
 assert r.batch_group == "gui"

import tomllib
from categorizer import load_registry, merge_registry, categorize_all

def test_load_registry_returns_dict(tmp_path: Path) -> None:
 toml = tmp_path / "reg.toml"
 toml.write_text('[files.test_x]\nfixture_class = "mock_app"\n', encoding="utf-8")
 reg = load_registry(toml)
 assert "test_x" in reg
 assert reg["test_x"]["fixture_class"] == "mock_app"

def test_merge_registry_overrides_auto(tmp_path: Path) -> None:
 p = _write(tmp_path, "test_x.py", "def test_x(): pass\n")
 auto = auto_classify(p)
 assert auto.fixture_class == FixtureClass.UNIT
 reg_entry = {"fixture_class": "mock_app", "subsystems": ["x"], "speed": "fast", "batch_group": "x"}
 merged = merge_registry(auto, reg_entry)
 assert merged.fixture_class == FixtureClass.MOCK_APP
 assert merged.source == "registry"
 assert merged.subsystems == ["x"]

def test_categorize_all_handles_real_tests_dir(tmp_path: Path) -> None:
 (tmp_path / "test_a.py").write_text("def test_x(): pass\n", encoding="utf-8")
 (tmp_path / "test_b_sim.py").write_text("def test_x(live_gui): pass\n", encoding="utf-8")
 reg_path = tmp_path / "reg.toml"
 reg_path.write_text("", encoding="utf-8")
 records = categorize_all(tmp_path, reg_path)
 assert len(records) == 2
 by_name = {r.filename: r for r in records}
 assert by_name["test_a.py"].fixture_class == FixtureClass.UNIT
 assert by_name["test_b_sim.py"].fixture_class == FixtureClass.LIVE_GUI
