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
e2e2unit: Metabot (#85991)
Nemanja Glumac2fab57570327
diff --git a/e2e/test/scenarios/metabot/ai-controls.cy.spec.ts b/e2e/test/scenarios/metabot/ai-controls.cy.spec.tsindex f9023004470..ee0877ca532 100644--- a/e2e/test/scenarios/metabot/ai-controls.cy.spec.ts+++ b/e2e/test/scenarios/metabot/ai-controls.cy.spec.ts@@ -2,7 +2,7 @@ import { NORMAL_USER_ID } from "e2e/support/cypress_sample_instance_data";const { H } = cy;-describe("AI Controls > Metabot access and customization", () => {+describe("AI Controls > Metabot access", () => {beforeEach(() => {H.restore();cy.signInAsAdmin();@@ -16,7 +16,7 @@ describe("AI Controls > Metabot access and customization", () => {});describe("Feature Access page", () => {- it("should display the AI feature access table with groups, save a permission change, and hide the Metabot chat icon for users in a group without Metabot access", () => {+ it("should hide the Metabot chat icon for users in a group without Metabot access, and show it once access is granted", () => {cy.intercept("GET", "/api/ee/ai-controls/permissions").as("getPermissions",);@@ -28,23 +28,6 @@ describe("AI Controls > Metabot access and customization", () => {cy.wait("@getPermissions");- cy.findByTestId("ai-feature-access-table").should("be.visible");-- // Verify column headers- cy.findByTestId("ai-feature-access-table").within(() => {- cy.findByText("AI features").should("be.visible");- cy.findByText("Chat and NLQ").should("be.visible");- cy.findByText("SQL generation").should("be.visible");- cy.findByText("Other tools").should("be.visible");- });-- // Admins row checkboxes should be always checked and disabled- cy.findByRole("row", { name: /Administrators permissions/ }).within(- () => {- cy.findByRole("switch").should("be.checked").and("be.disabled");- },- );-cy.findByRole("row", { name: /All Users permissions/ }).within(() => {// Toggle the metabot AI features switch off (default is checked)cy.findByRole("switch").should("be.checked").click({ force: true });@@ -52,12 +35,6 @@ describe("AI Controls > Metabot access and customization", () => {cy.wait("@updatePermissions").its("response.statusCode").should("eq", 200);-- // Check that the switch is off and all checkboxes are unchecked- cy.findByRole("switch").should("not.be.checked");- cy.findAllByRole("checkbox").each(($checkbox) => {- cy.wrap($checkbox).should("not.be.checked");- });});cy.intercept("GET", "/api/metabot/permissions/user-permissions").as(@@ -90,11 +67,6 @@ describe("AI Controls > Metabot access and customization", () => {cy.wait("@updatePermissions").its("response.statusCode").should("eq", 200);-- // When metabot permission is toggled on, all tools should be enabled by default- cy.findAllByRole("checkbox").each(($checkbox) => {- cy.wrap($checkbox).should("be.checked");- });});cy.log(@@ -113,162 +85,9 @@ describe("AI Controls > Metabot access and customization", () => {.should("be.visible");});});-- describe("Customization", () => {- it("should save a custom Metabot name, icon and illustrations setting and use them in the app", () => {- H.updateEnterpriseSettings({- "metabot-show-illustrations": true,- });-- cy.intercept("PUT", "/api/setting/metabot-name").as("saveName");- cy.intercept("PUT", "/api/setting/metabot-icon").as("saveIcon");- cy.intercept("PUT", "/api/setting/metabot-show-illustrations").as(- "saveIllustrations",- );-- cy.visit("/admin/metabot/customization");-- cy.findByRole("heading", { name: "Customization", level: 1 }).should(- "be.visible",- );- cy.findByLabelText("AI agent's name")- .should("be.visible")- .clear()- .type("HAL 9000");-- // Wait for debounced save- cy.wait("@saveName").its("response.statusCode").should("eq", 204);-- H.main().findByText("AI agent's icon").should("be.visible");- cy.findByRole("button", { name: "Upload a custom icon" }).should(- "be.visible",- );-- // Upload a tiny PNG via the hidden file input- cy.get('input[type="file"]').selectFile(- {- contents: Cypress.Buffer.from(TINY_PNG_BASE64, "base64"),- fileName: "metabot-icon.png",- mimeType: "image/png",- },- { force: true },- );-- cy.wait("@saveIcon").its("response.statusCode").should("eq", 204);-- // The "Remove custom icon" button should be visible- cy.findByLabelText("Remove custom icon").should("be.visible");-- H.main().findByText("Metabot illustrations").should("be.visible");-- // The switch should be ON- cy.findByRole("switch", { name: "Show Metabot illustrations" }).should(- "have.attr",- "data-checked",- "true",- );-- // Toggle illustrations off- cy.findByRole("switch", {- name: "Show Metabot illustrations",- }).click({ force: true });-- cy.wait("@saveIllustrations")- .its("response.statusCode")- .should("eq", 204);-- cy.visit("/");- cy.findByLabelText("Navigation bar").should("be.visible");-- // The app bar button tooltip/aria-label should reflect the custom name- H.appBar()- .findByRole("button", { name: /Chat with HAL 9000/ })- .should("be.visible")- .within(() => {- // When a custom icon is set, MetabotIcon renders an <img> with alt = metabotName- cy.get("img").should("be.visible");- });-- // Open Metabot chat to verify illustrations are hidden- H.appBar()- .findByRole("button", { name: /Chat with HAL 9000/ })- .click();- H.assertChatVisibility("visible");-- cy.findByTestId("metabot-empty-chat-info").should("be.visible");- // The SVG illustration should NOT be rendered when showIllustrations=false- cy.findByTestId("metabot-empty-chat-info")- .find("svg")- .should("not.exist");- // But the hint text should still be visible, with the no-illustration copy- cy.findByTestId("metabot-empty-chat-info")- .findByText(/Explore your metrics and models with AI/)- .should("be.visible");- });- });-- describe("System Prompts pages", () => {- it("should save custom Metabot chat and SQL generation system prompts", () => {- cy.intercept("PUT", "/api/setting/metabot-chat-system-prompt").as(- "savePrompt",- );- cy.intercept("PUT", "/api/setting/metabot-sql-system-prompt").as(- "saveSqlPrompt",- );-- cy.visit("/admin/metabot/system-prompts/metabot-chat");-- cy.findByRole("heading", {- name: "AI chat prompt instructions",- level: 1,- }).should("be.visible");-- cy.findByRole("textbox", { name: /AI chat prompt instructions/ })- .should("be.visible")- .click()- .type("Be concise and helpful.")- .blur();-- cy.wait("@savePrompt").its("response.statusCode").should("eq", 204);-- cy.findByTestId("admin-layout-sidebar")- .findByRole("link", { name: "SQL generation" })- .click();-- cy.findByRole("heading", {- name: "SQL generation prompt instructions",- level: 1,- }).should("be.visible");-- cy.findByRole("textbox", {- name: /SQL generation prompt instructions/,- })- .should("be.visible")- .click()- .type("Always use uppercase SQL keywords.")- .blur();-- cy.wait("@saveSqlPrompt").its("response.statusCode").should("eq", 204);-- // Reload and verify persistence- cy.reload();- cy.findByRole("textbox", {- name: /SQL generation prompt instructions/,- }).should("have.text", "Always use uppercase SQL keywords.");-- cy.findByTestId("admin-layout-sidebar")- .findByRole("link", { name: "AI chat" })- .click();- cy.findByRole("textbox", {- name: /AI chat prompt instructions/,- }).should("have.text", "Be concise and helpful.");- });- });});describe("AI controls > AI usage limits", () => {- const AI_USAGE_LIMITS_URL = "/admin/metabot/usage-controls/ai-usage-limits";-beforeEach(() => {H.restore();cy.signInAsAdmin();@@ -280,99 +99,6 @@ describe("AI controls > AI usage limits", () => {llmMockServerTeardown();});- describe("Instance limit settings and enforcement", () => {- beforeEach(() => {- cy.intercept("GET", "/api/ee/ai-controls/usage/instance").as(- "getInstanceLimit",- );- cy.intercept("PUT", "/api/ee/ai-controls/usage/instance").as(- "updateInstanceLimit",- );- cy.intercept("GET", "/api/ee/ai-controls/usage/group").as(- "getGroupLimits",- );- cy.intercept("PUT", "/api/setting/metabot-limit-unit").as(- "saveLimitUnit",- );- cy.intercept("PUT", "/api/setting/metabot-limit-reset-rate").as(- "saveLimitPeriod",- );- cy.intercept("PUT", "/api/setting/metabot-quota-reached-message").as(- "saveQuotaMessage",- );- });-- it("should clear and save instance limit settings, then enforce a zero limit in chat", () => {- // Pre-set a limit so there's something to clear- cy.request("PUT", "/api/ee/ai-controls/usage/instance", {- max_usage: 100,- });-- cy.visit(AI_USAGE_LIMITS_URL);- cy.wait("@getInstanceLimit");-- cy.findByRole("textbox", {- name: "Total monthly instance token limit",- }).clear();- cy.wait("@updateInstanceLimit").then(({ request }) => {- expect(request.body).to.deep.equal({ max_usage: null });- });-- // Change limit type to messages- cy.findByRole("radio", { name: "By message count" }).click({- force: true,- });- cy.wait("@saveLimitUnit").then(({ request }) => {- expect(request.body).to.deep.equal({ value: "messages" });- });-- // Change reset period to weekly- cy.findByRole("radio", { name: "Weekly" }).click({ force: true });- cy.wait("@saveLimitPeriod").then(({ request }) => {- expect(request.body).to.deep.equal({ value: "weekly" });- });-- // Type an instance limit value- cy.findByLabelText("Total weekly instance message limit").type("500");- cy.wait("@updateInstanceLimit").then(({ request }) => {- expect(request.body).to.deep.equal({ max_usage: 500 });- });-- // Type a quota-reached message- cy.findByLabelText("Quota-reached message")- .clear()- .type("You have hit the AI usage limit.");- cy.wait("@saveQuotaMessage").then(({ request }) => {- expect(request.body).to.deep.equal({- value: "You have hit the AI usage limit.",- });- });- cy.log("Enforce a zero instance limit in a fresh chat");- cy.findByLabelText("Total weekly instance message limit").type(- "{selectall}0",- );- cy.wait("@updateInstanceLimit").then(({ request }) => {- expect(request.body).to.deep.equal({ max_usage: 0 });- });-- H.updateSetting("metabot-enabled?", true);-- cy.intercept("GET", "/api/automagic-dashboards/database/*/candidates").as(- "xrayCandidates",- );- cy.visit("/");- cy.wait("@xrayCandidates");-- H.openMetabotViaSearchButton();- H.sendMetabotMessage("hello");-- H.lastChatMessage().should(- "have.text",- "You have hit the AI usage limit.",- );- });- });-describe("Group limits handling", () => {let groupAId: number;let groupBId: number;@@ -468,27 +194,7 @@ describe("AI controls > AI usage limits", () => {});});- it("should display both groups with their configured limits and respect the effective user limit (max across their groups)", () => {- cy.visit(AI_USAGE_LIMITS_URL);-- // Both groups should appear in the group limits table with correct values- cy.findByLabelText(- "Max messages per user for AI Limit Group A (low)",- ).should("have.value", "5");-- cy.findByLabelText(- "Max messages per user for AI Limit Group B (high)",- ).should("have.value", "100");-- // The section description should explain that users get the highest limit- cy.findByTestId("group-limits-tab").within(() => {- cy.findByText(- /If a user belongs to more than one group, they'll be given the highest limit among all the groups/i,- )- .scrollIntoView()- .should("be.visible");- });-+ it("should respect the effective user limit (max across their groups)", () => {// Normal user is in group A (limit 5) and group B (limit 100).// Effective limit = max(1, 1, 1, 5, 100) = 100.// (pre-existing groups are set to 1 in beforeEach)@@ -572,9 +278,6 @@ describe("AI Controls > Tenant usage limits", () => {cy.intercept("GET", "/api/automagic-dashboards/database/*/candidates").as("xrayCandidates",);- cy.intercept("PUT", "/api/ee/ai-controls/usage/tenant/*").as(- "updateTenantLimit",- );llmMockServerSetup();});@@ -595,25 +298,10 @@ describe("AI Controls > Tenant usage limits", () => {H.lastChatMessage().should("contain.text", MOCK_LLM_RESPONSE);H.lastChatMessage().should("not.contain.text", DEFAULT_QUOTA_MESSAGE);- cy.log(- "tenant limit set to 0 in the UI: the user gets the quota-reached message",- );+ cy.log("tenant limit set to 0: the user gets the quota-reached message");cy.signInAsAdmin();- cy.visit("/admin/metabot/usage-controls/ai-usage-limits");-- cy.findByRole("tab", { name: "Specific tenants" }).click();-- cy.findByTestId("tenant-limits-tab").should("be.visible");- cy.findByTestId("tenant-limits-tab")- .findByText("Test Corp")- .should("be.visible");- cy.findByLabelText("Max total monthly tokens for Test Corp").type("0");- cy.wait("@updateTenantLimit").then(({ request, response }) => {- expect(request.url).to.match(- new RegExp(`/api/ee/ai-controls/usage/tenant/${tenantId}$`),- );- expect(request.body).to.deep.equal({ max_usage: 0 });- expect(response?.statusCode).to.eq(200);+ cy.request("PUT", `/api/ee/ai-controls/usage/tenant/${tenantId}`, {+ max_usage: 0,});// Clear the backend limit-check cache so the new limit is seen immediately,cy.request("DELETE", "/api/testing/metabot/seed-ai-usage", {@@ -640,9 +328,6 @@ describe("AI Controls > Tenant usage limits", () => {}});-// A tiny valid PNG (1×1 transparent pixel), used for the icon upload test-const TINY_PNG_BASE64 =- "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";const MOCK_LLM_PORT = 6123;const MOCK_LLM_RESPONSE = "Hello from mock LLM!";const DEFAULT_QUOTA_MESSAGE =diff --git a/e2e/test/scenarios/metabot/mcp-apps-settings.cy.spec.ts b/e2e/test/scenarios/metabot/mcp-apps-settings.cy.spec.tsindex 0b97467152a..97532e503ae 100644--- a/e2e/test/scenarios/metabot/mcp-apps-settings.cy.spec.ts+++ b/e2e/test/scenarios/metabot/mcp-apps-settings.cy.spec.ts@@ -6,50 +6,15 @@ describe("admin > MCP apps settings > Cursor install link", () => {cy.signInAsAdmin();});- it("shows 'Install in Cursor' link only when the Cursor and VS Code switch is enabled", () => {+ it("keeps the 'Install in Cursor' link above the switch track so it can be hovered and clicked without toggling the switch", () => {cy.visit("/admin/metabot/mcp");H.main().within(() => {cy.findByText("Show inline charts in these MCP clients").scrollIntoView();-- cy.log("link is hidden by default");- cy.findByRole("link", { name: "Install in Cursor" }).should("not.exist");-- cy.log("enable Cursor and VS Code");cy.findByRole("switch", { name: /cursor and vs code/i }).click({force: true,});- cy.log("link appears with a valid Cursor deeplink");- cy.findByRole("link", { name: "Install in Cursor" })- .should("be.visible")- .invoke("attr", "href")- .should((href) => {- expect(href).to.be.a("string");- expect(href).to.match(- /^cursor:\/\/anysphere\.cursor-deeplink\/mcp\/install\?/,- );-- // Unjustified type cast. FIXME- const query = (href as string).split("?", 2)[1];- const params = new URLSearchParams(query);-- expect(params.get("name")).to.eq("Metabase");- const config = params.get("config");-- expect(config).to.be.a("string");-- // Unjustified type cast. FIXME- const decoded = JSON.parse(atob(config as string));-- expect(decoded.url).to.eq(- `${Cypress.config("baseUrl")}/api/metabase-mcp`,- );- });-- cy.log(- "link is hoverable — pointer events reach it (not the switch track)",- );const hoverState = { mouseEntered: false };cy.findByRole("link", { name: "Install in Cursor" }).then(($link) => {@@ -64,7 +29,6 @@ describe("admin > MCP apps settings > Cursor install link", () => {expect(hoverState.mouseEntered).to.be.true;});- cy.log("clicking the link does not toggle the parent switch");cy.findByRole("link", { name: "Install in Cursor" }).then(($link) => {// Prevent the cursor:// deeplink from being followed during the test@@ -75,14 +39,6 @@ describe("admin > MCP apps settings > Cursor install link", () => {cy.findByRole("switch", { name: /cursor and vs code/i }).should("be.checked",);-- cy.log("disable Cursor and VS Code");- cy.findByRole("switch", { name: /cursor and vs code/i }).click({- force: true,- });-- cy.log("link is hidden again");- cy.findByRole("link", { name: "Install in Cursor" }).should("not.exist");});});});diff --git a/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts b/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.tsdeleted file mode 100644index 4887b7dfd2c..00000000000--- a/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts+++ /dev/null@@ -1,91 +0,0 @@-import { SAMPLE_DB_ID } from "e2e/support/cypress_data";-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";--const { ORDERS_ID } = SAMPLE_DATABASE;--const { H } = cy;--const allOrdersQuestion = {- dataset_query: {- database: SAMPLE_DB_ID,- query: { "source-table": ORDERS_ID },- type: "query",- },- display: "table",- visualization_settings: {},-};--describe("Metabot Query Builder", () => {- beforeEach(() => {- H.restore();- cy.signInAsAdmin();- H.activateToken("pro-self-hosted");- H.setupAnthropicLlmProvider();- });-- it("should render errors and the agent's reply inline and support clicking suggested prompts", () => {- cy.visit("/question/ask");- H.metabotChatInput().should("be.visible");-- H.mockMetabotResponse({ body: mockErrorResponse });- H.sendMetabotMessage("Tell me about my data");-- // the full-page conversation uses the nlq profile- cy.wait("@metabotAgent").then(({ request }) => {- expect(request.body.profile_id).to.equal("nlq");- });- // the error renders inline in the full-page conversation...- H.lastChatMessage().should("contain.text", "Something went wrong");-- // ...and we move to the conversation's permalink- cy.url().should("include", "/metabot/conversation/");-- H.mockMetabotResponse({- body: mockTextOnlyResponse("Here's what I found."),- });- H.sendMetabotMessage("Tell me about my data");- H.lastChatMessage().should("have.text", "Here's what I found.");-- cy.log("suggested prompts");- // mock suggested prompts- cy.intercept("GET", "/api/metabot/metabot/*/prompt-suggestions*", {- prompts: [{ prompt: "Show me all orders" }],- });-- // visit AI exploration page- cy.visit("/question/ask");- H.metabotChatInput().should("be.visible");-- // click suggested prompt- H.mockMetabotResponse({- body: mockGeneratedEntityResponse(allOrdersQuestion.dataset_query),- });- cy.get("main").findByText("Show me all orders").click();-- // the chart renders inline rather than in the query builder- cy.wait("@metabotAgent");- cy.findByTestId("metabot-inline-chart").should("be.visible");- cy.findByTestId("qb-header").should("not.exist");- cy.url().should("include", "/metabot/conversation/");- });-});--// Response helpers-const mockTextOnlyResponse = (text: string) =>- H.createMetabotSSEBody(H.metabotTextPart(text));--const mockGeneratedEntityResponse = (datasetQuery: unknown) => {- const value = {- type: "card",- id: "card-1",- title: "All orders",- query: { id: "query-1", query: datasetQuery },- display: "table",- };- return H.createMetabotSSEBody(H.metabotDataPart("generated_entity", value));-};--const mockErrorResponse = H.createMetabotSSEBody(- H.metabotErrorPart("Anthropic API key expired or invalid"),- H.metabotFinishPart("error"),-);diff --git a/e2e/test/scenarios/metabot/metabot.cy.spec.ts b/e2e/test/scenarios/metabot/metabot.cy.spec.tsindex b9e2323c188..0433d39003a 100644--- a/e2e/test/scenarios/metabot/metabot.cy.spec.ts+++ b/e2e/test/scenarios/metabot/metabot.cy.spec.ts@@ -1,5 +1,3 @@-import { ORDERS_BY_YEAR_QUESTION_ID } from "e2e/support/cypress_sample_instance_data";-const { H } = cy;const loremIpsum =@@ -11,7 +9,6 @@ describe("Metabot UI", () => {cy.signInAsAdmin();H.activateToken("pro-self-hosted");H.setupAnthropicLlmProvider();- cy.intercept("POST", "/api/metabot/agent-streaming").as("agentReq");cy.intercept("GET", "/api/automagic-dashboards/database/*/candidates").as("xrayCandidates",);@@ -19,10 +16,16 @@ describe("Metabot UI", () => {describe("scroll management", () => {beforeEach(() => {+ H.resetSnowplow();+ H.enableTracking();cy.visit("/");cy.wait("@xrayCandidates");});+ afterEach(() => {+ H.expectNoBadSnowplowEvents();+ });+it("should size the filler element and manage the scroll position as messages are added", () => {H.openMetabotViaSearchButton();cy.findByTestId("metabot-empty-chat-info").should("be.visible");@@ -87,141 +90,6 @@ describe("Metabot UI", () => {});});});-- it("should not submit a prompt via /metabot/new and redirect /question/ask to the notebook when metabot is disabled", () => {- H.updateSetting("metabot-enabled?", false);- cy.visit("/metabot/new?q=Who%20is%20your%20favorite%3F");- cy.url().should("eq", Cypress.config().baseUrl + "/");- cy.wait("@xrayCandidates");- H.assertChatVisibility("not.visible");- cy.get("@agentReq.all").should("have.length", 0);-- cy.log(- "visiting '/question/ask' should redirect to notebook when metabot is disabled",- );- cy.visit("/question/ask");- cy.url().should("include", "/question#");- cy.findByTestId("metabot-chat").should("not.exist");- });-- describe("metabot events", () => {- beforeEach(() => {- H.resetSnowplow();- H.enableTracking();- });-- afterEach(() => {- H.expectNoBadSnowplowEvents();- });-- describe("Metabot chat", () => {- beforeEach(() => {- cy.visit("/");- cy.wait("@xrayCandidates");- });-- it("should be controlled via keyboard shortcut, be able to be opened and closed, send a message to the agent, handle successful or failed responses, and start a new conversation via /metabot/new", () => {- H.openMetabotViaShortcutKey();- H.expectUnstructuredSnowplowEvent({- event: "metabot_chat_opened",- triggered_from: "keyboard_shortcut",- });- H.closeMetabotViaShortcutKey();- cy.log("We don't track closing the chat via kbd");- H.expectUnstructuredSnowplowEvent(- {- event: "metabot_chat_opened",- triggered_from: "keyboard_shortcut",- },- 1,- );-- H.openMetabotViaSearchButton();- H.expectUnstructuredSnowplowEvent({- event: "metabot_chat_opened",- triggered_from: "header",- });- H.expectUnstructuredSnowplowEvent(- {- event: "metabot_chat_opened",- triggered_from: "keyboard_shortcut",- },- 1,- );- H.closeMetabotViaCloseButton();-- cy.log("send a message and handle successful or failed responses");- H.openMetabotViaSearchButton();- H.chatMessages().should("not.exist");-- H.mockMetabotResponse({- statusCode: 200,- body: whoIsYourFavoriteResponse,- });- H.sendMetabotMessage("Who is your favorite?");- H.expectUnstructuredSnowplowEvent({- event: "metabot_request_sent",- });-- H.lastChatMessage().should("have.text", "You, but don't tell anyone.");-- H.mockMetabotResponse({ statusCode: 200, body: apiKeyInvalidResponse });- H.sendMetabotMessage("Who is your favorite?");- H.lastChatMessage().should("contain.text", "Something went wrong");-- cy.log("start a new conversation via /metabot/new");- H.mockMetabotResponse({- statusCode: 200,- body: whoIsYourFavoriteResponse,- });- cy.visit("/metabot/new?q=Who%20is%20your%20favorite%3F");- H.assertChatVisibility("visible");- H.lastChatMessage().should("have.text", "You, but don't tell anyone.");- });- });- });-});--describe("Metabot in full-app embedding", () => {- beforeEach(() => {- H.restore();- cy.signInAsAdmin();- H.activateToken("pro-self-hosted");- H.setupAnthropicLlmProvider();- });-- it("should show the metabot button only when embedded-metabot-enabled? is true", () => {- cy.intercept("GET", "/api/metabot/permissions/user-permissions").as(- "getUserPermissions",- );-- H.updateEnterpriseSettings({ "embedded-metabot-enabled?": true });-- H.visitFullAppEmbeddingUrl({- url: `/question/${ORDERS_BY_YEAR_QUESTION_ID}`,- qs: {},- });-- cy.wait("@getUserPermissions");- H.appBar().icon("metabot").should("be.visible");-- H.updateEnterpriseSettings({ "embedded-metabot-enabled?": false });- cy.intercept("GET", "/api/metabot/permissions/user-permissions").as(- "getUserPermissionsWhenDisabled",- );-- H.visitFullAppEmbeddingUrl({- url: `/question/${ORDERS_BY_YEAR_QUESTION_ID}`,- qs: {},- });-- cy.log("Wait for the question to render");- H.main().findByText("Filter").should("be.visible");-- cy.log("Assert metabot buttons are not rendered");- H.appBar().icon("metabot").should("not.exist");- cy.get("@getUserPermissionsWhenDisabled.all").should("have.length", 0);- });});const whoIsYourFavoriteResponse = H.createMetabotSSEBody(@@ -232,11 +100,6 @@ const whoIsYourFavoriteResponse = H.createMetabotSSEBody(}),);-const apiKeyInvalidResponse = H.createMetabotSSEBody(- H.metabotErrorPart("Anthropic API key expired or invalid"),- H.metabotFinishPart("error"),-);-function getContainerPadding($container: JQuery<HTMLElement>) {const style = getComputedStyle($container[0]);return {diff --git a/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts b/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.tsdeleted file mode 100644index 82ff7f402e4..00000000000--- a/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts+++ /dev/null@@ -1,262 +0,0 @@-const { H } = cy;--const toggleInlineSQLPrompt = () => {- H.NativeEditor.get().focus();- cy.wait(250);- H.NativeEditor.get().realPress([H.metaKey, "Shift", "I"]);-};--const inlinePrompt = () => cy.findByTestId("metabot-inline-sql-prompt");-const inlinePromptInput = () =>- inlinePrompt().find(".ProseMirror[contenteditable=true]");-const generateButton = () => cy.findByTestId("metabot-inline-sql-generate");-const cancelButton = () => cy.findByTestId("metabot-inline-sql-cancel");-const errorMessage = () => cy.findByTestId("metabot-inline-sql-error");-const acceptButton = () => cy.findByTestId("accept-proposed-changes-button");-const rejectButton = () => cy.findByTestId("reject-proposed-changes-button");-const generatingLoader = () => cy.findByLabelText("Stop generating");--describe("Native SQL generation", () => {- it("should show setup guidance in SQL generation and on the AI exploration page when metabot is not configured", () => {- H.restore();- cy.signInAsAdmin();- H.activateToken("pro-self-hosted");-- H.startNewNativeQuestion();- H.NativeEditor.get().should("be.visible");- toggleInlineSQLPrompt();- inlinePrompt().should("be.visible");- inlinePrompt()- .findByText(/To use SQL generation, please/)- .should("be.visible");- generateButton().should("not.exist");-- cy.findByRole("button", { name: "connect to a model" }).click();- cy.findByTestId("ai-provider-configuration-modal").should("be.visible");-- cy.visit("/question/ask");- cy.url().should("include", "/question/ask");- cy.findByRole("button", { name: "connect to a model" }).should(- "be.visible",- );- cy.findByRole("button", { name: "connect to a model" }).click();- cy.findByTestId("ai-provider-configuration-modal").should("be.visible");- });-- describe("single db", () => {- beforeEach(() => {- H.restore();- cy.signInAsAdmin();- H.activateToken("pro-self-hosted");- H.setupAnthropicLlmProvider({ apiKey: "sk-ant-api03-test-token" });- });-- it("should be able to successfully generate sql and correctly control the input", () => {- H.startNewNativeQuestion({ query: "SELECT 1" });- H.NativeEditor.get().should("be.visible");-- toggleInlineSQLPrompt();- inlinePrompt().should("be.visible");- generateButton().should("be.disabled");-- inlinePromptInput().click();- cy.realType("select all users", { pressDelay: 10 });- generateButton().should("be.enabled");-- H.mockMetabotResponse({- body: mockCodeEditResponse("SELECT * FROM users"),- delay: 100,- });- generateButton().click();- generatingLoader().should("exist");- cy.wait("@metabotAgent").then(({ request }) => {- const codeEditor = request.body.context.user_is_viewing.find(- (ctx: { type: string }) => ctx.type === "code_editor",- );- expect(codeEditor).to.exist;- expect(codeEditor.buffers).to.have.length(1);- expect(codeEditor.buffers[0].source.language).to.eq("sql");- expect(codeEditor.buffers[0].source.database_id).to.eq(1); // Sample Database- });-- // should auto-close input and show diff with accept/reject buttons- inlinePrompt().should("not.exist");- acceptButton().should("be.visible");- rejectButton().should("be.visible");- H.NativeEditor.get().should("contain", "SELECT 1");- H.NativeEditor.get().should("contain", "SELECT * FROM users");-- // should be able to accept changes- acceptButton().click();- acceptButton().should("not.exist");- rejectButton().should("not.exist");- H.NativeEditor.get().should("not.contain", "SELECT 1");- H.NativeEditor.get().should("contain", "SELECT * FROM users");-- cy.log("control the input");- cy.visit("/");- H.startNewNativeQuestion();- H.NativeEditor.get().should("be.visible");-- // open / close the editor via key press- inlinePrompt().should("not.exist");- toggleInlineSQLPrompt();- inlinePrompt().should("be.visible");- toggleInlineSQLPrompt();- inlinePrompt().should("not.exist");-- // cancel button should close- toggleInlineSQLPrompt();- inlinePrompt().should("be.visible");- cancelButton().click();- inlinePrompt().should("not.exist");-- // cancel button should cancel inflight request- toggleInlineSQLPrompt();- inlinePromptInput().click();- cy.realType("select all users", { pressDelay: 10 });- H.mockMetabotResponse({- body: mockCodeEditResponse("SELECT * FROM users"),- delay: 1000,- });- generateButton().click();- generatingLoader().should("exist");- cancelButton().click();- cy.get("@metabotAgent").its("state").should("eq", "Errored");- inlinePrompt().should("not.exist");- acceptButton().should("not.exist");- });- });-- describe("multi-db", () => {- beforeEach(() => {- H.restore("postgres-12");- cy.signInAsAdmin();- H.activateToken("pro-self-hosted");- H.setupAnthropicLlmProvider({ apiKey: "sk-ant-api03-test-token" });- });-- it("should show error if no code_edit is received and manage conversation state correctly", () => {- H.startNewNativeQuestion();- H.NativeEditor.get().should("be.visible");-- toggleInlineSQLPrompt();- inlinePromptInput().click();- cy.realType("do something", { pressDelay: 10 });- H.mockMetabotResponse({- body: mockTextOnlyResponse("I can help with that!"),- });- generateButton().click();- cy.wait("@metabotAgent");-- errorMessage()- .should("be.visible")- .and(- "have.text",- "Sorry, I ran into an error. Could you please try that again?",- );- acceptButton().should("not.exist");-- cy.log("manage conversation state");- cy.visit("/");- H.startNewNativeQuestion({ query: "SELECT 1" });- H.NativeEditor.get().should("be.visible");-- // open input, send a prompt, get suggestion back- toggleInlineSQLPrompt();- inlinePromptInput().click();- cy.realType("select all users", { pressDelay: 10 });- H.mockMetabotResponse({- body: mockCodeEditResponse("SELECT * FROM users"),- });- generateButton().click();- cy.wait("@metabotAgent");- acceptButton().should("be.visible");-- // reject the change and open input again- rejectButton().click();- acceptButton().should("not.exist");- H.NativeEditor.get().should("contain", "SELECT 1");- H.NativeEditor.get().should("not.contain", "SELECT * FROM users");- H.NativeEditor.focus();- toggleInlineSQLPrompt();- inlinePrompt().should("be.visible");-- // send another message, history should contain rejection info- inlinePromptInput().click();- cy.realType("try again", { pressDelay: 10 });- H.mockMetabotResponse({- body: mockCodeEditResponse("SELECT id FROM users"),- });- generateButton().click();- cy.wait("@metabotAgent").then(({ request }) => {- expect(request.body.parent_message_id).to.be.a("string");- expect(request.body.message).to.include(- "User rejected the following suggestion:\n\nSELECT * FROM users",- );- });- acceptButton().should("be.visible");-- // change the selected database, should keep the input open- rejectButton().click();- toggleInlineSQLPrompt();- inlinePrompt().should("be.visible");- H.NativeEditor.selectDataSource("QA Postgres12");- inlinePrompt().should("be.visible");-- // changing the database starts a fresh conversation, so there's no parent- inlinePromptInput().click();- cy.realType("select something", { pressDelay: 10 });- H.mockMetabotResponse({- body: mockCodeEditResponse("SELECT 123"),- });- generateButton().click();- cy.wait("@metabotAgent").then(({ request }) => {- expect(request.body.parent_message_id).to.be.undefined;- });-- // should get a valid response back- acceptButton().should("be.visible");- H.NativeEditor.get().should("contain", "SELECT 123");-- // leave the page, go to new SQL page and send a new prompt- acceptButton().click();- cy.findByTestId("main-logo-link").click();- H.modal().button("Discard changes").click();- cy.location("pathname").should("eq", "/");- H.newButton("SQL query").click();- H.NativeEditor.get().should("be.visible");- toggleInlineSQLPrompt();- inlinePromptInput().click();- cy.realType("new prompt", { pressDelay: 10 });- H.mockMetabotResponse({- body: mockCodeEditResponse("SELECT 456"),- });- generateButton().click();- cy.wait("@metabotAgent").then(({ request }) => {- expect(request.body.parent_message_id).to.be.undefined;- });- acceptButton().should("be.visible");-- // manually editing the editor should dismiss suggestion buttons- H.NativeEditor.get().click();- H.NativeEditor.get().realPress([H.metaKey, "a"]);- H.NativeEditor.get().realPress("Backspace");- acceptButton().should("not.exist");- rejectButton().should("not.exist");- });- });-});--// Response helpers-const mockCodeEditResponse = (sql: string) =>- H.createMetabotSSEBody(- H.metabotDataPart("code_edit", {- buffer_id: "qb",- mode: "rewrite",- value: sql,- }),- );--const mockTextOnlyResponse = (text: string) =>- H.createMetabotSSEBody(H.metabotTextPart(text));diff --git a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/AiFeatureAccessPage/AiFeatureAccessPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/AiFeatureAccessPage/AiFeatureAccessPage.unit.spec.tsxindex a4263d9d07b..e0a63b2df9c 100644--- a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/AiFeatureAccessPage/AiFeatureAccessPage.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/AiFeatureAccessPage/AiFeatureAccessPage.unit.spec.tsx@@ -1,6 +1,7 @@import userEvent from "@testing-library/user-event";import fetchMock from "fetch-mock";+import { findRequestBodies } from "__support__/server-mocks";import { setupGroupsEndpoint } from "__support__/server-mocks/group";import {setupMetabotGroupPermissionsEndpoint,@@ -98,6 +99,13 @@ function getPermissionRow(groupName: string) {});}+function getPermissionUpdateBodies() {+ return findRequestBodies({+ method: "PUT",+ urlSuffix: "/api/ee/ai-controls/permissions",+ });+}+function getSwitchModeButton() {return screen.getByRole("button", {name: "Switch to group-level permissions",@@ -279,6 +287,43 @@ describe("AiFeatureAccessPage", () => {});});+ it("saves the All Users permissions when metabot is toggled off and on in simple mode", async () => {+ setup();+ await screen.findByTestId("ai-feature-access-table");++ const allUsersRow = getPermissionRow("All Users");+ const metabotSwitch = within(allUsersRow).getByRole("switch");+ await waitFor(() => expect(metabotSwitch).toBeChecked());++ await userEvent.click(metabotSwitch);+ await waitFor(async () =>+ expect(await getPermissionUpdateBodies()).toHaveLength(1),+ );+ expect((await getPermissionUpdateBodies())[0]).toEqual({+ permissions: expect.arrayContaining(+ createMockMetabotGroupPermissions(allUsersGroup.id, {+ [AIToolKey.Metabot]: "no",+ [AIToolKey.ChatAndNLQ]: "no",+ [AIToolKey.SQLGeneration]: "no",+ [AIToolKey.OtherTools]: "no",+ }),+ ),+ });++ await userEvent.click(metabotSwitch);+ await waitFor(async () =>+ expect(await getPermissionUpdateBodies()).toHaveLength(2),+ );+ expect((await getPermissionUpdateBodies())[1]).toEqual({+ permissions: expect.arrayContaining(+ createMockMetabotGroupPermissions(allUsersGroup.id),+ ),+ });+ within(allUsersRow)+ .getAllByRole("checkbox")+ .forEach((checkbox) => expect(checkbox).toBeChecked());+ });+it("auto-enables metabot when a tool is checked while metabot is off", async () => {setup({ advanced: true });await screen.findByTestId("ai-feature-access-table");diff --git a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotCustomizationPage/MetabotCustomizationPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotCustomizationPage/MetabotCustomizationPage.unit.spec.tsxindex 459037e56d0..750550bbcf8 100644--- a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotCustomizationPage/MetabotCustomizationPage.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotCustomizationPage/MetabotCustomizationPage.unit.spec.tsx@@ -2,11 +2,14 @@ import userEvent from "@testing-library/user-event";import fetchMock from "fetch-mock";import {+ findRequestBodies,setupSettingsEndpoints,setupStatefulSettingsEndpoints,} from "__support__/server-mocks";import { mockSettings } from "__support__/settings";import { renderWithProviders, screen, waitFor } from "__support__/ui";+import { checkNotNull } from "metabase/utils/types";+import type { EnterpriseSettingKey } from "metabase-types/api";import { createMockSettings } from "metabase-types/api/mocks";import { MetabotCustomizationPage } from "./MetabotCustomizationPage";@@ -37,7 +40,34 @@ function setup({});}+function getSettingUpdateBodies(settingKey: EnterpriseSettingKey) {+ return findRequestBodies({+ method: "PUT",+ urlSuffix: `/api/setting/${settingKey}`,+ });+}++// jsdom does not decode images, so fire the load event that a browser fires+// for an intact image.+function mockImageDecoding() {+ return jest+ .spyOn(HTMLImageElement.prototype, "src", "set")+ .mockImplementation(function (this: HTMLImageElement) {+ setTimeout(() => this.dispatchEvent(new Event("load")));+ });+}++function getIconFileInput() {+ return checkNotNull(+ document.querySelector<HTMLInputElement>('input[type="file"]'),+ );+}+describe("MetabotCustomizationPage", () => {+ afterEach(() => {+ jest.restoreAllMocks();+ });+it("renders the name input with the current metabot name", async () => {setup({ metabotName: "My AI Assistant" });@@ -77,6 +107,56 @@ describe("MetabotCustomizationPage", () => {expect(preview).toHaveAttribute("src", "data:image/png;base64,abc123");});+ it("saves the name once after the user stops typing", async () => {+ setup({ metabotName: "Metabot" });++ const nameInput = await screen.findByLabelText("AI agent's name");+ await userEvent.clear(nameInput);+ await userEvent.type(nameInput, "HAL 9000");++ await waitFor(async () =>+ expect(await getSettingUpdateBodies("metabot-name")).toEqual([+ { value: "HAL 9000" },+ ]),+ );+ });++ it("saves an uploaded icon and offers to remove it", async () => {+ mockImageDecoding();+ setup({ metabotIcon: "metabot" });++ await screen.findByText("AI agent's icon");+ await userEvent.upload(+ getIconFileInput(),+ new File(["icon"], "metabot-icon.png", { type: "image/png" }),+ );++ await waitFor(async () =>+ expect(await getSettingUpdateBodies("metabot-icon")).toEqual([+ { value: expect.stringMatching(/^data:image\/png;base64,/) },+ ]),+ );+ expect(+ await screen.findByRole("button", { name: /Remove custom icon/ }),+ ).toBeInTheDocument();+ });++ it("saves the illustrations setting when it is turned off", async () => {+ setup({ showIllustrations: true });++ const toggle = await screen.findByRole("switch", {+ name: /Show Metabot illustrations/,+ });+ await waitFor(() => expect(toggle).toBeChecked());+ await userEvent.click(toggle);++ await waitFor(async () =>+ expect(+ await getSettingUpdateBodies("metabot-show-illustrations"),+ ).toEqual([{ value: false }]),+ );+ });+it("keeps the illustrations toggle after turning illustrations on with the default icon", async () => {setup({ showIllustrations: false });diff --git a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotSystemPromptsPage/MetabotSystemPromptsPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotSystemPromptsPage/MetabotSystemPromptsPage.unit.spec.tsxindex 2649d86af3b..57f0d24b719 100644--- a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotSystemPromptsPage/MetabotSystemPromptsPage.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotSystemPromptsPage/MetabotSystemPromptsPage.unit.spec.tsx@@ -94,6 +94,21 @@ describe("MetabotSystemPromptsPage", () => {expect(screen.getByText(title)).toBeInTheDocument();expect(textarea).toHaveValue("Existing value");});++ it("saves the prompt to its own setting on blur", async () => {+ const { textarea } = await setup({ Component, settingKey, title });++ await userEvent.type(textarea, "Always use uppercase SQL keywords.");+ fireEvent.blur(textarea);++ await waitFor(() =>+ expect(getUpdateCallsFor(settingKey)).toHaveLength(1),+ );+ const [call] = getUpdateCallsFor(settingKey);+ expect(await call?.request?.json()).toEqual({+ value: "Always use uppercase SQL keywords.",+ });+ });});describe("MetabotChatPromptPage save behavior", () => {diff --git a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GeneralLimitsSettingsSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GeneralLimitsSettingsSection.unit.spec.tsxindex b1cc59258d8..013d8fcfab1 100644--- a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GeneralLimitsSettingsSection.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GeneralLimitsSettingsSection.unit.spec.tsx@@ -1,6 +1,7 @@import userEvent from "@testing-library/user-event";import {+ findRequestBodies,setupPropertiesEndpoints,setupSettingsEndpoints,setupUpdateSettingEndpoint,@@ -11,7 +12,11 @@ import {} from "__support__/server-mocks/metabot";import { createMockSettingsState } from "__support__/state";import { renderWithProviders, screen, waitFor } from "__support__/ui";-import type { MetabotLimitPeriod, MetabotLimitType } from "metabase-types/api";+import type {+ EnterpriseSettingKey,+ MetabotLimitPeriod,+ MetabotLimitType,+} from "metabase-types/api";import { createMockSettings } from "metabase-types/api/mocks";import { GeneralLimitsSettingsSection } from "./GeneralLimitsSettingsSection";@@ -48,6 +53,20 @@ async function setup({await screen.findByText("How do you want to limit AI usage?");}+function getSettingUpdateBodies(settingKey: EnterpriseSettingKey) {+ return findRequestBodies({+ method: "PUT",+ urlSuffix: `/api/setting/${settingKey}`,+ });+}++function getInstanceLimitUpdateBodies() {+ return findRequestBodies({+ method: "PUT",+ urlSuffix: "/api/ee/ai-controls/usage/instance",+ });+}+describe("GeneralLimitsSettingsSection", () => {it("renders the limit type segmented control with saved value", async () => {await setup({ limitType: "messages" });@@ -151,6 +170,74 @@ describe("GeneralLimitsSettingsSection", () => {).toBeInTheDocument();});+ it("saves the limit type when the user picks another option", async () => {+ await setup({ limitType: "tokens" });+ await userEvent.click(screen.getByText("By message count"));++ await waitFor(async () =>+ expect(await getSettingUpdateBodies("metabot-limit-unit")).toEqual([+ { value: "messages" },+ ]),+ );+ });++ it("saves the reset period and relabels the instance limit when the user picks another period", async () => {+ await setup({ limitType: "messages", limitPeriod: "monthly" });+ await userEvent.click(screen.getByText("Weekly"));++ expect(+ await screen.findByLabelText("Total weekly instance message limit"),+ ).toBeInTheDocument();+ await waitFor(async () =>+ expect(await getSettingUpdateBodies("metabot-limit-reset-rate")).toEqual([+ { value: "weekly" },+ ]),+ );+ });++ it("saves the instance limit the user types", async () => {+ await setup({ limitType: "messages" });+ const input = await screen.findByLabelText(+ "Total monthly instance message limit",+ );++ await userEvent.type(input, "500");++ await waitFor(async () =>+ expect(await getInstanceLimitUpdateBodies()).toEqual([+ { max_usage: 500 },+ ]),+ );+ });++ it("removes the instance limit when the user clears it", async () => {+ await setup({ limitType: "messages", instanceMaxUsage: 100 });+ const input = await screen.findByDisplayValue("100");++ await userEvent.clear(input);++ await waitFor(async () =>+ expect(await getInstanceLimitUpdateBodies()).toEqual([+ { max_usage: null },+ ]),+ );+ });++ it("saves the quota-reached message the user types", async () => {+ await setup();++ await userEvent.type(+ screen.getByLabelText("Quota-reached message"),+ "You have hit the AI usage limit.",+ );++ await waitFor(async () =>+ expect(+ await getSettingUpdateBodies("metabot-quota-reached-message"),+ ).toEqual([{ value: "You have hit the AI usage limit." }]),+ );+ });+it("updates reset period setting when user clicks a different option", async () => {await setup({ limitPeriod: "monthly" });expect(screen.getByRole("radio", { name: "Monthly" })).toBeChecked();diff --git a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GroupLimitsSettingsSection/GroupLimitsTab/GroupLimitsTab.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GroupLimitsSettingsSection/GroupLimitsTab/GroupLimitsTab.unit.spec.tsxindex fc8e58ae5de..93c0a010621 100644--- a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GroupLimitsSettingsSection/GroupLimitsTab/GroupLimitsTab.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GroupLimitsSettingsSection/GroupLimitsTab/GroupLimitsTab.unit.spec.tsx@@ -119,6 +119,16 @@ describe("GroupLimitsTab", () => {expect(screen.getByText("Tenant group")).toBeInTheDocument();});+ it("explains that a user in several groups gets the highest group limit", () => {+ setup();++ expect(+ screen.getByText(+ /If a user belongs to more than one group, they'll be given the highest limit among all the groups they belong to\./,+ ),+ ).toBeInTheDocument();+ });+it("populates inputs from existing group limits", () => {setup({groupLimits: [diff --git a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GroupLimitsSettingsSection/TenantLimitsTab/TenantLimitsTab.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GroupLimitsSettingsSection/TenantLimitsTab/TenantLimitsTab.unit.spec.tsxindex d76b1ba362c..c0d8c5e1653 100644--- a/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GroupLimitsSettingsSection/TenantLimitsTab/TenantLimitsTab.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/ai-controls/pages/MetabotUsageLimitsPage/GroupLimitsSettingsSection/TenantLimitsTab/TenantLimitsTab.unit.spec.tsx@@ -1,5 +1,6 @@import userEvent from "@testing-library/user-event";+import { findRequests } from "__support__/server-mocks";import { setupUpdateAIControlsTenantLimitEndpoint } from "__support__/server-mocks/metabot";import { renderWithProviders, screen, waitFor } from "__support__/ui";import type {@@ -158,6 +159,32 @@ describe("TenantLimitsTab", () => {expect(acmeInput).toHaveValue("300");});+ it("saves a zero limit for the tenant the user edits", async () => {+ setup();++ await userEvent.type(+ screen.getByLabelText("Max total monthly tokens for Acme Corp"),+ "0",+ );++ await waitFor(async () => {+ const requests = await findRequests("PUT");+ expect(+ requests.map(({ url, body }): { url: string; body: unknown } => ({+ url,+ body,+ })),+ ).toEqual([+ {+ url: expect.stringMatching(+ new RegExp(`/api/ee/ai-controls/usage/tenant/${tenant1.id}$`),+ ),+ body: { max_usage: 0 },+ },+ ]);+ });+ });+it("shows error when value exceeds the instance limit", async () => {setup({ instanceLimit: 100 });diff --git a/enterprise/frontend/src/metabase-enterprise/ai-controls/routes.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/ai-controls/routes.unit.spec.tsxnew file mode 100644index 00000000000..527d0b4dd40--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/ai-controls/routes.unit.spec.tsx@@ -0,0 +1,55 @@+import {+ setupPropertiesEndpoints,+ setupSettingsEndpoints,+} from "__support__/server-mocks";+import { createMockSettingsState } from "__support__/state";+import { renderWithProviders, screen } from "__support__/ui";+import { Route } from "metabase/router";+import { createMockSettings } from "metabase-types/api/mocks";++import { getAiControlsRoutes } from "./routes";++interface SetupOpts {+ initialRoute: string;+}++function setup({ initialRoute }: SetupOpts) {+ setupPropertiesEndpoints(createMockSettings());+ setupSettingsEndpoints([]);++ renderWithProviders(+ <Route path="/admin/metabot">{getAiControlsRoutes()}</Route>,+ {+ withRouter: true,+ initialRoute,+ storeInitialState: {+ settings: createMockSettingsState({+ "llm-metabot-configured?": true,+ }),+ },+ },+ );+}++describe("getAiControlsRoutes", () => {+ it.each([+ {+ path: "/admin/metabot/system-prompts/metabot-chat",+ heading: "AI chat prompt instructions",+ },+ {+ path: "/admin/metabot/system-prompts/natural-language-queries",+ heading: "Natural language query prompt instructions",+ },+ {+ path: "/admin/metabot/system-prompts/sql-generation",+ heading: "SQL generation prompt instructions",+ },+ ])("renders the $heading page at $path", async ({ path, heading }) => {+ setup({ initialRoute: path });++ expect(+ await screen.findByRole("heading", { name: heading, level: 1 }),+ ).toBeInTheDocument();+ });+});diff --git a/frontend/src/metabase/admin/ai/McpAppsSettings.unit.spec.tsx b/frontend/src/metabase/admin/ai/McpAppsSettings.unit.spec.tsxindex 3cc54de1a93..75ab8ce27d2 100644--- a/frontend/src/metabase/admin/ai/McpAppsSettings.unit.spec.tsx+++ b/frontend/src/metabase/admin/ai/McpAppsSettings.unit.spec.tsx@@ -123,6 +123,31 @@ describe("McpAppsSettings", () => {expect(await screen.findByRole("link", { name: "Install in Cursor" }),).toBeInTheDocument();++ await userEvent.click(+ screen.getByRole("switch", { name: "Cursor and VS Code" }),+ );++ await waitFor(() => {+ expect(+ screen.queryByRole("link", { name: "Install in Cursor" }),+ ).not.toBeInTheDocument();+ });+ });++ it("points 'Install in Cursor' to a Cursor deeplink that installs this instance's MCP server", async () => {+ await setup({ commonOrigins: ["cursor-vscode"] });++ const href = (+ await screen.findByRole("link", { name: "Install in Cursor" })+ ).getAttribute("href");+ const [base, query] = (href ?? "").split("?");+ const params = new URLSearchParams(query);+ const config: unknown = JSON.parse(atob(params.get("config") ?? ""));++ expect(base).toBe("cursor://anysphere.cursor-deeplink/mcp/install");+ expect(params.get("name")).toBe("Metabase");+ expect(config).toEqual({ url: MCP_URL });});it("updates custom inline chart origins on blur", async () => {diff --git a/frontend/src/metabase/admin/ai/MetabotNavPane.unit.spec.tsx b/frontend/src/metabase/admin/ai/MetabotNavPane.unit.spec.tsxindex 22b0334461a..a34961fc434 100644--- a/frontend/src/metabase/admin/ai/MetabotNavPane.unit.spec.tsx+++ b/frontend/src/metabase/admin/ai/MetabotNavPane.unit.spec.tsx@@ -173,6 +173,27 @@ describe("MetabotNavPane", () => {]);});+ it("links each system prompt page from the System prompts folder", async () => {+ setup({+ aiControlsEnabled: true,+ isConfigured: true,+ initialRoute: "/admin/metabot/system-prompts/metabot-chat",+ });++ expect(+ await screen.findByRole("link", { name: "AI chat" }),+ ).toHaveAttribute("href", "/admin/metabot/system-prompts/metabot-chat");+ expect(+ screen.getByRole("link", { name: "Natural language queries" }),+ ).toHaveAttribute(+ "href",+ "/admin/metabot/system-prompts/natural-language-queries",+ );+ expect(+ screen.getByRole("link", { name: "SQL generation" }),+ ).toHaveAttribute("href", "/admin/metabot/system-prompts/sql-generation");+ });+it("no longer exposes the Auditing folder or CLI analytics with audit_app", async () => {setup({ aiControlsEnabled: true, auditAppEnabled: true });diff --git a/frontend/src/metabase/metabot/components/MetabotAppBarButton.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotAppBarButton.unit.spec.tsxindex 7103bdb4d64..98accec7bfc 100644--- a/frontend/src/metabase/metabot/components/MetabotAppBarButton.unit.spec.tsx+++ b/frontend/src/metabase/metabot/components/MetabotAppBarButton.unit.spec.tsx@@ -4,7 +4,8 @@ import fetchMock from "fetch-mock";import { setupEnterprisePlugins } from "__support__/enterprise";import { mockSettings } from "__support__/settings";import { createMockState } from "__support__/state";-import { renderWithProviders, screen, waitFor } from "__support__/ui";+import { renderWithProviders, screen, waitFor, within } from "__support__/ui";+import * as Analytics from "metabase/analytics";import { Route } from "metabase/router";import type { UserMetabotPermissions } from "metabase-types/api";import { createMockUserMetabotPermissions } from "metabase-types/api/mocks";@@ -13,17 +14,23 @@ import { MetabotProvider } from "../context";import { MetabotAppBarButton } from "./MetabotAppBarButton";+interface SetupOpts {+ isMetabotEnabled?: boolean;+ isConfigured?: boolean;+ permissionOverrides?: Partial<UserMetabotPermissions>;+ pathname?: string;+ metabotName?: string;+ metabotIcon?: string;+}+function setup({isMetabotEnabled = true,isConfigured = true,permissionOverrides,pathname = "/",-}: {- isMetabotEnabled?: boolean;- isConfigured?: boolean;- permissionOverrides?: Partial<UserMetabotPermissions>;- pathname?: string;-} = {}) {+ metabotName = "Metabot",+ metabotIcon = "metabot",+}: SetupOpts = {}) {fetchMock.get("path:/api/metabot/permissions/user-permissions",createMockUserMetabotPermissions(permissionOverrides),@@ -32,6 +39,8 @@ function setup({const settings = mockSettings({"llm-metabot-configured?": isConfigured,"metabot-enabled?": isMetabotEnabled,+ "metabot-name": metabotName,+ "metabot-icon": metabotIcon,});setupEnterprisePlugins();@@ -124,4 +133,37 @@ describe("MetabotAppBarButton", () => {const newState = store.getState() as any;expect(newState.metabot.agents.omnibot.visible).toBe(true);});++ it("should track opening the chat from the header, but not closing it", async () => {+ const trackEvent = jest.mocked(Analytics.trackSimpleEvent);+ trackEvent.mockClear();+ setup();+ const button = await screen.findByRole("button", {+ name: /Chat with Metabot/,+ });++ await userEvent.click(button);+ expect(trackEvent).toHaveBeenCalledTimes(1);+ expect(trackEvent).toHaveBeenCalledWith({+ event: "metabot_chat_opened",+ triggered_from: "header",+ });++ await userEvent.click(button);+ expect(trackEvent).toHaveBeenCalledTimes(1);+ });++ it("should use the custom metabot name and icon", async () => {+ setup({+ metabotName: "HAL 9000",+ metabotIcon: "data:image/png;base64,iVBORw0KGgo=",+ });++ const button = await screen.findByRole("button", {+ name: /Chat with HAL 9000/,+ });+ expect(+ within(button).getByRole("img", { name: "HAL 9000" }),+ ).toHaveAttribute("src", "data:image/png;base64,iVBORw0KGgo=");+ });});diff --git a/frontend/src/metabase/metabot/components/MetabotAsk/MetabotAsk.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotAsk/MetabotAsk.unit.spec.tsxindex e43c557f0e6..22ebdb85f27 100644--- a/frontend/src/metabase/metabot/components/MetabotAsk/MetabotAsk.unit.spec.tsx+++ b/frontend/src/metabase/metabot/components/MetabotAsk/MetabotAsk.unit.spec.tsx@@ -1,11 +1,21 @@import userEvent from "@testing-library/user-event";import { assocIn } from "icepick";-import { createMockMetabotTextMessage } from "__support__/server-mocks";+import {+ createMockMetabotGeneratedCardPart,+ createMockMetabotTextMessage,+ setupCardDataset,+ setupDatabaseEndpoints,+ setupLlmProviderTypesEndpoint,+ setupLlmProvidersEndpoint,+ setupTableEndpoints,+} from "__support__/server-mocks";import { screen, waitFor } from "__support__/ui";import { getMetabotVisible } from "metabase/metabot/state";import {+ createMockDatabase,createMockMetabotConversation,+ createMockTable,createMockUser,} from "metabase-types/api/mocks";@@ -13,6 +23,9 @@ import {conversationIdForAgent,createTestMetabotState,enterChatMessage,+ erroredResponse,+ lastChatMessage,+ lastReqBody,mockAgentEndpoint,setup,testConversationId,@@ -21,6 +34,11 @@ import {import { MetabotAsk } from "./MetabotAsk";+jest.mock("metabase/visualizations/components/Visualization", () => ({+ __esModule: true,+ default: () => <div data-testid="visualization" />,+}));+const greetingTitle =/What would you like to know\?|What do you want to explore\?|What are you looking to learn\?/;@@ -69,6 +87,87 @@ describe("MetabotAsk", () => {expect(screen.queryByText(greetingTitle)).not.toBeInTheDocument();});+ it("sends messages with the nlq profile", async () => {+ setupMetabotAsk();+ const agentSpy = mockAgentEndpoint({ events: whoIsYourFavoriteResponse });++ await enterChatMessage("Who is your favorite?");++ const body: unknown = await lastReqBody(agentSpy);+ expect(body).toMatchObject({+ message: "Who is your favorite?",+ profile_id: "nlq",+ });+ });++ it("shows a failed response inline in the conversation", async () => {+ setupMetabotAsk();+ mockAgentEndpoint({ events: erroredResponse });++ await enterChatMessage("Tell me about my data");++ await waitFor(async () => {+ expect(await lastChatMessage()).toHaveTextContent(/Something went wrong/);+ });+ expect(screen.getByTestId("metabot-chat")).toBeInTheDocument();+ });++ it("sends a suggested prompt when it is clicked", async () => {+ setupMetabotAsk({+ promptSuggestions: [{ prompt: "Show me all orders" }],+ });+ const agentSpy = mockAgentEndpoint({ events: whoIsYourFavoriteResponse });++ await userEvent.click(await screen.findByText("Show me all orders"));++ const body: unknown = await lastReqBody(agentSpy);+ expect(body).toMatchObject({ message: "Show me all orders" });+ expect(agentSpy).toHaveBeenCalledTimes(1);+ });++ it("renders a streamed chart inline without leaving the conversation", async () => {+ const TABLE_ID = 2;+ const conversationRoute = `/metabot/conversation/${ASK_CONVERSATION_ID}`;+ setupCardDataset();+ setupDatabaseEndpoints(createMockDatabase({ id: 1 }));+ setupTableEndpoints(createMockTable({ id: TABLE_ID }));+ const { router } = setupMetabotAsk({+ withRouter: true,+ initialRoute: conversationRoute,+ });+ mockAgentEndpoint({+ events: [createMockMetabotGeneratedCardPart({ tableId: TABLE_ID }).part],+ });++ await enterChatMessage("Show me all orders");++ expect(+ await screen.findByTestId("metabot-inline-chart"),+ ).toBeInTheDocument();+ expect(router?.location.pathname).toBe(conversationRoute);+ });++ it("opens the AI provider setup from the greeting when not configured", async () => {+ setupLlmProviderTypesEndpoint();+ setupLlmProvidersEndpoint();+ setupMetabotAsk({+ currentUser: createMockUser({ is_superuser: true }),+ isConfigured: false,+ });++ expect(+ screen.queryByTestId("ai-provider-configuration-modal"),+ ).not.toBeInTheDocument();++ await userEvent.click(+ await screen.findByRole("button", { name: "connect to a model" }),+ );++ expect(+ await screen.findByTestId("ai-provider-configuration-modal"),+ ).toBeInTheDocument();+ });+it("replaces the untitled placeholder with the generated title", async () => {setupMetabotAsk();mockAgentEndpoint({diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.unit.spec.tsxindex 5255d474bba..1be9f8dbb80 100644--- a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.unit.spec.tsx+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.unit.spec.tsx@@ -1,7 +1,12 @@+import userEvent from "@testing-library/user-event";import type { ComponentProps } from "react";import { setupEnterprisePlugins } from "__support__/enterprise";-import { setupUserMetabotPermissionsEndpoint } from "__support__/server-mocks";+import {+ setupLlmProviderTypesEndpoint,+ setupLlmProvidersEndpoint,+ setupUserMetabotPermissionsEndpoint,+} from "__support__/server-mocks";import { mockSettings } from "__support__/settings";import { createMockState } from "__support__/state";import { renderWithProviders, screen } from "__support__/ui";@@ -77,6 +82,34 @@ describe("MetabotInlineSQLPrompt", () => {).toBeInTheDocument();});+ it("opens the AI provider configuration modal from the configure link", async () => {+ setupLlmProvidersEndpoint();+ setupLlmProviderTypesEndpoint();+ setup();++ await userEvent.click(+ await screen.findByRole("button", { name: "connect to a model" }),+ );++ expect(+ await screen.findByTestId("ai-provider-configuration-modal"),+ ).toBeInTheDocument();+ });++ it("disables the generate button until a prompt is typed", async () => {+ const { rerender } = setup({ isConfigured: true });++ expect(+ await screen.findByTestId("metabot-inline-sql-generate"),+ ).toBeDisabled();++ rerender({ value: " " });+ expect(screen.getByTestId("metabot-inline-sql-generate")).toBeDisabled();++ rerender({ value: "select all users" });+ expect(screen.getByTestId("metabot-inline-sql-generate")).toBeEnabled();+ });+it("shows the admin message when the user cannot configure AI", async () => {setup({ isAdmin: false });diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.unit.spec.tsxindex 9de8ad563a0..a1b7966c223 100644--- a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.unit.spec.tsx+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.unit.spec.tsx@@ -1,7 +1,7 @@jest.unmock("@uiw/react-codemirror");import userEvent from "@testing-library/user-event";-import type { ReactElement } from "react";+import { type ReactElement, useState } from "react";import { setupEnterprisePlugins } from "__support__/enterprise";import { createMockMetadataFromState } from "__support__/metadata";@@ -9,24 +9,34 @@ import {setupCollectionsEndpoints,setupDatabasesEndpoints,setupNativeQuerySnippetEndpoints,+ setupSearchEndpoints,setupUserMetabotPermissionsEndpoint,} from "__support__/server-mocks";import { mockSettings } from "__support__/settings";import { createMockState } from "__support__/state";import { createMockEntitiesState } from "__support__/store";-import { renderWithProviders, screen, waitFor, within } from "__support__/ui";+import {+ fireEvent,+ renderWithProviders,+ screen,+ waitFor,+ within,+} from "__support__/ui";import type { SSEEvent } from "metabase/api/ai-streaming/sse-types";import { MetabotChainOfThought } from "metabase/metabot/components/MetabotChat/MetabotChainOfThought";import { MetabotProvider } from "metabase/metabot/context";import {erroredResponse,+ lastReqBody,mockAgentEndpoint,} from "metabase/metabot/tests/utils";+import { getDiffControls } from "metabase/querying/components/CodeMirrorEditor/test-utils";import { NativeQueryEditor } from "metabase/querying/components/NativeQueryEditor/NativeQueryEditor";import { checkNotNull } from "metabase/utils/types";import type Question from "metabase-lib/v1/Question";import {createMockCard,+ createMockDatabase,createMockNativeDatasetQuery,createMockUser,} from "metabase-types/api/mocks";@@ -45,7 +55,10 @@ jest.mock(const TEST_DB = createSampleDatabase();+const OTHER_DB = createMockDatabase({ id: 2, name: "Other Database" });+const TEST_CARD = createMockCard({+ id: 1,dataset_query: createMockNativeDatasetQuery({database: TEST_DB.id,native: { query: "SELECT 1" },@@ -64,6 +77,20 @@ const LONG_REASONING_WORD_COUNT = 1500;// Keep it at most a third of the replay time above, and below the 30 s jest timeout.const SQL_ARRIVAL_TIMEOUT_MS = 5000;+const codeEditResponse = (sql: string): SSEEvent[] => [+ { type: "start", messageId: `msg-${sql}` },+ {+ type: "data-code_edit",+ data: { buffer_id: "qb", mode: "rewrite", value: sql },+ },+];++const TEXT_ONLY_RESPONSE: SSEEvent[] = [+ { type: "text-start", id: "t1" },+ { type: "text-delta", id: "t1", delta: "I can help with that!" },+ { type: "text-end", id: "t1" },+];+const LONG_REASONING_THEN_CODE_EDIT_RESPONSE: SSEEvent[] = [{ type: "reasoning-start", id: "r1" },{@@ -89,21 +116,31 @@ const OTHER_CONVERSATION_REASONING = ();function TestEditor({ question }: { question: Question }) {+ const [currentQuestion, setCurrentQuestion] = useState(question);const {extensions,portalElement,togglePrompt,isPromptOpen,proposedQuestion,- } = useInlineSQLPrompt(question, "qb");- const query = checkNotNull(question.legacyNativeQuery());+ handleAcceptProposed,+ handleRejectProposed,+ } = useInlineSQLPrompt(currentQuestion, "qb");+ const query = checkNotNull(currentQuestion.legacyNativeQuery());return (<><NativeQueryEditor- question={question}+ question={currentQuestion}query={query}- setDatasetQuery={jest.fn()}+ proposedQuestion={proposedQuestion}+ setDatasetQuery={(nextQuery) =>+ setCurrentQuestion(+ currentQuestion.setDatasetQuery(nextQuery.datasetQuery()),+ )+ }+ onAcceptProposed={handleAcceptProposed}+ onRejectProposed={handleRejectProposed}isNativeEditorOpenisInitiallyOpenextensions={extensions}@@ -127,6 +164,19 @@ function TestEditor({ question }: { question: Question }) {);}+function TestPage({ question }: { question: Question }) {+ const [pageKey, setPageKey] = useState(0);++ return (+ <>+ <TestEditor key={pageKey} question={question} />+ <button onClick={() => setPageKey((key) => key + 1)}>+ Open new SQL page+ </button>+ </>+ );+}+interface SetupOpts {isMetabotEnabled?: boolean;otherContent?: ReactElement;@@ -134,10 +184,11 @@ interface SetupOpts {function setup({ isMetabotEnabled = true, otherContent }: SetupOpts = {}) {setupEnterprisePlugins();- setupDatabasesEndpoints([TEST_DB]);+ setupDatabasesEndpoints([TEST_DB, OTHER_DB]);setupUserMetabotPermissionsEndpoint();setupCollectionsEndpoints({ collections: [] });setupNativeQuerySnippetEndpoints();+ setupSearchEndpoints([]);const state = createMockState({settings: mockSettings({@@ -146,17 +197,16 @@ function setup({ isMetabotEnabled = true, otherContent }: SetupOpts = {}) {}),currentUser: createMockUser(),entities: createMockEntitiesState({- databases: [TEST_DB],+ databases: [TEST_DB, OTHER_DB],questions: [TEST_CARD],}),});- const question = checkNotNull(- createMockMetadataFromState(state).question(TEST_CARD.id),- );+ const metadata = createMockMetadataFromState(state);+ const question = checkNotNull(metadata.question(TEST_CARD.id));renderWithProviders(<MetabotProvider>- <TestEditor question={question} />+ <TestPage question={question} />{otherContent}</MetabotProvider>,{ storeInitialState: state },@@ -191,6 +241,64 @@ async function startGenerating() {return prompt;}+function editorContent() {+ return checkNotNull(document.querySelector<HTMLElement>(".cm-content"));+}++async function openPrompt() {+ const button = await screen.findByLabelText("Ask Metabot");+ if (button.getAttribute("aria-pressed") !== "true") {+ await userEvent.click(button);+ }+ return screen.findByTestId("metabot-inline-sql-prompt");+}++async function sendPrompt(text: string) {+ const prompt = await openPrompt();+ await userEvent.type(within(prompt).getByRole("textbox"), text);+ await userEvent.click(+ within(prompt).getByRole("button", { name: "Send to Metabot" }),+ );+ return prompt;+}++type AgentEndpointMock = ReturnType<typeof mockAgentEndpoint>;++// Re-spying on an already mocked fetch would route other requests back into the+// mock, so restore the previous mock before setting the next response.+async function generateSql({+ previousAgent,+ prompt,+ sql,+}: {+ previousAgent?: AgentEndpointMock;+ prompt: string;+ sql: string;+}) {+ previousAgent?.mockRestore();+ const agent = mockAgentEndpoint({ events: codeEditResponse(sql) });+ await sendPrompt(prompt);+ await waitFor(() => expect(getDiffControls().row).toBeInTheDocument());+ const body: unknown = await lastReqBody(agent);+ return { agent, body };+}++// CodeMirror resolves a shifted letter binding from `keyCode`, which+// `userEvent.keyboard` does not set.+function pressPromptShortcut(target: HTMLElement) {+ fireEvent.keyDown(target, {+ key: "I",+ code: "KeyI",+ keyCode: 73,+ ctrlKey: true,+ shiftKey: true,+ });+}++function queryDiffControls() {+ return document.querySelector(".cm-diff-controls");+}+async function expectProposedSqlSoon() {await waitFor(() => {@@ -317,6 +425,191 @@ describe("useInlineSQLPrompt", () => {expect(within(prompt).queryByRole("status")).not.toBeInTheDocument();});+ it("opens and closes the prompt with the keyboard shortcut", async () => {+ setup();++ expect(await screen.findByLabelText("Ask Metabot")).toBeInTheDocument();+ await userEvent.click(editorContent());+ pressPromptShortcut(editorContent());+ const prompt = await screen.findByTestId("metabot-inline-sql-prompt");++ pressPromptShortcut(within(prompt).getByRole("textbox"));+ await waitFor(() => {+ expect(+ screen.queryByTestId("metabot-inline-sql-prompt"),+ ).not.toBeInTheDocument();+ });+ });++ it("sends the SQL editor as context of the request", async () => {+ const agent = mockAgentEndpoint({ events: CODE_EDIT_RESPONSE });+ setup();++ await sendPrompt("select all users");++ const body: unknown = await lastReqBody(agent);+ expect(body).toMatchObject({+ context: {+ user_is_viewing: [+ {+ type: "code_editor",+ buffers: [+ {+ id: "qb",+ source: { language: "sql", database_id: TEST_DB.id },+ },+ ],+ },+ ],+ },+ });+ });++ it("shows the generated SQL as a diff and applies it on accept", async () => {+ setup();++ await generateSql({ prompt: "select all users", sql: "SELECT 2" });++ expect(+ screen.queryByTestId("metabot-inline-sql-prompt"),+ ).not.toBeInTheDocument();+ expect(editorContent()).toHaveTextContent("SELECT 1");+ expect(editorContent()).toHaveTextContent("SELECT 2");++ await userEvent.click(+ within(getDiffControls().row).getByRole("button", { name: /Accept/ }),+ );++ await waitFor(() => expect(queryDiffControls()).not.toBeInTheDocument());+ expect(editorContent()).toHaveTextContent("SELECT 2");+ expect(editorContent()).not.toHaveTextContent("SELECT 1");+ });++ it("cancels the in-flight request when the prompt is closed", async () => {+ const agent = mockAgentEndpoint({+ stream: new ReadableStream<Uint8Array>(),+ });+ setup();++ const prompt = await startGenerating();+ await userEvent.click(+ within(prompt).getByTestId("metabot-inline-sql-cancel"),+ );++ await waitFor(() => {+ expect(+ screen.queryByTestId("metabot-inline-sql-prompt"),+ ).not.toBeInTheDocument();+ });+ const [request] = checkNotNull(agent.mock.calls.at(-1));+ expect(request).toBeInstanceOf(Request);+ expect(request instanceof Request && request.signal.aborted).toBe(true);+ expect(queryDiffControls()).not.toBeInTheDocument();+ });++ it("shows an error when the response has no SQL", async () => {+ mockAgentEndpoint({ events: TEXT_ONLY_RESPONSE });+ setup();++ const prompt = await sendPrompt("do something");++ expect(+ await within(prompt).findByTestId("metabot-inline-sql-error"),+ ).toHaveTextContent(+ "Sorry, I ran into an error. Could you please try that again?",+ );+ expect(screen.getByTestId("proposed-sql")).toBeEmptyDOMElement();+ });++ it("restores the SQL on reject and sends the rejection with the next prompt", async () => {+ setup();++ const { agent } = await generateSql({+ prompt: "select all users",+ sql: "SELECT * FROM users",+ });+ await userEvent.click(+ within(getDiffControls().row).getByRole("button", { name: /Reject/ }),+ );++ await waitFor(() => expect(queryDiffControls()).not.toBeInTheDocument());+ expect(editorContent()).toHaveTextContent("SELECT 1");+ expect(editorContent()).not.toHaveTextContent("SELECT * FROM users");++ const { body } = await generateSql({+ previousAgent: agent,+ prompt: "try again",+ sql: "SELECT id FROM users",+ });+ expect(body).toMatchObject({+ parent_message_id: expect.any(String),+ message: expect.stringContaining(+ "User rejected the following suggestion:\n\nSELECT * FROM users",+ ),+ });+ });++ it("keeps the prompt open and starts a new conversation when the database changes", async () => {+ setup();++ const { agent } = await generateSql({+ prompt: "select all users",+ sql: "SELECT 2",+ });+ await userEvent.click(+ within(getDiffControls().row).getByRole("button", { name: /Reject/ }),+ );+ await openPrompt();++ await userEvent.click(screen.getByTestId("selected-database"));+ await userEvent.click(await screen.findByText(OTHER_DB.name));+ expect(screen.getByTestId("selected-database")).toHaveTextContent(+ OTHER_DB.name,+ );+ expect(screen.getByTestId("metabot-inline-sql-prompt")).toBeInTheDocument();++ const { body } = await generateSql({+ previousAgent: agent,+ prompt: "select something",+ sql: "SELECT 123",+ });+ expect(body).not.toHaveProperty("parent_message_id");+ });++ it("starts a new conversation on a new SQL page", async () => {+ setup();++ const { agent } = await generateSql({+ prompt: "select all users",+ sql: "SELECT 2",+ });+ await userEvent.click(+ within(getDiffControls().row).getByRole("button", { name: /Accept/ }),+ );++ await userEvent.click(+ screen.getByRole("button", { name: "Open new SQL page" }),+ );++ const { body } = await generateSql({+ previousAgent: agent,+ prompt: "new prompt",+ sql: "SELECT 456",+ });+ expect(body).not.toHaveProperty("parent_message_id");+ });++ it("dismisses the proposal when the SQL is edited by hand", async () => {+ setup();++ await generateSql({ prompt: "select all users", sql: "SELECT 2" });++ await userEvent.type(editorContent(), " ");++ await waitFor(() => expect(queryDiffControls()).not.toBeInTheDocument());+ expect(screen.getByTestId("proposed-sql")).toBeEmptyDOMElement();+ });+it("does not render when metabot is not enabled", async () => {setup({ isMetabotEnabled: false });diff --git a/frontend/src/metabase/metabot/components/MetabotQuickLinks.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotQuickLinks.unit.spec.tsxnew file mode 100644index 00000000000..25480bba571--- /dev/null+++ b/frontend/src/metabase/metabot/components/MetabotQuickLinks.unit.spec.tsx@@ -0,0 +1,92 @@+import { setupEnterprisePlugins } from "__support__/enterprise";+import {+ setupDatabaseListEndpoint,+ setupGetMetabotConversationTitleEndpoint,+ setupListMetabotConversationsEndpoint,+ setupMetabotSamplePromptSuggestionsEndpoint,+ setupUserMetabotPermissionsEndpoint,+} from "__support__/server-mocks";+import { mockSettings } from "__support__/settings";+import { createMockState } from "__support__/state";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import { Outlet, Route } from "metabase/router";+import { createMockUserMetabotPermissions } from "metabase-types/api/mocks";++import { FIXED_METABOT_ENTITY_IDS } from "../constants";+import { MetabotProvider } from "../context";+import { metabotReducer } from "../state";+import {+ createTestMetabotState,+ lastReqBody,+ mockAgentEndpoint,+ whoIsYourFavoriteResponse,+} from "../tests/utils";++import { Metabot } from "./Metabot";+import { getMetabotQuickLinks } from "./MetabotQuickLinks";++const PROMPT = "Who is your favorite?";++interface SetupOpts {+ isMetabotEnabled: boolean;+}++const setup = ({ isMetabotEnabled }: SetupOpts) => {+ setupEnterprisePlugins();+ setupUserMetabotPermissionsEndpoint(createMockUserMetabotPermissions());+ setupMetabotSamplePromptSuggestionsEndpoint(FIXED_METABOT_ENTITY_IDS.DEFAULT);+ setupDatabaseListEndpoint([]);+ setupListMetabotConversationsEndpoint([]);+ setupGetMetabotConversationTitleEndpoint({ status: "pending", title: null });+ const agentSpy = mockAgentEndpoint({ events: whoIsYourFavoriteResponse });++ const { router } = renderWithProviders(+ <Route+ path="/"+ element={+ <MetabotProvider>+ <Metabot />+ <Outlet />+ </MetabotProvider>+ }+ >+ {getMetabotQuickLinks()}+ </Route>,+ {+ withRouter: true,+ initialRoute: `/metabot/new?q=${encodeURIComponent(PROMPT)}`,+ storeInitialState: createMockState({+ settings: mockSettings({+ "metabot-enabled?": isMetabotEnabled,+ "llm-metabot-configured?": true,+ }),+ metabot: createTestMetabotState(),+ }),+ customReducers: { metabot: metabotReducer },+ },+ );++ return { agentSpy, router };+};++describe("MetabotQuickLinks > /metabot/new", () => {+ it("redirects to the home page, opens the chat and submits the prompt", async () => {+ const { agentSpy, router } = setup({ isMetabotEnabled: true });++ await waitFor(() => expect(router?.location.pathname).toBe("/"));+ expect(await screen.findByTestId("metabot-chat")).toBeInTheDocument();++ const body: unknown = await lastReqBody(agentSpy);+ expect(body).toEqual(expect.objectContaining({ message: PROMPT }));+ expect(agentSpy).toHaveBeenCalledTimes(1);+ });++ it("redirects to the home page without submitting the prompt when metabot is disabled", async () => {+ const { agentSpy, router } = setup({ isMetabotEnabled: false });++ await waitFor(() => expect(router?.location.pathname).toBe("/"));+ expect(router?.location.search).toBe("");+ expect(screen.queryByTestId("metabot-chat")).not.toBeInTheDocument();+ expect(agentSpy).not.toHaveBeenCalled();+ });+});diff --git a/frontend/src/metabase/metabot/hooks/use-metabot-embedding-aware-enabled.unit.spec.tsx b/frontend/src/metabase/metabot/hooks/use-metabot-embedding-aware-enabled.unit.spec.tsxnew file mode 100644index 00000000000..9549f8dd061--- /dev/null+++ b/frontend/src/metabase/metabot/hooks/use-metabot-embedding-aware-enabled.unit.spec.tsx@@ -0,0 +1,71 @@+import { mockSettings } from "__support__/settings";+import { createMockState } from "__support__/state";+import { renderHookWithProviders } from "__support__/ui";+import type * as IframeUtils from "metabase/utils/iframe";+import { isWithinIframe } from "metabase/utils/iframe";++import { useMetabotEnabledEmbeddingAware } from "./use-metabot-embedding-aware-enabled";++jest.mock("metabase/utils/iframe", () => ({+ ...jest.requireActual<typeof IframeUtils>("metabase/utils/iframe"),+ isWithinIframe: jest.fn(),+}));++interface SetupOpts {+ isEmbedded: boolean;+ isMetabotEnabled: boolean;+ isEmbeddedMetabotEnabled: boolean;+}++const setup = ({+ isEmbedded,+ isMetabotEnabled,+ isEmbeddedMetabotEnabled,+}: SetupOpts) => {+ jest.mocked(isWithinIframe).mockReturnValue(isEmbedded);++ const { result } = renderHookWithProviders(+ () => useMetabotEnabledEmbeddingAware(),+ {+ storeInitialState: createMockState({+ settings: mockSettings({+ "metabot-enabled?": isMetabotEnabled,+ "embedded-metabot-enabled?": isEmbeddedMetabotEnabled,+ "llm-metabot-configured?": true,+ }),+ }),+ },+ );++ return result.current;+};++describe("useMetabotEnabledEmbeddingAware", () => {+ it.each([+ { isEmbeddedMetabotEnabled: true, isMetabotEnabled: false },+ { isEmbeddedMetabotEnabled: false, isMetabotEnabled: true },+ ])(+ "follows embedded-metabot-enabled? = $isEmbeddedMetabotEnabled in an iframe",+ ({ isEmbeddedMetabotEnabled, isMetabotEnabled }) => {+ expect(+ setup({ isEmbedded: true, isMetabotEnabled, isEmbeddedMetabotEnabled }),+ ).toBe(isEmbeddedMetabotEnabled);+ },+ );++ it.each([+ { isMetabotEnabled: true, isEmbeddedMetabotEnabled: false },+ { isMetabotEnabled: false, isEmbeddedMetabotEnabled: true },+ ])(+ "follows metabot-enabled? = $isMetabotEnabled outside an iframe",+ ({ isMetabotEnabled, isEmbeddedMetabotEnabled }) => {+ expect(+ setup({+ isEmbedded: false,+ isMetabotEnabled,+ isEmbeddedMetabotEnabled,+ }),+ ).toBe(isMetabotEnabled);+ },+ );+});diff --git a/frontend/src/metabase/metabot/hooks/use-user-metabot-permissions.unit.spec.tsx b/frontend/src/metabase/metabot/hooks/use-user-metabot-permissions.unit.spec.tsxindex f6ab8be96e2..68e425d97a3 100644--- a/frontend/src/metabase/metabot/hooks/use-user-metabot-permissions.unit.spec.tsx+++ b/frontend/src/metabase/metabot/hooks/use-user-metabot-permissions.unit.spec.tsx@@ -84,6 +84,11 @@ describe("useUserMetabotPermissions", () => {expect(perms.canUseSqlGeneration).toBe(false);expect(perms.canUseNlq).toBe(false);expect(perms.canUseOtherTools).toBe(false);+ expect(+ fetchMock.callHistory.called(+ "path:/api/metabot/permissions/user-permissions",+ ),+ ).toBe(false);});it("does not fetch user permissions without an authenticated user", async () => {diff --git a/frontend/src/metabase/metabot/tests/message.unit.spec.tsx b/frontend/src/metabase/metabot/tests/message.unit.spec.tsxindex 999341f7e33..2b36a3b982a 100644--- a/frontend/src/metabase/metabot/tests/message.unit.spec.tsx+++ b/frontend/src/metabase/metabot/tests/message.unit.spec.tsx@@ -1,6 +1,7 @@import userEvent from "@testing-library/user-event";import { screen, waitFor } from "__support__/ui";+import * as Analytics from "metabase/analytics";import type { SSEEvent } from "metabase/api/ai-streaming/sse-types";import { checkNotNull } from "metabase/utils/types";@@ -105,6 +106,29 @@ describe("metabot > message", () => {expect(await input()).toHaveFocus();});+ it("should track each sent chat message", async () => {+ const trackEvent = jest.mocked(Analytics.trackSimpleEvent);+ trackEvent.mockClear();+ setup();+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });++ await enterChatMessage("Who is your favorite?");+ expect(+ await screen.findByText("You, but don't tell anyone."),+ ).toBeInTheDocument();++ await waitFor(() => {+ expect(trackEvent).toHaveBeenCalledWith({+ event: "metabot_request_sent",+ });+ });+ expect(+ trackEvent.mock.calls.filter(+ ([event]) => event.event === "metabot_request_sent",+ ),+ ).toHaveLength(1);+ });+it("should be able to send a message via send button", async () => {setup();mockAgentEndpoint({ events: whoIsYourFavoriteResponse });diff --git a/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx b/frontend/src/metabase/metabot/tests/ui.unit.spec.tsxindex 90c38049a9b..8b47e883f5d 100644--- a/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx+++ b/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx@@ -10,7 +10,9 @@ import {setupGetMetabotConversationEndpoint,setupGetMetabotConversationEndpointError,} from "__support__/server-mocks";+import { mockSettings } from "__support__/settings";import { act, fireEvent, screen, waitFor, within } from "__support__/ui";+import * as Analytics from "metabase/analytics";import type { SSEEvent } from "metabase/api/ai-streaming/sse-types";import { FIXED_METABOT_ENTITY_IDS } from "metabase/metabot/constants";import { useMetabotAgent } from "metabase/metabot/hooks";@@ -121,6 +123,37 @@ describe("metabot > ui", () => {).not.toBeInTheDocument();});+ it.each([+ {+ showIllustrations: true,+ hasIllustration: true,+ hint: "I can help you explore your metrics and models.",+ },+ {+ showIllustrations: false,+ hasIllustration: false,+ hint: "Explore your metrics and models with AI.",+ },+ ])(+ "should show the empty state hint and illustration=$hasIllustration when metabot-show-illustrations is $showIllustrations",+ async ({ showIllustrations, hasIllustration, hint }) => {+ setup({+ storeInitialState: {+ settings: mockSettings({+ "llm-metabot-configured?": true,+ "metabot-show-illustrations": showIllustrations,+ }),+ },+ });++ const emptyChatInfo = await screen.findByTestId(+ "metabot-empty-chat-info",+ );+ expect(within(emptyChatInfo).getByText(hint)).toBeInTheDocument();+ expect(emptyChatInfo.innerHTML.includes("<svg")).toBe(hasIllustration);+ },+ );+it("should be able to toggle visibility", async () => {const { store } = setup();expect(await chat()).toBeInTheDocument();@@ -141,7 +174,9 @@ describe("metabot > ui", () => {const pressShortcut = () =>fireEvent.keyDown(window, { key: "e", ctrlKey: true });- it("should toggle visibility", async () => {+ it("should toggle visibility and track only the opening", async () => {+ const trackEvent = jest.mocked(Analytics.trackSimpleEvent);+ trackEvent.mockClear();const { store } = setup({withRouter: true,initialRoute: "/question/123",@@ -153,6 +188,15 @@ describe("metabot > ui", () => {pressShortcut();expect(await chat()).toBeInTheDocument();+ expect(trackEvent).toHaveBeenCalledTimes(1);+ expect(trackEvent).toHaveBeenCalledWith({+ event: "metabot_chat_opened",+ triggered_from: "keyboard_shortcut",+ });++ pressShortcut();+ await assertNotVisible();+ expect(trackEvent).toHaveBeenCalledTimes(1);});it.each([diff --git a/frontend/src/metabase/query_builder/components/MetabotQueryBuilder/MetabotQueryBuilder.unit.spec.tsx b/frontend/src/metabase/query_builder/components/MetabotQueryBuilder/MetabotQueryBuilder.unit.spec.tsxnew file mode 100644index 00000000000..faf36fc8f47--- /dev/null+++ b/frontend/src/metabase/query_builder/components/MetabotQueryBuilder/MetabotQueryBuilder.unit.spec.tsx@@ -0,0 +1,68 @@+import { setupEnterprisePlugins } from "__support__/enterprise";+import { setupUserMetabotPermissionsEndpoint } from "__support__/server-mocks";+import { mockSettings } from "__support__/settings";+import { createMockState } from "__support__/state";+import { renderWithProviders, screen } from "__support__/ui";+import type { UserMetabotPermissions } from "metabase-types/api";+import {+ createMockTokenFeatures,+ createMockUserMetabotPermissions,+} from "metabase-types/api/mocks";++import { MetabotQueryBuilder } from "./MetabotQueryBuilder";++jest.mock("metabase/metabot/components/MetabotAsk", () => ({+ MetabotAsk: () => <div data-testid="metabot-ask" />,+}));++jest.mock("../../containers/QueryBuilder", () => ({+ QueryBuilder: () => <div data-testid="query-builder" />,+}));++interface SetupOpts {+ isMetabotEnabled?: boolean;+ nlqPermission?: UserMetabotPermissions["metabot-nlq"];+}++const setup = ({+ isMetabotEnabled = true,+ nlqPermission = "yes",+}: SetupOpts = {}) => {+ setupEnterprisePlugins();+ setupUserMetabotPermissionsEndpoint(+ createMockUserMetabotPermissions({ "metabot-nlq": nlqPermission }),+ );++ renderWithProviders(<MetabotQueryBuilder />, {+ storeInitialState: createMockState({+ settings: mockSettings({+ "metabot-enabled?": isMetabotEnabled,+ "llm-metabot-configured?": true,+ "token-features": createMockTokenFeatures({ ai_controls: true }),+ }),+ }),+ });+};++describe("MetabotQueryBuilder", () => {+ it("renders the Metabot ask page, not the query builder, with NLQ access", async () => {+ setup();++ expect(await screen.findByTestId("metabot-ask")).toBeInTheDocument();+ expect(screen.queryByTestId("query-builder")).not.toBeInTheDocument();+ });++ it("renders the query builder without NLQ access", async () => {+ setup({ nlqPermission: "no" });++ expect(await screen.findByTestId("query-builder")).toBeInTheDocument();+ expect(screen.queryByTestId("metabot-ask")).not.toBeInTheDocument();+ });++ it("renders the query builder when metabot is disabled", async () => {+ setup({ isMetabotEnabled: false });++ expect(await screen.findByTestId("query-builder")).toBeInTheDocument();+ expect(screen.queryByTestId("metabot-ask")).not.toBeInTheDocument();+ });+});diff --git a/frontend/test/__support__/server-mocks/metabot.ts b/frontend/test/__support__/server-mocks/metabot.tsindex 0c95e0c0f9d..5a43201aa32 100644--- a/frontend/test/__support__/server-mocks/metabot.ts+++ b/frontend/test/__support__/server-mocks/metabot.ts@@ -240,6 +240,17 @@ export function setupMetabotPromptSuggestionsEndpointError(});}+/** Mocks the prompt suggestions the chat samples for its empty state. */+export function setupMetabotSamplePromptSuggestionsEndpoint(+ metabotEntityId: MetabotInfo["entity_id"],+ prompts: SuggestedMetabotPromptsResponse["prompts"] = [],+) {+ fetchMock.get(+ `path:/api/metabot/metabot/${metabotEntityId}/prompt-suggestions`,+ { prompts, limit: prompts.length, offset: 0, total: prompts.length },+ );+}+export function setupMetabotAddEntitiesEndpoint(metabotId: MetabotId) {fetchMock.put(`path:/api/metabot/metabot/${metabotId}/entities`, {status: 204,diff --git a/frontend/test/__support__/server-mocks/util.ts b/frontend/test/__support__/server-mocks/util.tsindex 672e07c72a8..834cf7ed3a2 100644--- a/frontend/test/__support__/server-mocks/util.ts+++ b/frontend/test/__support__/server-mocks/util.ts@@ -36,3 +36,20 @@ export async function findRequests(}),);}++/**+ * Returns the bodies of the `method` requests whose URL ends with+ * `urlSuffix`, in the order they were sent.+ */+export async function findRequestBodies({+ method,+ urlSuffix,+}: {+ method: Parameters<typeof findRequests>[0];+ urlSuffix: string;+}): Promise<unknown[]> {+ const requests = await findRequests(method);+ return requests+ .filter(({ url }) => url.endsWith(urlSuffix))+ .map(({ body }): unknown => body);+}