|
| 1 | +import '@testing-library/jest-dom/vitest'; |
| 2 | +import { cleanup, findByText, render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { formatStreamPart } from '../streams'; |
| 5 | +import { |
| 6 | + mockFetchDataStream, |
| 7 | + mockFetchDataStreamWithGenerator, |
| 8 | +} from '../tests/utils/mock-fetch'; |
| 9 | +import { useAssistant } from './use-assistant'; |
| 10 | + |
| 11 | +describe('stream data stream', () => { |
| 12 | + const TestComponent = () => { |
| 13 | + const { status, messages, append } = useAssistant({ |
| 14 | + api: '/api/assistant', |
| 15 | + }); |
| 16 | + |
| 17 | + return ( |
| 18 | + <div> |
| 19 | + <div data-testid="status">{status}</div> |
| 20 | + {messages.map((m, idx) => ( |
| 21 | + <div data-testid={`message-${idx}`} key={m.id}> |
| 22 | + {m.role === 'user' ? 'User: ' : 'AI: '} |
| 23 | + {m.content} |
| 24 | + </div> |
| 25 | + ))} |
| 26 | + |
| 27 | + <button |
| 28 | + data-testid="do-append" |
| 29 | + onClick={() => { |
| 30 | + append({ role: 'user', content: 'hi' }); |
| 31 | + }} |
| 32 | + /> |
| 33 | + </div> |
| 34 | + ); |
| 35 | + }; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + render(<TestComponent />); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + vi.restoreAllMocks(); |
| 43 | + cleanup(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should show streamed response', async () => { |
| 47 | + const { requestBody } = mockFetchDataStream({ |
| 48 | + url: 'https://example.com/api/assistant', |
| 49 | + chunks: [ |
| 50 | + formatStreamPart('assistant_control_data', { |
| 51 | + threadId: 't0', |
| 52 | + messageId: 'm0', |
| 53 | + }), |
| 54 | + formatStreamPart('assistant_message', { |
| 55 | + id: 'm0', |
| 56 | + role: 'assistant', |
| 57 | + content: [{ type: 'text', text: { value: '' } }], |
| 58 | + }), |
| 59 | + // text parts: |
| 60 | + '0:"Hello"\n', |
| 61 | + '0:","\n', |
| 62 | + '0:" world"\n', |
| 63 | + '0:"."\n', |
| 64 | + ], |
| 65 | + }); |
| 66 | + |
| 67 | + await userEvent.click(screen.getByTestId('do-append')); |
| 68 | + |
| 69 | + await screen.findByTestId('message-0'); |
| 70 | + expect(screen.getByTestId('message-0')).toHaveTextContent('User: hi'); |
| 71 | + |
| 72 | + await screen.findByTestId('message-1'); |
| 73 | + expect(screen.getByTestId('message-1')).toHaveTextContent( |
| 74 | + 'AI: Hello, world.', |
| 75 | + ); |
| 76 | + |
| 77 | + // check that correct information was sent to the server: |
| 78 | + expect(await requestBody).toStrictEqual( |
| 79 | + JSON.stringify({ |
| 80 | + threadId: null, |
| 81 | + message: 'hi', |
| 82 | + }), |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('loading state', () => { |
| 87 | + it('should show loading state', async () => { |
| 88 | + let finishGeneration: ((value?: unknown) => void) | undefined; |
| 89 | + const finishGenerationPromise = new Promise(resolve => { |
| 90 | + finishGeneration = resolve; |
| 91 | + }); |
| 92 | + |
| 93 | + mockFetchDataStreamWithGenerator({ |
| 94 | + url: 'https://example.com/api/chat', |
| 95 | + chunkGenerator: (async function* generate() { |
| 96 | + const encoder = new TextEncoder(); |
| 97 | + |
| 98 | + yield encoder.encode( |
| 99 | + formatStreamPart('assistant_control_data', { |
| 100 | + threadId: 't0', |
| 101 | + messageId: 'm1', |
| 102 | + }), |
| 103 | + ); |
| 104 | + |
| 105 | + yield encoder.encode( |
| 106 | + formatStreamPart('assistant_message', { |
| 107 | + id: 'm1', |
| 108 | + role: 'assistant', |
| 109 | + content: [{ type: 'text', text: { value: '' } }], |
| 110 | + }), |
| 111 | + ); |
| 112 | + |
| 113 | + yield encoder.encode('0:"Hello"\n'); |
| 114 | + |
| 115 | + await finishGenerationPromise; |
| 116 | + })(), |
| 117 | + }); |
| 118 | + |
| 119 | + await userEvent.click(screen.getByTestId('do-append')); |
| 120 | + |
| 121 | + await screen.findByTestId('status'); |
| 122 | + expect(screen.getByTestId('status')).toHaveTextContent('in_progress'); |
| 123 | + |
| 124 | + finishGeneration?.(); |
| 125 | + |
| 126 | + await findByText(await screen.findByTestId('status'), 'awaiting_message'); |
| 127 | + expect(screen.getByTestId('status')).toHaveTextContent( |
| 128 | + 'awaiting_message', |
| 129 | + ); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments