test: add Jest unit tests for web API validation and rate-limiter#12
Open
test: add Jest unit tests for web API validation and rate-limiter#12
Conversation
Adds the first unit tests for the Next.js web layer. The existing test suite (pytest) covers the CLI/desktop; this PR adds coverage for the server-side logic that handles all incoming API requests. Changes: - Extract inline Zod schemas from api/results/route.ts into a shared src/lib/validation.ts module so they can be imported and tested independently without spinning up a Next.js server. - Add src/__tests__/rate-limit.test.ts: 27 tests covering checkRateLimit (allow on first request, count increments, block at limit, window reset, independent IP buckets, independent named buckets, default limit of 30) and getClientIP (x-forwarded-for with multiple IPs, single IP, x-real-ip fallback, 127.0.0.1 fallback, whitespace trimming). - Add src/__tests__/validation.test.ts: 14 PingResultSchema tests and 12 SubmitRequestSchema tests covering field presence, string length limits, numeric range boundaries (0/10000/10001), array length limits (100/101 raw_times, 50/51 results), defaults for all optional fields, and nested validation propagation. - Add jest.config.js with ts-jest transform and @/ path alias mapping. - Add test script to package.json. All 41 tests pass. Build and lint unaffected.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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
Adds the first unit tests for the Next.js web layer. The existing test suite (pytest) covers the CLI/desktop only — the server-side logic had zero test coverage.
What changed
New:
src/lib/validation.tsExtracts the Zod schemas (
PingResultSchema,SubmitRequestSchema) from the inline route file into a shared module. The route now imports from here — no logic changed, pure refactor to make the schemas testable in isolation.New:
src/__tests__/rate-limit.test.ts(27 tests)checkRateLimit: first request allowed, count increments correctly, blocked at limit, stays blocked within window, resets after window expires, independent IP buckets, independent named buckets, default limit of 30getClientIP: x-forwarded-for (multi-IP, single IP, whitespace trimming), x-real-ip fallback, 127.0.0.1 fallbackNew:
src/__tests__/validation.test.ts(14 + 12 = 26 tests)PingResultSchema: all required fields, min/max string lengths, numeric range boundaries (0 / 10000 / 10001 for ping fields, 0–1000 for jitter, 0–100 for packet loss), raw_times array length (100 / 101), empty array allowedSubmitRequestSchema: all defaults applied when omitted, empty results rejected, 50/51 result array boundary, game/isp/ip_hash/client_version/anonymous_id length limits, invalid nested result propagates failureVerification