-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathChatTextArea.spec.tsx
More file actions
1208 lines (975 loc) · 40 KB
/
ChatTextArea.spec.tsx
File metadata and controls
1208 lines (975 loc) · 40 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { defaultModeSlug } from "@roo/modes"
import { render, fireEvent, screen } from "@src/utils/test-utils"
import { useExtensionState } from "@src/context/ExtensionStateContext"
import { vscode } from "@src/utils/vscode"
import * as pathMentions from "@src/utils/path-mentions"
import { ChatTextArea } from "../ChatTextArea"
vi.mock("@src/utils/vscode", () => ({
vscode: {
postMessage: vi.fn(),
},
}))
vi.mock("@src/components/common/CodeBlock")
vi.mock("@src/components/common/MarkdownBlock")
vi.mock("@src/utils/path-mentions", () => ({
convertToMentionPath: vi.fn((path, cwd) => {
// Simple mock implementation that mimics the real function's behavior
if (cwd && path.toLowerCase().startsWith(cwd.toLowerCase())) {
const relativePath = path.substring(cwd.length)
return "@" + (relativePath.startsWith("/") ? relativePath : "/" + relativePath)
}
return path
}),
}))
// Get the mocked postMessage function
const mockPostMessage = vscode.postMessage as ReturnType<typeof vi.fn>
const mockConvertToMentionPath = pathMentions.convertToMentionPath as ReturnType<typeof vi.fn>
// Mock ExtensionStateContext
vi.mock("@src/context/ExtensionStateContext")
// Custom query function to get the enhance prompt button
const getEnhancePromptButton = () => {
return screen.getByRole("button", {
name: (_, element) => {
// Find the button with the wand sparkles icon (Lucide React)
return element.querySelector(".lucide-wand-sparkles") !== null
},
})
}
describe("ChatTextArea", () => {
const defaultProps = {
inputValue: "",
setInputValue: vi.fn(),
onSend: vi.fn(),
sendingDisabled: false,
selectApiConfigDisabled: false,
onSelectImages: vi.fn(),
shouldDisableImages: false,
placeholderText: "Type a message...",
selectedImages: [],
setSelectedImages: vi.fn(),
onHeightChange: vi.fn(),
mode: defaultModeSlug,
setMode: vi.fn(),
modeShortcutText: "(⌘. for next mode)",
}
beforeEach(() => {
vi.clearAllMocks()
// Default mock implementation for useExtensionState
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "anthropic",
},
taskHistory: [],
cwd: "/test/workspace",
})
})
describe("enhance prompt button", () => {
it("should be enabled even when sendingDisabled is true (for message queueing)", () => {
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
taskHistory: [],
cwd: "/test/workspace",
})
render(<ChatTextArea {...defaultProps} sendingDisabled={true} />)
const enhanceButton = getEnhancePromptButton()
expect(enhanceButton).toHaveClass("cursor-pointer")
})
})
describe("handleEnhancePrompt", () => {
it("should send message with correct configuration when clicked", () => {
const apiConfiguration = {
apiProvider: "openrouter",
apiKey: "test-key",
}
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration,
taskHistory: [],
cwd: "/test/workspace",
})
render(<ChatTextArea {...defaultProps} inputValue="Test prompt" />)
const enhanceButton = getEnhancePromptButton()
fireEvent.click(enhanceButton)
expect(mockPostMessage).toHaveBeenCalledWith({
type: "enhancePrompt",
text: "Test prompt",
})
})
it("should not send message when input is empty", () => {
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "openrouter",
},
taskHistory: [],
cwd: "/test/workspace",
})
render(<ChatTextArea {...defaultProps} inputValue="" />)
// Clear any calls from component initialization (e.g., IndexingStatusBadge)
mockPostMessage.mockClear()
const enhanceButton = getEnhancePromptButton()
fireEvent.click(enhanceButton)
expect(mockPostMessage).not.toHaveBeenCalled()
})
it("should show loading state while enhancing", () => {
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "openrouter",
},
taskHistory: [],
cwd: "/test/workspace",
})
render(<ChatTextArea {...defaultProps} inputValue="Test prompt" />)
const enhanceButton = getEnhancePromptButton()
fireEvent.click(enhanceButton)
// Check if the WandSparkles icon has the animate-spin class
const animatingIcon = enhanceButton.querySelector(".animate-spin")
expect(animatingIcon).toBeInTheDocument()
})
})
describe("effect dependencies", () => {
it("should update when apiConfiguration changes", () => {
const { rerender } = render(<ChatTextArea {...defaultProps} />)
// Update apiConfiguration
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "openrouter",
newSetting: "test",
},
taskHistory: [],
cwd: "/test/workspace",
})
rerender(<ChatTextArea {...defaultProps} />)
// Verify the enhance button appears after apiConfiguration changes
expect(getEnhancePromptButton()).toBeInTheDocument()
})
})
describe("enhanced prompt response", () => {
it("should update input value using native browser methods when receiving enhanced prompt", () => {
const setInputValue = vi.fn()
// Mock document.execCommand
const mockExecCommand = vi.fn().mockReturnValue(true)
Object.defineProperty(document, "execCommand", {
value: mockExecCommand,
writable: true,
})
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Original prompt" />,
)
const textarea = container.querySelector("textarea")!
// Mock textarea methods
const mockSelect = vi.fn()
const mockFocus = vi.fn()
textarea.select = mockSelect
textarea.focus = mockFocus
// Simulate receiving enhanced prompt message
window.dispatchEvent(
new MessageEvent("message", {
data: {
type: "enhancedPrompt",
text: "Enhanced test prompt",
},
}),
)
// Verify native browser methods were used
expect(mockFocus).toHaveBeenCalled()
expect(mockSelect).toHaveBeenCalled()
expect(mockExecCommand).toHaveBeenCalledWith("insertText", false, "Enhanced test prompt")
})
it("should fallback to setInputValue when execCommand is not available", () => {
const setInputValue = vi.fn()
// Mock document.execCommand to be undefined (not available)
Object.defineProperty(document, "execCommand", {
value: undefined,
writable: true,
})
render(<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Original prompt" />)
// Simulate receiving enhanced prompt message
window.dispatchEvent(
new MessageEvent("message", {
data: {
type: "enhancedPrompt",
text: "Enhanced test prompt",
},
}),
)
// Verify fallback to setInputValue was used
expect(setInputValue).toHaveBeenCalledWith("Enhanced test prompt")
})
it("should not crash when textarea ref is not available", () => {
const setInputValue = vi.fn()
render(<ChatTextArea {...defaultProps} setInputValue={setInputValue} />)
// Simulate receiving enhanced prompt message when textarea ref might not be ready
expect(() => {
window.dispatchEvent(
new MessageEvent("message", {
data: {
type: "enhancedPrompt",
text: "Enhanced test prompt",
},
}),
)
}).not.toThrow()
})
})
describe("multi-file drag and drop", () => {
const mockCwd = "/Users/test/project"
beforeEach(() => {
vi.clearAllMocks()
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
cwd: mockCwd,
})
mockConvertToMentionPath.mockClear()
})
it("should process multiple file paths separated by newlines", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Initial text" />,
)
// Create a mock dataTransfer object with text data containing multiple file paths
const dataTransfer = {
getData: vi.fn().mockReturnValue("/Users/test/project/file1.js\n/Users/test/project/file2.js"),
files: [],
}
// Simulate drop event
fireEvent.drop(container.querySelector(".chat-text-area")!, {
dataTransfer,
preventDefault: vi.fn(),
})
// Verify convertToMentionPath was called for each file path
expect(mockConvertToMentionPath).toHaveBeenCalledTimes(2)
expect(mockConvertToMentionPath).toHaveBeenCalledWith("/Users/test/project/file1.js", mockCwd)
expect(mockConvertToMentionPath).toHaveBeenCalledWith("/Users/test/project/file2.js", mockCwd)
// Verify setInputValue was called with the correct value
// The mock implementation of convertToMentionPath will convert the paths to @/file1.js and @/file2.js
expect(setInputValue).toHaveBeenCalledWith("@/file1.js @/file2.js Initial text")
})
it("should filter out empty lines in the dragged text", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Initial text" />,
)
// Create a mock dataTransfer object with text data containing empty lines
const dataTransfer = {
getData: vi.fn().mockReturnValue("/Users/test/project/file1.js\n\n/Users/test/project/file2.js\n\n"),
files: [],
}
// Simulate drop event
fireEvent.drop(container.querySelector(".chat-text-area")!, {
dataTransfer,
preventDefault: vi.fn(),
})
// Verify convertToMentionPath was called only for non-empty lines
expect(mockConvertToMentionPath).toHaveBeenCalledTimes(2)
// Verify setInputValue was called with the correct value
expect(setInputValue).toHaveBeenCalledWith("@/file1.js @/file2.js Initial text")
})
it("should correctly update cursor position after adding multiple mentions", () => {
const setInputValue = vi.fn()
const initialCursorPosition = 5
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Hello world" />,
)
// Set the cursor position manually
const textArea = container.querySelector("textarea")
if (textArea) {
textArea.selectionStart = initialCursorPosition
textArea.selectionEnd = initialCursorPosition
}
// Create a mock dataTransfer object with text data
const dataTransfer = {
getData: vi.fn().mockReturnValue("/Users/test/project/file1.js\n/Users/test/project/file2.js"),
files: [],
}
// Simulate drop event
fireEvent.drop(container.querySelector(".chat-text-area")!, {
dataTransfer,
preventDefault: vi.fn(),
})
// The cursor position should be updated based on the implementation in the component
expect(setInputValue).toHaveBeenCalledWith("@/file1.js @/file2.js Hello world")
})
it("should handle very long file paths correctly", () => {
const setInputValue = vi.fn()
const { container } = render(<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />)
// Create a very long file path
const longPath =
"/Users/test/project/very/long/path/with/many/nested/directories/and/a/very/long/filename/with/extension.typescript"
// Create a mock dataTransfer object with the long path
const dataTransfer = {
getData: vi.fn().mockReturnValue(longPath),
files: [],
}
// Simulate drop event
fireEvent.drop(container.querySelector(".chat-text-area")!, {
dataTransfer,
preventDefault: vi.fn(),
})
// Verify convertToMentionPath was called with the long path
expect(mockConvertToMentionPath).toHaveBeenCalledWith(longPath, mockCwd)
// The mock implementation will convert it to @/very/long/path/...
expect(setInputValue).toHaveBeenCalledWith(
"@/very/long/path/with/many/nested/directories/and/a/very/long/filename/with/extension.typescript ",
)
})
it("should handle paths with special characters correctly", () => {
const setInputValue = vi.fn()
const { container } = render(<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />)
// Create paths with special characters
const specialPath1 = "/Users/test/project/file with spaces.js"
const specialPath2 = "/Users/test/project/file-with-dashes.js"
const specialPath3 = "/Users/test/project/file_with_underscores.js"
const specialPath4 = "/Users/test/project/file.with.dots.js"
// Create a mock dataTransfer object with the special paths
const dataTransfer = {
getData: vi.fn().mockReturnValue(`${specialPath1}\n${specialPath2}\n${specialPath3}\n${specialPath4}`),
files: [],
}
// Simulate drop event
fireEvent.drop(container.querySelector(".chat-text-area")!, {
dataTransfer,
preventDefault: vi.fn(),
})
// Verify convertToMentionPath was called for each path
expect(mockConvertToMentionPath).toHaveBeenCalledTimes(4)
expect(mockConvertToMentionPath).toHaveBeenCalledWith(specialPath1, mockCwd)
expect(mockConvertToMentionPath).toHaveBeenCalledWith(specialPath2, mockCwd)
expect(mockConvertToMentionPath).toHaveBeenCalledWith(specialPath3, mockCwd)
expect(mockConvertToMentionPath).toHaveBeenCalledWith(specialPath4, mockCwd)
// Verify setInputValue was called with the correct value
expect(setInputValue).toHaveBeenCalledWith(
"@/file with spaces.js @/file-with-dashes.js @/file_with_underscores.js @/file.with.dots.js ",
)
})
it("should handle paths outside the current working directory", () => {
const setInputValue = vi.fn()
const { container } = render(<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />)
// Create paths outside the current working directory
const outsidePath = "/Users/other/project/file.js"
// Mock the convertToMentionPath function to return the original path for paths outside cwd
mockConvertToMentionPath.mockImplementationOnce((path, _cwd) => {
return path // Return original path for this test
})
// Create a mock dataTransfer object with the outside path
const dataTransfer = {
getData: vi.fn().mockReturnValue(outsidePath),
files: [],
}
// Simulate drop event
fireEvent.drop(container.querySelector(".chat-text-area")!, {
dataTransfer,
preventDefault: vi.fn(),
})
// Verify convertToMentionPath was called with the outside path
expect(mockConvertToMentionPath).toHaveBeenCalledWith(outsidePath, mockCwd)
// Verify setInputValue was called with the original path
expect(setInputValue).toHaveBeenCalledWith("/Users/other/project/file.js ")
})
it("should do nothing when dropped text is empty", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Initial text" />,
)
// Create a mock dataTransfer object with empty text
const dataTransfer = {
getData: vi.fn().mockReturnValue(""),
files: [],
}
// Simulate drop event
fireEvent.drop(container.querySelector(".chat-text-area")!, {
dataTransfer,
preventDefault: vi.fn(),
})
// Verify convertToMentionPath was not called
expect(mockConvertToMentionPath).not.toHaveBeenCalled()
// Verify setInputValue was not called
expect(setInputValue).not.toHaveBeenCalled()
})
describe("prompt history navigation", () => {
const mockClineMessages = [
{ type: "say", say: "user_feedback", text: "First prompt", ts: 1000 },
{ type: "say", say: "user_feedback", text: "Second prompt", ts: 2000 },
{ type: "say", say: "user_feedback", text: "Third prompt", ts: 3000 },
]
beforeEach(() => {
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "anthropic",
},
taskHistory: [],
clineMessages: mockClineMessages,
cwd: "/test/workspace",
})
})
it("should navigate to previous prompt on arrow up when cursor is at beginning", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
)
const textarea = container.querySelector("textarea")!
// Ensure cursor is at the beginning
textarea.setSelectionRange(0, 0)
// Simulate arrow up key press
fireEvent.keyDown(textarea, { key: "ArrowUp" })
// Should set the newest conversation message (first in reversed array)
expect(setInputValue).toHaveBeenCalledWith("Third prompt")
})
it("should navigate through history with multiple arrow up presses", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
)
const textarea = container.querySelector("textarea")!
// First arrow up - newest conversation message
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Third prompt")
// Update input value to simulate the state change
setInputValue.mockClear()
// Second arrow up - previous conversation message
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Second prompt")
})
it("should navigate forward with arrow down", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
)
const textarea = container.querySelector("textarea")!
// Go back in history first (index 0 -> "Third prompt", then index 1 -> "Second prompt")
fireEvent.keyDown(textarea, { key: "ArrowUp" })
fireEvent.keyDown(textarea, { key: "ArrowUp" })
setInputValue.mockClear()
// Navigate forward (from index 1 back to index 0)
fireEvent.keyDown(textarea, { key: "ArrowDown" })
expect(setInputValue).toHaveBeenCalledWith("Third prompt")
})
it("should preserve current input when starting navigation", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Current input" />,
)
const textarea = container.querySelector("textarea")!
// Navigate to history
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Third prompt")
setInputValue.mockClear()
// Navigate back to current input
fireEvent.keyDown(textarea, { key: "ArrowDown" })
expect(setInputValue).toHaveBeenCalledWith("Current input")
})
it("should reset history navigation when user types", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
)
const textarea = container.querySelector("textarea")!
// Navigate to history
fireEvent.keyDown(textarea, { key: "ArrowUp" })
setInputValue.mockClear()
// Type something
fireEvent.change(textarea, { target: { value: "New input", selectionStart: 9 } })
// Should reset history navigation
expect(setInputValue).toHaveBeenCalledWith("New input")
})
it("should reset history navigation when sending message", () => {
const onSend = vi.fn()
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea
{...defaultProps}
onSend={onSend}
setInputValue={setInputValue}
inputValue="Test message"
/>,
)
const textarea = container.querySelector("textarea")!
// Navigate to history first
fireEvent.keyDown(textarea, { key: "ArrowUp" })
setInputValue.mockClear()
// Send message
fireEvent.keyDown(textarea, { key: "Enter" })
expect(onSend).toHaveBeenCalled()
})
it("should navigate history when cursor is at first line", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
)
const textarea = container.querySelector("textarea")!
// Clear any calls from initial render
setInputValue.mockClear()
// With empty input, cursor is at first line by default
// Arrow up should navigate history
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Third prompt")
})
it("should filter history by current workspace", () => {
const mixedClineMessages = [
{ type: "say", say: "user_feedback", text: "Workspace 1 prompt", ts: 1000 },
{ type: "say", say: "user_feedback", text: "Other workspace prompt", ts: 2000 },
{ type: "say", say: "user_feedback", text: "Workspace 1 prompt 2", ts: 3000 },
]
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "anthropic",
},
taskHistory: [],
clineMessages: mixedClineMessages,
cwd: "/test/workspace",
})
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
)
const textarea = container.querySelector("textarea")!
// Should show conversation messages newest first (after reverse)
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Workspace 1 prompt 2")
setInputValue.mockClear()
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Other workspace prompt")
})
it("should handle empty conversation history gracefully", () => {
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "anthropic",
},
taskHistory: [],
clineMessages: [],
cwd: "/test/workspace",
})
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
)
const textarea = container.querySelector("textarea")!
// Should not crash or call setInputValue
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).not.toHaveBeenCalled()
})
it("should ignore empty or whitespace-only messages", () => {
const clineMessagesWithEmpty = [
{ type: "say", say: "user_feedback", text: "Valid prompt", ts: 1000 },
{ type: "say", say: "user_feedback", text: "", ts: 2000 },
{ type: "say", say: "user_feedback", text: " ", ts: 3000 },
{ type: "say", say: "user_feedback", text: "Another valid prompt", ts: 4000 },
]
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "anthropic",
},
taskHistory: [],
clineMessages: clineMessagesWithEmpty,
cwd: "/test/workspace",
})
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
)
const textarea = container.querySelector("textarea")!
// Should skip empty messages, newest first for conversation
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Another valid prompt")
setInputValue.mockClear()
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Valid prompt")
})
it("should use task history (oldest first) when no conversation messages exist", () => {
const mockTaskHistory = [
{ task: "First task", workspace: "/test/workspace" },
{ task: "Second task", workspace: "/test/workspace" },
{ task: "Third task", workspace: "/test/workspace" },
]
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "anthropic",
},
taskHistory: mockTaskHistory,
clineMessages: [], // No conversation messages
cwd: "/test/workspace",
})
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
)
const textarea = container.querySelector("textarea")!
// Should show task history oldest first (chronological order)
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("First task")
setInputValue.mockClear()
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Second task")
})
it("should reset navigation position when switching between history sources", () => {
const setInputValue = vi.fn()
const { rerender } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
)
// Start with task history
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "anthropic",
},
taskHistory: [
{ task: "Task 1", workspace: "/test/workspace" },
{ task: "Task 2", workspace: "/test/workspace" },
],
clineMessages: [],
cwd: "/test/workspace",
})
rerender(<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />)
const textarea = document.querySelector("textarea")!
// Navigate in task history
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Task 1")
// Switch to conversation messages
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
apiConfiguration: {
apiProvider: "anthropic",
},
taskHistory: [],
clineMessages: [
{ type: "say", say: "user_feedback", text: "Message 1", ts: 1000 },
{ type: "say", say: "user_feedback", text: "Message 2", ts: 2000 },
],
cwd: "/test/workspace",
})
setInputValue.mockClear()
rerender(<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />)
// Should start from beginning of conversation history (newest first)
fireEvent.keyDown(textarea, { key: "ArrowUp" })
expect(setInputValue).toHaveBeenCalledWith("Message 2")
})
it("should not navigate history with arrow up when cursor is not at beginning", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Some text here" />,
)
const textarea = container.querySelector("textarea")!
// Set cursor to middle of text (not at beginning)
textarea.setSelectionRange(5, 5)
// Clear any calls from initial render
setInputValue.mockClear()
// Simulate arrow up key press
fireEvent.keyDown(textarea, { key: "ArrowUp" })
// Should not navigate history, allowing default behavior (move cursor to start)
expect(setInputValue).not.toHaveBeenCalled()
})
it("should navigate history with arrow up when cursor is at beginning", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Some text here" />,
)
const textarea = container.querySelector("textarea")!
// Set cursor to beginning of text
textarea.setSelectionRange(0, 0)
// Clear any calls from initial render
setInputValue.mockClear()
// Simulate arrow up key press
fireEvent.keyDown(textarea, { key: "ArrowUp" })
// Should navigate to history since cursor is at beginning
expect(setInputValue).toHaveBeenCalledWith("Third prompt")
})
it("should navigate history with Command+Up when cursor is at beginning", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Some text here" />,
)
const textarea = container.querySelector("textarea")!
// Set cursor to beginning of text
textarea.setSelectionRange(0, 0)
// Clear any calls from initial render
setInputValue.mockClear()
// Simulate Command+Up key press
fireEvent.keyDown(textarea, { key: "ArrowUp", metaKey: true })
// Should navigate to history since cursor is at beginning (same as regular Up)
expect(setInputValue).toHaveBeenCalledWith("Third prompt")
})
it("should not navigate history with Command+Up when cursor is not at beginning", () => {
const setInputValue = vi.fn()
const { container } = render(
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Some text here" />,
)
const textarea = container.querySelector("textarea")!
// Set cursor to middle of text (not at beginning)
textarea.setSelectionRange(5, 5)
// Clear any calls from initial render
setInputValue.mockClear()
// Simulate Command+Up key press
fireEvent.keyDown(textarea, { key: "ArrowUp", metaKey: true })
// Should not navigate history, allowing default behavior (same as regular Up)
expect(setInputValue).not.toHaveBeenCalled()
})
})
})
describe("slash command highlighting", () => {
const mockCommands = [
{ name: "setup", source: "project", description: "Setup the project" },
{ name: "deploy", source: "global", description: "Deploy the application" },
{ name: "test-command", source: "project", description: "Test command with dash" },
]
beforeEach(() => {
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
filePaths: [],
openedTabs: [],
taskHistory: [],
cwd: "/test/workspace",
commands: mockCommands,
})
})
it("should highlight valid slash commands", () => {
const { getByTestId } = render(<ChatTextArea {...defaultProps} inputValue="/setup the project" />)
const highlightLayer = getByTestId("highlight-layer")
expect(highlightLayer).toBeInTheDocument()
// The highlighting is applied via innerHTML, so we need to check the content
// The valid command "/setup" should be highlighted
expect(highlightLayer.innerHTML).toContain('<mark class="mention-context-textarea-highlight">/setup</mark>')
})
it("should not highlight invalid slash commands", () => {
const { getByTestId } = render(<ChatTextArea {...defaultProps} inputValue="/invalid command" />)
const highlightLayer = getByTestId("highlight-layer")
expect(highlightLayer).toBeInTheDocument()
// The invalid command "/invalid" should not be highlighted
expect(highlightLayer.innerHTML).not.toContain(
'<mark class="mention-context-textarea-highlight">/invalid</mark>',
)
// But it should still contain the text without highlighting
expect(highlightLayer.innerHTML).toContain("/invalid")
})
it("should highlight only the command portion, not arguments", () => {
const { getByTestId } = render(<ChatTextArea {...defaultProps} inputValue="/deploy to production" />)
const highlightLayer = getByTestId("highlight-layer")
expect(highlightLayer).toBeInTheDocument()
// Only "/deploy" should be highlighted, not "to production"
expect(highlightLayer.innerHTML).toContain(
'<mark class="mention-context-textarea-highlight">/deploy</mark>',
)
expect(highlightLayer.innerHTML).not.toContain(
'<mark class="mention-context-textarea-highlight">/deploy to production</mark>',
)
})
it("should handle commands with dashes and underscores", () => {
const { getByTestId } = render(<ChatTextArea {...defaultProps} inputValue="/test-command with args" />)
const highlightLayer = getByTestId("highlight-layer")
expect(highlightLayer).toBeInTheDocument()
// The command with dash should be highlighted
expect(highlightLayer.innerHTML).toContain(
'<mark class="mention-context-textarea-highlight">/test-command</mark>',
)
})
it("should be case-sensitive when matching commands", () => {
const { getByTestId } = render(<ChatTextArea {...defaultProps} inputValue="/Setup the project" />)
const highlightLayer = getByTestId("highlight-layer")
expect(highlightLayer).toBeInTheDocument()
// "/Setup" (capital S) should not be highlighted since the command is "setup" (lowercase)
expect(highlightLayer.innerHTML).not.toContain(
'<mark class="mention-context-textarea-highlight">/Setup</mark>',
)
expect(highlightLayer.innerHTML).toContain("/Setup")
})
it("should highlight multiple valid commands in the same text", () => {
const { getByTestId } = render(<ChatTextArea {...defaultProps} inputValue="/setup first then /deploy" />)
const highlightLayer = getByTestId("highlight-layer")
expect(highlightLayer).toBeInTheDocument()
// Both valid commands should be highlighted
expect(highlightLayer.innerHTML).toContain('<mark class="mention-context-textarea-highlight">/setup</mark>')
expect(highlightLayer.innerHTML).toContain(
'<mark class="mention-context-textarea-highlight">/deploy</mark>',
)
})