|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, type ReactNode } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +vi.mock('@/hooks/queries/skills', () => ({ useSkills: () => ({ data: [] }) })) |
| 9 | +vi.mock('@/hooks/queries/workflows', () => ({ useWorkflows: () => ({ data: [] }) })) |
| 10 | +vi.mock('@/hooks/queries/tables', () => ({ useTablesList: () => ({ data: [] }) })) |
| 11 | +vi.mock('@/hooks/queries/workspace-files', () => ({ useWorkspaceFiles: () => ({ data: [] }) })) |
| 12 | +vi.mock('@/hooks/queries/kb/knowledge', () => ({ useKnowledgeBasesQuery: () => ({ data: [] }) })) |
| 13 | +vi.mock('@/hooks/queries/folders', () => ({ useFolders: () => ({ data: [] }) })) |
| 14 | +vi.mock('@/hooks/queries/workspace-file-folders', () => ({ |
| 15 | + useWorkspaceFileFolders: () => ({ data: [] }), |
| 16 | +})) |
| 17 | +vi.mock('@/hooks/queries/mothership-chats', () => ({ useMothershipChats: () => ({ data: [] }) })) |
| 18 | +vi.mock('@/hooks/queries/schedules', () => ({ useWorkspaceSchedules: () => ({ data: [] }) })) |
| 19 | +vi.mock('@/hooks/queries/logs', () => ({ useLogsList: () => ({ data: undefined }) })) |
| 20 | +vi.mock('@/blocks/integration-matcher', () => ({ |
| 21 | + getIntegrationMatcher: () => ({ regex: null, byName: new Map() }), |
| 22 | + listIntegrations: () => [], |
| 23 | +})) |
| 24 | + |
| 25 | +import type { PlusMenuHandle } from '@/app/workspace/[workspaceId]/home/components/user-input/components/constants' |
| 26 | +import { |
| 27 | + type UsePromptEditorProps, |
| 28 | + usePromptEditor, |
| 29 | +} from '@/app/workspace/[workspaceId]/home/components/user-input/components/prompt-editor/use-prompt-editor' |
| 30 | + |
| 31 | +/** |
| 32 | + * Mounts `usePromptEditor` in a real React 19 root under jsdom (no |
| 33 | + * `@testing-library/react` in this repo — see `hooks/queries/unsubscribe.test.tsx` |
| 34 | + * for the established pattern) and wires a real `<textarea>` into its |
| 35 | + * `textareaRef` so selection/caret-driven behavior (`handleSelectAdjust`, |
| 36 | + * `syncMentionState`) runs exactly as it does in the rendered `PromptEditor`. |
| 37 | + */ |
| 38 | +function renderPromptEditor(props: UsePromptEditorProps) { |
| 39 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 40 | + const container = document.createElement('div') |
| 41 | + document.body.appendChild(container) |
| 42 | + const root: Root = createRoot(container) |
| 43 | + let latest: ReturnType<typeof usePromptEditor> |
| 44 | + |
| 45 | + function Probe() { |
| 46 | + latest = usePromptEditor(props) |
| 47 | + return null |
| 48 | + } |
| 49 | + |
| 50 | + function Wrapper({ children }: { children: ReactNode }) { |
| 51 | + return <>{children}</> |
| 52 | + } |
| 53 | + |
| 54 | + act(() => { |
| 55 | + root.render( |
| 56 | + <Wrapper> |
| 57 | + <Probe /> |
| 58 | + </Wrapper> |
| 59 | + ) |
| 60 | + }) |
| 61 | + |
| 62 | + const textarea = document.createElement('textarea') |
| 63 | + document.body.appendChild(textarea) |
| 64 | + latest!.textareaRef.current = textarea |
| 65 | + |
| 66 | + return { |
| 67 | + result: () => latest, |
| 68 | + textarea, |
| 69 | + unmount: () => { |
| 70 | + act(() => root.unmount()) |
| 71 | + container.remove() |
| 72 | + textarea.remove() |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** Fires a native `input` event carrying the new value, as the textarea would on a keystroke. */ |
| 78 | +function typeInto(textarea: HTMLTextAreaElement, value: string, caret = value.length) { |
| 79 | + textarea.value = value |
| 80 | + textarea.setSelectionRange(caret, caret) |
| 81 | + textarea.dispatchEvent(new Event('input', { bubbles: true })) |
| 82 | +} |
| 83 | + |
| 84 | +describe('usePromptEditor mention menu dismissal', () => { |
| 85 | + let openMenu: PlusMenuHandle |
| 86 | + |
| 87 | + beforeEach(() => { |
| 88 | + openMenu = { |
| 89 | + open: vi.fn(), |
| 90 | + close: vi.fn(), |
| 91 | + moveActive: vi.fn(), |
| 92 | + selectActive: vi.fn(() => false), |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + afterEach(() => { |
| 97 | + vi.clearAllMocks() |
| 98 | + }) |
| 99 | + |
| 100 | + it('reopens the menu while the user keeps typing an unmatched mention', () => { |
| 101 | + const { result, textarea, unmount } = renderPromptEditor({ workspaceId: 'ws-1' }) |
| 102 | + result().plusMenuRef.current = openMenu |
| 103 | + |
| 104 | + act(() => { |
| 105 | + typeInto(textarea, '@f') |
| 106 | + // `onChange` in the real component is wired to `handleInputChange`; the |
| 107 | + // hook only exposes it through the returned object, so invoke it the |
| 108 | + // same way `<textarea onChange={editor.handleInputChange} />` would. |
| 109 | + result().handleInputChange({ |
| 110 | + target: textarea, |
| 111 | + currentTarget: textarea, |
| 112 | + } as unknown as React.ChangeEvent<HTMLTextAreaElement>) |
| 113 | + }) |
| 114 | + |
| 115 | + expect(result().mentionQuery).toBe('f') |
| 116 | + expect(openMenu.open).toHaveBeenCalledTimes(1) |
| 117 | + |
| 118 | + unmount() |
| 119 | + }) |
| 120 | + |
| 121 | + it('does not reopen after the user clicks away, even if the caret lands back inside the open mention', () => { |
| 122 | + const { result, textarea, unmount } = renderPromptEditor({ workspaceId: 'ws-1' }) |
| 123 | + result().plusMenuRef.current = openMenu |
| 124 | + |
| 125 | + act(() => { |
| 126 | + typeInto(textarea, '@f') |
| 127 | + result().handleInputChange({ |
| 128 | + target: textarea, |
| 129 | + currentTarget: textarea, |
| 130 | + } as unknown as React.ChangeEvent<HTMLTextAreaElement>) |
| 131 | + }) |
| 132 | + expect(result().mentionQuery).toBe('f') |
| 133 | + |
| 134 | + // Radix's DismissableLayer calls this on outside pointerdown / Escape — |
| 135 | + // never on a programmatic `plusMenuRef.current.close()`. |
| 136 | + act(() => { |
| 137 | + result().handlePlusMenuClose() |
| 138 | + }) |
| 139 | + expect(result().mentionQuery).toBeNull() |
| 140 | + |
| 141 | + // The same click that dismissed the popover also moves the caret via |
| 142 | + // native textarea behavior; the caret lands back at the end of the text, |
| 143 | + // i.e. still inside the unterminated "@f" token. Simulate the resulting |
| 144 | + // onSelect/onMouseUp without any further keystroke. |
| 145 | + act(() => { |
| 146 | + textarea.setSelectionRange(2, 2) |
| 147 | + result().handleSelectAdjust() |
| 148 | + }) |
| 149 | + |
| 150 | + expect(result().mentionQuery).toBeNull() |
| 151 | + expect(openMenu.open).toHaveBeenCalledTimes(1) // only the original open — no reopen |
| 152 | + |
| 153 | + unmount() |
| 154 | + }) |
| 155 | + |
| 156 | + it('lets a further keystroke reopen the same mention after a dismiss', () => { |
| 157 | + const { result, textarea, unmount } = renderPromptEditor({ workspaceId: 'ws-1' }) |
| 158 | + result().plusMenuRef.current = openMenu |
| 159 | + |
| 160 | + act(() => { |
| 161 | + typeInto(textarea, '@f') |
| 162 | + result().handleInputChange({ |
| 163 | + target: textarea, |
| 164 | + currentTarget: textarea, |
| 165 | + } as unknown as React.ChangeEvent<HTMLTextAreaElement>) |
| 166 | + }) |
| 167 | + act(() => { |
| 168 | + result().handlePlusMenuClose() |
| 169 | + }) |
| 170 | + act(() => { |
| 171 | + textarea.setSelectionRange(2, 2) |
| 172 | + result().handleSelectAdjust() |
| 173 | + }) |
| 174 | + expect(openMenu.open).toHaveBeenCalledTimes(1) |
| 175 | + |
| 176 | + // User keeps editing the same token — this must reopen it. |
| 177 | + act(() => { |
| 178 | + typeInto(textarea, '@fo', 3) |
| 179 | + result().handleInputChange({ |
| 180 | + target: textarea, |
| 181 | + currentTarget: textarea, |
| 182 | + } as unknown as React.ChangeEvent<HTMLTextAreaElement>) |
| 183 | + }) |
| 184 | + |
| 185 | + expect(result().mentionQuery).toBe('fo') |
| 186 | + expect(openMenu.open).toHaveBeenCalledTimes(2) |
| 187 | + |
| 188 | + unmount() |
| 189 | + }) |
| 190 | + |
| 191 | + it('does not suppress a different mention typed after a dismiss', () => { |
| 192 | + const { result, textarea, unmount } = renderPromptEditor({ workspaceId: 'ws-1' }) |
| 193 | + result().plusMenuRef.current = openMenu |
| 194 | + |
| 195 | + act(() => { |
| 196 | + typeInto(textarea, '@f') |
| 197 | + result().handleInputChange({ |
| 198 | + target: textarea, |
| 199 | + currentTarget: textarea, |
| 200 | + } as unknown as React.ChangeEvent<HTMLTextAreaElement>) |
| 201 | + }) |
| 202 | + act(() => { |
| 203 | + result().handlePlusMenuClose() |
| 204 | + }) |
| 205 | + act(() => { |
| 206 | + textarea.setSelectionRange(2, 2) |
| 207 | + result().handleSelectAdjust() |
| 208 | + }) |
| 209 | + expect(openMenu.open).toHaveBeenCalledTimes(1) |
| 210 | + |
| 211 | + // Clear the field and start a brand new mention elsewhere in the text — |
| 212 | + // a stale dismissal must never leak onto an unrelated token. |
| 213 | + act(() => { |
| 214 | + typeInto(textarea, '@f done. @g', 11) |
| 215 | + result().handleInputChange({ |
| 216 | + target: textarea, |
| 217 | + currentTarget: textarea, |
| 218 | + } as unknown as React.ChangeEvent<HTMLTextAreaElement>) |
| 219 | + }) |
| 220 | + |
| 221 | + expect(result().mentionQuery).toBe('g') |
| 222 | + expect(openMenu.open).toHaveBeenCalledTimes(2) |
| 223 | + |
| 224 | + unmount() |
| 225 | + }) |
| 226 | +}) |
0 commit comments