And fix test_discussion_takes_gui.py patches to use ai_client_stub
3.3 KiB
Track: Fix Test Patches for ai_client_stub Integration
Context
After the refactor to use ai_client_stub as the module alias for app_controller, several tests fail because they use patch('src.ai_client.X') which doesn't properly reach the stub's module-level functions. This is a pre-existing architectural issue that needs fixing.
Root Cause Analysis
When tests use patch('src.ai_client.get_current_tier', return_value='Tier 3'):
patchcreates/overridessrc.ai_clientin thesrcpackage namespace- But
app_controllerdoesfrom src import ai_client_stub as ai_clientat module load time - The
ai_clientlocal reference inapp_controllerpoints directly toai_client_stubmodule - Patch modifies
src.ai_client(a different object), notsrc.ai_client_stub - Result: Functions in
ai_client_stubaren't patched
Failed Tests
-
tests/test_app_controller_offloading.py::test_on_tool_log_offloading- Patches
src.ai_client.get_current_tierexpecting it to affectapp_controller._on_tool_log() - But
app_controllerimported the stub directly, so patch doesn't reach it
- Patches
-
tests/test_process_pending_gui_tasks.py::test_redundant_calls_in_process_pending_gui_tasks- Patches
src.ai_client.set_providerandsrc.ai_client.reset_session - Same root cause
- Patches
-
tests/test_process_pending_gui_tasks.py::test_gcli_path_updates_adapter- Sets
ai_client._gemini_cli_adapter = Noneexpectingapp_controllerto see it - Same import aliasing issue
- Sets
-
tests/test_gui_updates.py::test_telemetry_data_updates_correctly- Patches
src.ai_client.get_token_stats - Same issue
- Patches
-
tests/test_gui_updates.py::test_gui_updates_on_event- Same issue with
get_token_statspatch
- Same issue with
Solution Options
Option A: Fix Tests to Use Correct Patch Path
Change all patches from patch('src.ai_client.X') to patch('src.ai_client_stub.X').
Pros:
- Minimal code change
- Resolves the actual issue
- Follows proper patch semantics
Cons:
- Must update all affected tests
Option B: Add Module-Level Alias in src package
Create src/ai_client.py that re-exports from ai_client_stub:
from src.ai_client_stub import *
Pros:
- Makes
src.ai_clientavailable as actual module - Tests using
patch('src.ai_client.X')would work
Cons:
- Creates dual namespace confusion
- May conflict with existing
ai_client_proxy.py
Option C: Fix app_controller to Not Use Alias
Change all from src import ai_client_stub as ai_client to from src import ai_client and ensure src.ai_client is the stub.
Cons:
- Significant refactor
- May break other imports
Recommended Solution: Option A
Fix the tests to use the correct patch path (src.ai_client_stub). This is surgical and addresses the root cause without creating architectural complexity.
Tasks
- Fix
test_on_tool_log_offloading- change patch path tosrc.ai_client_stub.get_current_tier - Fix
test_redundant_calls_in_process_pending_gui_tasks- change patches tosrc.ai_client_stub - Fix
test_gcli_path_updates_adapter- use correct patch/module path - Fix
test_telemetry_data_updates_correctly- change patch tosrc.ai_client_stub.get_token_stats - Fix
test_gui_updates_on_event- same as above - Run full batch test to verify all tests pass
- Create checkpoint commit