Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
21.3 KB
Raw Blame
import { fireEvent, screen, within } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { SidebarProvider, useSidebar } from "@multica/ui/components/ui/sidebar";
import { configStore } from "@multica/core/config";
import {
  BILLING_WORKSPACE_SUBSCRIPTIONS_FLAG,
  PLUGINS_V1_FLAG,
} from "@multica/core/feature-flags";
import { renderWithI18n } from "../../test/i18n";
import { ScrollRestorationProvider } from "../../platform";

// This file tests the settings SHELL — the chrome around the tabs — so every
// tab panel is stubbed out. Their contents have their own test files. Profile
// and Messaging keep the real SettingsTab, which is what feeds the page header.
const stub = vi.hoisted(() => (name: string) => () => ({
  [name]: () => <div>{name}</div>,
}));
const tabs = vi.hoisted(() => ({ channelDetail: false }));
vi.mock("./account-tab", async () => {
  const { SettingsTab } = await import("./settings-layout");
  return {
    AccountTab: () => (
      <SettingsTab
        title="Profile"
        description="How you appear to your team."
        scope="account"
        actions={<button type="button">Sign out</button>}
      >
        <div>AccountTab</div>
      </SettingsTab>
    ),
  };
});
vi.mock("./channels-tab", async () => {
  const { SettingsTab } = await import("./settings-layout");
  return {
    ChannelsTab: () =>
      tabs.channelDetail ? (
        <SettingsTab title="Slack" scope="workspace" breadcrumb={<nav>Messaging</nav>}>
          <div>SlackDetail</div>
        </SettingsTab>
      ) : (
        <SettingsTab title="Messaging" scope="workspace">
          <div>ChannelsTab</div>
        </SettingsTab>
      ),
  };
});
vi.mock("./preferences-tab", stub("PreferencesTab"));
vi.mock("./tokens-tab", stub("TokensTab"));
vi.mock("./workspace-tab", stub("WorkspaceTab"));
vi.mock("./members-tab", stub("MembersTab"));
vi.mock("./code-tab", stub("CodeTab"));
vi.mock("./notifications-tab", stub("NotificationsTab"));
vi.mock("./labels-tab", stub("LabelsTab"));
vi.mock("../../workflows/workflows-tab", stub("WorkflowsTab"));
vi.mock("./properties-tab", stub("PropertiesTab"));
vi.mock("./quick-actions-tab", stub("QuickActionsTab"));
vi.mock("./keyboard-shortcuts-tab", stub("KeyboardShortcutsTab"));
vi.mock("./plugins-tab", stub("PluginsTab"));
vi.mock("./mcp-tab", stub("McpTab"));
vi.mock("./billing-tab", stub("BillingTab"));
// The search index reads cached notification preferences; the shell has none.
vi.mock("@tanstack/react-query", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@tanstack/react-query")>()),
  useQuery: () => ({ data: undefined }),
}));

const shell = vi.hoisted(() => ({
  appsAvailable: false,
  role: "owner" as "owner" | "admin" | "member",
}));
vi.mock("../../common/use-composio-available", () => ({
  useComposioAvailable: () => shell.appsAvailable,
}));

vi.mock("@multica/core/paths", () => ({
  useCurrentWorkspace: () => ({ id: "ws-1", name: "Acme" }),
  useWorkspacePaths: () => ({ autopilots: () => "/acme/autopilots", apps: () => "/acme/apps" }),
}));
vi.mock("@multica/core/workspace/avatar-url", () => ({
  resolvePublicFileUrl: (url: string | null | undefined) => url ?? null,
}));
vi.mock("@multica/core/permissions", () => ({
  useCurrentMember: () => ({ role: shell.role, isLoading: false }),
}));
vi.mock("@multica/core/auth", () => {
  const state = { user: { id: "user-1", name: "Ada Lovelace", avatar_url: null } };
  const useAuthStore = Object.assign(
    (selector?: (s: typeof state) => unknown) =>
      selector ? selector(state) : state,
    { getState: () => state },
  );
  return { useAuthStore };
});

const replace = vi.fn();
const push = vi.fn();
const navigationState = { search: "" };
vi.mock("../../navigation/context", async (importOriginal) => ({
  ...(await importOriginal<typeof import("../../navigation/context")>()),
  useNavigation: () => ({
    searchParams: new URLSearchParams(navigationState.search),
    hash: "",
    pathname: "/acme/settings",
    replace,
    push,
  }),
}));

