"""Lifespan scheduler wiring 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 the intelligent-eval scheduler runtime that scans executing evals every 60s. This test verifies that on application startup the scan is actually invoked. 编排逻辑本身(watchdog/入队/触发)的测试在 ``tests/unit/test_intelligent_eval_scheduler.py``,直接驱动 ``scan_once``。 """ 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.scheduler as scheduler_mod 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(scheduler_mod, "get_session", lambda: MagicMock()) # Patch the scheduler module's reference, not the task_queue module's monkeypatch.setattr(scheduler_mod, "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"