Skip to content

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

128 KB
Raw Blame
import dedent from "ts-dedent";

import {
  SAMPLE_DB_ID,
  USER_GROUPS,
  WRITABLE_DB_ID,
} from "e2e/support/cypress_data";
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import {
  ALL_USERS_GROUP_ID,
  NORMAL_USER_ID,
} from "e2e/support/cypress_sample_instance_data";
import { createLibraryWithItems } from "e2e/support/test-library-data";
import type {
  CardType,
  CollectionId,
  PythonTransformTableAliases,
  TransformSourceCheckpointStrategy,
  TransformTagId,
} from "metabase-types/api";
import { DataPermissionValue } from "metabase-types/api/permissions";

const { H } = cy;

const { ORDERS_ID } = SAMPLE_DATABASE;

const DB_NAME = "Writable Postgres12";
const SOURCE_TABLE = "Animals";
const TARGET_TABLE = "transform_table";
const TARGET_TABLE_2 = "transform_table_2";
const TARGET_SCHEMA = "Schema A";
const TARGET_SCHEMA_2 = "Schema B";
const CUSTOM_SCHEMA = "custom_schema";

describe("scenarios > admin > transforms", { tags: ["@external"] }, () => {
  beforeEach(() => {
    H.restore("postgres-writable");
    H.resetTestTable({ type: "postgres", table: "many_schemas" });
    H.resetSnowplow();
    cy.signInAsAdmin();
    H.activateToken("pro-self-hosted");
    H.updateSetting("transforms-enabled", true);
    H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SOURCE_TABLE });

    cy.intercept("PUT", "/api/field/*").as("updateField");
    cy.intercept("POST", "/api/transform").as("createTransform");
    cy.intercept("PUT", "/api/transform/*").as("updateTransform");
    cy.intercept("DELETE", "/api/transform/*").as("deleteTransform");
    cy.intercept("DELETE", "/api/transform/*/table").as("deleteTransformTable");
    cy.intercept("POST", "/api/transform-tag").as("createTag");
    cy.intercept("PUT", "/api/transform-tag/*").as("updateTag");
    cy.intercept("DELETE", "/api/transform-tag/*").as("deleteTag");
  });

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

  describe("creation", () => {
    it("should be able to create and run an mbql transform", () => {
      cy.log("create a new transform");
      visitTransformListPage();
      cy.button("Create a transform").click();
      H.popover().findByText("Query builder").click();

      H.expectUnstructuredSnowplowEvent({
        event: "transform_create",
        event_detail: "query",
      });

      H.miniPicker().within(() => {
        cy.findByText(DB_NAME).click();
        cy.findByText(TARGET_SCHEMA).click();
        cy.findByText(SOURCE_TABLE).click();
      });
      getQueryEditor().button("Save").click();
      H.modal().within(() => {
        cy.findByLabelText("Name").clear().type("MBQL");

        cy.log("should auto-populate table name based on transform name...");
        cy.findByLabelText("Table name").should("have.value", "mbql");
        cy.findByLabelText("Table name").clear().type(TARGET_TABLE);

        cy.log("...unless user has manually modified the table name");
        cy.findByLabelText("Name").type(" transform");
        cy.findByLabelText("Table name").should("have.value", TARGET_TABLE);

        cy.log("should not allow to overwrite an existing table");
        cy.findByLabelText("Table name").clear().type(SOURCE_TABLE);
        cy.button("Save").click();
        cy.wait("@createTransform");
        cy.findByText("A table with that name already exists.").should(
          "be.visible",
        );
        cy.findByLabelText("Table name").clear().type(TARGET_TABLE);

        // The submit button shows "Failed" for a few seconds after the rejected save.
        cy.get("button[type=submit]").click();
        cy.wait("@createTransform");
      });

      cy.log("the target table does not exist before the first run");
      H.DataStudio.Transforms.settingsTab().click();
      getSchemaLink()
        .should("have.text", TARGET_SCHEMA)
        .should("have.attr", "aria-disabled", "false");
      getTableLink({ isActive: false })
        .should("have.text", TARGET_TABLE)
        .should("have.attr", "aria-disabled", "true");

      cy.log("run the transform and make sure its table can be queried");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");
      H.expectUnstructuredSnowplowEvent({
        event: "transform_trigger_manual_run",
      });

      H.DataStudio.Transforms.settingsTab().click();
      getSchemaLink().should("have.attr", "aria-disabled", "false");
      getTableLink().should("have.attr", "aria-disabled", "false").click();
      H.queryBuilderHeader().findByText("Transform Table").should("be.visible");
      H.assertQueryBuilderRowCount(3);
      H.expectUnstructuredSnowplowEvent({
        event: "transform_created",
      });

      cy.log("the SQL preview has no absolute-max-results LIMIT");
      cy.go("back");
      H.DataStudio.Transforms.definitionTab().click();
      H.DataStudio.Transforms.clickEditDefinition();
      cy.url().should("include", "/edit");
      getQueryEditor().findByLabelText("View SQL").click();
      H.sidebar().should("be.visible");
      H.NativeEditor.value()
        .should("contain", "FROM")
        .and("not.match", /\bLIMIT\b/i);
    });

    it("should not show you the library in the mini picker when building transforms (uxw-2403)", () => {
      createLibraryWithItems();

      visitTransformListPage();
      cy.button("Create a transform").click();
      H.popover().findByText("Query builder").click();
      H.miniPicker().within(() => {
        cy.findByText(DB_NAME).should("exist");
        cy.findByText("Our analytics").should("exist");
        cy.findByText("Browse all").should("exist");
        cy.findByText("Data").should("not.exist");
      });

      H.goToMainApp();
      H.modal().button("Discard changes").click();
      H.newButton("Question").click();

      H.miniPicker().within(() => {
        cy.findByText("Our analytics").should("not.exist");
        cy.findByText("Browse all").should("exist");
        cy.findByText("Data").click();
        cy.findByText("Orders").should("exist");
      });
    });

    it("should be able to create and run a SQL transform", () => {
      cy.log("create a new transform");
      visitTransformListPage();
      cy.button("Create a transform").click();
      H.popover().findByText("SQL query").click();
      H.expectUnstructuredSnowplowEvent({
        event: "transform_create",
        event_detail: "native",
      });

      H.popover().findByText(DB_NAME).click();
      H.NativeEditor.type(`SELECT * FROM "${TARGET_SCHEMA}"."${SOURCE_TABLE}"`);
      getQueryEditor().button("Save").click();
      H.modal().within(() => {
        cy.findByLabelText("Name").clear().type("SQL transform");
        cy.findByLabelText("Table name").type(TARGET_TABLE);
        cy.button("Save").click();
        cy.wait("@createTransform");
      });
      H.expectUnstructuredSnowplowEvent({
        event: "transform_created",
      });

      cy.log("run the transform and make sure its table can be queried");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");
      H.expectUnstructuredSnowplowEvent({
        event: "transform_trigger_manual_run",
      });

      H.DataStudio.Transforms.settingsTab().click();
      getTableLink().click();
      H.queryBuilderHeader().findByText(DB_NAME).should("be.visible");
      H.assertQueryBuilderRowCount(3);
    });

    it(
      "should be possible to create and run a Python transform",
      { tags: ["@python"] },
      () => {
        H.setPythonRunnerSettings();
        cy.log("create a new transform");
        visitTransformListPage();
        cy.button("Create a transform").click();
        H.popover().findByText("Python script").click();
        H.expectUnstructuredSnowplowEvent({
          event: "transform_create",
          event_detail: "python",
        });

        cy.findByTestId("python-transform-top-bar")
          .findByText("Writable Postgres12")
          .click();

        cy.log("Unsupported databases should be disabled");
        H.popover()
          .findByRole("option", { name: "Sample Database" })
          .should("have.attr", "aria-disabled", "true");

        cy.log("Select database");
        H.popover().findByText(DB_NAME).click();

        cy.log("open the editor search panel with Cmd/Ctrl+F (metabase#73290)");
        H.PythonEditor.focus();
        cy.realPress([H.metaKey, "f"]);
        cy.findByTestId("python-editor")
          .find(".cm-panels")
          .should("be.visible");

        getPythonDataPicker()
          .findByRole("button", { name: "Select a table…" })
          .click();

        cy.log(
          "the editor search panel must not paint over the modal (metabase#73290)",
        );
        H.entityPickerModal().should("be.visible");
        cy.findByTestId("python-editor")
          .find(".cm-panels")
          .should("not.be.visible");

        H.entityPickerModal().findByText("Animals").click();

        getPythonDataPicker().within(() => {
          cy.findByText("Writable Postgres12 / Schema A").should("be.visible");
          cy.findByText("Animals").should("be.visible");
          cy.findByPlaceholderText("Enter alias").should(
            "have.value",
            "animals",
          );
          cy.findByPlaceholderText("Enter alias")
            .clear()
            .type("foo bar")
            .blur();
          cy.findByPlaceholderText("Enter alias").should(
            "have.value",
            "foo_bar",
          );
        });

        H.PythonEditor.value()
          .should("contain", "def transform(foo_bar):")
          .should(
            "contain",
            'foo_bar: DataFrame containing the data from the "Writable Postgres12.Schema A.Animals" table',
          );

        getPythonDataPicker().within(() => {
          cy.findByText("Add a table").click();
          cy.button("Select a table…").click();
        });

        H.entityPickerModal().within(() => {
          cy.log("Selecting the same table should not be possible");
          cy.findByText("Animals")
            .parent()
            .parent()
            .parent()
            .should("have.attr", "data-disabled", "true");

          cy.findByText("Schema B").click();
          cy.findByText("Animals").click();
        });

        getPythonDataPicker().within(() => {
          cy.icon("refresh").click();
          cy.findAllByPlaceholderText("Enter alias")
            .first()
            .should("have.value", "animals_1")
            .clear()
            .type("foo bar")
            .blur()
            .should("have.value", "foo_bar");
          cy.findAllByPlaceholderText("Enter alias")
            .eq(1)
            .should("have.value", "animals")
            .clear()
            .type("foo bar")
            .blur()
            .should("have.value", "foo_bar_1");
        });

        getQueryEditor().button("Save").click();

        H.modal().within(() => {
          cy.findByLabelText("Name").clear().type("Python transform");
          cy.findByLabelText("Table name").clear().type("python_transform");
          cy.button("Save").click();
        });

        H.expectUnstructuredSnowplowEvent({
          event: "transform_created",
        });

        cy.log("run the transform and make sure its table can be queried");
        H.DataStudio.Transforms.runTab().click();
        H.runTransformFromUiAndWaitForStatus("succeeded");
        H.expectUnstructuredSnowplowEvent({
          event: "transform_trigger_manual_run",
        });
        H.DataStudio.Runs.content().should(
          "contain",
          "Executing Python transform",
        );

        H.DataStudio.Transforms.settingsTab().click();
        getTableLink().click();
        H.queryBuilderHeader().findByText(DB_NAME).should("be.visible");
        H.assertQueryBuilderRowCount(1);
      },
    );

    it("should be able to create and run a transform from a question or a model", () => {
      function testCardSource({ type }: { type: CardType }) {
        H.resetSnowplow();

        cy.log("create a query in the target database");
        H.getTableId({ name: SOURCE_TABLE, databaseId: WRITABLE_DB_ID }).then(
          (tableId) =>
            H.createQuestion({
              name: `Test ${type}`,
              type,
              database: WRITABLE_DB_ID,
              query: {
                "source-table": tableId,
              },
            }),
        );

        cy.log("create a new transform");
        visitTransformListPage();
        cy.button("Create a transform").click();
        H.popover().findByText("Copy of a saved question").click();
        H.expectUnstructuredSnowplowEvent({
          event: "transform_create",
          event_detail: "saved-question",
        });

        H.pickEntity({ path: ["Our analytics", `Test ${type}`], select: true });

        getQueryEditor().button("Save").click();
        H.modal().within(() => {
          cy.findByLabelText("Name").clear().type(`${type} transform`);
          cy.findByLabelText("Table name").type(`${type}_transform`);
          cy.button("Save").click();
          cy.wait("@createTransform");
        });

        H.expectUnstructuredSnowplowEvent({
          event: "transform_created",
        });

        cy.log("run the transform and make sure its table can be queried");
        H.DataStudio.Transforms.runTab().click();
        H.runTransformFromUiAndWaitForStatus("succeeded");
        H.expectUnstructuredSnowplowEvent({
          event: "transform_trigger_manual_run",
        });

        H.DataStudio.Transforms.settingsTab().click();
        getTableLink().click();
        H.queryBuilderHeader().findByText(DB_NAME).should("be.visible");
        H.assertQueryBuilderRowCount(3);
      }

      testCardSource({ type: "question" });
      testCardSource({ type: "model" });
    });

    it("should be possible to convert an MBQL transform to a SQL transform", () => {
      const EXPECTED_QUERY = `SELECT
  "Schema A"."Animals"."name" AS "name",
  "Schema A"."Animals"."score" AS "score"
FROM
  "Schema A"."Animals"
LIMIT
  5`;

      createMbqlTransform({ visitTransform: true });
      H.DataStudio.Transforms.clickEditDefinition();
      cy.url().should("include", "/edit");

      getQueryEditor().findByLabelText("View SQL").click();
      H.sidebar().should("be.visible");
      H.NativeEditor.value().should("eq", EXPECTED_QUERY);

      H.sidebar().findByText("Convert this transform to SQL").click();
      H.sidebar().should("be.visible");

      H.NativeEditor.value().should("eq", EXPECTED_QUERY);
      getQueryEditor().button("Save").click();
      cy.wait("@updateTransform");
      // Saving returns to read-only view mode; the "Run" tab only exists there,
      // so wait for the navigation off /edit before clicking it.
      cy.url().should("not.include", "/edit");
      H.NativeEditor.value().should("eq", EXPECTED_QUERY);

      cy.log("run the transform and make sure its table can be queried");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");
      H.expectUnstructuredSnowplowEvent({
        event: "transform_trigger_manual_run",
      });

      H.DataStudio.Transforms.settingsTab().click();
      getTableLink().click();
      H.queryBuilderHeader().findByText(DB_NAME).should("be.visible");
      H.assertQueryBuilderRowCount(3);
    });

    it("should be able to create a new schema when saving a transform", () => {
      visitTransformListPage();
      cy.button("Create a transform").click();
      H.popover().findByText("Query builder").click();

      H.miniPicker().within(() => {
        cy.findByText(DB_NAME).click();
        cy.findByText(TARGET_SCHEMA).click();
        cy.findByText(SOURCE_TABLE).click();
      });
      getQueryEditor().button("Save").click();
      H.modal().within(() => {
        cy.findByLabelText("Name").clear().type("MBQL transform");
        cy.findByLabelText("Table name").clear().type(TARGET_TABLE);
        cy.findByLabelText("Schema").clear().type(CUSTOM_SCHEMA);
      });
      H.popover().findByText("Create new schema").click();
      H.modal().within(() => {
        cy.button("Save").click();
        cy.wait("@createTransform");
      });

      H.DataStudio.Transforms.settingsTab().click();
      getSchemaLink()
        .should("have.text", CUSTOM_SCHEMA)
        .should("have.attr", "aria-disabled", "true")
        .realHover();
      H.tooltip()
        .should("be.visible")
        .should(
          "have.text",
          "This schema will be created when the transform runs",
        );
      getTableLink({ isActive: false })
        .should("have.text", TARGET_TABLE)
        .should("have.attr", "aria-disabled", "true");

      cy.log("run the transform and verify the table");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      H.DataStudio.Transforms.settingsTab().click();
      getSchemaLink()
        .should("have.text", CUSTOM_SCHEMA)
        .should("have.attr", "aria-disabled", "false")
        .realHover();
      getTableLink().click();
      H.queryBuilderHeader().findByText(CUSTOM_SCHEMA).should("be.visible");
      H.assertQueryBuilderRowCount(3);
    });

    it("should not be possible to create an MBQL transform from an unsupported database or from metrics", () => {
      H.getTableId({ name: "Animals", databaseId: WRITABLE_DB_ID }).then(
        (tableId) => {
          H.createQuestion({
            name: "Animal Question",
            database: WRITABLE_DB_ID,
            query: {
              "source-table": tableId,
            },
          });
          H.createQuestion({
            name: "Animal Metric",
            type: "metric",
            database: WRITABLE_DB_ID,
            query: {
              "source-table": tableId,
              aggregation: [["count"]],
            },
          });
        },
      );

      visitTransformListPage();
      cy.button("Create a transform").click();
      H.popover().findByText("Query builder").click();

      H.miniPicker().within(() => {
        // no sample db in mini picker
        cy.findByText(/Writable Postgres/).should("be.visible");
        cy.findByText("Sample Database").should("not.exist");

        cy.findByText("Our analytics").click();
        cy.findByText("Animal Question").should("be.visible");
        cy.findByText(/metric/i).should("not.exist");
      });

      H.miniPickerHeader().click(); // go back
      H.miniPickerBrowseAll().click();
      H.entityPickerModal().within(() => {
        H.entityPickerModalItem(0, "Databases").click();
        cy.findAllByTestId("picker-item")
          .contains("Sample Database")
          .should("have.attr", "data-disabled", "true");
        cy.findAllByTestId("picker-item")
          .contains(/Writable Postgres/)
          .should("not.have.attr", "data-disabled");

        cy.log("Should show a tooltip explaining why the database is disabled");
        cy.findAllByTestId("picker-item")
          .contains("Sample Database")
          .realHover();
      });
      H.tooltip().should(
        "contain.text",
        "Transforms can't be enabled on the Sample Database.",
      );

      cy.log("metrics are disabled in the entity picker");
      H.entityPickerModal().within(() => {
        cy.findByText("Our analytics").click();
        cy.findAllByTestId("picker-item")
          .contains("Animal Metric")
          .should("have.attr", "data-disabled", "true");
      });
    });

    it("should not be possible to create a sql transform from a table from an unsupported database", () => {
      visitTransformListPage();
      cy.button("Create a transform").click();

      H.popover().findByText("SQL query").click();

      cy.findByTestId("gui-builder-data")
        .findByText("Writable Postgres12")
        .click();

      H.popover()
        .findByRole("option", { name: "Sample Database" })
        .should("have.attr", "aria-disabled", "true")
        .click();

      cy.log("Clicking the disabled item does not close the popover");
      H.popover().should("be.visible");

      cy.log("Should show a tooltip explaining why the database is disabled");
      H.popover().findByRole("option", { name: "Sample Database" }).realHover();
      H.tooltip().should(
        "contain.text",
        "Transforms can't be enabled on the Sample Database.",
      );
    });

    it("should not show the 'Show details' buttons in ID columns (metabase#64473)", () => {
      const databaseId = WRITABLE_DB_ID;
      const sourceTable = SOURCE_TABLE;
      const nameColumn = "name";

      H.getTableId({ databaseId, name: sourceTable }).then((tableId) => {
        H.getFieldId({ tableId, name: nameColumn }).then((nameColumnId) => {
          // Make name a key
          cy.request("PUT", `/api/field/${nameColumnId}`, {
            semantic_type: "type/PK",
          });
        });
      });

      createMbqlTransform({
        databaseId,
        sourceTable,
        visitTransform: true,
      });

      H.DataStudio.Transforms.clickEditDefinition();

      getQueryEditor().within(() => {
        cy.findByTestId("run-button").eq(0).click();
        cy.findByTestId("loading-indicator").should("not.exist");
      });
      H.assertTableData({ columns: ["Name", "Score"] });
      getQueryEditor().findAllByTestId("detail-shortcut").should("not.exist");
    });

    it("should not be possible to create a transform from a question or a model that is based on an unsupported database", () => {
      const cardTypes: CardType[] = ["question", "model"];

      cy.log("create queries in the target database");
      cardTypes.forEach((type) => {
        H.createQuestion({
          name: `Test ${type}`,
          type,
          database: SAMPLE_DB_ID,
          query: {
            "source-table": ORDERS_ID,
          },
        });
      });

      cy.log("create a new transform");
      visitTransformListPage();
      cy.button("Create a transform").click();
      H.popover().findByText("Copy of a saved question").click();
      H.entityPickerModal().within(() => {
        cy.findByText("Our analytics").click();
        cardTypes.forEach((type) => {
          cy.findByText(`Test ${type}`)
            .closest("a")
            .should("have.attr", "data-disabled", "true");
        });
      });
    });

    it("should not auto-pivot query results for MBQL transforms", () => {
      cy.log("create a new transform");
      visitTransformListPage();
      cy.button("Create a transform").click();
      H.popover().findByText("Query builder").click();

      cy.log("build a query with 1 aggregation and 2 breakouts");
      H.miniPicker().within(() => {
        cy.findByText(DB_NAME).click();
        cy.findByText(TARGET_SCHEMA).click();
        cy.findByText(SOURCE_TABLE).click();
      });
      H.getNotebookStep("summarize")
        .findByText("Pick a function or metric")
        .click();
      H.popover().findByText("Count of rows").click();
      H.getNotebookStep("summarize")
        .findByText("Pick a column to group by")
        .click();
      H.popover().findByText("Name").click();
      H.getNotebookStep("summarize")
        .findByTestId("breakout-step")
        .icon("add")
        .click();
      H.popover().findByText("Score").click();
      H.runButtonOverlay().click();

      cy.log("verify that no pivoting is applied");
      H.tableInteractiveHeader().within(() => {
        cy.findByText("Name").should("be.visible");
        cy.findByText(/Score/).should("be.visible");
        cy.findByText("Count").should("be.visible");
      });
    });
  });

  describe("tags", () => {
    it("should be able to add, remove, delete, and rename tags", () => {
      createMbqlTransform({ visitTransform: true });
      H.DataStudio.Transforms.runTab().click();
      getTagsInput().click();

      H.popover().findByText("hourly").click();
      cy.wait("@updateTransform");
      H.expectUnstructuredSnowplowEvent({
        event: "transform_tags_updated",
        triggered_from: "transform_run_page",
        event_detail: "tag_added",
        target_id: 1,
        result: "success",
      });

      assertOptionSelected("hourly");
      assertOptionNotSelected("daily");

      H.popover().findByText("daily").click();

      cy.wait("@updateTransform");
      assertOptionSelected("hourly");
      assertOptionSelected("daily");
      getTagsInput().type("{backspace}");
      cy.wait("@updateTransform");
      H.expectUnstructuredSnowplowEvent({
        event: "transform_tags_updated",
        triggered_from: "transform_run_page",
        event_detail: "tag_removed",
        target_id: 1,
        result: "success",
      });

      assertOptionSelected("hourly");
      assertOptionNotSelected("daily");

      cy.log("reload to close the popover and the tag toasts");
      cy.reload();
      H.DataStudio.Transforms.runTab().click();
      assertOptionSelected("hourly");
      H.undoToastList().should("have.length", 0);

      cy.log("delete a tag that is not selected");
      getTagsInput().click();
      H.popover()
        .findByText("daily")
        .parent()
        .findByLabelText("Delete tag")
        .click({ force: true });
      H.modal().within(() => {
        cy.button("Delete tag").click();
        cy.wait("@deleteTag");
      });
      H.undoToast().should("contain.text", "Transform tags updated");
      assertOptionSelected("hourly");

      getTagsInput().click();
      H.popover().within(() => {
        cy.findByText("weekly").should("be.visible");
        cy.findByText("daily").should("not.exist");
      });

      cy.log("rename a tag");
      H.popover()
        .findByText("weekly")
        .parent()
        .findByLabelText("Rename tag")
        .click({ force: true });
      H.modal().within(() => {
        cy.findByLabelText("Name").clear().type("weekly_changed");
        cy.button("Save").click();
        cy.wait("@updateTag");
      });

      getTagsInput().click();
      H.popover().findByText("weekly_changed").should("be.visible");
    });

    it("should update tags on all transforms when deleting them from another transform", () => {
      createMbqlTransform({ name: "Transform B" });
      createMbqlTransform({
        name: "Transform A",
        visitTransform: true,
      });

      cy.log("Add new tag to transform A");
      H.DataStudio.Transforms.runTab().click();
      getTagsInput().type("New tag");
      H.popover().findByText("New tag").click();
      cy.wait("@createTag");
      H.undoToast().should("contain.text", "Transform tags updated");
      assertOptionSelected("New tag");

      cy.log("Navigate to transform B");
      H.DataStudio.nav()
        .findByRole("link", { name: "Data transformation" })
        .click();
      cy.findByRole("treegrid").findByText("Transform B").click();

      cy.log("Remove the new tag from transform B");
      H.DataStudio.Transforms.runTab().click();
      getTagsInput().click();
      H.popover()
        .findByText("New tag")
        .parent()
        .findByLabelText("Delete tag")
        .click({ force: true });
      H.modal().within(() => {
        cy.button("Delete tag").click();
        cy.wait("@deleteTag");
      });

      cy.log("Navigate to transform A");
      getTransformsNavLink().click();
      cy.findByRole("treegrid").findByText("Transform A").click();

      cy.log("The tag should be gone");
      H.DataStudio.Transforms.runTab().click();
      getTagsInput().should("be.visible");
      assertOptionNotSelected("New tag");
    });
  });

  describe("settings inline editing", () => {
    it("should update source strategy and checkpoint field inline", () => {
      createMbqlTransform({ visitTransform: true });
      H.DataStudio.Transforms.settingsTab().click();

      cy.log("enable incremental once its checkpoint options have loaded");
      getIncrementalSwitch().findByRole("switch").should("be.enabled").click();
      cy.wait("@updateTransform").then(({ request, response }) => {
        expect(response?.statusCode).to.eq(200);
        expect(request.body.target.type).to.eq("table-incremental");
        expect(request.body.source["source-incremental-strategy"]).to.include({
          type: "checkpoint",
        });
      });

      cy.log("the settings are read back from the server after a reload");
      cy.reload();
      isIncrementalSwitchEnabled();
      getFieldPicker().should("have.value", "Score");
    });
  });

  describe("targets", () => {
    it("should be able to change the target before running a transform", () => {
      cy.log("create but do not run the transform");
      createMbqlTransform({ visitTransform: true });

      cy.log("modify the transform before running");
      H.DataStudio.Transforms.settingsTab().click();
      getTransformsTargetContent().button("Change target").click();
      H.modal().within(() => {
        cy.findByLabelText("New table name").should("have.value", TARGET_TABLE);
        cy.findByLabelText("Schema").should("have.value", TARGET_SCHEMA);

        cy.log("the target can't be one of the source tables");
        cy.findByLabelText("New table name").clear().type(SOURCE_TABLE);
        cy.button("Change target").click();
        cy.wait("@updateTransform")
          .its("response.statusCode")
          .should("eq", 400);
        cy.findByText(/Cyclic transform definitions detected/).should(
          "be.visible",
        );
        cy.button("Cancel").click();
      });
      H.modal().should("not.exist");

      getTransformsTargetContent().button("Change target").click();
      H.modal().within(() => {
        cy.findByLabelText("New table name").clear().type(TARGET_TABLE_2);
        cy.findByLabelText("Schema").click();
      });
      H.popover().findByText(TARGET_SCHEMA_2).click();
      H.modal().within(() => {
        cy.button("Change target").click();
        cy.wait("@updateTransform");
      });
      getSchemaLink()
        .should("have.text", TARGET_SCHEMA_2)
        .should("have.attr", "aria-disabled", "false");
      getTableLink({ isActive: false })
        .should("have.text", TARGET_TABLE_2)
        .should("have.attr", "aria-disabled", "true");

      cy.log("run the transform and verify the table");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      H.DataStudio.Transforms.settingsTab().click();
      getSchemaLink()
        .should("have.text", TARGET_SCHEMA_2)
        .should("have.attr", "aria-disabled", "false");
      getTableLink()
        .should("have.text", TARGET_TABLE_2)
        .should("have.attr", "aria-disabled", "false")
        .click();
      H.queryBuilderHeader().findByText(TARGET_SCHEMA_2).should("be.visible");
      H.assertQueryBuilderRowCount(3);
    });

    it("should be possible to create a new schema", () => {
      cy.log("create but do not run the transform");
      createMbqlTransform({ visitTransform: true });

      cy.log("modify the transform before running");
      H.DataStudio.Transforms.settingsTab().click();
      getTransformsTargetContent().button("Change target").click();
      H.modal().within(() => {
        cy.findByLabelText("New table name").should("have.value", TARGET_TABLE);
        cy.findByLabelText("Schema").should("have.value", TARGET_SCHEMA);
        cy.findByLabelText("New table name").clear().type(TARGET_TABLE_2);
        cy.findByLabelText("Schema").clear().type(CUSTOM_SCHEMA);
      });
      H.popover().findByText("Create new schema").click();
      H.modal().within(() => {
        cy.button("Change target").click();
        cy.wait("@updateTransform");
      });
      getSchemaLink()
        .should("have.text", CUSTOM_SCHEMA)
        .should("have.attr", "aria-disabled", "true");
      getTableLink({ isActive: false })
        .should("have.text", TARGET_TABLE_2)
        .should("have.attr", "aria-disabled", "true");

      cy.log("run the transform and verify the table");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      H.DataStudio.Transforms.settingsTab().click();
      getSchemaLink()
        .should("have.text", CUSTOM_SCHEMA)
        .should("have.attr", "aria-disabled", "false");
      getTableLink()
        .should("have.text", TARGET_TABLE_2)
        .should("have.attr", "aria-disabled", "false")
        .click();
      H.queryBuilderHeader().findByText(CUSTOM_SCHEMA).should("be.visible");
      H.assertQueryBuilderRowCount(3);
    });

    it("should be able to change the target after running a transform and keep the old target", () => {
      cy.log("create and run a transform");
      createMbqlTransform({ visitTransform: true });
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      cy.log("modify the transform after running");
      H.DataStudio.Transforms.settingsTab().click();
      getTransformsTargetContent().button("Change target").click();
      H.modal().within(() => {
        cy.findByLabelText("New table name").should("have.value", TARGET_TABLE);
        cy.findByLabelText("Schema").should("have.value", TARGET_SCHEMA);
        cy.findByLabelText("Keep transform_table").should("be.checked");
        cy.findByLabelText("New table name").clear().type(TARGET_TABLE_2);
        cy.button("Change target").click();
        cy.wait("@updateTransform");
      });
      getSchemaLink()
        .should("have.text", TARGET_SCHEMA)
        .should("have.attr", "aria-disabled", "false");
      getTableLink({ isActive: false })
        .should("have.text", TARGET_TABLE_2)
        .should("have.attr", "aria-disabled", "true");

      cy.log("run the transform and verify the new table");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      H.DataStudio.Transforms.settingsTab().click();
      getSchemaLink()
        .should("have.text", TARGET_SCHEMA)
        .should("have.attr", "aria-disabled", "false");
      getTableLink()
        .should("have.text", TARGET_TABLE_2)
        .should("have.attr", "aria-disabled", "false")
        .click();
      H.queryBuilderHeader()
        .findByText("Transform Table 2")
        .should("be.visible");
      H.assertQueryBuilderRowCount(3);

      cy.log("verify that the original question still works");
      visitTableQuestion();
      H.assertQueryBuilderRowCount(3);
    });

    it("should be able to change the target after running a transform and delete the old target", () => {
      cy.log("create and run a transform");
      createMbqlTransform({ visitTransform: true });
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      cy.log("modify the transform after running");
      H.DataStudio.Transforms.settingsTab().click();
      getTransformsTargetContent().button("Change target").click();
      H.modal().within(() => {
        cy.findByLabelText("New table name").should("have.value", TARGET_TABLE);
        cy.findByLabelText("Schema").should("have.value", TARGET_SCHEMA);
        cy.findByLabelText("New table name").clear().type(TARGET_TABLE_2);
        cy.findByLabelText("Delete transform_table").click();
        cy.button("Change target and delete old table").click();
        cy.wait("@deleteTransformTable");
        cy.wait("@updateTransform");
      });
      getSchemaLink()
        .should("have.text", TARGET_SCHEMA)
        .should("have.attr", "aria-disabled", "false");
      getTableLink({ isActive: false })
        .should("have.text", TARGET_TABLE_2)
        .should("have.attr", "aria-disabled", "true");

      cy.log("run the transform and verify the new table");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      H.DataStudio.Transforms.settingsTab().click();
      getSchemaLink()
        .should("have.text", TARGET_SCHEMA)
        .should("have.attr", "aria-disabled", "false");
      getTableLink()
        .should("have.text", TARGET_TABLE_2)
        .should("have.attr", "aria-disabled", "false")
        .click();
      H.queryBuilderHeader()
        .findByText("Transform Table 2")
        .should("be.visible");
      H.assertQueryBuilderRowCount(3);

      cy.log("verify that the original question does not work");
      visitTableQuestion();
      assertTableDoesNotExistError();
    });

    it("should be able to delete the target and restore the same target back", () => {
      cy.log("create and run a transform");
      createMbqlTransform({ visitTransform: true });
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      cy.log("delete the old target without creating the new one");
      H.DataStudio.Transforms.settingsTab().click();
      getTransformsTargetContent().button("Change target").click();
      H.modal().within(() => {
        cy.findByLabelText("New table name").clear().type(TARGET_TABLE_2);
        cy.findByLabelText("Delete transform_table").click();
        cy.button("Change target and delete old table").click();
        cy.wait("@deleteTransformTable");
        cy.wait("@updateTransform");
      });

      cy.log("change the target back to the original one");
      getTransformsTargetContent().button("Change target").click();
      H.modal().within(() => {
        cy.findByLabelText("New table name").clear().type(TARGET_TABLE);
        cy.button("Change target").click();
        cy.wait("@updateTransform");
      });

      cy.log("run the transform to re-create the original target");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      cy.log("verify the target is available");
      H.DataStudio.Transforms.settingsTab().click();
      getTableLink().click();
      H.queryBuilderHeader().findByText("Transform Table").should("be.visible");
      H.assertQueryBuilderRowCount(3);
    });

    it("should not allow to overwrite an existing table when changing the target", () => {
      cy.log("run a transform so its target table exists");
      createMbqlTransform({ visitTransform: true });
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      cy.log("change another transform's target to that table");
      createMbqlTransform({
        name: "Other transform",
        targetTable: TARGET_TABLE_2,
        visitTransform: true,
      });
      H.DataStudio.Transforms.settingsTab().click();
      getTransformsTargetContent().button("Change target").click();
      H.modal().within(() => {
        cy.findByLabelText("New table name").clear().type(TARGET_TABLE);
        cy.button("Change target").click();
        cy.wait("@updateTransform")
          .its("response.statusCode")
          .should("eq", 409);
        cy.findByText("A table with that name already exists.").should(
          "be.visible",
        );
      });
    });
  });

  describe("metadata", () => {
    it("should be able to see the target database and schema and edit table metadata after table creation", () => {
      cy.log("before table creation");
      createMbqlTransform({ visitTransform: true });
      H.DataStudio.Transforms.settingsTab().click();
      getTransformsTargetContent().button("Change target").should("be.visible");
      getTransformsTargetContent()
        .findByText("Edit this table's metadata")
        .should("not.exist");

      getSchemaLink().should("have.text", TARGET_SCHEMA);
      getSchemaLink().click();
      H.main().within(() => {
        cy.findByText("Animals").should("be.visible");
        cy.findByText("Transform Table").should("not.exist");
      });
      cy.go("back");

      getDatabaseLink().should("have.text", DB_NAME);
      getDatabaseLink().click();
      H.main().within(() => {
        cy.findByText(TARGET_SCHEMA).should("be.visible");
        cy.findByText(TARGET_SCHEMA_2).should("be.visible");
      });
      cy.go("back");

      cy.log("after table creation");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");
      H.DataStudio.Transforms.settingsTab().click();
      getSchemaLink().click();
      H.main().within(() => {
        cy.findByText("Animals").should("be.visible");
        cy.findByText("Transform Table").should("be.visible");
      });
      cy.go("back");

      getDatabaseLink().click();
      H.main().within(() => {
        cy.findByText(TARGET_SCHEMA).should("be.visible");
        cy.findByText(TARGET_SCHEMA_2).should("be.visible");
      });
      cy.go("back");

      cy.log("edit table metadata");
      getTransformsTargetContent()
        .findByText("Edit this table's metadata")
        .click();
      H.DataModel.TableSection.clickFieldsTab();
      H.DataModel.TableSection.clickField("Name");
      H.DataModel.FieldSection.getNameInput().clear().type("New name").blur();
      cy.wait("@updateField");

      cy.log("verify query metadata");
      cy.go("back");
      cy.go("back");
      cy.go("back");
      getTableLink().click();
      H.assertTableData({ columns: ["New name", "Score"] });
    });
  });

  describe("queries", () => {
    it("should be able to update a MBQL query", () => {
      cy.log("create a new transform");
      createMbqlTransform({ visitTransform: true });

      cy.log("the query is read-only before edit mode");
      H.getNotebookStep("data")
        .findByText("Animals")
        .closest("button")
        .should("be.disabled");
      H.DataStudio.Transforms.getEditDefinitionLink().should(
        "have.attr",
        "href",
        "/data-studio/transforms/1/edit",
      );

      cy.log("visit edit mode");
      H.DataStudio.Transforms.clickEditDefinition();
      cy.url().should("include", "/edit");

      cy.log("update the query");
      H.getNotebookStep("data").button("Filter").click();
      H.popover().within(() => {
        cy.findByText("Name").click();
        cy.findByText("Duck").click();
        cy.button("Add filter").click();
      });

      getQueryEditor().button("Save").click();
      cy.wait("@updateTransform");

      cy.log("run the transform and make sure the query has changed");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      H.DataStudio.Transforms.settingsTab().click();
      getTableLink().click();
      H.queryBuilderHeader().findByText(DB_NAME).should("be.visible");
      H.assertQueryBuilderRowCount(1);
    });

    it("should be able to update a SQL query", () => {
      cy.log("create a new transform");
      createSqlTransform({
        sourceQuery: `SELECT * FROM "${TARGET_SCHEMA}"."${SOURCE_TABLE}"`,
        visitTransform: true,
      });

      cy.log("the query is read-only before edit mode");
      H.NativeEditor.get().should("have.attr", "contenteditable", "false");
      H.NativeEditor.get().should("have.attr", "aria-readonly", "true");
      H.DataStudio.Transforms.getEditDefinitionLink().should(
        "have.attr",
        "href",
        "/data-studio/transforms/1/edit",
      );

      cy.log("visit edit mode");
      H.DataStudio.Transforms.clickEditDefinition();
      cy.url().should("include", "/edit");

      cy.log("update the query");
      H.NativeEditor.type(" WHERE name = 'Duck'");
      getQueryEditor().button("Save").click();
      cy.wait("@updateTransform");

      cy.log("run the transform and make sure the query has changed");
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");
      H.DataStudio.Transforms.settingsTab().click();
      getTableLink().click();
      H.queryBuilderHeader().findByText(DB_NAME).should("be.visible");
      H.assertQueryBuilderRowCount(1);
    });

    it(
      "should be able to view and update a Python query",
      { tags: ["@python"] },
      () => {
        H.setPythonRunnerSettings();
        cy.log("create a new transform");
        H.getTableId({ name: "Animals", databaseId: WRITABLE_DB_ID }).then(
          (id) => {
            createPythonTransform({
              body: dedent`
          import pandas as pd

          def transform(foo):
            return pd.DataFrame([{"foo": 42 }])
        `,
              sourceTables: pythonSourceTables("foo", id),
              visitTransform: true,
            });
          },
        );

        cy.log("should be in read-only mode by default");
        H.DataStudio.Transforms.getEditDefinitionLink().should(
          "have.attr",
          "href",
          "/data-studio/transforms/1/edit",
        );

        cy.log("sidebar should be hidden in read-only mode");
        cy.findByTestId("python-data-picker").should("not.exist");

        cy.log("results panel should be hidden in read-only mode");
        H.DataStudio.Transforms.pythonResults().should("not.exist");

        cy.log("click Edit definition to enter edit mode");
        H.DataStudio.Transforms.clickEditDefinition();
        cy.url().should("include", "/edit");

        cy.log("sidebar should be visible in edit mode");
        cy.findByTestId("python-data-picker").should("be.visible");

        cy.log("results panel should be visible in edit mode");
        H.DataStudio.Transforms.pythonResults().should("be.visible");

        cy.log("Edit definition button should be hidden in edit mode");
        H.DataStudio.Transforms.editDefinitionButton().should("not.exist");

        cy.log("update the query");
        H.PythonEditor.type("{backspace}{backspace}{backspace} + 10 }])");
        getQueryEditor().button("Save").click();
        cy.wait("@updateTransform");

        cy.log("should return to read-only mode after save");
        cy.url().should("not.include", "/edit");
        H.DataStudio.Transforms.editDefinitionButton().should("be.visible");
        cy.findByTestId("python-data-picker").should("not.exist");
        H.DataStudio.Transforms.pythonResults().should("not.exist");

        cy.log("run the transform and make sure the query has changed");
        H.DataStudio.Transforms.runTab().click();
        H.runTransformFromUiAndWaitForStatus("succeeded");
        H.DataStudio.Transforms.settingsTab().click();
        getTableLink().click();
        H.queryBuilderHeader().findByText(DB_NAME).should("be.visible");
        H.assertQueryBuilderRowCount(1);
        cy.findByTestId("scalar-value").should("have.text", "52");
      },
    );
  });

  describe("runs", () => {
    it("should be able to navigate to a list of runs", () => {
      cy.log("create and run a transform");
      createMbqlTransform({
        targetTable: TARGET_TABLE,
        visitTransform: true,
      });
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      cy.log("create and run another transform");
      createSqlTransform({
        sourceQuery: `SELECT * FROM "${TARGET_SCHEMA}"."${SOURCE_TABLE}"`,
        targetTable: TARGET_TABLE_2,
        visitTransform: true,
      });
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");

      cy.log("assert that the list is filtered by the current transform");
      getRunListLink().click();
      getTransformRunTable().within(() => {
        cy.findByText("SQL transform").should("be.visible");
        cy.findByText("MBQL transform").should("not.exist");
        cy.findByText("Success").should("be.visible");
        cy.findByText("Manual").should("be.visible");
      });
    });
  });

  describe("deletion", () => {
    it("should be able to delete a transform before and after creating the table", () => {
      cy.log("create a transform without running");
      createMbqlTransform();
      visitTransformListPage();
      getTransformsList()
        .findByText("MBQL transform")
        .should("be.visible")
        .click();

      cy.log("delete the transform");
      H.DataStudio.Transforms.header().icon("ellipsis").click();
      H.popover().findByText("Delete").click();
      H.modal().within(() => {
        cy.button("Delete transform").should("be.visible");
        cy.findByLabelText("Delete the transform only").should("not.exist");
        cy.findByLabelText("Delete the transform and the table").should(
          "not.exist",
        );
        cy.button("Delete transform").click();
        cy.wait("@deleteTransform");
      });
      getTransformsNavLink().click();
      getTransformsList().should("be.visible");
      getTransformsList().findByText("MBQL transform").should("not.exist");

      cy.log("create a transform and the table");
      createMbqlTransform({ visitTransform: true });
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");
      H.DataStudio.Transforms.settingsTab().click();
      getTableLink().should("have.attr", "aria-disabled", "false");

      cy.log("delete the transform but keep the table");
      H.DataStudio.Transforms.header().icon("ellipsis").click();
      H.popover().findByText("Delete").click();
      H.modal().within(() => {
        cy.findByLabelText("Delete the transform only").should("be.checked");
        cy.button("Delete transform only").click();
        cy.wait("@deleteTransform");
      });
      getTransformsList().should("be.visible");

      cy.log("make sure the table still exists");
      visitTableQuestion();
      H.assertQueryBuilderRowCount(3);

      cy.log("create another transform and its table");
      createMbqlTransform({
        name: "MBQL transform 2",
        targetTable: TARGET_TABLE_2,
        visitTransform: true,
      });
      H.DataStudio.Transforms.runTab().click();
      H.runTransformFromUiAndWaitForStatus("succeeded");
      H.DataStudio.Transforms.settingsTab().click();
      getTableLink().should("have.attr", "aria-disabled", "false");

      cy.log("delete the transform and the table");
      H.DataStudio.Transforms.header().icon("ellipsis").click();
      H.popover().findByText("Delete").click();
      H.modal().within(() => {
        cy.findByLabelText("Delete the transform and the table").click();
        cy.button("Delete transform and table").click();
        cy.wait("@deleteTransformTable");
        cy.wait("@deleteTransform");
      });
      getTransformsList().should("be.visible");

      cy.log("make sure the table is deleted");
      visitTableQuestion({ targetTable: TARGET_TABLE_2 });
      assertTableDoesNotExistError({ targetTable: TARGET_TABLE_2 });
    });
  });

  describe("cancelation", () => {
    function createSlowTransform(seconds: number = 100) {
      H.createTransform(
        {
          name: "Slow transform",
          source: {
            type: "query",
            query: {
              database: WRITABLE_DB_ID,
              type: "native",
              native: {
                query: `SELECT name, cast(pg_sleep(${seconds}) as text) as slow FROM "Schema A"."Animals" LIMIT 1`,
              },
            },
          },
          target: {
            type: "table",
            database: WRITABLE_DB_ID,
            name: TARGET_TABLE,
            schema: TARGET_SCHEMA,
          },
          tag_ids: [],
        },
        { visitTransform: true },
      );
    }

    it("should be possible to cancel a transform from the transform page", () => {
      createSlowTransform();
      H.DataStudio.Transforms.runTab().click();
      getRunButton().click();
      getRunButton().should("have.text", "Running now…");
      getRunStatus().should("have.text", "Run in progress…");

      getCancelButton().click();
      H.modal().button("Cancel run").click();

      getRunButton().should("have.text", "Canceling…");
      getRunStatus().should("have.text", "Canceling…");

      // We need to pass a timeout here since canceling a transform can
      // take a while on the back end
      getRunButton({ timeout: 40_000 }).should("have.text", "Canceled");
      getRunStatus().should("contain", "Last run was canceled");

      cy.log("cancel a SQL transform from the preview (metabase#64474)");
      createSlowTransform(500);
      H.DataStudio.Transforms.clickEditDefinition();
      cy.url().should("include", "/edit");
      getQueryEditor().within(() => {
        cy.findAllByTestId("run-button").eq(0).click();
        cy.findByTestId("loading-indicator").should("be.visible");

        cy.findAllByTestId("run-button").eq(0).click();
        cy.findByTestId("loading-indicator").should("not.exist");
      });
    });

    it("should be possible to cancel a transform from the runs page", () => {
      createSlowTransform();
      H.DataStudio.Transforms.runTab().click();
      getRunButton().click();
      getRunButton().should("have.text", "Running now…");
      getRunStatus().should("have.text", "Run in progress…");

      H.DataStudio.breadcrumbs().findByText("Transforms").click();
      getRunsNavLink().click();
      getDetailedViewSwitch().click();
      getTransformRunTable().findByText("In progress").click();
      cy.findByTestId("run-list-sidebar").button("Cancel run").click();
      H.modal().button("Yes").click();

      getTransformRunTable().findByText("Canceling").should("exist");
      getTransformRunTable()
        .findByText("Canceled", { timeout: 30_000 })
        .should("exist");
    });

    it("should show a message when the run finished before it cancels", () => {
      createSlowTransform(1);
      H.DataStudio.Transforms.runTab().click();
      getRunButton().click();
      getRunButton().should("have.text", "Running now…");
      getRunStatus().should("have.text", "Run in progress…");

      getCancelButton().click();
      H.modal().button("Cancel run").click();

      getRunButton().should("have.text", "Canceling…");
      getRunStatus().should("have.text", "Canceling…");

      // We need to pass a timeout here since canceling a transform can
      // take a while on the back end
      getRunButton({ timeout: 40_000 }).should("have.text", "Ran successfully");
      getRunStatus().should(
        "contain",
        "Last ran a few seconds ago successfully.",
      );
      H.DataStudio.Runs.content().should(
        "contain",
        "This run succeeded before it had a chance to cancel.",
      );
    });
  });

  describe("dependencies", () => {
    it("should render the dependency graph", () => {
      createMbqlTransform({
        name: "Transform A",
        targetTable: "table_a",
        visitTransform: true,
      }).then(() => {
        H.DataStudio.Transforms.runTab().click();
        H.runTransformFromUiAndWaitForStatus("succeeded");
      });

      createMbqlTransform({
        name: "Transform B",
        sourceTable: "table_a",
        targetTable: "table_b",
        visitTransform: true,
      }).then(() => {
        H.DataStudio.Transforms.runTab().click();
        H.runTransformFromUiAndWaitForStatus("succeeded");
      });

      createMbqlTransform({
        name: "Transform C",
        sourceTable: "table_b",
        targetTable: "table_c",
        visitTransform: true,
      });

      H.waitForBackfillComplete();
      H.DataStudio.Transforms.dependenciesTab().click();
      H.DataStudio.Dependencies.content()
        .should("contain", "Transform B")
        .and("contain", "Transform A");
      H.DataStudio.Dependencies.content().should(
        "contain",
        "Nothing uses this",
      );
    });
  });

  describe("python > common library", () => {
    it(
      "should run transforms that import the default and a custom common library",
      { tags: ["@python"] },
      () => {
        H.setPythonRunnerSettings();

        cy.log(
          "run a transform with default import common without custom library code",
        );
        visitTransformListPage();
        cy.button("Create a transform").click();
        H.popover().findByText("Python script").click();

        cy.log("import common should be included by default");
        H.PythonEditor.value().should("contain", "import common");

        cy.log(
          "write a transform that imports common but does not use it - should still run",
        );
        H.PythonEditor.clear().type(
          dedent`
            import common
            import pandas as pd

            def transform():
                return pd.DataFrame([{"result": 42}])
          `,
          { allowFastSet: true },
        );

        cy.findByTestId("python-data-picker")
          .findByRole("button", { name: "Select a table…" })
          .click();

        H.entityPickerModal().within(() => {
          cy.findByText("Schema A").click();
          cy.findByText("Animals").click();
        });

        getQueryEditor().button("Save").click();

        H.modal().within(() => {
          cy.findByLabelText("Name").clear().type("Default common transform");
          cy.findByLabelText("Table name").clear().type("default_common");
          cy.button("Save").click();
        });

        H.DataStudio.Transforms.runTab().click();
        H.runTransformFromUiAndWaitForStatus("succeeded");
        H.DataStudio.Transforms.settingsTab().click();
        getTableLink().click();
        H.queryBuilderHeader()
          .findByText("Default Common")
          .should("be.visible");
        H.assertQueryBuilderRowCount(1);
        cy.findByTestId("scalar-value").should("have.text", "42");

        cy.log("use a custom common library");
        createPythonLibrary(
          "common.py",
          dedent`
            def useful_calculation(a, b):
              return a + b
          `,
        );

        visitTransformListPage();
        cy.button("Create a transform").click();
        H.popover().findByText("Python script").click();

        cy.log("import common should be included by default");
        H.PythonEditor.value().should("contain", "import common");

        H.PythonEditor.clear().type(
          dedent`
            import common
            import pandas as pd

            def transform():
                return pd.DataFrame([{"foo": common.useful_calculation(1, 2)}])
          `,
          { allowFastSet: true },
        );

        cy.findByTestId("python-data-picker")
          .findByRole("button", { name: "Select a table…" })
          .click();

        H.entityPickerModal().within(() => {
          cy.findByText("Schema A").click();
          cy.findByText("Animals").click();
        });

        getQueryEditor().button("Save").click();

        H.modal().within(() => {
          cy.findByLabelText("Name").clear().type("Python transform");
          cy.findByLabelText("Table name").clear().type("python_transform");
          cy.button("Save").click();
        });

        H.DataStudio.Transforms.runTab().click();
        H.runTransformFromUiAndWaitForStatus("succeeded");
        H.DataStudio.Transforms.settingsTab().click();
        getTableLink().click();
        H.queryBuilderHeader()
          .findByText("Python Transform")
          .should("be.visible");
        H.assertQueryBuilderRowCount(1);
        cy.findByTestId("scalar-value").should("have.text", "3");
        H.expectUnstructuredSnowplowEvent(
          {
            event: "transform_created",
          },
          2,
        );

        cy.log("update the common library and run the transform again");
        cy.go("back");
        createPythonLibrary(
          "common.py",
          dedent`
            def useful_calculation(a, b):
              return a + b + 40
          `,
        );
        H.DataStudio.Transforms.runTab().click();
        H.runTransformFromUiAndWaitForStatus("succeeded");
        H.DataStudio.Transforms.settingsTab().click();
        getTableLink().click();
        H.queryBuilderHeader()
          .findByText("Python Transform")
          .should("be.visible");
        H.assertQueryBuilderRowCount(1);
        cy.findByTestId("scalar-value").should("have.text", "43");
      },
    );

    it(
      "should open the common library when clicking or cmd-clicking 'common' in an import statement",
      { tags: ["@python"] },
      () => {
        visitTransformListPage();
        cy.window().then((win) => {
          cy.stub(win, "open").as("windowOpen");
        });
        cy.button("Create a transform").click();
        H.popover().findByText("Python script").click();

        cy.log("cmd-click opens the library in a new tab");
        cy.get(".cm-clickable-token").should("be.visible").click(H.holdMetaKey);
        cy.get("@windowOpen").should(
          "have.been.calledWithMatch",
          "/data-studio/transforms/library/common.py",
        );

        cy.log("click navigates to the library");
        cy.get(".cm-clickable-token").click();
        H.modal().button("Discard changes").click();
        cy.url().should("include", "/data-studio/transforms/library/common.py");
        cy.findByTestId("python-library-header").should("be.visible");
      },
    );
  });

  describe("collections", () => {
    it("should create collections and save transforms to them", () => {
      cy.log("create a collection from the transforms list");
      visitTransformListPage();
      cy.button("Create a transform").click();
      H.popover().findByText("Transform folder").click();

      H.modal().within(() => {
        cy.findByLabelText("Name").type("Marketing Transforms");
        cy.button("Create").click();
      });

      getTransformsList().within(() => {
        cy.findByText("Marketing Transforms").should("be.visible");
      });

      cy.log("create a nested collection");
      cy.button("Create a transform").click();
      H.popover().findByText("Transform folder").click();

      H.modal().within(() => {
        cy.findByLabelText("Name").type("Q4 Reports");
        cy.findByTestId("collection-picker-button").click();
      });

      cy.findByRole("dialog", { name: "Select a collection" }).within(() => {
        cy.findByText("Marketing Transforms").click();
        cy.findByRole("button", { name: "Select" }).click();
      });

      H.modal().findByRole("button", { name: "Create" }).click();

      getTransformsList().within(() => {
        // The list refetches its collection tree after the create, and the
        // parent row only renders an "Expand" control once the new child is
        // present in the refetched tree. Clicking the row name to toggle it is
        // a no-op until then (the row isn't yet expandable), which left the
        // nested collection hidden. Wait for the Expand control to appear — its
        // presence is the deterministic signal the child has landed — and click
        // that instead; it only ever expands, never toggles a collapse.
        cy.findByRole("button", { name: "Expand" }).click();
        cy.findByText("Q4 Reports").should("be.visible");
      });

      cy.log("create a transform and save it to a collection");
      cy.button("Create a transform").click();
      H.popover().findByText("Query builder").click();

      H.miniPicker().within(() => {
        cy.findByText(DB_NAME).click();
        cy.findByText(TARGET_SCHEMA).click();
        cy.findByText(SOURCE_TABLE).click();
      });

      getQueryEditor().button("Save").click();

      H.modal().within(() => {
        cy.findByLabelText("Name").clear().type("Sales Summary");
        cy.findByLabelText("Table name").clear().type("sales_summary");
        cy.findByTestId("collection-picker-button").click();
      });

      cy.findByRole("dialog", { name: "Select a collection" }).within(() => {
        cy.findByText("Marketing Transforms").click();
        cy.findByRole("button", { name: "Select" }).click();
      });

      H.modal().findByRole("button", { name: "Save" }).click();
      cy.wait("@createTransform");

      cy.log("verify breadcrumbs show the collection path");
      cy.findByTestId("data-studio-breadcrumbs").within(() => {
        cy.findByText("Marketing Transforms").should("be.visible");
        cy.findByText("Sales Summary").should("be.visible");
      });

      cy.log("navigate back to list via breadcrumb");
      cy.findByTestId("data-studio-breadcrumbs").within(() => {
        cy.findByRole("link", { name: "Marketing Transforms" }).click();
      });

      cy.url().should("include", "collectionId=");
      getTransformsList().within(() => {
        cy.findByText("Sales Summary").should("be.visible");
        cy.findByText("Q4 Reports").should("be.visible");
      });
    });

    it("should move transforms between collections", () => {
      H.createTransformCollection({ name: "Target Collection" });

      createMbqlTransform({
        name: "Movable Transform",
        targetTable: "movable_transform",
        visitTransform: true,
      });

      cy.log("move transform to collection");
      H.DataStudio.Transforms.header().icon("ellipsis").click();
      H.popover().findByText("Move").click();

      H.modal().within(() => {
        cy.findByText("Target Collection").click();
        cy.findByRole("button", { name: "Select" }).click();
      });

      H.undoToast().findByText("Transform moved").should("be.visible");

      cy.log("verify breadcrumbs show collection path");
      cy.findByTestId("data-studio-breadcrumbs").within(() => {
        cy.findByText("Target Collection").should("be.visible");
        cy.findByText("Movable Transform").should("be.visible");
      });

      cy.log("move transform back to root");
      H.DataStudio.Transforms.header().icon("ellipsis").click();
      H.popover().findByText("Move").click();

      H.modal().within(() => {
        cy.findByText("Transforms").click();
        cy.findByRole("button", { name: "Select" }).click();
      });

      H.undoToastList().findByText("Transform moved").should("be.visible");

      cy.log("verify breadcrumbs no longer show collection");
      cy.findByTestId("data-studio-breadcrumbs").within(() => {
        cy.findByText("Target Collection").should("not.exist");
        cy.findByText("Movable Transform").should("be.visible");
      });
    });

    it("should support search in the transforms list", () => {
      H.createTransformCollection({ name: "Analytics" }).then((collection) => {
        createMbqlTransform({
          name: "Alpha Transform",
          targetTable: "alpha_output",
        });

        createMbqlTransform({
          name: "Beta Transform",
          targetTable: "beta_output",
          collectionId: collection.body.id,
        });
      });

      visitTransformListPage();

      cy.log("search should find transforms by name");
      cy.findByPlaceholderText("Search...").type("alpha");

      getTransformsList().within(() => {
        cy.findAllByRole("row").should("have.length", 1);
        cy.findByText("Alpha Transform").should("be.visible");
        cy.findByText("Beta Transform").should("not.exist");
      });

      cy.log("search should find transforms by output table name");
      cy.findByPlaceholderText("Search...").clear().type("beta_output");

      getTransformsList().within(() => {
        cy.findByText("Beta Transform").should("be.visible");
        cy.findByText("Alpha Transform").should("not.exist");
        cy.findByText("Analytics").should("be.visible");
      });

      cy.findByPlaceholderText("Search...").clear();
    });

    it("should create a new collection from the collection picker while saving a transform", () => {
      visitTransformListPage();
      cy.button("Create a transform").click();
      H.popover().findByText("Query builder").click();

      H.miniPicker().within(() => {
        cy.findByText(DB_NAME).click();
        cy.findByText(TARGET_SCHEMA).click();
        cy.findByText(SOURCE_TABLE).click();
      });

      getQueryEditor().button("Save").click();

      cy.log("open collection picker and create new collection inline");
      H.modal().within(() => {
        cy.findByLabelText("Name").clear().type("Analytics Transform");
        cy.findByLabelText("Table name").clear().type("analytics_transform");
        cy.findByTestId("collection-picker-button").click();
      });

      cy.findByRole("dialog", { name: "Select a collection" }).within(() => {
        cy.findByRole("button", { name: /New folder/ }).click();
      });

      cy.findByRole("dialog", { name: "Create a new collection" }).within(
        () => {
          cy.findByLabelText("Give it a name").type("Analytics");
          cy.button("Create").click();
        },
      );

      cy.findByRole("dialog", { name: "Select a collection" }).within(() => {
        cy.findByText("Analytics").should("be.visible");
        cy.findByRole("button", { name: "Select" }).click();
      });

      H.modal().findByRole("button", { name: "Save" }).click();
      cy.wait("@createTransform");

      cy.log("verify transform is in the new collection");
      cy.findByTestId("data-studio-breadcrumbs").within(() => {
        cy.findByText("Analytics").should("be.visible");
        cy.findByText("Analytics Transform").should("be.visible");
      });

      getTransformsNavLink().click();
      getTransformsList().within(() => {
        cy.findByText("Analytics").should("be.visible");
        cy.findByText("Analytics").click();
        cy.findByText("Analytics Transform").should("be.visible");
      });
    });

    it("should sort transforms by all columns", () => {
      H.createTransformCollection({ name: "Reports" }).then((collection) => {
        createMbqlTransform({
          name: "Zebra Transform",
          targetTable: "zebra_output",
        });

        createMbqlTransform({
          name: "Alpha Transform",
          targetTable: "alpha_output",
          collectionId: collection.body.id,
        });

        createMbqlTransform({
          name: "Middle Transform",
          targetTable: "middle_output",
        });
      });

      visitTransformListPage();

      cy.log("expand Reports collection to see all transforms");
      getTransformsList().findByText("Reports").click();

      cy.log("verify sorting by name column ascending");
      getTransformsList().findByText("Name").click();

      getRowNames().should("deep.equal", [
        "Middle Transform",
        "Python library",
        "Reports",
        "Alpha Transform",
        "Zebra Transform",
      ]);

      cy.log("verify sorting by name column descending");
      getTransformsList().findByText("Name").click();
      getRowNames().should("deep.equal", [
        "Zebra Transform",
        "Reports",
        "Alpha Transform",
        "Python library",
        "Middle Transform",
      ]);

      cy.log("verify sorting by output table column ascending");
      getTransformsList().findByText("Output table").click();
      getRowNames().should("deep.equal", [
        "Reports",
        "Alpha Transform",
        "Python library",
        "Middle Transform",
        "Zebra Transform",
      ]);

      cy.log("verify sorting by output table column descending");
      getTransformsList().findByText("Output table").click();
      getRowNames().should("deep.equal", [
        "Zebra Transform",
        "Middle Transform",
        "Reports",
        "Alpha Transform",
        "Python library",
      ]);
    });

    it("should edit collection details and archive a collection with transforms", () => {
      H.createTransformCollection({ name: "Original Name" });
      H.createTransformCollection({ name: "Target Parent" });
      H.createTransformCollection({ name: "Archive Me" }).then((collection) => {
        createMbqlTransform({
          name: "Transform In Collection",
          targetTable: "archived_transform_table",
          collectionId: collection.body.id,
        });
      });

      visitTransformListPage();

      cy.log("open edit modal via collection menu");
      getTransformsList()
        .findByText("Original Name")
        .closest('[role="row"]')
        .findByRole("button", { name: "Collection options" })
        .click();

      H.popover().findByText("Edit collection details").click();

      cy.log("edit name and description");
      H.modal().within(() => {
        cy.findByText("Editing Original Name").should("be.visible");
        cy.findByLabelText("Name").clear().type("Renamed Collection");
        cy.findByLabelText("Description").type("A helpful description");
        cy.findByTestId("collection-picker-button").click();
      });

      cy.log("change parent collection");
      cy.findByRole("dialog", { name: "Select a collection" }).within(() => {
        cy.findByText("Target Parent").click();
        cy.findByRole("button", { name: "Select" }).click();
      });

      H.modal().button("Save").click();

      cy.log("verify collection was renamed and moved");
      getTransformsList().within(() => {
        cy.findByText("Original Name").should("not.exist");
        cy.findByText("Target Parent").click();
        cy.findByText("Renamed Collection").should("be.visible");
      });

      cy.log("archive a collection with transforms");
      getTransformsList().within(() => {
        cy.findByText("Archive Me").should("be.visible");
        cy.findByText("Archive Me").click();
        cy.findByText("Transform In Collection").should("be.visible");
      });

      cy.log("archive the collection via menu");
      getTransformsList()
        .findByText("Archive Me")
        .closest('[role="row"]')
        .findByRole("button", { name: "Collection options" })
        .click();

      H.popover().findByText("Archive").click();

      H.modal().within(() => {
        cy.findByText('Archive "Archive Me"?').should("be.visible");
        cy.findByText("This will also archive 1 transform inside it.").should(
          "be.visible",
        );
        cy.button("Archive").click();
      });

      H.undoToast()
        .findByText('"Archive Me" has been archived')
        .should("be.visible");

      cy.log("verify collection and its children are no longer visible");
      getTransformsList().within(() => {
        cy.findByText("Archive Me").should("not.exist");
        cy.findByText("Transform In Collection").should("not.exist");
      });
    });
  });

  describe("revision history", () => {
    beforeEach(() => {
      cy.intercept("POST", "/api/revision/revert").as("revert");
      cy.intercept("GET", "/api/revision*").as("revisionHistory");
    });

    it("should be able to view and revert transform revisions", () => {
      cy.log("Create a transform with initial name");
      createMbqlTransform({
        name: "Revision Test Transform",
        visitTransform: true,
      });

      cy.log("Make changes to create a revision");
      H.DataStudio.Transforms.header()
        .findByPlaceholderText("Name")
        .clear()
        .type("Updated Transform Name")
        .blur();
      cy.wait("@updateTransform");

      cy.log(
        "Dismiss the success toast so it can't outlive the later error toast",
      );
      H.undoToast().findByText("Transform name updated").should("be.visible");
      H.DataStudio.Transforms.header()
        .findByPlaceholderText("Name")
        .should("have.value", "Updated Transform Name");
      H.undoToast().icon("close").click();
      H.undoToast().should("not.exist");

      cy.log("Make another change");
      H.DataStudio.Transforms.header()
        .findByPlaceholderText("Name")
        .clear()
        .type("Another Updated Name")
        .blur();
      cy.wait("@updateTransform");

      cy.log(
        "Dismiss the success toast so it can't outlive the later error toast",
      );
      H.undoToast().findByText("Transform name updated").should("be.visible");
      H.undoToast().icon("close").click();
      H.undoToast().should("not.exist");

      cy.log("Open revision history");
      H.DataStudio.Transforms.header().icon("ellipsis").click();
      H.popover().findByText("History").click();

      cy.wait("@revisionHistory");

      cy.log("Verify revision history sidebar is open");
      cy.findByTestId("transform-history-list").should("be.visible");

      cy.log("Verify revision entries are displayed");
      cy.findByTestId("transform-history-list")
        .findByText(/created this/)
        .should("be.visible");

      cy.log("Revert to an earlier revision");
      cy.intercept("GET", "/api/transform/*").as("transformReload");
      cy.findByTestId("transform-history-list")
        .findByText(/created this/)
        .closest('[data-testid="revision-history-event"]')
        .findByTestId("question-revert-button")
        .click();
      cy.wait(["@revert", "@transformReload"]);

      cy.log("Verify transform was reverted");
      H.DataStudio.Transforms.header()
        .findByPlaceholderText("Name")
        .should("have.value", "Revision Test Transform");

      cy.log("Verify revert entry appears in history");
      cy.findByTestId("transform-history-list")
        .findByText(/reverted to an earlier version/)
        .should("be.visible");

      cy.log("Surface backend error when a revert fails (UXW-310)");
      cy.intercept("POST", "/api/revision/revert", {
        statusCode: 500,
        body: { message: "Cannot revert: missing transform" },
      }).as("failedRevert");

      cy.findByTestId("transform-history-list")
        .findAllByTestId("question-revert-button")
        .first()
        .click();
      cy.wait("@failedRevert");

      H.undoToast().should("contain.text", "Cannot revert: missing transform");
    });
  });

  describe("read-only remote sync", () => {
    beforeEach(() => {
      cy.log("create a transform");
      createSqlTransform({
        sourceQuery: "SELECT * FROM {{ table }}",
        tableVariableTable: SOURCE_TABLE,
        tableVariableSchema: TARGET_SCHEMA,
      });
      cy.log("set up remote sync");
      H.setupGitSync();
      H.configureGit("read-only");
    });

    it("should make transform pages read-only", () => {
      cy.log("visit transforms page");
      visitTransformListPage();

      cy.log("'Create a transform' button is disabled with tooltip");
      cy.button("Create a transform").should("be.visible").and("be.disabled");
      cy.button("Create a transform").realHover();
      H.tooltip()
        .should("be.visible")
        .and(
          "have.text",
          "Transforms can't be created when Remote Sync is in read-only mode",
        );

      cy.log("clicking Python library navigates to the library editor");
      getTransformsList().findByText("Python library").click();

      cy.log("python library editor is read-only");
      cy.url().should("include", "/data-studio/transforms/library/common.py");
      cy.findByRole("alert")
        .contains(/The Python library is not editable/)
        .should("be.visible");

      H.DataStudio.PythonLibrary.editor().within(() => {
        cy.findByRole("textbox").should(
          "have.attr",
          "contenteditable",
          "false",
        );
        cy.findByRole("textbox").should("have.attr", "aria-readonly", "true");
      });

      cy.log("visit transform");
      cy.visit("/data-studio/transforms/1");

      cy.log("'edit definition' button is not displayed");
      H.DataStudio.Transforms.runTab().should("be.visible");
      H.DataStudio.Transforms.editDefinitionButton().should("not.exist");

      cy.log("visit the Run tab");
      H.DataStudio.Transforms.runTab().click();

      cy.log("schedule tags are not editable");
      cy.findByLabelText("Tags").should("be.visible");
      cy.findByLabelText("Tags").should("be.disabled");

      cy.log("visit the Settings tab");
      H.DataStudio.Transforms.settingsTab().click();

      cy.log("'Change target' button is not displayed");
      getSchemaLink().should("be.visible");
      cy.findByRole("button", { name: /Change target/ }).should("not.exist");

      cy.log("'Only process new data' switch is not displayed");
      cy.findByRole("switch", { name: /Only process new data/ }).should(
        "be.disabled",
      );

      cy.log("visiting edit mode url directly redirects to view-only mode");
      cy.visit("/data-studio/transforms/1/edit");
      cy.url().should("not.include", "/edit");

      H.DataStudio.Transforms.header()
        .findByRole("img", { name: "ellipsis icon" })
        .click();

      cy.log("ellipsis menu does not have move or delete options");
      cy.findByRole("menu").within(() => {
        cy.findByRole("menuitem", { name: /History/ }).should("be.visible");
        cy.findByRole("menuitem", { name: /Move/ }).should("not.exist");
        cy.findByRole("menuitem", { name: /Delete/ }).should("not.exist");
      });

      cy.log("visit new native transform page");
      cy.visit("/data-studio/transforms/new/native");

      cy.log("should show not found message");
      cy.findByTestId("transform-query-editor").should("be.visible");
      cy.findByTestId("transform-query-editor")
        .findByText("We're a little lost...")
        .should("be.visible");

      cy.log("visit new python transform page");
      cy.visit("/data-studio/transforms/new/python");

      cy.log("should show not found message");
      cy.findByTestId("transform-query-editor").should("be.visible");
      cy.findByTestId("transform-query-editor")
        .findByText("We're a little lost...")
        .should("be.visible");

      cy.log("visit new query transform page");
      cy.visit("/data-studio/transforms/new/query");

      cy.log("should show not found message");
      cy.findByTestId("transform-query-editor").should("be.visible");
      cy.findByTestId("transform-query-editor")
        .findByText("We're a little lost...")
        .should("be.visible");
    });
  });
});