// Compact by default: that is the width where the nav is a sheet and this
// trigger is the only way to reach it.
const layout = { compact: true };
vi.mock("@multica/ui/hooks/use-mobile", () => ({
  useIsMobile: () => layout.compact,
  useIsCompact: () => layout.compact,
}));

import { SettingsPage } from "./settings-page";

function NavStateProbe() {
  const { openMobile } = useSidebar();
  return <div data-testid="nav-open">{String(openMobile)}</div>;
}

function trigger() {
  return screen.getByRole("button", { name: "Toggle left sidebar" });
}

function nav() {
  return screen.getByRole("navigation", { name: "Settings" });
}

beforeEach(() => {
  layout.compact = true;
  navigationState.search = "";
  shell.appsAvailable = false;
  shell.role = "owner";
  tabs.channelDetail = false;
  configStore.getState().setFeatureFlags({});
  replace.mockClear();
  push.mockClear();
});

describe("SettingsPage nav trigger", () => {
  it("opens the nav from settings at compact widths", () => {
    // At compact widths the nav is a sheet, so without this control a touch
    // user who lands here has no way back to it at all — the keyboard
    // shortcut is not an answer on a tablet.
    renderWithI18n(
      <SidebarProvider>
        <NavStateProbe />
        <SettingsPage />
      </SidebarProvider>,
    );

    expect(screen.getByTestId("nav-open").textContent).toBe("false");

    fireEvent.click(trigger());

    expect(screen.getByTestId("nav-open").textContent).toBe("true");
  });

  it("hides the trigger only where the nav is a permanent column", () => {
    // The nav is in-flow from `lg` up, so the control is CSS-gated rather than
    // unmounted — jsdom applies no stylesheet, hence the class assertion.
    renderWithI18n(
      <SidebarProvider>
        <SettingsPage />
      </SidebarProvider>,
    );

    expect(trigger().className).toContain("lg:hidden");
  });

  it("still renders standalone, without a sidebar around it", () => {
    // Desktop mounts settings inside its own shell; the trigger has to no-op
    // rather than throw when there is no SidebarProvider above it.
    renderWithI18n(<SettingsPage />);

    expect(
      screen.queryByRole("button", { name: "Toggle left sidebar" }),
    ).not.toBeInTheDocument();
    expect(screen.getByRole("heading", { name: "Settings" })).toBeInTheDocument();
  });
});

describe("SettingsPage header", () => {
  function pageHeader() {
    return within(screen.getByRole("banner"));
  }

  it.each([
    { search: "", label: "Profile" },
    { search: "tab=preferences", label: "Preferences" },
    { search: "tab=shortcuts", label: "Shortcuts" },
    { search: "tab=members", label: "Members" },
    { search: "tab=channels", label: "Messaging" },
    { search: "tab=mcp", label: "MCP servers" },
  ])("names the $label section in the page header", ({ search, label }) => {
    navigationState.search = search;
    renderWithI18n(<SettingsPage />);

    expect(pageHeader().getByRole("heading", { level: 2 })).toHaveTextContent(label);
  });

  it("moves the section's scope and actions into the header and drops the body title", () => {
    renderWithI18n(<SettingsPage />);

    const header = pageHeader();
    expect(header.getByText("Account · synced")).toBeInTheDocument();
    expect(header.getByRole("button", { name: "Sign out" })).toBeInTheDocument();
    expect(screen.getAllByRole("heading", { name: "Profile" })).toHaveLength(1);
    expect(screen.getByText("How you appear to your team.")).toBeInTheDocument();
    expect(header.queryByText("How you appear to your team.")).toBeNull();
  });

  it("shows a detail page as a trail back to its section", () => {
    navigationState.search = "tab=channels&integration=slack";
    tabs.channelDetail = true;
    renderWithI18n(<SettingsPage />);

    const header = pageHeader();
    const title = within(header.getByRole("heading", { level: 2 }));
    expect(title.getByText("Slack")).toHaveAttribute("aria-current", "page");
    const back = title.getByRole("link", { name: "Messaging" });
    expect(back.getAttribute("href")).toContain("tab=channels");
    expect(back.getAttribute("href")).not.toContain("integration");
    expect(header.getByText("Acme workspace")).toBeInTheDocument();
    expect(screen.queryByText("Messaging", { selector: "nav" })).toBeNull();
  });
});

