Repository navigation
Description
tests/api/ calls the real handlers for /api/og and the thumbnail endpoint, but the /api/ai routes only have schema tests (ai-generate.test.ts, ai-validate.test.ts), and /api/ai/autofill has nothing at all. #30 planned a tests/api/ai-autofill.test.ts that never landed. Autofill has the most branches of the three, since it chains a page fetch, an AI provider call and a JSON parse.
The branches in src/pages/api/ai/autofill.ts:
| Case | Lines | Response |
|---|---|---|
| Body isn't JSON | 23 to 31 | 400 Invalid JSON body |
A field is missing or wrong (unknown provider, empty apiKey, url not a URL) |
33 to 40 | 400 naming the field, like url: A valid URL is required |
fetchMetaTags() throws |
62 to 75 | 502 Could not fetch the URL, and the provider is never called |
| Provider answers 401 or 403 | 97 to 103 | 401 Invalid API key |
| Provider answers another error, like 500 | 104 to 107 | 502 AI provider error (500) |
| Provider reply has no usable JSON | 114 to 120 | 502 Could not parse AI response |
| The AI picks a template id that doesn't exist | 122 to 127 | 200 with templateId replaced by blog-minimal-dark |
| Everything works | 129 to 132 | 200 with templateId, fields and colors |
Provider is google |
87 | The key goes in the request URL as ?key=, not in a header |
What to do
- Create
tests/api/ai-autofill.test.ts. CallPOST({ request } as any)with anew Request(...), the waytests/api/og.test.tscallsGET. - Mock
@/lib/meta-fetcherwithvi.mockso no page is fetched, and stub the provider call withvi.stubGlobal('fetch', ...). Useopenaifor most cases (the reply text goes inchoices[0].message.content) andgooglefor the last row. - Tests only. If a test turns up a bug, comment here or open a new issue instead of changing the route in this PR.
Acceptance criteria
- One test per row above
- No real network requests:
fetchis stubbed in every test and restored afterwards - Tests check the status code and the parts of the body that matter, not a full snapshot
-
npm run testpasses
How to test
npx vitest run tests/api/ai-autofill.test.ts. For one or two rows, break the matching line in the route on purpose and check that its test fails, then put it back.
Want to work on this? Comment below and I'll assign it to you. Please take one issue at a time, branch off dev, and open your PR against dev with Closes #419 in the description.
Activity
Seyamalam commentedon Nov 18, 2026
Note
🤖 SWE-2 High (Devin) responding on behalf of Seyamalam
I'd like to take this one next (my #425 for #420 was just merged). Plan: create
tests/api/ai-autofill.test.ts, callingPOSTwith anew Request(...)liketests/api/og.test.ts, mocking@/lib/meta-fetcherand stubbingfetchfor the provider call — one test per branch in your table (bad JSON, schema 400s, fetch failure 502, 401/403 → 401, other provider errors → 502, unparseable reply → 502, unknown templateId →blog-minimal-dark, success, and the google?key=path). Could you assign it to me?