Skip to content

test(api): add route tests for /api/ai/autofill - #429

Merged
anurag629 merged 2 commits into
codercops:devfrom
Seyamalam:test/ai-autofill-route-tests
Nov 20, 2026
Merged

anurag629 merged 2 commits into
codercops:devfrom
Seyamalam:test/ai-autofill-route-tests

Conversation

@Seyamalam

Copy link
Copy Markdown
Contributor

Description

/api/ai/autofill had no route tests — the /api/ai routes only had schema tests, and the tests/api/ai-autofill.test.ts planned 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.ts with one test per branch: non-JSON body → 400 Invalid JSON body; schema failures → 400 naming the field; fetchMetaTags throwing → 502 Could not fetch the URL with the provider never called; provider 401/403 → 401 Invalid API key; other provider errors → 502 AI provider error (500); unparseable reply → 502 Could not parse AI response; unknown templateId → 200 falling back to blog-minimal-dark; the happy path → 200 with templateId/fields/colors; and the google provider sending the key in the request URL as ?key=.

Tests only — the route itself is untouched. @/lib/meta-fetcher is mocked and fetch is 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.
  • Per the issue's suggestion, broke two branches on purpose (limited the 401/403 check to 401 only, changed the templateId fallback) — the matching tests failed; restored and all green again.
  • npm run check (0 errors) and the full npm run test suite (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

  • Tests

Screenshots

N/A — test-only change, no UI.

Checklist

  • This PR targets dev
  • I was assigned the linked issue
  • npm run check passes
  • npm run test passes
  • For a bug fix: the new test fails without my change (verified by breaking route branches)
  • Tested locally with npm run dev (N/A — route tests exercise the handler directly)
  • Screenshots included for visual changes (N/A)
  • New templates follow the Template Guidelines (N/A)

Created with SWE-2 High in Devin CLI.

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 anurag629 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. The success test (tests/api/ai-autofill.test.ts:134) has the AI pick blog-minimal-dark, which is the same id the fallback writes at src/pages/api/ai/autofill.ts:126. So if the route replaced every id, this test and the fallback one would both still pass. Please have AI_JSON pick another real template, like product-waitlist, and check that it comes back unchanged.
  2. 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 contain sk-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>
@Seyamalam

Copy link
Copy Markdown
Contributor Author

Note

🤖 SWE-2 High (Devin) responding on behalf of Seyamalam

Both fixed in 20903cc:

  1. AI_JSON now picks product-minimal-badge (a real template — product-waitlist doesn't exist in the registry) and the success test asserts it comes back unchanged, so it fails if the route rewrites every id. Verified: forcing the fallback for all ids makes that test fail.
  2. The google test now also checks init.headers from the fetch call — asserts sk-test is not in the headers, alongside ?key= in the URL.

@anurag629 anurag629 added the codercops-accepted Merged PR that counts on the monthly leaderboard (oss.codercops.com) label Nov 20, 2026
@anurag629
anurag629 merged commit a8a5e76 into codercops:dev Nov 20, 2026
3 checks passed
@Seyamalam
Seyamalam deleted the test/ai-autofill-route-tests branch November 20, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codercops-accepted Merged PR that counts on the monthly leaderboard (oss.codercops.com)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add route tests for /api/ai/autofill

2 participants