# Plan: F-bin2 Phase 5 - Integration Test
## Objective
Implement end-to-end test of Binance reconnect drill (F-bin2) using MockBinance with WebSocket simulation.
## Phase 1: Extend MockBinance with WebSocket Simulation
**File:** `broker/tests/mock_binance.rs`
Add methods to MockBinance:
1. `simulate_websocket_disconnect()` - Set disconnected state flag
2. `simulate_websocket_reconnect()` - Clear disconnected state flag
3. State persistence - Orders and client_order_ids persist across disconnect/reconnect
4. `simulate_partial_fill(order_id: &str)` - Update order status to PartiallyFilled
**Verification:** ✅ MockBinance compiles and new methods are accessible.
## Phase 2: Create Integration Test File
**File:** `broker/tests/binance_f_bin2_reconnect_drill.rs` (new)
Create test file with imports and setup for using MockBinance with BinanceBroker.
**Verification:** ✅ File compiles with necessary imports.
## Phase 3: Implement test_no_double_submit_on_reconnect
- Create BinanceBroker with WebSocket mode
- Submit order
- Simulate partial fill
- Inject WebSocket disconnect
- Trigger reconnect
- Verify state reconciliation
- Verify no double-submit on resume (check that duplicate detection works)
**Verification:** ✅ Test passes, no duplicate orders after reconnect.
## Phase 4: Implement test_reconnect_within_30s
- Create BinanceBroker
- Submit multiple orders
- Simulate partial fills
- Simulate disconnect
- Measure time before reconnect (using std::time::Instant)
- Trigger reconnect and reconcile
- Measure time after reconcile
- Assert reconcile_duration_ms < 30_000
- Log reconciliation timing metrics
**Verification:** ✅ Test passes, reconciliation completes within 30s.
## Phase 5: Implement test_state_persists_across_disconnect
- Create BinanceBroker
- Submit order
- Simulate partial fill
- Verify order state before disconnect
- Simulate disconnect
- Simulate reconnect
- Verify order state after reconnect (should match pre-disconnect state)
**Verification:** ✅ Test passes, order state matches across disconnect/reconnect.
## Phase 6: Implement test_reconciliation_detects_orphan_order
- Create BinanceBroker
- Submit order via broker
- Simulate disconnect
- Add orphan order directly to MockBinance (simulate external order)
- Simulate reconnect
- Call reconcile_state()
- Verify orphan order detected in discrepancy report
**Verification:** ✅ Test passes, orphan order detected.
## Phase 7: Run Integration Tests
Run: `cargo test -p nanobook-broker --features binance --test binance_f_bin2_reconnect_drill`
**Verification:** ✅ All tests pass (4/4).
## Notes
- WebSocket simulation is simplified (no actual WebSocket, just state tracking)
- 30s target is for the full reconciliation process (reconnect + account info query + comparison)
- Use std::time::Instant for timing measurements
- Integration test uses BinanceBroker with MockBinance
- For "no double submit" test, focus on verifying reconciliation logic detects discrepancies
## Summary
✅ **Phase 5 Complete** - All 4 integration tests implemented and passing:
1. `test_no_double_submit_on_reconnect` - Verifies duplicate detection prevents double-submit after reconnect
2. `test_reconnect_within_30s` - Measures and validates reconciliation timing < 30s target
3. `test_state_persists_across_disconnect` - Confirms order state persists through disconnect/reconnect cycle
4. `test_reconciliation_detects_orphan_order` - Validates orphan order detection in discrepancy reports