Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use-assistant: fix missing message content. (#1425)
- Loading branch information
Showing
5 changed files
with
170 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
use-assistant: fix missing message content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import '@testing-library/jest-dom/vitest'; | ||
import { cleanup, findByText, render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { formatStreamPart } from '../streams'; | ||
import { | ||
mockFetchDataStream, | ||
mockFetchDataStreamWithGenerator, | ||
} from '../tests/utils/mock-fetch'; | ||
import { useAssistant } from './use-assistant'; | ||
|
||
describe('stream data stream', () => { | ||
const TestComponent = () => { | ||
const { status, messages, append } = useAssistant({ | ||
api: '/api/assistant', | ||
}); | ||
|
||
return ( | ||
<div> | ||
<div data-testid="status">{status}</div> | ||
{messages.map((m, idx) => ( | ||
<div data-testid={`message-${idx}`} key={m.id}> | ||
{m.role === 'user' ? 'User: ' : 'AI: '} | ||
{m.content} | ||
</div> | ||
))} | ||
|
||
<button | ||
data-testid="do-append" | ||
onClick={() => { | ||
append({ role: 'user', content: 'hi' }); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
beforeEach(() => { | ||
render(<TestComponent />); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
cleanup(); | ||
}); | ||
|
||
it('should show streamed response', async () => { | ||
const { requestBody } = mockFetchDataStream({ | ||
url: 'https://example.com/api/assistant', | ||
chunks: [ | ||
formatStreamPart('assistant_control_data', { | ||
threadId: 't0', | ||
messageId: 'm0', | ||
}), | ||
formatStreamPart('assistant_message', { | ||
id: 'm0', | ||
role: 'assistant', | ||
content: [{ type: 'text', text: { value: '' } }], | ||
}), | ||
// text parts: | ||
'0:"Hello"\n', | ||
'0:","\n', | ||
'0:" world"\n', | ||
'0:"."\n', | ||
], | ||
}); | ||
|
||
await userEvent.click(screen.getByTestId('do-append')); | ||
|
||
await screen.findByTestId('message-0'); | ||
expect(screen.getByTestId('message-0')).toHaveTextContent('User: hi'); | ||
|
||
await screen.findByTestId('message-1'); | ||
expect(screen.getByTestId('message-1')).toHaveTextContent( | ||
'AI: Hello, world.', | ||
); | ||
|
||
// check that correct information was sent to the server: | ||
expect(await requestBody).toStrictEqual( | ||
JSON.stringify({ | ||
threadId: null, | ||
message: 'hi', | ||
}), | ||
); | ||
}); | ||
|
||
describe('loading state', () => { | ||
it('should show loading state', async () => { | ||
let finishGeneration: ((value?: unknown) => void) | undefined; | ||
const finishGenerationPromise = new Promise(resolve => { | ||
finishGeneration = resolve; | ||
}); | ||
|
||
mockFetchDataStreamWithGenerator({ | ||
url: 'https://example.com/api/chat', | ||
chunkGenerator: (async function* generate() { | ||
const encoder = new TextEncoder(); | ||
|
||
yield encoder.encode( | ||
formatStreamPart('assistant_control_data', { | ||
threadId: 't0', | ||
messageId: 'm1', | ||
}), | ||
); | ||
|
||
yield encoder.encode( | ||
formatStreamPart('assistant_message', { | ||
id: 'm1', | ||
role: 'assistant', | ||
content: [{ type: 'text', text: { value: '' } }], | ||
}), | ||
); | ||
|
||
yield encoder.encode('0:"Hello"\n'); | ||
|
||
await finishGenerationPromise; | ||
})(), | ||
}); | ||
|
||
await userEvent.click(screen.getByTestId('do-append')); | ||
|
||
await screen.findByTestId('status'); | ||
expect(screen.getByTestId('status')).toHaveTextContent('in_progress'); | ||
|
||
finishGeneration?.(); | ||
|
||
await findByText(await screen.findByTestId('status'), 'awaiting_message'); | ||
expect(screen.getByTestId('status')).toHaveTextContent( | ||
'awaiting_message', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters