Files
DeepTutor/tests/api/test_settings_test_run_lookup.py
qiuqianyi66 32acc5889a fix(settings): answer 404 for unknown connection-test run ids (#2841)
* fix(settings): answer 404 for unknown connection-test run ids

* chore(api): refresh router index for the settings route change

tests/api/test_router_loader.py::test_repository_index_matches_router_sources
checks the fingerprint of router sources; regenerated with
scripts/build_router_index.py.

---------

Co-authored-by: zszz3 <91608029+zszz3@users.noreply.github.com>
2026-11-28 20:05:55 +08:00

55 lines
1.8 KiB
Python

"""The Settings connection-test endpoints answer 404 for an unknown run id.
Before this, a stale id (server restarted, page reconnected) turned into a
``KeyError`` and a 500 on the event stream, and the frontend's test card showed
a failed request instead of "this run is gone".
"""
from __future__ import annotations
import asyncio
from types import SimpleNamespace
from fastapi import HTTPException
import pytest
from deeptutor.api.routers import settings as settings_router
from deeptutor.services.config.test_runner import ConfigTestRunner
@pytest.fixture
def runner(monkeypatch: pytest.MonkeyPatch) -> ConfigTestRunner:
empty = ConfigTestRunner()
monkeypatch.setattr(settings_router, "_require_settings_admin", lambda: None)
monkeypatch.setattr(settings_router, "get_config_test_runner", lambda: empty)
return empty
def test_events_for_unknown_run_answer_404(runner: ConfigTestRunner) -> None:
request = SimpleNamespace()
with pytest.raises(HTTPException) as excinfo:
asyncio.run(settings_router.stream_service_test_events("llm", "llm-gone", request))
assert excinfo.value.status_code == 404
assert excinfo.value.detail == "Unknown test run"
def test_cancel_for_unknown_run_answers_404(runner: ConfigTestRunner) -> None:
with pytest.raises(HTTPException) as excinfo:
asyncio.run(settings_router.cancel_service_test("llm", "llm-gone"))
assert excinfo.value.status_code == 404
def test_cancel_for_known_run_confirms(runner: ConfigTestRunner) -> None:
from deeptutor.services.config.test_runner import TestRun
run = TestRun(id="llm-live", service="llm")
runner._runs[run.id] = run # noqa: SLF001 - the runner exposes no inject API
assert asyncio.run(settings_router.cancel_service_test("llm", run.id)) == {
"message": "Cancelled"
}
assert run.cancelled