d3ro-voice/apps/desktop/tests/main/services/online-llm-service.test.ts

239 lines
7.4 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { D3ROError, ErrorCode } from '@d3ro/core/errors'
import {
configGet,
configSet,
initInMemoryConfig,
resetInMemoryConfig,
} from '../../../src/main/services/ConfigService'
import {
getOnlineLLMService,
resetOnlineLLMServiceForTests,
} from '../../../src/main/services/OnlineLLMService'
async function collectChatStream(
gen: AsyncGenerator<string, string>,
): Promise<{ tokens: string[]; result: string }> {
const tokens: string[] = []
let next = await gen.next()
while (!next.done) {
tokens.push(next.value)
next = await gen.next()
}
return { tokens, result: next.value }
}
function captureAbortedFetch(onStarted: () => void): typeof fetch {
return vi.fn((_input: string | URL | Request, init?: RequestInit) => {
const signal = init?.signal
return new Promise<Response>((_resolve, reject) => {
if (signal?.aborted) {
reject(new DOMException('The operation was aborted', 'AbortError'))
return
}
signal?.addEventListener(
'abort',
() => reject(new DOMException('The operation was aborted', 'AbortError')),
{ once: true },
)
onStarted()
})
}) as unknown as typeof fetch
}
describe('OnlineLLMService chatStream', () => {
beforeEach(() => {
initInMemoryConfig()
configSet('authToken', 'valid-jwt-token')
configSet('onlineApiUrl', 'http://127.0.0.1:5000')
resetOnlineLLMServiceForTests()
})
afterEach(() => {
resetOnlineLLMServiceForTests()
resetInMemoryConfig()
vi.unstubAllGlobals()
})
it('chatStream_cancelGenerationCalledDuringRequest_abortsFetchAndThrowsCancelled', async () => {
const service = getOnlineLLMService()
let startedResolve!: () => void
const started = new Promise<void>((resolve) => {
startedResolve = resolve
})
vi.stubGlobal('fetch', captureAbortedFetch(startedResolve))
const run = collectChatStream(service.chatStream([{ role: 'user', content: 'hello' }]))
await started
service.cancelGeneration()
await expect(run).rejects.toMatchObject({
name: 'D3ROError',
code: ErrorCode.LLMProcessingCancelled,
})
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response(JSON.stringify({ text: 'recovered' }), { status: 200 })),
)
const retry = await collectChatStream(service.chatStream([{ role: 'user', content: 'next' }]))
expect(retry.tokens).toEqual(['recovered'])
expect(retry.result).toBe('recovered')
})
it('chatStream_disposeCalledDuringRequest_abortsFetchAndBlocksFutureCalls', async () => {
const service = getOnlineLLMService()
let startedResolve!: () => void
const started = new Promise<void>((resolve) => {
startedResolve = resolve
})
vi.stubGlobal('fetch', captureAbortedFetch(startedResolve))
const run = collectChatStream(service.chatStream([{ role: 'user', content: 'hello' }]))
await started
service.dispose()
await expect(run).rejects.toMatchObject({
name: 'D3ROError',
code: ErrorCode.LLMProcessingCancelled,
})
await expect(
collectChatStream(service.chatStream([{ role: 'user', content: 'after-dispose' }])),
).rejects.toMatchObject({
name: 'D3ROError',
code: ErrorCode.LLMProcessingFailed,
})
})
it('chatStream_externalSignalAborted_abortsFetchAndThrowsCancelled', async () => {
const service = getOnlineLLMService()
let startedResolve!: () => void
const started = new Promise<void>((resolve) => {
startedResolve = resolve
})
vi.stubGlobal('fetch', captureAbortedFetch(startedResolve))
const external = new AbortController()
const run = collectChatStream(
service.chatStream([{ role: 'user', content: 'hello' }], { signal: external.signal }),
)
await started
external.abort()
await expect(run).rejects.toMatchObject({
name: 'D3ROError',
code: ErrorCode.LLMProcessingCancelled,
})
})
it('chatStream_serverReturns401_clearsAuthTokenAndThrowsUnreachable', async () => {
const service = getOnlineLLMService()
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response('Unauthorized', { status: 401 })),
)
await expect(
collectChatStream(service.chatStream([{ role: 'user', content: 'hello' }])),
).rejects.toMatchObject({
name: 'D3ROError',
code: ErrorCode.LLMServerUnreachable,
})
expect(configGet('authToken')).toBe(null)
})
it('chatStream_serverReturns500_throwsLLMProcessingFailed', async () => {
const service = getOnlineLLMService()
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response('Internal Error', { status: 500 })),
)
await expect(
collectChatStream(service.chatStream([{ role: 'user', content: 'hello' }])),
).rejects.toMatchObject({
name: 'D3ROError',
code: ErrorCode.LLMProcessingFailed,
})
expect(configGet('authToken')).toBe('valid-jwt-token')
})
it.each([
{ label: 'missing text field', body: { model: 'd3ro-gpt4o-mini' } },
{ label: 'whitespace-only text', body: { text: ' \n\t ' } },
{ label: 'null response json', body: null },
])('chatStream_emptyOrInvalidPayload_$label_throwsLLMProcessingFailed', async ({ body }) => {
const service = getOnlineLLMService()
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response(JSON.stringify(body), { status: 200 })),
)
let caught: unknown
try {
await collectChatStream(service.chatStream([{ role: 'user', content: 'hello' }]))
} catch (err) {
caught = err
}
expect(caught instanceof D3ROError).toBe(true)
expect((caught as D3ROError).code).toBe(ErrorCode.LLMProcessingFailed)
})
it('chatStream_fetchRejectsOrInvalidJson_wrapsInLLMProcessingFailed', async () => {
const service = getOnlineLLMService()
vi.stubGlobal(
'fetch',
vi.fn(async () => {
throw new Error('socket hang up')
}),
)
await expect(
collectChatStream(service.chatStream([{ role: 'user', content: 'hello' }])),
).rejects.toMatchObject({
name: 'D3ROError',
code: ErrorCode.LLMProcessingFailed,
})
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response('{not-json', { status: 200 })),
)
await expect(
collectChatStream(service.chatStream([{ role: 'user', content: 'hello' }])),
).rejects.toMatchObject({
name: 'D3ROError',
code: ErrorCode.LLMProcessingFailed,
})
})
it('chatStream_messageContainsPII_redactsBeforeFetchAndRehydratesOutput', async () => {
const service = getOnlineLLMService()
let sentBody = ''
vi.stubGlobal(
'fetch',
vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {
sentBody = String(init?.body ?? '')
const parsed = JSON.parse(sentBody) as { messages: Array<{ content: string }> }
const redactedUserMsg = parsed.messages[0].content
return new Response(
JSON.stringify({ text: `확인 완료: ${redactedUserMsg}` }),
{ status: 200 },
)
}),
)
const { tokens, result } = await collectChatStream(
service.chatStream([{ role: 'user', content: '연락처는 alice@example.com 입니다.' }]),
)
expect(sentBody.includes('alice@example.com')).toBe(false)
expect(tokens).toEqual(['확인 완료: 연락처는 alice@example.com 입니다.'])
expect(result).toBe('확인 완료: 연락처는 alice@example.com 입니다.')
})
})