Skip to content

test(lib): add unit tests for src/lib/stellar.ts (closes #382) - #539

Open
Vyacheslav-Tomashevskiy wants to merge 1 commit into
Northgate-Systems:mainfrom
Vyacheslav-Tomashevskiy:test/382-stellar-unit-tests
Open

test(lib): add unit tests for src/lib/stellar.ts (closes #382)#539
Vyacheslav-Tomashevskiy wants to merge 1 commit into
Northgate-Systems:mainfrom
Vyacheslav-Tomashevskiy:test/382-stellar-unit-tests

Conversation

@Vyacheslav-Tomashevskiy

Copy link
Copy Markdown
Contributor

Closes #382

src/lib/stellar.ts had zero test coverage. This adds src/lib/__tests__/stellar.test.ts — 24 unit tests, no network access.

Approach

Horizon, Friendbot and the rate engine are mocked; Asset, Operation, TransactionBuilder and Keypair stay real, so buildSendTransaction assertions run against genuine XDR that is decoded back with TransactionBuilder.fromXDR() rather than against a stubbed builder. The module is re-imported per test (vi.resetModules()) because STELLAR_NETWORK / STELLAR_HORIZON_URL are read at module load.

What is covered

  • Network config — testnet defaults, and STELLAR_NETWORK=public switching NETWORK_PASSPHRASE to Networks.PUBLIC.
  • createTestnetAccount — the returned secret really is the secret for the returned public key (Keypair.fromSecret(...).publicKey()), the exact Friendbot URL, non-OK Friendbot response and a thrown fetch both swallowed (registration must not fail because Friendbot is down), and Friendbot not called on a non-testnet network.
  • fetchRate — delegates to getRate() and returns only .rate; a rate-engine failure propagates instead of yielding a fabricated rate.
  • getAccountBalancesnativeXLM, credit assets → asset_code, Horizon 404 → [] (unfunded), and non-404 errors rethrown rather than being silently reported as an empty wallet.
  • getNetworkStatus — field mapping plus the .order("desc").limit(1) query shape.
  • buildSendTransaction — the 1% destMin slippage floor (123.4567891122.2222212, 7dp), the 180s timebound, malformed recipient rejected before the Horizon round-trip (loadAccount asserted not called), insufficient balance, a missing trustline treated as a zero balance, a configured STELLAR_<CODE>_ISSUER resolving to the right Asset (with lower-case input, since resolveAsset upper-cases), and both issuer-misconfiguration errors (missing env var, malformed key).
  • submitTransaction — success hash, Horizon result_codes surfaced as a JSON string instead of throwing, unknown_error fallback, and an unparseable XDR rejecting before any submission.

Verification

  • npx vitest run src/lib/__tests__/stellar.test.ts24/24 pass.
  • Negative control (mutation check) — these tests actually fail when the code regresses. Flipping * 0.99 to * 1.0 and deleting the status === 404 branch in stellar.ts turned 3 tests red; reverted afterwards, so stellar.ts is unchanged in this PR (test file only, +1 file).
  • npx eslint src/lib/__tests__/stellar.test.ts → clean. npx tsc --noEmit0 errors in this file.
  • Live server (npm run dev): /api/health reports "stellar": true, i.e. the real Horizon path in this module works against testnet, and /api/stellar/status answers normally.

Pre-existing failures, disclosed

npx vitest run on the full suite still fails to collect 4 unrelated test files, and /api/stellar/rate 500s on a live dev server, both with ReferenceError: isValidStellarPublicKey is not defined from src/lib/validations.ts:40. That reproduces on a clean origin/main and is the bug introduced by #527; the fix is waiting in the still-open #529. Nothing in this PR touches it. The 34 tests that do run all pass.

Scope note per the issue's contributor guidance: the issue also mentions "route-finding", but there is no route-finding logic inside src/lib/stellar.ts today (/api/stellar/routes builds its list elsewhere), so I did not invent tests for a function that does not exist.

Covers asset resolution, Friendbot account creation, balance mapping,
network status, path-payment building (incl. the 1% destMin slippage
math) and Horizon submission result-code handling. Horizon, Friendbot
and the rate engine are mocked; Asset/Operation/TransactionBuilder stay
real so the assertions run against genuine XDR.

Closes Northgate-Systems#382
@vercel

vercel Bot commented Sep 9, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the codex723's projects Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add unit tests for src/lib/stellar.ts

1 participant