import pytest from httpx import AsyncClient from unittest.mock import patch, MagicMock @pytest.mark.asyncio async def test_ai_summarize(async_client: AsyncClient, client_a, auth_headers_a): mock_page = { "title": "Example Title", "description": "Example Description", "text": "Example Text content for the page.", } mock_ai_result = { "summary": "This is a summary.", "tags": ["tag1", "tag2"], "model": "gemini-1.5-flash" } with patch("app.routers.ai.fetch_page_content", return_value=mock_page), \ patch("app.routers.ai.summarize_url", return_value=mock_ai_result), \ patch("app.routers.ai.settings.AI_ENABLED", True): payload = {"url": "https://example.com", "language": "français"} response = await async_client.post("/ai/summarize", json=payload, headers=auth_headers_a) assert response.status_code == 200 data = response.json() assert data["summary"] == "This is a summary." assert "tag1" in data["tags"] assert data["title"] == "Example Title" @pytest.mark.asyncio async def test_ai_draft_task(async_client: AsyncClient, client_a, auth_headers_a): mock_task = { "title": "Fix Bug", "description": "Fix the bug in the code", "steps": ["Step 1", "Step 2"], "priority": "high", "estimated_time": "1h" } with patch("app.routers.ai.draft_task", return_value=mock_task), \ patch("app.routers.ai.settings.AI_ENABLED", True): payload = {"description": "Fix bug", "context": "Backend project"} response = await async_client.post("/ai/draft-task", json=payload, headers=auth_headers_a) assert response.status_code == 200 data = response.json() assert data["title"] == "Fix Bug" assert data["priority"] == "high" @pytest.mark.asyncio async def test_ai_disabled(async_client: AsyncClient, client_a, auth_headers_a): with patch("app.routers.ai.settings.AI_ENABLED", False): response = await async_client.post("/ai/draft-task", json={"description": "test"}, headers=auth_headers_a) assert response.status_code == 503 assert response.json()["detail"] == "AI désactivée"