From f586b93de0085388cdc0e701d6850b5847fbddd3 Mon Sep 17 00:00:00 2001 From: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:21:50 +0000 Subject: [PATCH 1/2] test(api): add route tests for /api/ai/autofill 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> --- tests/api/ai-autofill.test.ts | 165 ++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 tests/api/ai-autofill.test.ts diff --git a/tests/api/ai-autofill.test.ts b/tests/api/ai-autofill.test.ts new file mode 100644 index 0000000..18cfbeb --- /dev/null +++ b/tests/api/ai-autofill.test.ts @@ -0,0 +1,165 @@ +import { afterEach, beforeEach, describe, it, expect, vi } from 'vitest'; +import type { MetaTags } from '@/lib/meta-fetcher'; + +vi.mock('@/lib/meta-fetcher', () => ({ + fetchMetaTags: vi.fn(), +})); + +const { POST } = await import('@/pages/api/ai/autofill'); +const { fetchMetaTags } = await import('@/lib/meta-fetcher'); + +const VALID = { + provider: 'openai', + apiKey: 'sk-test', + model: 'gpt-4o-mini', + url: 'https://example.com', +}; + +const META: MetaTags = { + title: 'Example', + ogTitle: 'Example', + description: 'An example page', + ogDescription: 'An example page', + themeColor: '#123456', +} as MetaTags; + +const AI_JSON = JSON.stringify({ + templateId: 'blog-minimal-dark', + fields: { title: 'Example', description: 'An example page' }, + colors: { bgColor: '#123456', accentColor: '#ffffff' }, +}); + +function post(body: unknown, raw?: string) { + const request = new Request('http://localhost:4321/api/ai/autofill', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: raw ?? JSON.stringify(body), + }); + return POST({ request } as Parameters>[0]); +} + +function openAiReply(content: string, status = 200) { + return new Response( + JSON.stringify({ choices: [{ message: { content } }] }), + { status } + ); +} + +describe('POST /api/ai/autofill', () => { + beforeEach(() => { + vi.mocked(fetchMetaTags).mockResolvedValue(META); + vi.stubGlobal('fetch', vi.fn()); + }); + + afterEach(() => { + vi.unstubAllGlobals(); + vi.resetAllMocks(); + }); + + it('returns 400 when the body is not JSON', async () => { + const res = await post(null, 'this is not json {'); + + expect(res.status).toBe(400); + expect(await res.json()).toEqual({ error: 'Invalid JSON body' }); + }); + + it.each([ + [{ ...VALID, provider: 'not-a-provider' }, 'provider'], + [{ ...VALID, apiKey: '' }, 'apiKey'], + [{ ...VALID, url: 'not-a-url' }, 'url'], + [{ provider: 'openai', apiKey: 'sk-test', model: 'gpt-4o-mini' }, 'url'], + [{ url: 'https://example.com' }, 'provider'], + ])('returns 400 naming the bad field (%j)', async (body, field) => { + const res = await post(body); + + expect(res.status).toBe(400); + const json = await res.json(); + expect(json.error).toContain(`${field}:`); + expect(fetchMetaTags).not.toHaveBeenCalled(); + }); + + it('returns 502 and never calls the provider when the page fetch fails', async () => { + vi.mocked(fetchMetaTags).mockRejectedValue(new Error('network down')); + const fetchSpy = vi.mocked(fetch); + + const res = await post(VALID); + + expect(res.status).toBe(502); + expect(await res.json()).toEqual({ error: 'Could not fetch the URL' }); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it.each([401, 403])('maps a provider %i answer to 401 Invalid API key', async (status) => { + vi.mocked(fetch).mockResolvedValue(new Response('denied', { status })); + + const res = await post(VALID); + + expect(res.status).toBe(401); + expect(await res.json()).toEqual({ error: 'Invalid API key' }); + }); + + it('maps another provider error to 502 with the status', async () => { + vi.mocked(fetch).mockResolvedValue(new Response('oops', { status: 500 })); + + const res = await post(VALID); + + expect(res.status).toBe(502); + expect(await res.json()).toEqual({ error: 'AI provider error (500)' }); + }); + + it('returns 502 when the provider reply has no usable JSON', async () => { + vi.mocked(fetch).mockResolvedValue(openAiReply('sorry, I cannot help with that')); + + const res = await post(VALID); + + expect(res.status).toBe(502); + expect(await res.json()).toEqual({ error: 'Could not parse AI response' }); + }); + + it('falls back to blog-minimal-dark when the AI picks an unknown template', async () => { + vi.mocked(fetch).mockResolvedValue( + openAiReply( + JSON.stringify({ templateId: 'does-not-exist', fields: { title: 'Example' } }) + ) + ); + + const res = await post(VALID); + + expect(res.status).toBe(200); + const json = await res.json(); + expect(json.templateId).toBe('blog-minimal-dark'); + expect(json.fields).toEqual({ title: 'Example' }); + }); + + it('returns 200 with templateId, fields and colors when everything works', async () => { + vi.mocked(fetch).mockResolvedValue(openAiReply(AI_JSON)); + + const res = await post(VALID); + + expect(res.status).toBe(200); + const json = await res.json(); + expect(json.templateId).toBe('blog-minimal-dark'); + expect(json.fields).toEqual({ title: 'Example', description: 'An example page' }); + expect(json.colors).toEqual({ bgColor: '#123456', accentColor: '#ffffff' }); + expect(fetchMetaTags).toHaveBeenCalledWith('https://example.com'); + }); + + it('sends the api key in the request URL for google', async () => { + const fetchSpy = vi.mocked(fetch).mockResolvedValue( + new Response( + JSON.stringify({ + candidates: [{ content: { parts: [{ text: AI_JSON }] } }], + modelVersion: 'gemini-2.0-flash', + }), + { status: 200 } + ) + ); + + const res = await post({ ...VALID, provider: 'google', model: 'gemini-2.0-flash' }); + + expect(res.status).toBe(200); + const [calledUrl] = fetchSpy.mock.calls[0] as [string]; + expect(calledUrl).toContain('?key=sk-test'); + expect(calledUrl).toContain('gemini-2.0-flash:generateContent'); + }); +}); From 20903ccd5da007cd7f48a1db72c4e4e13fa43c3e Mon Sep 17 00:00:00 2001 From: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:29:22 +0000 Subject: [PATCH 2/2] test(api): disambiguate autofill success from the templateId fallback 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> --- tests/api/ai-autofill.test.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/api/ai-autofill.test.ts b/tests/api/ai-autofill.test.ts index 18cfbeb..ea8466f 100644 --- a/tests/api/ai-autofill.test.ts +++ b/tests/api/ai-autofill.test.ts @@ -23,8 +23,10 @@ const META: MetaTags = { themeColor: '#123456', } as MetaTags; +// Picks a real template that is not the fallback id, so the success test can +// tell the AI's choice apart from the 'blog-minimal-dark' rewrite below. const AI_JSON = JSON.stringify({ - templateId: 'blog-minimal-dark', + templateId: 'product-minimal-badge', fields: { title: 'Example', description: 'An example page' }, colors: { bgColor: '#123456', accentColor: '#ffffff' }, }); @@ -138,7 +140,7 @@ describe('POST /api/ai/autofill', () => { expect(res.status).toBe(200); const json = await res.json(); - expect(json.templateId).toBe('blog-minimal-dark'); + expect(json.templateId).toBe('product-minimal-badge'); expect(json.fields).toEqual({ title: 'Example', description: 'An example page' }); expect(json.colors).toEqual({ bgColor: '#123456', accentColor: '#ffffff' }); expect(fetchMetaTags).toHaveBeenCalledWith('https://example.com'); @@ -158,8 +160,9 @@ describe('POST /api/ai/autofill', () => { const res = await post({ ...VALID, provider: 'google', model: 'gemini-2.0-flash' }); expect(res.status).toBe(200); - const [calledUrl] = fetchSpy.mock.calls[0] as [string]; + const [calledUrl, init] = fetchSpy.mock.calls[0] as [string, RequestInit]; expect(calledUrl).toContain('?key=sk-test'); expect(calledUrl).toContain('gemini-2.0-flash:generateContent'); + expect(JSON.stringify(init.headers)).not.toContain('sk-test'); }); });