The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
const { H } = cy;
import {
InteractiveQuestion,
MetabotQuestion,
} from "@metabase/embedding-sdk-react";
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import { getSdkRoot } from "e2e/support/helpers/e2e-embedding-sdk-helpers";
import { mountSdkContent } from "e2e/support/helpers/embedding-sdk-component-testing";
import { signInAsAdminAndEnableEmbeddingSdk } from "e2e/support/helpers/embedding-sdk-testing";
import { mockAuthProviderAndJwtSignIn } from "e2e/support/helpers/embedding-sdk-testing/embedding-sdk-helpers";
const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
const metabot_id = "embeddedmetabotmetabo";
const query = {
"source-table": ORDERS_ID,
aggregation: [["max", ["field", ORDERS.QUANTITY, null]]],
breakout: [["field", ORDERS.PRODUCT_ID, null]],
limit: 2,
};
const generatedCard = {
type: "card",
id: "card-1",
title: "Question",
query: { id: "query-1", query: { database: 1, type: "query", query } },
display: "table",
};
const adHocQuestionPath = `/question#${btoa(
JSON.stringify({
dataset_query: generatedCard.query.query,
display: generatedCard.display,
displayIsLocked: true,
visualization_settings: {},
}),
)}`;
const metabotResponse = H.createMetabotSSEBody(
H.metabotTextPart(`Here is the [question link](${adHocQuestionPath})`),
);
// Metabot surfaces a chart to the SDK via a `generated_entity` card data part.
const metabotResponseWithChart = H.createMetabotSSEBody(
H.metabotTextPart(`Here is the [question link](${adHocQuestionPath})`),
H.metabotDataPart("generated_entity", generatedCard),
);
const metabotResponseWithSqlEditor = H.createMetabotSSEBody(
H.metabotTextPart(
"I'll write a SQL query for the most popular products so you can tweak it in the SQL editor!",
),
H.metabotDataPart("generated_entity", {
type: "card",
id: "card-sql",
title: "SQL query",
query: {
id: "query-sql",
query: { database: 1, type: "native", native: { query: "" } },
},
}),
);
const metabotRetryResponse = H.createMetabotSSEBody(
H.metabotTextPart(`Retry: Here is the [question link](${adHocQuestionPath})`),
);
describe("scenarios > embedding-sdk > metabot-question", () => {
const setup = (response: string) => {
signInAsAdminAndEnableEmbeddingSdk();
H.setupAnthropicLlmProvider();
H.mockMetabotResponse({
statusCode: 200,
body: response,
});
cy.log("Create a question");
H.createQuestion({
name: "1",
query: {
"source-table": ORDERS_ID,
aggregation: [["max", ["field", ORDERS.QUANTITY, null]]],
breakout: [["field", ORDERS.PRODUCT_ID, null]],
limit: 2,
},
}).then(({ body: question }) => {
cy.wrap(question.id).as("questionId");
cy.wrap(question.entity_id).as("questionEntityId");
});
cy.log("Create a collection");
cy.request("POST", "/api/collection", {
name: "first_collection",
description: "First collection",
}).then(({ body }) => {
cy.wrap(body.id).as("collectionId");
});
cy.signOut();
mockAuthProviderAndJwtSignIn();
};
it("should show the SQL editor when Metabot generates a native query", () => {
setup(metabotResponseWithSqlEditor);
mountSdkContent(<MetabotQuestion />);
getSdkRoot().within(() => {
cy.findByTestId("metabot-chat-input").type("Open the SQL editor {enter}");
cy.findByTestId("native-query-editor-container").should("be.visible");
});
});
it("should show drill-through results after drilling from a metabot question", () => {
setup(metabotResponseWithChart);
mountSdkContent(<MetabotQuestion />);
getSdkRoot().within(() => {
cy.findByTestId("metabot-chat-input").type("Show orders {enter}");
cy.findByTestId("visualization-root").should("exist");
H.tableInteractiveBody().findAllByRole("gridcell", { name: "2" }).click();
});
H.popover().findByText("View this Product's Orders").click();
getSdkRoot().within(() => {
cy.findByTestId("visualization-root").should("be.visible");
H.assertTableData({
columns: ["Product ID", "Max of Quantity"],
firstRows: [["2", "18"]],
});
});
});
it("should automatically show the ad-hoc question for the last agent message containing ad-hoc question link", () => {
setup(metabotResponseWithChart);
mountSdkContent(<MetabotQuestion />);
getSdkRoot().within(() => {
cy.findByTestId("metabot-chat-input").type("Show orders {enter}");
cy.findByTestId("visualization-root").should("exist");
});
});
it("should allow saving a question", () => {
cy.intercept("POST", "/api/card").as("postCard");
setup(metabotResponseWithChart);
cy.get("@collectionId").then((collectionId) => {
mountSdkContent(
<MetabotQuestion isSaveEnabled targetCollection={collectionId} />,
);
getSdkRoot().within(() => {
cy.findByTestId("metabot-chat-input").type("Show orders {enter}");
cy.findByTestId("visualization-root").should("exist");
cy.findByText("Save").should("be.visible").click();
H.modal().within(() => {
cy.findByText("Save new question").should("be.visible");
cy.findByRole("button", { name: "Save" }).click();
});
});
cy.wait("@postCard").then((interception) => {
const response = interception.response?.body;
const questionId = response.id;
mountSdkContent(<InteractiveQuestion questionId={questionId} />);
getSdkRoot().within(() => {
cy.findByText(
"Orders, Max of Quantity, Grouped by Product ID, 2 rows",
).should("be.visible");
});
});
});
});
it("should show the ad-hoc question when clicking its link", () => {
setup(metabotResponse);
mountSdkContent(<MetabotQuestion />);
getSdkRoot().within(() => {
cy.findByTestId("metabot-chat-input").type("Show orders {enter}");
cy.findAllByTestId("metabot-chat-message")
.should("have.length", 2)
.last()
.findByText("question link")
.click();
cy.findByTestId("visualization-root").should("exist");
});
});
it("should retry a message when clicking the retry button", () => {
setup(metabotResponse);
mountSdkContent(<MetabotQuestion />);
getSdkRoot().within(() => {
cy.findByTestId("metabot-chat-input").type("Show orders {enter}");
H.mockMetabotResponse({
statusCode: 200,
body: metabotRetryResponse,
});
cy.findAllByTestId("metabot-chat-message")
.should("have.length", 2)
.last()
.findByTestId("metabot-chat-message-retry")
.click();
cy.findAllByTestId("metabot-chat-message")
.should("have.length", 2)
.last()
.findByText(/Retry: Here is the/)
.should("exist");
});
});
it("should start a new conversation when clicking a `start new conversation` button", () => {
setup(metabotResponse);
mountSdkContent(<MetabotQuestion />);
getSdkRoot().within(() => {
cy.findByTestId("metabot-chat-input").type("Show orders {enter}");
H.mockMetabotResponse({
statusCode: 200,
body: metabotRetryResponse,
});
cy.findAllByTestId("metabot-chat-message").should("have.length", 2);
cy.findByTestId("metabot-new-conversation").click();
cy.findAllByTestId("metabot-chat-message").should("not.exist");
cy.findByTestId("metabot-chat-input").should("have.value", "");
cy.findByTestId("metabot-chat-input").type("Show orders {enter}");
cy.findAllByTestId("metabot-chat-message").should("have.length", 2);
});
});
it("should set correct metabot_id both for the a new message and when retrying", () => {
setup(metabotResponse);
mountSdkContent(<MetabotQuestion />);
getSdkRoot().within(() => {
cy.findByTestId("metabot-chat-input").type("Show orders {enter}");
cy.wait("@metabotAgent").then((interception) => {
const requestBody = interception.request.body;
expect(requestBody).to.have.property("metabot_id", metabot_id);
});
H.mockMetabotResponse({
statusCode: 200,
body: metabotRetryResponse,
});
cy.findAllByTestId("metabot-chat-message")
.should("have.length", 2)
.last()
.findByTestId("metabot-chat-message-retry")
.click();
cy.wait("@metabotAgent").then((interception) => {
const requestBody = interception.request.body;
expect(requestBody).to.have.property("metabot_id", metabot_id);
});
});
});
it("should show suggestion buttons when no messages exist", () => {
setup(metabotResponse);
mockSuggestedPrompts();
mountSdkContent(<MetabotQuestion />);
getSdkRoot().within(() => {
// Verify suggestion buttons are visible
cy.findAllByTestId("metabot-suggestion-button", { timeout: 10_000 })
.should("have.length", 3)
.should("be.visible");
// Verify suggestion texts are shown in order
cy.findAllByTestId("metabot-suggestion-button")
.eq(0)
.should("contain.text", "Show me total sales by product");
cy.findAllByTestId("metabot-suggestion-button")
.eq(1)
.should("contain.text", "What are the top performing products?");
cy.findAllByTestId("metabot-suggestion-button")
.eq(2)
.should("contain.text", "How many orders were placed last month?");
// Clicking on a suggestion should remove the suggestion buttons and add the user's message
cy.findByText("Show me total sales by product").click();
cy.findAllByTestId("metabot-suggestion-button").should("not.exist");
cy.findAllByTestId("metabot-chat-message").should("have.length", 2);
cy.findAllByTestId("metabot-chat-message")
.first()
.should("contain.text", "Show me total sales by product");
});
});
it("should switch to stacked layout on small viewport", () => {
setup(metabotResponse);
mountSdkContent(<MetabotQuestion />);
getSdkRoot().within(() => {
// On desktop, use the sidebar layout
cy.findByTestId("metabot-question-container").should(
"have.attr",
"data-layout",
"sidebar",
);
// On mobile, use the stacked layout
cy.viewport(400, 600);
cy.findByTestId("metabot-overflow-button").should("be.visible");
cy.findByTestId("metabot-question-container").should(
"have.attr",
"data-layout",
"stacked",
);
// On desktop, use the sidebar layout
cy.viewport(1200, 800);
cy.findByTestId("metabot-overflow-button").should("not.be.visible");
cy.findByTestId("metabot-question-container").should(
"have.attr",
"data-layout",
"sidebar",
);
});
});
it("should fallback to sidebar layout when invalid layout prop is provided and show a warning", () => {
setup(metabotResponse);
cy.window().then((win) => {
cy.spy(win.console, "warn").as("consoleWarn");
});
mountSdkContent(<MetabotQuestion layout="foobar" />);
getSdkRoot().within(() => {
cy.findByTestId("metabot-question-container").should(
"have.attr",
"data-layout",
"sidebar",
);
});
cy.get("@consoleWarn").should(
"have.been.calledWith",
'Invalid layout for MetabotQuestion: foobar. Valid values are "stacked", "sidebar", or "auto"',
);
});
});
describe("scenarios > embedding-sdk > metabot-question > enablement", () => {
it("should show an error when embedded-metabot-enabled? is false", () => {
signInAsAdminAndEnableEmbeddingSdk();
H.setupAnthropicLlmProvider();
cy.log("Disable embedded metabot");
H.updateEnterpriseSettings({ "embedded-metabot-enabled?": false });
cy.signOut();
mockAuthProviderAndJwtSignIn();
mountSdkContent(<MetabotQuestion />);
getSdkRoot().within(() => {
cy.findByText("Metabot is not enabled for embedded analytics.").should(
"be.visible",
);
cy.findByTestId("metabot-question-container").should("not.exist");
});
});
});
const mockSuggestedPrompts = () => {
cy.intercept(
"GET",
`/api/metabot/metabot/${metabot_id}/prompt-suggestions?limit=3&sample=true`,
{
statusCode: 200,
body: {
prompts: [
{
id: 1,
prompt: "Show me total sales by product",
},
{
id: 2,
prompt: "What are the top performing products?",
},
{
id: 3,
prompt: "How many orders were placed last month?",
},
],
limit: 3,
offset: 0,
total: 3,
},
},
).as("suggestedPrompts");
};