Repository navigation
test(api): add route tests for /api/ai/autofill - #429
Merged
Merged
Conversation
Cover every branch of the autofill handler: invalid JSON, schema 400s, page-fetch 502, provider 401/403 and other errors, unparseable AI replies, the templateId fallback, the happy path, and the google ?key= URL. meta-fetcher is mocked and fetch is stubbed per test, so nothing leaves the network. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
anurag629
requested changes
Nov 20, 2026
anurag629
left a comment
Member
There was a problem hiding this comment.
Hey @Seyamalam, nice work, every row in the #419 table has a test, fetch is stubbed in beforeEach and restored in afterEach, and the page fetch test checks the provider is never called.
A few things before I can merge:
- The success test (
tests/api/ai-autofill.test.ts:134) has the AI pickblog-minimal-dark, which is the same id the fallback writes atsrc/pages/api/ai/autofill.ts:126. So if the route replaced every id, this test and the fallback one would both still pass. Please haveAI_JSONpick another real template, likeproduct-waitlist, and check that it comes back unchanged. - The google test checks that the key is in the URL, but not the other half of that row, that it stays out of the headers. Please also check that the headers passed to
fetch(the second argument) don't containsk-test.
Everything else looks good.
Have the mocked AI pick product-minimal-badge so the success test fails if the route rewrites every id, and assert the google key stays out of the request headers as well as in the URL. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
Author
|
Note 🤖 SWE-2 High (Devin) responding on behalf of Seyamalam Both fixed in 20903cc:
|
anurag629
approved these changes
Nov 20, 2026
7 tasks
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.
Description
/api/ai/autofillhad no route tests — the/api/airoutes only had schema tests, and thetests/api/ai-autofill.test.tsplanned in #30 never landed. Autofill has the most branches of the three routes since it chains a page fetch, an AI provider call, and a JSON parse.This adds
tests/api/ai-autofill.test.tswith one test per branch: non-JSON body → 400Invalid JSON body; schema failures → 400 naming the field;fetchMetaTagsthrowing → 502Could not fetch the URLwith the provider never called; provider 401/403 → 401Invalid API key; other provider errors → 502AI provider error (500); unparseable reply → 502Could not parse AI response; unknown templateId → 200 falling back toblog-minimal-dark; the happy path → 200 withtemplateId/fields/colors; and thegoogleprovider sending the key in the request URL as?key=.Tests only — the route itself is untouched.
@/lib/meta-fetcheris mocked andfetchis stubbed per test, so no real network happens.Closes #419
How I Tested This
npx vitest run tests/api/ai-autofill.test.ts— 14 tests, all passing.npm run check(0 errors) and the fullnpm run testsuite (2134 tests) pass.AI Assistance
Devin CLI (SWE-2 High): wrote the test file from the issue's branch table and ran the verification; human-directed review of the diff.
Type of Change
Screenshots
N/A — test-only change, no UI.
Checklist
devnpm run checkpassesnpm run testpassesnpm run dev(N/A — route tests exercise the handler directly)Created with SWE-2 High in Devin CLI.