DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
test(question): pin mimic websocket error-path and stdout interceptor failure contracts (#1741)
evan188199-techd7a861073172
diff --git a/tests/api/test_question_router.py b/tests/api/test_question_router.pyindex 8972c67b7..5ef71d57a 100644--- a/tests/api/test_question_router.py+++ b/tests/api/test_question_router.py@@ -1,5 +1,6 @@from __future__ import annotations+import asynciofrom contextlib import contextmanagerimport importlibfrom pathlib import Path@@ -10,6 +11,7 @@ import pytestFastAPI = pytest.importorskip("fastapi").FastAPITestClient = pytest.importorskip("fastapi.testclient").TestClient+WebSocketDisconnect = pytest.importorskip("fastapi").WebSocketDisconnect@pytest.fixture(autouse=True)@@ -152,3 +154,269 @@ def test_mimic_websocket_accepts_config_and_returns_messages(assert messages[0]["stage"] == "init"assert messages[1]["stage"] == "processing"assert messages[2]["content"] == "stub mimic failure"+++# ---------------------------------------------------------------------------+# DT-22 HIGH-chain regression pins (evidence: agent/dt22-todo-scan report §7).+#+# Two HIGH findings on this router, both `except Exception: pass` around+# failure-prone I/O inside ``websocket_mimic_generate``:+#+# 1. the outer error broadcast — ``send_json({"type": "error", ...})``+# (report line 323; drifted to the outer handler's broadcast);+# 2. the stdout redirect write — ``StdoutInterceptor.write`` (renamed from+# ``TeeWrite`` since the scan) swallowing failures of the real terminal+# write (report line 131).+#+# These are failure-injection characterization tests: they pin the contract+# that a send/write failure stays isolated and that cleanup still runs (no+# process-wide stdout hijack, websocket always closed, run never crashes the+# handler). 现状豁免 (current-behavior exemption): the swallowed failures are+# themselves *silent* — no log record — and surfacing them would require a+# code change, which is out of scope for this test-only card; the exemption+# is recorded here instead of "fixing" the swallow.+# ---------------------------------------------------------------------------+++class _FakeMimicWebSocket:+ """Server-side websocket stand-in with targeted send/receive failures."""++ def __init__(+ self,+ incoming: list[dict] | None = None,+ fail_send_when=None,+ disconnect_immediately: bool = False,+ ) -> None:+ self._incoming = list(incoming or [])+ self._fail_send_when = fail_send_when+ self._disconnect_immediately = disconnect_immediately+ self.accepted = False+ self.sent: list[dict] = []+ self.closed = False++ async def accept(self) -> None:+ self.accepted = True++ async def receive_json(self) -> dict:+ if self._disconnect_immediately:+ raise WebSocketDisconnect(code=1000)+ if self._incoming:+ return self._incoming.pop(0)+ raise WebSocketDisconnect(code=1000)++ async def send_json(self, payload: dict) -> None:+ if self._fail_send_when is not None and self._fail_send_when(payload):+ raise RuntimeError("connection is closed")+ self.sent.append(payload)++ async def close(self, code: int = 1000) -> None:+ self.closed = True+++class _BrokenTerminal:+ """Stdout stand-in whose write/flush always fail (detached terminal)."""++ def __init__(self) -> None:+ self.write_attempts = 0+ self.flush_attempts = 0++ def write(self, message: str) -> int:+ self.write_attempts += 1+ raise OSError("terminal detached")++ def flush(self) -> None:+ self.flush_attempts += 1+ raise OSError("terminal detached")+++def _parsed_mode_config(tmp_path: Path) -> dict:+ return {+ "mode": "parsed",+ "paper_path": str(tmp_path / "paper"),+ "kb_name": "demo-kb",+ "max_questions": 1,+ }+++async def _run_mimic_endpoint(+ monkeypatch: pytest.MonkeyPatch, question_router_module, websocket: _FakeMimicWebSocket+) -> None:+ """Invoke the endpoint directly with auth bypassed, under a timeout."""++ async def _allow(_websocket) -> str:+ return "test-user-token"++ monkeypatch.setattr("deeptutor.api.routers.auth.ws_require_auth", _allow)++ await asyncio.wait_for(question_router_module.websocket_mimic_generate(websocket), timeout=10)+++async def test_mimic_error_broadcast_failure_is_isolated_and_cleanup_still_runs(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ """DT-22 HIGH pin #1: outer error broadcast swallow stays isolated.++ When the workflow itself raises and the client can no longer receive the+ error frame, the send failure must not escape the handler, stdout must be+ restored, and the websocket must still be closed. Removing the swallow+ naively would leak the send ``RuntimeError`` out of the endpoint and this+ test would go red — that is the regression being pinned.+ """+ question_router_module = _load_question_router_module(monkeypatch)++ async def _exploding_mimic(*_args, **_kwargs):+ raise RuntimeError("mimic exploded")++ monkeypatch.setattr(question_router_module, "mimic_exam_questions", _exploding_mimic)+ monkeypatch.setattr(+ question_router_module, "_mimic_output_dir", lambda: tmp_path / "mimic_papers"+ )++ websocket = _FakeMimicWebSocket(+ incoming=[_parsed_mode_config(tmp_path)],+ fail_send_when=lambda payload: payload.get("type") == "error",+ )+ terminal = object()+ monkeypatch.setattr(sys, "stdout", terminal)++ await _run_mimic_endpoint(monkeypatch, question_router_module, websocket)++ assert websocket.accepted+ assert websocket.closed+ assert [message["type"] for message in websocket.sent] == ["status", "status"]+ assert sys.stdout is terminal+++async def test_mimic_client_disconnect_before_config_ends_cleanly(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """DT-22 HIGH chain #1, disconnect flavor: a client vanishing right after+ accept must end the run without raising, without any frames sent, and with+ stdout restored and the socket close attempted (swallowed when already+ gone)."""+ question_router_module = _load_question_router_module(monkeypatch)++ websocket = _FakeMimicWebSocket(disconnect_immediately=True)+ terminal = object()+ monkeypatch.setattr(sys, "stdout", terminal)++ await _run_mimic_endpoint(monkeypatch, question_router_module, websocket)++ assert websocket.accepted+ assert websocket.closed+ assert websocket.sent == []+ assert sys.stdout is terminal+++async def test_mimic_failed_result_broadcast_to_dead_client_still_restores_stdout(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ """DT-22 HIGH chain #1, result-failure flavor: when the workflow reports+ ``success: False`` and the error broadcast itself fails because the client+ is gone, the run still cleans up (stdout restored, socket closed) instead+ of crashing."""+ question_router_module = _load_question_router_module(monkeypatch)++ async def _failing_mimic(*_args, **_kwargs):+ return {"success": False, "error": "generation failed"}++ monkeypatch.setattr(question_router_module, "mimic_exam_questions", _failing_mimic)+ monkeypatch.setattr(+ question_router_module, "_mimic_output_dir", lambda: tmp_path / "mimic_papers"+ )++ websocket = _FakeMimicWebSocket(+ incoming=[_parsed_mode_config(tmp_path)],+ fail_send_when=lambda payload: payload.get("type") == "error",+ )+ terminal = object()+ monkeypatch.setattr(sys, "stdout", terminal)++ await _run_mimic_endpoint(monkeypatch, question_router_module, websocket)++ assert websocket.closed+ assert [message["type"] for message in websocket.sent] == ["status", "status"]+ assert sys.stdout is terminal+++async def test_mimic_print_survives_detached_terminal_and_still_streams(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ """DT-22 HIGH pin #2: the stdout redirect swallow keeps generation alive.++ With the real terminal's write/flush raising, a ``print`` from inside the+ workflow must not propagate, its cleaned (ANSI-stripped) text must still+ reach the client as a process_log frame, and process stdout must be+ restored afterwards. 现状豁免: the terminal failure itself is swallowed+ silently — surfacing it (e.g. a debug log) would be a code change outside+ this test-only card.+ """+ question_router_module = _load_question_router_module(monkeypatch)++ async def _printing_mimic(*_args, **_kwargs):+ print("Generating \x1b[31mquestions\x1b[0m...")+ sys.stdout.flush()+ await asyncio.sleep(0)+ return {"success": True, "generated_questions": ["q1"], "failed_questions": []}++ monkeypatch.setattr(question_router_module, "mimic_exam_questions", _printing_mimic)+ monkeypatch.setattr(+ question_router_module, "_mimic_output_dir", lambda: tmp_path / "mimic_papers"+ )++ websocket = _FakeMimicWebSocket(incoming=[_parsed_mode_config(tmp_path)])+ terminal = _BrokenTerminal()+ monkeypatch.setattr(sys, "stdout", terminal)++ await _run_mimic_endpoint(monkeypatch, question_router_module, websocket)++ assert [message["type"] for message in websocket.sent] == [+ "status",+ "status",+ "process_log",+ "complete",+ ]+ log_frame = websocket.sent[2]+ assert log_frame["message"] == "Generating questions..."+ assert terminal.write_attempts >= 1+ assert terminal.flush_attempts >= 1+ assert sys.stdout is terminal+++async def test_mimic_log_pusher_send_failure_stops_stream_without_breaking_run(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ """DT-22 HIGH chain #1, mid-stream flavor: the log pusher treats a failed+ send as end-of-stream (break) instead of crashing the run — the workflow+ still completes, the final ``complete`` frame still goes out where+ possible, and cleanup runs."""+ question_router_module = _load_question_router_module(monkeypatch)++ async def _printing_mimic(*_args, **_kwargs):+ print("stream me")+ await asyncio.sleep(0)+ await asyncio.sleep(0)+ return {"success": True, "generated_questions": [], "failed_questions": []}++ monkeypatch.setattr(question_router_module, "mimic_exam_questions", _printing_mimic)+ monkeypatch.setattr(+ question_router_module, "_mimic_output_dir", lambda: tmp_path / "mimic_papers"+ )++ websocket = _FakeMimicWebSocket(+ incoming=[_parsed_mode_config(tmp_path)],+ fail_send_when=lambda payload: payload.get("type") == "process_log",+ )+ terminal = object()+ monkeypatch.setattr(sys, "stdout", terminal)++ await _run_mimic_endpoint(monkeypatch, question_router_module, websocket)++ assert [message["type"] for message in websocket.sent] == ["status", "status", "complete"]+ assert websocket.closed+ assert sys.stdout is terminal