Plan: Address High-Risk Test Coverage Gaps
Executive Summary
Problem: Backend coverage reported at 18.7% with 22 modules showing 0% coverage.
Root Cause Discovery: The exploration revealed that extensive tests EXIST for execution (900+ lines) and psychology (95+ tests) modules. The "0% coverage" is a configuration/measurement issue, not a lack of tests.
Actual Gaps Identified:
- Coverage configuration not measuring existing tests
- Psychology panic mode detection (truly untested)
- Multi-module integration tests (only 1 exists)
- Broker layer relies heavily on mocks instead of real integration
- Boundary condition tests missing across all modules
User Decisions
- Approach: Fix config AND write new tests in parallel
- Target Coverage: 70% (match frontend)
- Integration Strategy: Use real component instances (not mocks)
Phase 1: Fix Coverage Measurement (Highest Priority)
Goal: Make existing tests count toward coverage metrics
Tasks:
Verify pytest-cov configuration
- File:
pyproject.tomlandpytest.ini - Ensure
--cov=corecaptures all test imports - Add branch coverage:
--cov-branch
- File:
Check test imports
- Verify tests import from
core.*not local paths - Files to check:
tests/unit/test_execution_router.pytests/unit/test_execution_monitor.pytests/unit/test_slippage_model.pytests/unit/test_twap_executor.pytests/unit/test_vwap_executor.py
- Verify tests import from
Add coverage threshold enforcement
- Update
pyproject.toml:fail_under: 70 - This matches the frontend threshold and will drive test improvements
- Update
Phase 2: Psychology Module - Missing Tests
Goal: Cover the actually untested code paths
2.1 Panic Mode Detection Tests (EmotionalFirewall)
File to create: tests/unit/test_emotional_firewall_panic.py
Source: core/psychology/emotional_firewall.py lines 122-128, 327-328
Tests needed:
test_record_manual_intervention_trackingtest_panic_mode_triggered_3_interventions_in_10_mintest_panic_cooldown_2_hours_enforcedtest_panic_mode_disabled_when_flag_off
2.2 Timezone Edge Cases
File: Add to existing tests/unit/test_psychology.py
Tests needed:
test_timezone_fallback_on_invalid_timezonetest_dst_transition_handlingtest_late_night_check_across_timezone_boundary
2.3 Recovery Protocol Phase History
File: tests/unit/test_recovery_protocol_history.py
Source: core/psychology/recovery_protocol.py lines 414-416 (stub)
Tests needed:
test_phase_entry_recorded_on_starttest_phase_completion_recorded_on_advancetest_phase_duration_calculationtest_demotion_details_recorded
2.4 Multi-Module Integration
File to create: tests/integration/test_psychology_integration.py
Tests needed:
test_emotional_firewall_and_drawdown_monitor_togethertest_timeout_escalates_to_recovery_protocoltest_all_four_modules_coordinatedtest_circuit_breaker_triggers_recovery_mode
2.5 Boundary Condition Tests
File: Add to existing test files
Tests needed:
test_exactly_20_percent_drawdown_boundarytest_exactly_30_min_revenge_window_expirationtest_exactly_3_consecutive_losses_boundarytest_exactly_5_streak_alert_threshold
Phase 3: Broker Layer - Real Integration Tests
Goal: Replace mocks with REAL component instances (per user decision)
3.1 Real Risk System Integration
File: tests/integration/test_broker_risk_integration.py
Strategy: Instantiate real components, NOT mocks:
# Instead of: mock_kelly = Mock(spec=KellyPositionSizer)
# Use: kelly = KellyPositionSizer(config)
Components to test with real instances:
KellyPositionSizer- verify actual Kelly calculationsCircuitBreaker- verify actual tripping logicStopLossManager- verify actual stop placementPortfolioRisk- verify heat accumulation
Tests needed:
test_order_rejected_when_kelly_sizer_exceeds_limittest_circuit_breaker_trips_after_real_lossestest_stop_loss_actually_placed_after_buytest_portfolio_heat_accumulates_across_orderstest_full_order_lifecycle_with_real_risk_systems
3.2 Concurrent Order Handling
File: tests/unit/test_paper_trader_concurrency.py
Tests needed:
test_multiple_simultaneous_orderstest_position_consistency_under_concurrent_updatestest_async_lock_prevents_race_conditions
3.3 Bracket Order Edge Cases
File: Add to tests/unit/test_order_manager_coverage.py
Tests needed:
test_bracket_entry_fails_no_stop_target_placedtest_bracket_stop_fails_orphaned_position_handlingtest_bracket_partial_fill_scenarios
3.4 Database Recovery
File: tests/unit/test_paper_trader_database.py
Tests needed:
test_load_positions_after_restarttest_corrupted_db_recoverytest_schema_migration_with_existing_data
Phase 4: Execution Layer - Gap Coverage
Goal: Cover async timing and concurrency gaps
4.1 Async Timing Verification
File: Add to existing execution test files
Tests needed:
test_twap_timing_accuracy_under_loadtest_vwap_market_closure_handlingtest_router_concurrent_order_stress
4.2 Error Recovery Paths
Tests needed:
test_order_manager_api_timeout_handlingtest_execution_monitor_missing_fieldstest_slippage_model_extreme_volatility
File Summary
| Action | File | Priority |
|---|---|---|
| Modify | pyproject.toml |
P0 |
| Modify | pytest.ini |
P0 |
| Create | tests/unit/test_emotional_firewall_panic.py |
P1 |
| Create | tests/unit/test_recovery_protocol_history.py |
P1 |
| Create | tests/integration/test_psychology_integration.py |
P1 |
| Create | tests/integration/test_broker_risk_integration.py |
P2 |
| Create | tests/unit/test_paper_trader_concurrency.py |
P2 |
| Create | tests/unit/test_paper_trader_database.py |
P3 |
| Modify | tests/unit/test_psychology.py |
P2 |
| Modify | tests/unit/test_order_manager_coverage.py |
P2 |
Success Criteria
- Coverage Configuration: Running
pytest --cov=coreshows >70% overall - Panic Mode: 100% coverage on EmotionalFirewall panic detection
- Integration: At least 5 multi-module integration tests passing
- Broker Integration: Real KellySizer/CircuitBreaker/StopLossManager tests passing
- Threshold Enforcement: CI fails if coverage drops below 70%
- No More Mocks: Risk system tests use real component instances
Estimated Scope
- New Test Files: 6
- Modified Test Files: 3
- New Tests: ~50-60
- Configuration Changes: 2 files