Overview
src/services/sources/stellarDex.js fetches the orderbook from Horizon to derive prices. It has no tests, so regressions in the price derivation logic (midpoint calculation, empty orderbook handling) go undetected.
Scenarios to Test
| Scenario |
Expected behaviour |
| Normal orderbook (asks + bids) |
Returns midpoint of best ask and best bid |
| Empty asks array |
Uses best bid only |
| Empty bids array |
Uses best ask only |
| Both empty |
Returns null |
| Horizon returns non-200 |
Throws error |
| Horizon times out |
Throws error with timeout message |
| XLM/native asset (no issuer) |
Uses correct orderbook pair with native |
Test Setup
Mock @stellar/stellar-sdk Horizon.Server:
vi.mock('@stellar/stellar-sdk', () => ({
Horizon: {
Server: vi.fn().mockImplementation(() => ({
orderbook: vi.fn().mockReturnValue({
call: vi.fn()
})
}))
}
}));
Price Derivation Verification
Given:
- Best ask:
0.12 (someone selling XLM for USDC)
- Best bid:
0.11 (someone buying XLM with USDC)
Expected midpoint: 0.115
Test should assert this calculation is correct, not just that a number is returned.
Acceptance Criteria
Overview
src/services/sources/stellarDex.jsfetches the orderbook from Horizon to derive prices. It has no tests, so regressions in the price derivation logic (midpoint calculation, empty orderbook handling) go undetected.Scenarios to Test
nullTest Setup
Mock
@stellar/stellar-sdkHorizon.Server:Price Derivation Verification
Given:
0.12(someone selling XLM for USDC)0.11(someone buying XLM with USDC)Expected midpoint:
0.115Test should assert this calculation is correct, not just that a number is returned.
Acceptance Criteria
nullgracefullystellarDex.js≥ 90%