/* eslint-disable max-lines -- this suite covers the toolbar's desktop and mobile interaction surfaces. */ import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; afterEach(cleanup); beforeEach(resetMocks); // --- Constants declared before vi.mock so factories can reference them --- const TASK_ID = "task-1"; const SESSION_ID = "session-1"; const SRC_FILE = "src/foo.ts"; // data-testid constants to avoid sonarjs/no-duplicate-string warnings const TID_TOOLBAR = "passthrough-toolbar"; const TID_COMPOSER = "passthrough-composer"; const TID_TEXTAREA = "passthrough-composer-textarea"; const TID_PROCEED = "passthrough-proceed-next-step"; const TID_TOGGLE = "passthrough-toggle-composer"; const TID_TOGGLE_COMMENTS = "passthrough-toggle-comments"; const TID_COMMENTS_PANEL = "passthrough-comments-panel"; const TID_COMMENT_CARD = "passthrough-comment-card"; const TID_COMMENT_TEXTAREA = "passthrough-comment-textarea"; const TID_COMMENT_REMOVE = "passthrough-comment-remove"; const TID_COMMENT_FILE_REF = "passthrough-comment-file-ref"; const TID_PENDING_COUNT = "passthrough-pending-count"; const TID_PENDING_BANNER = "passthrough-pending-comments-banner"; const TID_PLAN_TOGGLE = "plan-mode-toggle-button"; const TID_ATTACHMENTS = "chat-attachments-button"; const TID_CONTEXT = "chat-context-button"; const TID_SEND_COMMENTS = "passthrough-send-comments"; // --- Mutable state for per-test overrides --- let mockSessionState: string | null = null; let mockKeyboardShortcuts: Record }> = {}; const responsiveMock = vi.hoisted(() => ({ breakpoint: "desktop" as "mobile" | "tablet" | "desktop", })); let mockPendingByFile: Record = {}; let mockPlanModeEnabled = false; let mockImplementPlanHandler: ((fresh: boolean) => void) | undefined; let mockIsFinePointer = true; let mockNextStep: { proceedStepName: string | null; proceed: ReturnType; isMoving: boolean; } = { proceedStepName: null, proceed: vi.fn(), isMoving: false }; const mockToast = vi.fn(); const mockMarkCommentsSent = vi.fn(); const mockUpdateComment = vi.fn(); const mockRemoveComment = vi.fn(); const mockOpenFile = vi.fn(); let mockWsRequestFn = vi.fn(); const chatInputMock = vi.hoisted(() => ({ renderProps: vi.fn(), focusInput: vi.fn(), getTextareaElement: vi.fn(() => null as HTMLElement | null), })); const passthroughTerminalMock = vi.hoisted(() => ({ renderProps: vi.fn(), })); // --- Module mocks (hoisted by Vitest) --- vi.mock("@/components/state-provider", () => ({ useAppStore: (selector: (state: Record) => unknown) => selector({ taskSessions: { items: mockSessionState ? { [SESSION_ID]: { id: SESSION_ID, state: mockSessionState } } : {}, }, quickChat: { sessions: [] }, kanban: { workflowId: null, tasks: [] }, kanbanMulti: { snapshots: {} }, workflows: { items: [] }, userSettings: { keyboardShortcuts: mockKeyboardShortcuts, chatSubmitKey: "enter" }, }), useAppStoreApi: () => ({ getState: () => ({ kanban: { steps: [] }, kanbanMulti: { snapshots: {} }, }), }), })); vi.mock("@/components/toast-provider", () => ({ useToast: () => ({ toast: mockToast }), })); vi.mock("@/hooks/domains/kanban/use-plan-actions", () => ({ usePlanActions: () => ({ ...mockNextStep, implementPlanHandler: mockImplementPlanHandler, }), })); vi.mock("@/hooks/domains/comments/use-diff-comments", () => ({ usePendingDiffCommentsByFile: () => mockPendingByFile, })); vi.mock("@/lib/state/slices/comments/comments-store", () => ({ useCommentsStore: ( selector: (s: { markCommentsSent: typeof mockMarkCommentsSent; updateComment: typeof mockUpdateComment; removeComment: typeof mockRemoveComment; }) => unknown, ) => selector({ markCommentsSent: mockMarkCommentsSent, updateComment: mockUpdateComment, removeComment: mockRemoveComment, }), })); vi.mock("@/hooks/use-file-editors", () => ({ useFileEditors: () => ({ openFile: mockOpenFile }), })); vi.mock("@/hooks/use-responsive-breakpoint", () => ({ useResponsiveBreakpoint: () => ({ isMobile: responsiveMock.breakpoint === "mobile", isTablet: responsiveMock.breakpoint === "tablet", isFinePointer: mockIsFinePointer, }), })); vi.mock("@/lib/ws/connection", () => ({ getWebSocketClient: () => ({ request: mockWsRequestFn }), })); // Stub heavy sub-components that involve xterm / canvas / WebGL. vi.mock("./passthrough-terminal", () => ({ PassthroughTerminal: (props: { enableTouchScroll?: boolean }) => { passthroughTerminalMock.renderProps(props); return
; }, })); vi.mock("@/components/github/pr-status-chip", () => ({ PRStatusChip: () => null, })); vi.mock("@/components/gitlab/mr-status-chip", () => ({ MRStatusChip: () => null, })); vi.mock("@/components/azure-devops/azure-devops-task-pull-request-chip", () => ({ AzureDevOpsTaskPullRequestChip: () => null, })); vi.mock("@/components/integrations/registered-change-request-status", () => ({ RegisteredChangeRequestStatus: ({ taskId, sessionId, surface, }: { taskId: string; sessionId: string; surface: string; }) => ( {taskId}:{sessionId} ), })); vi.mock("./chat/pr-archive-banners", () => ({ PRMergedBanner: () => null, })); vi.mock("@kandev/ui/tooltip", () => ({ Tooltip: ({ children }: { children: React.ReactNode }) => <>{children}, TooltipTrigger: ({ asChild: _asChild, children, }: { asChild?: boolean; children: React.ReactNode; }) => <>{children}, TooltipContent: ({ children }: { children: React.ReactNode }) => <>{children}, })); vi.mock("./chat/use-chat-panel-state", () => ({ useChatPanelState: () => ({ resolvedSessionId: SESSION_ID, taskId: TASK_ID, task: { id: TASK_ID, title: "Task title" }, taskDescription: "Task description", isCompleted: mockSessionState === "COMPLETED", planModeEnabled: mockPlanModeEnabled, planModeAvailable: true, mcpServers: ["kandev"], handlePlanModeChange: vi.fn(), isStarting: false, isPreparingEnvironment: false, contextItems: [{ id: "file:src/foo.ts", kind: "file", label: "foo.ts", filePath: SRC_FILE }], contextFiles: [{ path: SRC_FILE, name: "foo.ts" }], handleToggleContextFile: vi.fn(), handleAddContextFile: vi.fn(), addContextFile: vi.fn(), clearEphemeral: vi.fn(), planContextEnabled: false, chatSubmitKey: "enter", prompts: [{ id: "prompt-1", name: "Prompt", content: "Prompt content" }], planComments: [], pendingPRFeedback: [], walkthroughComments: [], messageComments: [], handleClearMessageComments: vi.fn(), pendingCommentsByFile: mockPendingByFile, }), })); vi.mock("./chat/chat-input-container", async () => { const React = await vi.importActual("react"); const ChatInputContainer = React.forwardRef, Record>( function MockChatInputContainer(props, ref) { const [value, setValue] = React.useState(""); chatInputMock.renderProps(props); React.useImperativeHandle(ref, () => ({ focusInput: chatInputMock.focusInput, getTextareaElement: chatInputMock.getTextareaElement, getValue: () => value, getSelectionStart: () => value.length, insertText: vi.fn(), clear: vi.fn(), getAttachments: () => [], })); return (
{props.hasContextComments ?
: null}