describe("scenarios > admin > transforms > databases without :schemas", () => {
  const DB_NAME = "QA MySQL8";

  beforeEach(() => {
    H.restore("mysql-8");
    cy.signInAsAdmin();
    H.activateToken("pro-self-hosted");
    H.updateSetting("transforms-enabled", true);
  });

  it("should not be possible to create a new schema when the database does not support schemas", () => {
    cy.log("update the target of a transform");
    createMbqlTransform({
      databaseId: WRITABLE_DB_ID,
      sourceTable: "ORDERS",
      visitTransform: true,
      targetSchema: null,
    });

    H.DataStudio.Transforms.settingsTab().click();
    getTransformsTargetContent().button("Change target").click();

    H.modal().findByLabelText("New table name").should("be.visible");
    H.modal().findByLabelText("Schema").should("not.exist");

    cy.log("create a new transform");
    visitTransformListPage();
    getTransformsList().button("Create a transform").click();
    H.popover().findByText("Query builder").click();

    H.miniPicker().within(() => {
      cy.findByText(DB_NAME).click();
      cy.findByText("Orders").click();
    });
    getQueryEditor().button("Save").click();
    H.modal().findByLabelText("Table name").should("be.visible");
    H.modal().findByLabelText("Schema").should("not.exist");
  });
});

