From 0fe011396dce1cda96743cf039d5cb8528a0f5ac Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Thu, 6 Mar 2025 10:16:14 -0700 Subject: [PATCH] update tests --- .../chatStreamMessageHelper.test.ts | 222 ++++++------------ .../NewChatProvider/useChatStreamMessage.ts | 3 - 2 files changed, 77 insertions(+), 148 deletions(-) diff --git a/web/src/context/Chats/NewChatProvider/chatStreamMessageHelper.test.ts b/web/src/context/Chats/NewChatProvider/chatStreamMessageHelper.test.ts index cbab0406b..0605d7892 100644 --- a/web/src/context/Chats/NewChatProvider/chatStreamMessageHelper.test.ts +++ b/web/src/context/Chats/NewChatProvider/chatStreamMessageHelper.test.ts @@ -16,6 +16,26 @@ import type { BusterChatMessageReasoning_pillContainer } from '@/api/asset_interfaces'; +const createBaseMessage = ( + messageId: string, + reasoningMessages: Record = {} +): IBusterChatMessage => ({ + id: messageId, + isCompletedStream: false, + request_message: { + request: 'test request', + sender_id: 'user1', + sender_name: 'Test User', + sender_avatar: null + }, + response_message_ids: [], + reasoning_message_ids: Object.keys(reasoningMessages), + response_messages: {}, + reasoning_messages: reasoningMessages, + created_at: new Date().toISOString(), + final_reasoning_message: null +}); + describe('initializeOrUpdateMessage', () => { it('should initialize a new message when currentMessage is undefined', () => { const messageId = 'test-id'; @@ -36,22 +56,7 @@ describe('initializeOrUpdateMessage', () => { it('should update an existing message', () => { const messageId = 'test-id'; - const currentMessage: IBusterChatMessage = { - id: messageId, - isCompletedStream: false, - request_message: { - request: 'original request', - sender_id: 'user1', - sender_name: 'Test User', - sender_avatar: null - }, - response_message_ids: [], - reasoning_message_ids: [], - response_messages: {}, - reasoning_messages: {}, - created_at: new Date().toISOString(), - final_reasoning_message: null - }; + const currentMessage = createBaseMessage(messageId); const updateFn = (draft: IBusterChatMessage) => { if (draft.request_message) { @@ -158,28 +163,15 @@ describe('updateResponseMessage', () => { }); it('should update existing message with new response', () => { - const currentMessage: IBusterChatMessage = { - id: 'test-message-id', - isCompletedStream: false, - request_message: { - request: 'test request', - sender_id: 'user1', - sender_name: 'Test User', - sender_avatar: null - }, - response_message_ids: ['response-1'], - reasoning_message_ids: [], - response_messages: { - 'response-1': { - id: 'response-1', - type: 'text', - message: 'Hello', - message_chunk: undefined - } - }, - reasoning_messages: {}, - created_at: new Date().toISOString(), - final_reasoning_message: null + const currentMessage: IBusterChatMessage = createBaseMessage('test-message-id'); + currentMessage.response_message_ids = ['response-1']; + currentMessage.response_messages = { + 'response-1': { + id: 'response-1', + type: 'text', + message: 'Hello', + message_chunk: undefined + } }; const mockEvent: ChatEvent_GeneratingResponseMessage = { @@ -317,32 +309,17 @@ describe('updateReasoningMessage', () => { }); it('should update existing text reasoning message with streaming chunks', () => { - const currentMessage: IBusterChatMessage = { - id: 'test-message-id', - isCompletedStream: false, - request_message: { - request: 'test request', - sender_id: 'user1', - sender_name: 'Test User', - sender_avatar: null - }, - response_message_ids: [], - reasoning_message_ids: ['reasoning-1'], - response_messages: {}, - reasoning_messages: { - 'reasoning-1': { - id: 'reasoning-1', - type: 'text', - message: 'Initial reasoning', - message_chunk: undefined, - title: 'Test Title', - secondary_title: 'Test Secondary Title', - status: 'loading' - } - }, - created_at: new Date().toISOString(), - final_reasoning_message: null - }; + const currentMessage = createBaseMessage('test-message-id', { + 'reasoning-1': { + id: 'reasoning-1', + type: 'text', + message: 'Initial reasoning', + message_chunk: undefined, + title: 'Test Title', + secondary_title: 'Test Secondary Title', + status: 'loading' + } + }); const reasoning: BusterChatMessageReasoning_text = { id: 'reasoning-1', @@ -376,34 +353,19 @@ describe('updateReasoningMessage', () => { } }; - const currentMessage: IBusterChatMessage = { - id: 'test-message-id', - isCompletedStream: false, - request_message: { - request: 'test request', - sender_id: 'user1', - sender_name: 'Test User', - sender_avatar: null - }, - response_message_ids: [], - reasoning_message_ids: ['reasoning-1'], - response_messages: {}, - reasoning_messages: { - 'reasoning-1': { - id: 'reasoning-1', - type: 'files', - file_ids: ['file-1'], - files: { - 'file-1': reasoningFile - }, - title: 'Test Title', - secondary_title: 'Test Secondary Title', - status: 'loading' - } - }, - created_at: new Date().toISOString(), - final_reasoning_message: null - }; + const currentMessage = createBaseMessage('test-message-id', { + 'reasoning-1': { + id: 'reasoning-1', + type: 'files', + file_ids: ['file-1'], + files: { + 'file-1': reasoningFile + }, + title: 'Test Title', + secondary_title: 'Test Secondary Title', + status: 'loading' + } + }); const reasoning: BusterChatMessageReasoning_files = { id: 'reasoning-1', @@ -446,31 +408,16 @@ describe('updateReasoningMessage', () => { ] }; - const currentMessage: IBusterChatMessage = { - id: 'test-message-id', - isCompletedStream: false, - request_message: { - request: 'test request', - sender_id: 'user1', - sender_name: 'Test User', - sender_avatar: null - }, - response_message_ids: [], - reasoning_message_ids: ['reasoning-1'], - response_messages: {}, - reasoning_messages: { - 'reasoning-1': { - id: 'reasoning-1', - type: 'pills', - pill_containers: [pillContainer], - title: 'Test Title', - secondary_title: 'Test Secondary Title', - status: 'loading' - } - }, - created_at: new Date().toISOString(), - final_reasoning_message: null - }; + const currentMessage = createBaseMessage('test-message-id', { + 'reasoning-1': { + id: 'reasoning-1', + type: 'pills', + pill_containers: [pillContainer], + title: 'Test Title', + secondary_title: 'Test Secondary Title', + status: 'loading' + } + }); const reasoning: BusterChatMessageReasoning_pills = { id: 'reasoning-1', @@ -528,34 +475,19 @@ describe('updateReasoningMessage', () => { } }; - const currentMessage: IBusterChatMessage = { - id: 'test-message-id', - isCompletedStream: false, - request_message: { - request: 'test request', - sender_id: 'user1', - sender_name: 'Test User', - sender_avatar: null - }, - response_message_ids: [], - reasoning_message_ids: ['reasoning-1'], - response_messages: {}, - reasoning_messages: { - 'reasoning-1': { - id: 'reasoning-1', - type: 'files', - file_ids: ['file-1'], - files: { - 'file-1': reasoningFile1 - }, - title: 'Test Title', - secondary_title: 'Test Secondary Title', - status: 'loading' - } - }, - created_at: new Date().toISOString(), - final_reasoning_message: null - }; + const currentMessage = createBaseMessage('test-message-id', { + 'reasoning-1': { + id: 'reasoning-1', + type: 'files', + file_ids: ['file-1'], + files: { + 'file-1': reasoningFile1 + }, + title: 'Test Title', + secondary_title: 'Test Secondary Title', + status: 'loading' + } + }); const reasoning: BusterChatMessageReasoning_files = { id: 'reasoning-1', diff --git a/web/src/context/Chats/NewChatProvider/useChatStreamMessage.ts b/web/src/context/Chats/NewChatProvider/useChatStreamMessage.ts index 2b29e71cf..3fda7168c 100644 --- a/web/src/context/Chats/NewChatProvider/useChatStreamMessage.ts +++ b/web/src/context/Chats/NewChatProvider/useChatStreamMessage.ts @@ -23,7 +23,6 @@ import { queryKeys } from '@/api/query_keys'; import { useQueryClient } from '@tanstack/react-query'; import { create } from 'mutative'; import { - initializeOrUpdateMessage, updateChatTitle, updateResponseMessage, updateReasoningMessage @@ -122,13 +121,11 @@ export const useChatStreamMessage = () => { const _generatingResponseMessageCallback = useMemoizedFn( (_: null, d: ChatEvent_GeneratingResponseMessage) => { const { message_id } = d; - const updatedMessage = updateResponseMessage( message_id, chatRefMessages.current[message_id], d ); - onUpdateChatMessageTransition({ id: message_id, response_messages: updatedMessage?.response_messages,