test(lib): add unit tests for src/lib/stellar.ts (closes #382) - #539
Open
Vyacheslav-Tomashevskiy wants to merge 1 commit into
Open
Conversation
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
|
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. |
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #382
src/lib/stellar.tshad zero test coverage. This addssrc/lib/__tests__/stellar.test.ts— 24 unit tests, no network access.Approach
Horizon, Friendbot and the rate engine are mocked;
Asset,Operation,TransactionBuilderandKeypairstay real, sobuildSendTransactionassertions run against genuine XDR that is decoded back withTransactionBuilder.fromXDR()rather than against a stubbed builder. The module is re-imported per test (vi.resetModules()) becauseSTELLAR_NETWORK/STELLAR_HORIZON_URLare read at module load.What is covered
STELLAR_NETWORK=publicswitchingNETWORK_PASSPHRASEtoNetworks.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 thrownfetchboth swallowed (registration must not fail because Friendbot is down), and Friendbot not called on a non-testnet network.fetchRate— delegates togetRate()and returns only.rate; a rate-engine failure propagates instead of yielding a fabricated rate.getAccountBalances—native→XLM, 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%destMinslippage floor (123.4567891→122.2222212, 7dp), the 180s timebound, malformed recipient rejected before the Horizon round-trip (loadAccountasserted not called), insufficient balance, a missing trustline treated as a zero balance, a configuredSTELLAR_<CODE>_ISSUERresolving to the rightAsset(with lower-case input, sinceresolveAssetupper-cases), and both issuer-misconfiguration errors (missing env var, malformed key).submitTransaction— success hash, Horizonresult_codessurfaced as a JSON string instead of throwing,unknown_errorfallback, and an unparseable XDR rejecting before any submission.Verification
npx vitest run src/lib/__tests__/stellar.test.ts→ 24/24 pass.* 0.99to* 1.0and deleting thestatus === 404branch instellar.tsturned 3 tests red; reverted afterwards, sostellar.tsis unchanged in this PR (test file only, +1 file).npx eslint src/lib/__tests__/stellar.test.ts→ clean.npx tsc --noEmit→ 0 errors in this file.npm run dev):/api/healthreports"stellar": true, i.e. the real Horizon path in this module works against testnet, and/api/stellar/statusanswers normally.Pre-existing failures, disclosed
npx vitest runon the full suite still fails to collect 4 unrelated test files, and/api/stellar/rate500s on a live dev server, both withReferenceError: isValidStellarPublicKey is not definedfromsrc/lib/validations.ts:40. That reproduces on a cleanorigin/mainand 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.tstoday (/api/stellar/routesbuilds its list elsewhere), so I did not invent tests for a function that does not exist.