Skip to content

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

34.3 KB
Raw Blame
import userEvent from "@testing-library/user-event";

import { findRequests } from "__support__/server-mocks";
import { screen, waitFor } from "__support__/ui";
import { waitForRequestsToSettle } from "__support__/utils";
import { PLUGIN_TRANSFORMS } from "metabase/plugins";
import { createMockCollectionItemFromCollection } from "metabase-types/api/mocks";

import {
  createMockLibraryCollection,
  setup,
} from "./RemoteSyncSettingsForm.setup.spec";

describe("RemoteSyncSettingsForm", () => {
  it("should display Git settings section with URL and Token fields", async () => {
    setup();

    expect(screen.getByText("Git settings")).toBeInTheDocument();
    expect(screen.getByLabelText(/Repository URL/i)).toBeInTheDocument();
    expect(screen.getByLabelText(/Access token/i)).toBeInTheDocument();
  });

  it("should display Sync Mode section with radio options", async () => {
    setup();

    expect(screen.getByText("Sync mode for this instance")).toBeInTheDocument();
    expect(screen.getByLabelText("Read-only")).toBeInTheDocument();
    expect(screen.getByLabelText("Read-write")).toBeInTheDocument();
  });

  it("should show set up help link", async () => {
    setup();
    expect(
      screen.getByRole("link", { name: "setup guide" }),
    ).toBeInTheDocument();
  });

  describe("conditional branch section", () => {
    it("should show branch settings when read-only mode is selected", async () => {
      setup({ remoteSyncType: "read-only" });

      expect(screen.getByText("Branch to sync with")).toBeInTheDocument();
      expect(
        screen.getByRole("textbox", { name: "Sync branch" }),
      ).toBeInTheDocument();
      expect(screen.getByLabelText(/Auto-sync with git/i)).toBeInTheDocument();
    });

    it("should hide branch settings when read-write mode is selected", async () => {
      setup({ remoteSyncType: "read-write" });

      // Wait for the form to be fully rendered
      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      expect(screen.queryByText("Branch to sync with")).not.toBeInTheDocument();
    });
  });

  describe("submit behavior - read-write mode", () => {
    it("should show the 'Save changes' button", async () => {
      const libraryCollection = createMockLibraryCollection({ id: 999 });

      setup({
        remoteSyncType: "read-write",
        remoteSyncUrl: "https://github.com/test/repo.git",
        remoteSyncEnabled: true,
        libraryCollection,
      });

      // Verify the submit button shows correct text for existing setup
      expect(
        screen.getByRole("button", { name: /Save changes/i }),
      ).toBeInTheDocument();
    });
  });

  describe("submit behavior - read-only mode", () => {
    beforeEach(() => {
      setup({
        remoteSyncType: "read-only",
        remoteSyncUrl: "",
        remoteSyncEnabled: false,
      });
    });

    it("should show the 'Set up remote sync' button", async () => {
      // Verify the submit button shows correct text for new setup
      expect(
        screen.getByRole("button", { name: /Set up remote sync/i }),
      ).toBeInTheDocument();
    });

    it("should show the 'Branch to sync with' section", async () => {
      expect(
        screen.getByRole("heading", { name: "Branch to sync with" }),
      ).toBeInTheDocument();
      expect(
        screen.getByRole("switch", { name: /Auto-sync with git/ }),
      ).toBeInTheDocument();
    });
  });

  describe("cancel behavior", () => {
    it("does not show the cancel button if onCancel is not set", () => {
      setup({
        onCancel: undefined,
        remoteSyncUrl: "https://github.com/test/repo.git",
      });
      expect(
        screen.queryByRole("button", { name: "Cancel" }),
      ).not.toBeInTheDocument();
    });

    it("should close modal when Cancel is clicked", async () => {
      const onCancel = jest.fn();

      setup({
        onCancel,
        remoteSyncUrl: "https://github.com/test/repo.git",
      });

      const cancelButton = screen.getByRole("button", { name: "Cancel" });
      await userEvent.click(cancelButton);

      expect(onCancel).toHaveBeenCalled();
    });
  });

  describe("read-only mode restriction", () => {
    it("should disable read-only option when there are unsynced changes", async () => {
      const dirty = [
        { id: 1, name: "Test Card", model: "card", sync_status: "update" },
      ];

      setup({
        remoteSyncType: "read-write",
        remoteSyncEnabled: true,
        dirty,
      });

      await waitFor(() => {
        const readOnlyRadio = screen.getByLabelText("Read-only");
        expect(readOnlyRadio).toBeDisabled();
      });
    });
  });

  describe("settings-modal variant", () => {
    it("should not show set up help link", async () => {
      setup({ variant: "settings-modal" });
      expect(
        screen.queryByRole("link", { name: "setup guide" }),
      ).not.toBeInTheDocument();
    });

    it("should show 'Content to sync' section when read-write mode is selected", async () => {
      const libraryCollection = createMockLibraryCollection({ id: 999 });

      setup({
        remoteSyncType: "read-write",
        variant: "settings-modal",
        libraryCollection,
      });

      await waitFor(() => {
        expect(screen.getByText("Content to sync")).toBeInTheDocument();
      });
    });

    it("should not show 'Content to sync' section when read-only mode is selected", async () => {
      const libraryCollection = createMockLibraryCollection({ id: 999 });

      setup({
        remoteSyncType: "read-only",
        variant: "settings-modal",
        libraryCollection,
      });

      await waitFor(() => {
        expect(screen.getByLabelText("Read-only")).toBeChecked();
      });

      expect(screen.queryByText("Content to sync")).not.toBeInTheDocument();
    });

    describe("library toggle", () => {
      it("should show library toggle in modal variant when read-write mode is selected", async () => {
        const libraryCollection = createMockLibraryCollection({
          id: 999,
          name: "Library",
        });

        setup({
          remoteSyncType: "read-write",
          variant: "settings-modal",
          libraryCollection,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        expect(screen.getByLabelText("Sync Library")).toBeInTheDocument();
      });

      it("should have library toggle checked by default during first-time setup", async () => {
        const libraryCollection = createMockLibraryCollection({
          id: 999,
          name: "Library",
          is_remote_synced: false,
        });

        setup({
          remoteSyncType: "read-write",
          remoteSyncEnabled: false,
          variant: "settings-modal",
          libraryCollection,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        const libraryToggle = screen.getByLabelText("Sync Library");
        expect(libraryToggle).toBeChecked();
      });

      it("should respect existing sync state when remote sync is already enabled", async () => {
        const libraryCollection = createMockLibraryCollection({
          id: 999,
          name: "Library",
          is_remote_synced: false,
        });

        setup({
          remoteSyncType: "read-write",
          remoteSyncEnabled: true,
          remoteSyncUrl: "https://github.com/test/repo.git",
          variant: "settings-modal",
          libraryCollection,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        const libraryToggle = screen.getByLabelText("Sync Library");
        expect(libraryToggle).not.toBeChecked();
      });

      it("should allow toggling library sync state", async () => {
        const libraryCollection = createMockLibraryCollection({
          id: 999,
          name: "Library",
        });

        setup({
          remoteSyncType: "read-write",
          variant: "settings-modal",
          libraryCollection,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        const libraryToggle = screen.getByLabelText("Sync Library");
        expect(libraryToggle).toBeChecked();

        await userEvent.click(libraryToggle);
        expect(libraryToggle).not.toBeChecked();
      });
    });

    describe("library toggle when library does not exist", () => {
      it("should show library toggle even when library collection does not exist", async () => {
        setup({
          remoteSyncType: "read-write",
          variant: "settings-modal",
          libraryCollection: null,
          hasLibraryFeature: true,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        expect(screen.getByLabelText("Sync Library")).toBeInTheDocument();
        expect(screen.getByText("Library")).toBeInTheDocument();
      });

      it("should have library toggle checked by default when library does not exist", async () => {
        setup({
          remoteSyncType: "read-write",
          remoteSyncEnabled: false,
          variant: "settings-modal",
          libraryCollection: null,
          hasLibraryFeature: true,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        const libraryToggle = screen.getByLabelText("Sync Library");
        expect(libraryToggle).toBeChecked();
      });

      it("should allow toggling library sync state when library does not exist", async () => {
        setup({
          remoteSyncType: "read-write",
          variant: "settings-modal",
          libraryCollection: null,
          hasLibraryFeature: true,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        const libraryToggle = screen.getByLabelText("Sync Library");
        expect(libraryToggle).toBeChecked();

        await userEvent.click(libraryToggle);
        expect(libraryToggle).not.toBeChecked();
      });
      it("should show library toggle in admin settings when library does not exist", async () => {
        setup({
          remoteSyncType: "read-write",
          libraryCollection: null,
          hasLibraryFeature: true,
        });

        expect(
          await screen.findByLabelText("Sync Library"),
        ).toBeInTheDocument();
      });

      it("should check existing library by default in admin settings during first-time setup", async () => {
        setup({
          remoteSyncType: "read-write",
          remoteSyncEnabled: false,
          libraryCollection: createMockLibraryCollection({
            is_remote_synced: false,
          }),
          hasLibraryFeature: true,
        });

        expect(await screen.findByLabelText("Sync Library")).toBeChecked();
      });

      it("should check transforms by default in admin settings during first-time setup", async () => {
        PLUGIN_TRANSFORMS.isEnabled = true;
        setup({
          remoteSyncType: "read-write",
          remoteSyncEnabled: false,
          libraryCollection: null,
          hasLibraryFeature: true,
        });

        expect(await screen.findByLabelText("Sync Transforms")).toBeChecked();
        PLUGIN_TRANSFORMS.isEnabled = false;
      });

      it("should neither create library nor change transforms sync when setting up read-only sync", async () => {
        PLUGIN_TRANSFORMS.isEnabled = true;
        setup({
          remoteSyncType: "read-only",
          remoteSyncEnabled: false,
          libraryCollection: null,
          hasLibraryFeature: true,
        });

        await userEvent.type(
          screen.getByLabelText(/Repository URL/i),
          "https://github.com/foo/bar.git",
        );
        await userEvent.click(
          screen.getByRole("button", { name: /Set up remote sync/i }),
        );

        const puts = await findRequests("PUT");
        const settingsRequest = puts.find((request) =>
          request.url.includes("/api/ee/remote-sync/settings"),
        );
        expect(settingsRequest?.body).not.toHaveProperty(
          "remote-sync-transforms",
        );
        const posts = await findRequests("POST");
        expect(
          posts.filter((request) => request.url.includes("/api/ee/library")),
        ).toHaveLength(0);
        PLUGIN_TRANSFORMS.isEnabled = false;
      });

      it("should not show library toggle without library feature", async () => {
        setup({
          remoteSyncType: "read-write",
          variant: "settings-modal",
          libraryCollection: null,
          hasLibraryFeature: false,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        expect(screen.queryByLabelText("Sync Library")).not.toBeInTheDocument();
      });

      it("should create library and sync it when saving admin settings", async () => {
        setup({
          remoteSyncType: "read-write",
          remoteSyncEnabled: false,
          libraryCollection: null,
          hasLibraryFeature: true,
          createdLibraryCollection: createMockLibraryCollection({ id: 555 }),
        });

        expect(await screen.findByLabelText("Sync Library")).toBeChecked();
        await userEvent.type(
          screen.getByLabelText(/Repository URL/i),
          "https://github.com/foo/bar.git",
        );
        await userEvent.click(
          screen.getByRole("button", { name: /Set up remote sync/i }),
        );

        const posts = await findRequests("POST");
        expect(
          posts.filter((request) => request.url.includes("/api/ee/library")),
        ).toHaveLength(1);
        const puts = await findRequests("PUT");
        const settingsRequest = puts.find((request) =>
          request.url.includes("/api/ee/remote-sync/settings"),
        );
        expect(settingsRequest?.body).toMatchObject({
          collections: { 555: true },
        });
      });
    });

    describe("transforms toggle", () => {
      afterEach(() => {
        PLUGIN_TRANSFORMS.isEnabled = false;
      });

      it("should show transforms toggle in modal variant when feature is enabled", async () => {
        PLUGIN_TRANSFORMS.isEnabled = true;
        const libraryCollection = createMockLibraryCollection({ id: 999 });

        setup({
          remoteSyncType: "read-write",
          variant: "settings-modal",
          libraryCollection,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        expect(screen.getByLabelText("Sync Transforms")).toBeInTheDocument();
      });

      it("should not show transforms toggle in modal variant when feature is disabled", async () => {
        PLUGIN_TRANSFORMS.isEnabled = false;
        const libraryCollection = createMockLibraryCollection({ id: 999 });

        setup({
          remoteSyncType: "read-write",
          variant: "settings-modal",
          libraryCollection,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        expect(
          screen.queryByLabelText("Sync Transforms"),
        ).not.toBeInTheDocument();
      });

      it("should have transforms toggle checked by default during first-time setup when feature is enabled", async () => {
        PLUGIN_TRANSFORMS.isEnabled = true;
        const libraryCollection = createMockLibraryCollection({ id: 999 });

        setup({
          remoteSyncType: "read-write",
          remoteSyncEnabled: false,
          remoteSyncTransforms: false,
          variant: "settings-modal",
          libraryCollection,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        const transformsToggle = screen.getByLabelText("Sync Transforms");
        expect(transformsToggle).toBeChecked();
      });

      it("should respect existing sync state when remote sync is already enabled", async () => {
        PLUGIN_TRANSFORMS.isEnabled = true;
        const libraryCollection = createMockLibraryCollection({ id: 999 });

        setup({
          remoteSyncType: "read-write",
          remoteSyncEnabled: true,
          remoteSyncUrl: "https://github.com/test/repo.git",
          remoteSyncTransforms: false,
          variant: "settings-modal",
          libraryCollection,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        const transformsToggle = screen.getByLabelText("Sync Transforms");
        expect(transformsToggle).not.toBeChecked();
      });

      it("should allow toggling transforms sync state", async () => {
        PLUGIN_TRANSFORMS.isEnabled = true;
        const libraryCollection = createMockLibraryCollection({ id: 999 });

        setup({
          remoteSyncType: "read-write",
          variant: "settings-modal",
          libraryCollection,
        });

        await waitFor(() => {
          expect(screen.getByText("Content to sync")).toBeInTheDocument();
        });

        const transformsToggle = screen.getByLabelText("Sync Transforms");
        expect(transformsToggle).toBeChecked();

        await userEvent.click(transformsToggle);
        expect(transformsToggle).not.toBeChecked();
      });
    });
  });

  describe("transforms sync toggle", () => {
    afterEach(() => {
      PLUGIN_TRANSFORMS.isEnabled = false;
    });

    it("should not display transforms toggle when transforms feature is disabled", async () => {
      setup({
        remoteSyncEnabled: true,
        remoteSyncType: "read-write",
        remoteSyncUrl: "https://github.com/test/repo.git",
      });

      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      // The branch dropdown only starts loading once the read-write form is on
      // screen, so its request has to be flushed after that wait, not before.
      await waitForRequestsToSettle();

      expect(
        screen.queryByLabelText("Sync Transforms"),
      ).not.toBeInTheDocument();
    });

    it("should display transforms row in collections list when transforms feature is enabled", async () => {
      PLUGIN_TRANSFORMS.isEnabled = true;

      setup({
        remoteSyncEnabled: true,
        remoteSyncType: "read-write",
        remoteSyncUrl: "https://github.com/test/repo.git",
      });

      // Wait for the form to be fully rendered with read-write mode
      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      expect(screen.getByLabelText("Sync Transforms")).toBeInTheDocument();
    });

    it("should display transforms toggle in modal variant 'Content to sync' section when feature is enabled", async () => {
      PLUGIN_TRANSFORMS.isEnabled = true;
      const libraryCollection = createMockLibraryCollection({ id: 999 });

      setup({
        remoteSyncEnabled: true,
        remoteSyncType: "read-write",
        remoteSyncUrl: "https://github.com/test/repo.git",
        variant: "settings-modal",
        libraryCollection,
      });

      // Wait for the form to be fully rendered with read-write mode
      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      // In modal variant, transforms toggle is shown in "Content to sync" section
      expect(screen.getByText("Content to sync")).toBeInTheDocument();
      expect(screen.getByLabelText("Sync Transforms")).toBeInTheDocument();
    });
  });

  describe("URL validation", () => {
    it("should show a validation error for non-HTTPS URLs", async () => {
      setup({
        remoteSyncType: "read-only",
        remoteSyncUrl: "",
        remoteSyncEnabled: false,
      });

      const urlInput = screen.getByLabelText(/Repository URL/i);
      await userEvent.type(urlInput, "git://github.com/foo/bar.git");

      // Tab away to trigger validation
      await userEvent.tab();

      await waitFor(() => {
        expect(
          screen.getByText(/Only HTTPS URLs are supported/),
        ).toBeInTheDocument();
      });
    });

    it("should not show a validation error for HTTPS URLs", async () => {
      setup({
        remoteSyncType: "read-only",
        remoteSyncUrl: "",
        remoteSyncEnabled: false,
      });

      const urlInput = screen.getByLabelText(/Repository URL/i);
      await userEvent.type(urlInput, "https://github.com/foo/bar.git");

      // Tab away to trigger validation
      await userEvent.tab();

      // Give validation time to run, then verify no error
      await waitFor(() => {
        expect(
          screen.queryByText(/Only HTTPS URLs are supported/),
        ).not.toBeInTheDocument();
      });
    });
  });

  describe("test connection button", () => {
    it("should be disabled when no URL is provided", async () => {
      setup({
        remoteSyncType: "read-only",
        remoteSyncUrl: "",
        remoteSyncEnabled: false,
      });

      expect(
        screen.getByRole("button", { name: /Test connection/i }),
      ).toBeDisabled();
    });

    it("should be enabled when a URL is provided", async () => {
      setup({
        remoteSyncType: "read-only",
        remoteSyncUrl: "https://github.com/test/repo.git",
        remoteSyncEnabled: true,
      });

      await waitFor(() => {
        expect(
          screen.getByRole("button", { name: /Test connection/i }),
        ).toBeEnabled();
      });
    });

    it("should POST only the URL and token to the test-connection endpoint", async () => {
      setup({
        remoteSyncType: "read-only",
        remoteSyncUrl: "https://github.com/test/repo.git",
        remoteSyncToken: "ghp_abc123",
        remoteSyncBranch: "main",
        remoteSyncEnabled: true,
      });

      const testButton = await screen.findByRole("button", {
        name: /Test connection/i,
      });
      await waitFor(() => expect(testButton).toBeEnabled());
      await userEvent.click(testButton);

      await waitFor(async () => {
        const posts = await findRequests("POST");
        const testRequest = posts.find((r) =>
          r.url.includes("/api/ee/remote-sync/test-connection"),
        );
        expect(testRequest).toBeDefined();
        expect(testRequest?.body).toEqual({
          "remote-sync-url": "https://github.com/test/repo.git",
          "remote-sync-token": "ghp_abc123",
        });
      });
    });

    it("should show the connection error in the failure icon's tooltip", async () => {
      setup({
        remoteSyncType: "read-only",
        remoteSyncUrl: "https://github.com/test/repo.git",
        remoteSyncToken: "ghp_bad",
        remoteSyncEnabled: true,
        testConnectionError: {
          status: 400,
          message: "Authentication failed: Please check your git credentials",
        },
      });

      const testButton = await screen.findByRole("button", {
        name: /Test connection/i,
      });
      await waitFor(() => expect(testButton).toBeEnabled());
      await userEvent.click(testButton);

      await waitFor(async () => {
        const posts = await findRequests("POST");
        const testRequest = posts.find((r) =>
          r.url.includes("/api/ee/remote-sync/test-connection"),
        );
        expect(testRequest).toBeDefined();
      });

      await userEvent.hover(await screen.findByLabelText("Connection failed"));
      expect(await screen.findByRole("tooltip")).toHaveTextContent(
        "Authentication failed: Please check your git credentials",
      );
      expect(screen.getByLabelText(/Repository URL/i)).toHaveValue(
        "https://github.com/test/repo.git",
      );
    });
  });

  describe("save error handling", () => {
    it("should show backend error message in toast when save fails", async () => {
      setup({
        remoteSyncType: "read-only",
        remoteSyncUrl: "",
        remoteSyncEnabled: false,
        settingsError: { status: 400, message: "Invalid branch name" },
      });

      const urlInput = screen.getByLabelText(/Repository URL/i);
      await userEvent.type(urlInput, "https://github.com/foo/bar.git");

      const submitButton = screen.getByRole("button", {
        name: /Set up remote sync/i,
      });
      await userEvent.click(submitButton);

      await waitFor(() => {
        expect(screen.getByText(/Invalid branch name/)).toBeInTheDocument();
      });
    });
  });

  describe("collection change filtering", () => {
    it("should not send collections in request body when no collection sync states changed", async () => {
      const syncedCollection = createMockCollectionItemFromCollection({
        id: 10,
        name: "Synced Collection",
        is_remote_synced: true,
        can_write: true,
      });
      const unsyncedCollection = createMockCollectionItemFromCollection({
        id: 20,
        name: "Unsynced Collection",
        is_remote_synced: false,
        can_write: true,
      });

      setup({
        remoteSyncType: "read-write",
        remoteSyncUrl: "https://github.com/test/repo.git",
        remoteSyncEnabled: true,
        rootCollectionItems: [syncedCollection, unsyncedCollection],
      });

      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      // Change the URL to make the form dirty (but don't change collections)
      const urlInput = screen.getByLabelText(/Repository URL/i);
      await userEvent.clear(urlInput);
      await userEvent.type(urlInput, "https://github.com/test/other-repo.git");

      const submitButton = screen.getByRole("button", {
        name: /Save changes/i,
      });
      await userEvent.click(submitButton);

      const puts = await findRequests("PUT");
      const settingsRequest = puts.find((r) =>
        r.url.includes("/api/ee/remote-sync/settings"),
      );
      expect(settingsRequest).toBeDefined();
      expect(settingsRequest?.body).not.toHaveProperty("collections");
    });

    it("should only send changed collections in request body", async () => {
      const syncedCollection = createMockCollectionItemFromCollection({
        id: 10,
        name: "Synced Collection",
        is_remote_synced: true,
        can_write: true,
      });
      const unsyncedCollection = createMockCollectionItemFromCollection({
        id: 20,
        name: "Unsynced Collection",
        is_remote_synced: false,
        can_write: true,
      });

      setup({
        remoteSyncType: "read-write",
        remoteSyncUrl: "https://github.com/test/repo.git",
        remoteSyncEnabled: true,
        rootCollectionItems: [syncedCollection, unsyncedCollection],
      });

      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      // Toggle the unsynced collection to synced
      const unsyncedToggle = screen.getByLabelText("Sync Unsynced Collection");
      await userEvent.click(unsyncedToggle);

      const submitButton = screen.getByRole("button", {
        name: /Save changes/i,
      });
      await userEvent.click(submitButton);

      // Should only include collection 20 (changed), not collection 10 (unchanged)
      const puts = await findRequests("PUT");
      const settingsRequest = puts.find((r) =>
        r.url.includes("/api/ee/remote-sync/settings"),
      );
      expect(settingsRequest).toBeDefined();
      expect(settingsRequest?.body).toHaveProperty("collections", {
        "20": true,
      });
    });
  });

  describe("environment variable overrides", () => {
    it("should disable both sync mode radio options and show env var description when remote-sync-type is set by env", async () => {
      setup({
        remoteSyncType: "read-write",
        envSettings: ["remote-sync-type"],
      });

      await waitFor(() => {
        expect(screen.getByLabelText("Read-only")).toBeDisabled();
      });
      expect(screen.getByLabelText("Read-write")).toBeDisabled();
      expect(screen.getByText("Using MB_REMOTE_SYNC_TYPE")).toBeInTheDocument();
    });

    it("should not disable sync mode radio options when remote-sync-type is not set by env", async () => {
      setup({
        remoteSyncType: "read-write",
      });

      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      expect(screen.getByLabelText("Read-only")).toBeEnabled();
      expect(screen.getByLabelText("Read-write")).toBeEnabled();
    });

    it("should hide the disable button when remote-sync-enabled is set by env", async () => {
      setup({
        remoteSyncEnabled: true,
        remoteSyncUrl: "https://github.com/test/repo.git",
        remoteSyncType: "read-write",
        envSettings: ["remote-sync-enabled"],
      });

      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      expect(
        screen.queryByRole("button", { name: /Disable remote sync/i }),
      ).not.toBeInTheDocument();
    });

    it("should make URL and token fields read-only when set by env", async () => {
      setup({
        remoteSyncUrl: "https://github.com/test/repo.git",
        remoteSyncToken: "ghp_abc123",
        envSettings: ["remote-sync-url", "remote-sync-token"],
      });

      await waitFor(() => {
        expect(
          screen.getByText("Using MB_REMOTE_SYNC_URL"),
        ).toBeInTheDocument();
      });

      expect(screen.getByLabelText(/Repository URL/i)).toHaveAttribute(
        "readonly",
      );
      expect(
        screen.getByText("Using MB_REMOTE_SYNC_TOKEN"),
      ).toBeInTheDocument();
    });

    it("should make branch field read-only when remote-sync-branch is set by env", async () => {
      setup({
        remoteSyncType: "read-only",
        remoteSyncBranch: "main",
        envSettings: ["remote-sync-branch"],
      });

      await waitFor(() => {
        expect(
          screen.getByText("Using MB_REMOTE_SYNC_BRANCH"),
        ).toBeInTheDocument();
      });

      expect(
        screen.getByRole("textbox", { name: "Sync branch" }),
      ).toHaveAttribute("readonly");
    });
  });

  describe("DevInstanceUpsell", () => {
    it("should show the upsell when not a dev instance and upsell has not been dismissed", async () => {
      setup({ isDevInstance: false, upsellDismissed: false });

      await waitFor(() => {
        expect(
          screen.getByText("Need a dedicated development environment?"),
        ).toBeInTheDocument();
      });
    });

    it("should not show the upsell when it has been dismissed", async () => {
      setup({ isDevInstance: false, upsellDismissed: true });

      await waitFor(() => {
        expect(screen.getByText("Git settings")).toBeInTheDocument();
      });

      expect(
        screen.queryByText("Need a dedicated development environment?"),
      ).not.toBeInTheDocument();
    });

    it("should not show the upsell when it is a dev instance", async () => {
      setup({ isDevInstance: true, upsellDismissed: false });

      await waitFor(() => {
        expect(screen.getByText("Git settings")).toBeInTheDocument();
      });

      expect(
        screen.queryByText("Need a dedicated development environment?"),
      ).not.toBeInTheDocument();
    });
  });

  describe("collections to sync section", () => {
    it("should display collections section in admin variant when read-write mode is selected during initial setup", async () => {
      setup({
        remoteSyncEnabled: false,
        remoteSyncType: "read-write",
      });

      // Wait for the form to be fully rendered with read-write mode
      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      expect(screen.getByText("Collections to sync")).toBeInTheDocument();
    });

    it("should not display collections section in admin variant when remote-sync-custom-collections-enabled is off", async () => {
      setup({
        remoteSyncEnabled: false,
        remoteSyncType: "read-write",
        remoteSyncCustomCollectionsEnabled: false,
      });

      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      expect(screen.queryByText("Collections to sync")).not.toBeInTheDocument();
    });

    it("should create and sync library and sync transforms by default when collections section is hidden", async () => {
      PLUGIN_TRANSFORMS.isEnabled = true;
      setup({
        remoteSyncEnabled: false,
        remoteSyncType: "read-write",
        remoteSyncCustomCollectionsEnabled: false,
        libraryCollection: null,
        hasLibraryFeature: true,
        createdLibraryCollection: createMockLibraryCollection({ id: 555 }),
      });

      await userEvent.type(
        screen.getByLabelText(/Repository URL/i),
        "https://github.com/foo/bar.git",
      );
      await userEvent.click(
        screen.getByRole("button", { name: /Set up remote sync/i }),
      );

      const posts = await findRequests("POST");
      expect(
        posts.filter((request) => request.url.includes("/api/ee/library")),
      ).toHaveLength(1);
      const puts = await findRequests("PUT");
      const settingsRequest = puts.find((request) =>
        request.url.includes("/api/ee/remote-sync/settings"),
      );
      expect(settingsRequest?.body).toMatchObject({
        "remote-sync-transforms": true,
        collections: { 555: true },
      });
      PLUGIN_TRANSFORMS.isEnabled = false;
    });

    it("should not display collections section in modal variant when read-write mode is selected", async () => {
      setup({
        remoteSyncEnabled: false,
        remoteSyncType: "read-write",
        variant: "settings-modal",
      });

      // Wait for the form to be fully rendered with read-write mode
      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      // Collections section (which includes transforms row) should not appear in modal variant
      expect(screen.queryByText("Collections to sync")).not.toBeInTheDocument();
    });

    it("should not display collections section in modal variant even when remote sync is enabled", async () => {
      setup({
        remoteSyncEnabled: true,
        remoteSyncType: "read-write",
        remoteSyncUrl: "https://github.com/test/repo.git",
        variant: "settings-modal",
      });

      // Wait for the form to be fully rendered with read-write mode
      await waitFor(() => {
        expect(screen.getByLabelText("Read-write")).toBeChecked();
      });

      // Collections section (which includes transforms row) should not appear in modal variant
      expect(screen.queryByText("Collections to sync")).not.toBeInTheDocument();
    });
  });
});