Skip to content

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

34.8 KB
Raw Blame
const { H } = cy;
import { SAMPLE_DB_ID, USER_GROUPS } from "e2e/support/cypress_data";
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import { ORDERS_BY_YEAR_QUESTION_ID } from "e2e/support/cypress_sample_instance_data";

const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
const { ALL_USERS_GROUP, COLLECTION_GROUP, DATA_GROUP } = USER_GROUPS;

// brand (#509EE3) in getComputedStyle's normalized color format
const HIGHLIGHTED_DOT_FILL = "rgb(80, 158, 227)";

describe("scenarios > organization > timelines > question", () => {
  beforeEach(() => {
    H.restore();
  });

  describe("as admin", () => {
    beforeEach(() => {
      cy.signInAsAdmin();
      cy.intercept("GET", "/api/collection/root").as("getCollection");
      cy.intercept("POST", "/api/timeline-event").as("createEvent");
      cy.intercept("PUT", "/api/timeline-event/**").as("updateEvent");
    });

    it("should create the first event and timeline", () => {
      H.visitQuestion(ORDERS_BY_YEAR_QUESTION_ID);
      cy.wait("@getCollection");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Visualization").should("be.visible");

      cy.icon("calendar").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Create event").click();

      cy.findByLabelText("Event name").type("RC1");
      cy.findByLabelText("Date").type("10/20/2027");
      cy.button("Create").click();
      cy.wait("@createEvent");

      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Our analytics events").should("be.visible");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("RC1").should("be.visible");
    });

    it("should create an event within the default timeline", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [{ name: "RC1", timestamp: "2027-10-20T00:00:00Z" }],
      });

      H.visitQuestion(ORDERS_BY_YEAR_QUESTION_ID);
      cy.wait("@getCollection");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Visualization").should("be.visible");

      cy.icon("calendar").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Create event").click();

      cy.findByLabelText("Event name").type("RC2");
      cy.findByLabelText("Date").type("10/30/2027");
      cy.button("Create").click();
      cy.wait("@createEvent");

      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Releases").should("be.visible");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("RC1").should("be.visible");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("RC2").should("be.visible");
    });

    it("should display all events in data view", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [
          { name: "v1", timestamp: "2030-01-01T00:00:00Z" },
          { name: "v2", timestamp: "2026-01-01T00:00:00Z" },
          { name: "v3", timestamp: "2029-01-01T00:00:00Z" },
        ],
      });

      H.visitQuestion(ORDERS_BY_YEAR_QUESTION_ID);
      cy.wait("@getCollection");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Visualization").should("be.visible");

      cy.findByLabelText("calendar icon").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("v1").should("not.exist");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("v2").should("be.visible");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("v3").should("be.visible");

      cy.findByLabelText("Switch to data").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("v1").should("be.visible");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("v2").should("be.visible");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("v3").should("be.visible");
    });

    it("should edit an event", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [{ name: "RC1", timestamp: "2027-10-20T00:00:00Z" }],
      });

      H.visitQuestion(ORDERS_BY_YEAR_QUESTION_ID);
      cy.wait("@getCollection");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Visualization").should("be.visible");

      cy.icon("calendar").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Releases").should("be.visible");
      H.rightSidebar().icon("ellipsis").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Edit event").click();

      cy.findByLabelText("Event name").clear().type("RC2");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Update").click();
      cy.wait("@updateEvent");

      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Releases").should("be.visible");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("RC2").should("be.visible");
    });

    it("should move an event", () => {
      H.createTimeline({ name: "Releases" });
      H.createTimelineWithEvents({
        timeline: { name: "Builds" },
        events: [{ name: "RC2", timestamp: "2027-10-20T00:00:00Z" }],
      });

      H.visitQuestion(ORDERS_BY_YEAR_QUESTION_ID);
      cy.wait("@getCollection");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Visualization").should("be.visible");

      cy.icon("calendar").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Builds").should("be.visible");
      H.rightSidebar().icon("ellipsis").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Move event").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Releases").click();
      cy.button("Move").click();
      cy.wait("@updateEvent");

      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Builds").should("not.exist");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Releases").should("be.visible");
    });

    it("should archive and unarchive an event", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [{ name: "RC1", timestamp: "2027-10-20T00:00:00Z" }],
      });

      H.visitQuestion(ORDERS_BY_YEAR_QUESTION_ID);
      cy.wait("@getCollection");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Visualization").should("be.visible");

      cy.icon("calendar").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Releases").should("be.visible");
      H.rightSidebar().icon("ellipsis").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Archive event").click();
      cy.wait("@updateEvent");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("RC1").should("not.exist");

      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Undo").click();
      cy.wait("@updateEvent");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("RC1").should("be.visible");
    });

    it("should support markdown in event description", () => {
      H.createTimelineWithEvents({
        timeline: {
          name: "Releases",
        },
        events: [
          {
            name: "RC1",
            description: "[Release notes](https://metabase.test)",
            timestamp: "2027-10-20T00:00:00Z",
          },
        ],
      });

      H.visitQuestion(ORDERS_BY_YEAR_QUESTION_ID);
      cy.icon("calendar").click();

      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Releases").should("be.visible");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Release notes").should("be.visible");
    });

    it("should show events for ad-hoc questions", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [{ name: "RC1", timestamp: "2027-10-20T00:00:00Z" }],
      });

      H.visitQuestionAdhoc({
        dataset_query: {
          type: "query",
          query: {
            "source-table": ORDERS_ID,
            aggregation: [["count"]],
            breakout: [["field", ORDERS.CREATED_AT, null]],
          },
          database: SAMPLE_DB_ID,
        },
        display: "bar",
        visualization_settings: {
          "graph.dimensions": ["TOTAL"],
          "graph.metrics": ["count"],
        },
      });

      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Visualization").should("be.visible");
      H.timelineEventChip("RC1").should("be.visible");
    });

    it("should not show events for non-timeseries questions", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [{ name: "RC1", timestamp: "2027-10-20T00:00:00Z" }],
      });

      H.visitQuestionAdhoc({
        dataset_query: {
          type: "query",
          query: {
            "source-table": ORDERS_ID,
            aggregation: [["count"]],
            breakout: [
              ["field", ORDERS.TOTAL, { binning: { strategy: "default" } }],
            ],
          },
          database: SAMPLE_DB_ID,
        },
        display: "bar",
        visualization_settings: {
          "graph.dimensions": ["TOTAL"],
          "graph.metrics": ["count"],
        },
      });

      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Visualization").should("be.visible");
      H.timelineEventChip("RC1").should("not.exist");
    });

    it("should show events for native queries", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [{ name: "RC1", timestamp: "2027-10-20T00:00:00Z" }],
      });

      cy.intercept("GET", "/api/timeline?include=events").as("getTimelines");

      // Aggregate by month so the result is a deterministic ~48-row series that
      // spans the full ORDERS date range. A raw `SELECT TOTAL, CREATED_AT FROM
      // ORDERS` is truncated to the first 2000 (unordered) rows, so the chart's
      // x-domain was non-deterministic and often fell short of the event date
      // (2027-10-20); the event then landed outside the domain, was filtered out
      // of the visible timeline events, and the star marker never rendered.
      H.visitQuestionAdhoc({
        dataset_query: {
          type: "native",
          native: {
            query:
              "SELECT DATE_TRUNC('month', CREATED_AT) AS CREATED_AT, COUNT(*) AS TOTAL FROM ORDERS GROUP BY DATE_TRUNC('month', CREATED_AT) ORDER BY 1",
          },
          database: SAMPLE_DB_ID,
        },
        display: "line",
        visualization_settings: {
          "graph.dimensions": ["CREATED_AT"],
          "graph.metrics": ["TOTAL"],
        },
      });

      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Visualization").should("be.visible");
      cy.wait("@getTimelines");

      H.timelineEventChip("RC1").should("be.visible");
    });

    it("should toggle individual event visibility", () => {
      cy.request("POST", "/api/collection", {
        name: "Parent",
        parent_id: null,
      }).then(({ body: { id: PARENT_COLLECTION_ID } }) => {
        H.createTimelineWithEvents({
          timeline: { name: "Releases" },
          events: [
            { name: "RC1", timestamp: "2027-10-20T00:00:00Z", icon: "cloud" },
          ],
        }).then(({ timeline }) => createQuestionWithTimeline(timeline.id));

        H.createTimelineWithEvents({
          timeline: {
            name: "Timeline for collection",
            collection_id: PARENT_COLLECTION_ID,
          },
          events: [
            { name: "TC1", timestamp: "2025-05-20T00:00:00Z", icon: "warning" },
          ],
        });

        H.visitQuestion("@questionId");

        cy.findByTestId("view-footer")
          .findByText("Visualization")
          .should("be.visible");
        H.timelineEventChip("RC1").should("be.visible");

        // should hide individual events from chart if hidden in sidebar
        cy.icon("calendar").click();
        cy.findByTestId("sidebar-content").findByText("Releases").click();
        H.toggleTimelineEventVisibility("RC1");

        H.timelineEventChip("RC1").should("not.exist");

        // should show individual events in chart again
        H.toggleTimelineEventVisibility("RC1");

        H.timelineEventChip("RC1").should("be.visible");

        // should show a newly created event
        cy.button("Create event").click();
        cy.findByLabelText("Event name").type("RC2");
        cy.findByLabelText("Date").clear().type("10/20/2026");
        cy.button("Create").click();
        H.waitForTimelinesAfterCreatingAnEvent("RC2");

        H.undoToast().icon("close").click();
        H.timelineEventChip("RC2").should("be.visible");

        // should then hide the newly created event
        H.timelineEventVisibility("RC2").should("be.checked");
        H.toggleTimelineEventVisibility("RC2");
        H.timelineEventVisibility("RC2").should("not.be.checked");

        H.timelineEventChip("RC2").should("not.exist");

        // its timeline, visible but having one hidden event
        // should display its checkbox in an indeterminate state
        H.timelineVisibility("Releases").should(
          "have.prop",
          "indeterminate",
          true,
        );

        // Hide the timeline then show it again
        H.timelineVisibility("Releases").click();
        H.timelineVisibility("Releases").click();

        // once timeline is visible, all its events should be visible
        H.timelineEventChip("RC2").should("be.visible");
        H.timelineEventChip("RC1").should("be.visible");

        // should initialize events in a hidden timelime
        // with event checkboxes unchecked
        cy.findByTestId("sidebar-content")
          .findByText("Timeline for collection")
          .click();

        cy.findByTestId("sidebar-content")
          .findByText("TC1")
          .closest("[aria-label='Timeline event card']")
          .within(() => {
            cy.findByRole("checkbox").should("not.be.checked");
          });

        // making a hidden timeline visible
        // should make its events automatically visible
        H.timelineVisibility("Timeline for collection").click();

        H.timelineEventChip("TC1").should("be.visible");

        // events whose timeline was invisible on page load
        // should be hideable once their timelines are visible
        H.toggleTimelineEventVisibility("TC1");

        H.timelineEventChip("TC1").should("not.exist");

        /**
         * This tests the case where group by unit with the event in the end range bucket
         * not included when it's the only event selected.
         *
         * e.g. group by month from 2027-01-01 to 2028-01-01 with event at 2028-01-15,
         * it should be included, but was not previously
         */
        cy.log(
          "test single event after the starting end range unit (metabase#56290)",
        );
        cy.findByText("Create event").click();

        H.modal().within(() => {
          cy.findByLabelText("Event name").type("Event at the end of range");
          cy.findByLabelText("Date").clear().type("10/20/2029");

          cy.button("Create").click();
        });
        H.waitForTimelinesAfterCreatingAnEvent("Event at the end of range");

        H.modal().should("not.exist"); // wait for modal to close

        cy.log("remove all other events except the new one");
        H.toggleTimelineEventVisibility("RC1").should("not.be.checked");
        H.toggleTimelineEventVisibility("RC2").should("not.be.checked");

        cy.log("the new event should be visible in the chart");
        H.timelineEventChip("Event at the end of range").should("be.visible");
      });
    });

    it("should show a single-event popover on hover without a 'See all' link", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [
          { name: "RC1", timestamp: "2027-10-20T00:00:00Z", icon: "star" },
        ],
      }).then(({ timeline }) => createQuestionWithTimeline(timeline.id));

      H.visitQuestion("@questionId");

      H.timelineEventChip("RC1").should("be.visible").realHover();

      cy.log(
        "hovering a single event shows a compact popover with no 'See all'",
      );
      cy.findByTestId("timeline-event-popover").within(() => {
        cy.findByText("RC1").should("be.visible");
        cy.findByText("See all").should("not.exist");
      });

      cy.log(
        "hovering also shows a marker line and highlights the closest data point",
      );
      H.timelineEventMarkerLine().should("exist");
      // the highlighted datum is drawn as a solid brand-filled dot (resting dots are hollow)
      H.cartesianChartCircleWithFillColor(HIGHLIGHTED_DOT_FILL).should(
        "have.length",
        1,
      );

      cy.log("unhovering removes the marker line and the highlight");
      cy.findByTestId("qb-header").realHover();
      H.timelineEventMarkerLine().should("not.exist");
      H.cartesianChartCircleWithFillColor(HIGHLIGHTED_DOT_FILL).should(
        "not.exist",
      );
    });

    it("should show the event popover when hovering on a stacked chart #74005", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [
          { name: "RC1", timestamp: "2027-10-20T00:00:00Z", icon: "star" },
        ],
      });

      H.visitQuestionAdhoc({
        dataset_query: {
          type: "query",
          query: {
            "source-table": ORDERS_ID,
            aggregation: [["count"], ["sum", ["field", ORDERS.TOTAL, null]]],
            breakout: [
              ["field", ORDERS.CREATED_AT, { "temporal-unit": "month" }],
            ],
          },
          database: SAMPLE_DB_ID,
        },
        display: "line",
        visualization_settings: {
          "graph.split_panels": true,
        },
      });

      H.timelineEventChip("RC1").should("be.visible").realHover();
      cy.findByTestId("timeline-event-popover")
        .findByText("RC1")
        .should("be.visible");
    });

    it("should stack close events from different data points and spread the stack on hover", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [
          {
            name: "Jan release",
            timestamp: "2027-01-30T00:00:00Z",
            icon: "star",
          },
          {
            name: "Feb fix",
            timestamp: "2027-02-02T00:00:00Z",
            icon: "warning",
          },
          {
            name: "Summer party",
            timestamp: "2028-07-04T00:00:00Z",
            icon: "cake",
          },
        ],
      });

      H.visitQuestionAdhoc({
        dataset_query: {
          type: "query",
          query: {
            "source-table": ORDERS_ID,
            aggregation: [["count"]],
            breakout: [
              ["field", ORDERS.CREATED_AT, { "temporal-unit": "month" }],
            ],
          },
          database: SAMPLE_DB_ID,
        },
        display: "line",
      });

      cy.log("adjacent-month events collapse into an overlapped stack");
      cy.findByTestId("timeline-event-stack").should(
        "have.attr",
        "data-expanded",
        "false",
      );
      H.timelineEventChip("Summer party").should("be.visible");

      cy.log("hovering the stack spreads it and hides unrelated chips");
      H.timelineEventChip("Feb fix").realHover({ position: "right" });
      cy.findByTestId("timeline-event-stack").should(
        "have.attr",
        "data-expanded",
        "true",
      );
      H.timelineEventChip("Summer party").should("not.be.visible");
      H.timelineEventMarkerLine().should("exist");

      cy.log("a spread member behaves like a regular chip");
      H.timelineEventChip("Jan release").realHover();
      cy.findByTestId("timeline-event-popover")
        .findByText("Jan release")
        .should("be.visible");
      H.timelineEventMarkerLine().should("exist");

      cy.log("moving away collapses the stack and restores other chips");
      cy.findByTestId("qb-header").realHover();
      cy.findByTestId("timeline-event-stack").should(
        "have.attr",
        "data-expanded",
        "false",
      );
      H.timelineEventChip("Summer party").should("be.visible");
    });

    it("should collapse close events into a count chip and focus the sidebar on the group from 'See all' or the chip", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [
          { name: "Alpha", timestamp: "2027-10-03T00:00:00Z" },
          { name: "Beta", timestamp: "2027-10-10T00:00:00Z" },
          { name: "Gamma", timestamp: "2027-10-17T00:00:00Z" },
          { name: "Delta", timestamp: "2027-10-24T00:00:00Z" },
        ],
      });
      // a second timeline that must be filtered out while the cluster is focused
      H.createTimelineWithEvents({
        timeline: { name: "Other" },
        events: [{ name: "Outsider", timestamp: "2028-01-15T00:00:00Z" }],
      });

      H.visitQuestionAdhoc({
        dataset_query: {
          type: "query",
          query: {
            "source-table": ORDERS_ID,
            aggregation: [["count"]],
            breakout: [
              ["field", ORDERS.CREATED_AT, { "temporal-unit": "month" }],
            ],
          },
          database: SAMPLE_DB_ID,
        },
        display: "line",
      });

      cy.findByTestId("visualization-root").findByTestId(
        "timeline-events-band",
      );

      cy.log(
        "the four same-month events collapse into one chip with the count",
      );
      H.timelineEventChip("4 events").should("be.visible").and("contain", "4");

      cy.log("hovering shows the first three events and a 'See all' link");
      H.timelineEventChip("4 events").realHover();
      cy.findByTestId("timeline-event-popover").within(() => {
        cy.findByText("Alpha").should("be.visible");
        cy.findByText("Gamma").should("be.visible");
        cy.findByText("Delta").should("not.exist");
        cy.findByText("See all").click();
      });

      cy.findByTestId("timeline-event-popover").should("not.exist");
      cy.log("'See all' selects the cluster and focuses the sidebar on it");
      H.timelineEventChip("4 events").should(
        "have.attr",
        "data-selected",
        "true",
      );
      cy.findByTestId("sidebar-content").within(() => {
        H.timelineEventCard("Alpha").should("be.visible");
        H.timelineEventCard("Delta").should("be.visible");
      });

      cy.log("the unrelated timeline is filtered out of the focused list");
      cy.findByTestId("sidebar-content")
        .findByText("Other")
        .should("not.exist");

      cy.log("'All events' restores the full list of timelines");
      cy.findByTestId("timeline-sidebar-show-all").click();
      cy.findByTestId("sidebar-content")
        .findByText("Other")
        .should("be.visible");

      cy.log("clicking the still-selected chip clears the selection");
      H.timelineEventChip("4 events").click();
      H.timelineEventChip("4 events").should(
        "have.attr",
        "data-selected",
        "false",
      );
      cy.findByTestId("sidebar-content")
        .findByText("Other")
        .should("be.visible");

      cy.log("clicking it again focuses the sidebar on the group");
      H.timelineEventChip("4 events").click();
      cy.findByTestId("timeline-sidebar-show-all").should("be.visible");
      cy.findByTestId("sidebar-content").within(() => {
        H.timelineEventCard("Alpha").should("be.visible");
        cy.findByText("Other").should("not.exist");
      });
    });

    it("should select a single event and open the full sidebar when its chip is clicked", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [{ name: "RC1", timestamp: "2027-01-01T00:00:00Z" }],
      }).then(({ timeline }) => createQuestionWithTimeline(timeline.id));

      H.visitQuestion("@questionId");

      H.timelineEventChip("RC1").should("be.visible").click();

      cy.log("the sidebar opens (unfiltered) with the event selected");
      H.timelineEventChip("RC1").should("have.attr", "data-selected", "true");
      cy.findByTestId("sidebar-content").findByText("RC1").should("be.visible");
      cy.findByTestId("timeline-sidebar-show-all").should("not.exist");
    });

    // TODO @nemanjaglumac 2026-04-17: Simplify or potentially remove this repro altogether!
    // It is hacky and it is fragile because it relies on the MAX date in the ORDERS table
    // so all timestamps need to be carefully hard coded relative to it.
    // I've added comments that hopefully make this easier for the next person to understand.
    // Additionally, one granularity bucket would sufficiently reproduce the issue.
    it("should not filter out events in last period (metabase#23336)", () => {
      H.createTimelineWithEvents({
        events: [
          // All events are AFTER the ORDERS max (~Apr 19, 2029) but within
          // the last bucket of their respective granularity. The bug (#23336)
          // was that events in the last period's extended range were filtered out.
          // Last week bucket is Apr 13-19 (Sun-Sat): Apr 20 is next week
          { name: "Last week", timestamp: "2029-04-20T12:00:00Z" },
          // Last month bucket is April: Apr 27 is still in April
          { name: "Last month", timestamp: "2029-04-27T12:00:00Z" },
          // Last quarter bucket is Q2: May 10 is still in Q2
          { name: "Last quarter", timestamp: "2029-05-10T12:00:00Z" },
          // Last year bucket is 2029: Sep 10 is still in 2029
          { name: "Last year", timestamp: "2029-09-10T12:00:00Z" },
        ],
      });

      H.visitQuestionAdhoc({
        dataset_query: {
          type: "query",
          database: SAMPLE_DB_ID,
          query: {
            aggregation: [["count"]],
            breakout: [
              ["field", ORDERS.CREATED_AT, { "temporal-unit": "week" }],
            ],
            "source-table": ORDERS_ID,
          },
        },
      });

      cy.icon("calendar").click();

      // Week
      H.rightSidebar().within(() => {
        cy.findByText("Last week").should("exist");
        cy.findByText("Last month").should("not.exist");
        cy.findByText("Last quarter").should("not.exist");
        cy.findByText("Last year").should("not.exist");
      });

      // Month
      cy.findByTestId("timeseries-chrome").findByText("Week").click();
      H.popover().findByText("Month").click();
      cy.wait("@dataset");
      H.rightSidebar().within(() => {
        cy.findByText("Last week").should("exist");
        cy.findByText("Last month").should("exist");
        cy.findByText("Last quarter").should("not.exist");
        cy.findByText("Last year").should("not.exist");
      });

      // Quarter
      cy.findByTestId("timeseries-chrome").findByText("Month").click();
      H.popover().findByText("Quarter").click();
      cy.wait("@dataset");
      H.rightSidebar().within(() => {
        cy.findByText("Last week").should("exist");
        cy.findByText("Last month").should("exist");
        cy.findByText("Last quarter").should("exist");
        cy.findByText("Last year").should("not.exist");
      });

      // Year
      cy.findByTestId("timeseries-chrome").findByText("Quarter").click();
      H.popover().findByText("Year").click();
      cy.wait("@dataset");
      H.rightSidebar().within(() => {
        cy.findByText("Last week").should("exist");
        cy.findByText("Last month").should("exist");
        cy.findByText("Last quarter").should("exist");
        cy.findByText("Last year").should("exist");
      });
    });
  });

  describe("saved selections", () => {
    beforeEach(() => {
      cy.signInAsAdmin();
      cy.intercept("PUT", "/api/card/*").as("updateQuestion");
    });

    it("should keep changing the display after a selected timeline is deleted", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [{ name: "RC1", timestamp: "2027-10-20T00:00:00Z" }],
      }).then(({ timeline }) => {
        H.createQuestion(
          {
            name: "Orders by month",
            display: "table",
            query: {
              "source-table": ORDERS_ID,
              aggregation: [["count"]],
              breakout: [
                ["field", ORDERS.CREATED_AT, { "temporal-unit": "month" }],
              ],
            },
            visualization_settings: {
              "timeline.selected_timeline_ids": [timeline.id],
            },
          },
          { wrapId: true },
        );
        cy.request("DELETE", `/api/timeline/${timeline.id}`);
      });

      H.visitQuestion("@questionId");
      H.openVizTypeSidebar();
      H.vizTypeSidebar().findByTestId("Line-button").click();
      H.echartsContainer().findByText("Created At: Month").should("be.visible");
      H.saveSavedQuestion();

      cy.get("@updateQuestion").its("response.statusCode").should("eq", 200);
      cy.findByTestId("qb-header").button("Save").should("not.exist");
    });

    it("should duplicate a question whose selected timeline the user cannot access", () => {
      cy.intercept("POST", "/api/card").as("cardCreate");
      H.createCollection({ name: "Events" }).then(({ body: { id } }) => {
        H.createTimelineWithEvents({
          timeline: { name: "Releases", collection_id: id },
          events: [{ name: "RC1", timestamp: "2027-10-20T00:00:00Z" }],
        }).then(({ timeline }) => {
          H.createQuestion(
            {
              name: "Orders by month",
              display: "line",
              query: {
                "source-table": ORDERS_ID,
                aggregation: [["count"]],
                breakout: [
                  ["field", ORDERS.CREATED_AT, { "temporal-unit": "month" }],
                ],
              },
              visualization_settings: {
                "timeline.selected_timeline_ids": [timeline.id],
              },
            },
            { wrapId: true },
          );
        });
        cy.updateCollectionGraph({
          [ALL_USERS_GROUP]: { [id]: "none" },
          [COLLECTION_GROUP]: { [id]: "none" },
          [DATA_GROUP]: { [id]: "none" },
        });
      });

      cy.signInAsNormalUser();
      H.visitQuestion("@questionId");
      H.openQuestionActions();
      H.popover().findByText("Duplicate").click();
      H.modal().button("Duplicate").click();

      cy.wait("@cardCreate").its("response.statusCode").should("eq", 200);
      cy.findByTestId("qb-header-left-side")
        .findByDisplayValue("Orders by month - Duplicate")
        .should("be.visible");
    });
  });

  describe("as readonly user", () => {
    it("should not allow creating default timelines", () => {
      cy.signIn("readonly");
      H.visitQuestion(ORDERS_BY_YEAR_QUESTION_ID);
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Created At: Year").should("be.visible");

      cy.icon("calendar").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText(/Events in Metabase/);
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Create event").should("not.exist");
    });

    it("should not allow creating or editing events", () => {
      cy.signInAsAdmin();
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [{ name: "RC1", timestamp: "2027-10-20T00:00:00Z" }],
      });
      cy.signOut();
      cy.signIn("readonly");
      H.visitQuestion(ORDERS_BY_YEAR_QUESTION_ID);
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Created At: Year").should("be.visible");

      cy.icon("calendar").click();
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Releases").should("be.visible");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Create event").should("not.exist");
      H.rightSidebar().icon("ellipsis").should("not.exist");
    });
  });

  describe("analytics", () => {
    beforeEach(() => {
      H.resetSnowplow();
      cy.signInAsAdmin();
      H.enableTracking();
    });

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

    it("should track opening the events panel and saving a question with a recorded selection", () => {
      H.createTimelineWithEvents({
        timeline: { name: "Releases" },
        events: [{ name: "RC1", timestamp: "2027-10-20T00:00:00Z" }],
      }).then(({ timeline }) => createQuestionWithTimeline(timeline.id));

      H.visitQuestion("@questionId");
      H.timelineEventChip("RC1").should("be.visible");

      cy.icon("calendar").click();
      H.expectUnstructuredSnowplowEvent({
        event: "question_events_panel_opened",
        triggered_from: "footer",
      });

      H.toggleTimelineEventVisibility("RC1");
      H.timelineEventChip("RC1").should("not.exist");
      H.saveSavedQuestion();

      cy.get("@questionId").then((questionId) =>
        H.expectUnstructuredSnowplowEvent({
          event: "question_timeline_events_saved",
          target_id: questionId,
        }),
      );
    });
  });
});

function createQuestionWithTimeline(timelineId) {
  return H.createQuestion(
    {
      name: "Orders by year",
      display: "line",
      query: {
        "source-table": ORDERS_ID,
        aggregation: [["count"]],
        breakout: [["field", ORDERS.CREATED_AT, { "temporal-unit": "year" }]],
      },
      visualization_settings: {
        "timeline.selected_timeline_ids": [timelineId],
      },
    },
    { wrapId: true },
  );
}