From 30e7b7c498552f2433852f948758912a5aef1497 Mon Sep 17 00:00:00 2001 From: Adryan Eka Vandra Date: Fri, 17 Apr 2026 16:01:24 +0700 Subject: [PATCH] test(scripts): restore real websockets module before repro smoke test Some earlier tests in the suite replace sys.modules['websockets'] with a stub. When the smoke test ran after those, uvicorn's websockets-sansio backend tried to import websockets.server at connect time, resolved through the stub, and failed with 'No module named websockets.server; websockets is not a package', preventing the mock proxy from starting. Add an autouse fixture that drops stub websockets.* entries from sys.modules, re-imports the real package + websockets.asyncio.server, and restores prior state on teardown. Keeps the smoke test deterministic regardless of collection order. --- .../test_repro_codex_replay_smoke.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_scripts/test_repro_codex_replay_smoke.py b/tests/test_scripts/test_repro_codex_replay_smoke.py index 745b70c81..d6db64e0e 100644 --- a/tests/test_scripts/test_repro_codex_replay_smoke.py +++ b/tests/test_scripts/test_repro_codex_replay_smoke.py @@ -13,6 +13,7 @@ from __future__ import annotations import asyncio import contextlib +import importlib import io import socket import sys @@ -24,6 +25,30 @@ import pytest import uvicorn from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect + +@pytest.fixture(autouse=True) +def _restore_real_websockets_module() -> Iterator[None]: + """Some earlier tests replace ``sys.modules['websockets']`` with a stub. + + uvicorn's websockets-sansio backend imports ``websockets.server`` at + connect time; if a stub is installed the import fails and the mock + proxy never starts. Force-reload the real package before this test. + """ + originals = { + name: sys.modules.pop(name, None) + for name in list(sys.modules) + if name == "websockets" or name.startswith("websockets.") + } + importlib.import_module("websockets") + importlib.import_module("websockets.asyncio.server") + yield + for name in list(sys.modules): + if name == "websockets" or name.startswith("websockets."): + del sys.modules[name] + for name, mod in originals.items(): + if mod is not None: + sys.modules[name] = mod + # Make sure `scripts/` is importable when running via pytest from repo root. ROOT = Path(__file__).resolve().parents[2] SCRIPTS_DIR = ROOT / "scripts"