test: add Vitest suite covering AI fallback, rate limiter, audit scoring, admin auth - #1
Draft
riteshbonthalakoti wants to merge 8 commits into
Draft
test: add Vitest suite covering AI fallback, rate limiter, audit scoring, admin auth#1riteshbonthalakoti wants to merge 8 commits into
riteshbonthalakoti wants to merge 8 commits into
Conversation
Constructing all 5 OpenAI clients up front made getProviders() throw whenever any single provider key was unset, since the SDK validates apiKey synchronously — one missing key crashed the whole audit request instead of being skipped as intended.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
What
Adds a Vitest test suite to a codebase that previously had zero automated tests, covering the highest-risk pure/isolable logic:
lib/ai/providers.ts—isRetryableError,withFallback, andgetProviders()key filteringlib/rate-limit.ts— free-tier window/reset/per-IP behavior andgetClientIpheader parsinglib/ai/analyze.ts—runAuditscore clamping/rounding, markdown-fence stripping, malformed-JSON fallback, and multi-dimension score averaginglib/email.ts—scoreColor/scoreVerdictthreshold boundaries (exported for direct testing)app/api/admin/upgrade-self/route.ts— the admin-email authorization gate (401/403/200 paths)Also fixes a real bug found while writing the provider tests:
getProviders()built all 5OpenAIclients eagerly before filtering by key presence, and the OpenAI SDK throws synchronously on an emptyapiKey— so any environment missing even one of the five provider keys would crash the whole audit request instead of gracefully skipping that provider. Fixed by filtering configs by key presence before constructing clients.Why
The repo had no test runner, no config, and no test files across 91 TS/TSX files, despite containing payment webhooks, an admin authorization gate, and the core AI scoring pipeline. This adds test infrastructure (Vitest) and an initial suite targeting the areas where a silent regression would be costliest: entitlement/auth checks, the AI provider fallback chain (the app's core reliability mechanism), and audit score integrity.
Type
Checklist
npx vitest runpasses (44/44 tests)npx tsc --noEmitpassesnpx eslintpasses on all new/changed filesnpm run build— not verified in this sandbox; it fails here becauseRESEND_API_KEY/STRIPE_SECRET_KEYaren't configured in this environment (pre-existing eager SDK client construction inlib/email.tsandapp/api/stripe/route.ts, unrelated to this change)Generated by Claude Code