Skip to content

Latest commit

 

History

History
427 lines (309 loc) · 15.8 KB

File metadata and controls

427 lines (309 loc) · 15.8 KB

Test Suite Updates - Based on Recent Bugfixes

This document summarizes the comprehensive test suite updates added based on the last 10 bugfixes (commits from 2026-01-09).

Overview

Added two new test files to detect and prevent regressions from critical bugfixes:

  • tests/test_strategy_logic.js - Strategy engine rebalancing and placement logic
  • tests/test_accounting_logic.js - Fund tracking and fee accounting

Bugs Detected and Tests Added

1. VIRTUAL Order Placement Capping Bug (b913661)

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

2. PARTIAL Order Update Bug (b913661)

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

3. Grid Divergence & Stale Cache (c02b66d)

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

4. BoundaryIdx Persistence & Recovery (d17ece6)

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

5. BUY Side Geometric Weighting - Reverse Parameter (d17ece6)

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

6. Fund Tracking Integration (32d81ea)

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

7. Rotation Completion (265772d)

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

8. Fee Calculation with isMaker Parameter (d17ece6)

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

9. Market & Blockchain Taker Fees (7b0a5c5)

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

10. Fund Precision & Delta Validation (0a3d24d)

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

Running the Tests

# 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

Test Coverage Summary

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

Key Assertions

These tests use critical assertions to catch regressions:

  1. Size Assertions: Verify order sizes are correct (no capping, no loss)
  2. State Assertions: Confirm order states transition properly (VIRTUAL→ACTIVE→PARTIAL)
  3. Fund Invariants: Ensure total funds = virtual + committed (no leaks)
  4. Index Assertions: Validate boundaryIdx consistency across operations
  5. Fee Assertions: Confirm both fee types are accounted for

Running Tests in CI/CD

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

Notes for Developers

  • 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

Fill Batching & Recovery Regression Tests (Feb 7-8, 2026)

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.

Overview

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.

Test Coverage

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

Running Tests

# 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.js

Test Details

Test 1: Fixed-Cap Batch Pipeline

Scenario: 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: 4

Test 2: Recovery Retry Mechanism

Scenario: 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 retries

Test 3: Orphan-Fill Deduplication

Scenario: 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

Test 4: Cache Remainder Parity

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

Test 5: Hard-Abort Cooldown Arming

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

Test 6: Single-Op Stale-Cancel Fast-Path

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)

Test Statistics

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

Key Assertions in Tests

  1. Batch Size Assertions: Verify fixed-cap sizing and chunking behavior
  2. Recovery State Assertions: Confirm count/time-based retry state machine
  3. Stale ID Assertions: Validate orphan deduplication map behavior
  4. Cache Remainder Assertions: Verify per-slot allocation tracking
  5. Cooldown Assertions: Confirm hard-abort arms maintenance cooldown
  6. Fast-Path Assertions: Verify single-op stale recovery doesn't trigger full sync

Integration with CI/CD

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.js

Regression Prevention

These 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)

Related Documentation

See complete technical details in:


Test Cleanup & Deprecation (Feb 28, 2026)

Removed Obsolete Tests

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:

  1. Public API tests rather than internal implementation details
  2. Integration tests for end-to-end behavior
  3. Scenario-based tests in COW pipeline test suite

Future Maintenance

When adding new bugfixes:

  1. Create a test that would have caught the bug
  2. Add it to the appropriate test file (strategy.test.js, accounting.test.js, or test_patch*.js)
  3. Reference the commit hash in a comment
  4. Update this summary document
  5. Ensure test covers both success and failure paths
  6. 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.