Skip to content
Shell
6.5 KB
Raw Blame

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:

  1. Coverage configuration not measuring existing tests
  2. Psychology panic mode detection (truly untested)
  3. Multi-module integration tests (only 1 exists)
  4. Broker layer relies heavily on mocks instead of real integration
  5. 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:

  1. Verify pytest-cov configuration

    • File: pyproject.toml and pytest.ini
    • Ensure --cov=core captures all test imports
    • Add branch coverage: --cov-branch
  2. Check test imports

    • Verify tests import from core.* not local paths
    • Files to check:
      • tests/unit/test_execution_router.py
      • tests/unit/test_execution_monitor.py
      • tests/unit/test_slippage_model.py
      • tests/unit/test_twap_executor.py
      • tests/unit/test_vwap_executor.py
  3. Add coverage threshold enforcement

    • Update pyproject.toml: fail_under: 70
    • This matches the frontend threshold and will drive test improvements

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_tracking
  • test_panic_mode_triggered_3_interventions_in_10_min
  • test_panic_cooldown_2_hours_enforced
  • test_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_timezone
  • test_dst_transition_handling
  • test_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_start
  • test_phase_completion_recorded_on_advance
  • test_phase_duration_calculation
  • test_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_together
  • test_timeout_escalates_to_recovery_protocol
  • test_all_four_modules_coordinated
  • test_circuit_breaker_triggers_recovery_mode

2.5 Boundary Condition Tests

File: Add to existing test files

Tests needed:

  • test_exactly_20_percent_drawdown_boundary
  • test_exactly_30_min_revenge_window_expiration
  • test_exactly_3_consecutive_losses_boundary
  • test_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 calculations
  • CircuitBreaker - verify actual tripping logic
  • StopLossManager - verify actual stop placement
  • PortfolioRisk - verify heat accumulation

Tests needed:

  • test_order_rejected_when_kelly_sizer_exceeds_limit
  • test_circuit_breaker_trips_after_real_losses
  • test_stop_loss_actually_placed_after_buy
  • test_portfolio_heat_accumulates_across_orders
  • test_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_orders
  • test_position_consistency_under_concurrent_updates
  • test_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_placed
  • test_bracket_stop_fails_orphaned_position_handling
  • test_bracket_partial_fill_scenarios

3.4 Database Recovery

File: tests/unit/test_paper_trader_database.py

Tests needed:

  • test_load_positions_after_restart
  • test_corrupted_db_recovery
  • test_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_load
  • test_vwap_market_closure_handling
  • test_router_concurrent_order_stress

4.2 Error Recovery Paths

Tests needed:

  • test_order_manager_api_timeout_handling
  • test_execution_monitor_missing_fields
  • test_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

  1. Coverage Configuration: Running pytest --cov=core shows >70% overall
  2. Panic Mode: 100% coverage on EmotionalFirewall panic detection
  3. Integration: At least 5 multi-module integration tests passing
  4. Broker Integration: Real KellySizer/CircuitBreaker/StopLossManager tests passing
  5. Threshold Enforcement: CI fails if coverage drops below 70%
  6. 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