feat(tests): add response schema stability tests for GET /api/billing/credits - #857
Merged
greatest0fallt1me merged 1 commit intoJul 27, 2026
Conversation
…/credits - Add tests/schema/credits.test.ts with 19 tests covering: - 200 success shape: exact keys, field types, ISO 8601 timestamps - New-user zero-balance auto-creation response - High-precision balance (up to 7 decimal places as string) - 401 unauthenticated: standardized error envelope shape - 400 unexpected query param: VALIDATION_ERROR envelope shape - Structural inline snapshot for schema-drift CI detection - Commit tests/schema/__snapshots__/credits.test.ts.snap (6 snapshots) - Fix: add missing creditsHistogramMiddleware import in credits.ts - Fix: remove duplicate declarations in errorHandler.ts that caused SyntaxError at parse time in all tests importing errorHandler directly Closes CalloraOrg#688
|
@christy-dev4 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
|
Merged via direct push to main (admin) |
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.
Response Schema Stability Test for
GET /api/billing/creditsSummary
Adds a dedicated response schema stability test suite for the
GET /api/billing/creditsendpoint (tests/schema/credits.test.ts). The test suite uses Jest snapshot testing and inline assertions to lock down the exact response shape so that any accidental schema drift — a renamed field, a changed type, an added or removed key — fails immediately in CI rather than shipping silently.This is part of the GrantFox FWC26 campaign (Stellar Wave) stability work.
What Changed
New:
tests/schema/credits.test.tsA 19-test suite covering the full observable surface of
GET /api/billing/credits:0.00balance shape is identical to existing-user shapesuccess: false,UNAUTHORIZEDcodeVALIDATION_ERRORerror envelope shape,detailsarray with field-level diagnosticstoMatchInlineSnapshoton key/type metadata for schema-drift CI detectionThe six
toMatchSnapshotcalls produce a committed snapshot file (tests/schema/__snapshots__/credits.test.ts.snap) so the exact wire format is code-reviewed alongside the test.Bug fix:
src/routes/billing/credits.tsAdded the missing import for
creditsHistogramMiddlewarefrom../../middleware/creditsHistogram.js. The middleware was already wired into the route handler but was never imported, causing aReferenceErrorat module load time in any test that imported this router directly.Bug fix:
src/middleware/errorHandler.tsThe file contained two merged/duplicated versions of the module — two
const isProductiondeclarations, two import blocks, and twodetails/bodyvariable declarations insideerrorHandler(). This caused aSyntaxError: Identifier 'isProduction' has already been declaredat parse time, breaking any test suite that importederrorHandlerdirectly (including the existingtests/schema/export.test.ts). The duplicate block has been removed, leaving the canonical implementation intact.Test Strategy
Tests follow the patterns established in:
tests/schema/usage.test.ts— inline key assertions +toMatchSnapshotfor structural drifttests/schema/export.test.ts—toMatchSnapshotfor full success and error envelopesThe credits test uses a mocked
defaultCreditsRepositoryso the suite is fully self-contained and does not require a running database. All fixtures use deterministic timestamps and user IDs, making snapshots stable across environments.Test coverage on changed lines
tests/schema/credits.test.tssrc/routes/billing/credits.tssrc/middleware/errorHandler.tsTotal: well above the 90 % minimum on changed lines.
How to Run
Response Schema (Locked)
200 OK —
{ user_id, balance_usdc, created_at, updated_at }(all strings, timestamps in ISO 8601){ "user_id": "dev-schema-test-user", "balance_usdc": "42.50", "created_at": "2026-01-01T00:00:00.000Z", "updated_at": "2026-03-15T08:30:00.000Z" }401 Unauthorized — standardized error envelope
{ "success": false, "error": { "code": "UNAUTHORIZED", "message": "Unauthorized" }, "requestId": "...", "timestamp": "..." }400 Bad Request — standardized error envelope with
details{ "success": false, "error": { "code": "VALIDATION_ERROR", "message": "Request validation failed", "details": [ { "code": "UNRECOGNIZED_KEYS", "field": "query", "message": "Unrecognized key: \"unexpected\"" } ] }, "requestId": "...", "timestamp": "..." }Checklist
19/19 passed, 6 snapshots stable)creditsHistogramMiddlewareimport bug fixederrorHandler.tsduplicate-declaration bug fixedCloses #688