describe("SettingsPage scroll restoration", () => {
  // Every settings page shares the route, so one page's offset must not land
  // on another when the content remounts.
  it("gives each page its own saved offset", () => {
    const get = vi.fn((key: string) =>
      key === "settings:members:" ? { top: 360, height: 1400 } : undefined,
    );
    navigationState.search = "tab=members";
    const { rerender } = renderWithI18n(
      <ScrollRestorationProvider adapter={{ get }}>
        <SettingsPage />
      </ScrollRestorationProvider>,
    );

    const members = screen.getByText("MembersTab").closest<HTMLElement>("[data-tab-scroll-root]")!;
    expect(members.getAttribute("data-tab-scroll-root")).toBe("settings:members:");
    expect(members.scrollTop).toBe(360);

    navigationState.search = "tab=labels";
    rerender(
      <ScrollRestorationProvider adapter={{ get }}>
        <SettingsPage />
      </ScrollRestorationProvider>,
    );
    const labels = screen.getByText("LabelsTab").closest<HTMLElement>("[data-tab-scroll-root]")!;
    expect(labels.getAttribute("data-tab-scroll-root")).toBe("settings:labels:");
    expect(labels.scrollTop).toBe(0);
  });
});

describe("SettingsPage Plugin feature flag", () => {
  it("hides Plugins and falls back from a direct tab URL when disabled", () => {
    navigationState.search = "tab=plugins";

    renderWithI18n(<SettingsPage />);

    expect(
      screen.queryByRole("link", { name: "Plugins" }),
    ).not.toBeInTheDocument();
    expect(screen.queryByText("PluginsTab")).not.toBeInTheDocument();
    expect(screen.getByText("AccountTab")).toBeInTheDocument();
  });

  it("shows and mounts Plugins when explicitly enabled", () => {
    navigationState.search = "tab=plugins";
    configStore.getState().setFeatureFlags({ [PLUGINS_V1_FLAG]: true });

    renderWithI18n(<SettingsPage />);

    expect(screen.getByRole("link", { name: "Plugins" })).toBeInTheDocument();
    expect(screen.getByText("PluginsTab")).toBeInTheDocument();
  });
});

describe("SettingsPage workflows", () => {
  it("mounts workflows without feature flag configuration", () => {
    navigationState.search = "tab=workflows";

    renderWithI18n(<SettingsPage />);

    expect(screen.getByText("WorkflowsTab")).toBeInTheDocument();
    expect(screen.getByRole("link", { name: "Workflows" })).toHaveAttribute("aria-current", "page");
    expect(screen.queryByRole("link", { name: "Issue statuses" })).not.toBeInTheDocument();
  });

  it("opens old status catalog links in workflows", () => {
    navigationState.search = "tab=issue-statuses";

    renderWithI18n(<SettingsPage />);

    expect(screen.getByText("WorkflowsTab")).toBeInTheDocument();
    expect(screen.getByRole("link", { name: "Workflows" })).toHaveAttribute("aria-current", "page");
  });

  it("sends old wakeup default links to the issue wakeups list", () => {
    navigationState.search = "tab=wakeups";

    renderWithI18n(<SettingsPage />);

    expect(replace).toHaveBeenCalledWith("/acme/autopilots?tab=wakeups");
  });
});

describe("SettingsPage workspace subscription feature flag", () => {
  it("hides Billing and falls back to Workspace General from a direct URL", () => {
    navigationState.search = "tab=billing";

    renderWithI18n(<SettingsPage />);

    expect(
      screen.queryByRole("link", { name: "Billing" }),
    ).not.toBeInTheDocument();
    expect(screen.queryByText("BillingTab")).not.toBeInTheDocument();
    expect(screen.getByText("WorkspaceTab")).toBeInTheDocument();
  });

  it("shows and mounts Billing only when explicitly enabled", () => {
    navigationState.search = "tab=billing";
    configStore.getState().setFeatureFlags({
      [BILLING_WORKSPACE_SUBSCRIPTIONS_FLAG]: true,
    });

    renderWithI18n(<SettingsPage />);

    expect(screen.getByRole("link", { name: "Billing" })).toBeInTheDocument();
    expect(screen.getByText("BillingTab")).toBeInTheDocument();
  });
});

