test(api/chat): add DI seam for OpenAI client + mocked route tests#18
Open
sergiopesch wants to merge 1 commit into
Open
test(api/chat): add DI seam for OpenAI client + mocked route tests#18sergiopesch wants to merge 1 commit into
sergiopesch wants to merge 1 commit into
Conversation
Introduces a minimal dependency-injection seam so POST /api/chat can
be driven in tests without a live OpenAI API key:
app/api/chat/openai-client.ts
export type ChatCompletionClient = { chat: { completions: { create } } }
export function getChatClient(): ChatCompletionClient
export function __setChatClientForTests(client | null): void
Production behavior is unchanged: getChatClient() lazily constructs a
real OpenAI client when no test override is set. The route.ts handler
now imports getChatClient() instead of constructing OpenAI inline.
New tests (tests/chat-route-mocked.test.ts, 7 tests):
- Returns a validated spec card from a canned LLM response
- Voice mode attaches voice.spokenSummary
- Unparseable LLM output -> FALLBACK_TEXT_RESPONSE
- Partially-valid payload is rescued via toSafeTextResponse
- AbortError from the client -> FALLBACK_TEXT_RESPONSE (not 500)
- Non-timeout upstream error -> 500 with generic error message
- Post-normalize: requestedImageCount > 1 overrides LLM's imageCount
Together with the pure-function tests added earlier, POST /api/chat
now has end-to-end coverage for every major branch except the
happy-path forced-photo fast path (already covered).
Total: 17 -> 24 tests. All pass with validate + build.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Why
The pure-function tests added in #16 cover every branch of
POST /api/chatexcept the ones that actually call OpenAI. That means the normalize → validate → sanitize pipeline (the bulk of the server-side logic) is exercised only through its individual unit tests, not end-to-end.Rather than mock OpenAI at the module level (brittle, tsx-specific) or spin up a real API call in CI (flaky, needs secrets), introduce a minimal dependency-injection seam so tests can swap in a fake client.
What
app/api/chat/openai-client.ts(new)new OpenAI(), same as before.__setChatClientForTests(fake)to inject,nullto clear.app/api/chat/route.tsOne-line swap:
getClient()→getChatClient().import OpenAI from 'openai'is removed (no longer needed in this file — the concrete type lives behind the seam).No behavior change on the happy path.
tests/chat-route-mocked.test.ts(new, 7 tests)Drives the full handler with canned LLM outputs:
voice.spokenSummaryVOICE_PROMPT_RULES!normalizedbranchtoSafeTextResponse!validated+ text-recovery branchrequestedImageCount > 1overrides LLM'simageCountThe fake client also records calls, so tests can assert on the exact
messages/modelsent to OpenAI.Test count
#17fix/chat-route-hardeningVerification
npm run validatenpm test— 24/24npm run build— production build succeedsDepends on #17 (uses
__resetRateLimitMapForTestsfrom there). Stacked ontofix/chat-route-hardening.