describe("scenarios > admin > transforms > jobs", () => {
  beforeEach(() => {
    H.restore("postgres-writable");
    H.resetTestTable({ type: "postgres", table: "many_schemas" });
    H.resetSnowplow();
    cy.signInAsAdmin();
    H.activateToken("pro-self-hosted");
    H.updateSetting("transforms-enabled", true);
    H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SOURCE_TABLE });

    cy.intercept("POST", "/api/transform-job").as("createJob");
    cy.intercept("PUT", "/api/transform-job/*").as("updateJob");
    cy.intercept("DELETE", "/api/transform-job/*").as("deleteJob");
  });

  describe("creation", () => {
    afterEach(() => {
      H.expectNoBadSnowplowEvents();
    });

    it("should pre-create default jobs and tags and create jobs with default and custom properties", () => {
      cy.log("built-in jobs are recognized in the cron builder");
      visitJobListPage();

      const jobNameToFrequency = {
        "Hourly job": "hourly",
        "Daily job": "daily",
        "Weekly job": "weekly",
        "Monthly job": "monthly",
      };
      Object.entries(jobNameToFrequency).forEach(([jobName, frequency]) => {
        H.DataStudio.Jobs.list().findByText(jobName).click();
        H.DataStudio.Jobs.editor().within(() => {
          getScheduleFrequencyInput().should("have.value", frequency);
        });
        cy.go("back");
      });

      cy.log("make sure that default jobs are created");
      const jobNames = ["Hourly job", "Daily job", "Weekly job", "Monthly job"];
      const tagNames = ["hourly", "daily", "weekly", "monthly"];
      H.DataStudio.Jobs.list().within(() => {
        jobNames.forEach((jobName) =>
          cy.findByText(jobName).should("be.visible"),
        );
      });

      cy.log("make sure that default tags are available for selection");
      H.DataStudio.Jobs.list().findByRole("link", { name: /New/ }).click();
      getTagsInput().click();
      H.popover().within(() => {
        tagNames.forEach((tagName) =>
          cy.findByText(tagName).should("be.visible"),
        );
      });

      cy.log("create a job with default properties");
      visitJobListPage();
      H.DataStudio.Jobs.list().findByRole("link", { name: /New/ }).click();

      H.DataStudio.Jobs.editor().button("Save").click();
      cy.wait("@createJob");

      cy.log("verify transform_job_created event was tracked");
      H.expectUnstructuredSnowplowEvent({
        event: "transform_job_created",
        triggered_from: "transform_job_new",
        result: "success",
      });

      H.undoToast().findByText("New job created").should("be.visible");
      H.undoToast().icon("close").click();
      H.undoToast().should("not.exist");

      H.DataStudio.Jobs.editor().within(() => {
        cy.findByPlaceholderText("Name").should("have.value", "New job");
        getScheduleFrequencyInput().should("have.value", "daily");
        getScheduleTimeInput().should("have.value", "12:00");
      });

      cy.log("create a job with custom property values");
      visitJobListPage();
      H.DataStudio.Jobs.list().findByRole("link", { name: /New/ }).click();

      H.DataStudio.Jobs.editor().within(() => {
        cy.findByPlaceholderText("Name").clear().type("Job");
        getScheduleFrequencyInput().click();
      });
      H.popover().findByText("custom").click();
      H.DataStudio.Jobs.editor().within(() => {
        getCronInput().clear().type("0 * * * ?");
        getTagsInput().click();
      });
      H.popover().findByText("daily").click();
      H.DataStudio.Jobs.editor().button("Save").click();
      cy.wait("@createJob");

      cy.log("verify transform_job_created event was tracked");
      H.expectUnstructuredSnowplowEvent(
        {
          event: "transform_job_created",
          triggered_from: "transform_job_new",
          result: "success",
        },
        2,
      );

      H.undoToast().findByText("New job created").should("be.visible");

      H.DataStudio.Jobs.editor().within(() => {
        cy.findByPlaceholderText("Name").should("have.value", "Job");
        getCronInput().should("have.value", "0 * * * ?");
        cy.findByText(/This job will run every hour/).should("be.visible");
        cy.findByText("daily").should("be.visible");
      });
    });
  });

  describe("schedule", () => {
    afterEach(() => {
      H.expectNoBadSnowplowEvents();
    });

    it("should be able to run a job on a schedule", () => {
      H.createTransformTag({ name: "New tag" }).then(({ body: tag }) => {
        createMbqlTransform({
          tagIds: [tag.id],
        });
        H.createTransformJob({
          name: "New job",
          schedule: "* * * * * ? *", // every second
          tag_ids: [tag.id],
        });
      });
      H.waitForSucceededTransformRuns();
      visitRunListPage();
      getTransformRunTable().within(() => {
        cy.findAllByText("MBQL transform").should("have.length.gte", 1);
        cy.findAllByText("Success").should("have.length.gte", 1);
        cy.findAllByText("Schedule").should("have.length.gte", 1);
      });

      cy.log("open detail sidebar");
      cy.findAllByText("MBQL transform").first().click();
      H.DataStudio.Runs.sidebar()
        .findByRole("img", { name: "close icon" })
        .should("be.visible");
      cy.findByRole("link", { name: "View this transform" })
        .should("be.visible")
        .should("have.attr", "href", "/data-studio/transforms/1");
      cy.findByRole("link", { name: "View in dependency graph" })
        .should("be.visible")
        .should(
          "have.attr",
          "href",
          "/data-studio/dependencies?id=1&type=transform",
        )
        .click();
      H.expectUnstructuredSnowplowEvent({
        event: "dependency_entity_selected",
        triggered_from: "transform-run-list",
        event_detail: "transform-run",
      });
    });

    it("should be able to edit the schedule, name, and tags of a job and delete it", () => {
      H.createTransformJob({ name: "New job" }, { visitTransformJob: true });

      cy.log("a job without transforms does not render the transforms table");
      H.DataStudio.Jobs.editor()
        .findByText(/There are no transforms for this job/)
        .scrollIntoView()
        .should("be.visible");
      getJobTransformTable().should("not.exist");

      cy.log("change the schedule");
      H.DataStudio.Jobs.editor().within(() => {
        getScheduleFrequencyInput().click();
      });
      H.popover().findByText("weekly").click();
      cy.wait("@updateJob");
      H.undoToast().findByText("Job schedule updated").should("be.visible");
      H.undoToast().icon("close").click();
      H.undoToast().should("not.exist");
      H.DataStudio.Jobs.editor().within(() => {
        getScheduleFrequencyInput().should("have.value", "weekly");
      });

      cy.log("change the name");
      H.DataStudio.Jobs.editor()
        .findByPlaceholderText("Name")
        .clear()
        .type("New name")
        .blur();
      cy.wait("@updateJob");
      H.undoToast().findByText("Job name updated").should("be.visible");
      H.undoToast().icon("close").click();
      H.undoToast().should("not.exist");
      H.DataStudio.Jobs.editor()
        .findByPlaceholderText("Name")
        .should("have.value", "New name");

      cy.log("add and remove tags");
      getTagsInput().click();

      H.popover().findByText("hourly").click();
      cy.wait("@updateJob");
      assertOptionSelected("hourly");
      assertOptionNotSelected("daily");

      H.popover().findByText("daily").click();
      cy.wait("@updateJob");
      assertOptionSelected("hourly");
      assertOptionSelected("daily");

      getTagsInput().type("{backspace}");
      cy.wait("@updateJob");
      assertOptionSelected("hourly");
      assertOptionNotSelected("daily");

      cy.log("delete the job");
      H.DataStudio.Jobs.header().icon("ellipsis").click();
      H.popover().findByText("Delete").click();
      H.modal().within(() => {
        cy.button("Delete job").click();
        cy.wait("@deleteJob");
      });
      H.DataStudio.Jobs.list().findByText("Hourly job").should("be.visible");
      H.DataStudio.Jobs.list().findByText("New name").should("not.exist");
    });
  });

  describe("runs", () => {
    beforeEach(() => {
      cy.intercept("GET", "/api/transform-job/*/transforms").as(
        "jobTransforms",
      );
    });

    it("should be able to manually run a job", () => {
      H.createTransformTag({ name: "New tag" }).then(({ body: tag }) => {
        createMbqlTransform({
          tagIds: [tag.id],
        });
        H.createTransformJob(
          { name: "New job", tag_ids: [tag.id] },
          { visitTransformJob: true },
        );
      });

      cy.log("the job renders its transforms table");
      cy.wait("@jobTransforms");
      H.DataStudio.Jobs.editor()
        .findByText("Transforms")
        .scrollIntoView()
        .should("be.visible");
      getJobTransformTable().findByText("MBQL transform").should("be.visible");

      H.runTransformJobFromUiAndWaitForStatus("succeeded");
      H.expectUnstructuredSnowplowEvent({
        event: "transform_job_trigger_manual_run",
      });

      H.DataStudio.Jobs.editor()
        .findByText("Last ran a few seconds ago successfully.")
        .should("be.visible");

      H.DataStudio.breadcrumbs().findByText("Jobs").click();

      getRunsNavLink().click();
      getDetailedViewSwitch().click();
      cy.log("the manual run is listed, whatever scheduled runs exist");
      getTransformRunTable()
        .findAllByRole("row")
        .filter(':contains("Manual")')
        .should("have.length", 1)
        .within(() => {
          cy.findByText("MBQL transform").should("be.visible");
          cy.findByText("Success").should("be.visible");
        });
    });

    it("should display the error message from a failed run", () => {
      H.createTransformTag({ name: "New tag" }).then(({ body: tag }) => {
        createSqlTransform({
          sourceQuery: "SELECT * FROM abc",
          tagIds: [tag.id],
        });
        H.createTransformJob(
          { name: "New job", tag_ids: [tag.id] },
          { visitTransformJob: true },
        );
      });
      cy.log("the run button stays disabled until the job transforms load");
      cy.wait("@jobTransforms");
      H.runTransformJobFromUiAndWaitForStatus("failed");
      H.DataStudio.Jobs.editor().findByText(
        "Last run failed a few seconds ago.",
      );
      getRunErrorInfoButton().click();
      H.modal().should("contain.text", 'relation "abc" does not exist');
    });
  });

  describe("active flag", () => {
    beforeEach(() => {
      cy.intercept("PUT", "/api/transform-job/active").as(
        "bulkUpdateJobActive",
      );
    });

    it("can disable and re-enable jobs from the list, the detail page, and in bulk", () => {
      H.createTransformJob({ name: "Job A" });
      H.createTransformJob({ name: "Job B" });

      visitJobListPage();

      cy.log("disable Job A from the row menu — no navigation");
      getJobRow("Job A").icon("ellipsis").click();
      H.popover().findByText("Disable").click();
      cy.wait("@updateJob")
        .its("request.body")
        .should("deep.equal", { active: false });
      H.undoToast().findByText("Job disabled").should("be.visible");
      H.undoToast().findByRole("img", { name: /close/i }).click();
      getJobRow("Job A").findByText("Disabled").should("be.visible");
      cy.location("pathname").should("eq", "/data-studio/transforms/jobs");

      cy.log("re-enable Job A from the row menu");
      getJobRow("Job A").icon("ellipsis").click();
      H.popover().findByText("Re-enable").click();
      cy.wait("@updateJob")
        .its("request.body")
        .should("deep.equal", { active: true });
      H.undoToast().findByText("Job enabled").should("be.visible");
      H.undoToast().findByRole("img", { name: /close/i }).click();
      getJobRow("Job A").findByText("Disabled").should("not.exist");

      cy.log("disable Job A from the detail page");
      getJobRow("Job A").click();
      H.DataStudio.Jobs.header().icon("ellipsis").click();
      H.popover().findByText("Disable").click();
      cy.wait("@updateJob")
        .its("request.body")
        .should("deep.equal", { active: false });
      H.undoToast().findByText("Job disabled").should("be.visible");
      H.undoToast().findByRole("img", { name: /close/i }).click();
      H.DataStudio.Jobs.editor().findByText("Disabled").should("be.visible");

      cy.log("re-enable Job A from the detail page");
      H.DataStudio.Jobs.header().icon("ellipsis").click();
      H.popover().findByText("Re-enable").click();
      cy.wait("@updateJob")
        .its("request.body")
        .should("deep.equal", { active: true });
      H.DataStudio.Jobs.editor().findByText("Disabled").should("not.exist");

      H.DataStudio.breadcrumbs().findByText("Jobs").click();

      cy.log("bulk-disable: cancel from the modal does not fire the mutation");
      openBulkActionsMenu();
      H.popover().findByText("Disable all").click();
      H.modal().button("Cancel").click();
      H.modal().should("not.exist");
      cy.get("@bulkUpdateJobActive.all").should("have.length", 0);
      getJobRow("Job A").findByText("Disabled").should("not.exist");
      getJobRow("Job B").findByText("Disabled").should("not.exist");

      cy.log(
        "bulk-disable: confirming sends { active: false } and badges all rows",
      );
      openBulkActionsMenu();
      H.popover().findByText("Disable all").click();
      H.modal().button("Disable all").click();
      cy.wait("@bulkUpdateJobActive")
        .its("request.body")
        .should("deep.equal", { active: false });
      getJobRow("Job A").findByText("Disabled").should("be.visible");
      getJobRow("Job B").findByText("Disabled").should("be.visible");

      cy.log("mixed state: bulk menu shows both items, then bulk-re-enable");
      getJobRow("Job A").icon("ellipsis").click();
      H.popover().findByText("Re-enable").click();
      cy.wait("@updateJob");
      openBulkActionsMenu();
      H.popover().within(() => {
        cy.findByText("Disable all").should("be.visible");
        cy.findByText("Re-enable all").should("be.visible").click();
      });
      cy.wait("@bulkUpdateJobActive")
        .its("request.body")
        .should("deep.equal", { active: true });
      getJobRow("Job A").findByText("Disabled").should("not.exist");
      getJobRow("Job B").findByText("Disabled").should("not.exist");
    });
  });
});

