|
| 1 | +/* eslint-disable @typescript-eslint/unbound-method */ |
| 2 | +import { UIMessage } from 'ai' |
| 3 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +import { AiHandler, StreamChatChunk, StreamChatEnd } from './types' |
| 6 | + |
| 7 | +vi.mock('electron', () => ({ |
| 8 | + ipcMain: { |
| 9 | + on: vi.fn(), |
| 10 | + }, |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('./model', () => ({ |
| 14 | + getOpenAiModel: vi.fn(), |
| 15 | + getGrafanaAssistantModel: vi.fn(), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('./a2a/assistantAuth', () => ({ |
| 19 | + initialize: vi.fn(), |
| 20 | +})) |
| 21 | + |
| 22 | +function createMockWebContents() { |
| 23 | + return { send: vi.fn() } as unknown as Electron.WebContents |
| 24 | +} |
| 25 | + |
| 26 | +function createUserMessage(text: string): UIMessage { |
| 27 | + return { |
| 28 | + id: 'msg-1', |
| 29 | + role: 'user', |
| 30 | + parts: [{ type: 'text', text }], |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function getStreamChatHandler() { |
| 35 | + const { ipcMain } = await import('electron') |
| 36 | + const { initialize } = await import('./index') |
| 37 | + initialize() |
| 38 | + |
| 39 | + const calls = vi.mocked(ipcMain.on).mock.calls |
| 40 | + const entry = calls.find( |
| 41 | + ([channel]) => channel === (AiHandler.StreamChat as string) |
| 42 | + ) |
| 43 | + return entry![1] as (...args: unknown[]) => Promise<void> |
| 44 | +} |
| 45 | + |
| 46 | +function getSentChunks(webContents: Electron.WebContents) { |
| 47 | + return vi |
| 48 | + .mocked(webContents.send) |
| 49 | + .mock.calls.filter( |
| 50 | + (call): call is [string, StreamChatChunk] => |
| 51 | + call[0] === (AiHandler.StreamChatChunk as string) |
| 52 | + ) |
| 53 | + .map(([, data]) => data) |
| 54 | +} |
| 55 | + |
| 56 | +function getSentEnds(webContents: Electron.WebContents) { |
| 57 | + return vi |
| 58 | + .mocked(webContents.send) |
| 59 | + .mock.calls.filter( |
| 60 | + (call): call is [string, StreamChatEnd] => |
| 61 | + call[0] === (AiHandler.StreamChatEnd as string) |
| 62 | + ) |
| 63 | + .map(([, data]) => data) |
| 64 | +} |
| 65 | + |
| 66 | +describe('handleStreamChat error handling', () => { |
| 67 | + afterEach(() => { |
| 68 | + vi.restoreAllMocks() |
| 69 | + vi.resetModules() |
| 70 | + }) |
| 71 | + |
| 72 | + it('forwards the error chunk and sends StreamChatEnd when the model throws', async () => { |
| 73 | + const { getOpenAiModel } = await import('./model') |
| 74 | + vi.mocked(getOpenAiModel).mockResolvedValue({ |
| 75 | + specificationVersion: 'v2', |
| 76 | + provider: 'test', |
| 77 | + modelId: 'test', |
| 78 | + supportedUrls: {}, |
| 79 | + // eslint-disable-next-line @typescript-eslint/require-await |
| 80 | + doStream: async () => { |
| 81 | + throw new Error('insufficient_quota: You exceeded your current quota') |
| 82 | + }, |
| 83 | + // eslint-disable-next-line @typescript-eslint/require-await |
| 84 | + doGenerate: async () => { |
| 85 | + throw new Error('not implemented') |
| 86 | + }, |
| 87 | + }) |
| 88 | + |
| 89 | + const handler = await getStreamChatHandler() |
| 90 | + const webContents = createMockWebContents() |
| 91 | + |
| 92 | + await handler( |
| 93 | + { sender: webContents }, |
| 94 | + { |
| 95 | + id: 'req-1', |
| 96 | + trigger: 'submit-message', |
| 97 | + messages: [createUserMessage('Hello')], |
| 98 | + } |
| 99 | + ) |
| 100 | + |
| 101 | + // The AI SDK's toUIMessageStream emits an error chunk with the real error |
| 102 | + const chunks = getSentChunks(webContents) |
| 103 | + const errorChunk = chunks.find((data) => data.chunk?.type === 'error') |
| 104 | + expect(errorChunk).toBeDefined() |
| 105 | + expect(errorChunk?.chunk).toHaveProperty('errorText') |
| 106 | + |
| 107 | + // StreamChatEnd must always be sent so the renderer doesn't hang |
| 108 | + const ends = getSentEnds(webContents) |
| 109 | + expect(ends).toHaveLength(1) |
| 110 | + expect(ends[0]?.id).toBe('req-1') |
| 111 | + }) |
| 112 | + |
| 113 | + it('sends an error chunk and StreamChatEnd when message conversion fails', async () => { |
| 114 | + const handler = await getStreamChatHandler() |
| 115 | + const webContents = createMockWebContents() |
| 116 | + |
| 117 | + await handler( |
| 118 | + { sender: webContents }, |
| 119 | + { |
| 120 | + id: 'req-2', |
| 121 | + trigger: 'submit-message', |
| 122 | + // Messages with null parts cause convertToModelMessages to throw |
| 123 | + messages: [ |
| 124 | + { role: 'user', id: 'x', parts: null } as unknown as UIMessage, |
| 125 | + ], |
| 126 | + } |
| 127 | + ) |
| 128 | + |
| 129 | + // Should send an error chunk from the catch block |
| 130 | + const chunks = getSentChunks(webContents) |
| 131 | + const errorChunk = chunks.find((data) => data.chunk?.type === 'error') |
| 132 | + expect(errorChunk).toBeDefined() |
| 133 | + |
| 134 | + // StreamChatEnd must always be sent |
| 135 | + const ends = getSentEnds(webContents) |
| 136 | + expect(ends).toHaveLength(1) |
| 137 | + expect(ends[0]?.id).toBe('req-2') |
| 138 | + }) |
| 139 | +}) |
0 commit comments