#!/usr/bin/env python3
"""
Claude API 测试脚本
"""

import asyncio
import sys
from pathlib import Path

# 添加项目路径
sys.path.append(str(Path(__file__).parent))

from utils.claude_client import ClaudeClient
from utils.token_reader import TokenReader
import logging

# 配置日志
logging.basicConfig(level=logging.INFO)

async def test_claude_api():
    """测试Claude API功能"""
    print("🧪 测试Claude API...")
    
    try:
        # 读取API密钥
        token_reader = TokenReader("/home/will/docker/tokens.txt")
        claude_api_key = token_reader.get_token("claude_API")
        
        if not claude_api_key:
            print("❌ 未找到Claude API密钥")
            return
        
        print(f"✅ 找到Claude API密钥: {claude_api_key[:20]}...")
        
        # 初始化Claude客户端
        claude_client = ClaudeClient(claude_api_key)
        
        # 测试基础聊天
        print("\n🔧 测试基础聊天...")
        messages = [{"role": "user", "content": "你好，请简单介绍一下Docker"}]
        response = await claude_client.chat_completion(messages, max_tokens=200)
        
        if response:
            print(f"✅ 聊天测试成功:\n{response[:200]}...")
        else:
            print("❌ 聊天测试失败")
        
        # 测试命令解释
        print("\n🔧 测试命令解释...")
        test_commands = [
            "查看Docker容器状态",
            "列出当前目录文件",
            "删除所有文件",  # 应该被拒绝
            "查看磁盘使用情况"
        ]
        
        for cmd in test_commands:
            print(f"\n   测试命令: {cmd}")
            result = await claude_client.interpret_command(cmd)
            if result:
                print(f"   解释结果: {result}")
            else:
                print(f"   ❌ 解释失败")
        
        # 测试错误解释
        print("\n🔧 测试错误解释...")
        error_output = "docker: command not found"
        explanation = await claude_client.explain_error("docker ps", error_output)
        if explanation:
            print(f"✅ 错误解释: {explanation}")
        else:
            print("❌ 错误解释失败")
            
    except Exception as e:
        print(f"❌ 测试失败: {e}")

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