#!/usr/bin/env python3
"""Test TUI with simulated input."""

import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

os.environ["ZAI_API_KEY"] = os.environ.get("ZAI_API_KEY", "test-key")

from core.zai_client import ZAiClient
from core.salience import SalienceEngine
from core.blackboard import Blackboard
from agents.tlp import TLPGateLoop, Phase


def test_modules():
    """Test all modules can be imported and instantiated."""
    print("[1] Testing ZAiClient...")
    try:
        client = ZAiClient(os.environ["ZAI_API_KEY"])
        client.close()
        print("    OK")
    except Exception as e:
        print(f"    FAIL: {e}")
        return False

    print("[2] Testing SalienceEngine...")
    try:
        engine = SalienceEngine()
        l_sal = engine.calculate_l_sal(0.5, 0.2, 0.3, 0.9, 0.1, 1.0)
        print(f"    OK - L_sal = {l_sal:.4f}")
    except Exception as e:
        print(f"    FAIL: {e}")
        return False

    print("[3] Testing Blackboard...")
    try:
        bb = Blackboard()
        bb.write("test", "value", "test")
        v = bb.read("test")
        print(f"    OK - read: {v}")
    except Exception as e:
        print(f"    FAIL: {e}")
        return False

    print("[4] Testing TLP...")
    try:
        bb = Blackboard()
        tlp = TLPGateLoop(bb)
        tlp.step()
        print(f"    OK - phase: {tlp.current_phase.name}")
    except Exception as e:
        print(f"    FAIL: {e}")
        return False

    print()
    print("[OK] All TUI modules working")
    return True


if __name__ == "__main__":
    test_modules()
