87 lines
2.6 KiB
TypeScript
87 lines
2.6 KiB
TypeScript
import {
|
|
AdminServiceError,
|
|
listAdminUsers,
|
|
} from '../src/features/admin/admin-service'
|
|
import {
|
|
ChatServiceError,
|
|
sendChatMessage,
|
|
} from '../src/features/chat/chat-service'
|
|
import { streamTalkResponse } from '../src/features/talk/llm-stream-service'
|
|
|
|
const originalFetch = global.fetch
|
|
|
|
afterEach(() => {
|
|
global.fetch = originalFetch
|
|
jest.restoreAllMocks()
|
|
jest.useRealTimers()
|
|
})
|
|
|
|
describe('abort and streaming lifecycle guards', () => {
|
|
test('sendChatMessage_이미중단된시그널전달시_fetch없이즉시CANCELLED거부', async () => {
|
|
const fetchMock = jest.fn()
|
|
global.fetch = fetchMock
|
|
const controller = new AbortController()
|
|
controller.abort()
|
|
|
|
await expect(
|
|
sendChatMessage([{ role: 'user', content: 'hello' }], {
|
|
accessToken: 'signed-access-token',
|
|
timeoutMs: 60_000,
|
|
signal: controller.signal,
|
|
}),
|
|
).rejects.toMatchObject({
|
|
name: 'ChatServiceError',
|
|
code: 'CANCELLED',
|
|
retryable: false,
|
|
})
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
expect(ChatServiceError.name).toBe('ChatServiceError')
|
|
})
|
|
|
|
test('adminService_이미중단된시그널전달시_비재시도네트워크오류로즉시거부', async () => {
|
|
const fetchMock = jest.fn()
|
|
global.fetch = fetchMock
|
|
const controller = new AbortController()
|
|
controller.abort()
|
|
|
|
await expect(
|
|
listAdminUsers({
|
|
accessToken: 'authenticated-admin-access-token',
|
|
signal: controller.signal,
|
|
}),
|
|
).rejects.toMatchObject({
|
|
code: 'network',
|
|
retryable: false,
|
|
})
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
expect(AdminServiceError.name).toBe('AdminServiceError')
|
|
})
|
|
|
|
test('streamTalkResponse_생성영수증ID누락시_응답스트림을취소하고실패종료', async () => {
|
|
const cancelMock = jest.fn(async () => undefined)
|
|
global.fetch = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
status: 200,
|
|
headers: {
|
|
get: (name: string) => (
|
|
name.toLowerCase() === 'content-type' ? 'text/event-stream' : null
|
|
),
|
|
},
|
|
body: {
|
|
cancel: cancelMock,
|
|
getReader: () => ({
|
|
read: async () => ({ done: true, value: undefined }),
|
|
releaseLock: jest.fn(),
|
|
}),
|
|
},
|
|
} as unknown as Response)
|
|
|
|
await expect(
|
|
streamTalkResponse([{ role: 'user', content: 'hello' }], {
|
|
accessToken: 'signed-talk-token',
|
|
onTextDelta: jest.fn(),
|
|
}),
|
|
).rejects.toMatchObject({ code: 'INVALID_RESPONSE', retryable: true })
|
|
expect(cancelMock).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|