import { cleanup, fireEvent, render, screen } from "@testing-library/react"; import type { ReactNode } from "react"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const MAIN_WORKSPACE_ID = "ws-1"; const ARCHIVE_WORKSPACE_ID = "ws-10"; const MAIN_WORKSPACE_NAME = "Main Workspace"; const ARCHIVE_WORKSPACE_NAME = "Archive Workspace"; const state = { workspaces: { activeId: MAIN_WORKSPACE_ID, items: [{ id: MAIN_WORKSPACE_ID, name: MAIN_WORKSPACE_NAME }], }, setActiveWorkspace: vi.fn(), settingsAgents: { items: [], }, executors: { items: [], }, features: { office: false, plugins: false, }, }; const integrationAvailability = vi.hoisted(() => ({ azureDevOps: true, github: false, gitlab: false, jira: false, linear: false, sentry: false, slack: false, })); vi.mock("@/components/state-provider", () => ({ useAppStore: (selector: (s: typeof state) => unknown) => selector(state), })); vi.mock("@/hooks/domains/settings/use-available-agents", () => ({ useAvailableAgents: () => undefined, })); vi.mock("@/hooks/domains/azure-devops/use-azure-devops-availability", () => ({ useAzureDevOpsAvailable: () => integrationAvailability.azureDevOps, })); vi.mock("@/hooks/domains/github/use-github-status", () => ({ useGitHubStatus: () => ({ status: integrationAvailability.github ? { authenticated: true } : null, loading: false, }), })); vi.mock("@/hooks/domains/gitlab/use-task-mr", () => ({ useGitLabAvailable: () => integrationAvailability.gitlab, })); vi.mock("@/hooks/domains/jira/use-jira-availability", () => ({ useJiraAuthed: () => integrationAvailability.jira, })); vi.mock("@/hooks/domains/linear/use-linear-availability", () => ({ useLinearAuthed: () => integrationAvailability.linear, })); vi.mock("@/hooks/domains/sentry/use-sentry-availability", () => ({ useSentryAvailable: () => integrationAvailability.sentry, })); vi.mock("@/hooks/domains/slack/use-slack-availability", () => ({ useSlackAuthed: () => integrationAvailability.slack, })); vi.mock("@kandev/ui/collapsible", async () => { const React = await vi.importActual("react"); const CollapsibleContext = React.createContext(false); return { Collapsible: ({ open, children }: { open?: boolean; children: ReactNode }) => React.createElement(CollapsibleContext.Provider, { value: Boolean(open) }, children), CollapsibleContent: ({ children, className }: { children: ReactNode; className?: string }) => { const open = React.useContext(CollapsibleContext); return open ? React.createElement("div", { className }, children) : null; }, }; }); import { SettingsTree } from "./settings-tree"; import { WorkspacesGroup } from "./workspaces-group"; describe("SettingsTree rendering", () => { beforeEach(() => { state.workspaces.activeId = MAIN_WORKSPACE_ID; state.workspaces.items = [{ id: MAIN_WORKSPACE_ID, name: MAIN_WORKSPACE_NAME }]; state.setActiveWorkspace.mockClear(); state.settingsAgents.items = []; state.executors.items = []; integrationAvailability.azureDevOps = true; integrationAvailability.github = false; integrationAvailability.gitlab = false; integrationAvailability.jira = false; integrationAvailability.linear = false; integrationAvailability.sentry = false; integrationAvailability.slack = false; }); afterEach(() => cleanup()); it("renders workspace repository and workflow links when Workspaces is open", () => { render(); expect(screen.getByRole("link", { name: "Repositories" }).getAttribute("href")).toBe( "/settings/workspace/ws-1/repositories", ); expect(screen.getByRole("link", { name: "Workflows" }).getAttribute("href")).toBe( "/settings/workspace/ws-1/workflows", ); expect(screen.getByRole("link", { name: "Automations" }).getAttribute("href")).toBe( "/settings/workspace/ws-1/automations", ); expect(screen.getByRole("link", { name: "Integrations" }).getAttribute("href")).toBe( "/settings/workspace/ws-1/integrations", ); }); it("opens the active workspace by default when the settings tree opens", () => { state.workspaces.items = [ { id: MAIN_WORKSPACE_ID, name: MAIN_WORKSPACE_NAME }, { id: ARCHIVE_WORKSPACE_ID, name: ARCHIVE_WORKSPACE_NAME }, ]; render(); expect(screen.getByRole("link", { name: `${MAIN_WORKSPACE_NAME} Active` })).toBeTruthy(); expect(screen.getByRole("link", { name: ARCHIVE_WORKSPACE_NAME })).toBeTruthy(); expect(screen.queryByText("[active]")).toBeNull(); expect(screen.getByRole("link", { name: "Repositories" }).getAttribute("href")).toBe( "/settings/workspace/ws-1/repositories", ); expect(screen.getByRole("link", { name: "Automations" }).getAttribute("href")).toBe( "/settings/workspace/ws-1/automations", ); }); it("uses an accordion for workspace subsections", () => { state.workspaces.items = [ { id: MAIN_WORKSPACE_ID, name: MAIN_WORKSPACE_NAME }, { id: ARCHIVE_WORKSPACE_ID, name: ARCHIVE_WORKSPACE_NAME }, ]; render(); expect(screen.getByRole("link", { name: "Repositories" }).getAttribute("href")).toBe( "/settings/workspace/ws-1/repositories", ); fireEvent.click(screen.getByRole("button", { name: "Expand Archive Workspace" })); expect(screen.getByRole("button", { name: "Expand Main Workspace" })).toBeTruthy(); expect(screen.getByRole("link", { name: "Repositories" }).getAttribute("href")).toBe( "/settings/workspace/ws-10/repositories", ); }); it("only opens the routed workspace subsection on workspace detail routes", () => { state.workspaces.items = [ { id: MAIN_WORKSPACE_ID, name: MAIN_WORKSPACE_NAME }, { id: ARCHIVE_WORKSPACE_ID, name: ARCHIVE_WORKSPACE_NAME }, ]; const { rerender } = render(); expect(screen.getAllByRole("link", { name: "Repositories" })).toHaveLength(1); rerender(); expect( screen.getByRole("link", { name: `${MAIN_WORKSPACE_NAME} Active` }).getAttribute("href"), ).toBe("/settings/workspace/ws-1"); const repositoryLinks = screen.getAllByRole("link", { name: "Repositories" }); const workflowLinks = screen.getAllByRole("link", { name: "Workflows" }); expect(repositoryLinks).toHaveLength(1); expect(workflowLinks).toHaveLength(1); expect(repositoryLinks[0].getAttribute("href")).toBe("/settings/workspace/ws-10/repositories"); expect(workflowLinks[0].getAttribute("href")).toBe("/settings/workspace/ws-10/workflows"); expect(screen.getByRole("link", { name: ARCHIVE_WORKSPACE_NAME }).getAttribute("href")).toBe( "/settings/workspace/ws-10", ); }); it("opens workspace integrations when a workspace integration route is active", () => { state.workspaces.items = [ { id: MAIN_WORKSPACE_ID, name: MAIN_WORKSPACE_NAME }, { id: ARCHIVE_WORKSPACE_ID, name: ARCHIVE_WORKSPACE_NAME }, ]; render(); expect(screen.getByRole("link", { name: "GitHub" }).getAttribute("href")).toBe( "/settings/workspace/ws-10/integrations/github", ); expect(screen.getByRole("button", { name: "Expand Main Workspace" })).toBeTruthy(); }); }); describe("SettingsTree integration status", () => { beforeEach(() => { state.workspaces.activeId = MAIN_WORKSPACE_ID; state.workspaces.items = [{ id: MAIN_WORKSPACE_ID, name: MAIN_WORKSPACE_NAME }]; integrationAvailability.azureDevOps = true; integrationAvailability.github = false; }); afterEach(() => cleanup()); it("labels configured integrations as enabled", () => { render( , ); expect(screen.getByRole("link", { name: "Azure DevOps Enabled" })).toBeTruthy(); expect(screen.getByTestId("azure-devops-icon")).toBeTruthy(); expect(screen.getByRole("link", { name: "GitHub" })).toBeTruthy(); }); }); describe("SettingsTree standalone leaves", () => { afterEach(() => { cleanup(); state.features.plugins = false; }); it("keeps Voice Mode in the settings tree as a standalone active leaf", () => { render(); expect(screen.getByRole("link", { name: "Voice Mode" }).getAttribute("href")).toBe( "/settings/voice-mode", ); cleanup(); render(); expect(screen.getByRole("link", { name: "Voice Mode" }).className).toContain( "before:bg-primary", ); expect(screen.queryByRole("link", { name: "Appearance" })).toBeNull(); }); it("puts Plugins immediately before System when plugins are enabled", () => { state.features.plugins = true; render(); expect( screen .getAllByRole("link") .slice(-2) .map((link) => link.textContent), ).toEqual(["Plugins", "System"]); }); }); describe("WorkspacesGroup active workspace presentation", () => { beforeEach(() => { state.workspaces.activeId = MAIN_WORKSPACE_ID; state.workspaces.items = [ { id: ARCHIVE_WORKSPACE_ID, name: ARCHIVE_WORKSPACE_NAME }, { id: MAIN_WORKSPACE_ID, name: MAIN_WORKSPACE_NAME }, ]; state.setActiveWorkspace.mockClear(); }); afterEach(() => cleanup()); it("keeps the active workspace first even when the API returns it later", () => { render(); const workspaceLinks = getWorkspaceRootLinks(); expect(workspaceLinks.map((link) => link.textContent)).toEqual([ `${MAIN_WORKSPACE_NAME}Active`, ARCHIVE_WORKSPACE_NAME, ]); }); it("expands another workspace without changing the active workspace", () => { render(); fireEvent.click(screen.getByRole("button", { name: "Expand Archive Workspace" })); expect(state.setActiveWorkspace).not.toHaveBeenCalled(); expect(getWorkspaceRootLinks()[0].textContent).toBe(`${MAIN_WORKSPACE_NAME}Active`); expect(screen.getByRole("link", { name: `${MAIN_WORKSPACE_NAME} Active` })).toBeTruthy(); }); }); describe("WorkspacesGroup integration route sync", () => { beforeEach(() => { state.workspaces.activeId = MAIN_WORKSPACE_ID; state.workspaces.items = [ { id: MAIN_WORKSPACE_ID, name: MAIN_WORKSPACE_NAME }, { id: ARCHIVE_WORKSPACE_ID, name: ARCHIVE_WORKSPACE_NAME }, ]; }); afterEach(() => cleanup()); it("opens workspace integrations after navigating into an integration route", async () => { const { rerender } = render( , ); expect(screen.queryByRole("link", { name: "GitHub" })).toBeNull(); rerender(); expect((await screen.findByRole("link", { name: "GitHub" })).getAttribute("href")).toBe( "/settings/workspace/ws-10/integrations/github", ); }); }); function getWorkspaceRootLinks(): HTMLAnchorElement[] { return screen.getAllByRole("link").filter((link): link is HTMLAnchorElement => { const href = link.getAttribute("href"); return Boolean(href?.match(/^\/settings\/workspace\/[^/]+$/)); }); }