Skip to content

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

Open
Seyamalam wants to merge 1 commit into
codercops:devfrom
Seyamalam:test/ai-autofill-route-tests
Open

Seyamalam wants to merge 1 commit 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>

This branch has not been deployed

No deployments
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.

Add route tests for /api/ai/autofill

1 participant