#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
API 路由测试
"""

import pytest
from httpx import AsyncClient
from httpx._transports.asgi import ASGITransport

import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))

from app.main import app


@pytest.fixture
async def client():
    """创建测试客户端"""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as ac:
        yield ac


class TestHealthCheck:
    """健康检查测试"""

    @pytest.mark.asyncio
    async def test_root(self, client):
        """测试根路径"""
        response = await client.get("/")
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "ok"
        assert "message" in data

    @pytest.mark.asyncio
    async def test_health(self, client):
        """测试健康检查"""
        response = await client.get("/health")
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "healthy"


class TestModulesAPI:
    """模块 API 测试"""

    @pytest.mark.asyncio
    async def test_list_modules(self, client):
        """测试获取模块列表"""
        response = await client.get("/api/modules")
        assert response.status_code == 200
        data = response.json()
        assert "total" in data
        assert "items" in data
        assert isinstance(data["items"], list)


class TestCasesAPI:
    """用例 API 测试"""

    @pytest.mark.asyncio
    async def test_list_cases(self, client):
        """测试获取用例列表"""
        response = await client.get("/api/cases")
        assert response.status_code == 200
        data = response.json()
        assert "total" in data
        assert "items" in data


class TestExecutionsAPI:
    """执行 API 测试"""

    @pytest.mark.asyncio
    async def test_list_executions(self, client):
        """测试获取执行记录列表"""
        response = await client.get("/api/executions")
        assert response.status_code == 200
        data = response.json()
        assert "total" in data
        assert "items" in data


class TestReportsAPI:
    """报告 API 测试"""

    @pytest.mark.asyncio
    async def test_list_reports(self, client):
        """测试获取报告列表"""
        response = await client.get("/api/reports/list")
        assert response.status_code == 200
        data = response.json()
        assert "total" in data
        assert "items" in data


class TestCleanupAPI:
    """清理 API 测试"""

    @pytest.mark.asyncio
    async def test_get_stats(self, client):
        """测试获取存储统计"""
        response = await client.get("/api/cleanup/stats")
        assert response.status_code == 200
        data = response.json()
        assert data["success"] == True
        assert "data" in data

    @pytest.mark.asyncio
    async def test_dry_run_cleanup(self, client):
        """测试模拟清理"""
        response = await client.post(
            "/api/cleanup/cleanup",
            params={"days": 30, "dry_run": True}
        )
        assert response.status_code == 200
        data = response.json()
        assert data["success"] == True
        assert data["data"]["dry_run"] == True
