# -*- coding: utf-8 -*-
"""
verify_deployment.py - 验证部署的功能
"""

import requests
import json

HOST = '192.168.5.60'
PORT = 8088
BASE_URL = f'http://{HOST}:{PORT}'

print("="*60)
print("  Deployment Verification")
print("="*60)

# 1. 健康检查
print("\n[1] Health Check API:")
try:
    resp = requests.get(f'{BASE_URL}/api/health', timeout=5)
    data = resp.json()
    print(f"  Status: {data.get('status')}")
    print(f"  Version: {data.get('version')}")
    print(f"  Knowledge Base: {data.get('knowledge_base', {}).get('total_records')} records")
    print(f"  Cache: {data.get('cache', {}).get('total_files')} files")
    print(f"  Components: {data.get('components')}")
    print("[OK] Health check passed")
except Exception as e:
    print(f"[FAIL] {e}")

# 2. 项目列表
print("\n[2] Projects API:")
try:
    resp = requests.get(f'{BASE_URL}/api/projects', timeout=5)
    data = resp.json()
    projects = data.get('projects', [])
    print(f"  Total projects: {len(projects)}")
    if projects:
        print(f"  Sample: {', '.join(projects[:5])}...")
    print("[OK] Projects API passed")
except Exception as e:
    print(f"[FAIL] {e}")

# 3. 测试敏感信息过滤（通过搜索 API）
print("\n[3] Search API:")
try:
    resp = requests.post(
        f'{BASE_URL}/api/search',
        json={'query': 'MQTT连接失败'},
        timeout=5
    )
    data = resp.json()
    cases = data.get('matched_cases', [])
    print(f"  Found {len(cases)} matched cases")
    if cases:
        print(f"  Top match: {cases[0].get('title', 'N/A')}")
    print("[OK] Search API passed")
except Exception as e:
    print(f"[FAIL] {e}")

# 4. 测试主页
print("\n[4] Homepage:")
try:
    resp = requests.get(f'{BASE_URL}/', timeout=5)
    if resp.status_code == 200 and '问题排查助手' in resp.text:
        print("[OK] Homepage loaded")
    else:
        print(f"[FAIL] Status: {resp.status_code}")
except Exception as e:
    print(f"[FAIL] {e}")

print("\n" + "="*60)
print("  Verification Complete!")
print("="*60)
print(f"\nAccess URL: {BASE_URL}")
print(f"Health Check: {BASE_URL}/api/health")
print("="*60)