Skip to content

test: contract-test API routes against apiContracts schemas#896

Merged
1nonlypiece merged 1 commit into
Commitlabs-Org:masterfrom
fikrah-Tech:test/api-contract-schemas
Jun 27, 2026
Merged

test: contract-test API routes against apiContracts schemas#896
1nonlypiece merged 1 commit into
Commitlabs-Org:masterfrom
fikrah-Tech:test/api-contract-schemas

Conversation

@yunus-dev-codecrafter

Copy link
Copy Markdown
Contributor

closes #775

test: contract-test API routes against apiContracts schemas

Summary

Adds a schema-contract test (tests/api/contract-schemas.test.ts) that validates real handler responses against the declared apiContracts Zod schemas, ensuring the API shape stays in sync with its contract definitions.

Also fixes two schema/handler mismatches that the new test uncovered.

Changes

New file: tests/api/contract-schemas.test.ts

  • 11 tests covering 5 read routes via a centralized routeSchemaMap:

    Route Schema
    GET /api/health HealthResponseSchema
    GET /api/commitments CommitmentsListResponseSchema
    GET /api/commitments/[id] CommitmentDetailResponseSchema
    GET /api/commitments/search CommitmentSearchResponseSchema
    GET /api/marketplace/listings MarketplaceListingsResponseSchema
  • Success envelope (5 tests) — calls each handler, asserts status === 200, success === true, and schema.safeParse(body).success === true

  • Empty collections (3 tests) — verifies [] items/data/listings still pass schema validation

  • Optional/nullable fields (2 tests) — commitments with missing complianceScore, currentValue, feeEarned, etc.; commitment detail with maxLossPercent: null and absent drawdownPercent/tokenId

  • Error envelope (1 test) — calls GET /api/commitments without ownerAddress, asserts 400 status and ErrorBodySchema.safeParse(body).success === true

  • Adding a new route is a one-line addition to routeSchemaMap

  • All mock data factories (makeChainCommitment, makeChainCommitmentDetail, makeMarketplaceListing) are centralized and reusable

Fixed: schema-handler mismatches (caught by the new test)

  • src/lib/schemas/apiContracts.ts:

    • HealthResponseSchema: changed from data: { status, timestamp } to data: { status, uptime, version } to match what the handler actually returns
    • CommitmentDetailSchema: drawdownPercent and tokenId changed from .optional() to .nullable().optional() to match the handler's ?? null pattern
  • tests/api/contracts.test.ts: Updated HealthResponseSchema unit tests to reflect the corrected schema shape

Test results

  • pnpm vitest run tests/api/contract-schemas.test.ts11/11 passed
  • pnpm vitest run tests/api/contracts.test.ts70/70 passed (existing)
  • pnpm vitest run tests/api/health.test.ts2/2 passed (existing)
  • pnpm vitest run tests/api/commitment-search.test.ts51/51 passed (existing)
  • pnpm run lint — no new errors from these changes

@vercel

vercel Bot commented Jun 27, 2026

Copy link
Copy Markdown

@yunus-dev-codecrafter is attempting to deploy a commit to the 1nonly's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave

drips-wave Bot commented Jun 27, 2026

Copy link
Copy Markdown

@yunus-dev-codecrafter 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! 🚀

Learn more about application limits

@1nonlypiece 1nonlypiece merged commit b82dc2d into Commitlabs-Org:master Jun 27, 2026
0 of 2 checks passed
@1nonlypiece

Copy link
Copy Markdown
Contributor

contract-testing the api routes against the schemas is exactly what we needed. in

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 a contract test asserting API routes conform to the apiContracts schemas

2 participants