This document summarizes the comprehensive test suite updates added based on the last 10 bugfixes (commits from 2026-01-09).
Added two new test files to detect and prevent regressions from critical bugfixes:
tests/test_strategy_logic.js- Strategy engine rebalancing and placement logictests/test_accounting_logic.js- Fund tracking and fee accounting
Problem: VIRTUAL orders couldn't activate because their sizes were capped by availablePool, preventing activation of orders with pre-allocated capital.
Tests Added (strategy.test.js):
- ✅
should activate VIRTUAL orders at full allocated size regardless of availablePool - ✅
should only cap size INCREASE, not full order size for VIRTUAL orders
What These Tests Catch:
- Ensures VIRTUAL orders can activate even when availablePool=0
- Verifies only new capital consumption is capped, not pre-allocated amounts
Problem: Non-dust PARTIAL orders weren't being updated, preventing rebalancing when opposite side filled.
Tests Added (strategy.test.js):
- ✅
should update dust PARTIAL orders to target size - ✅
should update non-dust PARTIAL orders for grid rebalancing
What These Tests Catch:
- Detects when PARTIAL orders don't receive update operations
- Verifies both dust and non-dust PARTIALs are handled correctly
Problem: Stale in-memory cache causing false divergence detections during grid comparisons.
Tests Added (strategy.test.js):
- ✅
should detect divergence when grid is reloaded - ✅
should maintain consistent grid state after persistence reload
What These Tests Catch:
- Ensures fresh data is used for divergence checks
- Prevents false positives from stale cache
Problem: Boundary index not persisting across restarts, causing grid misalignment.
Tests Added (strategy.test.js):
- ✅
should initialize boundaryIdx from startPrice on first run - ✅
should recover boundaryIdx from existing BUY orders - ✅
should persist boundaryIdx across rebalance operations
What These Tests Catch:
- Verifies boundary index is properly initialized and recovered
- Ensures grid zones maintain proper BUY/SELL separation
Problem: Wrong reverse parameter for BUY side weighting, causing incorrect capital distribution.
Tests Added (strategy.test.js):
- ✅
should use correct reverse parameter for BUY side weighting - ✅
should concentrate BUY capital near market price
What These Tests Catch:
- Ensures BUY orders get maximum weight at market-closest positions
- Verifies geometric weighting follows expected distribution
Problem: Bootstrap flag and fund tracking integration not tracking spread correction properly.
Tests Added (strategy.test.js):
- ✅
should deduct available funds after new placements - ✅
should not double-deduct for updates and rotations
What These Tests Catch:
- Verifies fund tracking is correct during placement operations
- Prevents double-deduction for rotations
Problem: Rotations being skipped instead of completing after divergence checks.
Tests Added (strategy.test.js):
- ✅
should complete rotations without skipping - ✅
should not skip rotations when divergence check succeeds
What These Tests Catch:
- Ensures rotations are always executed
- Prevents grid gaps from incomplete rotations
Problem: Missing isMaker parameter in getAssetFees calls causing crashes.
Tests Added (strategy.test.js):
- ✅
should correctly process fills with isMaker parameter - ✅
should account for both maker and taker fees in fill processing
What These Tests Catch:
- Ensures isMaker parameter is properly handled
- Detects fee calculation crashes
Problem: Not accounting for both market and blockchain taker fees in fill processing.
Tests Added (accounting.test.js):
- ✅
should account for market taker fees in SELL order proceeds - ✅
should account for blockchain taker fees in fill processing - ✅
should correctly calculate net proceeds with both fee types
What These Tests Catch:
- Verifies both fee types are deducted
- Prevents fund leaks from fee miscalculation
Problem: Precision loss and delta validation issues in fund calculations.
Tests Added (accounting.test.js):
- ✅
should maintain precision when adding multiple orders - ✅
should detect fund delta mismatches - ✅
should validate fund totals after state transitions
What These Tests Catch:
- Detects floating-point precision errors
- Verifies fund invariants are maintained
- Catches lost or phantom funds
# Run all strategy tests
node tests/test_strategy_logic.js
# Run accounting tests (with fee enhancements)
node tests/test_accounting_logic.js
# Run full test suite
npm test| Category | Test Count | Coverage |
|---|---|---|
| Placement Capping | 2 | VIRTUAL order activation |
| PARTIAL Handling | 2 | Dust & non-dust updates |
| Grid Divergence | 2 | Cache & reload logic |
| BoundaryIdx | 3 | Init, recovery, persistence |
| BUY Weighting | 2 | Reverse parameter & distribution |
| CacheFunds | 2 | Deduction tracking |
| Rotations | 2 | Completion & divergence |
| Fees (isMaker) | 2 | Parameter handling |
| Taker Fees | 3 | Market & blockchain fees |
| Fund Precision | 3 | Precision & delta validation |
| Total | 23 | All critical bugfixes |
These tests use critical assertions to catch regressions:
- Size Assertions: Verify order sizes are correct (no capping, no loss)
- State Assertions: Confirm order states transition properly (VIRTUAL→ACTIVE→PARTIAL)
- Fund Invariants: Ensure total funds = virtual + committed (no leaks)
- Index Assertions: Validate boundaryIdx consistency across operations
- Fee Assertions: Confirm both fee types are accounted for
These tests are integrated with the existing test suite:
# Full test run (includes new tests)
npm test
# Run individual test file
node tests/test_strategy_logic.js- Tests use
manager.pauseFundRecalc()/resumeFundRecalc()for batch operations - Fee cache is mocked where necessary to avoid external dependencies
- All tests are async-safe and handle rebalance promises correctly
- Tests clean up state in
beforeEach()to ensure isolation
The tests above target individual bugfixes discovered during normal operation. The tests below target a different failure class: cascading failures under market stress. These were written after the Feb 7 crash where 29 rapid fills exposed race conditions in batch processing, orphan-fill handling, and recovery retry logic.
Coverage for the Feb 7 market crash post-mortem fixes is now split across active test files. These tests ensure fill batching, recovery retry system, and orphan-fill deduplication remain robust.
Fill Batching & Recovery Retries:
| Test | File | Purpose |
|---|---|---|
simulateBatching |
tests/sim_batching.js |
Verifies fixed-cap behavior: <=MAX_FILL_BATCH_SIZE unified; larger queues chunked at max 4 |
recovery retry cooldown and reset |
tests/test_accounting_logic.js |
Validates count+time-based retry system with cooldown and retry counter behavior |
testSingleStaleCancelBatchUsesStaleOnlyFastPath |
tests/test_patch17_invariants.js |
Ensures stale-cleaned order IDs prevent double-crediting of delayed orphan fills |
Cache & Stale-Order Handling:
| Test | File | Purpose |
|---|---|---|
testGridResizeRespectsBudgetAfterCap |
tests/test_patch17_invariants.js |
Verifies capped grid resize never allocates above budget and preserves correct remainder behavior |
testIllegalBatchAbortArmsMaintenanceCooldown |
tests/test_patch17_invariants.js |
Ensures illegal-state batch hard-abort arms _maintenanceCooldownCycles |
testSingleStaleCancelBatchUsesStaleOnlyFastPath |
tests/test_patch17_invariants.js |
Validates single-op stale cancel recovery avoids unnecessary full sync |
# Run fixed-cap batching simulator
node tests/sim_batching.js
# Run recovery retry coverage
node tests/test_accounting_logic.js
# Run batch hard-abort/stale-cancel/capped-resize invariants
node tests/test_patch17_invariants.jsScenario: Simulate 29 fills arriving over 90 seconds of market crash
Validation:
- ✅ Fixed-cap batching enforced (max 4 fills per broadcast)
- ✅ All fills processed in ~8 broadcasts instead of 29
- ✅ Processing time reduced from ~90s to ~24s
- ✅ Single rebalance call per batch (not per-fill)
- ✅ Cache funds credited once per batch
Configuration Tested:
MAX_FILL_BATCH_SIZE: 4Scenario: Recovery fails once, should retry automatically
Validation:
- ✅ First recovery attempt fails, recorded in state
- ✅ Retry counter increments (1/5)
- ✅ Retry blocked before 60s interval elapses
- ✅ After 60s, retry succeeds
- ✅ State reset on success (count=0, time=0)
- ✅ Periodic sync also triggers state reset
Configuration Tested:
RECOVERY_RETRY_INTERVAL_MS: 60000 // 60 second minimum
MAX_RECOVERY_ATTEMPTS: 5 // Max 5 retriesScenario: Batch fails (stale order), cleanup recorded, orphan fill arrives later
Validation:
- ✅ Stale order ID recorded with timestamp when batch fails
- ✅ Orphan fill event arrives for same order ID
- ✅ Guard check (
_staleCleanedOrderIds.has(orderId)) returns true - ✅ Proceeds NOT credited (double-credit prevented)
- ✅ Log shows
[ORPHAN-FILL] Skipping double-credit - ✅ TTL pruning removes entries after 5 minutes
Scenario: Grid resize capped by available funds
Validation:
- ✅ Ideal grid total: 1000 BTS
- ✅ Available funds: 600 BTS
- ✅ Allocated: 600 BTS (track per-slot applied sizes)
- ✅ Unallocated remainder: 400 BTS (1000 - 600, not inflated)
- ✅ Next cycle gets correct 400 BTS available for allocation
- ✅ No "stuck fund" situations
Scenario: Batch execution fails with illegal state or accounting error
Validation:
- ✅ Primary hard-abort path arms cooldown (50 cycles)
- ✅ Retry hard-abort path also arms cooldown
- ✅ Maintenance doesn't run immediately after abort
- ✅ Next maintenance cycle respects cooldown
- ✅ Log shows
[COOLDOWN] Arming maintenance cooldown after hard-abort
Scenario: Single order in batch becomes stale on-chain
Validation:
- ✅ Batch contains only 1 operation
- ✅ Stale order detected during execution
- ✅ Fast-path recovery applied (not full sync)
- ✅ Cleanup performed locally (stale ID recorded)
- ✅ No expensive
synchronizeWithChain()call - ✅ Log shows
[STALE-CANCEL] Single-op batch stale recovery (fast-path)
Total Tests: 6
Status: ✅ All passing
Modules Tested:
- dexbot_class.js (batch processing, recovery, hard-abort, stale-cancel)
- accounting.js (recovery state management, resetRecoveryState)
- order/grid.js (remainder tracking during capped resize)
- order/strategy.js (fill boundary shifts, reaction cap precision)
Configuration Validated:
- FILL_PROCESSING.MAX_FILL_BATCH_SIZE
- PIPELINE_TIMING.RECOVERY_RETRY_INTERVAL_MS
- PIPELINE_TIMING.MAX_RECOVERY_ATTEMPTS
Coverage Targets:
✅ Fill batch processing (1-4 fills per broadcast)
✅ Fixed-cap batch sizing (max 4 per batch)
✅ Recovery retry system (count+time-based)
✅ Orphan-fill double-credit prevention
✅ Remainder accuracy (per-slot tracking)
✅ Hard-abort cooldown consistency
✅ Stale-order fast-path recovery
- Batch Size Assertions: Verify fixed-cap sizing and chunking behavior
- Recovery State Assertions: Confirm count/time-based retry state machine
- Stale ID Assertions: Validate orphan deduplication map behavior
- Cache Remainder Assertions: Verify per-slot allocation tracking
- Cooldown Assertions: Confirm hard-abort arms maintenance cooldown
- Fast-Path Assertions: Verify single-op stale recovery doesn't trigger full sync
Fill batching tests are part of the main test suite:
# Full test suite
npm test
# Targeted fixed-cap batching and recovery tests
node tests/sim_batching.js && node tests/test_accounting_logic.js && node tests/test_patch17_invariants.jsThese tests catch regressions in:
- Fill batch processing logic (fixed-cap sizing)
- Recovery retry state management (count+time system)
- Orphan-fill deduplication guards (stale ID tracking)
- Remainder calculation (per-slot accuracy)
- Hard-abort cooldown arming (both paths)
- Stale-order fast-path recovery (single-op optimization)
See complete technical details in:
- docs/FUND_MOVEMENT_AND_ACCOUNTING.md § 1.4 - Fill batch processing
- docs/FUND_MOVEMENT_AND_ACCOUNTING.md § 3.6 - Orphan-fill prevention
- docs/architecture.md - Fill Processing Pipeline - Architecture & diagrams
The following 5 test files were removed as they referenced deprecated APIs or tested internal implementation details:
| Test File | Reason | Coverage |
|---|---|---|
test_rotation_fallback_recheck.js |
Cleanup verification only; verifying old helper functions were removed | Other tests cover rotation logic |
test_grid_funding_manual.js |
References removed Grid.updateGridOrderSizesForSide() API |
Strategy module tests cover grid sizing |
test_fee_refinement.js |
Tests old accounting behavior; proceeds now consumed immediately | test_bts_fee_accounting.js, test_fee_backwards_compat.js |
test_fill_queue_logic.js |
Unit tests internal _consumeFillQueue implementation detail |
Integration tests & COW pipeline tests cover fills |
test_fix_proceeds_fee_deduction.js |
Uses removed OrderManager methods | test_accounting_logic.js, test_bts_fee_accounting.js |
Impact: No regression risk. All removed functionality is covered by current active tests.
Migration Path: If these test scenarios need specific coverage in the future, implement through:
- Public API tests rather than internal implementation details
- Integration tests for end-to-end behavior
- Scenario-based tests in COW pipeline test suite
When adding new bugfixes:
- Create a test that would have caught the bug
- Add it to the appropriate test file (strategy.test.js, accounting.test.js, or test_patch*.js)
- Reference the commit hash in a comment
- Update this summary document
- Ensure test covers both success and failure paths
- Test maintenance: Remove tests that reference deprecated APIs or internal details no longer in use
This ensures continuous regression detection and documents the evolution of the test suite.