describe("scenarios > admin > transforms > runs", () => {
  beforeEach(() => {
    H.restore("postgres-writable");
    H.resetTestTable({ type: "postgres", table: "many_schemas" });
    cy.signInAsAdmin();
    H.activateToken("pro-self-hosted");
    H.updateSetting("transforms-enabled", true);
    H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SOURCE_TABLE });
  });

  it("should be able to filter and sort runs", () => {
    function createInitialData() {
      H.createTransformTag({ name: "tag1" }).then(({ body: tag1 }) => {
        H.createTransformTag({ name: "tag2" }).then(({ body: tag2 }) => {
          createMbqlTransform({
            targetTable: TARGET_TABLE,
            tagIds: [tag1.id],
            visitTransform: true,
          });
          H.DataStudio.Transforms.runTab().click();
          H.runTransformFromUiAndWaitForStatus("succeeded");
          createSqlTransform({
            sourceQuery: "SELECT * FROM abc",
            targetTable: TARGET_TABLE_2,
            tagIds: [tag2.id],
            visitTransform: true,
          });
          H.DataStudio.Transforms.runTab().click();
          H.runTransformFromUiAndWaitForStatus("failed");

          cy.log("the failed run shows its error message");
          getRunErrorInfoButton().click();
          H.modal().should("contain.text", 'relation "abc" does not exist');
          cy.realPress("Escape");
          H.modal().should("not.exist");
        });
      });
    }

    function testTransformFilter() {
      cy.log("no filters");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("transform filter - add a filter");
      getTransformFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("MBQL transform").click();
        cy.button("Add filter").click();
      });
      getTransformFilterWidget()
        .findByText("MBQL transform")
        .should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("not.exist");
      });

      cy.log("transform filter - update a filter");
      getTransformFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("MBQL transform").click();
        cy.findByText("SQL transform").click();
        cy.button("Update filter").click();
      });
      getTransformFilterWidget()
        .findByText("SQL transform")
        .should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("not.exist");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("transform filter - multiple options");
      getTransformFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("MBQL transform").click();
        cy.button("Update filter").click();
      });
      getTransformFilterWidget()
        .findByText("2 transforms")
        .should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("transform filter - remove filter");
      getTransformFilterWidget().button("Remove filter").click();
      getTransformFilterWidget().findByText("2 transforms").should("not.exist");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });
    }

    function testStatusFilter() {
      cy.log("no filters");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("status filter - add a filter");
      getStatusFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("Success").click();
        cy.button("Add filter").click();
      });
      getStatusFilterWidget().findByText("Success").should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("not.exist");
      });

      cy.log("status filter - update a filter");
      getStatusFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("Success").click();
        cy.findByText("Failed").click();
        cy.button("Update filter").click();
      });
      getStatusFilterWidget().findByText("Failed").should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("not.exist");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("status filter - multiple options");
      getStatusFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("Success").click();
        cy.button("Update filter").click();
      });
      getStatusFilterWidget().findByText("2 statuses").should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("transform filter - remove filter");
      getStatusFilterWidget().button("Remove filter").click();
      getStatusFilterWidget().findByText("2 statuses").should("not.exist");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });
    }

    function testTagFilter() {
      cy.log("no filters");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("tag filter - add a filter");
      getTagFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("tag1").click();
        cy.button("Add filter").click();
      });
      getTagFilterWidget().findByText("tag1").should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("not.exist");
      });

      cy.log("tag filter - update a filter");
      getTagFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("tag1").click();
        cy.findByText("tag2").click();
        cy.button("Update filter").click();
      });
      getTagFilterWidget().findByText("tag2").should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("not.exist");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("tag filter - multiple options");
      getTagFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("tag1").click();
        cy.button("Update filter").click();
      });
      getTagFilterWidget().findByText("2 tags").should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("tag filter - remove filter");
      getTagFilterWidget().button("Remove filter").click();
      getTagFilterWidget().findByText("2 tags").should("not.exist");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });
    }

    function testRunMethodFilter() {
      cy.log("no filters");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("run method filter - add a filter");
      getRunMethodFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("Manual").click();
        cy.button("Add filter").click();
      });
      getRunMethodFilterWidget().findByText("Manual").should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("run method filter - update a filter");
      getRunMethodFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("Manual").click();
        cy.findByText("Schedule").click();
        cy.button("Update filter").click();
      });
      getRunMethodFilterWidget().findByText("Schedule").should("be.visible");
      H.main().findByText("No runs found").should("be.visible");

      cy.log("run method filter - multiple options");
      getRunMethodFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("Manual").click();
        cy.button("Update filter").click();
      });
      getRunMethodFilterWidget()
        .findByText("Schedule, Manual")
        .should("be.visible");

      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("run method filter - remove filter");
      getRunMethodFilterWidget().button("Remove filter").click();
      getRunMethodFilterWidget()
        .findByText("Schedule, Manual")
        .should("not.exist");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });
    }

    function testStartAtFilter() {
      cy.log("no filters");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("today");
      getStartAtFilterWidget().click();
      H.popover().findByText("Today").click();
      getStartAtFilterWidget().findByText("Today").should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      getStartAtFilterWidget().button("Remove filter").click();
      getStartAtFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("Relative date range…").click();
        cy.findByLabelText("Include today").click();
        cy.button("Apply").click();
      });
      getStartAtFilterWidget()
        .findByText("Previous 30 days or today")
        .should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      getStartAtFilterWidget().button("Remove filter").click();
      getStartAtFilterWidget().click();
      H.popover().findByText("Previous week").click();
      getStartAtFilterWidget().findByText("Previous week").should("be.visible");
      H.main().findByText("No runs found").should("be.visible");

      getStartAtFilterWidget().button("Remove filter").click();
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });
    }

    function testEndAtFilter() {
      cy.log("no filters");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      cy.log("today");
      getEndAtFilterWidget().click();
      H.popover().findByText("Today").click();
      getEndAtFilterWidget().findByText("Today").should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      getEndAtFilterWidget().button("Remove filter").click();
      getEndAtFilterWidget().click();
      H.popover().within(() => {
        cy.findByText("Relative date range…").click();
        cy.findByLabelText("Include today").click();
        cy.button("Apply").click();
      });
      getEndAtFilterWidget()
        .findByText("Previous 30 days or today")
        .should("be.visible");
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });

      getEndAtFilterWidget().button("Remove filter").click();
      getEndAtFilterWidget().click();
      H.popover().findByText("Previous week").click();
      getEndAtFilterWidget().findByText("Previous week").should("be.visible");
      H.main().findByText("No runs found").should("be.visible");

      getEndAtFilterWidget().button("Remove filter").click();
      getTransformRunTable().within(() => {
        cy.findByText("MBQL transform").should("be.visible");
        cy.findByText("SQL transform").should("be.visible");
      });
    }

    function testSorting({
      columnName,
      transformNames,
    }: {
      columnName: string;
      transformNames: string[];
    }) {
      // A new intercept per click so the wait can't match an earlier request
      const clickHeaderAndWaitForRuns = () => {
        cy.intercept("GET", /\/api\/transform\/run\?/).as("listRuns");
        getTransformRunTable().findByText(columnName).click();
        cy.wait("@listRuns");
      };

      cy.log(`sort by ${columnName} ascending`);
      clickHeaderAndWaitForRuns();
      checkSortingOrder(transformNames);

      cy.log(`sort by ${columnName} descending`);
      clickHeaderAndWaitForRuns();
      checkSortingOrder([...transformNames].reverse());
    }

    createInitialData();
    H.DataStudio.breadcrumbs().findByText("Transforms").click();
    getRunsNavLink().click();
    getDetailedViewSwitch().click();
    testTransformFilter();
    testStatusFilter();
    testTagFilter();
    testRunMethodFilter();
    testStartAtFilter();
    testEndAtFilter();

    // ascending: "MBQL transform" < "SQL transform"
    testSorting({
      columnName: "Transform",
      transformNames: ["MBQL transform", "SQL transform"],
    });

    // ascending: MBQL started earlier
    testSorting({
      columnName: "Started at",
      transformNames: ["MBQL transform", "SQL transform"],
    });

    // ascending: MBQL ended earlier
    testSorting({
      columnName: "Ended at",
      transformNames: ["MBQL transform", "SQL transform"],
    });

    // ascending: "Failed" < "Success"
    testSorting({
      columnName: "Status",
      transformNames: ["SQL transform", "MBQL transform"],
    });

    // ascending: both "Manual", stable sort by id — MBQL created first
    testSorting({
      columnName: "Trigger",
      transformNames: ["MBQL transform", "SQL transform"],
    });

    // ascending: "tag1" < "tag2"
    testSorting({
      columnName: "Tags",
      transformNames: ["MBQL transform", "SQL transform"],
    });
  });
});

