Skip to content

fix(tests): fix sys.modules leaks causing 47 cross-file test errors (#752) - #756

Merged
vybe merged 2 commits into
devfrom
feature/752-fix-test-ip-rate-limit-sys-modules-leak
May 10, 2026
Merged

vybe merged 2 commits into
devfrom
feature/752-fix-test-ip-rate-limit-sys-modules-leak

Conversation

@vybe

@vybe vybe commented May 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • test_ip_rate_limit_fix.py permanently overwrote sys.modules[\"fastapi\"] with a stripped stub (no status, security, or other attrs) and never restored it, causing ImportError: cannot import name 'status' from 'fastapi' in every test that ran after it in the same session — 47 errors across the 7 files listed in fix(tests): update check_login_rate_limit import across 7 test files #752
  • test_sync_health_service.py failed after test_fleet_status_resilience.py because the latter installed a SimpleNamespace stub for services.agent_client without AgentClient, breaking sync_health_service.py's module-level import

Changes

  • tests/test_ip_rate_limit_fix.py: Refactored _load_public_router() to accept monkeypatch and use monkeypatch.setitem for all sys.modules mutations (fastapi, fastapi.*, database, dependencies, models, all service stubs). Added missing dependencies, models, and services.upload_service stubs that were causing the transitive import chain to fail. Added autouse load_module fixture to TestPerTokenRateLimit (previously called _load_public_router() directly with no cleanup).
  • tests/unit/test_sync_health_service.py: Extended tmp_db fixture to evict and re-stub services.agent_client via monkeypatch, ensuring a clean AgentClient-bearing stub is always present when sync_health_service.py is imported, regardless of what prior test files installed.

Test Plan

  • All 7 previously-erroring files pass individually
  • All 60 tests pass when all 7 files run together: pytest tests/test_ip_rate_limit_fix.py tests/unit/test_login_rate_limit_split.py tests/unit/test_webhook_rate_limit_inprocess.py tests/unit/test_webhook_rate_limit_toctou.py tests/unit/test_fleet_status_resilience.py tests/unit/test_sync_health_service.py tests/test_github_pat_propagation_unit.py
  • pytest tests/ --collect-only reports 0 collection errors for all 7 files (only 3 pre-existing unrelated errors remain: trinity_cli ×2, twilio ×1)
  • No regressions in broader unit test suite

Fixes #752

Generated with Claude Code

vybe and others added 2 commits May 10, 2026 14:46
…752)

test_ip_rate_limit_fix.py was unconditionally overwriting sys.modules["fastapi"]
with a stripped stub (no `status` attr) and never restoring it, breaking every
subsequent test that imports any module doing `from fastapi import status`.
Converted _load_public_router() to accept monkeypatch and use monkeypatch.setitem
for all sys.modules mutations so pytest auto-restores state after each test.
Added missing stubs (dependencies, models, services.upload_service) to prevent
the transitive import chain from reaching the real dependencies.py.

test_sync_health_service.py was failing when run after test_fleet_status_resilience.py
because the latter installs services.agent_client as a SimpleNamespace without
AgentClient. Fixed by evicting and re-stubbing services.agent_client in the tmp_db
fixture via monkeypatch.

Fixes #752

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@vybe
vybe merged commit c5ad320 into dev May 10, 2026
8 checks passed
@trinity-ability trinity-ability mentioned this pull request Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant