|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { handler, OpenAiProxyParameters } from './openai-proxy'; |
| 3 | + |
| 4 | +type HandlerEvent = Parameters<typeof handler>[0]; |
| 5 | +type HandlerContext = Parameters<typeof handler>[1]; |
| 6 | + |
| 7 | +// The proxy only reads `key` off the installation params and `messages`/`model` |
| 8 | +// off the action body — build the smallest shapes the handler actually touches. |
| 9 | +const makeEvent = (body: OpenAiProxyParameters): HandlerEvent => |
| 10 | + ({ body } as unknown as HandlerEvent); |
| 11 | + |
| 12 | +const makeContext = (key?: string): HandlerContext => |
| 13 | + ({ appInstallationParameters: key ? { key } : {} } as unknown as HandlerContext); |
| 14 | + |
| 15 | +const validBody: OpenAiProxyParameters = { |
| 16 | + messages: JSON.stringify([{ role: 'user', content: 'hi' }]), |
| 17 | + model: 'gpt-4', |
| 18 | +}; |
| 19 | + |
| 20 | +describe('openai-proxy handler', () => { |
| 21 | + beforeEach(() => { |
| 22 | + vi.stubGlobal('fetch', vi.fn()); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + vi.unstubAllGlobals(); |
| 27 | + vi.restoreAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('throws when no API key is configured', async () => { |
| 31 | + await expect(handler(makeEvent(validBody), makeContext())).rejects.toThrow( |
| 32 | + 'OpenAI API key is not configured' |
| 33 | + ); |
| 34 | + expect(fetch).not.toHaveBeenCalled(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('throws when messages is not valid JSON', async () => { |
| 38 | + const event = makeEvent({ messages: 'not json', model: 'gpt-4' }); |
| 39 | + |
| 40 | + await expect(handler(event, makeContext('sk-test'))).rejects.toThrow( |
| 41 | + 'Invalid messages parameter: must be a JSON-encoded array' |
| 42 | + ); |
| 43 | + expect(fetch).not.toHaveBeenCalled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('calls OpenAI with the key and parsed messages, returning the completion text', async () => { |
| 47 | + vi.mocked(fetch).mockResolvedValue({ |
| 48 | + ok: true, |
| 49 | + json: async () => ({ choices: [{ message: { content: 'generated' } }] }), |
| 50 | + } as Response); |
| 51 | + |
| 52 | + const result = await handler(makeEvent(validBody), makeContext('sk-test')); |
| 53 | + |
| 54 | + expect(result).toEqual({ text: 'generated' }); |
| 55 | + expect(fetch).toHaveBeenCalledWith( |
| 56 | + 'https://api.openai.com/v1/chat/completions', |
| 57 | + expect.objectContaining({ |
| 58 | + method: 'POST', |
| 59 | + headers: expect.objectContaining({ Authorization: 'Bearer sk-test' }), |
| 60 | + body: JSON.stringify({ |
| 61 | + model: 'gpt-4', |
| 62 | + messages: [{ role: 'user', content: 'hi' }], |
| 63 | + }), |
| 64 | + }) |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns empty text when the completion has no content', async () => { |
| 69 | + vi.mocked(fetch).mockResolvedValue({ |
| 70 | + ok: true, |
| 71 | + json: async () => ({ choices: [] }), |
| 72 | + } as Response); |
| 73 | + |
| 74 | + const result = await handler(makeEvent(validBody), makeContext('sk-test')); |
| 75 | + |
| 76 | + expect(result).toEqual({ text: '' }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('surfaces the OpenAI error message on a non-ok response', async () => { |
| 80 | + vi.mocked(fetch).mockResolvedValue({ |
| 81 | + ok: false, |
| 82 | + status: 401, |
| 83 | + statusText: 'Unauthorized', |
| 84 | + json: async () => ({ error: { message: 'Invalid API key' } }), |
| 85 | + } as Response); |
| 86 | + |
| 87 | + await expect(handler(makeEvent(validBody), makeContext('sk-test'))).rejects.toThrow( |
| 88 | + 'OpenAI request failed: 401 Invalid API key' |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('falls back to statusText when the error body has no message', async () => { |
| 93 | + vi.mocked(fetch).mockResolvedValue({ |
| 94 | + ok: false, |
| 95 | + status: 500, |
| 96 | + statusText: 'Internal Server Error', |
| 97 | + json: async () => { |
| 98 | + throw new Error('not json'); |
| 99 | + }, |
| 100 | + } as unknown as Response); |
| 101 | + |
| 102 | + await expect(handler(makeEvent(validBody), makeContext('sk-test'))).rejects.toThrow( |
| 103 | + 'OpenAI request failed: 500 Internal Server Error' |
| 104 | + ); |
| 105 | + }); |
| 106 | +}); |
0 commit comments