describe(
  "scenarios > admin > transforms > python runner",
  { tags: ["@python"] },
  () => {
    beforeEach(() => {
      H.restore("postgres-writable");
      H.resetTestTable({ type: "postgres", table: "many_schemas" });
      H.resetSnowplow();
      cy.signInAsAdmin();
      H.activateToken("pro-self-hosted");
      H.updateSetting("transforms-enabled", true);
      H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SOURCE_TABLE });

      H.setPythonRunnerSettings();
    });

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

    it("should be possible to test run a Python script", () => {
      H.getTableId({ name: "Animals", databaseId: WRITABLE_DB_ID }).then(
        (id) => {
          createPythonLibrary(
            "common.py",
            dedent`
              def useful_calculation(a, b):
                return a + b
            `,
          );

          createPythonTransform({
            body: dedent`
          import pandas as pd
          import common


          def transform(foo):
            print("Hello, world!")
            return pd.DataFrame([{"foo": common.useful_calculation(40, 2) }])
        `,
            sourceTables: pythonSourceTables("foo", id),
            visitTransform: true,
          });
        },
      );

      cy.log("enter edit mode");
      H.DataStudio.Transforms.clickEditDefinition();
      H.DataStudio.Transforms.pythonResults()
        .findByText("Done")
        .should("not.exist");
      H.DataStudio.Transforms.pythonResults()
        .findByText("Preview based on the first 100 rows from each table.")
        .should("not.exist");

      cy.log("running the script should work");
      runPythonScriptAndWaitForSuccess();
      cy.log("Preview notice should appear after the run");
      H.DataStudio.Transforms.pythonResults()
        .findByText("Done")
        .should("be.visible");
      H.DataStudio.Transforms.pythonResults()
        .findByText("Preview based on the first 100 rows from each table.")
        .should("be.visible");
      H.assertTableData({
        columns: ["foo"],
        firstRows: [["42"]],
      });

      cy.log("updating the common library should affect the results");
      createPythonLibrary(
        "common.py",
        dedent`
              def useful_calculation(a, b):
                return a + b + 1
            `,
      );

      runPythonScriptAndWaitForSuccess();
      H.assertTableData({
        columns: ["foo"],
        firstRows: [["43"]],
      });
    });
  },
);

