Skip to content

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

54.8 KB
Raw Blame
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import type { StructuredQuestionDetails } from "e2e/support/helpers";
import type {
  Exploration,
  GetExplorationDataResponse,
} from "metabase-types/api";

const { H } = cy;
const { ORDERS_ID, ORDERS, PRODUCTS } = SAMPLE_DATABASE;

type StructuredQuestionDetailsWithName = StructuredQuestionDetails & {
  name: string;
};

const ORDERS_COUNT_METRIC_NAME = "Count of orders";
const ORDERS_COUNT_METRIC: StructuredQuestionDetailsWithName = {
  name: ORDERS_COUNT_METRIC_NAME,
  type: "metric" as const,
  query: {
    "source-table": ORDERS_ID,
    aggregation: [["count"]],
  },
  display: "scalar" as const,
};

const ORDERS_TIMESERIES_METRIC_NAME = "Count of orders over time";
const ORDERS_TIMESERIES_METRIC: StructuredQuestionDetailsWithName = {
  name: ORDERS_TIMESERIES_METRIC_NAME,
  type: "metric" as const,
  query: {
    "source-table": ORDERS_ID,
    aggregation: [["count"]],
    breakout: [
      [
        "field",
        ORDERS.CREATED_AT,
        { "base-type": "type/DateTime", "temporal-unit": "month" },
      ],
    ],
  },
  display: "line" as const,
};

function seedMetrics() {
  for (const metric of [ORDERS_COUNT_METRIC, ORDERS_TIMESERIES_METRIC]) {
    H.createQuestion(metric).then(({ body }) => {
      // The response is unused, but `GET /api/metric/:id` calls
      // `sync-dimensions!` server-side, persisting the metric's dimensions
      // and dimension_mappings — which `/api/exploration/dimensions` needs.
      cy.request("GET", `/api/metric/${body.id}`);
    });
  }
}

/**
 * Create a timeline + one sentinel event in a single chain. The
 * Exploration timeline picker (`useGetExplorationTimelinesQuery`) hides
 * timelines without events, so test fixtures that want to surface the
 * timeline in the picker — or attach it to an exploration — need at
 * least one event hanging off it.
 *
 * Returns the new `timeline_id` so callers that need it (e.g. to assert
 * a request body's `timeline_ids` field) can chain off it; call sites
 * that just need the timeline to exist can ignore the return value.
 */
function createTimelineWithSentinelEvent(
  name: string,
  icon: string,
): Cypress.Chainable<number> {
  return cy
    .request("POST", "/api/timeline", {
      name,
      collection_id: null,
      icon,
      default: false,
    })
    .then(({ body }: { body: { id: number } }) => {
      const timelineId = body.id;
      return cy
        .request("POST", "/api/timeline-event", {
          timeline_id: timelineId,
          name: `${name} event`,
          icon,
          timestamp: "2026-01-01T00:00:00Z",
          time_matters: false,
          timezone: "UTC",
        })
        .then(() => timelineId);
    });
}

/**
 * Resolve the "Count of orders" metric plus its first two dimensions and
 * create an exploration with them — yielding one metric-group heading with
 * exactly two dimension-named pages, the smallest deterministic tree for
 * sidebar-triage assertions.
 */
function createTwoPageExploration(name: string): Cypress.Chainable<{
  explorationId: number;
  metricName: string;
  pageNames: string[];
}> {
  return cy.request("GET", "/api/exploration/dimensions").then(({ body }) => {
    // Unjustified type cast. FIXME
    const data = body as GetExplorationDataResponse;
    const ordersMetric = data.metrics.find(
      (metric) => metric.name === ORDERS_COUNT_METRIC_NAME,
    );
    expect(
      ordersMetric,
      `"${ORDERS_COUNT_METRIC_NAME}" metric is exposed by /api/exploration/dimensions`,
    ).to.exist;
    const dimsById = new Map(
      data.dimension_groups.flatMap((group) =>
        group.dimensions.map((dim) => [dim.id, dim] as const),
      ),
    );
    const pickedDimensions = ordersMetric!.dimension_ids
      .map((id) => dimsById.get(id))
      .filter((dim) => dim != null)
      .slice(0, 2);
    expect(
      pickedDimensions.length,
      "metric exposes at least two dimensions",
    ).to.eq(2);

    return H.createExplorationViaApi({
      name,
      metricCardIds: [ordersMetric!.id],
      dimensionIds: pickedDimensions.map((dim) => dim!.id),
    }).then((explorationId) => ({
      explorationId,
      metricName: ordersMetric!.name,
      pageNames: pickedDimensions.map((dim) => dim!.display_name),
    }));
  });
}

/**
 * Visit an exploration and wait until it has fully settled. The "Ready" icons
 * only say every page's queries finished; the page keeps polling
 * `GET /api/exploration/:id` (rebuilding the sidebar tree on each response)
 * until the BE also stamps the thread's `completed_at` after its post-query
 * handling. Interact with the tree only after that final response, or clicks
 * can land on nodes a re-render just replaced.
 */
function visitExplorationUntilSettled(
  explorationId: number,
  minReadyCount: number,
): void {
  cy.intercept("GET", `/api/exploration/${explorationId}`).as("getExploration");
  H.visitExploration(explorationId);
  cy.findAllByLabelText("Ready", { timeout: 30000 }).should(
    "have.length.at.least",
    minReadyCount,
  );
  const awaitThreadCompletion = (attempt: number) => {
    cy.wait("@getExploration", { timeout: 30000 }).then(({ response }) => {
      // Unjustified type cast. FIXME
      const threads = (response?.body as Exploration).threads ?? [];
      if (!threads.every((thread) => thread.completed_at != null)) {
        expect(
          attempt,
          "exploration completes within the poll budget",
        ).to.be.lessThan(30);
        awaitThreadCompletion(attempt + 1);
      }
    });
  };
  awaitThreadCompletion(0);
}

/**
 * The FE polls every 2s while the new thread's queries are in flight, then
 * toasts once its first page lands. Timing is covered by
 * ExplorationPage.unit.spec.tsx — here we just wait for the real BE.
 * Scoped to the toast container so we don't match the thread name in the
 * sidebar.
 */
