"""test_detect_select.py — select_detectors routing units (pure, deterministic, no LLM). Fixture manifests."""
import importlib.util, os, tempfile

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def _load(name, path):
    spec = importlib.util.spec_from_file_location(name, path)
    mod = importlib.util.module_from_spec(spec); spec.loader.exec_module(mod); return mod


detect = _load("sg_detect", os.path.join(ROOT, "orchestrator", "detect.py"))

BASELINE = {"id": "baseline", "kind": "llm", "applies_to": {"kinds": [], "signal": "", "always": True}}
AUTH = {"id": "auth", "kind": "llm",
        "applies_to": {"kinds": ["http-file-route"], "signal": r"\b(token|session)\b", "always": False}}
FINANCE = {"id": "finance", "kind": "llm",
           "applies_to": {"kinds": ["http-defn-call"], "signal": r"\b(payout|refund)\b", "always": False}}
ORACLE = {"id": "oracle", "exec": ["bun", "x"], "scope": "per-file", "class": "S9"}  # not kind==llm
NOAPPLIES = {"id": "weird", "kind": "llm"}  # no applies_to → never routed


def _tmpfile(text):
    fd, p = tempfile.mkstemp(suffix=".ts")
    with os.fdopen(fd, "w") as fh:
        fh.write(text)
    return p


def _ids(sel):
    return [d["id"] for d in sel]


def test_baseline_always_included_even_with_no_match():
    p = _tmpfile("const x = 1\n")
    sel = detect.select_detectors(p, "unknown-kind", [BASELINE, AUTH, FINANCE])
    assert _ids(sel) == ["baseline"]


def test_kind_match_includes_specialist():
    p = _tmpfile("const x = 1\n")  # no signal hit; routed by KIND
    sel = detect.select_detectors(p, "http-file-route", [BASELINE, AUTH])
    assert set(_ids(sel)) == {"baseline", "auth"}


def test_signal_match_includes_specialist_on_content():
    p = _tmpfile("if (refund > 0) doPayout()\n")  # finance kind is http-defn-call; here matched by SIGNAL
    sel = detect.select_detectors(p, "graphql-resolver", [BASELINE, FINANCE])
    assert set(_ids(sel)) == {"baseline", "finance"}


def test_always_true_specialist_included():
    always = {"id": "everywhere", "kind": "llm", "applies_to": {"always": True}}
    p = _tmpfile("const x = 1\n")
    sel = detect.select_detectors(p, "x", [BASELINE, always])
    assert set(_ids(sel)) == {"baseline", "everywhere"}


def test_no_applies_to_not_routed():
    p = _tmpfile("const token = 1\n")
    sel = detect.select_detectors(p, "x", [BASELINE, NOAPPLIES])
    assert _ids(sel) == ["baseline"]


def test_non_llm_detector_skipped():
    p = _tmpfile("const x = 1\n")
    sel = detect.select_detectors(p, "x", [BASELINE, ORACLE])
    assert _ids(sel) == ["baseline"]


def test_never_narrower_routes_baseline_plus_all_matches():
    p = _tmpfile("session refund payout token\n")
    sel = detect.select_detectors(p, "http-file-route", [BASELINE, AUTH, FINANCE])
    assert set(_ids(sel)) == {"baseline", "auth", "finance"}


def test_baseline_absent_returns_none():
    p = _tmpfile("const x = 1\n")
    assert detect.select_detectors(p, "x", [AUTH, FINANCE]) is None