describe("scenarios > admin > transforms", () => {
  beforeEach(() => {
    H.restore();
    H.resetSnowplow();
    cy.signInAsAdmin();
    H.activateToken("pro-self-hosted");
    H.updateSetting("transforms-enabled", true);
  });

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

  it("should show a message when no supported databases are available", () => {
    cy.log("create a new transform");
    visitTransformListPage();
    cy.findByRole("heading", {
      name: "No compatible database connection",
    }).should("exist");
    cy.findByRole("link", { name: "View your database connections" }).should(
      "exist",
    );
    H.DataStudio.Transforms.sectionHeader().should("be.visible");
    H.DataStudio.Transforms.sectionHeader()
      .findByRole("link", { name: "Transforms" })
      .should("not.exist");
    H.DataStudio.Transforms.sectionHeader()
      .findByRole("link", { name: "Jobs" })
      .should("not.exist");
    H.DataStudio.Transforms.sectionHeader()
      .findByRole("link", { name: "Runs" })
      .should("not.exist");
  });
});

function getTransformsNavLink() {
  return H.DataStudio.nav().findByRole("link", { name: "Data transformation" });
}

function getRunsNavLink() {
  return H.DataStudio.Transforms.runsTab();
}

function getTransformsList() {
  return cy.findByTestId("transforms-list");
}

