AgentEvalTool/tests/integration/test_intelligent_eval_scan_scheduler.py
sinohqb 84627a3c6a
All checks were successful
CI / test (push) Successful in 3m16s
refactor(intelligent-eval): 消除 lifecycle.py 和 scheduler.py 中的重复延迟导入
- 将延迟导入移至模块顶部,消除 Shotgun Surgery 气味
- lifecycle.py: 移除 41 行重复导入
- scheduler.py: 移除 14 行重复导入
- 修复测试:更新 monkeypatch 以补丁 scheduler 模块的引用而非原始模块
- 符合代码规范:避免函数内重复导入

Closes code-review finding: repeated deferred imports (Shotgun Surgery)
2026-08-24 01:47:47 +08:00

41 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""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"