#!/usr/bin/env python3
"""
Dispatcher → Backplane → Helix64 smoke test.

Requires:
  - Helix64 backend running on :5065
  - helix_cdc on PYTHONPATH

Usage:
  cd /home/voidstr3m33/helix-cdc
  PYTHONPATH=. python3 tests/test_dispatcher_smoke.py
"""

import json
import sys
from typing import List

from helix_cdc.bloomos.dispatcher import dispatch_command, parse_command


def _pp(label: str, obj) -> None:
    print(f"\n=== {label} ===")
    if isinstance(obj, str):
        print(obj)
    else:
        print(json.dumps(obj, indent=2, sort_keys=True))


def run_case(cmd: str) -> None:
    print("\n" + "=" * 72)
    print(f"🧪 Command: {cmd!r}")

    intent = parse_command(cmd)
    _pp("Parsed intent", intent.__dict__)

    try:
        result = dispatch_command(cmd)
        # Show summary only
        out = {
            "task_type": result.get("task_type"),
            "gate": result.get("gate", {}).get("policy"),
            "anchors": result.get("anchors_resolved"),
            "logits_mean": round(result.get("logits_mean", 0.0), 4),
            "logits_std": round(result.get("logits_std", 0.0), 4),
            "program": result.get("gate", {}).get("program_after"),
            "dispatcher": result.get("dispatcher"),
        }
        _pp("Result summary", out)
    except Exception as e:
        print(f"[ERROR] {type(e).__name__}: {e}")


def main() -> None:
    print("\n🧬 Dispatcher → Backplane → Helix64 Smoke Test\n")

    cases: List[str] = [
        # sysop cheats
        "list.all.files",
        "print.working.directory",
        "sys.shell.ls_a",
        "ls -a",
        "pwd",
        "ps aux",
        "kill 1234",

        # natural sysop
        "show me everything in this folder",
        "what is my current directory",
        "show running processes",

        # code
        "fix this python function",
        "write a python function that adds two numbers",

        # math
        "solve the equation 2x + 5 = 13",
        "what is the derivative of x^2",

        # json
        "return a JSON schema with fields: name, age, email",
    ]

    for cmd in cases:
        run_case(cmd)

    print("\n" + "=" * 72)
    print("✅ Dispatcher smoke test complete!")
    print("=" * 72)


if __name__ == "__main__":
    main()