describe("SettingsPage information architecture", () => {
  it("lists pages in flat sections under a single level of headings", () => {
    renderWithI18n(<SettingsPage />);
    const sections = within(nav()).getAllByRole("region");
    expect(sections.map((section) => within(section).getByRole("heading").textContent)).toEqual([
      "Personal",
      "Workspace",
      "Issues & workflow",
      "Connections & extensions",
    ]);
    const personal = within(nav()).getByRole("region", { name: "Personal" });
    expect(within(personal).getByRole("link", { name: "Preferences" })).toBeInTheDocument();
    expect(within(personal).getByRole("link", { name: "API tokens" })).toBeInTheDocument();

    const workspace = within(nav()).getByRole("region", { name: "Workspace" });
    expect(within(workspace).getByRole("link", { name: "Members" })).toBeInTheDocument();
    const issues = within(nav()).getByRole("region", { name: "Issues & workflow" });
    expect(within(issues).getByRole("link", { name: "Workflows" })).toBeInTheDocument();
    const connections = within(nav()).getByRole("region", { name: "Connections & extensions" });
    for (const name of ["Code", "Messaging", "MCP servers"]) {
      expect(within(connections).getByRole("link", { name })).toBeInTheDocument();
    }
  });

  it("keeps retired pages out of navigation", () => {
    renderWithI18n(<SettingsPage />);
    expect(
      within(nav()).queryByRole("link", {
        name: /^(Issue|Chat|GitHub|Labs|Integrations|Repositories|Wakeups)$/,
      }),
    ).not.toBeInTheDocument();
  });

  it("opens old issue bookmarks in preferences", () => {
    navigationState.search = "tab=issue";
    renderWithI18n(<SettingsPage />);
    expect(screen.getByText("PreferencesTab")).toBeInTheDocument();
    expect(screen.getByRole("link", { name: "Preferences" })).toHaveAttribute(
      "aria-current",
      "page",
    );
  });

  it("opens the GitHub App return URL on the Code page", () => {
    navigationState.search = "tab=repositories&github_connected=1";
    renderWithI18n(<SettingsPage />);
    expect(screen.getByText("CodeTab")).toBeInTheDocument();
    expect(screen.getByRole("link", { name: "Code" })).toHaveAttribute(
      "aria-current",
      "page",
    );
  });

  it("keeps accounts out of settings, sending old links to the Apps page's Accounts", () => {
    shell.appsAvailable = true;
    const first = renderWithI18n(<SettingsPage />);
    expect(within(nav()).queryByRole("link", { name: /Accounts|Connected apps/ })).toBeNull();
    first.unmount();

    navigationState.search = "tab=accounts";
    const second = renderWithI18n(<SettingsPage />);
    expect(replace).toHaveBeenLastCalledWith("/acme/apps?tab=accounts");
    second.unmount();

    navigationState.search = "tab=connected-accounts";
    renderWithI18n(<SettingsPage />);
    expect(replace).toHaveBeenLastCalledWith("/acme/apps?tab=accounts");
  });

  it("sends the retired Apps tab and team connect returns to the Apps page", () => {
    shell.appsAvailable = true;
    navigationState.search = "tab=apps";
    const first = renderWithI18n(<SettingsPage />);
    expect(replace).toHaveBeenLastCalledWith("/acme/apps");
    first.unmount();

    navigationState.search = "tab=apps&connected=notion&scope=team";
    renderWithI18n(<SettingsPage />);
    expect(replace).toHaveBeenLastCalledWith("/acme/apps?connected=notion");
  });

  it("sends a personal connect return to the Apps page's Accounts", () => {
    navigationState.search = "tab=apps&connected=notion";
    renderWithI18n(<SettingsPage />);
    expect(replace).toHaveBeenLastCalledWith("/acme/apps?tab=accounts&connected=notion");
  });

  it("marks pages a member can only view", () => {
    shell.role = "member";
    renderWithI18n(<SettingsPage />);
    const general = screen.getByRole("link", { name: /^General/ });
    expect(
      within(general).getByLabelText("Only owners and admins can change this"),
    ).toBeInTheDocument();
    // Labels stay editable for every member, so they carry no lock.
    const labels = screen.getByRole("link", { name: "Labels" });
    expect(within(labels).queryByLabelText(/Only owners/)).toBeNull();
  });

  it("places platform settings in a device group", () => {
    renderWithI18n(
      <SettingsPage
        extraDeviceTabs={[
          {
            value: "updates",
            label: "Updates",
            icon: () => null,
            content: <div>Device updates</div>,
          },
        ]}
      />,
    );
    const device = screen.getByRole("region", { name: "This device" });
    expect(
      within(device).getByRole("link", { name: "Updates" }),
    ).toBeInTheDocument();
  });

  it("navigates from the compact selector and clears the old detail", () => {
    navigationState.search = "tab=channels&integration=slack&keep=1";
    renderWithI18n(<SettingsPage />);
    fireEvent.change(
      screen.getByRole("combobox", { name: "Go to settings page" }),
      { target: { value: "preferences" } },
    );
    expect(push).toHaveBeenCalledWith("/acme/settings?tab=preferences&keep=1");
  });
});

