Skip to content

Add route tests for /api/ai/autofill #419

Open

Description

@anurag629

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. Call POST({ request } as any) with a new Request(...), the way tests/api/og.test.ts calls GET.
  • Mock @/lib/meta-fetcher with vi.mock so no page is fetched, and stub the provider call with vi.stubGlobal('fetch', ...). Use openai for most cases (the reply text goes in choices[0].message.content) and google for 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: fetch is 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 test passes

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

  1. Seyamalam commented on Nov 18, 2026

    @Seyamalam
    Contributor

    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, calling POST with a new Request(...) like tests/api/og.test.ts, mocking @/lib/meta-fetcher and stubbing fetch for 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    aiAI-powered featuresgood first issueGood for newcomerstestingTests and quality assurance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions