|
| 1 | +import React from "react" |
| 2 | +import { render, fireEvent, screen } from "@src/utils/test-utils" |
| 3 | +import { useExtensionState } from "@src/context/ExtensionStateContext" |
| 4 | +import { vscode } from "@src/utils/vscode" |
| 5 | +import { ContextMenuOptionType } from "@src/utils/context-mentions" |
| 6 | +import { ChatTextArea } from "../ChatTextArea" |
| 7 | + |
| 8 | +// Mock VS Code messaging |
| 9 | +vi.mock("@src/utils/vscode", () => ({ |
| 10 | + vscode: { |
| 11 | + postMessage: vi.fn(), |
| 12 | + }, |
| 13 | +})) |
| 14 | + |
| 15 | +// Capture the last props passed to ContextMenu so we can invoke onSelect directly |
| 16 | +let lastContextMenuProps: any = null |
| 17 | +vi.mock("../ContextMenu", () => { |
| 18 | + return { |
| 19 | + __esModule: true, |
| 20 | + default: (props: any) => { |
| 21 | + lastContextMenuProps = props |
| 22 | + return <div data-testid="context-menu" /> |
| 23 | + }, |
| 24 | + __getLastProps: () => lastContextMenuProps, |
| 25 | + } |
| 26 | +}) |
| 27 | + |
| 28 | +// Mock ExtensionStateContext |
| 29 | +vi.mock("@src/context/ExtensionStateContext") |
| 30 | + |
| 31 | +describe("ChatTextArea - folder drilldown behavior", () => { |
| 32 | + const defaultProps = { |
| 33 | + inputValue: "", |
| 34 | + setInputValue: vi.fn(), |
| 35 | + onSend: vi.fn(), |
| 36 | + sendingDisabled: false, |
| 37 | + selectApiConfigDisabled: false, |
| 38 | + onSelectImages: vi.fn(), |
| 39 | + shouldDisableImages: false, |
| 40 | + placeholderText: "Type a message...", |
| 41 | + selectedImages: [], |
| 42 | + setSelectedImages: vi.fn(), |
| 43 | + onHeightChange: vi.fn(), |
| 44 | + mode: "architect", |
| 45 | + setMode: vi.fn(), |
| 46 | + modeShortcutText: "(⌘. for next mode)", |
| 47 | + } |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + vi.clearAllMocks() |
| 51 | + ;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({ |
| 52 | + filePaths: ["src/", "src/index.ts"], |
| 53 | + openedTabs: [], |
| 54 | + taskHistory: [], |
| 55 | + cwd: "/test/workspace", |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + it("keeps picker open and triggers folder children search when selecting a folder", () => { |
| 60 | + const setInputValue = vi.fn() |
| 61 | + |
| 62 | + const { container } = render(<ChatTextArea {...defaultProps} setInputValue={setInputValue} />) |
| 63 | + |
| 64 | + // Type to open the @-context menu and set a query |
| 65 | + const textarea = container.querySelector("textarea")! |
| 66 | + fireEvent.change(textarea, { |
| 67 | + target: { value: "@s", selectionStart: 2 }, |
| 68 | + }) |
| 69 | + |
| 70 | + // Ensure our mocked ContextMenu rendered and captured props |
| 71 | + expect(screen.getByTestId("context-menu")).toBeInTheDocument() |
| 72 | + const props = lastContextMenuProps |
| 73 | + expect(props).toBeTruthy() |
| 74 | + expect(typeof props.onSelect).toBe("function") |
| 75 | + |
| 76 | + // Simulate selecting a concrete folder suggestion (e.g. "/src") |
| 77 | + props.onSelect(ContextMenuOptionType.Folder, "/src") |
| 78 | + |
| 79 | + // The input should contain "@/src/" with NO trailing space and the picker should remain open |
| 80 | + expect(setInputValue).toHaveBeenCalled() |
| 81 | + const finalValue = setInputValue.mock.calls.at(-1)?.[0] |
| 82 | + expect(finalValue).toBe("@/src/") |
| 83 | + |
| 84 | + // Context menu should still be present (picker remains open) |
| 85 | + expect(screen.getByTestId("context-menu")).toBeInTheDocument() |
| 86 | + |
| 87 | + // It should have kicked off a searchFiles request for the folder children |
| 88 | + const pm = vscode.postMessage as ReturnType<typeof vi.fn> |
| 89 | + expect(pm).toHaveBeenCalled() |
| 90 | + const lastMsg = pm.mock.calls.at(-1)?.[0] |
| 91 | + expect(lastMsg).toMatchObject({ type: "searchFiles" }) |
| 92 | + // Query mirrors substring after '@' including leading slash per existing logic |
| 93 | + expect(lastMsg.query).toBe("/src/") |
| 94 | + expect(typeof lastMsg.requestId).toBe("string") |
| 95 | + }) |
| 96 | +}) |
0 commit comments