@@ -445,13 +445,15 @@ describe("ChatTextArea", () => {
445445 } )
446446 } )
447447
448- it ( "should navigate to previous prompt on arrow up" , ( ) => {
448+ it ( "should navigate to previous prompt on arrow up when cursor is at beginning " , ( ) => {
449449 const setInputValue = vi . fn ( )
450450 const { container } = render (
451451 < ChatTextArea { ...defaultProps } setInputValue = { setInputValue } inputValue = "" /> ,
452452 )
453453
454454 const textarea = container . querySelector ( "textarea" ) !
455+ // Ensure cursor is at the beginning
456+ textarea . setSelectionRange ( 0 , 0 )
455457
456458 // Simulate arrow up key press
457459 fireEvent . keyDown ( textarea , { key : "ArrowUp" } )
@@ -755,6 +757,86 @@ describe("ChatTextArea", () => {
755757 fireEvent . keyDown ( textarea , { key : "ArrowUp" } )
756758 expect ( setInputValue ) . toHaveBeenCalledWith ( "Message 2" )
757759 } )
760+
761+ it ( "should not navigate history with arrow up when cursor is not at beginning" , ( ) => {
762+ const setInputValue = vi . fn ( )
763+ const { container } = render (
764+ < ChatTextArea { ...defaultProps } setInputValue = { setInputValue } inputValue = "Some text here" /> ,
765+ )
766+
767+ const textarea = container . querySelector ( "textarea" ) !
768+ // Set cursor to middle of text (not at beginning)
769+ textarea . setSelectionRange ( 5 , 5 )
770+
771+ // Clear any calls from initial render
772+ setInputValue . mockClear ( )
773+
774+ // Simulate arrow up key press
775+ fireEvent . keyDown ( textarea , { key : "ArrowUp" } )
776+
777+ // Should not navigate history, allowing default behavior (move cursor to start)
778+ expect ( setInputValue ) . not . toHaveBeenCalled ( )
779+ } )
780+
781+ it ( "should navigate history with arrow up when cursor is at beginning" , ( ) => {
782+ const setInputValue = vi . fn ( )
783+ const { container } = render (
784+ < ChatTextArea { ...defaultProps } setInputValue = { setInputValue } inputValue = "Some text here" /> ,
785+ )
786+
787+ const textarea = container . querySelector ( "textarea" ) !
788+ // Set cursor to beginning of text
789+ textarea . setSelectionRange ( 0 , 0 )
790+
791+ // Clear any calls from initial render
792+ setInputValue . mockClear ( )
793+
794+ // Simulate arrow up key press
795+ fireEvent . keyDown ( textarea , { key : "ArrowUp" } )
796+
797+ // Should navigate to history since cursor is at beginning
798+ expect ( setInputValue ) . toHaveBeenCalledWith ( "Third prompt" )
799+ } )
800+
801+ it ( "should navigate history with Command+Up when cursor is at beginning" , ( ) => {
802+ const setInputValue = vi . fn ( )
803+ const { container } = render (
804+ < ChatTextArea { ...defaultProps } setInputValue = { setInputValue } inputValue = "Some text here" /> ,
805+ )
806+
807+ const textarea = container . querySelector ( "textarea" ) !
808+ // Set cursor to beginning of text
809+ textarea . setSelectionRange ( 0 , 0 )
810+
811+ // Clear any calls from initial render
812+ setInputValue . mockClear ( )
813+
814+ // Simulate Command+Up key press
815+ fireEvent . keyDown ( textarea , { key : "ArrowUp" , metaKey : true } )
816+
817+ // Should navigate to history since cursor is at beginning (same as regular Up)
818+ expect ( setInputValue ) . toHaveBeenCalledWith ( "Third prompt" )
819+ } )
820+
821+ it ( "should not navigate history with Command+Up when cursor is not at beginning" , ( ) => {
822+ const setInputValue = vi . fn ( )
823+ const { container } = render (
824+ < ChatTextArea { ...defaultProps } setInputValue = { setInputValue } inputValue = "Some text here" /> ,
825+ )
826+
827+ const textarea = container . querySelector ( "textarea" ) !
828+ // Set cursor to middle of text (not at beginning)
829+ textarea . setSelectionRange ( 5 , 5 )
830+
831+ // Clear any calls from initial render
832+ setInputValue . mockClear ( )
833+
834+ // Simulate Command+Up key press
835+ fireEvent . keyDown ( textarea , { key : "ArrowUp" , metaKey : true } )
836+
837+ // Should not navigate history, allowing default behavior (same as regular Up)
838+ expect ( setInputValue ) . not . toHaveBeenCalled ( )
839+ } )
758840 } )
759841 } )
760842
0 commit comments