#!/usr/bin/env python3
"""测试前端API调用"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

import requests

def test_api():
    """测试API"""
    try:
        # 调用算法列表API
        response = requests.get('http://localhost:8001/api/v1/algorithms/')
        
        if response.status_code == 200:
            data = response.json()
            algorithms = data.get('algorithms', [])
            
            print(f"成功获取 {len(algorithms)} 个算法\n")
            
            # 检查每个算法的字段
            for algo in algorithms:
                print(f"算法: {algo['name']}")
                print(f"  技术分类: {algo.get('tech_category', 'N/A')}")
                print(f"  输出类型: {algo.get('output_type', 'N/A')}")
                print()
            
            # 测试筛选
            print("测试筛选功能：")
            
            # 按技术分类筛选
            cv_algorithms = [a for a in algorithms if a.get('tech_category') == 'computer_vision']
            print(f"  计算机视觉算法: {len(cv_algorithms)} 个")
            
            # 按输出类型筛选
            image_algorithms = [a for a in algorithms if a.get('output_type') == 'image']
            print(f"  图片输出算法: {len(image_algorithms)} 个")
            
            # 按名称搜索
            search_results = [a for a in algorithms if '视频' in a.get('name', '')]
            print(f"  包含'视频'的算法: {len(search_results)} 个")
            
        else:
            print(f"API调用失败: {response.status_code}")
            print(response.text)
            
    except Exception as e:
        print(f"测试失败: {e}")
        sys.exit(1)

if __name__ == "__main__":
    test_api()