Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

3.7 KB
Raw Blame
import { assocIn } from "icepick";

import { act, screen } from "__support__/ui";
import { metabotActions } from "metabase/metabot/state";
import type { MetabotMessagePart } from "metabase/metabot/state/types";
import {
  createTestMetabotState,
  setup,
  testConversationId,
} from "metabase/metabot/tests/utils";

import { MetabotChatHistory } from "./MetabotChatHistory";

const makeVisibleState = (parts: MetabotMessagePart[]) =>
  assocIn(
    assocIn(createTestMetabotState(), ["agents", "omnibot", "visible"], true),
    ["conversations", testConversationId("omnibot"), "messages"],
    parts.map((part, index) => ({
      id: `message-${index}`,
      role: part.role,
      parts: [part],
      status: { type: "done" },
    })),
  );

describe("MetabotChatHistory", () => {
  it("should hide the long chat notice while the agent is responding", () => {
    const { store } = setup({
      ui: <MetabotChatHistory />,
      metabotInitialState: assocIn(
        assocIn(
          makeVisibleState([
            { id: "1", role: "user", type: "text", message: "hi" },
            { id: "2", role: "agent", type: "text", message: "hello" },
          ]),
          [
            "conversations",
            testConversationId("omnibot"),
            "messages",
            1,
            "contextTokens",
          ],
          200,
        ),
        ["conversations", testConversationId("omnibot"), "contextWindowTokens"],
        200,
      ),
    });

    expect(screen.getByTestId("metabot-long-chat-notice")).toBeInTheDocument();

    act(() => {
      store.dispatch(
        metabotActions.setIsProcessing({
          conversationId: testConversationId("omnibot"),
          processing: true,
        }),
      );
    });

    expect(
      screen.queryByTestId("metabot-long-chat-notice"),
    ).not.toBeInTheDocument();
  });

  it("does not render an agent message for a card-only reply", () => {
    setup({
      ui: <MetabotChatHistory />,
      metabotInitialState: makeVisibleState([
        {
          id: "prompt",
          role: "user",
          type: "text",
          message: "Show me orders",
        },
        {
          id: "card",
          role: "agent",
          type: "data_part",
          part: {
            type: "data-generated_entity",
            data: {
              type: "card",
              id: "card-1",
              title: "Orders",
              query: {
                id: "q-1",
                query: {
                  database: 1,
                  type: "query",
                  query: { "source-table": 1 },
                },
              },
              display: "table",
            },
          },
        },
      ]),
    });

    expect(screen.getByText("Show me orders")).toBeInTheDocument();
    expect(screen.getAllByTestId("metabot-chat-message")).toHaveLength(1);
  });

  it("should not render chain_of_thought messages (app-only reasoning UI)", () => {
    setup({
      ui: <MetabotChatHistory />,
      metabotInitialState: makeVisibleState([
        {
          id: "1",
          role: "agent",
          type: "chain_of_thought",
          steps: [{ kind: "reasoning", text: "Let me think about this" }],
          finished: true,
        },
      ]),
    });

    expect(
      screen.queryByTestId("metabot-chain-of-thought"),
    ).not.toBeInTheDocument();
  });

  it("should render non-chart messages normally", async () => {
    setup({
      ui: <MetabotChatHistory />,
      metabotInitialState: makeVisibleState([
        { id: "1", role: "agent", type: "text", message: "Hello world" },
      ]),
    });

    expect(
      await screen.findByTestId("metabot-chat-message"),
    ).toBeInTheDocument();
    expect(screen.getByText("Hello world")).toBeInTheDocument();
  });
});