Skip to content

test: add Jest unit tests for web API validation and rate-limiter#12

Open
bokiko wants to merge 1 commit intomainfrom
auto/improve-2026-03-20-testing
Open

test: add Jest unit tests for web API validation and rate-limiter#12
bokiko wants to merge 1 commit intomainfrom
auto/improve-2026-03-20-testing

Conversation

@bokiko
Copy link
Owner

@bokiko bokiko commented Mar 20, 2026

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.ts
Extracts 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 30
  • getClientIP: x-forwarded-for (multi-IP, single IP, whitespace trimming), x-real-ip fallback, 127.0.0.1 fallback

New: 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 allowed
  • SubmitRequestSchema: 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 failure

Verification

npm test   → 41 tests, 2 suites — all pass
npm build  → clean build, no errors

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.
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

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.

1 participant