Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions apps/web/src/__tests__/qa-chat-hook.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,156 @@ describe('useQAChat', () => {
expect(result.current.messages).toHaveLength(2);
expect(result.current.messages[1].content).toContain('something went wrong');
});

it('cancels the in-flight request when a new question is sent', async () => {
const signals: AbortSignal[] = [];
let call = 0;

mockFetch.mockImplementation((_input: unknown, init?: RequestInit) => {
call += 1;
const signal = init?.signal as AbortSignal;
signals.push(signal);

// The first question never gets an answer: it is still in flight when
// the second one is sent.
if (call === 1) {
return new Promise<Response>((_resolve, reject) => {
signal.addEventListener('abort', () => {
const error = new Error('The operation was aborted');
error.name = 'AbortError';
reject(error);
});
});
}

return Promise.resolve(
createMockSSEResponse([
'data: {"type":"token","text":"Second answer"}',
'data: {"type":"metadata","confidence":"HIGH","sources":[],"latencyMs":200}',
'data: [DONE]',
]),
);
});

const { result } = renderHook(() => useQAChat());

await act(async () => {
void result.current.sendMessage('First question');
});

await waitFor(() => {
expect(signals).toHaveLength(1);
});

await act(async () => {
void result.current.sendMessage('Second question');
});

await waitFor(() => {
expect(signals).toHaveLength(2);
});

// The first request is cancelled, not merely overwritten.
expect(signals[0].aborted).toBe(true);
expect(signals[1].aborted).toBe(false);

// The cancelled turn leaves the transcript: only the answered one is shown.
await waitFor(() => {
expect(result.current.messages).toHaveLength(2);
expect(result.current.messages[1].content).toBe('Second answer');
});
expect(result.current.messages[0].content).toBe('Second question');

// The cancelled question is not offered to the model as history.
const secondCallBody = JSON.parse(mockFetch.mock.calls[1][1].body);
expect(secondCallBody.conversationHistory).toEqual([]);
});

it('drops the superseded turn even when the cancelled request never settles', async () => {
const signals: AbortSignal[] = [];

// A fetch that ignores the abort signal: the first request never
// settles, so nothing cleans up after it.
mockFetch.mockImplementationOnce((_input: unknown, init?: RequestInit) => {
signals.push(init?.signal as AbortSignal);
return new Promise<Response>(() => {});
});
mockFetch.mockImplementationOnce((_input: unknown, init?: RequestInit) => {
signals.push(init?.signal as AbortSignal);
return Promise.resolve(
createMockSSEResponse([
'data: {"type":"token","text":"Second answer"}',
'data: {"type":"metadata","confidence":"HIGH","sources":[],"latencyMs":200}',
'data: [DONE]',
]),
);
});

const { result } = renderHook(() => useQAChat());

await act(async () => {
void result.current.sendMessage('First question');
});

await waitFor(() => {
expect(signals).toHaveLength(1);
});

await act(async () => {
void result.current.sendMessage('Second question');
});

await waitFor(() => {
expect(result.current.messages).toHaveLength(2);
expect(result.current.messages[1].content).toBe('Second answer');
});

// The cancelled question leaves the transcript: no question without an
// answer, and no bubble that streams forever.
expect(signals[0].aborted).toBe(true);
expect(result.current.messages[0].content).toBe('Second question');
});

it('does not replay the error placeholder as conversation history', async () => {
mockFetch.mockResolvedValueOnce(
new Response(JSON.stringify({ error: 'Server error' }), {
status: 500,
}),
);
mockFetch.mockResolvedValueOnce(
createMockSSEResponse([
'data: {"type":"token","text":"Second answer"}',
'data: {"type":"metadata","confidence":"HIGH","sources":[],"latencyMs":200}',
'data: [DONE]',
]),
);

const { result } = renderHook(() => useQAChat());

await act(async () => {
await result.current.sendMessage('Bad request');
});

await waitFor(() => {
expect(result.current.loading).toBe(false);
});

// The failure is visible in the transcript.
expect(result.current.messages).toHaveLength(2);
expect(result.current.messages[1].content).toContain('something went wrong');

await act(async () => {
await result.current.sendMessage('Second question');
});

await waitFor(() => {
expect(result.current.messages).toHaveLength(4);
});

// The placeholder is not something the model said, so it is not history.
const secondCallBody = JSON.parse(mockFetch.mock.calls[1][1].body);
expect(secondCallBody.conversationHistory).toEqual([]);
// And it never reaches the model, whatever shape the history takes.
expect(mockFetch.mock.calls[1][1].body).not.toContain('something went wrong');
});
});
Loading