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
import { mockSettings } from "__support__/settings";
import { createMockState } from "__support__/state";
import { renderWithProviders, screen, waitFor } from "__support__/ui";
import { PLUGIN_DATA_APPS, reinitialize } from "metabase/plugins";
import type * as Router from "metabase/router";
import { Route, registerPagePrefetch } from "metabase/router";
import * as Urls from "metabase/urls";
import {
getAiAuditingRoutes,
getAiAuditingUpsellRoutes,
registerAiAuditingPagePrefetch,
} from "./routes";
jest.mock("metabase/router", () => ({
...jest.requireActual<typeof Router>("metabase/router"),
registerPagePrefetch: jest.fn(),
}));
jest.mock("./metabot-analytics/components/ConversationStatsPage", () => ({
ConversationStatsPage: () => <div>Usage stats page</div>,
}));
jest.mock("./metabot-analytics/components/ConversationsPage", () => ({
ConversationsPage: () => <div>Conversations page</div>,
}));
jest.mock("./metabot-analytics/components/ConversationDetailPage", () => ({
ConversationDetailPage: () => <div>Conversation detail page</div>,
}));
jest.mock(
"./metabot-analytics/components/MetabotAnalyticsUpsellPage/MetabotAnalyticsUpsellPage",
() => ({
MetabotAnalyticsUpsellPage: () => <div>Usage stats upsell</div>,
}),
);
jest.mock("./mcp-analytics/components/McpAnalyticsSectionLayout", () => {
const { Outlet } = jest.requireActual<typeof Router>("metabase/router");
return {
McpAnalyticsSectionLayout: () => (
<div>
MCP analytics page
<Outlet />
</div>
),
};
});
jest.mock("./cli-analytics/components/CliAnalyticsSectionLayout", () => {
const { Outlet } = jest.requireActual<typeof Router>("metabase/router");
return {
CliAnalyticsSectionLayout: () => (
<div>
CLI analytics page
<Outlet />
</div>
),
};
});
jest.mock("./mcp-analytics/components/McpUsagePage", () => ({
McpUsagePage: () => <div>MCP usage page</div>,
}));
jest.mock("./mcp-analytics/components/McpEventsPage", () => ({
McpEventsPage: () => <div>MCP tool calls page</div>,
}));
jest.mock("./cli-analytics/components/CliUsagePage", () => ({
CliUsagePage: () => <div>CLI usage page</div>,
}));
jest.mock("./cli-analytics/components/CliCallsPage", () => ({
CliCallsPage: () => <div>CLI calls page</div>,
}));
jest.mock("./data-apps-analytics/components/DataAppsAnalyticsPage", () => ({
DataAppsAnalyticsPage: () => <div>Data app analytics page</div>,
}));
type SetupOpts = {
route: string;
upsell?: boolean;
aiFeaturesEnabled?: boolean;
isConfigured?: boolean;
mcpEnabled?: boolean;
hasDataApps?: boolean;
};
function setup({
route,
upsell = false,
aiFeaturesEnabled = true,
isConfigured = true,
mcpEnabled = true,
hasDataApps = false,
}: SetupOpts) {
// The routes read it as they're built, so set it first.
PLUGIN_DATA_APPS.isEnabled = hasDataApps;
const settings = mockSettings({
"ai-features-enabled?": aiFeaturesEnabled,
"llm-metabot-configured?": isConfigured,
"mcp-enabled?": mcpEnabled,
});
return renderWithProviders(
<Route path="/monitor/ai-auditing">
{upsell ? getAiAuditingUpsellRoutes() : getAiAuditingRoutes()}
<Route path="*" element={<div>Not found</div>} />
</Route>,
{
initialRoute: route,
withRouter: true,
storeInitialState: createMockState({ settings }),
},
);
}
describe("AI Auditing routes", () => {
afterEach(() => {
reinitialize();
});
it.each([false, true])(
"redirects the section root to Usage stats and preserves the query when upsell is %s",
async (upsell) => {
const { router } = setup({
route: `${Urls.monitorAiAuditing()}?date=past7days~`,
upsell,
});
await waitFor(() => {
expect(router?.location).toMatchObject({
pathname: upsell
? Urls.monitorAiAuditingUsage()
: Urls.monitorAiAuditingUsageMetric("conversations"),
search: "?date=past7days~",
});
});
},
);
it.each([
[Urls.monitorAiAuditingUsage(), "Usage stats page"],
["/monitor/ai-auditing/conversations", "Conversations page"],
["/monitor/ai-auditing/conversations/42", "Conversation detail page"],
])("blocks %s when AI features are disabled", async (route, pageText) => {
setup({ route, aiFeaturesEnabled: false });
expect(
await screen.findByText("AI features are disabled"),
).toBeInTheDocument();
expect(screen.queryByText(pageText)).not.toBeInTheDocument();
});
it.each([false, true])(
"prioritizes globally disabled AI on the MCP route when upsell is %s",
async (upsell) => {
setup({
route: "/monitor/ai-auditing/mcp",
upsell,
aiFeaturesEnabled: false,
mcpEnabled: false,
});
expect(
await screen.findByText("AI features are disabled"),
).toBeInTheDocument();
expect(screen.queryByText("MCP analytics page")).not.toBeInTheDocument();
expect(
screen.getByRole("link", { name: "Go to AI Settings" }),
).toHaveAttribute("href", Urls.adminAiSettings());
},
);
it("redirects Usage stats to its Conversations tab, preserving the query", async () => {
const { router } = setup({
route: `${Urls.monitorAiAuditingUsage()}?date=past7days~`,
});
await waitFor(() => {
expect(router?.location).toMatchObject({
pathname: Urls.monitorAiAuditingUsageMetric("conversations"),
search: "?date=past7days~",
});
});
expect(await screen.findByText("Usage stats page")).toBeInTheDocument();
});
it.each(["conversations", "tokens", "messages"])(
"renders Usage stats at the %s tab route",
async (metric) => {
setup({ route: Urls.monitorAiAuditingUsageMetric(metric) });
expect(await screen.findByText("Usage stats page")).toBeInTheDocument();
},
);
it("renders the upsell at a Usage stats tab route", async () => {
setup({
route: Urls.monitorAiAuditingUsageMetric("tokens"),
upsell: true,
});
expect(await screen.findByText("Usage stats upsell")).toBeInTheDocument();
});
it("renders full Metabot analytics when enabled and configured", async () => {
setup({ route: "/monitor/ai-auditing/conversations" });
expect(await screen.findByText("Conversations page")).toBeInTheDocument();
});
it("blocks MCP analytics when MCP is disabled", async () => {
setup({ route: "/monitor/ai-auditing/mcp", mcpEnabled: false });
expect(await screen.findByText("MCP is disabled")).toBeInTheDocument();
expect(screen.queryByText("MCP analytics page")).not.toBeInTheDocument();
});
it("keeps the license upsell ahead of AI configuration", async () => {
setup({
route: Urls.monitorAiAuditingUsage(),
upsell: true,
aiFeaturesEnabled: false,
isConfigured: false,
});
expect(await screen.findByText("Usage stats upsell")).toBeInTheDocument();
expect(
screen.queryByText("AI features are disabled"),
).not.toBeInTheDocument();
});
it.each([false, true])(
"renders CLI analytics unconditionally when upsell is %s",
async (upsell) => {
setup({
route: Urls.monitorAiAuditingCli(),
upsell,
aiFeaturesEnabled: false,
isConfigured: false,
mcpEnabled: false,
});
expect(await screen.findByText("CLI analytics page")).toBeInTheDocument();
},
);
describe.each([false, true])(
"MCP and CLI sub-routes (upsell %s)",
(upsell) => {
it.each([
[Urls.monitorAiAuditingMcpUsage(), "MCP usage page"],
[Urls.monitorAiAuditingMcpEvents(), "MCP tool calls page"],
[Urls.monitorAiAuditingCliUsage(), "CLI usage page"],
[Urls.monitorAiAuditingCliCalls(), "CLI calls page"],
])("renders %s", async (route, pageText) => {
setup({ route, upsell });
expect(await screen.findByText(pageText)).toBeInTheDocument();
});
it.each([
[Urls.monitorAiAuditingMcp(), Urls.monitorAiAuditingMcpUsage()],
[Urls.monitorAiAuditingCli(), Urls.monitorAiAuditingCliUsage()],
])(
"redirects %s to its usage sub-route, preserving the query",
async (route, target) => {
const { router } = setup({
route: `${route}?date=past7days~`,
upsell,
});
await waitFor(() => {
expect(router?.location).toMatchObject({
pathname: target,
search: "?date=past7days~",
});
});
},
);
},
);
describe.each([false, true])("Data app analytics (upsell %s)", (upsell) => {
it("renders the page with the data-apps feature, whatever the AI settings", async () => {
setup({
route: Urls.monitorAiAuditingDataApps(),
upsell,
hasDataApps: true,
aiFeaturesEnabled: false,
isConfigured: false,
});
expect(
await screen.findByText("Data app analytics page"),
).toBeInTheDocument();
});
it("does not mount the section without the data-apps feature", async () => {
setup({
route: Urls.monitorAiAuditingDataApps(),
upsell,
hasDataApps: false,
});
expect(await screen.findByText("Not found")).toBeInTheDocument();
expect(
screen.queryByText("Data app analytics page"),
).not.toBeInTheDocument();
});
});
it("registers the Data app analytics prefetch before the data apps plugin sets its flag", () => {
PLUGIN_DATA_APPS.isEnabled = false;
registerAiAuditingPagePrefetch();
expect(jest.mocked(registerPagePrefetch)).toHaveBeenCalledWith(
Urls.monitorAiAuditingDataApps(),
expect.any(Function),
);
});
it("applies MCP availability to the upsell route set", async () => {
setup({
route: "/monitor/ai-auditing/mcp",
upsell: true,
mcpEnabled: false,
});
expect(await screen.findByText("MCP is disabled")).toBeInTheDocument();
expect(screen.queryByText("MCP analytics page")).not.toBeInTheDocument();
});
});