describe("SettingsPage search", () => {
  it("finds settings inside pages and opens the one chosen", () => {
    renderWithI18n(<SettingsPage />);
    const input = screen.getByRole("searchbox", { name: "Search settings" });

    fireEvent.change(input, { target: { value: "time zone" } });

    const results = screen.getByRole("listbox", { name: "Search settings" });
    const option = within(results).getByRole("option", { name: /Time zone/ });
    expect(option).toHaveTextContent("Personal › Preferences");

    fireEvent.click(option);
    expect(push).toHaveBeenCalledWith(
      "/acme/settings?tab=preferences&section=timezone",
    );
  });

  it("opens the highlighted result from the keyboard and clears on Escape", () => {
    renderWithI18n(<SettingsPage />);
    const input = screen.getByRole("searchbox", { name: "Search settings" });

    fireEvent.change(input, { target: { value: "slack" } });
    fireEvent.keyDown(input, { key: "Enter" });
    expect(push).toHaveBeenCalledWith(
      "/acme/settings?tab=channels&integration=slack",
    );

    fireEvent.keyDown(input, { key: "Escape" });
    expect(input).toHaveValue("");
    expect(screen.queryByRole("listbox")).toBeNull();
  });

  it("finds the issue-opening preference by the name of its options", () => {
    renderWithI18n(<SettingsPage />);
    fireEvent.change(screen.getByRole("searchbox", { name: "Search settings" }), {
      target: { value: "side preview" },
    });
    const results = screen.getByRole("listbox", { name: "Search settings" });
    const option = within(results).getByRole("option", { name: /Opening issues/ });
    expect(within(results).getAllByRole("option")).toHaveLength(1);
    fireEvent.click(option);
    expect(push).toHaveBeenCalledWith(
      "/acme/settings?tab=preferences&section=issue-opening",
    );
  });

  it("does not offer pages hidden by feature flags", () => {
    renderWithI18n(<SettingsPage />);
    fireEvent.change(
      screen.getByRole("searchbox", { name: "Search settings" }),
      { target: { value: "plugins" } },
    );
    expect(screen.getByText("No matching settings")).toBeInTheDocument();
  });
});

describe("SettingsPage search for the browser notifications row", () => {
  // The row only renders on web with the Notification API, so search must
  // follow the same rule or a result opens the page and finds nothing.
  class FakeNotification {
    static permission: NotificationPermission = "default";
  }

  afterEach(() => {
    vi.unstubAllGlobals();
  });

  function search(value: string) {
    fireEvent.change(screen.getByRole("searchbox", { name: "Search settings" }), {
      target: { value },
    });
  }

  it("offers it on web when the Notification API exists", () => {
    vi.stubGlobal("Notification", FakeNotification);
    renderWithI18n(<SettingsPage />);
    search("browser");
    const results = screen.getByRole("listbox", { name: "Search settings" });
    const option = within(results).getByRole("option", { name: /Browser notifications/ });
    fireEvent.click(option);
    expect(push).toHaveBeenCalledWith(
      "/acme/settings?tab=notifications&section=browser",
    );
  });

  it.each(["browser", "notifi"])("leaves it out on desktop when searching %s", (query) => {
    // Electron exposes Notification too; the desktop shell alone rules it out.
    vi.stubGlobal("Notification", FakeNotification);
    vi.stubGlobal("desktopAPI", { pickDirectory: vi.fn() });
    renderWithI18n(<SettingsPage />);
    search(query);
    expect(
      screen.queryByRole("option", { name: /Browser notifications/ }),
    ).not.toBeInTheDocument();
  });

  it("leaves it out where the Notification API is missing", () => {
    vi.stubGlobal("Notification", undefined);
    renderWithI18n(<SettingsPage />);
    search("browser");
    expect(
      screen.queryByRole("option", { name: /Browser notifications/ }),
    ).not.toBeInTheDocument();
  });

  it("keeps the other notification results on desktop", () => {
    vi.stubGlobal("Notification", FakeNotification);
    vi.stubGlobal("desktopAPI", { pickDirectory: vi.fn() });
    renderWithI18n(<SettingsPage />);
    search("notifi");
    const results = screen.getByRole("listbox", { name: "Search settings" });
    expect(
      within(results).getByRole("option", { name: /Show system notifications/ }),
    ).toBeInTheDocument();
  });
});