function openExploreFurtherToast(threadName: string): void {
  H.undoToastListContainer().within(() => {
    cy.findByText(`Added ${threadName}`, { timeout: 60000 }).should(
      "be.visible",
    );
    cy.findByRole("button", { name: "View" }).click();
  });
}
function measureGreeting(title: string | RegExp, promptInputTestId: string) {
  return cy.findByText(title).then(($title) =>
    cy.findByTestId(promptInputTestId).then(($input) => {
      const titleRect = $title[0].getBoundingClientRect();
      const inputRect = $input[0].getBoundingClientRect();
      return {
        titleTop: Math.round(titleRect.top),
        titleLeft: Math.round(titleRect.left),
        inputTop: Math.round(inputRect.top),
      };
    }),
  );
}

describe("scenarios > explorations", () => {
  beforeEach(() => {
    cy.task("stopMockLlmServer");
    H.restore();
    cy.signInAsAdmin();
    H.enableExplorations();
    seedMetrics();
    H.resetSnowplow();
    H.enableTracking();
  });

  afterEach(() => {
    H.expectNoBadSnowplowEvents();
  });

  describe("entry page", () => {
    it("aligns the title and prompt input with the /question/ask page", () => {
      cy.visit("/question/ask");
      cy.findByTestId("metabot-conversation-history").should("be.visible");
      measureGreeting(
        /What (would you like to know|do you want to explore|are you looking to learn)\?/,
        "metabot-chat-input",
      ).then((askLayout) => {
        cy.visit("/question/research");
        measureGreeting(
          "What do you want to research?",
          "exploration-prompt-input",
        ).then((researchLayout) => {
          expect(researchLayout).to.deep.equal(askLayout);
        });
      });
    });
  });

  describe("new research", () => {
    it("picks Our analytics + metrics + timelines, filters the data pickers by search, creates an exploration, and lands on the detail page", () => {
      createTimelineWithSentinelEvent("Releases", "star").then((releasesId) => {
        createTimelineWithSentinelEvent("Marketing campaigns", "bell").then(
          (marketingId) => {
            H.visitNewExploration();

            // Root is a FE-only id (`"root"`); the create POST must send
            // `collection_id: null` or the BE schema rejects it.
            cy.findByRole("button", { name: /Personal collection/i }).click();
            H.pickEntity({ path: ["Our analytics"], select: true });
            cy.findByRole("button", { name: /Our analytics/i }).should(
              "be.visible",
            );

            H.startManualExploration();

            // Manual setup is its own location, so browser back returns here
            // instead of leaving the research flow (UXW-4832).
            cy.location("pathname").should("eq", "/question/research/plan");

            cy.log("Search inside the + Metrics modal");
            cy.intercept({
              method: "GET",
              pathname: "/api/exploration/dimensions",
              query: { q: "over time" },
            }).as("searchOverTime");
            cy.intercept({
              method: "GET",
              pathname: "/api/exploration/dimensions",
              query: { q: "zzz" },
            }).as("searchNoMatch");
            cy.findByRole("button", { name: /Metrics/ }).click();
            // Seeded names are "Count of orders" + "Count of orders over time".
            // Seeded metrics aren't in the library; switch off the default Library tab.
            H.selectAllMetricsTab();
            cy.findByRole("checkbox", {
              name: ORDERS_COUNT_METRIC_NAME,
            }).should("exist");
            cy.findByRole("checkbox", {
              name: ORDERS_TIMESERIES_METRIC_NAME,
            }).should("exist");

            // Type a substring that only matches the timeseries metric.
            cy.findByPlaceholderText("Search for a metric").type("over time");
            cy.wait("@searchOverTime");
            cy.findByRole("checkbox", {
              name: ORDERS_TIMESERIES_METRIC_NAME,
            }).should("exist");
            cy.findByRole("checkbox", {
              name: ORDERS_COUNT_METRIC_NAME,
            }).should("not.exist");

            // Clear the input → both rows return.
            cy.findByPlaceholderText("Search for a metric").clear();
            cy.findByRole("checkbox", {
              name: ORDERS_COUNT_METRIC_NAME,
            }).should("exist");
            cy.findByRole("checkbox", {
              name: ORDERS_TIMESERIES_METRIC_NAME,
            }).should("exist");

            // Search for something that matches no metric → empty-state copy.
            cy.findByPlaceholderText("Search for a metric").type("zzz");
            cy.wait("@searchNoMatch");
            cy.findByRole("dialog").should("contain", "No results");

            cy.findByPlaceholderText("Search for a metric").clear();
            cy.findByRole("checkbox", {
              name: ORDERS_COUNT_METRIC_NAME,
            }).should("exist");
            cy.get("body").type("{esc}");

            cy.log("Search inside the + Events modal");
            cy.findByRole("button", { name: /Events/ }).click();
            cy.findByRole("checkbox", { name: "Releases" }).should("exist");
            cy.findByRole("checkbox", { name: "Marketing campaigns" }).should(
              "exist",
            );

            // Filter to just one timeline by name fragment.
            cy.findByPlaceholderText("Search for a timeline").type("release");
            cy.findByRole("checkbox", { name: "Releases" }).should("exist");
            cy.findByRole("checkbox", { name: "Marketing campaigns" }).should(
              "not.exist",
            );

            // Clear → both return.
            cy.findByPlaceholderText("Search for a timeline").clear();
            cy.findByRole("checkbox", { name: "Releases" }).should("exist");
            cy.findByRole("checkbox", { name: "Marketing campaigns" }).should(
              "exist",
            );
            cy.get("body").type("{esc}");

            cy.log("Add a metric");
            cy.findByRole("button", { name: /Metrics/ }).click();
            H.selectAllMetricsTab();
            cy.findByRole("checkbox", { name: ORDERS_COUNT_METRIC_NAME }).check(
              {
                force: true,
              },
            );
            cy.findByRole("button", { name: "Add" }).click();

            cy.findByTestId("research-content")
              .should("be.visible")
              .and("contain", ORDERS_COUNT_METRIC_NAME);

            // Pick two timelines via the "+ Events" modal.
            H.addTimelinesToExploration(["Marketing campaigns", "Releases"]);

            // The first picked timeline shows as a pill next to the Events
            // button; the rest collapse into a "+N" overflow pill.
            cy.findByTestId("selected-timelines-container").within(() => {
              cy.findByText("Marketing campaigns").should("be.visible");
              cy.findByText("+1").should("be.visible");
            });

            H.beginResearch().then((id) => {
              // `beginResearch` consumed the create request; re-read it off its
              // alias to assert timeline ids + root collection were forwarded.
              cy.get("@createExploration")
                .its("request.body")
                .should((body) => {
                  expect(body.timeline_ids).to.deep.eq([
                    marketingId,
                    releasesId,
                  ]);
                  expect(body.collection_id).to.eq(null);
                });

              H.expectUnstructuredSnowplowEvent({
                event: "exploration_plan_edited",
                triggered_from: "manual",
                event_detail: "metrics",
              });
              H.expectUnstructuredSnowplowEvent({
                event: "exploration_plan_edited",
                triggered_from: "manual",
                event_detail: "timelines",
              });
              H.expectUnstructuredSnowplowEvent({
                event: "exploration_created",
                target_id: id,
              });

              // `beginResearch` already asserted the detail-page URL.
              // No new-exploration CTA on the detail page.
              cy.findAllByRole("treeitem", { timeout: 15000 })
                .first()
                .should("be.visible");
              cy.findByRole("button", { name: /Start research/i }).should(
                "not.exist",
              );
            });
          },
        );
      });
    });

    it("auto-populates metrics + dimensions + name from agent tool calls, then Start research succeeds", () => {
      cy.request("GET", "/api/exploration/dimensions").then(({ body }) => {
        // Unjustified type cast. FIXME
        const data = body as {
          metrics: Array<{ id: number; name: string; dimension_ids: string[] }>;
          dimension_groups: Array<{
            name: string;
            dimension_interestingness: number | null;
            dimensions: Array<{
              id: string;
              name: string;
              display_name: string;
              dimension_interestingness: number | null;
            }>;
          }>;
        };

        const firstMetric = data.metrics[0];
        const interestingGroup =
          data.dimension_groups.find((g) =>
            g.dimensions.some((d) => (d.dimension_interestingness ?? 0) >= 0.7),
          ) ?? data.dimension_groups[0];
        expect(
          firstMetric,
          "seeded metric is exposed by /api/exploration/dimensions",
        ).to.exist;
        expect(interestingGroup, "at least one dimension group is exposed").to
          .exist;

        const agentName = "New exploration";
        H.mockExplorationsAgentToolCalls([
          {
            toolCallId: "groups-1",
            toolName: "add_research_groups",
            // The tool result is only the LLM's summary of the edit; the picker
            // hydration rides a `research_plan_update` data part.
            result: `Added 1 group(s) to the research plan:\n- ${firstMetric.name}, by the automatically-selected dimensions`,
            dataParts: [
              {
                dataType: "research_plan_update",
                data: {
                  metrics: [firstMetric],
                  dimension_groups: [interestingGroup],
                  groups: [{ metric_id: firstMetric.id }],
                },
              },
            ],
          },
          {
            toolCallId: "name-1",
            toolName: "set_research_name",
            result: { name: agentName },
          },
        ]);

        H.visitNewExploration();

        H.explorationsMetabotPromptInput().type("Why are signups down?");
        cy.findByRole("button", { name: /Create plan/i }).click();

        // The chat dispatch went through with the `explorations`
        // profile, confirming we wired the new-exploration page to
        // the right agent.
        cy.wait("@metabotAgent")
          .its("request.body.profile_id")
          .should("eq", "explorations");

        // Right panel hydrated from the tool-call result — the agent's
        // metric now heads a research-plan block.
        cy.findByRole("main").findByText(firstMetric.name).should("be.visible");

        // Click Start research; the create-exploration POST body
        // should carry the name the agent picked.
        cy.intercept("POST", "/api/exploration").as("createExploration");
        cy.findByRole("button", { name: /Start research/i }).click();
        cy.wait("@createExploration").then(({ request, response }) => {
          expect(request.body.name).to.eq(agentName);
          // Unjustified type cast. FIXME
          const id = response?.body?.id as number;
          H.expectUnstructuredSnowplowEvent({
            event: "exploration_agent_message_sent",
          });
          H.expectUnstructuredSnowplowEvent({
            event: "exploration_plan_edited",
            triggered_from: "agent",
            event_detail: "metrics",
          });
          H.expectUnstructuredSnowplowEvent({
            event: "exploration_created",
            target_id: id,
          });
        });
      });
    });
  });

  describe("detail page", () => {
    it("preserves the URL `timeline` param across navigation and reload", () => {
      createTimelineWithSentinelEvent("Releases", "star").then((timelineId) => {
        cy.request("GET", "/api/exploration/dimensions").then(({ body }) => {
          // Unjustified type cast. FIXME
          const data = body as GetExplorationDataResponse;
          const temporalDimension = data.dimension_groups
            .flatMap((group) => group.dimensions)
            .find((dim) => dim.effective_type.includes("Date"));
          expect(temporalDimension, "sample DB exposes a temporal dimension").to
            .exist;

          H.createExplorationViaApi({
            name: "Timeline persistence fixture",
            timelineIds: [timelineId],
            dimensionIds: [temporalDimension!.id],
          }).then((id) => {
            cy.visit(`/question/research/${id}?timeline=${timelineId}`);
            // Sidebar treeitems appear once the BE returns query rows.
            cy.findAllByRole("treeitem", { timeout: 15000 })
              .first()
              .should("be.visible");

            cy.findByRole("treeitem", {
              name: new RegExp(`${temporalDimension!.display_name}$`), // anchor at end so we don't match day of week or hour of day
            }).click();

            cy.findByTestId("exploration-chart-grid").within(() => {
              H.timelineEventChip("Releases event").should("be.visible");
            });

            cy.location("search").should("include", `timeline=${timelineId}`);

            // Reload — the URL param is the source of truth and the
            // router shouldn't rewrite it away on hydration.
            cy.reload();
            cy.findAllByRole("treeitem", { timeout: 15000 })
              .first()
              .should("be.visible");
            cy.location("search").should("include", `timeline=${timelineId}`);

            cy.findByTestId("exploration-chart-grid").within(() => {
              H.timelineEventChip("Releases event").should("be.visible");
            });
          });
        });
      });
    });
  });

  describe("sidebar triage", () => {
    const sidebar = () => cy.findByTestId("exploration-page-sidebar");
    const filterToggle = () =>
      cy.findByTestId("exploration-show-hidden-toggle");
    const selectedRows = () =>
      cy.findAllByRole("treeitem").filter('[aria-selected="true"]');
    const toggleShowHiddenItems = () => {
      filterToggle().click();
      H.menu().findByText("Show hidden items").click();
      cy.get("body").type("{esc}");
    };

    it("selects a page on click, filters pages with the Stars and Discussions tabs, persists the sort preference across reloads, and hides and reveals pages from the toolbar and group menus", () => {
      createTwoPageExploration("Sidebar triage fixture").then(
        ({ explorationId, metricName, pageNames }) => {
          visitExplorationUntilSettled(explorationId, 2);

          cy.log("Clicking an unselected page selects it and shows its chart");
          // The page auto-selects a row on load, so clicking the first (already
          // selected) row would pass even with click-to-select broken. Click a
          // row that is NOT selected and assert the selection moves to it.
          cy.findAllByRole("treeitem").should("have.length", 3); // summary document + 2 pages
          cy.findAllByRole("treeitem")
            .filter('[aria-selected="false"]')
            .eq(1) // first treeitem is the summary document
            .invoke("attr", "href")
            .then((href) => {
              cy.get(`[role="treeitem"][href="${href}"]`).click();
              cy.get(`[role="treeitem"][href="${href}"]`)
                .should("have.attr", "aria-selected", "true")
                .invoke("text")
                .then((text) => text.trim())
                .as("clickedPageName");
            });

          // The clicked page's chart renders in the main area — assert a real
          // visualization anchor, not just "main is not empty" (a spinner
          // satisfies that).
          cy.location("pathname").should("match", /\/page\/\d+$/);
          cy.findByRole("main")
            .findByTestId("visualization-root", { timeout: 15000 })
            .should("be.visible");

          cy.log("Stars tab is empty until something is starred");
          cy.findByRole("radio", { name: "Stars" }).click({ force: true });
          cy.location("search").should("include", "tab=stars");
          H.main()
            .findByText("Nothing's been starred yet.")
            .should("be.visible");
          cy.findAllByRole("treeitem").should("not.exist");

          cy.log("Discussions tab shows its own empty message");
          cy.findByRole("radio", { name: "Discussions" }).click({
            force: true,
          });
          H.main().findByText("No discussions yet.").should("be.visible");
          cy.findAllByRole("treeitem").should("not.exist");

          cy.log("Star the selected page with the s shortcut");
          cy.findByRole("radio", { name: "All" }).click({ force: true });
          cy.findAllByRole("treeitem").should("have.length", 3); // summary document + 2 pages
          cy.get<string>("@clickedPageName").then((name) => {
            sidebar().findByText(name).click();
            selectedRows().should("contain.text", name);
          });
          cy.intercept("PUT", "/api/exploration/page/*/starred").as(
            "setStarred",
          );
          cy.get("body").type("s");
          cy.wait("@setStarred").its("request.body.starred").should("eq", true);
          H.main()
            .findByRole("button", { name: "Remove star" })
            .should("be.visible");

          cy.log("The c shortcut opens the comment editor");
          cy.get("body").type("c");
          // The editor's placeholder is CSS-rendered by TipTap (not real text),
          // so anchor on the editor's Send button instead. The popover is
          // portaled outside <main>.
          H.popover()
            .findByRole("button", { name: "Send" })
            .should("be.visible");
          H.main().findByRole("button", { name: "Add comment" }).click();
          cy.findByRole("button", { name: "Send" }).should("not.exist");

          cy.log("The l shortcut copies a link to the selected page");
          // Headless Chrome denies real clipboard access here (NotAllowedError),
          // so stub the write and assert the call instead of reading it back.
          cy.window().then((win) => {
            cy.stub(win.navigator.clipboard, "writeText")
              .as("copyToClipboard")
              .resolves();
          });
          cy.get("body").type("l");
          H.undoToastListContainer()
            .findByText("Copied link")
            .should("be.visible");
          cy.location("href").then((href) => {
            cy.get("@copyToClipboard").should("have.been.calledWith", href);
          });

          cy.log("Stars tab now shows only the starred page");
          cy.findByRole("radio", { name: "Stars" }).click({ force: true });
          cy.get<string>("@clickedPageName").then((name) => {
            cy.findAllByRole("treeitem")
              .should("have.length", 1)
              .first()
              .should("contain.text", name);
          });

          cy.log("Alphabetical sort is remembered per exploration");
          cy.findByRole("radio", { name: "All" }).click({ force: true });
          // Assert and click through separate queries — chaining `.click()` off
          // the assertion can act on a node a poll-driven re-render detached.
          filterToggle().should("have.attr", "aria-pressed", "false");
          filterToggle().click();
          H.menu().findByText("Alphabetical").click();
          H.menu()
            .findByRole("menuitem", { name: /Alphabetical/ })
            .should("have.attr", "data-checked", "true");
          cy.get("body").type("{esc}");
          cy.log("Sorting is not a filter, so the toggle stays unfilled");
          filterToggle().should("have.attr", "aria-pressed", "false");

          cy.reload();
          H.closeNavigationSidebar();
          cy.findAllByRole("treeitem", { timeout: 15000 })
            .first()
            .should("be.visible");
          filterToggle().should("have.attr", "aria-pressed", "false");
          filterToggle().click();
          H.menu()
            .findByRole("menuitem", { name: /Alphabetical/ })
            .should("have.attr", "data-checked", "true");
          cy.get("body").type("{esc}");

          cy.intercept("PUT", "/api/exploration/pages/hidden").as(
            "setPagesHidden",
          );
          // Which of the two dimension-named pages is selected is
          // data-dependent; derive it from the DOM instead of assuming.
          cy.findAllByRole("treeitem").should("have.length", 3); // summary document + 2 pages
          selectedRows().should("have.length", 1);
          selectedRows()
            .first()
            .invoke("text")
            .then((text) => {
              const firstPageName = text.trim();
              const secondPageName = pageNames.find(
                (name) => name !== firstPageName,
              )!;

              cy.log("Triage arrows step between pages");
              cy.findByRole("button", { name: "Next" }).click();
              selectedRows().should("contain.text", secondPageName);
              cy.findByRole("button", { name: "Previous" }).click();
              selectedRows().should("contain.text", firstPageName);

              cy.log("Hide the selected page with the h shortcut");
              cy.get("body").type("h");
              cy.wait("@setPagesHidden").then(({ request }) => {
                expect(request.body.hidden).to.eq(true);
                expect(request.body.page_ids).to.have.length(1);
              });
              H.undoToastListContainer()
                .findByText(`"${firstPageName}" hidden`)
                .should("be.visible");

              cy.log("Auto-advance moves the selection to the remaining page");
              selectedRows().should("contain.text", secondPageName);
              cy.findAllByRole("treeitem").should("have.length", 2); // summary document + 1 non-hidden page
              sidebar().findByText(firstPageName).should("not.exist");

              cy.log("Show hidden items reveals the page with a Hidden marker");
              toggleShowHiddenItems();
              filterToggle().should("have.attr", "aria-pressed", "true");
              sidebar().findByText(firstPageName).should("be.visible");
              sidebar().findAllByLabelText("Hidden").should("have.length", 1);
              toggleShowHiddenItems();
              cy.findAllByRole("treeitem").should("have.length", 2);
              sidebar().findByText(firstPageName).should("not.exist");
            });

          cy.log("Hide the whole group from its actions menu");
          sidebar()
            .findByRole("group", { name: metricName })
            .findByRole("button", { name: "Group actions" })
            .click({ force: true });
          H.menu().findByText("Hide").click();
          cy.wait("@setPagesHidden")
            .its("request.body.hidden")
            .should("eq", true);
          H.undoToastListContainer()
            .findByText(`${metricName} hidden`)
            .should("be.visible");

          cy.log("Everything but the summary document is hidden");
          cy.findAllByRole("treeitem").should("have.length", 1);

          cy.log("Hidden state is persisted server-side across a reload");
          cy.reload();
          H.closeNavigationSidebar();
          cy.findAllByRole("treeitem").should("have.length", 1);

          cy.log("Show hidden items + the group Show action restore the pages");
          toggleShowHiddenItems();
          cy.findAllByRole("treeitem").should("have.length", 3);
          sidebar().findAllByLabelText("Hidden").should("have.length", 2);
          sidebar()
            .findByRole("group", { name: metricName })
            .findByRole("button", { name: "Group actions" })
            .click({ force: true });
          H.menu().findByText("Show").click();
          cy.wait("@setPagesHidden")
            .its("request.body.hidden")
            .should("eq", false);
          cy.findAllByRole("treeitem").should("have.length", 3);
          sidebar().findAllByLabelText("Hidden").should("not.exist");

          cy.log(
            "Hiding the group with every page visible sends both page ids",
          );
          toggleShowHiddenItems();
          cy.findAllByRole("treeitem").should("have.length", 3);
          sidebar()
            .findByRole("group", { name: metricName })
            .findByRole("button", { name: "Group actions" })
            .click({ force: true });
          H.menu().findByText("Hide").click();
          cy.wait("@setPagesHidden").then(({ request }) => {
            expect(request.body.hidden).to.eq(true);
            expect(request.body.page_ids).to.have.length(2);
          });
          cy.findAllByRole("treeitem").should("have.length", 1);

          cy.log("Undo on the group-hidden toast restores the whole group");
          H.undoToastListContainer()
            .findByText(`${metricName} hidden`)
            .should("be.visible");
          H.undoToastListContainer()
            .findByRole("button", { name: "Undo" })
            .click();
          cy.wait("@setPagesHidden")
            .its("request.body.hidden")
            .should("eq", false);
          cy.findAllByRole("treeitem").should("have.length", 3);
        },
      );
    });
  });

  describe("chart click-through", () => {
    it("clicking a cartesian point opens Explore further + Add comment, posts explore-further filters, and navigates from the new-thread toast", () => {
      createTimelineWithSentinelEvent("Releases", "star").then((timelineId) => {
        cy.request("GET", "/api/exploration/dimensions").then(({ body }) => {
          // manual typecast for BE data
          const seeded = body as GetExplorationDataResponse;
          const seededMetric = seeded.metrics.find(
            (metric) => metric.name === ORDERS_COUNT_METRIC_NAME,
          );
          expect(
            seededMetric,
            `"${ORDERS_COUNT_METRIC_NAME}" metric is exposed by /api/exploration/dimensions`,
          ).to.exist;
          cy.request("POST", `/api/metric/${seededMetric!.id}/dimension/add`, {
            dimensions: [
              {
                id: "e2e00000-cafe-4bb8-9d8a-0123456789ab",
                mapping_target: [
                  "field",
                  { "source-field": ORDERS.PRODUCT_ID },
                  PRODUCTS.CATEGORY,
                ],
              },
            ],
          });
        });

        cy.request("GET", "/api/exploration/dimensions").then(({ body }) => {
          // Unjustified type cast. FIXME
          const data = body as GetExplorationDataResponse;
          const ordersMetric = data.metrics.find(
            (metric) => metric.name === ORDERS_COUNT_METRIC_NAME,
          );
          expect(
            ordersMetric,
            `"${ORDERS_COUNT_METRIC_NAME}" metric is exposed by /api/exploration/dimensions`,
          ).to.exist;
          const dimsById = new Map(
            data.dimension_groups.flatMap((group) =>
              group.dimensions.map((dim) => [dim.id, dim] as const),
            ),
          );
          const dimension = ordersMetric!.dimension_ids
            .map((id) => dimsById.get(id))
            .find((dim) => dim != null && dim.display_name === "Category");
          expect(
            dimension,
            'orders metric exposes the curated "Category" dimension',
          ).to.exist;

          H.createExplorationViaApi({
            name: "Chart click-through fixture",
            metricCardIds: [ordersMetric!.id],
            dimensionIds: [dimension!.id],
            timelineIds: [timelineId],
          }).then((explorationId) => {
            let initialThreadIds: number[] = [];

            cy.intercept(
              "POST",
              `/api/exploration/${explorationId}/explore-further`,
            ).as("exploreFurther");

            cy.visit(
              `/question/research/${explorationId}?timeline=${timelineId}`,
            );
            cy.findAllByRole("treeitem", { timeout: 30000 })
              .first()
              .should("be.visible");

            cy.request("GET", `/api/exploration/${explorationId}`).then(
              ({ body }) => {
                // Unjustified type cast. FIXME
                const exploration = body as Exploration;
                initialThreadIds = (exploration.threads ?? []).map(
                  (thread) => thread.id,
                );
                // Navigate straight to the dimension's page. Driving the
                // sidebar here would depend on which group happens to be
                // auto-expanded — a state this test doesn't control.
                const pages = (exploration.threads ?? []).flatMap((thread) =>
                  (thread.blocks ?? []).flatMap((block) => block.pages ?? []),
                );
                const page = pages.find((p) =>
                  (p.name ?? "").includes(dimension!.display_name),
                );
                expect(page, "the dimension's page exists on the exploration")
                  .to.exist;
                cy.wrap(page!.id).as("pageId");
                cy.visit(
                  `/question/research/${explorationId}/page/${page!.id}?timeline=${timelineId}`,
                );
              },
            );

            cy.location("pathname").should("include", "/page/");

            // Wait for the selected page's queries to settle before clicking
            // the chart — `Ready` is the settled-state icon label.
            cy.findAllByLabelText("Ready", { timeout: 30000 })
              .first()
              .should("be.visible");

            H.chartPathWithFillColor("#509EE3").first().click({ force: true });

            cy.findByTestId("click-actions-view").within(() => {
              cy.findByRole("button", { name: /Explore further/i }).should(
                "be.visible",
              );
              cy.findByRole("button", { name: /Add comment/i }).should(
                "be.visible",
              );
            });

            cy.findByTestId("click-actions-view")
              .findByRole("button", { name: /Explore further/i })
              .click();

            cy.wait("@exploreFurther").then(({ request, response }) => {
              cy.get("@pageId").then((pageId) => {
                expect(request.body.page_id).to.eq(pageId);
              });
              expect(request.body.explore_filters).to.be.an("array").and.not.be
                .empty;
              expect(request.body.explore_filters[0]).to.include.keys(
                "operator",
                "field_ref",
                "value",
                "display_value",
              );
              expect(request.body.explore_filters[0].operator).to.eq("=");

              // Unjustified type cast. FIXME
              const threads = (response?.body as Exploration).threads ?? [];
              const newThread = threads.find(
                (thread) => !initialThreadIds.includes(thread.id),
              );
              expect(newThread, "explore-further adds a new thread").to.exist;
              cy.wrap(newThread!.name).as("newThreadName");
            });

            cy.get<string>("@newThreadName").then((name) => {
              openExploreFurtherToast(name);
            });

            cy.get("@pageId").then((pageId) => {
              cy.location("pathname")
                .should("include", `/question/research/${explorationId}/page/`)
                .and(
                  "not.eq",
                  `/question/research/${explorationId}/page/${pageId}`,
                );
            });
            cy.findByRole("main")
              .findAllByTestId("visualization-root", { timeout: 15000 })
              .first()
              .should("be.visible");
            cy.location("search").should("include", "tab=all");
            cy.location("search").should("include", `timeline=${timelineId}`);

            cy.get("@newThreadName").then((name) => {
              cy.findByRole("group", { name: String(name) }).should(
                "have.attr",
                "aria-expanded",
                "true",
              );
            });
          });
        });
      });
    });

    it("brushing a timeseries cartesian chart opens Explore further only, posts between explore_filters, navigates from the new-thread toast, and Add to Summary flips the placeholder Summary, preserves filter pills, and mirrors page comments", () => {
      cy.request<GetExplorationDataResponse>(
        "GET",
        "/api/exploration/dimensions",
      ).then(({ body: data }) => {
        const ordersMetric = data.metrics.find(
          (metric) => metric.name === ORDERS_COUNT_METRIC_NAME,
        );
        expect(
          ordersMetric,
          `"${ORDERS_COUNT_METRIC_NAME}" metric is exposed by /api/exploration/dimensions`,
        ).to.exist;
        const dimsById = new Map(
          data.dimension_groups.flatMap((group) =>
            group.dimensions.map((dim) => [dim.id, dim] as const),
          ),
        );
        const temporalDimension = ordersMetric!.dimension_ids
          .map((id) => dimsById.get(id))
          .find((dim) => dim != null && dim.effective_type.includes("Date"));
        expect(
          temporalDimension,
          "orders metric exposes at least one temporal dimension",
        ).to.exist;

        H.createExplorationViaApi({
          name: "Chart brush explore-further fixture",
          metricCardIds: [ordersMetric!.id],
          dimensionIds: [temporalDimension!.id],
        }).then((explorationId) => {
          let initialThreadIds: number[] = [];

          cy.intercept(
            "POST",
            `/api/exploration/${explorationId}/explore-further`,
          ).as("exploreFurther");

          visitExplorationUntilSettled(explorationId, 1);

          // Summary is pinned at the top of the tree but not initially selected
          // while it is still a placeholder.
          cy.findAllByRole("treeitem")
            .first()
            .should("contain.text", "Summary")
            .and("have.attr", "aria-selected", "false");
          cy.findAllByRole("treeitem")
            .filter('[aria-selected="true"]')
            .should("have.length", 1)
            .and("not.contain.text", "Summary")
            .invoke("attr", "href")
            .should("match", /\/page\/\d+/);

          cy.request<Exploration>(
            "GET",
            `/api/exploration/${explorationId}`,
          ).then(({ body: exploration }) => {
            expect(exploration.document, "BE auto-creates a Summary document")
              .to.exist;
            expect(exploration.document?.name).to.eq("Summary");
            expect(exploration.document?.is_placeholder).to.eq(true);
            initialThreadIds = (exploration.threads ?? []).map(
              (thread) => thread.id,
            );
          });

          cy.findByTestId("exploration-page-sidebar").within(() => {
            cy.findByRole("group", { name: ordersMetric!.name }).then(
              ($group) => {
                if ($group.attr("aria-expanded") !== "true") {
                  cy.wrap($group).click();
                }
              },
            );
            cy.findByRole("treeitem", {
              // Anchor on end so we don't match e.g. "Hour of day".
              name: new RegExp(`${temporalDimension!.display_name}$`),
            }).click();
          });

          H.ensureEchartsContainerHasSvg();

          cy.intercept(
            "POST",
            `/api/exploration/${explorationId}/summary/append`,
          ).as("appendSummary");

          cy.findByRole("button", { name: "Add to Summary" }).click();
          cy.wait("@appendSummary").then(({ response }) => {
            expect(response?.statusCode).to.eq(200);
            expect(response?.body?.is_placeholder).to.eq(false);
          });
          H.undoToastListContainer().within(() => {
            cy.findByText(/Added to/).should("be.visible");
            cy.findByLabelText("close icon").click();
            cy.findByText(/Added to/).should("not.exist");
          });

          H.ensureEchartsContainerHasSvg();
          H.applyBrush(120, 280);

          cy.findByTestId("click-actions-view").within(() => {
            cy.findByRole("button", { name: /Explore further/i }).should(
              "be.visible",
            );
            cy.findByRole("button", { name: /Add comment/i }).should(
              "not.exist",
            );
          });

          cy.findByTestId("click-actions-view")
            .findByRole("button", { name: /Explore further/i })
            .click();

          cy.wait("@exploreFurther").then(({ request, response }) => {
            expect(request.body.explore_filters)
              .to.be.an("array")
              .and.have.length(1);
            expect(request.body.explore_filters[0]).to.include.keys(
              "operator",
              "field_ref",
              "values",
              "display_value",
            );
            expect(request.body.explore_filters[0].operator).to.eq("between");
            expect(request.body.explore_filters[0].values)
              .to.be.an("array")
              .and.have.length(2);

            // Cypress does not type intercept response bodies.
            const threads = (response?.body as Exploration).threads ?? [];
            const newThread = threads.find(
              (thread) => !initialThreadIds.includes(thread.id),
            );
            expect(newThread, "explore-further adds a new thread").to.exist;
            cy.wrap(newThread!.name).as("newThreadName");
          });

          cy.location("pathname")
            .should("include", `/question/research/${explorationId}/page/`)
            .then((pathname) => {
              cy.wrap(pathname).as("pathnameBeforeView");
            });

          cy.get<string>("@newThreadName").then((name) => {
            openExploreFurtherToast(name);
          });

          cy.get<string>("@pathnameBeforeView").then((pathnameBeforeView) => {
            cy.location("pathname")
              .should("include", `/question/research/${explorationId}/page/`)
              .and("not.eq", pathnameBeforeView)
              .then((pathname) => {
                const pageId = Number(/\/page\/(\d+)/.exec(pathname)![1]);
                cy.wrap(pageId).as("pageId");
              });
          });
          cy.location("search").should("include", "tab=all");
          cy.get("@newThreadName").then((name) => {
            cy.findByRole("group", { name: String(name) }).should(
              "have.attr",
              "aria-expanded",
              "true",
            );
          });

          // Temporal ranges use an en-dash (e.g. "Feb 2020 – Mar 2020"); exact
          // bounds depend on brush pixel coords, so just assert range formatting.
          cy.findByTestId("filter-pill")
            .should("be.visible")
            .invoke("text")
            .should("match", /–/)
            .as("exploreFilterPillText", { type: "static" });

          cy.findByRole("button", { name: "Add to Summary" }).click();
          cy.wait("@appendSummary")
            .its("response.statusCode")
            .should("eq", 200);

          H.undoToastListContainer().within(() => {
            cy.findByText(/Added to/).should("be.visible");
            cy.findByRole("button", { name: "View" }).click();
          });

          cy.location("pathname").should(
            "eq",
            `/question/research/${explorationId}/summary`,
          );
          cy.findAllByTestId("document-card-embed", { timeout: 15000 }).should(
            "have.length",
            2,
          );
          cy.findAllByTestId("document-card-embed")
            .eq(0)
            .within(() => {
              cy.findByTestId("visualization-root", { timeout: 15000 }).should(
                "be.visible",
              );
              cy.findByTestId("filter-pill").should("not.exist");
            });
          cy.findAllByTestId("document-card-embed")
            .eq(1)
            .should("be.visible")
            .findByTestId("filter-pill")
            .should("be.visible")
            .invoke("text")
            .then((text) => {
              cy.get("@exploreFilterPillText").should("eq", text);
            });

          // Seed a page comment via API
          cy.get<number>("@pageId").then((pageId) => {
            H.createComment({
              target_type: "exploration",
              target_id: explorationId,
              child_target_id: String(pageId),
              parent_comment_id: null,
              content: {
                type: "doc",
                content: [
                  {
                    type: "paragraph",
                    content: [{ type: "text", text: "Shared comment" }],
                  },
                ],
              },
            });
          });

          // Cold-load the Summary with the comments query param — the panel
          // must open without a prior click (the deep-link regression).
          cy.get<number>("@pageId").then((pageId) => {
            cy.visit(
              `/question/research/${explorationId}/summary?comments=${pageId}`,
            );
            cy.findByTestId("exploration-summary-comments", {
              timeout: 15000,
            })
              .should("be.visible")
              .and("contain.text", "Shared comment");
          });

          cy.get<number>("@pageId").then((pageId) => {
            cy.visit(
              `/question/research/${explorationId}/page/${pageId}?comments=${pageId}`,
            );
          });

          cy.findByTestId("exploration-comments", { timeout: 15000 }).should(
            "contain.text",
            "Shared comment",
          );
        });
      });
    });
  });

  describe("collection placement + archive", () => {
    it("places a newly-created exploration in the creator's personal collection, moves it to trash, and restores and permanently deletes archived explorations from the trash banner and the /trash page", () => {
      const explorationName = "Personal-collection archive fixture";
      const restoreName = "Trash-page restore fixture";
      const deleteName = "Trash-page delete-permanently fixture";

      H.createExplorationViaApi({ name: explorationName }).as("explorationId");
      cy.request("GET", "/api/user/current")
        .its("body.personal_collection_id")
        .should("be.a", "number")
        .as("personalCollectionId");

      cy.get<number>("@explorationId").then((explorationId) => {
        cy.get<number>("@personalCollectionId").then((personalCollectionId) => {
          cy.request("GET", `/api/exploration/${explorationId}`).then(
            ({ body }) => {
              expect(
                body.collection_id,
                "exploration lives in the creator's personal collection",
              ).to.eq(personalCollectionId);
            },
          );

          // Visit the personal collection and confirm the row
          // renders.
          H.visitCollection(personalCollectionId);
        });

        // Archiving an exploration goes through the centralized
        // `useSetArchive` hook → `PUT /api/exploration/:id
        // { archived: true }`.
        cy.intercept("PUT", `/api/exploration/${explorationId}`).as(
          "archiveExploration",
        );
      });
      cy.findByTestId("collection-table")
        .findByText(explorationName)
        .should("be.visible");

      // Open the row's overflow menu and click `Move to trash`.
      H.openCollectionItemMenu(explorationName);
      // The EntityItem dropdown renders each action as a
      // `<li aria-labelledby="...">`, not `role="menuitem"`,
      // so match the visible text — scoped to the open popover.
      H.popover().findByText("Move to trash").click();

      cy.wait("@archiveExploration").then(({ request, response }) => {
        expect(request.body).to.deep.eq({ archived: true });
        expect(response?.statusCode).to.eq(200);
      });

      // After archive, the collection shows its empty state and
      // the exploration row is gone from the page.
      cy.findByTestId("collection-empty-state")
        .findByText("This collection is empty")
        .should("be.visible");
      H.main().findByText(explorationName).should("not.exist");

      // BE state agrees: a subsequent GET reports
      // `archived: true`.
      cy.get<number>("@explorationId").then((explorationId) => {
        cy.request("GET", `/api/exploration/${explorationId}`).then(
          ({ body }) => {
            expect(body.archived, "exploration is archived").to.eq(true);
          },
        );
      });

      H.undoToast().findByText("Trashed research").should("exist");
      H.undo();

      cy.findByTestId("collection-table")
        .findByText(explorationName)
        .should("be.visible");

      H.createExplorationViaApi({ name: restoreName }).as("restoreId");
      H.createExplorationViaApi({ name: deleteName }).as("deleteId");
      for (const alias of ["@explorationId", "@restoreId", "@deleteId"]) {
        cy.get<number>(alias).then((id) => {
          cy.request("PUT", `/api/exploration/${id}`, { archived: true });
        });
      }

      cy.log("An archived exploration opens with the trash banner");
      cy.get<number>("@explorationId").then((explorationId) => {
        cy.intercept("PUT", `/api/exploration/${explorationId}`).as(
          "bannerRestore",
        );
        H.visitExploration(explorationId);
      });
      cy.findByTestId("archive-banner")
        .should("contain", "This research is in the trash.")
        .findByRole("button", { name: /Restore/ })
        .click();

      cy.wait("@bannerRestore").then(({ request, response }) => {
        expect(request.body).to.deep.eq({ archived: false });
        expect(response?.statusCode).to.eq(200);
      });
      cy.findByTestId("exploration-page-sidebar").should("be.visible");
      cy.findByTestId("archive-banner").should("not.exist");

      cy.log("Delete permanently from the banner lands on the trash page");
      cy.get<number>("@explorationId").then((explorationId) => {
        cy.request("PUT", `/api/exploration/${explorationId}`, {
          archived: true,
        });
        cy.intercept("DELETE", `/api/exploration/${explorationId}`).as(
          "bannerDelete",
        );
      });
      cy.reload();
      cy.findByTestId("archive-banner")
        .findByRole("button", { name: /Delete permanently/ })
        .click();
      H.modal()
        .findByRole("button", { name: /Delete permanently/i })
        .click();

      cy.wait("@bannerDelete").its("response.statusCode").should("eq", 204);
      cy.location("pathname").should("eq", "/trash");
      H.undoToast()
        .findByText("This item has been permanently deleted.")
        .should("be.visible");
      cy.get<number>("@explorationId").then((explorationId) => {
        cy.request({
          method: "GET",
          url: `/api/exploration/${explorationId}`,
          failOnStatusCode: false,
        })
          .its("status")
          .should("eq", 404);
      });

      cy.findByTestId("collection-table")
        .findByText(restoreName)
        .should("be.visible");
      cy.findByTestId("collection-table")
        .findByText(deleteName)
        .should("be.visible");

      cy.log("Restore from the /trash page row menu");
      cy.visit("/trash");
      cy.findByTestId("collection-table")
        .findByText(restoreName)
        .should("be.visible");
      cy.findByTestId("collection-table")
        .findByText(deleteName)
        .should("be.visible");

      cy.get<number>("@restoreId").then((restoreId) => {
        cy.intercept("PUT", `/api/exploration/${restoreId}`).as("trashRestore");
      });
      H.openCollectionItemMenu(restoreName);
      H.popover().findByText("Restore").click();

      cy.wait("@trashRestore").then(({ request, response }) => {
        expect(request.body).to.deep.eq({ archived: false });
        expect(response?.statusCode).to.eq(200);
      });
      H.main().findByText(restoreName).should("not.exist");
      cy.get<number>("@restoreId").then((restoreId) => {
        cy.request("GET", `/api/exploration/${restoreId}`).then(({ body }) => {
          expect(body.archived, "exploration is no longer archived").to.eq(
            false,
          );
        });
      });

      cy.log("Delete permanently from the /trash page row menu");
      cy.findByTestId("collection-table")
        .findByText(deleteName)
        .should("be.visible");
      cy.get<number>("@deleteId").then((deleteId) => {
        cy.intercept("DELETE", `/api/exploration/${deleteId}`).as(
          "trashDelete",
        );
      });
      H.openCollectionItemMenu(deleteName);
      H.popover().findByText("Delete permanently").click();
      // The confirmation modal owns the final destructive button.
      H.modal()
        .findByRole("button", { name: /Delete permanently/i })
        .click();

      cy.wait("@trashDelete").its("response.statusCode").should("eq", 204);
      H.main().findByText(deleteName).should("not.exist");
      cy.get<number>("@deleteId").then((deleteId) => {
        cy.request({
          method: "GET",
          url: `/api/exploration/${deleteId}`,
          failOnStatusCode: false,
        })
          .its("status")
          .should("eq", 404);
      });
    });
  });
});