function getTransformsTargetContent() {
  return cy.findByTestId("transforms-target-content");
}

function getQueryEditor() {
  return cy.findByTestId("transform-query-editor");
}

function getRunButton(options: { timeout?: number } = {}) {
  return cy.findAllByTestId("run-button").eq(0, options);
}

function getCancelButton() {
  return cy.findByTestId("cancel-button");
}

function getRunStatus() {
  return cy.findByTestId("run-status");
}

function getRunListLink() {
  return cy.findByRole("link", { name: "See all runs" });
}

function getRunErrorInfoButton() {
  return cy.findByLabelText("See error");
}

function getTableLink({ isActive = true }: { isActive?: boolean } = {}) {
  return cy
    .findByTestId("table-link")
    .should("have.attr", "aria-disabled", String(!isActive));
}

function getDatabaseLink() {
  return cy.findByTestId("database-link");
}

function getSchemaLink() {
  return cy.findByTestId("schema-link");
}

function getQueryVisualization() {
  return cy.findByTestId("query-visualization-root");
}

function getSchedulePicker() {
  return cy.findByTestId("schedule-picker");
}

function getScheduleFrequencyInput() {
  return getSchedulePicker().findByLabelText("Frequency");
}

function getScheduleTimeInput() {
  return getSchedulePicker().findByLabelText("Time");
}

