fix: Fix tests on useChat

This commit is contained in:
Vanalite 2025-10-03 10:17:27 +07:00
parent 4ac45aba23
commit e7c9275488

View File

@ -333,7 +333,7 @@ describe('useChat', () => {
const { result } = renderHook(() => useChat()) const { result } = renderHook(() => useChat())
await act(async () => { await act(async () => {
await result.current('Hello world', true, undefined, undefined) await result.current('Hello world', true, undefined, undefined, undefined)
}) })
expect(completionLib.newUserThreadContent).toHaveBeenCalledWith( expect(completionLib.newUserThreadContent).toHaveBeenCalledWith(
@ -367,7 +367,7 @@ describe('useChat', () => {
const { result } = renderHook(() => useChat()) const { result } = renderHook(() => useChat())
await act(async () => { await act(async () => {
await result.current('Continue', true, undefined, 'msg-123') await result.current('', true, undefined, undefined, 'msg-123')
}) })
expect(completionLib.newUserThreadContent).not.toHaveBeenCalled() expect(completionLib.newUserThreadContent).not.toHaveBeenCalled()
@ -392,14 +392,18 @@ describe('useChat', () => {
const { result } = renderHook(() => useChat()) const { result } = renderHook(() => useChat())
await act(async () => { await act(async () => {
await result.current('', true, undefined, 'msg-123') await result.current('', true, undefined, undefined, 'msg-123')
}) })
expect(mockCompletionMessagesBuilder.addAssistantMessage).toHaveBeenCalledWith( // Should be called twice: once with partial message (line 517-521), once after completion (line 689)
const assistantCalls = mockCompletionMessagesBuilder.addAssistantMessage.mock.calls
expect(assistantCalls.length).toBeGreaterThanOrEqual(1)
// First call should be with the partial response content
expect(assistantCalls[0]).toEqual([
'Partial response', 'Partial response',
undefined, undefined,
[] []
) ])
}) })
it('should filter out stopped message from context when continuing', async () => { it('should filter out stopped message from context when continuing', async () => {
@ -421,15 +425,15 @@ describe('useChat', () => {
const { result } = renderHook(() => useChat()) const { result } = renderHook(() => useChat())
await act(async () => { await act(async () => {
await result.current('', true, undefined, 'msg-123') await result.current('', true, undefined, undefined, 'msg-123')
}) })
await waitFor(() => { // The CompletionMessagesBuilder is called with filtered messages (line 507-512)
expect(messagesLib.CompletionMessagesBuilder).toHaveBeenCalledWith( // The stopped message should be filtered out from the context
[userMsg], // stopped message filtered out expect(messagesLib.CompletionMessagesBuilder).toHaveBeenCalled()
'test instructions' const builderCall = (messagesLib.CompletionMessagesBuilder as any).mock.calls[0]
) expect(builderCall[0]).toEqual([userMsg]) // stopped message filtered out
}) expect(builderCall[1]).toEqual('test instructions')
}) })
it('should update existing message instead of adding new one when continuing', async () => { it('should update existing message instead of adding new one when continuing', async () => {
@ -446,17 +450,16 @@ describe('useChat', () => {
const { result } = renderHook(() => useChat()) const { result } = renderHook(() => useChat())
await act(async () => { await act(async () => {
await result.current('', true, undefined, 'msg-123') await result.current('', true, undefined, undefined, 'msg-123')
}) })
await waitFor(() => { // finalizeMessage is called at line 700-708, which should update the message
expect(mockUpdateMessage).toHaveBeenCalledWith( expect(mockUpdateMessage).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
id: 'msg-123', id: 'msg-123',
status: MessageStatus.Ready, status: MessageStatus.Ready,
}) })
) )
})
}) })
it('should start with previous content when continuing', async () => { it('should start with previous content when continuing', async () => {
@ -482,24 +485,24 @@ describe('useChat', () => {
const { result } = renderHook(() => useChat()) const { result } = renderHook(() => useChat())
await act(async () => { await act(async () => {
await result.current('', true, undefined, 'msg-123') await result.current('', true, undefined, undefined, 'msg-123')
}) })
// The accumulated text should contain the previous content // The accumulated text should contain the previous content plus new content
await waitFor(() => { // accumulatedTextRef starts with 'Partial response' (line 490)
expect(mockUpdateMessage).toHaveBeenCalledWith( // Then gets ' continued' appended (line 585)
expect.objectContaining({ expect(mockUpdateMessage).toHaveBeenCalledWith(
id: 'msg-123', expect.objectContaining({
content: expect.arrayContaining([ id: 'msg-123',
expect.objectContaining({ content: expect.arrayContaining([
text: expect.objectContaining({ expect.objectContaining({
value: expect.stringContaining('Partial response'), text: expect.objectContaining({
}) value: 'Partial response continued',
}) })
]) })
}) ])
) })
}) )
}) })
it('should handle attachments correctly when not continuing', async () => { it('should handle attachments correctly when not continuing', async () => {
@ -515,7 +518,7 @@ describe('useChat', () => {
] ]
await act(async () => { await act(async () => {
await result.current('Message with attachment', true, attachments, undefined) await result.current('Message with attachment', true, attachments, undefined, undefined)
}) })
expect(completionLib.newUserThreadContent).toHaveBeenCalledWith( expect(completionLib.newUserThreadContent).toHaveBeenCalledWith(
@ -543,17 +546,16 @@ describe('useChat', () => {
const { result } = renderHook(() => useChat()) const { result } = renderHook(() => useChat())
await act(async () => { await act(async () => {
await result.current('', true, undefined, 'msg-123') await result.current('', true, undefined, undefined, 'msg-123')
}) })
await waitFor(() => { // finalContent is created at line 678-683 with status Ready when continuing
expect(mockUpdateMessage).toHaveBeenCalledWith( expect(mockUpdateMessage).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
id: 'msg-123', id: 'msg-123',
status: MessageStatus.Ready, status: MessageStatus.Ready,
}) })
) )
})
}) })
}) })
@ -586,7 +588,7 @@ describe('useChat', () => {
const { result } = renderHook(() => useChat()) const { result } = renderHook(() => useChat())
await act(async () => { await act(async () => {
await result.current('', true, undefined, 'msg-123') await result.current('', true, undefined, undefined, 'msg-123')
}) })
expect(result.current).toBeDefined() expect(result.current).toBeDefined()