import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; // --- 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"; // --- Mutable state for per-test overrides --- let mockSessionState: string | null = null; let mockKeyboardShortcuts: Record }> = {}; let mockPendingByFile: Record = {}; 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), })); // --- 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 } } : {}, }, 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", () => ({ useNextWorkflowStep: () => mockNextStep, })); 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("@/lib/ws/connection", () => ({ getWebSocketClient: () => ({ request: mockWsRequestFn }), })); // Stub heavy sub-components that involve xterm / canvas / WebGL. vi.mock("./passthrough-terminal", () => ({ PassthroughTerminal: () =>
, })); vi.mock("@/components/github/pr-status-chip", () => ({ PRStatusChip: () => null, })); vi.mock("./chat/chat-input-area", () => ({ 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", planModeEnabled: false, 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: [], 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}