|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +// Mock the auth helper so we control whether a user is present. |
| 4 | +const getUserFromRequest = vi.fn(); |
| 5 | +vi.mock('@/lib/auth-helpers', () => ({ |
| 6 | + getUserFromRequest: (request: Request) => getUserFromRequest(request), |
| 7 | +})); |
| 8 | + |
| 9 | +// Mock the model call so we never hit the real SDK. |
| 10 | +const completeJson = vi.fn(); |
| 11 | +vi.mock('@/lib/ai/client', () => ({ |
| 12 | + completeJson: (...args: unknown[]) => completeJson(...args), |
| 13 | +})); |
| 14 | + |
| 15 | +import { createAIHandler, validateShape } from '@/lib/ai/handlers'; |
| 16 | + |
| 17 | +interface SampleBody { |
| 18 | + text: string; |
| 19 | +} |
| 20 | +interface SampleResult { |
| 21 | + score: number; |
| 22 | +} |
| 23 | + |
| 24 | +function makeConfig() { |
| 25 | + return { |
| 26 | + systemPrompt: 'sys', |
| 27 | + buildUserPrompt: (body: SampleBody) => `user: ${body.text}`, |
| 28 | + validate: (body: unknown) => { |
| 29 | + const shape = validateShape(body, ['text']); |
| 30 | + return shape.ok |
| 31 | + ? ({ ok: true, value: body as SampleBody } as const) |
| 32 | + : ({ ok: false, status: 400, error: 'text required' } as const); |
| 33 | + }, |
| 34 | + onParseFailure: () => ({ ok: false, status: 500, error: 'parse failed' } as const), |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +function postReq(body: unknown) { |
| 39 | + return { |
| 40 | + json: async () => body, |
| 41 | + } as unknown as Request; |
| 42 | +} |
| 43 | + |
| 44 | +describe('createAIHandler', () => { |
| 45 | + beforeEach(() => { |
| 46 | + vi.clearAllMocks(); |
| 47 | + getUserFromRequest.mockResolvedValue({ id: 'u1' }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns 401 when no user', async () => { |
| 51 | + getUserFromRequest.mockResolvedValue(null); |
| 52 | + const handler = createAIHandler<SampleBody, SampleResult>(makeConfig()); |
| 53 | + const res = await handler(postReq({ text: 'hi' })); |
| 54 | + expect(res.status).toBe(401); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns 400 on invalid body', async () => { |
| 58 | + const handler = createAIHandler<SampleBody, SampleResult>(makeConfig()); |
| 59 | + const res = await handler(postReq({})); |
| 60 | + expect(res.status).toBe(400); |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns 200 with parsed result on success', async () => { |
| 64 | + completeJson.mockResolvedValue({ score: 9 }); |
| 65 | + const handler = createAIHandler<SampleBody, SampleResult>(makeConfig()); |
| 66 | + const res = await handler(postReq({ text: 'hi' })); |
| 67 | + expect(res.status).toBe(200); |
| 68 | + expect(await res.json()).toEqual({ score: 9 }); |
| 69 | + // Verify the model was called with the built prompt. |
| 70 | + expect(completeJson).toHaveBeenCalledWith('sys', 'user: hi', expect.anything()); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns configured error when model output cannot be parsed', async () => { |
| 74 | + completeJson.mockResolvedValue(null); |
| 75 | + const handler = createAIHandler<SampleBody, SampleResult>(makeConfig()); |
| 76 | + const res = await handler(postReq({ text: 'hi' })); |
| 77 | + expect(res.status).toBe(500); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns 500 when the model call throws', async () => { |
| 81 | + completeJson.mockRejectedValue(new Error('boom')); |
| 82 | + const handler = createAIHandler<SampleBody, SampleResult>(makeConfig()); |
| 83 | + const res = await handler(postReq({ text: 'hi' })); |
| 84 | + expect(res.status).toBe(500); |
| 85 | + }); |
| 86 | + |
| 87 | + it('returns 400 on malformed JSON body', async () => { |
| 88 | + const badReq = { |
| 89 | + json: async () => { |
| 90 | + throw new Error('bad json'); |
| 91 | + }, |
| 92 | + } as unknown as Request; |
| 93 | + const handler = createAIHandler<SampleBody, SampleResult>(makeConfig()); |
| 94 | + const res = await handler(badReq); |
| 95 | + expect(res.status).toBe(400); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('validateShape', () => { |
| 100 | + it('passes when all keys present', () => { |
| 101 | + expect(validateShape({ a: 1, b: 2 }, ['a', 'b']).ok).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it('reports missing keys', () => { |
| 105 | + const result = validateShape({ a: 1 }, ['a', 'b']); |
| 106 | + expect(result.ok).toBe(false); |
| 107 | + if (!result.ok) expect(result.missing).toEqual(['b']); |
| 108 | + }); |
| 109 | + |
| 110 | + it('fails on non-object', () => { |
| 111 | + expect(validateShape(null, ['a']).ok).toBe(false); |
| 112 | + expect(validateShape(42, ['a']).ok).toBe(false); |
| 113 | + }); |
| 114 | +}); |
0 commit comments