From 960925b84a72bd777dd35cbc5594eeaa45cbbfc2 Mon Sep 17 00:00:00 2001 From: AIOSAI Date: Mon, 20 Apr 2026 14:22:56 -0700 Subject: [PATCH] =?UTF-8?q?fix(memory/tests):=20repair=20test=5Fjson=5Fhan?= =?UTF-8?q?dler=20harness=20=E2=80=94=20KeyError=20on=20sys.modules=20look?= =?UTF-8?q?up?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _get_json_handler() was reading sys.modules[] directly after _fresh_json_handler had just popped the module out. importlib.import_module() replaces the lookup so the module is actually imported fresh each test. Also removes dead reload/pop code from the fixture that never ran. Fixes 27 failures (issue #360 finding A2). Co-Authored-By: Claude Sonnet 4.6 --- src/aipass/memory/.seedgo/bypass.json | 10 +++++++ src/aipass/memory/tests/test_json_handler.py | 29 +++----------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/aipass/memory/.seedgo/bypass.json b/src/aipass/memory/.seedgo/bypass.json index 0d8b4a5e..79891a7b 100644 --- a/src/aipass/memory/.seedgo/bypass.json +++ b/src/aipass/memory/.seedgo/bypass.json @@ -585,6 +585,16 @@ "file": "tests/test_vector.py", "standard": "meta", "reason": "Test file — META block present at lines 1-7; hook false-positive on test file format." + }, + { + "file": "tests/test_json_handler.py", + "standard": "architecture", + "reason": "Test file — lives in tests/ by design, not in 3-layer apps/ structure." + }, + { + "file": "tests/test_json_handler.py", + "standard": "meta", + "reason": "Test file — META block present at lines 1-7; hook false-positive on test file format." } ], "notes": { diff --git a/src/aipass/memory/tests/test_json_handler.py b/src/aipass/memory/tests/test_json_handler.py index 0010b0ac..925f5ef6 100644 --- a/src/aipass/memory/tests/test_json_handler.py +++ b/src/aipass/memory/tests/test_json_handler.py @@ -41,42 +41,21 @@ import pytest @pytest.fixture(autouse=True) def _fresh_json_handler(monkeypatch): - """Ensure json_handler module is freshly imported each test. - - The conftest autouse fixture mocks the json package. We need to - restore the real module for direct testing via importlib.reload. - """ - saved_json_pkg = sys.modules.pop("aipass.memory.apps.handlers.json", None) + """Ensure json_handler module is freshly imported each test.""" + sys.modules.pop("aipass.memory.apps.handlers.json", None) sys.modules.pop("aipass.memory.apps.handlers.json.json_handler", None) sys.modules.pop("aipass.memory.apps.handlers.json.memory_files", None) - - try: - pass # noqa: F811 - except Exception: - if saved_json_pkg is not None: - sys.modules["aipass.memory.apps.handlers.json"] = saved_json_pkg - - jh_key = "aipass.memory.apps.handlers.json.json_handler" - existing = sys.modules.get(jh_key) - if existing is not None: - importlib.reload(existing) - else: - sys.modules.pop(jh_key, None) - yield def _get_json_handler(): """Import and return the json_handler module.""" - return sys.modules["aipass.memory.apps.handlers.json.json_handler"] + return importlib.import_module("aipass.memory.apps.handlers.json.json_handler") def _get_memory_files(): """Import and return the memory_files module.""" - mf_key = "aipass.memory.apps.handlers.json.memory_files" - if mf_key not in sys.modules: - pass # noqa: F811 - return sys.modules[mf_key] + return importlib.import_module("aipass.memory.apps.handlers.json.memory_files") # ===========================================================================