test: add unit tests for env config validation schema - #485
Merged
K1NGD4VID merged 6 commits intoSep 2, 2026
Conversation
Export the env schema and error formatter from src/config.ts and rewrite tests/config.test.ts to validate the real schema instead of a duplicated, out-of-date copy. Covers valid configs (testnet/public, coercion, defaults), every missing/empty mandatory variable, invalid formats, numeric bounds, and error reporting.
|
@vrse-vrde Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
MergeKeeper review Scope: in scope for linked issue The pull request correctly implements unit tests for environment configuration validation, expense payload validation, and expense split calculations as requested by the linked issues. Reviewed commit: |
…pay#442, mergepay#456) Extend tests/settlement.test.ts with even and sub-stroop splits, single participant, zero/negative amount guards, and empty participant list. Extend tests/validations/expense.test.ts with zero/exponent amounts, 7-decimal precision, unsupported asset codes, XLM-with-issuer rejection, USDC acceptance, invalid splitType, and descriptive error messages.
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.
Summary
Closes #446, Closes #456, Closes #442 — one PR covering three related testing/validation gaps:
src/config.ts.What changed
src/config.ts— exported the Zod schema asenvSchemaand the error formatter assafeErrorMessage(purely additive; runtime behavior unchanged).tests/config.test.ts— rewritten to validate the real schema instead of a duplicated, out-of-date copy. The old file re-declared a partial schema that had drifted (referencedAUTH_RATE_LIMIT_MAX/SETTLEMENT_RATE_LIMIT_MAX, which no longer exist, and missed ~80 real fields). A copied schema can pass while the actual startup validation is broken, so tests now import and exerciseenvSchemadirectly.tests/settlement.test.ts— extended thecomputeSharescoverage (the expense split engine insrc/services/settlement.ts, BigInt stroop math): exact/even splits, sub-stroop splits with a 1-stroop remainder, single participant, single custom share, zero and negative amount guards, empty participant list, exact 3-way percentage shares, and percentage tolerance.tests/validations/expense.test.ts— extendedcreateExpenseSchemacoverage: zero amount, >7-decimal precision, exponent notation, unsupported asset code, XLM-with-issuer rejection, USDC acceptance (with and without the configured issuer), invalidsplitType, and descriptive error-message assertions for title/amount/asset failures.Key design decisions
src/config.tsso tests can never silently drift from what runs at startup. Importingsrc/config.tsin tests is an established pattern (20+ test files do it).process.envshape. All mock config values are strings (as dotenv would provide them), so coercion paths (PORT: "4000"→4000) are exercised realistically.currency ('XLM' | 'USDC')as a plain enum, but the repo validates assets through the sharedvalidateAssetregistry insrc/lib/money.ts(XLM native; USDC againstSTABLE_ASSET_ISSUER), which is the single source of truth used by both request validation and XDR construction. Amounts are decimal strings (never floats) validated to 7dp — matching the repo's financial-math conventions. Tests assert the real, descriptive error messages from those shared validators.Acceptance criteria
#446
src/config.ts(schema + startup parse +safeErrorMessage).tests/config.test.tsvalid-configuration block: testnet/public, coercion, defaults (PORT4000,LOG_LEVEL"info",CORS_ALLOW_CREDENTIALSfalse,ACCESS_TOKEN_TTL_SECONDS900), optional SEP-10/Horizon fields.it.eachover all 10 required keys, both missing and empty, plus assertions the missing variable's name appears in the reported error.#456
src/validations/expense.ts(createExpenseSchema, already present; imported bysrc/routes/expenses.ts).min(1).max(80); amount viacanonicalAmountSchema(positive, 7dp max, no exponent/whitespace); asset viarefineValidatedAsset(XLM/USDC against the shared registry);splitTypeenum + non-emptyshareswith split-type-specific checks (custom amounts present, percentages sum to 100 ±0.01).tests/validations/expense.test.ts(37 tests): valid equal/custom/percentage/optional-field payloads; invalid title/amount/asset/shares/splitType; message-content assertions ("greater than zero","7-decimal precision","Unsupported asset code","native asset","at least 1 character").#442
tests/settlement.test.tsforcomputeSharesinsrc/services/settlement.ts(pure, BigInt-stroop math — no I/O).9/3 →3each), uneven with remainder absorbed on first share (10/3 →3.3333334/3.3333333;0.01/3 →0.0033334/0.0033333), single participant, zero amount, negative amount, empty participant list, percentage remainder/exactness and 0.001 tolerance.Test output + coverage
npx vitest run tests/config.test.ts tests/settlement.test.ts tests/validations/expense.test.ts→ 99/99 passed (43 config + 37 expense validation + 19 settlement).git stash) — pre-existing, environment-dependent (DB/network) failures unrelated to this PR.npx tsc --noEmit→ clean.npm run lint→ 0 errors (26 pre-existing warnings, none in the changed files).@vitest/coverage-v8not in devDependencies) and no threshold configured invitest.config.ts. To measure:npm i -D @vitest/coverage-v8thennpx vitest run --coverage tests/config.test.ts tests/settlement.test.ts tests/validations/expense.test.ts --coverage.include="src/config.ts" --coverage.include="src/services/settlement.ts" --coverage.include="src/validations/expense.ts".Follow-ups (honest)
@vitest/coverage-v8+ acoverage.thresholdsblock invitest.config.tswould let CI enforce a floor for these modules going forward.main; this PR completes the acceptance-criteria coverage rather than introducing new source code.Security note
Only additive exports from
src/config.tsand test-only changes; no validation rules, secrets handling, or runtime behavior changed. No secrets or credentials are introduced by this PR.