@@ -1347,5 +1347,154 @@ describe("ChatTextArea", () => {
13471347 // The URL is inserted at cursor position 0, followed by a space.
13481348 expect ( defaultProps . setInputValue ) . toHaveBeenCalledWith ( "https://example.com visit " )
13491349 } )
1350+
1351+ it ( "uses the re-rendered input when inserting command text" , ( ) => {
1352+ const { container, rerender } = render ( < ChatTextArea { ...defaultProps } inputValue = "hello " /> )
1353+ rerender ( < ChatTextArea { ...defaultProps } inputValue = "updated " /> )
1354+ // Reset the DOM cursor: JSDOM moves the selection to the end of a
1355+ // re-rendered value, so anchor the insertion at position 0 explicitly.
1356+ getTextarea ( container ) . setSelectionRange ( 0 , 0 )
1357+
1358+ act ( ( ) => {
1359+ window . dispatchEvent (
1360+ new MessageEvent ( "message" , { data : { type : "insertTextIntoTextarea" , text : "/command" } } ) ,
1361+ )
1362+ } )
1363+
1364+ // A stale effect would still insert into the original "hello " value.
1365+ expect ( defaultProps . setInputValue ) . toHaveBeenCalledWith ( "/command updated " )
1366+ } )
1367+
1368+ it ( "posts the trimmed input on enhance and follows a re-rendered value" , ( ) => {
1369+ const { rerender } = render ( < ChatTextArea { ...defaultProps } inputValue = " Test prompt " /> )
1370+
1371+ fireEvent . click ( getEnhancePromptButton ( ) )
1372+ // Surrounding whitespace must not reach the extension.
1373+ expect ( mockPostMessage ) . toHaveBeenCalledWith ( { type : "enhancePrompt" , text : "Test prompt" } )
1374+
1375+ rerender ( < ChatTextArea { ...defaultProps } inputValue = "updated" /> )
1376+ fireEvent . click ( getEnhancePromptButton ( ) )
1377+ // A stale handler would re-post the original "Test prompt" value.
1378+ expect ( mockPostMessage ) . toHaveBeenLastCalledWith ( { type : "enhancePrompt" , text : "updated" } )
1379+ } )
1380+
1381+ it ( "keeps whitespace-only input hidden from the send button and placeholder" , ( ) => {
1382+ const getSendButton = ( container : HTMLElement ) =>
1383+ Array . from ( container . querySelectorAll ( "button" ) ) . find ( ( b ) => b . querySelector ( ".lucide-send-horizontal" ) )
1384+ const { container, rerender } = render ( < ChatTextArea { ...defaultProps } inputValue = " " /> )
1385+
1386+ // Whitespace-only input has no sendable content, and the placeholder only
1387+ // renders for a truly empty value.
1388+ expect ( getSendButton ( container ) ) . toHaveClass ( "opacity-0" )
1389+ expect ( container . querySelector ( ".left-2.z-30" ) ) . toBeNull ( )
1390+
1391+ // A stale content memo would keep the send button hidden for real text.
1392+ rerender ( < ChatTextArea { ...defaultProps } inputValue = "Text" /> )
1393+ expect ( getSendButton ( container ) ) . toHaveClass ( "opacity-100" )
1394+ } )
1395+
1396+ it ( "moves the cursor to the end of the mention on Backspace at the end of the input" , ( ) => {
1397+ const { container } = render ( < ChatTextArea { ...defaultProps } inputValue = "hello @problems " /> )
1398+ const textarea = getTextarea ( container )
1399+
1400+ textarea . setSelectionRange ( 16 , 16 )
1401+ fireEvent . mouseUp ( textarea )
1402+ fireEvent . keyDown ( textarea , { key : "Backspace" } )
1403+
1404+ // Nothing follows the trailing space, so the space is not deleted; the
1405+ // cursor moves to the end of the mention instead.
1406+ expect ( textarea . selectionStart ) . toBe ( 15 )
1407+ expect ( defaultProps . setInputValue ) . not . toHaveBeenCalled ( )
1408+ } )
1409+
1410+ it ( "does not intercept Backspace when a word follows the mention" , ( ) => {
1411+ const { container } = render ( < ChatTextArea { ...defaultProps } inputValue = "@problems done " /> )
1412+ const textarea = getTextarea ( container )
1413+
1414+ textarea . setSelectionRange ( 15 , 15 )
1415+ fireEvent . mouseUp ( textarea )
1416+ fireEvent . keyDown ( textarea , { key : "Backspace" } )
1417+
1418+ // The mention is not at the end of the inspected prefix, so the
1419+ // selection is untouched.
1420+ expect ( textarea . selectionStart ) . toBe ( 15 )
1421+ expect ( defaultProps . setInputValue ) . not . toHaveBeenCalled ( )
1422+ } )
1423+
1424+ it ( "applies the pending cursor once the pasted value commits" , ( ) => {
1425+ const { container, rerender } = render ( < ChatTextArea { ...defaultProps } inputValue = "visit " /> )
1426+ const textarea = getTextarea ( container )
1427+
1428+ const pasteEvent = new window . Event ( "paste" , { bubbles : true , cancelable : true } )
1429+ Object . defineProperty ( pasteEvent , "clipboardData" , {
1430+ value : { items : [ ] , getData : ( ) => "https://example.com" } ,
1431+ } )
1432+
1433+ act ( ( ) => {
1434+ textarea . dispatchEvent ( pasteEvent )
1435+ // Commit the value the paste handler produced so the pending cursor
1436+ // (0 + 19 + 1 = 20) is applied against the full 26-char value.
1437+ rerender ( < ChatTextArea { ...defaultProps } inputValue = "https://example.com visit " /> )
1438+ } )
1439+
1440+ // A stale effect would never re-apply the intended cursor position.
1441+ expect ( defaultProps . setInputValue ) . toHaveBeenLastCalledWith ( "https://example.com visit " )
1442+ expect ( textarea . selectionStart ) . toBe ( 20 )
1443+ } )
1444+
1445+ it ( "inserts a pasted URL at the cursor and tracks re-rendered input" , ( ) => {
1446+ const { container, rerender } = render ( < ChatTextArea { ...defaultProps } inputValue = "visit this" /> )
1447+ const textarea = getTextarea ( container )
1448+
1449+ textarea . setSelectionRange ( 6 , 6 )
1450+ fireEvent . mouseUp ( textarea )
1451+
1452+ const pasteEvent = new window . Event ( "paste" , { bubbles : true , cancelable : true } )
1453+ Object . defineProperty ( pasteEvent , "clipboardData" , {
1454+ value : { items : [ ] , getData : ( ) => "https://example.com" } ,
1455+ } )
1456+
1457+ act ( ( ) => {
1458+ textarea . dispatchEvent ( pasteEvent )
1459+ } )
1460+
1461+ // The URL splits "visit this" at the cursor instead of reusing the whole
1462+ // value for the tail.
1463+ expect ( defaultProps . setInputValue ) . toHaveBeenLastCalledWith ( "visit https://example.com this" )
1464+
1465+ rerender ( < ChatTextArea { ...defaultProps } inputValue = "changed " /> )
1466+ textarea . setSelectionRange ( 0 , 0 )
1467+ fireEvent . mouseUp ( textarea )
1468+ act ( ( ) => {
1469+ textarea . dispatchEvent ( pasteEvent )
1470+ } )
1471+ // A stale paste handler would repeat the original "visit this" value.
1472+ expect ( defaultProps . setInputValue ) . toHaveBeenLastCalledWith ( "https://example.com changed " )
1473+ } )
1474+
1475+ it ( "re-highlights mentions after the input changes" , ( ) => {
1476+ const { container, rerender } = render ( < ChatTextArea { ...defaultProps } inputValue = "" /> )
1477+ rerender ( < ChatTextArea { ...defaultProps } inputValue = "@problems" /> )
1478+
1479+ // A stale highlight effect would keep the empty highlight from the
1480+ // initial render.
1481+ expect ( container . querySelector ( '[data-testid="highlight-layer"]' ) ?. innerHTML ) . toContain ( "<mark" )
1482+ } )
1483+
1484+ it ( "inserts a dropped path at the cursor and preserves the tail" , ( ) => {
1485+ const { container } = render ( < ChatTextArea { ...defaultProps } inputValue = "abcdef" /> )
1486+ const textarea = getTextarea ( container )
1487+
1488+ textarea . setSelectionRange ( 3 , 3 )
1489+ fireEvent . mouseUp ( textarea )
1490+
1491+ fireEvent . drop ( container . querySelector ( ".chat-text-area" ) ! , {
1492+ dataTransfer : { getData : ( ) => "/some/path" , files : [ ] } ,
1493+ preventDefault : vi . fn ( ) ,
1494+ } )
1495+
1496+ // The remainder after the cursor ("def") is preserved.
1497+ expect ( defaultProps . setInputValue ) . toHaveBeenCalledWith ( "abc/some/path def" )
1498+ } )
13501499 } )
13511500} )
0 commit comments