Skip to content

Add Stellar DEX source integration tests with mocked Horizon #33

Description

@prodbycorne

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

  • All 7 scenarios above have passing tests
  • Midpoint calculation asserted precisely (not just truthy)
  • Empty orderbook returns null gracefully
  • Horizon mock used — no real network calls
  • Tests run in < 100ms
  • Code coverage for stellarDex.js ≥ 90%

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial CampaignCampaign: Official CampaigntestingTests and test infrastructure

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions