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
Move flaky usage-auditing e2e coverage into unit tests (BOT-1909) (#78838)
Maksym Yakubyche4705cad9b97
diff --git a/e2e/test/scenarios/metabot/usage-auditing.cy.spec.ts b/e2e/test/scenarios/metabot/usage-auditing.cy.spec.tsindex ddc97a8e3c9..83c76e67f55 100644--- a/e2e/test/scenarios/metabot/usage-auditing.cy.spec.ts+++ b/e2e/test/scenarios/metabot/usage-auditing.cy.spec.ts@@ -1,51 +1,18 @@const { H } = cy;import {- ADMINISTRATORS_GROUP_ID,ADMIN_USER_ID,NORMAL_USER_ID,} from "e2e/support/cypress_sample_instance_data";-type UsageStatsMetric = "conversations" | "messages" | "tokens";type SeedUsageAuditingResponse = {inserted: number;date: string;};-type SeedUsageAuditingRequest = {- user_id: number;- second_user_id: number;- tenant_id?: number;- second_tenant_id?: number;-};-type UsageAuditingTenant = {- id: number;- name: string;- slug: string;-};-type UsageAuditingTenants = {- bobbyTenant: UsageAuditingTenant;- robertTenant: UsageAuditingTenant;-};// must match the title seeded by testing_api/api.cljconst SEEDED_CONVERSATION_TITLE = "E2E usage auditing conversation";-const METRIC_TAB_NAMES: Record<UsageStatsMetric, string> = {- conversations: "Conversations",- messages: "Messages",- tokens: "Tokens",-};--const BOBBY_TENANT = {- name: "Bobby Analytics",- slug: "bobby-analytics",-};-const ROBERT_TENANT = {- name: "Robert Analytics",- slug: "robert-analytics",-};-const TENANT_CONVERSATIONS_CHART_TITLE = "Tenants with most conversations";-// The usage-stats page only waits for the audit metadata before rendering; each// chart then fires its own adhoc /api/dataset query and mounts ECharts. With six// charts rendering at once, the [data-testid="chart-container"] mount can blow@@ -53,357 +20,54 @@ const TENANT_CONVERSATIONS_CHART_TITLE = "Tenants with most conversations";// assertion a generous budget rather than racing the render.const CHART_RENDER_TIMEOUT = 12000;-const METRIC_CHART_TITLES: Record<UsageStatsMetric, string[]> = {- conversations: [- "Conversations by day",- "Conversations by source",- "Conversations by profile",- "Groups with most conversations",- "Users with most conversations",- "IP addresses with most conversations",- ],- messages: [- "Messages by day",- "Messages by source",- "Messages by profile",- "Groups with most messages",- "Users with most messages",- "IP addresses with most messages",- ],- tokens: [- "Tokens by day",- "Tokens by source",- "Tokens by profile",- "Groups with most tokens",- "Users with most tokens",- "IP addresses with most tokens",- ],-};--type DateFilterLabel =- | "Today"- | "Yesterday"- | "Last 7 days"- | "Last 30 days"- | "Previous month"- | "Previous 3 months"- | "Previous 12 months";--const DATE_FILTER_CASES: DateFilterLabel[] = ["Today", "Last 7 days"];--const MAIN_PROFILE_LABELS: string[] = [- "Internal",- "Slackbot",- "SQL",- "NLQ",- "Embedding",- "Transforms codegen",+const CONVERSATION_CHART_TITLES = [+ "Conversations by day",+ "Conversations by source",+ "Conversations by profile",+ "Groups with most conversations",+ "Users with most conversations",+ "IP addresses with most conversations",];-function visitUsageStatsPage(path = "/admin/metabot/usage-auditing"): void {- cy.intercept("GET", "/api/database/13371337/metadata*").as("auditMetadata");- cy.intercept("POST", "/api/dataset").as("dataset");+function seedUsageAuditingData(): void {+ cy.request<SeedUsageAuditingResponse>(+ "POST",+ "/api/testing/metabot/seed-usage-auditing",+ { user_id: ADMIN_USER_ID, second_user_id: NORMAL_USER_ID },+ );+}- cy.visit(path);+function visitUsageStatsPage(): void {+ cy.intercept("GET", "/api/database/13371337/metadata*").as("auditMetadata");+ cy.visit("/admin/metabot/usage-auditing");cy.wait("@auditMetadata");}-function interceptConversationsApi(): void {+function visitConversationsPage(): void {cy.intercept("GET", "/api/ee/metabot-analytics/conversations?*").as("conversations",);cy.intercept("GET", "/api/ee/metabot-analytics/conversations/*").as("conversationDetail",);-}--function waitForConversations(): void {+ cy.visit("/admin/metabot/usage-auditing/conversations?date=past7days~");cy.wait("@conversations").its("response.statusCode").should("eq", 200);}-function seedUsageAuditingData(- request: SeedUsageAuditingRequest = {- user_id: ADMIN_USER_ID,- second_user_id: NORMAL_USER_ID,- },-): Cypress.Chainable<Cypress.Response<SeedUsageAuditingResponse>> {- return cy- .request<SeedUsageAuditingResponse>(- "POST",- "/api/testing/metabot/seed-usage-auditing",- request,- )- .as("usageAuditingSeed");-}--function getUsageAuditingSeed(): Cypress.Chainable<- Cypress.Response<SeedUsageAuditingResponse>-> {- return cy.get<Cypress.Response<SeedUsageAuditingResponse>>(- "@usageAuditingSeed",- );-}--function createUsageAuditingTenant({- name,- slug,-}: Omit<UsageAuditingTenant, "id">): Cypress.Chainable<UsageAuditingTenant> {- return cy- .request<UsageAuditingTenant>("POST", "/api/ee/tenant", { name, slug })- .its("body");-}--function setupUsageAuditingTenants(): Cypress.Chainable<UsageAuditingTenants> {- H.updateSetting("use-tenants", true);-- return createUsageAuditingTenant(BOBBY_TENANT).then((bobbyTenant) => {- return createUsageAuditingTenant(ROBERT_TENANT).then((robertTenant) => {- return seedUsageAuditingData({- user_id: ADMIN_USER_ID,- second_user_id: NORMAL_USER_ID,- tenant_id: bobbyTenant.id,- second_tenant_id: robertTenant.id,- }).then(() => ({ bobbyTenant, robertTenant }));- });- });-}--function visitConversationsPage(- path = "/admin/metabot/usage-auditing/conversations",-): void {- interceptConversationsApi();- cy.visit(path);- waitForConversations();-}--function getChartCard(title: string): Cypress.Chainable<JQuery<HTMLElement>> {- return H.main()+function assertChartRendered(title: string): void {+ H.main().findByText(title).scrollIntoView().should("be.visible")- .parent();-}--function assertChartRendered(title: string): void {- getChartCard(title).within(() => {- cy.findByTestId(/^(chart|row-chart)-container$/, {- timeout: CHART_RENDER_TIMEOUT,- })- .should("be.visible")- .find("svg")- .should("exist");- });-}--function assertMetricChartsRendered(metric: UsageStatsMetric): void {- METRIC_CHART_TITLES[metric].forEach(assertChartRendered);-}--function selectMetricTab(- metric: Exclude<UsageStatsMetric, "conversations">,-): void {- H.main().findByRole("tab", { name: METRIC_TAB_NAMES[metric] }).realClick();- cy.url().should("include", `metric=${metric}`);- cy.wait("@dataset");-}--function getMetricTimeseriesTitle(- metric: UsageStatsMetric,- dateLabel: DateFilterLabel,-): string {- const timeseriesUnit =- dateLabel === "Today" || dateLabel === "Yesterday" ? "hour" : "day";-- return `${METRIC_TAB_NAMES[metric]} by ${timeseriesUnit}`;-}--function assertMetricChartsRenderedForDate(- metric: UsageStatsMetric,- dateLabel: DateFilterLabel,-): void {- assertChartRendered(getMetricTimeseriesTitle(metric, dateLabel));- METRIC_CHART_TITLES[metric].slice(1).forEach((title) => {- assertChartRendered(title);- });-}--function selectGroupFilter(groupName: string): void {- H.main().findByTestId("conversation-filters-group-select").realClick();- H.selectDropdown().findByText(groupName).realClick();- cy.url().should("include", "group=");- cy.wait("@dataset");-}--function selectUserFilter(userName: string, waitAlias = "@dataset"): void {- H.main().findByTestId("conversation-filters-user-select").realClick();- H.selectDropdown().findByText(userName).realClick();- // Anchor on the select reflecting the newly chosen user before waiting. A bare- // `url includes "user="` check is a no-op after a drill-through (the URL already- // carries the drilled user), so it doesn't gate on the filter actually switching- // and the conversations request/table can be read mid-transition. Mirrors- // selectDateFilter.- H.main()- .findByTestId("conversation-filters-user-select")- .should("have.value", userName);- cy.wait(waitAlias);-}--function selectTenantFilter(tenantName: string, waitAlias = "@dataset"): void {- H.main().findByTestId("conversation-filters-tenant-select").realClick();- H.selectDropdown().findByText(tenantName).realClick();- cy.url().should("include", "tenant=");- cy.wait(waitAlias);-}--function selectDateFilter(- dateLabel: DateFilterLabel,- waitAlias = "@dataset",-): void {- H.main().findByTestId("conversation-filters-date-select").realClick();- H.selectDropdown().findByText(dateLabel).realClick();- H.main()- .findByTestId("conversation-filters-date-select")- .should("have.value", dateLabel);- cy.wait(waitAlias);-}--function assertConversationTableContains(labels: readonly string[]): void {- H.main()- .findByRole("table")+ .parent().within(() => {- labels.forEach((label) => {- cy.findAllByText(label)- .filter(":visible")- .should("have.length.greaterThan", 0);- });- });-}--function assertConversationTableDoesNotContain(- labels: readonly string[],-): void {- H.main()- .findByRole("table")- .within(() => {- labels.forEach((label) => {- cy.contains(label).should("not.exist");- });- });-}--function assertTodayConversationTable(): void {- assertConversationTableContains([- "Bobby Tables",- "NLQ",- "Slackbot",- "Transforms codegen",- "10.0.0.1",- "10.0.0.6",- "10.0.0.7",- ]);- assertConversationTableDoesNotContain([- "Robert Tableton",- "Internal",- "10.0.0.2",- "10.0.0.3",- "10.0.0.99",- ]);-}--function assertLatestHourConversationTable(): void {- assertConversationTableContains([- "Bobby Tables",- "Transforms codegen",- "10.0.0.7",- ]);- assertConversationTableDoesNotContain([- "Robert Tableton",- "NLQ",- "10.0.0.1",- "10.0.0.2",- "10.0.0.3",- "10.0.0.99",- ]);-}--function assertHourDateFilterInUrl(): void {- cy.location("search").then((search) => {- const date = new URLSearchParams(search).get("date");-- expect(date).to.match(- /^\d{4}-\d{2}-\d{2}T\d{2}:00:00~\d{4}-\d{2}-\d{2}T\d{2}:00:00$/,- );- });-}--function clickLastTimeseriesChartDot(title: string): void {- // Anchor on the chart being fully rendered before drilling: the page only- // waits for the audit metadata, so the chart's dataset query + ECharts render- // can still be in flight. Without this, cartesianChartCircle() races the- // render and times out looking for [data-testid="chart-container"].- assertChartRendered(title);- getChartCard(title).within(() => {- // eslint-disable-next-line metabase/no-unsafe-element-filtering- H.cartesianChartCircle()- .should("have.length.greaterThan", 0)- .last()- .realClick();- });-}--function clickRowChartBarForLabel(title: string, label: string): void {- // Drill by clicking the bar element itself rather than coordinate-clicking the- // container at a fixed offset from the axis label. Two things made the old- // `labelRect.right + 30` approach flaky:- // - The page only waits for the audit metadata, so the chart's dataset query- // + ECharts render could still be in flight.- // - Row charts measure their size asynchronously (ExplicitSize, throttled),- // so they first render with zero-width bars before re-laying out at the- // final geometry — and assertChartRendered only proves the <svg> exists,- // it passes in both the unmeasured and measured states.- // A pixel coordinate computed in either window lands off the bar: the- // drill-through never fires and cy.wait("@conversations") times out, or a- // near-miss drills a neighboring group. Instead wait for the measured- // (non-zero-width) bar whose vertical band contains the label's midpoint and- // realClick it — Cypress re-resolves its center at click time and retries for- // actionability, so the click can't miss.- assertChartRendered(title);- getChartCard(title).within(() => {- cy.findByText(label).then(([labelEl]) => {- cy.findAllByRole("graphics-symbol")- .filter((_index, barEl) => {- const labelRect = labelEl.getBoundingClientRect();- const labelMidY = labelRect.top + labelRect.height / 2;- const barRect = barEl.getBoundingClientRect();- return (- barRect.width > 0 &&- barRect.top <= labelMidY &&- labelMidY <= barRect.bottom- );- })- .should("have.length", 1)- .realClick({ scrollBehavior: false });+ cy.findByTestId(/^(chart|row-chart)-container$/, {+ timeout: CHART_RENDER_TIMEOUT,+ })+ .should("be.visible")+ .find("svg")+ .should("exist");});- });-}--function openConversationFromProfile(profileLabel: string): void {- cy.get("tbody")- .contains("tr", profileLabel)- .scrollIntoView()- .should("be.visible")- .realClick();- cy.wait("@conversationDetail").its("response.statusCode").should("eq", 200);-}--function assertConversationDetailProfile(profileLabel: string): void {- H.main().within(() => {- cy.findByRole("heading", { name: SEEDED_CONVERSATION_TITLE }).should(- "be.visible",- );- cy.findByText(profileLabel).should("be.visible");- cy.findByText("Total tokens").should("be.visible");- });}describe("scenarios > metabot > usage auditing", () => {@@ -414,7 +78,7 @@ describe("scenarios > metabot > usage auditing", () => {seedUsageAuditingData();});- it("shows conversation usage stats charts", () => {+ it("renders the usage stats charts from the audit views", () => {visitUsageStatsPage();H.main().within(() => {@@ -425,388 +89,38 @@ describe("scenarios > metabot > usage auditing", () => {"true",);});- assertMetricChartsRendered("conversations");- });-- it("renders usage stats charts for selected date shortcuts on conversations", () => {- visitUsageStatsPage();-- const metric = "conversations";-- DATE_FILTER_CASES.forEach((label) => {- cy.log(`${METRIC_TAB_NAMES[metric]} date filter: ${label}`);- selectDateFilter(label);- assertMetricChartsRenderedForDate(metric, label);- });+ CONVERSATION_CHART_TITLES.forEach(assertChartRendered);});- it("renders usage stats charts for selected date shortcuts on tokens", () => {- visitUsageStatsPage();-- const metric = "tokens";- selectMetricTab(metric);-- DATE_FILTER_CASES.forEach((label) => {- cy.log(`${METRIC_TAB_NAMES[metric]} date filter: ${label}`);- selectDateFilter(label);-- assertMetricChartsRenderedForDate(metric, label);- });- });-- it("renders usage stats charts for selected date shortcuts on messages", () => {- visitUsageStatsPage();-- const metric = "messages";- selectMetricTab(metric);-- DATE_FILTER_CASES.forEach((label) => {- cy.log(`${METRIC_TAB_NAMES[metric]} date filter: ${label}`);- selectDateFilter(label);-- assertMetricChartsRenderedForDate(metric, label);- });- });-- it("filters conversation charts by group", () => {- visitUsageStatsPage();- selectGroupFilter("Administrators");-- assertMetricChartsRendered("conversations");-- selectGroupFilter("data");-- assertMetricChartsRendered("conversations");- });-- it("filters conversation charts by user", () => {- visitUsageStatsPage();- selectUserFilter("Robert Tableton");-- assertMetricChartsRendered("conversations");-- selectUserFilter("Bobby Tables");-- assertMetricChartsRendered("conversations");- });-- it("shows tenant filters and tenant charts when tenants are enabled", () => {- setupUsageAuditingTenants().then(() => {- visitUsageStatsPage();-- H.main().findByDisplayValue("All tenants").should("be.visible");- assertChartRendered(TENANT_CONVERSATIONS_CHART_TITLE);- });- });-- it("filters conversation charts by tenant", () => {- setupUsageAuditingTenants().then(({ bobbyTenant, robertTenant }) => {- visitUsageStatsPage();-- selectTenantFilter(bobbyTenant.name);- assertMetricChartsRendered("conversations");- assertChartRendered(TENANT_CONVERSATIONS_CHART_TITLE);-- selectTenantFilter(robertTenant.name);- assertMetricChartsRendered("conversations");- assertChartRendered(TENANT_CONVERSATIONS_CHART_TITLE);- });- });-- it("filters the conversations list by tenant", () => {- setupUsageAuditingTenants().then(({ bobbyTenant, robertTenant }) => {- visitConversationsPage(- "/admin/metabot/usage-auditing/conversations?date=past7days~",- );-- H.main().findByDisplayValue("All tenants").should("be.visible");- selectTenantFilter(bobbyTenant.name, "@conversations");- cy.location("search").should("include", `tenant=${bobbyTenant.id}`);- assertConversationTableContains([- "Bobby Tables",- "Internal",- "NLQ",- "Slackbot",- "Transforms codegen",- "10.0.0.1",- "10.0.0.2",- "10.0.0.6",- "10.0.0.7",- ]);- assertConversationTableDoesNotContain([- "Robert Tableton",- "10.0.0.3",- "10.0.0.4",- "10.0.0.5",- ]);-- selectTenantFilter(robertTenant.name, "@conversations");- cy.location("search").should("include", `tenant=${robertTenant.id}`);- assertConversationTableContains([- "Robert Tableton",- "SQL",- "Documents",- "Embedding",- "10.0.0.3",- "10.0.0.4",- "10.0.0.5",- ]);- assertConversationTableDoesNotContain([- "Bobby Tables",- "10.0.0.1",- "10.0.0.2",- "10.0.0.6",- "10.0.0.7",- ]);- });- });-- it("drills through from the tenants chart to the conversations list", () => {- setupUsageAuditingTenants().then(({ bobbyTenant, robertTenant }) => {- visitUsageStatsPage("/admin/metabot/usage-auditing?date=past7days~");- interceptConversationsApi();-- assertMetricChartsRendered("conversations");-- clickRowChartBarForLabel(- TENANT_CONVERSATIONS_CHART_TITLE,- bobbyTenant.name,- );-- waitForConversations();- cy.location("pathname").should(- "eq",- "/admin/metabot/usage-auditing/conversations",- );- cy.location("search").should("include", `tenant=${bobbyTenant.id}`);- H.main().findByDisplayValue(bobbyTenant.name).should("be.visible");- assertConversationTableContains([- "Bobby Tables",- "Internal",- "NLQ",- "Slackbot",- "Transforms codegen",- "10.0.0.1",- "10.0.0.2",- "10.0.0.6",- "10.0.0.7",- ]);- assertConversationTableDoesNotContain([- robertTenant.name,- "Robert Tableton",- "10.0.0.3",- "10.0.0.4",- "10.0.0.5",- ]);- });- });-- it("drills through from the groups chart to the conversations list", () => {- visitUsageStatsPage("/admin/metabot/usage-auditing?date=past7days~");- interceptConversationsApi();-- assertMetricChartsRendered("conversations");-- clickRowChartBarForLabel(- "Groups with most conversations",- "Administrators",- );-- waitForConversations();- cy.location("pathname").should(- "eq",- "/admin/metabot/usage-auditing/conversations",- );- cy.location("search").should("include", `group=${ADMINISTRATORS_GROUP_ID}`);- H.main().findByDisplayValue("Administrators").should("be.visible");- assertConversationTableContains([- "Bobby Tables",- "Internal",- "NLQ",- "10.0.0.1",- "10.0.0.2",- ]);- assertConversationTableDoesNotContain([- "Robert Tableton",- "10.0.0.3",- "10.0.0.4",- "10.0.0.5",- "10.0.0.99",- ]);- });-- it("drills through from conversation charts to the conversations list and updates list filters", () => {- visitUsageStatsPage();- interceptConversationsApi();-- assertMetricChartsRendered("conversations");-- clickRowChartBarForLabel("Users with most conversations", "Bobby Tables");-- waitForConversations();- cy.location("pathname").should(- "eq",- "/admin/metabot/usage-auditing/conversations",- );- cy.location("search").should("include", `user=${ADMIN_USER_ID}`);- H.main().findByDisplayValue("Bobby Tables").should("be.visible");- assertConversationTableContains([- "Bobby Tables",- "Internal",- "NLQ",- "10.0.0.1",- "10.0.0.2",- ]);- assertConversationTableDoesNotContain([- "Robert Tableton",- "10.0.0.3",- "10.0.0.99",- ]);-- selectUserFilter("Robert Tableton", "@conversations");- assertConversationTableContains([- "Robert Tableton",- "SQL",- "Documents",- "Embedding",- "10.0.0.3",- "10.0.0.4",- "10.0.0.5",- ]);- assertConversationTableDoesNotContain(["Bobby Tables", "10.0.0.1"]);-- selectDateFilter("Yesterday", "@conversations");- assertConversationTableContains([- "Robert Tableton",- "SQL",- "Documents",- "10.0.0.3",- "10.0.0.4",- ]);- assertConversationTableDoesNotContain([- "Embedding",- "10.0.0.5",- "Bobby Tables",- ]);- });-- it("drills through from the conversations by day chart to the conversations list", () => {- getUsageAuditingSeed().then(({ body }) => {- visitUsageStatsPage("/admin/metabot/usage-auditing?date=past7days~");- interceptConversationsApi();-- clickLastTimeseriesChartDot("Conversations by day");-- waitForConversations();- cy.location("pathname").should(- "eq",- "/admin/metabot/usage-auditing/conversations",- );- cy.location("search").should("include", `date=${body.date}`);- assertTodayConversationTable();- });- });-- it("drills through from the conversations by hour chart to the conversations list", () => {- getUsageAuditingSeed().then(({ body }) => {- visitUsageStatsPage(`/admin/metabot/usage-auditing?date=${body.date}`);- interceptConversationsApi();-- clickLastTimeseriesChartDot("Conversations by hour");-- waitForConversations();- cy.location("pathname").should(- "eq",- "/admin/metabot/usage-auditing/conversations",- );- assertHourDateFilterInUrl();- assertLatestHourConversationTable();- });- });-- it("opens conversation details from the list for each main profile", () => {- visitConversationsPage(- "/admin/metabot/usage-auditing/conversations?date=past7days~",- );-- MAIN_PROFILE_LABELS.forEach((profileLabel, index) => {- cy.log(`Profile: ${profileLabel}`);- H.main()- .findByRole("heading", { name: "Conversations" })- .should("be.visible");-- openConversationFromProfile(profileLabel);- assertConversationDetailProfile(profileLabel);-- if (index < MAIN_PROFILE_LABELS.length - 1) {- cy.go("back");- cy.location("pathname").should(- "eq",- "/admin/metabot/usage-auditing/conversations",- );- }- });- });-- it("sorts the conversations table by each sortable column", () => {- const sortableColumns: Array<{ headerLabel: RegExp; sortBy: string }> = [- { headerLabel: /^User/, sortBy: "user" },- { headerLabel: /^Profile/, sortBy: "profile_id" },- { headerLabel: /^Date/, sortBy: "created_at" },- { headerLabel: /^Messages/, sortBy: "message_count" },- { headerLabel: /^Tokens/, sortBy: "total_tokens" },- { headerLabel: /^IP/, sortBy: "ip_address" },- ];-+ it("lists the seeded conversations and opens their details", () => {visitConversationsPage();- for (const { headerLabel, sortBy } of sortableColumns) {- H.main()- .findByRole("table")- .findByRole("button", { name: headerLabel })- .click();-- cy.wait("@conversations")- .its("request.url")- .should("include", `sort_by=${sortBy}`);- }- });-- it("shows message usage stats charts", () => {- visitUsageStatsPage();- H.main().findByRole("tab", { name: "Messages" }).realClick();-- cy.url().should("include", "metric=messages");- H.main()- .findByRole("tab", { name: "Messages" })- .should("have.attr", "aria-selected", "true");- assertMetricChartsRendered("messages");- });-- it("shows token usage stats charts", () => {- visitUsageStatsPage();- H.main().findByRole("tab", { name: "Tokens" }).realClick();-- cy.url().should("include", "metric=tokens");H.main()- .findByRole("tab", { name: "Tokens" })- .should("have.attr", "aria-selected", "true");- assertMetricChartsRendered("tokens");- });+ .findByRole("table")+ .within(() => {+ ["Bobby Tables", "Robert Tableton", "NLQ", "10.0.0.1"].forEach(+ (label) => {+ cy.findAllByText(label)+ .filter(":visible")+ .should("have.length.greaterThan", 0);+ },+ );+ });- it("uses hourly stats for a single-day date filter", () => {- getUsageAuditingSeed().then(({ body }) => {- visitUsageStatsPage(`/admin/metabot/usage-auditing?date=${body.date}`);+ cy.get("tbody")+ .contains("tr", "NLQ")+ .scrollIntoView()+ .should("be.visible")+ .realClick();+ cy.wait("@conversationDetail").its("response.statusCode").should("eq", 200);- H.main().within(() => {- cy.findByRole("heading", { name: "Usage stats" }).should("be.visible");- cy.findByText("Conversations by day").should("not.exist");- });- assertChartRendered("Conversations by hour");- METRIC_CHART_TITLES.conversations.slice(1).forEach((title) => {- assertChartRendered(title);- });+ H.main().within(() => {+ cy.findByRole("heading", { name: SEEDED_CONVERSATION_TITLE }).should(+ "be.visible",+ );+ cy.findByText("NLQ").should("be.visible");+ cy.findByText("Total tokens").should("be.visible");});});});diff --git a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/BreakoutChart.tsx b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/BreakoutChart.tsxindex 99f3736e376..05d375efc59 100644--- a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/BreakoutChart.tsx+++ b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/BreakoutChart.tsx@@ -46,7 +46,7 @@ export function BreakoutChart({...rest}: Props) {if (!provider || !table || !groupMembersTable) {- return <Skeleton h={h} />;+ return <Skeleton h={h} data-testid="breakout-chart-skeleton" />;}return (<BreakoutChartInnerdiff --git a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/BreakoutChart.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/BreakoutChart.unit.spec.tsxnew file mode 100644index 00000000000..dca5c771121--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/BreakoutChart.unit.spec.tsx@@ -0,0 +1,210 @@+import fetchMock from "fetch-mock";++import { renderWithProviders, screen } from "__support__/ui";+import type { DateFilterValue } from "metabase/querying/common/types";+import { registerVisualizations } from "metabase/visualizations/register";+import type { Dataset } from "metabase-types/api";+import {+ createMockColumn,+ createMockDataset,+ createMockDatasetData,+} from "metabase-types/api/mocks";++import { BreakoutChart } from "./BreakoutChart";+import { ConversationsByDayChart } from "./ConversationsByDayChart";+import { buildAuditViewsFixture } from "./audit-views-fixture";+import {+ type UsageStatsMetric,+ buildSourceBreakoutQuery,+ tableForMetric,+} from "./query-utils";+import type { ChartDataSources } from "./types";++registerVisualizations();++const LAST_30_DAYS: DateFilterValue = {+ type: "relative",+ unit: "day",+ value: -30,+ options: { includeCurrent: true },+};++const SINGLE_DAY: DateFilterValue = {+ type: "specific",+ operator: "=",+ values: [new Date(2026, 3, 17)],+ hasTime: false,+};++const fixture = buildAuditViewsFixture();++const METRICS: UsageStatsMetric[] = ["conversations", "messages", "tokens"];++const buildTitles = (suffix: string): Record<UsageStatsMetric, string> => ({+ conversations: `Conversations ${suffix}`,+ messages: `Messages ${suffix}`,+ tokens: `Tokens ${suffix}`,+});++const TITLES = buildTitles("by source");+const DAILY_TITLES = buildTitles("by day");+const HOURLY_TITLES = buildTitles("by hour");++const AGGREGATION_NAMES: Record<UsageStatsMetric, string[]> = {+ conversations: ["count"],+ messages: ["sum"],+ tokens: ["sum", "sum_2"],+};++type BreakoutColumnSpec = Parameters<typeof createMockColumn>[0];++const SOURCE_BREAKOUT: BreakoutColumnSpec = {+ source: "breakout",+ name: "source_name",+ display_name: "Source",+};++const TIMESERIES_BREAKOUT: BreakoutColumnSpec = {+ source: "breakout",+ name: "created_at",+ display_name: "Created at",+ base_type: "type/DateTimeWithLocalTZ",+ unit: "day",+};++function buildDataset(+ metric: UsageStatsMetric,+ breakoutColumn: BreakoutColumnSpec,+ breakoutValues: string[],+): Dataset {+ const aggregationNames = AGGREGATION_NAMES[metric];++ return createMockDataset({+ data: createMockDatasetData({+ cols: [+ createMockColumn(breakoutColumn),+ ...aggregationNames.map((name) =>+ createMockColumn({+ source: "aggregation",+ name,+ display_name: name,+ base_type: "type/BigInteger",+ }),+ ),+ ],+ rows: breakoutValues.map((value, index) => [+ value,+ ...aggregationNames.map((_, offset) => (index + 1) * 10 + offset),+ ]),+ }),+ row_count: breakoutValues.length,+ });+}++function dataSources(metric: UsageStatsMetric): ChartDataSources {+ return {+ provider: fixture.provider,+ table: tableForMetric(metric, fixture.conversations, fixture.usageLog),+ groupMembersTable: fixture.groupMembers,+ };+}++function setupBreakoutChart(metric: UsageStatsMetric, display: "bar" | "row") {+ fetchMock.post(+ "path:/api/dataset",+ buildDataset(metric, SOURCE_BREAKOUT, ["Slackbot", "Internal"]),+ );++ return renderWithProviders(+ <BreakoutChart+ {...dataSources(metric)}+ metric={metric}+ dateFilter={LAST_30_DAYS}+ titles={TITLES}+ display={display}+ buildQuery={(opts) =>+ buildSourceBreakoutQuery({ ...opts, breakoutColumn: "source_name" })+ }+ />,+ );+}++function setupTimeseriesChart(+ metric: UsageStatsMetric,+ dateFilter: DateFilterValue = LAST_30_DAYS,+) {+ fetchMock.post(+ "path:/api/dataset",+ buildDataset(metric, TIMESERIES_BREAKOUT, [+ "2026-04-15T00:00:00",+ "2026-04-16T00:00:00",+ ]),+ );++ return renderWithProviders(+ <ConversationsByDayChart+ {...dataSources(metric)}+ metric={metric}+ dateFilter={dateFilter}+ />,+ );+}++describe("metabot usage stats charts", () => {+ it.each(METRICS)("renders a bar chart for %s", async (metric) => {+ setupBreakoutChart(metric, "bar");++ expect(await screen.findByText(TITLES[metric])).toBeInTheDocument();+ const container = await screen.findByTestId("chart-container");+ expect(container.querySelector("svg")).toBeInTheDocument();+ });++ it("renders a row chart", async () => {+ setupBreakoutChart("conversations", "row");++ expect(await screen.findByText(TITLES.conversations)).toBeInTheDocument();+ expect(+ await screen.findByTestId("row-chart-container"),+ ).toBeInTheDocument();+ });++ it.each(METRICS)("renders the daily timeseries for %s", async (metric) => {+ setupTimeseriesChart(metric);++ expect(await screen.findByText(DAILY_TITLES[metric])).toBeInTheDocument();+ const container = await screen.findByTestId("chart-container");+ expect(container.querySelector("svg")).toBeInTheDocument();+ });++ it.each(METRICS)(+ "renders the hourly timeseries for %s with a single-day filter",+ async (metric) => {+ setupTimeseriesChart(metric, SINGLE_DAY);++ expect(+ await screen.findByText(HOURLY_TITLES[metric]),+ ).toBeInTheDocument();+ expect(await screen.findByTestId("chart-container")).toBeInTheDocument();+ },+ );++ it("shows a skeleton until the audit table metadata is available", () => {+ renderWithProviders(+ <BreakoutChart+ provider={null}+ table={null}+ groupMembersTable={null}+ metric="conversations"+ dateFilter={LAST_30_DAYS}+ titles={TITLES}+ display="bar"+ buildQuery={(opts) =>+ buildSourceBreakoutQuery({ ...opts, breakoutColumn: "source_name" })+ }+ />,+ );++ expect(screen.getByTestId("breakout-chart-skeleton")).toBeInTheDocument();+ expect(screen.queryByText(TITLES.conversations)).not.toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/ConversationStatsPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/ConversationStatsPage.unit.spec.tsxnew file mode 100644index 00000000000..62d71d335cc--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/ConversationStatsPage.unit.spec.tsx@@ -0,0 +1,514 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import { setupEnterprisePlugins } from "__support__/enterprise";+import {+ setupGroupsEndpoint,+ setupTenantEntpoints,+ setupUsersEndpoints,+} from "__support__/server-mocks";+import { mockSettings } from "__support__/settings";+import { renderWithProviders, screen, waitFor, within } from "__support__/ui";+import { createMockState } from "metabase/redux/store/mocks";+import { Route, withRouteProps } from "metabase/router";+import type { ClickObject } from "metabase-lib";+import type { Dataset, RawSeries, RowValue } from "metabase-types/api";+import {+ createMockColumn,+ createMockDataset,+ createMockDatasetData,+ createMockTokenFeatures,+} from "metabase-types/api/mocks";++import { AUDIT_DB_ID } from "../../constants";+import {+ ADMIN_GROUP,+ ALL_USERS_GROUP,+ BOBBY,+ BOBBY_TENANT,+ DATA_GROUP,+ ROBERT,+ ROBERT_TENANT,+ selectFilterOption,+} from "../../tests/fixtures";++import { ConversationStatsPage } from "./ConversationStatsPage";+import { buildAuditViewsFixture } from "./audit-views-fixture";++jest.mock("metabase/admin/ai/MetabotAdminLayout", () => ({+ MetabotAdminLayout: ({ children }: { children: React.ReactNode }) => children,+}));++jest.mock("metabase/visualizations/components/Visualization", () => {+ type StubProps = {+ rawSeries?: RawSeries | null;+ handleVisualizationClick?: (clicked: ClickObject | null) => void;+ };++ const StubVisualization = ({+ rawSeries,+ handleVisualizationClick,+ }: StubProps) => {+ const data = rawSeries?.[0]?.data;+ const dimensionIndex =+ data?.cols.findIndex((col) => col.source === "breakout") ?? -1;++ if (!data || dimensionIndex < 0) {+ return <div data-testid="visualization" />;+ }++ const dimensionColumn = data.cols[dimensionIndex];++ return (+ <div data-testid="visualization">+ {data.rows.map((row, index) => (+ <button+ key={index}+ onClick={() =>+ handleVisualizationClick?.({+ dimensions: [+ { value: row[dimensionIndex], column: dimensionColumn },+ ],+ })+ }+ >+ {String(row[dimensionIndex])}+ </button>+ ))}+ </div>+ );+ };++ return { __esModule: true, default: StubVisualization };+});++const RoutedConversationStatsPage = withRouteProps(ConversationStatsPage);++const STATS_PATH = "/admin/metabot/usage-auditing";+const CONVERSATIONS_PATH = "/admin/metabot/usage-auditing/conversations";++const { database: auditDatabase } = buildAuditViewsFixture();++const FIELD_NAME_BY_ID = new Map(+ (auditDatabase.tables ?? []).flatMap((table) =>+ (table.fields ?? []).map(+ (field) => [Number(field.id), field.name] as const,+ ),+ ),+);++function fieldIdByName(tableName: string, fieldName: string): number {+ const table = auditDatabase.tables?.find(({ name }) => name === tableName);+ const field = table?.fields?.find(({ name }) => name === fieldName);+ if (field == null) {+ throw new Error(`No field ${tableName}.${fieldName} in the audit fixture`);+ }+ return Number(field.id);+}++const CONVERSATIONS_USER_ID_FIELD_ID = fieldIdByName(+ "v_metabot_conversations",+ "user_id",+);+const CONVERSATIONS_TENANT_ID_FIELD_ID = fieldIdByName(+ "v_metabot_conversations",+ "tenant_id",+);++const DAY_BUCKETS = ["2026-04-15T00:00:00", "2026-04-16T00:00:00"];+const HOUR_BUCKETS = ["2026-04-17T09:00:00", "2026-04-17T10:00:00"];+const SINGLE_DAY = "2026-04-17";++const BREAKOUT_VALUES: Record<string, RowValue[]> = {+ source_name: ["Slackbot", "Internal"],+ profile_name: ["NLQ", "SQL"],+ user_display_name: ["Bobby Tables", "Robert Tableton"],+ ip_address: ["10.0.0.1", "10.0.0.2"],+ tenant_id: [BOBBY_TENANT.id, ROBERT_TENANT.id],+ group_name: ["Administrators", "data"],+};++type FieldRef = [string, { "temporal-unit"?: string }, number];+type FilterClause = [string, object, FieldRef, ...unknown[]];+type RequestStage = {+ aggregation?: unknown[];+ breakout?: FieldRef[];+ filters?: FilterClause[];+ joins?: unknown[];+};++function parseStage(body: unknown): RequestStage | null {+ if (typeof body !== "string") {+ return null;+ }+ return JSON.parse(body).stages?.[0] ?? null;+}++function getBreakoutValues(stage: RequestStage): RowValue[] {+ const [breakout] = stage.breakout ?? [];+ if (!breakout) {+ return [];+ }+ const columnName = FIELD_NAME_BY_ID.get(breakout[2]);+ if (columnName === "created_at") {+ return breakout[1]?.["temporal-unit"] === "hour"+ ? HOUR_BUCKETS+ : DAY_BUCKETS;+ }+ const values = BREAKOUT_VALUES[columnName ?? ""];+ if (!values) {+ throw new Error(+ `Unexpected breakout column in dataset request: ${String(columnName)}`,+ );+ }+ return values;+}++function buildDatasetResponse(body: unknown): Dataset {+ const stage = parseStage(body);+ const values = stage ? getBreakoutValues(stage) : [];+ const aggregationNames =+ (stage?.aggregation ?? []).length > 1 ? ["sum", "sum_2"] : ["count"];++ return createMockDataset({+ data: createMockDatasetData({+ cols: [+ createMockColumn({ source: "breakout", name: "dimension" }),+ ...aggregationNames.map((name) =>+ createMockColumn({ source: "aggregation", name }),+ ),+ ],+ rows: values.map((value, index) => [+ value,+ ...aggregationNames.map((_, offset) => (index + 1) * 10 + offset),+ ]),+ }),+ database_id: AUDIT_DB_ID,+ row_count: values.length,+ });+}++function getDatasetStages(): RequestStage[] {+ return fetchMock.callHistory+ .calls("dataset")+ .map((call) => parseStage(call.options?.body))+ .filter((stage): stage is RequestStage => stage != null);+}++function getFilteredFieldIds(): number[] {+ return getDatasetStages().flatMap((stage) =>+ (stage.filters ?? []).map((filter) => filter[2]?.[2]),+ );+}++type SetupOpts = {+ initialRoute?: string;+ hasTenants?: boolean;+};++function setup({+ initialRoute = STATS_PATH,+ hasTenants = false,+}: SetupOpts = {}) {+ setupEnterprisePlugins();++ fetchMock.get(`path:/api/database/${AUDIT_DB_ID}/metadata`, auditDatabase);+ fetchMock.post(+ "path:/api/dataset",+ (call) => buildDatasetResponse(call?.options.body),+ { name: "dataset" },+ );+ setupUsersEndpoints([BOBBY, ROBERT]);+ setupGroupsEndpoint([ALL_USERS_GROUP, ADMIN_GROUP, DATA_GROUP]);+ setupTenantEntpoints([BOBBY_TENANT, ROBERT_TENANT]);++ return renderWithProviders(+ <>+ <Route path={STATS_PATH} element={<RoutedConversationStatsPage />} />+ <Route+ path={CONVERSATIONS_PATH}+ element={<div data-testid="conversations-page" />}+ />+ </>,+ {+ initialRoute,+ withRouter: true,+ storeInitialState: createMockState({+ settings: mockSettings({+ "token-features": createMockTokenFeatures({+ audit_app: true,+ tenants: hasTenants,+ }),+ "use-tenants": hasTenants,+ }),+ }),+ },+ );+}++async function findChartCard(title: string): Promise<HTMLElement> {+ const { parentElement } = await screen.findByText(title);+ if (!parentElement) {+ throw new Error(`No chart card found for "${title}"`);+ }+ return parentElement;+}++async function drillInto(chartTitle: string, dimensionLabel: string) {+ const card = await findChartCard(chartTitle);+ await userEvent.click(+ await within(card).findByRole("button", { name: dimensionLabel }),+ );+}++function chartTitles(label: string): string[] {+ const lowerLabel = label.toLowerCase();+ return [+ `${label} by day`,+ `${label} by source`,+ `${label} by profile`,+ `Groups with most ${lowerLabel}`,+ `Users with most ${lowerLabel}`,+ `IP addresses with most ${lowerLabel}`,+ ];+}++describe("ConversationStatsPage", () => {+ describe("charts", () => {+ it("renders every conversation chart with the Conversations tab selected", async () => {+ setup();++ expect(+ await screen.findByRole("heading", { name: "Usage stats" }),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("tab", { name: "Conversations" }),+ ).toHaveAttribute("aria-selected", "true");++ for (const title of chartTitles("Conversations")) {+ expect(await screen.findByText(title)).toBeInTheDocument();+ }+ });++ it.each([+ { tab: "Tokens", metric: "tokens" },+ { tab: "Messages", metric: "messages" },+ ])(+ "switches to the $metric charts and records the metric in the url",+ async ({ tab, metric }) => {+ const { history } = setup();++ await screen.findByText("Conversations by day");+ await userEvent.click(screen.getByRole("tab", { name: tab }));++ expect(screen.getByRole("tab", { name: tab })).toHaveAttribute(+ "aria-selected",+ "true",+ );+ for (const title of chartTitles(tab)) {+ expect(await screen.findByText(title)).toBeInTheDocument();+ }+ await waitFor(() => {+ expect(history?.getCurrentLocation().query).toMatchObject({+ metric,+ });+ });+ },+ );++ it("buckets the timeseries by hour for a single-day date filter", async () => {+ setup({ initialRoute: `${STATS_PATH}?date=${SINGLE_DAY}` });++ expect(+ await screen.findByText("Conversations by hour"),+ ).toBeInTheDocument();+ expect(+ screen.queryByText("Conversations by day"),+ ).not.toBeInTheDocument();+ expect(+ await screen.findByText("Users with most conversations"),+ ).toBeInTheDocument();+ });++ it("buckets the timeseries by hour after picking the Today shortcut", async () => {+ setup();++ await screen.findByText("Conversations by day");+ await selectFilterOption("conversation-filters-date-select", "Today");++ expect(+ await screen.findByText("Conversations by hour"),+ ).toBeInTheDocument();+ });++ it("keeps daily buckets after picking a multi-day shortcut", async () => {+ setup({ initialRoute: `${STATS_PATH}?date=${SINGLE_DAY}` });++ await screen.findByText("Conversations by hour");+ await selectFilterOption(+ "conversation-filters-date-select",+ "Last 7 days",+ );++ expect(+ await screen.findByText("Conversations by day"),+ ).toBeInTheDocument();+ });+ });++ describe("tenants", () => {+ it("hides the tenant filter and tenant chart when tenants are disabled", async () => {+ setup();++ await screen.findByText("Conversations by day");++ expect(+ screen.queryByTestId("conversation-filters-tenant-select"),+ ).not.toBeInTheDocument();+ expect(+ screen.queryByText("Tenants with most conversations"),+ ).not.toBeInTheDocument();+ });++ it("shows the tenant filter and labels tenant bars with tenant names", async () => {+ setup({ hasTenants: true });++ expect(+ await screen.findByDisplayValue("All tenants"),+ ).toBeInTheDocument();++ const card = await findChartCard("Tenants with most conversations");+ expect(+ await within(card).findByRole("button", { name: BOBBY_TENANT.name }),+ ).toBeInTheDocument();+ expect(+ within(card).getByRole("button", { name: ROBERT_TENANT.name }),+ ).toBeInTheDocument();+ });++ it("filters the charts by the selected tenant", async () => {+ const { history } = setup({ hasTenants: true });++ await screen.findByText("Conversations by day");+ await selectFilterOption(+ "conversation-filters-tenant-select",+ BOBBY_TENANT.name,+ );++ await waitFor(() => {+ expect(history?.getCurrentLocation().query).toMatchObject({+ tenant: String(BOBBY_TENANT.id),+ });+ });+ await waitFor(() => {+ expect(getFilteredFieldIds()).toContain(+ CONVERSATIONS_TENANT_ID_FIELD_ID,+ );+ });+ });+ });++ describe("filters", () => {+ it("applies the selected user to the url and to the chart queries", async () => {+ const { history } = setup();++ await screen.findByText("Conversations by day");+ await selectFilterOption(+ "conversation-filters-user-select",+ "Robert Tableton",+ );++ await waitFor(() => {+ expect(history?.getCurrentLocation().query).toMatchObject({+ user: String(ROBERT.id),+ });+ });+ await waitFor(() => {+ expect(getFilteredFieldIds()).toContain(CONVERSATIONS_USER_ID_FIELD_ID);+ });+ });++ it("joins group members when a group is selected", async () => {+ const { history } = setup();++ await screen.findByText("Conversations by day");+ await selectFilterOption("conversation-filters-group-select", "data");++ await waitFor(() => {+ expect(history?.getCurrentLocation().query).toMatchObject({+ group: String(DATA_GROUP.id),+ });+ });+ await waitFor(() => {+ const joinedStages = getDatasetStages().filter(+ (stage) => (stage.joins ?? []).length > 0,+ );+ expect(joinedStages.length).toBeGreaterThan(0);+ });+ });+ });++ describe("drill-through to the conversations list", () => {+ it.each([+ {+ chart: "Users with most conversations",+ label: "Bobby Tables",+ query: { user: String(BOBBY.id) },+ },+ {+ chart: "Groups with most conversations",+ label: "Administrators",+ query: { group: String(ADMIN_GROUP.id) },+ },+ {+ chart: "Tenants with most conversations",+ label: BOBBY_TENANT.name,+ query: { tenant: String(BOBBY_TENANT.id) },+ setupOpts: { hasTenants: true },+ },+ {+ chart: "Conversations by day",+ label: "2026-04-16T00:00:00",+ query: { date: "2026-04-16" },+ },+ {+ chart: "Conversations by hour",+ label: "2026-04-17T10:00:00",+ query: { date: "2026-04-17T10:00:00~2026-04-17T11:00:00" },+ setupOpts: { initialRoute: `${STATS_PATH}?date=${SINGLE_DAY}` },+ },+ ])(+ "drills from the $chart chart",+ async ({ chart, label, query, setupOpts }) => {+ const { history } = setup(setupOpts);++ await drillInto(chart, label);++ expect(history?.getCurrentLocation().pathname).toBe(CONVERSATIONS_PATH);+ expect(history?.getCurrentLocation().query).toEqual(query);+ },+ );++ it("carries the active filters over to the conversations list", async () => {+ const { history } = setup({+ initialRoute: `${STATS_PATH}?date=past6days~&group=${ADMIN_GROUP.id}`,+ });++ await drillInto("Users with most conversations", "Bobby Tables");++ expect(history?.getCurrentLocation().query).toEqual({+ date: "past6days~",+ group: String(ADMIN_GROUP.id),+ user: String(BOBBY.id),+ });+ });++ it("does not drill on charts without a dimension handler", async () => {+ const { history } = setup();++ await drillInto("IP addresses with most conversations", "10.0.0.1");++ expect(history?.getCurrentLocation().pathname).toBe(STATS_PATH);+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/audit-views-fixture.ts b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/audit-views-fixture.tsindex 63aeb9929c4..aec12d5f892 100644--- a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/audit-views-fixture.ts+++ b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationStatsPage/audit-views-fixture.ts@@ -7,7 +7,8 @@ import {createMockTable,} from "metabase-types/api/mocks";-const AUDIT_DB_ID = 13371337;+import { AUDIT_DB_ID } from "../../constants";+const CONVERSATIONS_TABLE_ID = 1001;const USAGE_LOG_TABLE_ID = 1002;const GROUP_MEMBERS_TABLE_ID = 1003;@@ -112,6 +113,7 @@ export function buildAuditViewsFixture() {const provider = Lib.metadataProvider(AUDIT_DB_ID, metadata);return {+ database,provider,conversations: Lib.tableOrCardMetadata(provider, CONVERSATIONS_TABLE_ID)!,usageLog: Lib.tableOrCardMetadata(provider, USAGE_LOG_TABLE_ID)!,diff --git a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/ConversationsPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/ConversationsPage.unit.spec.tsxindex 485d25e203a..413774d23fe 100644--- a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/ConversationsPage.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/ConversationsPage.unit.spec.tsx@@ -1,12 +1,31 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import { setupEnterprisePlugins } from "__support__/enterprise";import {setupGroupsEndpoint,setupListMetabotAnalyticsConversationsEndpoint,+ setupTenantEntpoints,setupUsersEndpoints,} from "__support__/server-mocks";-import { renderWithProviders, screen, within } from "__support__/ui";+import { mockSettings } from "__support__/settings";+import { renderWithProviders, screen, waitFor, within } from "__support__/ui";+import { createMockState } from "metabase/redux/store/mocks";import { Route, withRouteProps } from "metabase/router";-import { createMockUser } from "metabase-types/api/mocks";+import {+ createMockTokenFeatures,+ createMockUser,+} from "metabase-types/api/mocks";+import {+ ALL_USERS_GROUP,+ BOBBY,+ BOBBY_TENANT,+ DATA_GROUP,+ ROBERT,+ ROBERT_TENANT,+ selectFilterOption,+} from "../../tests/fixtures";import type { ConversationSummary } from "../../types";import { ConversationsPage } from "./ConversationsPage";@@ -17,6 +36,9 @@ jest.mock("metabase/admin/ai/MetabotAdminLayout", () => ({MetabotAdminLayout: ({ children }: { children: React.ReactNode }) => children,}));+const CONVERSATIONS_PATH = "/admin/metabot/usage-auditing/conversations";+const CONVERSATIONS_ENDPOINT = "path:/api/ee/metabot-analytics/conversations";+function createSummary(opts: Partial<ConversationSummary> &Pick<ConversationSummary, "conversation_id">,@@ -44,39 +66,278 @@ function createSummary(};}-function setup(conversations: ConversationSummary[]) {- setupListMetabotAnalyticsConversationsEndpoint(conversations);- setupUsersEndpoints([]);- setupGroupsEndpoint([]);+const BOBBY_CONVERSATION = createSummary({+ conversation_id: "c1",+ title: "How many orders?",+ profile_id: "nlq",+ ip_address: "10.0.0.1",+ message_count: 4,+ total_tokens: 1200,+ user: {+ id: BOBBY.id,+ first_name: BOBBY.first_name,+ last_name: BOBBY.last_name,+ tenant_id: BOBBY_TENANT.id,+ },+});++const ROBERT_CONVERSATION = createSummary({+ conversation_id: "c2",+ title: null,+ profile_id: "sql",+ ip_address: "10.0.0.3",+ user: {+ id: ROBERT.id,+ first_name: ROBERT.first_name,+ last_name: ROBERT.last_name,+ tenant_id: ROBERT_TENANT.id,+ },+});++type SetupOpts = {+ conversations?: ConversationSummary[];+ total?: number;+ initialRoute?: string;+ hasTenants?: boolean;+};++function setup({+ conversations = [BOBBY_CONVERSATION, ROBERT_CONVERSATION],+ total,+ initialRoute = CONVERSATIONS_PATH,+ hasTenants = false,+}: SetupOpts = {}) {+ setupEnterprisePlugins();+ setupListMetabotAnalyticsConversationsEndpoint(conversations, total);+ setupUsersEndpoints([BOBBY, ROBERT]);+ setupGroupsEndpoint([ALL_USERS_GROUP, DATA_GROUP]);+ setupTenantEntpoints([BOBBY_TENANT, ROBERT_TENANT]);+return renderWithProviders(- <Route path="/conversations" element={<RoutedConversationsPage />} />,+ <>+ <Route path={CONVERSATIONS_PATH} element={<RoutedConversationsPage />} />+ <Route+ path={`${CONVERSATIONS_PATH}/:conversationId`}+ element={<div data-testid="conversation-detail-page" />}+ />+ </>,{withRouter: true,- initialRoute: "/conversations",- storeInitialState: {+ initialRoute,+ storeInitialState: createMockState({currentUser: createMockUser({ is_superuser: true }),- },+ settings: mockSettings({+ "token-features": createMockTokenFeatures({+ audit_app: true,+ tenants: hasTenants,+ }),+ "use-tenants": hasTenants,+ }),+ }),},);}+async function assertRequestedWithParams(+ expected: Record<string, string>,+): Promise<void> {+ await waitFor(() => {+ expect(+ fetchMock.callHistory.called(CONVERSATIONS_ENDPOINT, { query: expected }),+ ).toBe(true);+ });+}+describe("ConversationsPage", () => {- it("surfaces conversation titles in the table, falling back to Untitled", async () => {- setup([- createSummary({ conversation_id: "c1", title: "How many orders?" }),- createSummary({ conversation_id: "c2", title: null }),- ]);+ describe("table", () => {+ it("renders the title, user, profile label and IP address of each conversation", async () => {+ setup();- expect(- await screen.findByRole("columnheader", { name: "Title" }),- ).toBeInTheDocument();-- const [, titledRow, untitledRow] = screen.getAllByRole("row");- expect(within(titledRow).getAllByRole("cell")[0]).toHaveTextContent(- "How many orders?",- );- expect(within(untitledRow).getAllByRole("cell")[0]).toHaveTextContent(- "Untitled",- );+ expect(await screen.findByText("How many orders?")).toBeInTheDocument();+ expect(+ screen.getByRole("columnheader", { name: "Title" }),+ ).toBeInTheDocument();++ const [, titledRow, untitledRow] = screen.getAllByRole("row");+ expect(within(titledRow).getAllByRole("cell")[0]).toHaveTextContent(+ "How many orders?",+ );+ expect(within(untitledRow).getAllByRole("cell")[0]).toHaveTextContent(+ "Untitled",+ );++ const table = screen.getByRole("table");+ expect(within(table).getByText("Bobby Tables")).toBeInTheDocument();+ expect(within(table).getByText("NLQ")).toBeInTheDocument();+ expect(within(table).getByText("10.0.0.1")).toBeInTheDocument();+ expect(within(table).getByText("Robert Tableton")).toBeInTheDocument();+ expect(within(table).getByText("SQL")).toBeInTheDocument();+ expect(within(table).getByText("10.0.0.3")).toBeInTheDocument();+ });++ it("shows an empty state when no conversations match", async () => {+ setup({ conversations: [] });++ expect(+ await screen.findByText("No conversations found"),+ ).toBeInTheDocument();+ });++ it("opens the conversation detail page when a row is clicked", async () => {+ const { history } = setup();++ await userEvent.click(await screen.findByText("How many orders?"));++ expect(+ await screen.findByTestId("conversation-detail-page"),+ ).toBeInTheDocument();+ expect(history?.getCurrentLocation().pathname).toBe(+ `${CONVERSATIONS_PATH}/${BOBBY_CONVERSATION.conversation_id}`,+ );+ });+ });++ describe("sorting", () => {+ it("requests the default sort on first load", async () => {+ setup();++ await screen.findByRole("table");+ await assertRequestedWithParams({+ sort_by: "created_at",+ sort_dir: "desc",+ });+ });++ it.each([+ { headerLabel: /^User/, sortBy: "user" },+ { headerLabel: /^Profile/, sortBy: "profile_id" },+ { headerLabel: /^Date/, sortBy: "created_at" },+ { headerLabel: /^Messages/, sortBy: "message_count" },+ { headerLabel: /^Tokens/, sortBy: "total_tokens" },+ { headerLabel: /^Cached tokens/, sortBy: "cache_read_tokens" },+ { headerLabel: /^IP/, sortBy: "ip_address" },+ ])("sorts by $sortBy", async ({ headerLabel, sortBy }) => {+ setup();++ const table = await screen.findByRole("table");+ fetchMock.clearHistory();+ await userEvent.click(+ within(table).getByRole("button", { name: headerLabel }),+ );++ await assertRequestedWithParams({ sort_by: sortBy, sort_dir: "asc" });+ });+ });++ describe("filters", () => {+ it("applies the filters from the url to the request and the filter selects", async () => {+ setup({+ hasTenants: true,+ initialRoute: `${CONVERSATIONS_PATH}?user=${ROBERT.id}&group=${DATA_GROUP.id}&tenant=${BOBBY_TENANT.id}&date=past6days~`,+ });++ await screen.findByRole("table");+ await assertRequestedWithParams({+ user_id: String(ROBERT.id),+ group_id: String(DATA_GROUP.id),+ tenant_id: String(BOBBY_TENANT.id),+ date: "past6days~",+ });++ expect(+ await screen.findByDisplayValue(ROBERT.common_name),+ ).toBeInTheDocument();+ expect(+ await screen.findByDisplayValue(DATA_GROUP.name),+ ).toBeInTheDocument();+ expect(+ await screen.findByDisplayValue(BOBBY_TENANT.name),+ ).toBeInTheDocument();+ });++ it("filters by user", async () => {+ const { history } = setup();++ await screen.findByRole("table");+ await selectFilterOption(+ "conversation-filters-user-select",+ "Robert Tableton",+ );++ await assertRequestedWithParams({ user_id: String(ROBERT.id) });+ await waitFor(() => {+ expect(history?.getCurrentLocation().query).toMatchObject({+ user: String(ROBERT.id),+ });+ });+ });++ it("filters by group", async () => {+ setup();++ await screen.findByRole("table");+ await selectFilterOption("conversation-filters-group-select", "data");++ await assertRequestedWithParams({ group_id: String(DATA_GROUP.id) });+ });++ it("filters by date", async () => {+ setup();++ await screen.findByRole("table");+ await selectFilterOption(+ "conversation-filters-date-select",+ "Last 7 days",+ );++ await assertRequestedWithParams({ date: "past6days~" });+ });++ it("filters by tenant when tenants are enabled", async () => {+ const { history } = setup({ hasTenants: true });++ expect(+ await screen.findByDisplayValue("All tenants"),+ ).toBeInTheDocument();+ await selectFilterOption(+ "conversation-filters-tenant-select",+ ROBERT_TENANT.name,+ );++ await assertRequestedWithParams({ tenant_id: String(ROBERT_TENANT.id) });+ await waitFor(() => {+ expect(history?.getCurrentLocation().query).toMatchObject({+ tenant: String(ROBERT_TENANT.id),+ });+ });+ });++ it("hides the tenant filter when tenants are disabled", async () => {+ setup();++ await screen.findByRole("table");+ expect(+ screen.queryByTestId("conversation-filters-tenant-select"),+ ).not.toBeInTheDocument();+ });+ });++ describe("pagination", () => {+ it("requests and renders the next page of conversations", async () => {+ const conversations = Array.from({ length: 26 }, (_, index) =>+ createSummary({+ conversation_id: `c${index}`,+ title: `Conversation ${index}`,+ }),+ );+ setup({ conversations });++ expect(await screen.findByText("Conversation 0")).toBeInTheDocument();+ await userEvent.click(screen.getByLabelText("Next page"));++ await assertRequestedWithParams({ offset: "25", limit: "25" });+ expect(await screen.findByText("Conversation 25")).toBeInTheDocument();+ expect(screen.queryByText("Conversation 0")).not.toBeInTheDocument();+ });});});diff --git a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/tests/fixtures.tsx b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/tests/fixtures.tsxnew file mode 100644index 00000000000..d35e497c346--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/tests/fixtures.tsx@@ -0,0 +1,46 @@+import userEvent from "@testing-library/user-event";++import { screen, within } from "__support__/ui";+import {+ createMockGroup,+ createMockTenant,+ createMockUser,+} from "metabase-types/api/mocks";++export const BOBBY = createMockUser({+ id: 1,+ first_name: "Bobby",+ last_name: "Tables",+ common_name: "Bobby Tables",+});++export const ROBERT = createMockUser({+ id: 2,+ first_name: "Robert",+ last_name: "Tableton",+ common_name: "Robert Tableton",+});++export const ALL_USERS_GROUP = createMockGroup({ id: 1, name: "All Users" });+export const ADMIN_GROUP = createMockGroup({ id: 2, name: "Administrators" });+export const DATA_GROUP = createMockGroup({ id: 5, name: "data" });++export const BOBBY_TENANT = createMockTenant({+ id: 1,+ name: "Bobby Analytics",+ slug: "bobby-analytics",+});++export const ROBERT_TENANT = createMockTenant({+ id: 2,+ name: "Robert Analytics",+ slug: "robert-analytics",+});++export async function selectFilterOption(testId: string, optionName: string) {+ await userEvent.click(screen.getByTestId(testId));+ const dropdown = await screen.findByRole("listbox");+ await userEvent.click(+ within(dropdown).getByRole("option", { name: optionName }),+ );+}diff --git a/frontend/test/__support__/server-mocks/metabot-analytics.ts b/frontend/test/__support__/server-mocks/metabot-analytics.tsindex 708b1f79f93..7aef51f5c83 100644--- a/frontend/test/__support__/server-mocks/metabot-analytics.ts+++ b/frontend/test/__support__/server-mocks/metabot-analytics.ts@@ -17,12 +17,18 @@ export function setupMetabotConversationEndpoint(export function setupListMetabotAnalyticsConversationsEndpoint(conversations: ConversationSummary[],+ total = conversations.length,) {- const response: ConversationsResponse = {- data: conversations,- total: conversations.length,- limit: conversations.length,- offset: 0,- };- fetchMock.get("path:/api/ee/metabot-analytics/conversations", response);+ fetchMock.get("path:/api/ee/metabot-analytics/conversations", (call) => {+ const params = new URL(call.url, "http://localhost").searchParams;+ const offset = Number(params.get("offset") ?? 0);+ const limit = Number(params.get("limit") ?? conversations.length);+ const response: ConversationsResponse = {+ data: conversations.slice(offset, offset + limit),+ total,+ limit,+ offset,+ };+ return response;+ });}