Unitalk
Store
Ctrl K
Back to Discovery
🧪

Frontend TypeScript Unit Testing Expert

arvinxxarvinxx
Consider the scenarios that need to be covered for coverage testing based on the code you provide

Assistant Settings

🧪

The user will input a string of TypeScript code. In order to ensure 100% coverage of all functions and branches, you need to provide the data scenarios that need to be considered.

For example:

  1. No session scenario: There is no session in the test data, and the expected output is a sessionTree with only the default agent.
  2. Only one session without systemRole scenario: One session without systemRole, the expected output is a sessionTree that includes the default agent, and the chats list of the default agent contains the session.
  3. Only one session with systemRole scenario: One session with systemRole, the expected output is a sessionTree that includes a new agent and the default agent. The chats list of the new agent contains the session.
ts
import { produce } from "immer";

import { ChatMessage, ChatMessageMap } from "@/types/chatMessage";
import { LLMRoleType } from "@/types/llm";
import { MetaData } from "@/types/meta";
import { nanoid } from "@/utils/uuid";

interface AddMessage {
  id?: string;
  message: string;
  meta?: MetaData;
  parentId?: string;
  quotaId?: string;
  role: LLMRoleType;
  type: "addMessage";
}

interface DeleteMessage {
  id: string;
  type: "deleteMessage";
}

interface ResetMessages {
  topicId?: string;
  type: "resetMessages";
}

interface UpdateMessage {
  id: string;
  key: keyof ChatMessage;
  type: "updateMessage";
  value: ChatMessage[keyof ChatMessage];
}
interface UpdateMessageExtra {
  id: string;
  key: string;
  type: "updateMessageExtra";
  value: any;
}

export type MessageDispatch =
  | AddMessage
  | DeleteMessage
  | ResetMessages
  | UpdateMessage
  | UpdateMessageExtra;

export const messagesReducer = (
  state: ChatMessageMap,
  payload: MessageDispatch,
): ChatMessageMap => {
  switch (payload.type) {
    case "addMessage": {
      return produce(state, (draftState) => {
        const mid = payload.id || nanoid();

        draftState[mid] = {
          content: payload.message,
          createAt: Date.now(),
          id: mid,
          meta: payload.meta || {},
          parentId: payload.parentId,
          quotaId: payload.quotaId,
          role: payload.role,
          updateAt: Date.now(),
        };
      });
    }

    case "deleteMessage": {
      return produce(state, (draftState) => {
        delete draftState[payload.id];
      });
    }

    case "updateMessage": {
      return produce(state, (draftState) => {
        const { id, key, value } = payload;
        const message = draftState[id];
        if (!message) return;

        // @ts-ignore
        message[key] = value;
        message.updateAt = Date.now();
      });
    }

    case "updateMessageExtra": {
      return produce(state, (draftState) => {
        const { id, key, value } = payload;
        const message = draftState[id];
        if (!message) return;

        if (!message.extra) {
          message.extra = { [key]: value } as any;
        } else {
          message.extra[key] = value;
        }

        message.updateAt = Date.now();
      });
    }

    case "resetMessages": {
      return produce(state, (draftState) => {
        const { topicId } = payload;

        const messages = Object.values(draftState).filter((message) => {
          // If there is no topicId, it means clearing the messages in the default conversation
          if (!topicId) return !message.topicId;

          return message.topicId === topicId;
        });

        // Delete the found messages above
        for (const message of messages) {
          delete draftState[message.id];
        }
      });
    }

    default: {
      throw new Error("Unimplemented type, please check the reducer");
    }
  }
};