function getCronInput() {
  return cy.findByPlaceholderText("For example 5 0 * Aug ?");
}

function getTagsInput() {
  return cy.findByPlaceholderText("Add tags");
}

function getTagsInputContainer() {
  return getTagsInput().parent();
}

function getFieldPicker() {
  return cy.findByLabelText("Field to check for new values");
}

function getIncrementalSwitch() {
  return cy.findByTestId("incremental-switch");
}

function isIncrementalSwitchEnabled() {
  return getIncrementalSwitch().findByRole("switch").should("be.checked");
}

function getJobTransformTable() {
  return cy.findByLabelText("Job transforms");
}

function getTransformRunTable() {
  return cy.findByLabelText("Transform runs");
}

function getTransformFilterWidget() {
  return cy.findByRole("group", { name: "Transform" });
}

function getStatusFilterWidget() {
  return cy.findByRole("group", { name: "Status" });
}

function getTagFilterWidget() {
  return cy.findByRole("group", { name: "Tags" });
}

function getRunMethodFilterWidget() {
  return cy.findByRole("group", { name: "Trigger" });
}

function getStartAtFilterWidget() {
  return cy.findByRole("group", { name: "Started at" });
}

function getEndAtFilterWidget() {
  return cy.findByRole("group", { name: "Ended at" });
}

function visitTransformListPage() {
  return cy.visit("/data-studio/transforms");
}

function visitJobListPage() {
  return cy.visit("/data-studio/transforms/jobs");
}

function getJobRow(name: string) {
  return H.DataStudio.Jobs.list()
    .findAllByRole("row")
    .filter(`:contains("${name}")`);
}

function openBulkActionsMenu() {
  cy.findByLabelText("More job options").click();
}

function visitRunListPage() {
  return cy.visit("/data-studio/transforms/runs/individual");
}

function getDetailedViewSwitch() {
  return cy.findByTestId("detailed-view-switch");
}

function createMbqlTransform(
  opts: {
    sourceTable?: string;
    sourceSchema?: string | null;
    targetTable?: string;
    targetSchema?: string | null;
    tagIds?: TransformTagId[];
    name?: string;
    databaseId?: number;
    visitTransform?: boolean;
    collectionId?: CollectionId | null;
  } = {},
) {
  return H.createMbqlTransform({
    sourceTable: SOURCE_TABLE,
    // "Animals" exists in every schema of the many_schemas fixture, so pin the
    // source schema to keep the resolved source table (and its compiled SQL)
    // deterministic. Only for the default table: custom source tables (e.g.
    // composite_pk_table, mysql ORDERS) don't live in "Schema A".
    sourceSchema: opts.sourceTable == null ? TARGET_SCHEMA : undefined,
    targetTable: TARGET_TABLE,
    targetSchema: TARGET_SCHEMA,
    name: "MBQL transform",
    ...opts,
  });
}
function createSqlTransform(opts: {
  sourceQuery: string;
  targetTable?: string;
  targetSchema?: string;
  tagIds?: TransformTagId[];
  visitTransform?: boolean;
  sourceCheckpointStrategy?: TransformSourceCheckpointStrategy;
  tableVariableTable?: string;
  tableVariableSchema?: string;
}) {
  return H.createSqlTransform({
    targetTable: TARGET_TABLE,
    targetSchema: TARGET_SCHEMA,
    ...opts,
  });
}

function createPythonTransform(opts: {
  body: string;
  sourceTables: PythonTransformTableAliases;
  targetTable?: string;
  targetSchema?: string;
  tagIds?: TransformTagId[];
  visitTransform?: boolean;
}) {
  return H.createPythonTransform({
    targetTable: TARGET_TABLE,
    targetSchema: TARGET_SCHEMA,
    ...opts,
  });
}

function pythonSourceTables(
  alias: string,
  tableId: number,
): PythonTransformTableAliases {
  return [
    {
      alias,
      table_id: tableId,
      database_id: WRITABLE_DB_ID,
      schema: TARGET_SCHEMA,
    },
  ];
}

function visitTableQuestion({
  targetTable = TARGET_TABLE,
  targetSchema = TARGET_SCHEMA,
}: { targetTable?: string; targetSchema?: string } = {}) {
  H.createNativeQuestion(
    {
      database: WRITABLE_DB_ID,
      native: {
        query: `SELECT * FROM "${targetSchema}"."${targetTable}"`,
        "template-tags": {},
      },
    },
    { visitQuestion: true },
  );
}

function assertTableDoesNotExistError({
  targetTable = TARGET_TABLE,
  targetSchema = TARGET_SCHEMA,
}: { targetTable?: string; targetSchema?: string } = {}) {
  getQueryVisualization()
    .contains(`"${targetSchema}.${targetTable}" does not exist`)
    .should("be.visible");
}

function assertOptionSelected(name: string) {
  getTagsInputContainer().findByText(name).should("be.visible");
}

function assertOptionNotSelected(name: string) {
  getTagsInputContainer().findByText(name).should("not.exist");
}

function getPythonDataPicker() {
  return cy.findByTestId("python-data-picker");
}

function createPythonLibrary(path: string, source: string) {
  cy.request("PUT", `/api/ee/transforms-python/library/${path}`, {
    source,
  });
}

function runPythonScriptAndWaitForSuccess() {
  getQueryEditor().findByTestId("run-button").click();

  getQueryEditor()
    .findByTestId("loading-indicator", { timeout: 60000 })
    .should("not.exist");

  H.DataStudio.Transforms.pythonResults().should("be.visible");
}

function getRowNames(): Cypress.Chainable<string[]> {
  return getTransformsList()
    .findAllByTestId("tree-node-name")
    .then(($rows) => $rows.get().map((row) => row.textContent.trim()));
}

describe("scenarios > data studio > transforms > permissions", () => {
  beforeEach(() => {
    H.restore("postgres-writable");
    H.resetTestTable({ type: "postgres", table: "many_schemas" });
    cy.signInAsAdmin();
    H.activateToken("pro-self-hosted");
    H.updateSetting("transforms-enabled", true);
    H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SOURCE_TABLE });

    cy.intercept("POST", "/api/transform").as("createTransform");
  });

  it("should allow non-admin users with data-studio permission to create transforms", () => {
    cy.log("grant transforms permission to the data group");
    cy.visit("/admin/permissions/application");
    cy.updatePermissionsGraph({
      [USER_GROUPS.DATA_GROUP]: {
        [WRITABLE_DB_ID]: {
          transforms: DataPermissionValue.YES,
          "view-data": DataPermissionValue.UNRESTRICTED,
          "create-queries": DataPermissionValue.QUERY_BUILDER_AND_NATIVE,
        },
      },
    });
    H.setUserAsAnalyst(NORMAL_USER_ID);

    cy.log(
      "Ensure that transform permissions are visible with the pro-self-hosted token",
    );

    cy.findByRole("tab", { name: "Data" }).click({ force: true });
    cy.findByRole("menuitem", { name: "All Users" }).click();

    cy.findByRole("columnheader", { name: /Transforms/ })
      .scrollIntoView()
      .should("be.visible");

    cy.log("sign in as normal user and create a transform");
    cy.signInAsNormalUser();
    visitTransformListPage();
    cy.button("Create a transform").click();
    H.popover().findByText("Query builder").click();

    H.miniPicker().within(() => {
      cy.findByText(DB_NAME).click();
      cy.findByText(TARGET_SCHEMA).click();
      cy.findByText(SOURCE_TABLE).click();
    });
    getQueryEditor().button("Save").click();
    H.modal().within(() => {
      cy.findByLabelText("Name").clear().type("Non-admin transform");
      cy.findByLabelText("Table name").type(TARGET_TABLE);
      cy.button("Save").click();
      cy.wait("@createTransform");
    });

    cy.log("Verify transform was created");
    getTransformsNavLink().click();
    H.DataStudio.Transforms.list()
      .findByText("Non-admin transform")
      .should("be.visible");
  });
});

function checkSortingOrder(transformNames: string[]) {
  getTransformRunTable().within(() => {
    transformNames.forEach((name, index) => {
      cy.findByText(name)
        .parents("[data-index]")
        .should("have.attr", "data-index", index.toString());
    });
  });
}

describe("scenarios > data studio > transforms > permissions > oss", () => {
  beforeEach(() => {
    H.restore("postgres-writable");
    cy.signInAsAdmin();
    // Both tests in this file that reach the enable page turn the setting on.
    // The snapshot restore alone does not always win the race against the
    // backend's settings cache, so clear it explicitly. It must be unset, not
    // false: the getter falls back to the token feature only when the setting
    // has no value, and an explicit false hides the transforms UI outright.
    H.updateSetting("transforms-enabled", null);
  });

  it(
    "should be able to enable transforms in OSS without upsell gem icon",
    { tags: ["@OSS", "@external"] },
    () => {
      cy.log("ensure that transform permissions are not shown");
      cy.visit(`/admin/permissions/data/group/${ALL_USERS_GROUP_ID}`);

      //Check that a known header is present
      cy.findByRole("columnheader", { name: "Database name" }).should(
        "be.visible",
      );
      //Ensure transform permissions are not displayed
      cy.findByRole("columnheader", { name: /Transforms/ }).should("not.exist");

      cy.log("Visit data studio page");
      H.DataStudio.visit();

      cy.log("Verify Transforms menu item is visible");
      H.DataStudio.nav()
        .findByText("Data transformation")
        .should("be.visible")
        .click();

      cy.log("Verify the upsell gem icon is displayed on a gated menu item");
      H.DataStudio.nav()
        .findByText("Remote sync")
        .closest("a")
        .findByTestId("upsell-gem")
        .should("be.visible");

      cy.log("Verify no upsell gem icon is displayed in Transforms menu item");
      H.DataStudio.nav()
        .findByText("Data transformation")
        .closest("a")
        .within(() => {
          cy.findByTestId("upsell-gem").should("not.exist");
        });

      cy.log("Verify transforms page is accessible");

      H.DataStudio.Transforms.enableTransformPage()
        .findByRole("button", { name: "Enable transforms" })
        .click();

      H.DataStudio.nav().findByText("Data transformation").click();

      H.DataStudio.Transforms.list().should("be.visible");

      cy.log("Verify can create transforms in OSS");
      cy.button("Create a transform").should("be.visible").click();

      cy.log("Verify Python transforms are not available in OSS");
      H.popover().findByText("Query builder").should("be.visible");
      H.popover()
        .findByText(/Python/i)
        .should("not.exist");

      cy.log("transform permissions should still not");
      cy.visit(`/admin/permissions/data/group/${ALL_USERS_GROUP_ID}`);

      //Check that a known header is present
      cy.findByRole("columnheader", { name: "Database name" }).should(
        "be.visible",
      );
      //Ensure transform permissions are not displayed
      cy.findByRole("columnheader", { name: /Transforms/ }).should("not.exist");
    },
  );
});

describe(
  "scenarios > data studio > transforms > permissions > pro-self-hosted",
  { tags: ["@external"] },
  () => {
    beforeEach(() => {
      H.restore("postgres-writable");
      cy.signInAsAdmin();
      // See the note in the `oss` describe above.
      H.updateSetting("transforms-enabled", null);
    });

    it("should have transforms available in self-hosted pro without upsell gem icon", () => {
      H.activateToken("pro-self-hosted").then(() => {
        cy.log("ensure that transform permissions are not shown");
        cy.visit(`/admin/permissions/data/group/${ALL_USERS_GROUP_ID}`);

        //Check that a known header is present
        cy.findByRole("columnheader", { name: "Database name" }).should(
          "be.visible",
        );
        //Ensure transform permissions are not displayed
        cy.findByRole("columnheader", { name: /Transforms/ }).should(
          "not.exist",
        );

        cy.log("Visit data studio page");
        H.DataStudio.visit();

        cy.log("Verify Transforms menu item is visible");
        H.DataStudio.nav()
          .findByText("Data transformation")
          .should("be.visible");

        cy.log(
          "Verify no upsell gem icon is displayed in Transforms menu item",
        );
        H.DataStudio.nav()
          .findByText("Data transformation")
          .closest("a")
          .within(() => {
            cy.findByTestId("upsell-gem").should("not.exist");
          });

        cy.log("Verify transforms page is accessible");
        H.DataStudio.nav().findByText("Data transformation").click();
        H.DataStudio.Transforms.enableTransformPage()
          .findByRole("button", { name: "Enable transforms" })
          .click();
        H.DataStudio.Transforms.list().should("be.visible");

        cy.log("Verify can create transforms in pro-self-hosted");
        cy.button("Create a transform").should("be.visible");

        cy.log("transform permissions should now be visible");
        cy.visit(`/admin/permissions/data/group/${ALL_USERS_GROUP_ID}`);

        //Check that a known header is present
        cy.findByRole("columnheader", { name: "Database name" }).should(
          "be.visible",
        );
        //Ensure transform permissions are displayed
        cy.findByRole("columnheader", { name: /Transforms/ })
          .scrollIntoView()
          .should("be.visible");
      });
    });
  },
);