Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

19.9 KB
Raw Blame
import { parse } from '@lobechat/conversation-flow';
import { type ChatToolPayload, type UIChatMessage } from '@lobechat/types';
import i18n from 'i18next';

import { messagesReducer as conversationMessagesReducer } from '@/features/Conversation/store/slices/data/reducer';

import { type MessageDispatch } from './reducer';
import { messagesReducer } from './reducer';

const defaultToolPayload = (): ChatToolPayload => ({
  id: 'tc_1',
  type: 'default',
  identifier: 'tool1',
  apiName: 'testFunction',
  arguments: '{"arg1": "value1"}',
});

describe('messagesReducer', () => {
  let initialState: UIChatMessage[];

  beforeEach(() => {
    initialState = [
      {
        id: 'message1',
        content: 'Hello World',
        createdAt: 1629264000000,
        updatedAt: 1629264000000,
        role: 'user',
      },
      {
        id: 'message2',
        content: 'How are you?',
        createdAt: 1629264000000,
        updatedAt: 1629264000000,
        role: 'assistant',
        tools: [
          { identifier: 'tool1', apiName: 'calculator', id: 'abc', type: 'default', arguments: '' },
        ],
      },
    ];
  });

  describe('updateMessage', () => {
    it('should update the specified message with the provided value', () => {
      const payload: MessageDispatch = {
        type: 'updateMessage',
        id: 'message1',
        value: { content: 'Updated Message' },
      };

      const newState = messagesReducer(initialState, payload);
      const newMessage = newState.find((i) => i.id === 'message1');

      expect(newMessage).not.toBeUndefined();
      expect(newMessage?.content).toBe('Updated Message');
      expect(newMessage?.updatedAt).toBeGreaterThan(initialState[0].updatedAt);
    });

    it('should not modify the state if the specified message does not exist', () => {
      const payload: MessageDispatch = {
        type: 'updateMessage',
        id: 'nonexistentMessage',
        value: { content: 'Updated Message' },
      };

      const newState = messagesReducer(initialState, payload);

      expect(newState).toEqual(initialState);
    });
  });

  describe('unimplemented type', () => {
    it('should throw an error when an unimplemented type is provided', () => {
      // @ts-ignore
      const payload: MessageDispatch = { type: 'unimplementedType' };

      expect(() => messagesReducer(initialState, payload)).toThrowError(
        i18n.t('errors.unimplementedType', { ns: 'common' }),
      );
    });
  });

  describe('updateMessageExtra', () => {
    it('should update the extra field of a message', () => {
      const payload: MessageDispatch = {
        type: 'updateMessageExtra',
        id: 'message1',
        key: 'testKey',
        value: 'testValue',
      };

      const newState = messagesReducer(initialState, payload);
      const updatedMessage = newState.find((m) => m.id === 'message1');

      expect(updatedMessage?.extra).toEqual({ testKey: 'testValue' });
      expect(updatedMessage?.updatedAt).toBeGreaterThan(initialState[0].updatedAt);
    });

    it('should update the extra field of a message if extra exist', () => {
      const payload: MessageDispatch = {
        type: 'updateMessageExtra',
        id: 'data',
        key: 'abc',
        value: '2',
      };

      const newState = messagesReducer(
        [
          {
            id: 'data',
            content: 'Hello World',
            createdAt: 1629264000000,
            updatedAt: 1629264000000,
            role: 'user',
            extra: { abc: '1' },
          } as UIChatMessage,
          ...initialState,
        ],
        payload,
      );
      const updatedMessage = newState.find((m) => m.id === 'data');

      expect(updatedMessage?.extra).toEqual({ abc: '2' });
      expect(updatedMessage?.updatedAt).toBeGreaterThan(initialState[0].updatedAt);
    });

    it('should not modify state if message is not found', () => {
      const payload: MessageDispatch = {
        type: 'updateMessageExtra',
        id: 'nonexistent',
        key: 'testKey',
        value: 'testValue',
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });
  });

  describe('updateMessageMetadata', () => {
    it('should merge update the metadata field of a message', () => {
      const payload: MessageDispatch = {
        type: 'updateMessageMetadata',
        id: 'message1',
        value: { activeBranchIndex: 1 },
      };

      const newState = messagesReducer(initialState, payload);
      const updatedMessage = newState.find((m) => m.id === 'message1');

      expect(updatedMessage?.metadata).toEqual({ activeBranchIndex: 1 });
      expect(updatedMessage?.updatedAt).toBeGreaterThan(initialState[0].updatedAt);
    });

    it('should merge update the metadata field if metadata already exists', () => {
      const state = [
        {
          ...initialState[0],
          metadata: { activeBranchIndex: 0 },
        },
      ];

      const payload: MessageDispatch = {
        type: 'updateMessageMetadata',
        id: 'message1',
        value: { activeBranchIndex: 2 },
      };

      const newState = messagesReducer(state, payload);
      const updatedMessage = newState.find((m) => m.id === 'message1');

      expect(updatedMessage?.metadata).toEqual({ activeBranchIndex: 2 });
      expect(updatedMessage?.updatedAt).toBeGreaterThan(initialState[0].updatedAt);
    });

    it('should not modify state if message is not found', () => {
      const payload: MessageDispatch = {
        type: 'updateMessageMetadata',
        id: 'nonexistent',
        value: { activeBranchIndex: 1 },
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });
  });

  describe('updatePluginState', () => {
    it('should update the plugin state of a message', () => {
      const payload: MessageDispatch = {
        type: 'updatePluginState',
        id: 'message1',
        key: 'pluginKey',
        value: 'pluginValue',
      };

      const newState = messagesReducer(initialState, payload);
      const updatedMessage = newState.find((m) => m.id === 'message1');

      expect(updatedMessage?.pluginState).toEqual({ pluginKey: 'pluginValue' });
      expect(updatedMessage?.updatedAt).toBeGreaterThan(initialState[0].updatedAt);
    });

    it('should not modify state if message is not found', () => {
      const payload: MessageDispatch = {
        type: 'updatePluginState',
        id: 'nonexistent',
        key: 'pluginKey',
        value: 'pluginValue',
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });

    it('should not modify state if new plugin state is equal to old state', () => {
      const state = [
        {
          ...initialState[0],
          pluginState: { existingKey: 'existingValue' },
        },
      ];

      const payload: MessageDispatch = {
        type: 'updatePluginState',
        id: 'message1',
        key: 'existingKey',
        value: 'existingValue',
      };

      const newState = messagesReducer(state, payload);
      expect(newState).toEqual(state);
    });

    it('treats a repeated identical dispatch as a no-op (immer-draft comparison guard)', () => {
      const payload: MessageDispatch = {
        type: 'updatePluginState',
        id: 'message1',
        key: 'pluginKey',
        value: 'pluginValue',
      };

      // First dispatch writes the value and bumps updatedAt.
      const firstState = messagesReducer(initialState, payload);
      const firstMessage = firstState.find((m) => m.id === 'message1')!;

      // Second, identical dispatch must be a no-op. Inside `produce` the
      // message's pluginState is an immer draft; getComparablePluginState must
      // unwrap it via `current()` so isEqual sees no change (regression guard —
      // immer 11.1.9 broke comparing a draft directly, causing spurious
      // updatedAt bumps / re-renders).
      const secondState = messagesReducer(firstState, payload);
      const secondMessage = secondState.find((m) => m.id === 'message1')!;

      expect(secondMessage).toBe(firstMessage);
      expect(secondMessage.updatedAt).toBe(firstMessage.updatedAt);
    });
  });

  describe('replaceMessagePluginState', () => {
    it('replaces plugin state and advances its metadata watermark atomically', () => {
      const state: UIChatMessage[] = [
        {
          content: '',
          id: 'tool-1',
          metadata: { heteroMessageId: 'native-tool-1' },
          pluginState: { obsolete: true, todos: { items: [{ text: 'old' }] } },
          role: 'tool',
        } as UIChatMessage,
      ];

      const newState = messagesReducer(state, {
        id: 'tool-1',
        metadata: {
          heterogeneousToolStateOperationId: 'op-1',
          heterogeneousToolStateSeq: 2,
        },
        type: 'replaceMessagePluginState',
        value: { todos: { items: [{ status: 'processing', text: 'new' }] } },
      });

      expect(newState[0].pluginState).toEqual({
        todos: { items: [{ status: 'processing', text: 'new' }] },
      });
      expect(newState[0].metadata).toEqual({
        heteroMessageId: 'native-tool-1',
        heterogeneousToolStateOperationId: 'op-1',
        heterogeneousToolStateSeq: 2,
      });
    });
  });

  describe('updateMessagePlugin', () => {
    it.each([
      ['chat', messagesReducer],
      ['conversation', conversationMessagesReducer],
    ] as const)('shows and clears tool failures before a server refresh in %s', (_, reduce) => {
      const state: UIChatMessage[] = [
        initialState[0],
        { ...initialState[1], parentId: 'message1' },
        {
          content: 'Body saved for review; title not changed.',
          createdAt: 1629264000001,
          id: 'tool-result',
          parentId: 'message2',
          role: 'tool',
          tool_call_id: 'abc',
          updatedAt: 1629264000001,
        },
      ];
      const error = { message: 'Page changed while saving', type: 'PageChangedDuringCommand' };
      const resultError = (messages: UIChatMessage[]) =>
        parse(messages).flatList.find((row) => row.role === 'assistantGroup')?.children?.[0]
          .tools?.[0].result?.error;
      const failed = reduce(state, {
        id: 'tool-result',
        type: 'updateMessagePlugin',
        value: { error },
      });
      expect(resultError(failed)).toEqual(error);

      const unchanged = reduce(failed, {
        id: 'tool-result',
        type: 'updateMessagePlugin',
        value: { arguments: '{}' },
      });
      expect(resultError(unchanged)).toEqual(error);

      const recovered = reduce(unchanged, {
        id: 'tool-result',
        type: 'updateMessagePlugin',
        value: { error: null },
      });
      expect(resultError(recovered)).toBeUndefined();
    });

    it('should update the plugin of a tool message', () => {
      const toolMessage: UIChatMessage = {
        id: 'toolMessage',
        role: 'tool',
        content: 'Tool content',
        createdAt: 1629264000000,
        updatedAt: 1629264000000,
        plugin: {
          identifier: 'tool1',
          apiName: 'calculator',
          type: 'default',
          arguments: '',
        },
        tool_call_id: 'abc',
      };

      const state = [...initialState, toolMessage];

      const payload: MessageDispatch = {
        type: 'updateMessagePlugin',
        id: 'toolMessage',
        value: { identifier: 'newPlugin' },
      };

      const newState = messagesReducer(state, payload);
      const updatedMessage = newState.find((m) => m.id === 'toolMessage');

      expect(updatedMessage?.plugin).toEqual({
        apiName: 'calculator',
        arguments: '',
        identifier: 'newPlugin',
        type: 'default',
      });
      expect(updatedMessage?.updatedAt).toBeGreaterThan(toolMessage.updatedAt);
    });

    it('should not modify state if message is not found or not a tool message', () => {
      const payload: MessageDispatch = {
        type: 'updateMessagePlugin',
        id: 'message1', // This is a user message, not a tool message
        value: { identifier: 'newPlugin' },
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });
  });

  describe('updateMessageTools', () => {
    it('should update a specific tool in an assistant message', () => {
      const payload: MessageDispatch = {
        type: 'updateMessageTools',
        id: 'message2',
        tool_call_id: 'abc',
        value: { apiName: 'updatedCalculator' },
      };

      const newState = messagesReducer(initialState, payload);
      const updatedMessage = newState.find((m) => m.id === 'message2');

      expect(updatedMessage?.tools?.[0]).toEqual({
        apiName: 'updatedCalculator',
        arguments: '',
        id: 'abc',
        identifier: 'tool1',
        type: 'default',
      });
      expect(updatedMessage?.updatedAt).toBeGreaterThan(initialState[1].updatedAt);
    });

    it('should not modify state if message is not found or not an assistant message', () => {
      const payload: MessageDispatch = {
        type: 'updateMessageTools',
        id: 'message1', // This is a user message, not an assistant message
        tool_call_id: 'tool1',
        value: { identifier: 'updatedCalculator' },
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });

    it('should not modify state if tool is not found in the message', () => {
      const payload: MessageDispatch = {
        type: 'updateMessageTools',
        id: 'message2',
        tool_call_id: 'nonexistentTool',
        value: { identifier: 'updatedCalculator' },
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });
  });

  describe('addMessageTool', () => {
    it('should add a tool to the specified assistant message if it dont have tools', () => {
      const messageId = '1';
      const toolPayload: ChatToolPayload = defaultToolPayload();

      const payload: MessageDispatch = {
        type: 'addMessageTool',
        id: messageId,
        value: toolPayload,
      };

      const newState = messagesReducer(
        [...initialState, { id: messageId, role: 'assistant', content: '' } as UIChatMessage],
        payload,
      );
      const updatedMessage = newState.find((m) => m.id === messageId);

      expect(updatedMessage).not.toBeUndefined();
      expect(updatedMessage?.tools).toHaveLength(1);
      expect(updatedMessage?.tools?.[0]).toEqual(toolPayload);
      expect(updatedMessage?.updatedAt).toBeGreaterThan(initialState[0].updatedAt);
    });

    it('should add a tool to the specified assistant message', () => {
      const messageId = 'message2';
      const toolPayload: ChatToolPayload = defaultToolPayload();

      const payload: MessageDispatch = {
        type: 'addMessageTool',
        id: messageId,
        value: toolPayload,
      };

      const newState = messagesReducer(
        [...initialState, { id: messageId, role: 'assistant', content: '' } as UIChatMessage],
        payload,
      );
      const updatedMessage = newState.find((m) => m.id === messageId);

      expect(updatedMessage).not.toBeUndefined();
      expect(updatedMessage?.tools).toHaveLength(2);
      expect(updatedMessage?.tools?.[1]).toEqual(toolPayload);
    });

    it('should not modify the state if the message is not found', () => {
      const toolPayload: ChatToolPayload = defaultToolPayload();

      const payload: MessageDispatch = {
        type: 'addMessageTool',
        id: 'nonexistentMessage',
        value: toolPayload,
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });

    it('should not add a tool if the message is not an assistant message', () => {
      const toolPayload: ChatToolPayload = defaultToolPayload();

      const payload: MessageDispatch = {
        type: 'addMessageTool',
        id: 'message1', // This is a user message
        value: toolPayload,
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });
  });

  describe('deleteMessageTool', () => {
    it('should delete the specified tool from the message', () => {
      const payload: MessageDispatch = {
        type: 'deleteMessageTool',
        id: 'message2',
        tool_call_id: 'abc',
      };

      const newState = messagesReducer(initialState, payload);
      const updatedMessage = newState.find((m) => m.id === 'message2');

      expect(updatedMessage).not.toBeUndefined();
      expect(updatedMessage?.tools).toHaveLength(0);
      expect(updatedMessage?.updatedAt).toBeGreaterThan(initialState[0].updatedAt);
    });

    it('should not modify the state if the message is not found', () => {
      const payload: MessageDispatch = {
        type: 'deleteMessageTool',
        id: 'nonexistentMessage',
        tool_call_id: 'tool1',
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });

    it('should not modify the state if the tool is not found', () => {
      const payload: MessageDispatch = {
        type: 'deleteMessageTool',
        id: 'message1',
        tool_call_id: 'nonexistentTool',
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });

    it('should not delete a tool if the message is not an assistant message', () => {
      const payload: MessageDispatch = {
        type: 'deleteMessageTool',
        id: 'message1', // This is a user message
        tool_call_id: 'tool1',
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });
  });

  describe('createMessage', () => {
    it('should add a new message to the state', () => {
      const payload: MessageDispatch = {
        type: 'createMessage',
        id: 'newMessage',
        value: {
          content: 'New message content',
          role: 'user',
          sessionId: 'inbox',
        },
      };

      const newState = messagesReducer(initialState, payload);

      expect(newState.length).toBe(initialState.length + 1);
      const newMessage = newState.find((m) => m.id === 'newMessage');
      expect(newMessage).toBeDefined();
      expect(newMessage?.content).toBe('New message content');
      expect(newMessage?.role).toBe('user');
      expect(newMessage?.createdAt).toBeDefined();
      expect(newMessage?.updatedAt).toBeDefined();
    });

    it('keeps a creation time the dispatch supplies', () => {
      const newState = messagesReducer(initialState, {
        createdAt: 1_700_000_000_000,
        id: 'seeded',
        type: 'createMessage',
        value: { content: '', role: 'assistant', sessionId: 'inbox' },
      });

      expect(newState.find((m) => m.id === 'seeded')?.createdAt).toBe(1_700_000_000_000);
    });
  });

  describe('deleteMessage', () => {
    it('should remove a message from the state', () => {
      const payload: MessageDispatch = {
        type: 'deleteMessage',
        id: 'message1',
      };

      const newState = messagesReducer(initialState, payload);

      expect(newState.length).toBe(initialState.length - 1);
      expect(newState.find((m) => m.id === 'message1')).toBeUndefined();
    });

    it('should not modify state if message to delete is not found', () => {
      const payload: MessageDispatch = {
        type: 'deleteMessage',
        id: 'nonexistentMessage',
      };

      const newState = messagesReducer(initialState, payload);
      expect(newState).toEqual(initialState);
    });
  });

  describe('deleteMessages', () => {
    it('should remove 2 messages from the state', () => {
      const payload: MessageDispatch = {
        type: 'deleteMessages',
        ids: ['message1', 'message2'],
      };

      const newState = messagesReducer(initialState, payload);

      expect(newState.length).toBe(0);
      expect(newState.find((m) => m.id === 'message1')).toBeUndefined();
      expect(newState.find((m) => m.id === 'message2')).toBeUndefined();
    });
  });
});