Spaces:
Paused
Paused
| """Tests for the FastAPI REST API layer.""" | |
| from __future__ import annotations | |
| import json | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from backend.app import app | |
| from backend.deps import services | |
| from agentic_core.project_store import ProjectStore | |
| from agentic_core.artifacts import ArtifactStore | |
| from agentic_core.orchestrator import ExecutionTracker, EventBus, Orchestrator | |
| from agentic_core.llm import LLMService, FakeLLMProvider | |
| from tests.helpers import discovery_output, build_handler | |
| def api_client(tmp_path, settings): | |
| # Override global services for hermetic testing | |
| fake_provider = FakeLLMProvider() | |
| fake_provider.set_handler(build_handler()) | |
| app_settings = settings | |
| service = LLMService(fake_provider, app_settings) | |
| bus = EventBus() | |
| tracker = ExecutionTracker(app_settings.runs_dir) | |
| store = ProjectStore(app_settings.db_path) | |
| artifacts = ArtifactStore(app_settings.artifacts_dir) | |
| orchestrator = Orchestrator(service, bus, tracker, app_settings) | |
| services.settings = app_settings | |
| services.provider = fake_provider | |
| services.llm_service = service | |
| services.event_bus = bus | |
| services.tracker = tracker | |
| services.project_store = store | |
| services.artifact_store = artifacts | |
| services.orchestrator = orchestrator | |
| services.generation_tasks = {} | |
| client = TestClient(app) | |
| yield client | |
| def test_health_check(api_client): | |
| response = api_client.get("/api/health") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["status"] == "healthy" | |
| assert "version" in data | |
| assert "provider" in data | |
| def test_project_lifecycle_api(api_client): | |
| # 1. Create project | |
| create_resp = api_client.post("/api/projects", json={"business_idea": "Build a task manager app"}) | |
| assert create_resp.status_code == 201 | |
| proj_data = create_resp.json() | |
| project_id = proj_data["project_id"] | |
| assert proj_data["business_idea"] == "Build a task manager app" | |
| # 2. List projects | |
| list_resp = api_client.get("/api/projects") | |
| assert list_resp.status_code == 200 | |
| assert project_id in list_resp.json()["projects"] | |
| # 3. Fetch project | |
| get_resp = api_client.get(f"/api/projects/{project_id}") | |
| assert get_resp.status_code == 200 | |
| assert get_resp.json()["project_id"] == project_id | |
| # 4. Fetch runs (telemetry) | |
| runs_resp = api_client.get(f"/api/projects/{project_id}/runs") | |
| assert runs_resp.status_code == 200 | |
| assert "runs" in runs_resp.json() | |
| # 5. Delete project | |
| del_resp = api_client.delete(f"/api/projects/{project_id}") | |
| assert del_resp.status_code == 200 | |
| assert del_resp.json()["status"] == "deleted" | |
| # 6. Verify 404 after deletion | |
| get_again = api_client.get(f"/api/projects/{project_id}") | |
| assert get_again.status_code == 404 | |