"""
工作流API测试
"""
import pytest
from fastapi import status


@pytest.mark.unit
@pytest.mark.workflow
class TestWorkflows:
    """工作流相关测试"""
    
    def test_create_workflow(self, authenticated_client, sample_workflow_data):
        """测试创建工作流"""
        response = authenticated_client.post(
            "/api/v1/workflows",
            json=sample_workflow_data
        )
        assert response.status_code == status.HTTP_201_CREATED
        data = response.json()
        assert data["name"] == sample_workflow_data["name"]
        assert data["description"] == sample_workflow_data["description"]
        assert "id" in data
        assert "nodes" in data
        assert "edges" in data
    
    def test_get_workflow_list(self, authenticated_client, sample_workflow_data):
        """测试获取工作流列表"""
        # 创建几个工作流
        for i in range(3):
            workflow_data = sample_workflow_data.copy()
            workflow_data["name"] = f"工作流{i+1}"
            authenticated_client.post("/api/v1/workflows", json=workflow_data)
        
        # 获取列表
        response = authenticated_client.get("/api/v1/workflows")
        assert response.status_code == status.HTTP_200_OK
        data = response.json()
        assert isinstance(data, list)
        assert len(data) >= 3
    
    def test_get_workflow_detail(self, authenticated_client, sample_workflow_data):
        """测试获取工作流详情"""
        # 创建工作流
        create_response = authenticated_client.post(
            "/api/v1/workflows",
            json=sample_workflow_data
        )
        workflow_id = create_response.json()["id"]
        
        # 获取详情
        response = authenticated_client.get(f"/api/v1/workflows/{workflow_id}")
        assert response.status_code == status.HTTP_200_OK
        data = response.json()
        assert data["id"] == workflow_id
        assert data["name"] == sample_workflow_data["name"]
        assert len(data["nodes"]) == len(sample_workflow_data["nodes"])
        assert len(data["edges"]) == len(sample_workflow_data["edges"])
    
    def test_update_workflow(self, authenticated_client, sample_workflow_data):
        """测试更新工作流"""
        # 创建工作流
        create_response = authenticated_client.post(
            "/api/v1/workflows",
            json=sample_workflow_data
        )
        workflow_id = create_response.json()["id"]
        
        # 更新工作流
        update_data = sample_workflow_data.copy()
        update_data["name"] = "更新后的工作流"
        update_data["description"] = "更新后的描述"
        
        response = authenticated_client.put(
            f"/api/v1/workflows/{workflow_id}",
            json=update_data
        )
        assert response.status_code == status.HTTP_200_OK
        data = response.json()
        assert data["name"] == "更新后的工作流"
        assert data["description"] == "更新后的描述"
    
    def test_delete_workflow(self, authenticated_client, sample_workflow_data):
        """测试删除工作流"""
        # 创建工作流
        create_response = authenticated_client.post(
            "/api/v1/workflows",
            json=sample_workflow_data
        )
        workflow_id = create_response.json()["id"]
        
        # 删除工作流
        response = authenticated_client.delete(f"/api/v1/workflows/{workflow_id}")
        assert response.status_code == status.HTTP_200_OK
        
        # 验证已删除
        get_response = authenticated_client.get(f"/api/v1/workflows/{workflow_id}")
        assert get_response.status_code == status.HTTP_404_NOT_FOUND
    
    def test_get_workflow_not_found(self, authenticated_client):
        """测试获取不存在的工作流"""
        response = authenticated_client.get("/api/v1/workflows/nonexistent-id")
        assert response.status_code == status.HTTP_404_NOT_FOUND
    
    def test_create_workflow_unauthorized(self, client, sample_workflow_data):
        """测试未授权创建工作流"""
        response = client.post("/api/v1/workflows", json=sample_workflow_data)
        assert response.status_code == status.HTTP_401_UNAUTHORIZED
