from contextlib import asynccontextmanager from fastapi.testclient import TestClient from unittest.mock import Mock, AsyncMock from dotigent.backend.app.main import app @asynccontextmanager async def test_lifespan(_app): # Setup fake resources fake_runtime = Mock() fake_runtime.list_agents = AsyncMock(return_value=[{"id": "test"}]) _app.state.runtime = fake_runtime # Signal startup complete yield # Optionally cleanup _app.state.runtime = None def test_with_test_lifespan(monkeypatch): # monkeypatch the app.lifespan callable to our test_lifespan monkeypatch.setattr(app, "lifespan", test_lifespan) with TestClient(app) as client: r = client.get("/agents") assert r.status_code == 200 assert r.json() == [{"id":"test"}]