#!/usr/bin/env python3
"""Mock 测试脚本 - 模拟 QQ 消息输入。"""

import asyncio
import json
import sys
import httpx


async def send_message(user_id: str, content: str):
    """发送自然语言消息。"""
    async with httpx.AsyncClient() as client:
        response = await client.post(
            "http://localhost:9600/api/mock/message",
            json={"user_id": user_id, "content": content},
            headers={"Content-Type": "application/json"},
            timeout=60.0,
        )
        if response.status_code != 200:
            print(f"[ERROR] {response.status_code}: {response.text}")
            return

        data = response.json()
        mode = data.get("mode", "unknown")
        command = data.get("command", "")
        reply = data.get("reply", "")

        if command:
            print(f"  命令: {command}")
        print(f"  回复: {reply}")


async def get_raw_output(user_id: str, limit: int = 5):
    """获取原始输出日志。"""
    async with httpx.AsyncClient() as client:
        response = await client.get(
            "http://localhost:9600/api/raw-output",
            params={"user_id": user_id, "limit": limit},
            timeout=30.0,
        )
        data = response.json()
        for log in data.get("logs", []):
            print(f"  [{log['timestamp']}] {log['command']}")
            if log["output"]:
                # 截取前 200 字符
                print(f"    {log['output'][:200]}")


async def main():
    if len(sys.argv) < 2:
        print("Usage: python mock_test.py <command> [args]")
        print("\nCommands:")
        print("  <text>          发送自然语言消息")
        print("  raw [user_id]   查看原始输出日志")
        return

    user_id = "test_user_001"

    if sys.argv[1] == "raw":
        uid = sys.argv[2] if len(sys.argv) > 2 else user_id
        limit = int(sys.argv[3]) if len(sys.argv) > 3 else 5
        await get_raw_output(uid, limit)
    else:
        content = " ".join(sys.argv[1:])
        print(f"发送: {content}")
        await send_message(user_id, content)


if __name__ == "__main__":
    asyncio.run(main())
