"""Lifespan scan-loop test (v1.1.0 defect fix). `scan_and_enqueue_tasks` previously had no scheduler — the OpenClaw Worker wakes every minute but could never pull a task. The lifespan now starts an asyncio background task that scans executing evals every 60s. This test verifies that on application startup the scan is actually invoked. """ from unittest.mock import MagicMock from fastapi.testclient import TestClient def test_lifespan_starts_scan_loop(monkeypatch): """Lifespan startup must invoke the intelligent-eval scan loop once.""" import agenteval.intelligent_eval.task_queue as tq import agenteval.web.app as app_mod calls: list[int] = [] real_scan = tq.scan_and_enqueue_tasks def fake_scan(session): calls.append(1) return real_scan(session) # The scan loop calls get_session() to open a DB session; replace it with a # no-op mock so the test does not touch the real SQLite file. monkeypatch.setattr(app_mod, "get_session", lambda: MagicMock()) monkeypatch.setattr(tq, "scan_and_enqueue_tasks", fake_scan) with TestClient(app_mod.app) as client: assert client.get("/api/health").status_code == 200 # The background task runs immediately (before its first 60s sleep). assert calls, "scan_and_enqueue_tasks should have been invoked on startup"