2025-05-16 11:58:37 -07:00
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
2026-01-16 09:33:13 -08:00
import { describe , it , expect , beforeEach , afterEach , vi } from 'vitest' ;
2025-08-13 17:33:01 -07:00
import stripAnsi from 'strip-ansi' ;
2025-10-28 10:32:15 -07:00
import { act } from 'react' ;
2026-02-09 13:19:51 -08:00
import * as fs from 'node:fs' ;
import * as path from 'node:path' ;
import * as os from 'node:os' ;
2026-01-16 09:33:13 -08:00
import {
renderHook ,
renderHookWithProviders ,
} from '../../../test-utils/render.js' ;
2026-02-09 13:19:51 -08:00
2025-08-26 00:04:53 +02:00
import type {
2025-05-20 16:50:32 -07:00
Viewport ,
TextBuffer ,
2025-08-26 00:04:53 +02:00
TextBufferState ,
TextBufferAction ,
2026-01-22 07:05:38 -08:00
Transformation ,
2025-10-17 16:16:12 -07:00
VisualLayout ,
2025-10-29 18:58:08 -07:00
TextBufferOptions ,
2025-08-26 00:04:53 +02:00
} from './text-buffer.js' ;
import {
useTextBuffer ,
2025-05-20 16:50:32 -07:00
offsetToLogicalPos ,
2025-07-25 15:36:42 -07:00
logicalPosToOffset ,
2025-07-03 17:53:17 -07:00
textBufferReducer ,
2025-08-11 11:58:32 -07:00
findWordEndInLine ,
findNextWordStartInLine ,
2026-02-05 10:29:30 -07:00
findNextBigWordStartInLine ,
findPrevBigWordStartInLine ,
findBigWordEndInLine ,
2025-08-11 11:58:32 -07:00
isWordCharStrict ,
2025-12-23 12:46:09 -08:00
calculateTransformationsForLine ,
calculateTransformedLine ,
getTransformUnderCursor ,
getTransformedImagePath ,
2025-05-20 16:50:32 -07:00
} from './text-buffer.js' ;
2025-08-11 11:58:32 -07:00
import { cpLen } from '../../utils/textUtils.js' ;
2026-02-12 18:27:56 -08:00
import { escapePath } from '@google/gemini-cli-core' ;
2025-05-16 11:58:37 -07:00
2025-10-17 16:16:12 -07:00
const defaultVisualLayout : VisualLayout = {
visualLines : [ '' ] ,
logicalToVisualMap : [ [ [ 0 , 0 ] ] ] ,
visualToLogicalMap : [ [ 0 , 0 ] ] ,
2025-12-23 12:46:09 -08:00
transformedToLogicalMaps : [ [ ] ] ,
visualToTransformedMap : [ ] ,
2025-10-17 16:16:12 -07:00
} ;
2025-07-03 17:53:17 -07:00
const initialState : TextBufferState = {
lines : [ '' ] ,
cursorRow : 0 ,
cursorCol : 0 ,
preferredCol : null ,
undoStack : [ ] ,
redoStack : [ ] ,
clipboard : null ,
selectionAnchor : null ,
2025-10-17 16:16:12 -07:00
viewportWidth : 80 ,
viewportHeight : 24 ,
2025-12-23 12:46:09 -08:00
transformationsByLine : [ [ ] ] ,
2025-10-17 16:16:12 -07:00
visualLayout : defaultVisualLayout ,
2026-01-21 21:09:24 -05:00
pastedContent : { } ,
2026-01-27 06:19:54 -08:00
expandedPaste : null ,
2026-03-11 11:43:42 -07:00
yankRegister : null ,
2025-07-03 17:53:17 -07:00
} ;
2026-01-22 07:05:38 -08:00
/**
* Helper to create a TextBufferState with properly calculated transformations.
*/
function createStateWithTransformations (
partial : Partial < TextBufferState > ,
) : TextBufferState {
const state = { . . . initialState , . . . partial } ;
return {
. . . state ,
transformationsByLine : state.lines.map ( ( l ) = >
calculateTransformationsForLine ( l ) ,
) ,
} ;
}
2025-07-03 17:53:17 -07:00
describe ( 'textBufferReducer' , ( ) = > {
2026-01-16 09:33:13 -08:00
afterEach ( ( ) = > {
vi . restoreAllMocks ( ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should return the initial state if state is undefined' , async ( ) = > {
2025-07-03 17:53:17 -07:00
const action = { type : 'unknown_action' } as unknown as TextBufferAction ;
const state = textBufferReducer ( initialState , action ) ;
2025-07-31 16:16:29 -07:00
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( state ) . toEqual ( initialState ) ;
} ) ;
2026-02-05 10:29:30 -07:00
describe ( 'Big Word Navigation Helpers' , ( ) = > {
describe ( 'findNextBigWordStartInLine (W)' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should skip non-whitespace and then whitespace' , async ( ) = > {
2026-02-05 10:29:30 -07:00
expect ( findNextBigWordStartInLine ( 'hello world' , 0 ) ) . toBe ( 6 ) ;
expect ( findNextBigWordStartInLine ( 'hello.world test' , 0 ) ) . toBe ( 12 ) ;
expect ( findNextBigWordStartInLine ( ' test' , 0 ) ) . toBe ( 3 ) ;
expect ( findNextBigWordStartInLine ( 'test ' , 0 ) ) . toBe ( null ) ;
} ) ;
} ) ;
describe ( 'findPrevBigWordStartInLine (B)' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should skip whitespace backwards then non-whitespace' , async ( ) = > {
2026-02-05 10:29:30 -07:00
expect ( findPrevBigWordStartInLine ( 'hello world' , 6 ) ) . toBe ( 0 ) ;
expect ( findPrevBigWordStartInLine ( 'hello.world test' , 12 ) ) . toBe ( 0 ) ;
expect ( findPrevBigWordStartInLine ( ' test' , 3 ) ) . toBe ( null ) ; // At start of word
expect ( findPrevBigWordStartInLine ( ' test' , 4 ) ) . toBe ( 3 ) ; // Inside word
expect ( findPrevBigWordStartInLine ( 'test ' , 6 ) ) . toBe ( 0 ) ;
} ) ;
} ) ;
describe ( 'findBigWordEndInLine (E)' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should find end of current big word' , async ( ) = > {
2026-02-05 10:29:30 -07:00
expect ( findBigWordEndInLine ( 'hello world' , 0 ) ) . toBe ( 4 ) ;
expect ( findBigWordEndInLine ( 'hello.world test' , 0 ) ) . toBe ( 10 ) ;
expect ( findBigWordEndInLine ( 'hello.world test' , 11 ) ) . toBe ( 15 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should skip whitespace if currently on whitespace' , async ( ) = > {
2026-02-05 10:29:30 -07:00
expect ( findBigWordEndInLine ( 'hello world' , 5 ) ) . toBe ( 12 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should find next big word end if at end of current' , async ( ) = > {
2026-02-05 10:29:30 -07:00
expect ( findBigWordEndInLine ( 'hello world' , 4 ) ) . toBe ( 10 ) ;
} ) ;
} ) ;
} ) ;
2025-07-03 17:53:17 -07:00
describe ( 'set_text action' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should set new text and move cursor to the end' , async ( ) = > {
2025-07-03 17:53:17 -07:00
const action : TextBufferAction = {
type : 'set_text' ,
payload : 'hello\nworld' ,
} ;
const state = textBufferReducer ( initialState , action ) ;
2025-07-31 16:16:29 -07:00
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( state . lines ) . toEqual ( [ 'hello' , 'world' ] ) ;
expect ( state . cursorRow ) . toBe ( 1 ) ;
expect ( state . cursorCol ) . toBe ( 5 ) ;
expect ( state . undoStack . length ) . toBe ( 1 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not create an undo snapshot if pushToUndo is false' , async ( ) = > {
2025-07-03 17:53:17 -07:00
const action : TextBufferAction = {
type : 'set_text' ,
payload : 'no undo' ,
pushToUndo : false ,
} ;
const state = textBufferReducer ( initialState , action ) ;
2025-07-31 16:16:29 -07:00
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( state . lines ) . toEqual ( [ 'no undo' ] ) ;
expect ( state . undoStack . length ) . toBe ( 0 ) ;
} ) ;
} ) ;
describe ( 'insert action' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should insert a character' , async ( ) = > {
2025-07-03 17:53:17 -07:00
const action : TextBufferAction = { type : 'insert' , payload : 'a' } ;
const state = textBufferReducer ( initialState , action ) ;
2025-07-31 16:16:29 -07:00
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( state . lines ) . toEqual ( [ 'a' ] ) ;
expect ( state . cursorCol ) . toBe ( 1 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should insert a newline' , async ( ) = > {
2025-07-03 17:53:17 -07:00
const stateWithText = { . . . initialState , lines : [ 'hello' ] } ;
const action : TextBufferAction = { type : 'insert' , payload : '\n' } ;
const state = textBufferReducer ( stateWithText , action ) ;
2025-07-31 16:16:29 -07:00
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( state . lines ) . toEqual ( [ '' , 'hello' ] ) ;
expect ( state . cursorRow ) . toBe ( 1 ) ;
expect ( state . cursorCol ) . toBe ( 0 ) ;
} ) ;
} ) ;
2025-10-29 18:58:08 -07:00
describe ( 'insert action with options' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should filter input using inputFilter option' , async ( ) = > {
2025-10-29 18:58:08 -07:00
const action : TextBufferAction = { type : 'insert' , payload : 'a1b2c3' } ;
const options : TextBufferOptions = {
inputFilter : ( text ) = > text . replace ( /[0-9]/g , '' ) ,
} ;
const state = textBufferReducer ( initialState , action , options ) ;
expect ( state . lines ) . toEqual ( [ 'abc' ] ) ;
expect ( state . cursorCol ) . toBe ( 3 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should strip newlines when singleLine option is true' , async ( ) = > {
2025-10-29 18:58:08 -07:00
const action : TextBufferAction = {
type : 'insert' ,
payload : 'hello\nworld' ,
} ;
const options : TextBufferOptions = { singleLine : true } ;
const state = textBufferReducer ( initialState , action , options ) ;
expect ( state . lines ) . toEqual ( [ 'helloworld' ] ) ;
expect ( state . cursorCol ) . toBe ( 10 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should apply both inputFilter and singleLine options' , async ( ) = > {
2025-10-29 18:58:08 -07:00
const action : TextBufferAction = {
type : 'insert' ,
payload : 'h\ne\nl\nl\no\n1\n2\n3' ,
} ;
const options : TextBufferOptions = {
singleLine : true ,
inputFilter : ( text ) = > text . replace ( /[0-9]/g , '' ) ,
} ;
const state = textBufferReducer ( initialState , action , options ) ;
expect ( state . lines ) . toEqual ( [ 'hello' ] ) ;
expect ( state . cursorCol ) . toBe ( 5 ) ;
} ) ;
} ) ;
2026-01-21 21:09:24 -05:00
describe ( 'add_pasted_content action' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should add content to pastedContent Record' , async ( ) = > {
2026-01-21 21:09:24 -05:00
const action : TextBufferAction = {
type : 'add_pasted_content' ,
payload : { id : '[Pasted Text: 6 lines]' , text : 'large content' } ,
} ;
const state = textBufferReducer ( initialState , action ) ;
expect ( state . pastedContent ) . toEqual ( {
'[Pasted Text: 6 lines]' : 'large content' ,
} ) ;
} ) ;
} ) ;
2025-07-03 17:53:17 -07:00
describe ( 'backspace action' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should remove a character' , async ( ) = > {
2025-07-03 17:53:17 -07:00
const stateWithText : TextBufferState = {
. . . initialState ,
lines : [ 'a' ] ,
cursorRow : 0 ,
cursorCol : 1 ,
} ;
const action : TextBufferAction = { type : 'backspace' } ;
const state = textBufferReducer ( stateWithText , action ) ;
2025-07-31 16:16:29 -07:00
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( state . lines ) . toEqual ( [ '' ] ) ;
expect ( state . cursorCol ) . toBe ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should join lines if at the beginning of a line' , async ( ) = > {
2025-07-03 17:53:17 -07:00
const stateWithText : TextBufferState = {
. . . initialState ,
lines : [ 'hello' , 'world' ] ,
cursorRow : 1 ,
cursorCol : 0 ,
} ;
const action : TextBufferAction = { type : 'backspace' } ;
const state = textBufferReducer ( stateWithText , action ) ;
2025-07-31 16:16:29 -07:00
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( state . lines ) . toEqual ( [ 'helloworld' ] ) ;
expect ( state . cursorRow ) . toBe ( 0 ) ;
expect ( state . cursorCol ) . toBe ( 5 ) ;
} ) ;
} ) ;
2026-01-21 21:09:24 -05:00
describe ( 'atomic placeholder deletion' , ( ) = > {
describe ( 'paste placeholders' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'backspace at end of paste placeholder removes entire placeholder' , async ( ) = > {
2026-01-21 21:09:24 -05:00
const placeholder = '[Pasted Text: 6 lines]' ;
2026-01-22 07:05:38 -08:00
const stateWithPlaceholder = createStateWithTransformations ( {
2026-01-21 21:09:24 -05:00
lines : [ placeholder ] ,
cursorRow : 0 ,
cursorCol : placeholder.length , // cursor at end
pastedContent : {
[ placeholder ] : 'line1\nline2\nline3\nline4\nline5\nline6' ,
} ,
2026-01-22 07:05:38 -08:00
} ) ;
2026-01-21 21:09:24 -05:00
const action : TextBufferAction = { type : 'backspace' } ;
const state = textBufferReducer ( stateWithPlaceholder , action ) ;
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
expect ( state . lines ) . toEqual ( [ '' ] ) ;
expect ( state . cursorCol ) . toBe ( 0 ) ;
// pastedContent should be cleaned up
expect ( state . pastedContent [ placeholder ] ) . toBeUndefined ( ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'delete at start of paste placeholder removes entire placeholder' , async ( ) = > {
2026-01-21 21:09:24 -05:00
const placeholder = '[Pasted Text: 6 lines]' ;
2026-01-22 07:05:38 -08:00
const stateWithPlaceholder = createStateWithTransformations ( {
2026-01-21 21:09:24 -05:00
lines : [ placeholder ] ,
cursorRow : 0 ,
cursorCol : 0 , // cursor at start
pastedContent : {
[ placeholder ] : 'line1\nline2\nline3\nline4\nline5\nline6' ,
} ,
2026-01-22 07:05:38 -08:00
} ) ;
2026-01-21 21:09:24 -05:00
const action : TextBufferAction = { type : 'delete' } ;
const state = textBufferReducer ( stateWithPlaceholder , action ) ;
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
expect ( state . lines ) . toEqual ( [ '' ] ) ;
expect ( state . cursorCol ) . toBe ( 0 ) ;
// pastedContent should be cleaned up
expect ( state . pastedContent [ placeholder ] ) . toBeUndefined ( ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'backspace inside paste placeholder does normal deletion' , async ( ) = > {
2026-01-21 21:09:24 -05:00
const placeholder = '[Pasted Text: 6 lines]' ;
2026-01-22 07:05:38 -08:00
const stateWithPlaceholder = createStateWithTransformations ( {
2026-01-21 21:09:24 -05:00
lines : [ placeholder ] ,
cursorRow : 0 ,
cursorCol : 10 , // cursor in middle
pastedContent : {
[ placeholder ] : 'line1\nline2\nline3\nline4\nline5\nline6' ,
} ,
2026-01-22 07:05:38 -08:00
} ) ;
2026-01-21 21:09:24 -05:00
const action : TextBufferAction = { type : 'backspace' } ;
const state = textBufferReducer ( stateWithPlaceholder , action ) ;
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
// Should only delete one character
expect ( state . lines [ 0 ] . length ) . toBe ( placeholder . length - 1 ) ;
expect ( state . cursorCol ) . toBe ( 9 ) ;
// pastedContent should NOT be cleaned up (placeholder is broken)
expect ( state . pastedContent [ placeholder ] ) . toBeDefined ( ) ;
} ) ;
} ) ;
describe ( 'image placeholders' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'backspace at end of image path removes entire path' , async ( ) = > {
2026-01-21 21:09:24 -05:00
const imagePath = '@test.png' ;
2026-01-22 07:05:38 -08:00
const stateWithImage = createStateWithTransformations ( {
2026-01-21 21:09:24 -05:00
lines : [ imagePath ] ,
cursorRow : 0 ,
cursorCol : imagePath.length , // cursor at end
2026-01-22 07:05:38 -08:00
} ) ;
2026-01-21 21:09:24 -05:00
const action : TextBufferAction = { type : 'backspace' } ;
const state = textBufferReducer ( stateWithImage , action ) ;
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
expect ( state . lines ) . toEqual ( [ '' ] ) ;
expect ( state . cursorCol ) . toBe ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'delete at start of image path removes entire path' , async ( ) = > {
2026-01-21 21:09:24 -05:00
const imagePath = '@test.png' ;
2026-01-22 07:05:38 -08:00
const stateWithImage = createStateWithTransformations ( {
2026-01-21 21:09:24 -05:00
lines : [ imagePath ] ,
cursorRow : 0 ,
cursorCol : 0 , // cursor at start
2026-01-22 07:05:38 -08:00
} ) ;
2026-01-21 21:09:24 -05:00
const action : TextBufferAction = { type : 'delete' } ;
const state = textBufferReducer ( stateWithImage , action ) ;
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
expect ( state . lines ) . toEqual ( [ '' ] ) ;
expect ( state . cursorCol ) . toBe ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'backspace inside image path does normal deletion' , async ( ) = > {
2026-01-21 21:09:24 -05:00
const imagePath = '@test.png' ;
2026-01-22 07:05:38 -08:00
const stateWithImage = createStateWithTransformations ( {
2026-01-21 21:09:24 -05:00
lines : [ imagePath ] ,
cursorRow : 0 ,
cursorCol : 5 , // cursor in middle
2026-01-22 07:05:38 -08:00
} ) ;
2026-01-21 21:09:24 -05:00
const action : TextBufferAction = { type : 'backspace' } ;
const state = textBufferReducer ( stateWithImage , action ) ;
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
// Should only delete one character
expect ( state . lines [ 0 ] . length ) . toBe ( imagePath . length - 1 ) ;
expect ( state . cursorCol ) . toBe ( 4 ) ;
} ) ;
} ) ;
describe ( 'undo behavior' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'undo after placeholder deletion restores everything' , async ( ) = > {
2026-01-21 21:09:24 -05:00
const placeholder = '[Pasted Text: 6 lines]' ;
const pasteContent = 'line1\nline2\nline3\nline4\nline5\nline6' ;
2026-01-22 07:05:38 -08:00
const stateWithPlaceholder = createStateWithTransformations ( {
2026-01-21 21:09:24 -05:00
lines : [ placeholder ] ,
cursorRow : 0 ,
cursorCol : placeholder.length ,
pastedContent : { [ placeholder ] : pasteContent } ,
2026-01-22 07:05:38 -08:00
} ) ;
2026-01-21 21:09:24 -05:00
// Delete the placeholder
const deleteAction : TextBufferAction = { type : 'backspace' } ;
const stateAfterDelete = textBufferReducer (
stateWithPlaceholder ,
deleteAction ,
) ;
expect ( stateAfterDelete . lines ) . toEqual ( [ '' ] ) ;
expect ( stateAfterDelete . pastedContent [ placeholder ] ) . toBeUndefined ( ) ;
// Undo should restore
const undoAction : TextBufferAction = { type : 'undo' } ;
const stateAfterUndo = textBufferReducer ( stateAfterDelete , undoAction ) ;
expect ( stateAfterUndo ) . toHaveOnlyValidCharacters ( ) ;
expect ( stateAfterUndo . lines ) . toEqual ( [ placeholder ] ) ;
expect ( stateAfterUndo . pastedContent [ placeholder ] ) . toBe ( pasteContent ) ;
} ) ;
} ) ;
} ) ;
2025-07-03 17:53:17 -07:00
describe ( 'undo/redo actions' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should undo and redo a change' , async ( ) = > {
2025-07-03 17:53:17 -07:00
// 1. Insert text
const insertAction : TextBufferAction = {
type : 'insert' ,
payload : 'test' ,
} ;
const stateAfterInsert = textBufferReducer ( initialState , insertAction ) ;
2025-07-31 16:16:29 -07:00
expect ( stateAfterInsert ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( stateAfterInsert . lines ) . toEqual ( [ 'test' ] ) ;
expect ( stateAfterInsert . undoStack . length ) . toBe ( 1 ) ;
// 2. Undo
const undoAction : TextBufferAction = { type : 'undo' } ;
const stateAfterUndo = textBufferReducer ( stateAfterInsert , undoAction ) ;
2025-07-31 16:16:29 -07:00
expect ( stateAfterUndo ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( stateAfterUndo . lines ) . toEqual ( [ '' ] ) ;
expect ( stateAfterUndo . undoStack . length ) . toBe ( 0 ) ;
expect ( stateAfterUndo . redoStack . length ) . toBe ( 1 ) ;
// 3. Redo
const redoAction : TextBufferAction = { type : 'redo' } ;
const stateAfterRedo = textBufferReducer ( stateAfterUndo , redoAction ) ;
2025-07-31 16:16:29 -07:00
expect ( stateAfterRedo ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( stateAfterRedo . lines ) . toEqual ( [ 'test' ] ) ;
expect ( stateAfterRedo . undoStack . length ) . toBe ( 1 ) ;
expect ( stateAfterRedo . redoStack . length ) . toBe ( 0 ) ;
} ) ;
} ) ;
describe ( 'create_undo_snapshot action' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should create a snapshot without changing state' , async ( ) = > {
2025-07-03 17:53:17 -07:00
const stateWithText : TextBufferState = {
. . . initialState ,
lines : [ 'hello' ] ,
cursorRow : 0 ,
cursorCol : 5 ,
} ;
const action : TextBufferAction = { type : 'create_undo_snapshot' } ;
const state = textBufferReducer ( stateWithText , action ) ;
2025-07-31 16:16:29 -07:00
expect ( state ) . toHaveOnlyValidCharacters ( ) ;
2025-07-03 17:53:17 -07:00
expect ( state . lines ) . toEqual ( [ 'hello' ] ) ;
expect ( state . cursorRow ) . toBe ( 0 ) ;
expect ( state . cursorCol ) . toBe ( 5 ) ;
expect ( state . undoStack . length ) . toBe ( 1 ) ;
expect ( state . undoStack [ 0 ] . lines ) . toEqual ( [ 'hello' ] ) ;
expect ( state . undoStack [ 0 ] . cursorRow ) . toBe ( 0 ) ;
expect ( state . undoStack [ 0 ] . cursorCol ) . toBe ( 5 ) ;
} ) ;
} ) ;
2025-09-02 21:00:41 -04:00
describe ( 'delete_word_left action' , ( ) = > {
2025-11-03 23:40:57 +05:30
const createSingleLineState = (
text : string ,
col : number ,
) : TextBufferState = > ( {
. . . initialState ,
lines : [ text ] ,
cursorRow : 0 ,
cursorCol : col ,
} ) ;
it . each ( [
{
input : 'hello world' ,
2025-09-02 21:00:41 -04:00
cursorCol : 11 ,
2025-11-03 23:40:57 +05:30
expectedLines : [ 'hello ' ] ,
expectedCol : 6 ,
desc : 'simple word' ,
} ,
{
input : 'path/to/file' ,
2025-09-02 21:00:41 -04:00
cursorCol : 12 ,
2025-11-03 23:40:57 +05:30
expectedLines : [ 'path/to/' ] ,
expectedCol : 8 ,
desc : 'path segment' ,
} ,
{
input : 'variable_name' ,
2025-09-02 21:00:41 -04:00
cursorCol : 13 ,
2025-11-03 23:40:57 +05:30
expectedLines : [ 'variable_' ] ,
expectedCol : 9 ,
desc : 'variable_name parts' ,
} ,
] ) (
'should delete $desc' ,
( { input , cursorCol , expectedLines , expectedCol } ) = > {
const state = textBufferReducer (
createSingleLineState ( input , cursorCol ) ,
{ type : 'delete_word_left' } ,
) ;
expect ( state . lines ) . toEqual ( expectedLines ) ;
expect ( state . cursorCol ) . toBe ( expectedCol ) ;
} ,
) ;
2025-09-02 21:00:41 -04:00
2026-03-20 20:08:29 +00:00
it ( 'should act like backspace at the beginning of a line' , async ( ) = > {
2025-09-02 21:00:41 -04:00
const stateWithText : TextBufferState = {
. . . initialState ,
lines : [ 'hello' , 'world' ] ,
cursorRow : 1 ,
cursorCol : 0 ,
} ;
2025-11-03 23:40:57 +05:30
const state = textBufferReducer ( stateWithText , {
type : 'delete_word_left' ,
} ) ;
2025-09-02 21:00:41 -04:00
expect ( state . lines ) . toEqual ( [ 'helloworld' ] ) ;
expect ( state . cursorRow ) . toBe ( 0 ) ;
expect ( state . cursorCol ) . toBe ( 5 ) ;
} ) ;
} ) ;
describe ( 'delete_word_right action' , ( ) = > {
2025-11-03 23:40:57 +05:30
const createSingleLineState = (
text : string ,
col : number ,
) : TextBufferState = > ( {
. . . initialState ,
lines : [ text ] ,
cursorRow : 0 ,
cursorCol : col ,
} ) ;
it . each ( [
{
input : 'hello world' ,
2025-09-02 21:00:41 -04:00
cursorCol : 0 ,
2025-11-03 23:40:57 +05:30
expectedLines : [ 'world' ] ,
expectedCol : 0 ,
desc : 'simple word' ,
} ,
{
input : 'variable_name' ,
cursorCol : 0 ,
expectedLines : [ '_name' ] ,
expectedCol : 0 ,
desc : 'variable_name parts' ,
} ,
] ) (
'should delete $desc' ,
( { input , cursorCol , expectedLines , expectedCol } ) = > {
const state = textBufferReducer (
createSingleLineState ( input , cursorCol ) ,
{ type : 'delete_word_right' } ,
) ;
expect ( state . lines ) . toEqual ( expectedLines ) ;
expect ( state . cursorCol ) . toBe ( expectedCol ) ;
} ,
) ;
2026-03-20 20:08:29 +00:00
it ( 'should delete path segments progressively' , async ( ) = > {
2025-09-02 21:00:41 -04:00
const stateWithText : TextBufferState = {
. . . initialState ,
lines : [ 'path/to/file' ] ,
cursorRow : 0 ,
cursorCol : 0 ,
} ;
2025-11-03 23:40:57 +05:30
let state = textBufferReducer ( stateWithText , {
type : 'delete_word_right' ,
} ) ;
2025-09-02 21:00:41 -04:00
expect ( state . lines ) . toEqual ( [ '/to/file' ] ) ;
2025-11-03 23:40:57 +05:30
state = textBufferReducer ( state , { type : 'delete_word_right' } ) ;
2025-09-02 21:00:41 -04:00
expect ( state . lines ) . toEqual ( [ 'to/file' ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should act like delete at the end of a line' , async ( ) = > {
2025-09-02 21:00:41 -04:00
const stateWithText : TextBufferState = {
. . . initialState ,
lines : [ 'hello' , 'world' ] ,
cursorRow : 0 ,
cursorCol : 5 ,
} ;
2025-11-03 23:40:57 +05:30
const state = textBufferReducer ( stateWithText , {
type : 'delete_word_right' ,
} ) ;
2025-09-02 21:00:41 -04:00
expect ( state . lines ) . toEqual ( [ 'helloworld' ] ) ;
expect ( state . cursorRow ) . toBe ( 0 ) ;
expect ( state . cursorCol ) . toBe ( 5 ) ;
} ) ;
} ) ;
2026-01-26 21:59:09 -05:00
2026-03-17 16:16:26 -04:00
describe ( 'kill_line_left action' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should clean up pastedContent when deleting a placeholder line-left' , async ( ) = > {
2026-03-17 16:16:26 -04:00
const placeholder = '[Pasted Text: 6 lines]' ;
const stateWithPlaceholder = createStateWithTransformations ( {
lines : [ placeholder ] ,
cursorRow : 0 ,
cursorCol : cpLen ( placeholder ) ,
pastedContent : {
[ placeholder ] : 'line1\nline2\nline3\nline4\nline5\nline6' ,
} ,
} ) ;
const state = textBufferReducer ( stateWithPlaceholder , {
type : 'kill_line_left' ,
} ) ;
expect ( state . lines ) . toEqual ( [ '' ] ) ;
expect ( state . cursorCol ) . toBe ( 0 ) ;
expect ( Object . keys ( state . pastedContent ) ) . toHaveLength ( 0 ) ;
} ) ;
} ) ;
describe ( 'kill_line_right action' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should reset preferredCol when deleting to end of line' , async ( ) = > {
2026-03-17 16:16:26 -04:00
const stateWithText : TextBufferState = {
. . . initialState ,
lines : [ 'hello world' ] ,
cursorRow : 0 ,
cursorCol : 5 ,
preferredCol : 9 ,
} ;
const state = textBufferReducer ( stateWithText , {
type : 'kill_line_right' ,
} ) ;
expect ( state . lines ) . toEqual ( [ 'hello' ] ) ;
expect ( state . preferredCol ) . toBe ( null ) ;
} ) ;
} ) ;
2026-01-26 21:59:09 -05:00
describe ( 'toggle_paste_expansion action' , ( ) = > {
const placeholder = '[Pasted Text: 6 lines]' ;
const content = 'line1\nline2\nline3\nline4\nline5\nline6' ;
2026-03-20 20:08:29 +00:00
it ( 'should expand a placeholder correctly' , async ( ) = > {
2026-01-26 21:59:09 -05:00
const stateWithPlaceholder = createStateWithTransformations ( {
lines : [ 'prefix ' + placeholder + ' suffix' ] ,
cursorRow : 0 ,
cursorCol : 0 ,
pastedContent : { [ placeholder ] : content } ,
} ) ;
const action : TextBufferAction = {
type : 'toggle_paste_expansion' ,
2026-01-27 06:19:54 -08:00
payload : { id : placeholder , row : 0 , col : 7 } ,
2026-01-26 21:59:09 -05:00
} ;
const state = textBufferReducer ( stateWithPlaceholder , action ) ;
expect ( state . lines ) . toEqual ( [
'prefix line1' ,
'line2' ,
'line3' ,
'line4' ,
'line5' ,
'line6 suffix' ,
] ) ;
2026-01-27 06:19:54 -08:00
expect ( state . expandedPaste ? . id ) . toBe ( placeholder ) ;
const info = state . expandedPaste ;
2026-01-26 21:59:09 -05:00
expect ( info ) . toEqual ( {
2026-01-27 06:19:54 -08:00
id : placeholder ,
2026-01-26 21:59:09 -05:00
startLine : 0 ,
lineCount : 6 ,
prefix : 'prefix ' ,
suffix : ' suffix' ,
} ) ;
// Cursor should be at the end of expanded content (before suffix)
expect ( state . cursorRow ) . toBe ( 5 ) ;
expect ( state . cursorCol ) . toBe ( 5 ) ; // length of 'line6'
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should collapse an expanded placeholder correctly' , async ( ) = > {
2026-01-26 21:59:09 -05:00
const expandedState = createStateWithTransformations ( {
lines : [
'prefix line1' ,
'line2' ,
'line3' ,
'line4' ,
'line5' ,
'line6 suffix' ,
] ,
cursorRow : 5 ,
cursorCol : 5 ,
pastedContent : { [ placeholder ] : content } ,
2026-01-27 06:19:54 -08:00
expandedPaste : {
id : placeholder ,
startLine : 0 ,
lineCount : 6 ,
prefix : 'prefix ' ,
suffix : ' suffix' ,
} ,
2026-01-26 21:59:09 -05:00
} ) ;
const action : TextBufferAction = {
type : 'toggle_paste_expansion' ,
2026-01-27 06:19:54 -08:00
payload : { id : placeholder , row : 0 , col : 7 } ,
2026-01-26 21:59:09 -05:00
} ;
const state = textBufferReducer ( expandedState , action ) ;
expect ( state . lines ) . toEqual ( [ 'prefix ' + placeholder + ' suffix' ] ) ;
2026-01-27 06:19:54 -08:00
expect ( state . expandedPaste ) . toBeNull ( ) ;
2026-01-26 21:59:09 -05:00
// Cursor should be at the end of the collapsed placeholder
expect ( state . cursorRow ) . toBe ( 0 ) ;
expect ( state . cursorCol ) . toBe ( ( 'prefix ' + placeholder ) . length ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should expand single-line content correctly' , async ( ) = > {
2026-01-26 21:59:09 -05:00
const singleLinePlaceholder = '[Pasted Text: 10 chars]' ;
const singleLineContent = 'some text' ;
const stateWithPlaceholder = createStateWithTransformations ( {
lines : [ singleLinePlaceholder ] ,
cursorRow : 0 ,
cursorCol : 0 ,
pastedContent : { [ singleLinePlaceholder ] : singleLineContent } ,
} ) ;
const state = textBufferReducer ( stateWithPlaceholder , {
type : 'toggle_paste_expansion' ,
2026-01-27 06:19:54 -08:00
payload : { id : singleLinePlaceholder , row : 0 , col : 0 } ,
2026-01-26 21:59:09 -05:00
} ) ;
expect ( state . lines ) . toEqual ( [ 'some text' ] ) ;
expect ( state . cursorRow ) . toBe ( 0 ) ;
expect ( state . cursorCol ) . toBe ( 9 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should return current state if placeholder ID not found in pastedContent' , async ( ) = > {
2026-01-26 21:59:09 -05:00
const action : TextBufferAction = {
type : 'toggle_paste_expansion' ,
2026-01-27 06:19:54 -08:00
payload : { id : 'unknown' , row : 0 , col : 0 } ,
2026-01-26 21:59:09 -05:00
} ;
const state = textBufferReducer ( initialState , action ) ;
expect ( state ) . toBe ( initialState ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should preserve expandedPaste when lines change from edits outside the region' , async ( ) = > {
2026-01-26 21:59:09 -05:00
// Start with an expanded paste at line 0 (3 lines long)
const placeholder = '[Pasted Text: 3 lines]' ;
const expandedState = createStateWithTransformations ( {
lines : [ 'line1' , 'line2' , 'line3' , 'suffix' ] ,
cursorRow : 3 ,
cursorCol : 0 ,
pastedContent : { [ placeholder ] : 'line1\nline2\nline3' } ,
2026-01-27 06:19:54 -08:00
expandedPaste : {
id : placeholder ,
startLine : 0 ,
lineCount : 3 ,
prefix : '' ,
suffix : '' ,
} ,
2026-01-26 21:59:09 -05:00
} ) ;
2026-01-27 06:19:54 -08:00
expect ( expandedState . expandedPaste ) . not . toBeNull ( ) ;
2026-01-26 21:59:09 -05:00
// Insert a newline at the end - this changes lines but is OUTSIDE the expanded region
const stateAfterInsert = textBufferReducer ( expandedState , {
type : 'insert' ,
payload : '\n' ,
} ) ;
2026-01-27 06:19:54 -08:00
// Lines changed, but expandedPaste should be PRESERVED and optionally shifted (no shift here since edit is after)
expect ( stateAfterInsert . expandedPaste ) . not . toBeNull ( ) ;
expect ( stateAfterInsert . expandedPaste ? . id ) . toBe ( placeholder ) ;
2026-01-26 21:59:09 -05:00
} ) ;
} ) ;
2025-07-03 17:53:17 -07:00
} ) ;
2025-07-31 16:16:29 -07:00
const getBufferState = ( result : { current : TextBuffer } ) = > {
expect ( result . current ) . toHaveOnlyValidCharacters ( ) ;
return {
text : result.current.text ,
lines : [ . . . result . current . lines ] , // Clone for safety
cursor : [ . . . result . current . cursor ] as [ number , number ] ,
allVisualLines : [ . . . result . current . allVisualLines ] ,
viewportVisualLines : [ . . . result . current . viewportVisualLines ] ,
visualCursor : [ . . . result . current . visualCursor ] as [ number , number ] ,
visualScrollRow : result.current.visualScrollRow ,
preferredCol : result.current.preferredCol ,
} ;
} ;
2025-05-16 11:58:37 -07:00
describe ( 'useTextBuffer' , ( ) = > {
let viewport : Viewport ;
beforeEach ( ( ) = > {
viewport = { width : 10 , height : 3 } ; // Default viewport for tests
} ) ;
2026-01-16 09:33:13 -08:00
afterEach ( ( ) = > {
vi . restoreAllMocks ( ) ;
} ) ;
2025-05-16 11:58:37 -07:00
describe ( 'Initialization' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should initialize with empty text and cursor at (0,0) by default' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-05-16 11:58:37 -07:00
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( '' ) ;
expect ( state . lines ) . toEqual ( [ '' ] ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 0 ] ) ;
expect ( state . allVisualLines ) . toEqual ( [ '' ] ) ;
expect ( state . viewportVisualLines ) . toEqual ( [ '' ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 0 , 0 ] ) ;
expect ( state . visualScrollRow ) . toBe ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should initialize with provided initialText' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'hello' ,
viewport ,
} ) ,
2025-05-16 11:58:37 -07:00
) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'hello' ) ;
expect ( state . lines ) . toEqual ( [ 'hello' ] ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 0 ] ) ; // Default cursor if offset not given
expect ( state . allVisualLines ) . toEqual ( [ 'hello' ] ) ;
expect ( state . viewportVisualLines ) . toEqual ( [ 'hello' ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 0 , 0 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should initialize with initialText and initialCursorOffset' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-05-16 11:58:37 -07:00
useTextBuffer ( {
initialText : 'hello\nworld' ,
initialCursorOffset : 7 , // Should be at 'o' in 'world'
viewport ,
} ) ,
) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'hello\nworld' ) ;
expect ( state . lines ) . toEqual ( [ 'hello' , 'world' ] ) ;
expect ( state . cursor ) . toEqual ( [ 1 , 1 ] ) ; // Logical cursor at 'o' in "world"
expect ( state . allVisualLines ) . toEqual ( [ 'hello' , 'world' ] ) ;
expect ( state . viewportVisualLines ) . toEqual ( [ 'hello' , 'world' ] ) ;
expect ( state . visualCursor [ 0 ] ) . toBe ( 1 ) ; // On the second visual line
expect ( state . visualCursor [ 1 ] ) . toBe ( 1 ) ; // At 'o' in "world"
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should wrap visual lines' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-05-16 11:58:37 -07:00
useTextBuffer ( {
initialText : 'The quick brown fox jumps over the lazy dog.' ,
initialCursorOffset : 2 , // After '好'
viewport : { width : 15 , height : 4 } ,
} ) ,
) ;
const state = getBufferState ( result ) ;
expect ( state . allVisualLines ) . toEqual ( [
'The quick' ,
'brown fox' ,
'jumps over the' ,
'lazy dog.' ,
] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should wrap visual lines with multiple spaces' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-05-16 11:58:37 -07:00
useTextBuffer ( {
initialText : 'The quick brown fox jumps over the lazy dog.' ,
viewport : { width : 15 , height : 4 } ,
} ) ,
) ;
const state = getBufferState ( result ) ;
// Including multiple spaces at the end of the lines like this is
// consistent with Google docs behavior and makes it intuitive to edit
// the spaces as needed.
expect ( state . allVisualLines ) . toEqual ( [
'The quick ' ,
'brown fox ' ,
'jumps over the' ,
'lazy dog.' ,
] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should wrap visual lines even without spaces' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-05-16 11:58:37 -07:00
useTextBuffer ( {
initialText : '123456789012345ABCDEFG' , // 4 chars, 12 bytes
viewport : { width : 15 , height : 2 } ,
} ) ,
) ;
const state = getBufferState ( result ) ;
// Including multiple spaces at the end of the lines like this is
// consistent with Google docs behavior and makes it intuitive to edit
// the spaces as needed.
expect ( state . allVisualLines ) . toEqual ( [ '123456789012345' , 'ABCDEFG' ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should initialize with multi-byte unicode characters and correct cursor offset' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-05-16 11:58:37 -07:00
useTextBuffer ( {
initialText : '你好世界' , // 4 chars, 12 bytes
initialCursorOffset : 2 , // After '好'
viewport : { width : 5 , height : 2 } ,
} ) ,
) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( '你好世界' ) ;
expect ( state . lines ) . toEqual ( [ '你好世界' ] ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 2 ] ) ;
// Visual: "你好" (width 4), "世"界" (width 4) with viewport width 5
expect ( state . allVisualLines ) . toEqual ( [ '你好' , '世界' ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 1 , 0 ] ) ;
} ) ;
} ) ;
describe ( 'Basic Editing' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'insert: should insert a character and update cursor' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-05-16 11:58:37 -07:00
act ( ( ) = > result . current . insert ( 'a' ) ) ;
let state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'a' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 1 ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 0 , 1 ] ) ;
act ( ( ) = > result . current . insert ( 'b' ) ) ;
state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'ab' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 2 ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 0 , 2 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'insert: should insert text in the middle of a line' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-16 22:21:22 +00:00
useTextBuffer ( {
initialText : 'abc' ,
viewport ,
} ) ,
) ;
act ( ( ) = > result . current . move ( 'right' ) ) ;
act ( ( ) = > result . current . insert ( '-NEW-' ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'a-NEW-bc' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 6 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'insert: should use placeholder for large text paste' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-21 21:09:24 -05:00
const largeText = '1\n2\n3\n4\n5\n6' ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( '[Pasted Text: 6 lines]' ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'insert: should NOT use placeholder for large text if NOT a paste' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-21 21:09:24 -05:00
const largeText = '1\n2\n3\n4\n5\n6' ;
act ( ( ) = > result . current . insert ( largeText , { paste : false } ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( largeText ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'insert: should clean up pastedContent when placeholder is deleted' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-21 21:09:24 -05:00
const largeText = '1\n2\n3\n4\n5\n6' ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
// Delete the placeholder using setText
act ( ( ) = > result . current . setText ( '' ) ) ;
expect ( Object . keys ( result . current . pastedContent ) ) . toHaveLength ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'insert: should clean up pastedContent when placeholder is removed via atomic backspace' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-21 21:09:24 -05:00
const largeText = '1\n2\n3\n4\n5\n6' ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
// Single backspace at end of placeholder removes entire placeholder
act ( ( ) = > {
result . current . backspace ( ) ;
} ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '' ) ;
// pastedContent is cleaned up when placeholder is deleted atomically
expect ( Object . keys ( result . current . pastedContent ) ) . toHaveLength ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'deleteWordLeft: should clean up pastedContent and avoid #2 suffix on repaste' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-03-17 16:16:26 -04:00
const largeText = '1\n2\n3\n4\n5\n6' ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '[Pasted Text: 6 lines]' ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
act ( ( ) = > {
for ( let i = 0 ; i < 12 ; i ++ ) {
result . current . deleteWordLeft ( ) ;
}
} ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '' ) ;
expect ( Object . keys ( result . current . pastedContent ) ) . toHaveLength ( 0 ) ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '[Pasted Text: 6 lines]' ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'deleteWordRight: should clean up pastedContent and avoid #2 suffix on repaste' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-03-17 16:16:26 -04:00
const largeText = '1\n2\n3\n4\n5\n6' ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '[Pasted Text: 6 lines]' ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
act ( ( ) = > result . current . move ( 'home' ) ) ;
act ( ( ) = > {
for ( let i = 0 ; i < 12 ; i ++ ) {
result . current . deleteWordRight ( ) ;
}
} ) ;
expect ( getBufferState ( result ) . text ) . not . toContain (
'[Pasted Text: 6 lines]' ,
) ;
expect ( Object . keys ( result . current . pastedContent ) ) . toHaveLength ( 0 ) ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
expect ( getBufferState ( result ) . text ) . toContain ( '[Pasted Text: 6 lines]' ) ;
expect ( getBufferState ( result ) . text ) . not . toContain ( '#2' ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'killLineLeft: should clean up pastedContent and avoid #2 suffix on repaste' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-03-17 16:16:26 -04:00
const largeText = '1\n2\n3\n4\n5\n6' ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '[Pasted Text: 6 lines]' ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
act ( ( ) = > result . current . killLineLeft ( ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '' ) ;
expect ( Object . keys ( result . current . pastedContent ) ) . toHaveLength ( 0 ) ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '[Pasted Text: 6 lines]' ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'killLineRight: should clean up pastedContent and avoid #2 suffix on repaste' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-03-17 16:16:26 -04:00
const largeText = '1\n2\n3\n4\n5\n6' ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '[Pasted Text: 6 lines]' ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
act ( ( ) = > {
for ( let i = 0 ; i < 40 ; i ++ ) {
result . current . move ( 'left' ) ;
}
} ) ;
act ( ( ) = > result . current . killLineRight ( ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '' ) ;
expect ( Object . keys ( result . current . pastedContent ) ) . toHaveLength ( 0 ) ;
act ( ( ) = > result . current . insert ( largeText , { paste : true } ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '[Pasted Text: 6 lines]' ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 6 lines]' ] ) . toBe (
largeText ,
) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'newline: should create a new line and move cursor' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'ab' ,
viewport ,
} ) ,
2025-05-16 11:58:37 -07:00
) ;
act ( ( ) = > result . current . move ( 'end' ) ) ; // cursor at [0,2]
act ( ( ) = > result . current . newline ( ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'ab\n' ) ;
expect ( state . lines ) . toEqual ( [ 'ab' , '' ] ) ;
expect ( state . cursor ) . toEqual ( [ 1 , 0 ] ) ;
expect ( state . allVisualLines ) . toEqual ( [ 'ab' , '' ] ) ;
expect ( state . viewportVisualLines ) . toEqual ( [ 'ab' , '' ] ) ; // viewport height 3
expect ( state . visualCursor ) . toEqual ( [ 1 , 0 ] ) ; // On the new visual line
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'backspace: should delete char to the left or merge lines' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'a\nb' ,
viewport ,
} ) ,
2025-05-16 11:58:37 -07:00
) ;
act ( ( ) = > {
result . current . move ( 'down' ) ;
} ) ;
act ( ( ) = > {
result . current . move ( 'end' ) ; // cursor to [1,1] (end of 'b')
} ) ;
act ( ( ) = > result . current . backspace ( ) ) ; // delete 'b'
let state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'a\n' ) ;
expect ( state . cursor ) . toEqual ( [ 1 , 0 ] ) ;
act ( ( ) = > result . current . backspace ( ) ) ; // merge lines
state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'a' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 1 ] ) ; // cursor after 'a'
expect ( state . allVisualLines ) . toEqual ( [ 'a' ] ) ;
expect ( state . viewportVisualLines ) . toEqual ( [ 'a' ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 0 , 1 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'del: should delete char to the right or merge lines' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'a\nb' ,
viewport ,
} ) ,
2025-05-16 11:58:37 -07:00
) ;
// cursor at [0,0]
act ( ( ) = > result . current . del ( ) ) ; // delete 'a'
let state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( '\nb' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 0 ] ) ;
act ( ( ) = > result . current . del ( ) ) ; // merge lines (deletes newline)
state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'b' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 0 ] ) ;
expect ( state . allVisualLines ) . toEqual ( [ 'b' ] ) ;
expect ( state . viewportVisualLines ) . toEqual ( [ 'b' ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 0 , 0 ] ) ;
} ) ;
} ) ;
2025-06-07 14:48:56 -07:00
describe ( 'Drag and Drop File Paths' , ( ) = > {
2026-02-09 13:19:51 -08:00
let tempDir : string ;
beforeEach ( ( ) = > {
tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'gemini-cli-test-' ) ) ;
} ) ;
afterEach ( ( ) = > {
fs . rmSync ( tempDir , { recursive : true , force : true } ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should prepend @ to a valid file path on insert' , async ( ) = > {
2026-02-09 13:19:51 -08:00
const filePath = path . join ( tempDir , 'file.txt' ) ;
fs . writeFileSync ( filePath , '' ) ;
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = >
2026-02-09 13:19:51 -08:00
useTextBuffer ( { viewport , escapePastedPaths : true } ) ,
2025-06-07 14:48:56 -07:00
) ;
2025-07-25 20:26:13 +00:00
act ( ( ) = > result . current . insert ( filePath , { paste : true } ) ) ;
2026-02-13 14:19:08 -08:00
expect ( getBufferState ( result ) . text ) . toBe ( ` @ ${ escapePath ( filePath ) } ` ) ;
2025-06-07 14:48:56 -07:00
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not prepend @ to an invalid file path on insert' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-02-09 13:19:51 -08:00
const notAPath = path . join ( tempDir , 'non_existent.txt' ) ;
2025-07-25 20:26:13 +00:00
act ( ( ) = > result . current . insert ( notAPath , { paste : true } ) ) ;
2025-06-07 14:48:56 -07:00
expect ( getBufferState ( result ) . text ) . toBe ( notAPath ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle quoted paths' , async ( ) = > {
2026-02-09 13:19:51 -08:00
const filePath = path . join ( tempDir , 'file.txt' ) ;
fs . writeFileSync ( filePath , '' ) ;
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = >
2026-02-09 13:19:51 -08:00
useTextBuffer ( { viewport , escapePastedPaths : true } ) ,
2025-06-07 14:48:56 -07:00
) ;
2026-02-09 13:19:51 -08:00
const quotedPath = ` ' ${ filePath } ' ` ;
act ( ( ) = > result . current . insert ( quotedPath , { paste : true } ) ) ;
2026-02-13 14:19:08 -08:00
expect ( getBufferState ( result ) . text ) . toBe ( ` @ ${ escapePath ( filePath ) } ` ) ;
2025-06-07 14:48:56 -07:00
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not prepend @ to short text that is not a path' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2026-02-09 13:19:51 -08:00
useTextBuffer ( { viewport , escapePastedPaths : true } ) ,
2025-06-07 14:48:56 -07:00
) ;
const shortText = 'ab' ;
2025-07-25 20:26:13 +00:00
act ( ( ) = > result . current . insert ( shortText , { paste : true } ) ) ;
2025-06-07 14:48:56 -07:00
expect ( getBufferState ( result ) . text ) . toBe ( shortText ) ;
} ) ;
2025-12-12 12:14:35 -05:00
2026-03-20 20:08:29 +00:00
it ( 'should prepend @ to multiple valid file paths on insert' , async ( ) = > {
2026-02-09 13:19:51 -08:00
const file1 = path . join ( tempDir , 'file1.txt' ) ;
const file2 = path . join ( tempDir , 'file2.txt' ) ;
fs . writeFileSync ( file1 , '' ) ;
fs . writeFileSync ( file2 , '' ) ;
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = >
2026-02-09 13:19:51 -08:00
useTextBuffer ( { viewport , escapePastedPaths : true } ) ,
2025-12-12 12:14:35 -05:00
) ;
2026-02-13 14:19:08 -08:00
const filePaths = ` ${ escapePath ( file1 ) } ${ escapePath ( file2 ) } ` ;
2025-12-12 12:14:35 -05:00
act ( ( ) = > result . current . insert ( filePaths , { paste : true } ) ) ;
2026-02-13 14:19:08 -08:00
expect ( getBufferState ( result ) . text ) . toBe (
` @ ${ escapePath ( file1 ) } @ ${ escapePath ( file2 ) } ` ,
) ;
2025-12-12 12:14:35 -05:00
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle multiple paths with escaped spaces' , async ( ) = > {
2026-02-09 13:19:51 -08:00
const file1 = path . join ( tempDir , 'my file.txt' ) ;
const file2 = path . join ( tempDir , 'other.txt' ) ;
fs . writeFileSync ( file1 , '' ) ;
fs . writeFileSync ( file2 , '' ) ;
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = >
2026-02-09 13:19:51 -08:00
useTextBuffer ( { viewport , escapePastedPaths : true } ) ,
2025-12-12 12:14:35 -05:00
) ;
2026-02-12 18:27:56 -08:00
2026-02-13 14:19:08 -08:00
const filePaths = ` ${ escapePath ( file1 ) } ${ escapePath ( file2 ) } ` ;
2026-02-09 13:19:51 -08:00
2025-12-12 12:14:35 -05:00
act ( ( ) = > result . current . insert ( filePaths , { paste : true } ) ) ;
2026-02-12 18:27:56 -08:00
expect ( getBufferState ( result ) . text ) . toBe (
2026-02-13 14:19:08 -08:00
` @ ${ escapePath ( file1 ) } @ ${ escapePath ( file2 ) } ` ,
2026-02-12 18:27:56 -08:00
) ;
2025-12-12 12:14:35 -05:00
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not prepend @ unless all paths are valid' , async ( ) = > {
2026-02-09 13:19:51 -08:00
const validFile = path . join ( tempDir , 'valid.txt' ) ;
const invalidFile = path . join ( tempDir , 'invalid.jpg' ) ;
fs . writeFileSync ( validFile , '' ) ;
// Do not create invalidFile
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = >
2025-12-12 12:14:35 -05:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
escapePastedPaths : true ,
2025-12-12 12:14:35 -05:00
} ) ,
) ;
2026-02-09 13:19:51 -08:00
const filePaths = ` ${ validFile } ${ invalidFile } ` ;
2025-12-12 12:14:35 -05:00
act ( ( ) = > result . current . insert ( filePaths , { paste : true } ) ) ;
2026-02-12 18:27:56 -08:00
expect ( getBufferState ( result ) . text ) . toBe ( ` ${ validFile } ${ invalidFile } ` ) ;
2025-12-12 12:14:35 -05:00
} ) ;
2025-06-07 14:48:56 -07:00
} ) ;
2025-07-05 16:20:12 -06:00
describe ( 'Shell Mode Behavior' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should not prepend @ to valid file paths when shellModeActive is true' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-07-05 16:20:12 -06:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
escapePastedPaths : true ,
2025-07-05 16:20:12 -06:00
shellModeActive : true ,
} ) ,
) ;
const filePath = '/path/to/a/valid/file.txt' ;
2025-07-25 20:26:13 +00:00
act ( ( ) = > result . current . insert ( filePath , { paste : true } ) ) ;
2025-07-05 16:20:12 -06:00
expect ( getBufferState ( result ) . text ) . toBe ( filePath ) ; // No @ prefix
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not prepend @ to quoted paths when shellModeActive is true' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-07-05 16:20:12 -06:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
escapePastedPaths : true ,
2025-07-05 16:20:12 -06:00
shellModeActive : true ,
} ) ,
) ;
const quotedFilePath = "'/path/to/a/valid/file.txt'" ;
2025-07-25 20:26:13 +00:00
act ( ( ) = > result . current . insert ( quotedFilePath , { paste : true } ) ) ;
2025-07-05 16:20:12 -06:00
expect ( getBufferState ( result ) . text ) . toBe ( quotedFilePath ) ; // No @ prefix, keeps quotes
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should behave normally with invalid paths when shellModeActive is true' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-07-05 16:20:12 -06:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
2025-07-05 16:20:12 -06:00
shellModeActive : true ,
} ) ,
) ;
const notAPath = 'this is just some text' ;
2025-07-25 20:26:13 +00:00
act ( ( ) = > result . current . insert ( notAPath , { paste : true } ) ) ;
2025-07-05 16:20:12 -06:00
expect ( getBufferState ( result ) . text ) . toBe ( notAPath ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should behave normally with short text when shellModeActive is true' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-07-05 16:20:12 -06:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
escapePastedPaths : true ,
2025-07-05 16:20:12 -06:00
shellModeActive : true ,
} ) ,
) ;
const shortText = 'ls' ;
2025-07-25 20:26:13 +00:00
act ( ( ) = > result . current . insert ( shortText , { paste : true } ) ) ;
2025-07-05 16:20:12 -06:00
expect ( getBufferState ( result ) . text ) . toBe ( shortText ) ; // No @ prefix for short text
} ) ;
} ) ;
2025-05-16 11:58:37 -07:00
describe ( 'Cursor Movement' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'move: left/right should work within and across visual lines (due to wrapping)' , async ( ) = > {
2025-05-16 11:58:37 -07:00
// Text: "long line1next line2" (20 chars)
// Viewport width 5. Word wrapping should produce:
// "long " (5)
// "line1" (5)
// "next " (5)
// "line2" (5)
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = >
2025-05-16 11:58:37 -07:00
useTextBuffer ( {
initialText : 'long line1next line2' , // Corrected: was 'long line1next line2'
viewport : { width : 5 , height : 4 } ,
} ) ,
) ;
// Initial cursor [0,0] logical, visual [0,0] ("l" of "long ")
act ( ( ) = > result . current . move ( 'right' ) ) ; // visual [0,1] ("o")
expect ( getBufferState ( result ) . visualCursor ) . toEqual ( [ 0 , 1 ] ) ;
act ( ( ) = > result . current . move ( 'right' ) ) ; // visual [0,2] ("n")
act ( ( ) = > result . current . move ( 'right' ) ) ; // visual [0,3] ("g")
act ( ( ) = > result . current . move ( 'right' ) ) ; // visual [0,4] (" ")
expect ( getBufferState ( result ) . visualCursor ) . toEqual ( [ 0 , 4 ] ) ;
act ( ( ) = > result . current . move ( 'right' ) ) ; // visual [1,0] ("l" of "line1")
expect ( getBufferState ( result ) . visualCursor ) . toEqual ( [ 1 , 0 ] ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 5 ] ) ; // logical cursor
act ( ( ) = > result . current . move ( 'left' ) ) ; // visual [0,4] (" " of "long ")
expect ( getBufferState ( result ) . visualCursor ) . toEqual ( [ 0 , 4 ] ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 4 ] ) ; // logical cursor
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'move: up/down should preserve preferred visual column' , async ( ) = > {
2025-05-16 11:58:37 -07:00
const text = 'abcde\nxy\n12345' ;
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : text ,
viewport ,
} ) ,
2025-05-16 11:58:37 -07:00
) ;
expect ( result . current . allVisualLines ) . toEqual ( [ 'abcde' , 'xy' , '12345' ] ) ;
// Place cursor at the end of "abcde" -> logical [0,5]
act ( ( ) = > {
result . current . move ( 'home' ) ; // to [0,0]
} ) ;
for ( let i = 0 ; i < 5 ; i ++ ) {
act ( ( ) = > {
result . current . move ( 'right' ) ; // to [0,5]
} ) ;
}
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 5 ] ) ;
expect ( getBufferState ( result ) . visualCursor ) . toEqual ( [ 0 , 5 ] ) ;
// Set preferredCol by moving up then down to the same spot, then test.
act ( ( ) = > {
result . current . move ( 'down' ) ; // to xy, logical [1,2], visual [1,2], preferredCol should be 5
} ) ;
let state = getBufferState ( result ) ;
expect ( state . cursor ) . toEqual ( [ 1 , 2 ] ) ; // Logical cursor at end of 'xy'
expect ( state . visualCursor ) . toEqual ( [ 1 , 2 ] ) ; // Visual cursor at end of 'xy'
expect ( state . preferredCol ) . toBe ( 5 ) ;
act ( ( ) = > result . current . move ( 'down' ) ) ; // to '12345', preferredCol=5.
state = getBufferState ( result ) ;
expect ( state . cursor ) . toEqual ( [ 2 , 5 ] ) ; // Logical cursor at end of '12345'
expect ( state . visualCursor ) . toEqual ( [ 2 , 5 ] ) ; // Visual cursor at end of '12345'
expect ( state . preferredCol ) . toBe ( 5 ) ; // Preferred col is maintained
act ( ( ) = > result . current . move ( 'left' ) ) ; // preferredCol should reset
state = getBufferState ( result ) ;
expect ( state . preferredCol ) . toBe ( null ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'move: home/end should go to visual line start/end' , async ( ) = > {
2025-05-16 11:58:37 -07:00
const initialText = 'line one\nsecond line' ;
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText ,
viewport : { width : 5 , height : 5 } ,
} ) ,
2025-05-16 11:58:37 -07:00
) ;
expect ( result . current . allVisualLines ) . toEqual ( [
'line' ,
'one' ,
'secon' ,
'd' ,
'line' ,
] ) ;
// Initial cursor [0,0] (start of "line")
act ( ( ) = > result . current . move ( 'down' ) ) ; // visual cursor from [0,0] to [1,0] ("o" of "one")
act ( ( ) = > result . current . move ( 'right' ) ) ; // visual cursor to [1,1] ("n" of "one")
expect ( getBufferState ( result ) . visualCursor ) . toEqual ( [ 1 , 1 ] ) ;
act ( ( ) = > result . current . move ( 'home' ) ) ; // visual cursor to [1,0] (start of "one")
expect ( getBufferState ( result ) . visualCursor ) . toEqual ( [ 1 , 0 ] ) ;
act ( ( ) = > result . current . move ( 'end' ) ) ; // visual cursor to [1,3] (end of "one")
expect ( getBufferState ( result ) . visualCursor ) . toEqual ( [ 1 , 3 ] ) ; // "one" is 3 chars
} ) ;
} ) ;
describe ( 'Visual Layout & Viewport' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should wrap long lines correctly into visualLines' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-05-16 11:58:37 -07:00
useTextBuffer ( {
initialText : 'This is a very long line of text.' , // 33 chars
viewport : { width : 10 , height : 5 } ,
} ) ,
) ;
const state = getBufferState ( result ) ;
// Expected visual lines with word wrapping (viewport width 10):
// "This is a"
// "very long"
// "line of"
// "text."
expect ( state . allVisualLines . length ) . toBe ( 4 ) ;
expect ( state . allVisualLines [ 0 ] ) . toBe ( 'This is a' ) ;
expect ( state . allVisualLines [ 1 ] ) . toBe ( 'very long' ) ;
expect ( state . allVisualLines [ 2 ] ) . toBe ( 'line of' ) ;
expect ( state . allVisualLines [ 3 ] ) . toBe ( 'text.' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should update visualScrollRow when visualCursor moves out of viewport' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-05-16 11:58:37 -07:00
useTextBuffer ( {
initialText : 'l1\nl2\nl3\nl4\nl5' ,
viewport : { width : 5 , height : 3 } , // Can show 3 visual lines
} ) ,
) ;
// Initial: l1, l2, l3 visible. visualScrollRow = 0. visualCursor = [0,0]
expect ( getBufferState ( result ) . visualScrollRow ) . toBe ( 0 ) ;
expect ( getBufferState ( result ) . allVisualLines ) . toEqual ( [
'l1' ,
'l2' ,
'l3' ,
'l4' ,
'l5' ,
] ) ;
expect ( getBufferState ( result ) . viewportVisualLines ) . toEqual ( [
'l1' ,
'l2' ,
'l3' ,
] ) ;
act ( ( ) = > result . current . move ( 'down' ) ) ; // vc=[1,0]
act ( ( ) = > result . current . move ( 'down' ) ) ; // vc=[2,0] (l3)
expect ( getBufferState ( result ) . visualScrollRow ) . toBe ( 0 ) ;
act ( ( ) = > result . current . move ( 'down' ) ) ; // vc=[3,0] (l4) - scroll should happen
// Now: l2, l3, l4 visible. visualScrollRow = 1.
let state = getBufferState ( result ) ;
expect ( state . visualScrollRow ) . toBe ( 1 ) ;
expect ( state . allVisualLines ) . toEqual ( [ 'l1' , 'l2' , 'l3' , 'l4' , 'l5' ] ) ;
expect ( state . viewportVisualLines ) . toEqual ( [ 'l2' , 'l3' , 'l4' ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 3 , 0 ] ) ;
act ( ( ) = > result . current . move ( 'up' ) ) ; // vc=[2,0] (l3)
act ( ( ) = > result . current . move ( 'up' ) ) ; // vc=[1,0] (l2)
expect ( getBufferState ( result ) . visualScrollRow ) . toBe ( 1 ) ;
act ( ( ) = > result . current . move ( 'up' ) ) ; // vc=[0,0] (l1) - scroll up
// Now: l1, l2, l3 visible. visualScrollRow = 0
state = getBufferState ( result ) ; // Assign to the existing `state` variable
expect ( state . visualScrollRow ) . toBe ( 0 ) ;
expect ( state . allVisualLines ) . toEqual ( [ 'l1' , 'l2' , 'l3' , 'l4' , 'l5' ] ) ;
expect ( state . viewportVisualLines ) . toEqual ( [ 'l1' , 'l2' , 'l3' ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 0 , 0 ] ) ;
} ) ;
} ) ;
describe ( 'Undo/Redo' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should undo and redo an insert operation' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-05-16 11:58:37 -07:00
act ( ( ) = > result . current . insert ( 'a' ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'a' ) ;
act ( ( ) = > result . current . undo ( ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '' ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 0 ] ) ;
act ( ( ) = > result . current . redo ( ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'a' ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 1 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should undo and redo a newline operation' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'test' ,
viewport ,
} ) ,
2025-05-16 11:58:37 -07:00
) ;
act ( ( ) = > result . current . move ( 'end' ) ) ;
act ( ( ) = > result . current . newline ( ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'test\n' ) ;
act ( ( ) = > result . current . undo ( ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'test' ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 4 ] ) ;
act ( ( ) = > result . current . redo ( ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'test\n' ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 1 , 0 ] ) ;
} ) ;
} ) ;
describe ( 'Unicode Handling' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'insert: should correctly handle multi-byte unicode characters' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-05-16 11:58:37 -07:00
act ( ( ) = > result . current . insert ( '你好' ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( '你好' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 2 ] ) ; // Cursor is 2 (char count)
expect ( state . visualCursor ) . toEqual ( [ 0 , 2 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'backspace: should correctly delete multi-byte unicode characters' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : '你好' ,
viewport ,
} ) ,
2025-05-16 11:58:37 -07:00
) ;
act ( ( ) = > result . current . move ( 'end' ) ) ; // cursor at [0,2]
act ( ( ) = > result . current . backspace ( ) ) ; // delete '好'
let state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( '你' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 1 ] ) ;
act ( ( ) = > result . current . backspace ( ) ) ; // delete '你'
state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( '' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 0 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'move: left/right should treat multi-byte chars as single units for visual cursor' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-05-16 11:58:37 -07:00
useTextBuffer ( {
initialText : '🐶🐱' ,
viewport : { width : 5 , height : 1 } ,
} ) ,
) ;
// Initial: visualCursor [0,0]
act ( ( ) = > result . current . move ( 'right' ) ) ; // visualCursor [0,1] (after 🐶)
let state = getBufferState ( result ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 1 ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 0 , 1 ] ) ;
act ( ( ) = > result . current . move ( 'right' ) ) ; // visualCursor [0,2] (after 🐱)
state = getBufferState ( result ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 2 ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 0 , 2 ] ) ;
act ( ( ) = > result . current . move ( 'left' ) ) ; // visualCursor [0,1] (before 🐱 / after 🐶)
state = getBufferState ( result ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 1 ] ) ;
expect ( state . visualCursor ) . toEqual ( [ 0 , 1 ] ) ;
2026-02-02 13:55:45 -08:00
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'move: up/down should work on wrapped lines (regression test)' , async ( ) = > {
2026-02-02 13:55:45 -08:00
// Line that wraps into two visual lines
// Viewport width 10. "0123456789ABCDE" (15 chars)
// Visual Line 0: "0123456789"
// Visual Line 1: "ABCDE"
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = >
2026-02-02 13:55:45 -08:00
useTextBuffer ( {
viewport : { width : 10 , height : 5 } ,
} ) ,
) ;
act ( ( ) = > {
result . current . setText ( '0123456789ABCDE' ) ;
} ) ;
// Cursor should be at the end: logical [0, 15], visual [1, 5]
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 15 ] ) ;
expect ( getBufferState ( result ) . visualCursor ) . toEqual ( [ 1 , 5 ] ) ;
// Press Up arrow - should move to first visual line
// This currently fails because handleInput returns false if cursorRow === 0
let handledUp = false ;
act ( ( ) = > {
handledUp = result . current . handleInput ( {
name : 'up' ,
shift : false ,
alt : false ,
ctrl : false ,
cmd : false ,
insertable : false ,
sequence : '\x1b[A' ,
} ) ;
} ) ;
expect ( handledUp ) . toBe ( true ) ;
expect ( getBufferState ( result ) . visualCursor [ 0 ] ) . toBe ( 0 ) ;
// Press Down arrow - should move back to second visual line
// This would also fail if cursorRow is the last logical row
let handledDown = false ;
act ( ( ) = > {
handledDown = result . current . handleInput ( {
name : 'down' ,
shift : false ,
alt : false ,
ctrl : false ,
cmd : false ,
insertable : false ,
sequence : '\x1b[B' ,
} ) ;
} ) ;
expect ( handledDown ) . toBe ( true ) ;
expect ( getBufferState ( result ) . visualCursor [ 0 ] ) . toBe ( 1 ) ;
2025-05-16 11:58:37 -07:00
} ) ;
2025-11-21 07:56:31 +08:00
2026-03-20 20:08:29 +00:00
it ( 'moveToVisualPosition: should correctly handle wide characters (Chinese)' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-11-21 07:56:31 +08:00
useTextBuffer ( {
initialText : '你好' , // 2 chars, width 4
viewport : { width : 10 , height : 1 } ,
} ) ,
) ;
// '你' (width 2): visual 0-1. '好' (width 2): visual 2-3.
// Click on '你' (first half, x=0) -> index 0
act ( ( ) = > result . current . moveToVisualPosition ( 0 , 0 ) ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 0 ] ) ;
// Click on '你' (second half, x=1) -> index 1 (after first char)
act ( ( ) = > result . current . moveToVisualPosition ( 0 , 1 ) ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 1 ] ) ;
// Click on '好' (first half, x=2) -> index 1 (before second char)
act ( ( ) = > result . current . moveToVisualPosition ( 0 , 2 ) ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 1 ] ) ;
// Click on '好' (second half, x=3) -> index 2 (after second char)
act ( ( ) = > result . current . moveToVisualPosition ( 0 , 3 ) ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 2 ] ) ;
} ) ;
2025-05-16 11:58:37 -07:00
} ) ;
describe ( 'handleInput' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should insert printable characters' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-06-27 10:57:32 -07:00
result . current . handleInput ( {
name : 'h' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : true ,
2025-06-27 10:57:32 -07:00
sequence : 'h' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
void act ( ( ) = >
2025-06-27 10:57:32 -07:00
result . current . handleInput ( {
name : 'i' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : true ,
2025-06-27 10:57:32 -07:00
sequence : 'i' ,
} ) ,
) ;
2025-05-16 11:58:37 -07:00
expect ( getBufferState ( result ) . text ) . toBe ( 'hi' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle "Enter" key as newline' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-06-27 10:57:32 -07:00
result . current . handleInput ( {
2026-03-10 02:32:40 +00:00
name : 'enter' ,
2025-06-27 10:57:32 -07:00
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : true ,
2025-06-27 10:57:32 -07:00
sequence : '\r' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-05-16 11:58:37 -07:00
expect ( getBufferState ( result ) . lines ) . toEqual ( [ '' , '' ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle Ctrl+J as newline' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2026-01-20 16:20:51 -08:00
result . current . handleInput ( {
name : 'j' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : true ,
cmd : false ,
2026-01-20 16:20:51 -08:00
insertable : false ,
sequence : '\n' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2026-01-20 16:20:51 -08:00
expect ( getBufferState ( result ) . lines ) . toEqual ( [ '' , '' ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should do nothing for a tab key press' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-10-21 19:54:51 -07:00
result . current . handleInput ( {
name : 'tab' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : false ,
2025-10-21 19:54:51 -07:00
sequence : '\t' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-10-21 19:54:51 -07:00
expect ( getBufferState ( result ) . text ) . toBe ( '' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should do nothing for a shift tab key press' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-10-21 19:54:51 -07:00
result . current . handleInput ( {
name : 'tab' ,
shift : true ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : false ,
2025-10-21 19:54:51 -07:00
sequence : '\u001b[9;2u' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-10-21 19:54:51 -07:00
expect ( getBufferState ( result ) . text ) . toBe ( '' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle CLEAR_INPUT (Ctrl+C)' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2026-01-30 16:11:14 -08:00
useTextBuffer ( {
initialText : 'hello' ,
viewport ,
} ) ,
) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'hello' ) ;
let handled = false ;
act ( ( ) = > {
handled = result . current . handleInput ( {
name : 'c' ,
shift : false ,
alt : false ,
ctrl : true ,
cmd : false ,
insertable : false ,
sequence : '\u0003' ,
} ) ;
} ) ;
expect ( handled ) . toBe ( true ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should NOT handle CLEAR_INPUT if buffer is empty' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-30 16:11:14 -08:00
let handled = true ;
act ( ( ) = > {
handled = result . current . handleInput ( {
name : 'c' ,
shift : false ,
alt : false ,
ctrl : true ,
cmd : false ,
insertable : false ,
sequence : '\u0003' ,
} ) ;
} ) ;
expect ( handled ) . toBe ( false ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle "Backspace" key' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'a' ,
viewport ,
} ) ,
2025-05-16 11:58:37 -07:00
) ;
act ( ( ) = > result . current . move ( 'end' ) ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-06-27 10:57:32 -07:00
result . current . handleInput ( {
name : 'backspace' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : false ,
2025-06-27 10:57:32 -07:00
sequence : '\x7f' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-05-16 11:58:37 -07:00
expect ( getBufferState ( result ) . text ) . toBe ( '' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle multiple delete characters in one input' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-16 06:25:11 +00:00
useTextBuffer ( {
initialText : 'abcde' ,
viewport ,
} ) ,
) ;
act ( ( ) = > result . current . move ( 'end' ) ) ; // cursor at the end
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 5 ] ) ;
act ( ( ) = > {
2025-07-03 17:53:17 -07:00
result . current . handleInput ( {
name : 'backspace' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : false ,
2025-07-03 17:53:17 -07:00
sequence : '\x7f' ,
} ) ;
result . current . handleInput ( {
name : 'backspace' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : false ,
2025-07-03 17:53:17 -07:00
sequence : '\x7f' ,
} ) ;
result . current . handleInput ( {
name : 'backspace' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : false ,
2025-07-03 17:53:17 -07:00
sequence : '\x7f' ,
} ) ;
2025-06-16 06:25:11 +00:00
} ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'ab' ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 2 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle inserts that contain delete characters' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-16 06:25:11 +00:00
useTextBuffer ( {
initialText : 'abcde' ,
viewport ,
} ) ,
) ;
act ( ( ) = > result . current . move ( 'end' ) ) ; // cursor at the end
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 5 ] ) ;
act ( ( ) = > {
2025-07-03 17:53:17 -07:00
result . current . insert ( '\x7f\x7f\x7f' ) ;
2025-06-16 06:25:11 +00:00
} ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'ab' ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 2 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle inserts with a mix of regular and delete characters' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-16 06:25:11 +00:00
useTextBuffer ( {
initialText : 'abcde' ,
viewport ,
} ) ,
) ;
act ( ( ) = > result . current . move ( 'end' ) ) ; // cursor at the end
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 5 ] ) ;
act ( ( ) = > {
2025-07-03 17:53:17 -07:00
result . current . insert ( '\x7fI\x7f\x7fNEW' ) ;
2025-06-16 06:25:11 +00:00
} ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'abcNEW' ) ;
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 6 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle arrow keys for movement' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'ab' ,
viewport ,
} ) ,
2025-05-16 11:58:37 -07:00
) ;
act ( ( ) = > result . current . move ( 'end' ) ) ; // cursor [0,2]
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-06-27 10:57:32 -07:00
result . current . handleInput ( {
name : 'left' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : false ,
2025-06-27 10:57:32 -07:00
sequence : '\x1b[D' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-05-16 11:58:37 -07:00
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 1 ] ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-06-27 10:57:32 -07:00
result . current . handleInput ( {
name : 'right' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : false ,
2025-06-27 10:57:32 -07:00
sequence : '\x1b[C' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-05-16 11:58:37 -07:00
expect ( getBufferState ( result ) . cursor ) . toEqual ( [ 0 , 2 ] ) ;
} ) ;
2025-05-16 13:17:48 -07:00
2026-03-20 20:08:29 +00:00
it ( 'should strip ANSI escape codes when pasting text' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-05-16 13:17:48 -07:00
const textWithAnsi = '\x1B[31mHello\x1B[0m \x1B[32mWorld\x1B[0m' ;
// Simulate pasting by calling handleInput with a string longer than 1 char
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-06-27 10:57:32 -07:00
result . current . handleInput ( {
2025-07-05 16:20:12 -06:00
name : '' ,
2025-06-27 10:57:32 -07:00
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : true ,
2025-06-27 10:57:32 -07:00
sequence : textWithAnsi ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-05-16 13:17:48 -07:00
expect ( getBufferState ( result ) . text ) . toBe ( 'Hello World' ) ;
} ) ;
2025-05-20 23:42:40 -07:00
2026-03-20 20:08:29 +00:00
it ( 'should handle VSCode terminal Shift+Enter as newline' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-06-27 10:57:32 -07:00
result . current . handleInput ( {
2026-03-10 02:32:40 +00:00
name : 'enter' ,
2025-06-27 10:57:32 -07:00
shift : true ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : true ,
2025-06-27 10:57:32 -07:00
sequence : '\r' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ; // Simulates Shift+Enter in VSCode terminal
2025-05-20 23:42:40 -07:00
expect ( getBufferState ( result ) . lines ) . toEqual ( [ '' , '' ] ) ;
} ) ;
2025-06-02 14:31:35 -07:00
2026-03-20 20:08:29 +00:00
it ( 'should correctly handle repeated pasting of long text' , async ( ) = > {
2025-06-02 14:31:35 -07:00
const longText = ` not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lore
` ;
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-06-02 14:31:35 -07:00
// Simulate pasting the long text multiple times
2025-06-16 06:25:11 +00:00
act ( ( ) = > {
2025-07-25 20:26:13 +00:00
result . current . insert ( longText , { paste : true } ) ;
result . current . insert ( longText , { paste : true } ) ;
result . current . insert ( longText , { paste : true } ) ;
2025-06-16 06:25:11 +00:00
} ) ;
2025-06-02 14:31:35 -07:00
const state = getBufferState ( result ) ;
2026-01-22 07:05:38 -08:00
// Check that the text is the result of three concatenations of unique placeholders.
// Now that ID generation is in the reducer, they are correctly unique even when batched.
2026-01-21 21:09:24 -05:00
expect ( state . lines ) . toStrictEqual ( [
2026-01-22 07:05:38 -08:00
'[Pasted Text: 8 lines][Pasted Text: 8 lines #2][Pasted Text: 8 lines #3]' ,
2026-01-21 21:09:24 -05:00
] ) ;
expect ( result . current . pastedContent [ '[Pasted Text: 8 lines]' ] ) . toBe (
longText ,
2025-06-02 14:31:35 -07:00
) ;
2026-01-22 07:05:38 -08:00
expect ( result . current . pastedContent [ '[Pasted Text: 8 lines #2]' ] ) . toBe (
longText ,
) ;
expect ( result . current . pastedContent [ '[Pasted Text: 8 lines #3]' ] ) . toBe (
longText ,
) ;
2025-06-02 14:31:35 -07:00
const expectedCursorPos = offsetToLogicalPos (
state . text ,
state . text . length ,
) ;
expect ( state . cursor ) . toEqual ( expectedCursorPos ) ;
} ) ;
2025-05-16 11:58:37 -07:00
} ) ;
// More tests would be needed for:
// - setText, replaceRange
// - deleteWordLeft, deleteWordRight
// - More complex undo/redo scenarios
// - Selection and clipboard (copy/paste) - might need clipboard API mocks or internal state check
// - openInExternalEditor (heavy mocking of fs, child_process, os)
// - All edge cases for visual scrolling and wrapping with different viewport sizes and text content.
2025-05-20 16:50:32 -07:00
describe ( 'replaceRange' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should replace a single-line range with single-line text' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : '@pac' ,
viewport ,
} ) ,
2025-05-20 16:50:32 -07:00
) ;
act ( ( ) = > result . current . replaceRange ( 0 , 1 , 0 , 4 , 'packages' ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( '@packages' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 9 ] ) ; // cursor after 'typescript'
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should replace a multi-line range with single-line text' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'hello\nworld\nagain' ,
viewport ,
} ) ,
2025-05-20 16:50:32 -07:00
) ;
act ( ( ) = > result . current . replaceRange ( 0 , 2 , 1 , 3 , ' new ' ) ) ; // replace 'llo\nwor' with ' new '
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'he new ld\nagain' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 7 ] ) ; // cursor after ' new '
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should delete a range when replacing with an empty string' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'hello world' ,
viewport ,
} ) ,
2025-05-20 16:50:32 -07:00
) ;
act ( ( ) = > result . current . replaceRange ( 0 , 5 , 0 , 11 , '' ) ) ; // delete ' world'
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'hello' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 5 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle replacing at the beginning of the text' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'world' ,
viewport ,
} ) ,
2025-05-20 16:50:32 -07:00
) ;
act ( ( ) = > result . current . replaceRange ( 0 , 0 , 0 , 0 , 'hello ' ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'hello world' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 6 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle replacing at the end of the text' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'hello' ,
viewport ,
} ) ,
2025-05-20 16:50:32 -07:00
) ;
act ( ( ) = > result . current . replaceRange ( 0 , 5 , 0 , 5 , ' world' ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'hello world' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 11 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle replacing the entire buffer content' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'old text' ,
viewport ,
} ) ,
2025-05-20 16:50:32 -07:00
) ;
act ( ( ) = > result . current . replaceRange ( 0 , 0 , 0 , 8 , 'new text' ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'new text' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 8 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should correctly replace with unicode characters' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'hello *** world' ,
viewport ,
} ) ,
2025-05-20 16:50:32 -07:00
) ;
act ( ( ) = > result . current . replaceRange ( 0 , 6 , 0 , 9 , '你好' ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'hello 你好 world' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 8 ] ) ; // after '你好'
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle invalid range by returning false and not changing text' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'test' ,
viewport ,
} ) ,
2025-05-20 16:50:32 -07:00
) ;
act ( ( ) = > {
2025-07-03 17:53:17 -07:00
result . current . replaceRange ( 0 , 5 , 0 , 3 , 'fail' ) ; // startCol > endCol in same line
2025-05-20 16:50:32 -07:00
} ) ;
2025-07-03 17:53:17 -07:00
2025-05-20 16:50:32 -07:00
expect ( getBufferState ( result ) . text ) . toBe ( 'test' ) ;
act ( ( ) = > {
2025-07-03 17:53:17 -07:00
result . current . replaceRange ( 1 , 0 , 0 , 0 , 'fail' ) ; // startRow > endRow
2025-05-20 16:50:32 -07:00
} ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'test' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'replaceRange: multiple lines with a single character' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-06-07 14:48:56 -07:00
useTextBuffer ( {
initialText : 'first\nsecond\nthird' ,
viewport ,
} ) ,
2025-05-20 16:50:32 -07:00
) ;
act ( ( ) = > result . current . replaceRange ( 0 , 2 , 2 , 3 , 'X' ) ) ; // Replace 'rst\nsecond\nthi'
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'fiXrd' ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 3 ] ) ; // After 'X'
} ) ;
2025-07-31 16:16:29 -07:00
2026-03-20 20:08:29 +00:00
it ( 'should replace a single-line range with multi-line text' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-07-31 16:16:29 -07:00
useTextBuffer ( {
initialText : 'one two three' ,
viewport ,
} ) ,
) ;
// Replace "two" with "new\nline"
act ( ( ) = > result . current . replaceRange ( 0 , 4 , 0 , 7 , 'new\nline' ) ) ;
const state = getBufferState ( result ) ;
expect ( state . lines ) . toEqual ( [ 'one new' , 'line three' ] ) ;
expect ( state . text ) . toBe ( 'one new\nline three' ) ;
expect ( state . cursor ) . toEqual ( [ 1 , 4 ] ) ; // cursor after 'line'
} ) ;
2025-05-20 16:50:32 -07:00
} ) ;
2025-06-16 06:25:11 +00:00
describe ( 'Input Sanitization' , ( ) = > {
2025-11-03 23:40:57 +05:30
const createInput = ( sequence : string ) = > ( {
name : '' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : true ,
2025-11-03 23:40:57 +05:30
sequence ,
} ) ;
it . each ( [
{
input : '\x1B[31mHello\x1B[0m \x1B[32mWorld\x1B[0m' ,
expected : 'Hello World' ,
desc : 'ANSI escape codes' ,
} ,
{
input : 'H\x07e\x08l\x0Bl\x0Co' ,
expected : 'Hello' ,
desc : 'control characters' ,
} ,
{
input : '\u001B[4mH\u001B[0mello' ,
expected : 'Hello' ,
desc : 'mixed ANSI and control characters' ,
} ,
{
input : '\u001B[4mPasted\u001B[4m Text' ,
expected : 'Pasted Text' ,
desc : 'pasted text with ANSI' ,
} ,
2026-03-20 20:08:29 +00:00
] ) ( 'should strip $desc from input' , async ( { input , expected } ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
result . current . handleInput ( createInput ( input ) ) ;
} ) ;
2025-11-03 23:40:57 +05:30
expect ( getBufferState ( result ) . text ) . toBe ( expected ) ;
2025-06-16 06:25:11 +00:00
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not strip standard characters or newlines' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-06-16 06:25:11 +00:00
const validText = 'Hello World\nThis is a test.' ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
result . current . handleInput ( createInput ( validText ) ) ;
} ) ;
2025-06-16 06:25:11 +00:00
expect ( getBufferState ( result ) . text ) . toBe ( validText ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should sanitize large text (>5000 chars) and strip unsafe characters' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-09-10 21:20:40 -07:00
const unsafeChars = '\x07\x08\x0B\x0C' ;
const largeTextWithUnsafe =
'safe text' . repeat ( 600 ) + unsafeChars + 'more safe text' ;
expect ( largeTextWithUnsafe . length ) . toBeGreaterThan ( 5000 ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-09-10 21:20:40 -07:00
result . current . handleInput ( {
name : '' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : true ,
2025-09-10 21:20:40 -07:00
sequence : largeTextWithUnsafe ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-09-10 21:20:40 -07:00
const resultText = getBufferState ( result ) . text ;
expect ( resultText ) . not . toContain ( '\x07' ) ;
expect ( resultText ) . not . toContain ( '\x08' ) ;
expect ( resultText ) . not . toContain ( '\x0B' ) ;
expect ( resultText ) . not . toContain ( '\x0C' ) ;
expect ( resultText ) . toContain ( 'safe text' ) ;
expect ( resultText ) . toContain ( 'more safe text' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should sanitize large ANSI text (>5000 chars) and strip escape codes' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-09-10 21:20:40 -07:00
const largeTextWithAnsi =
'\x1B[31m' +
'red text' . repeat ( 800 ) +
'\x1B[0m' +
'\x1B[32m' +
'green text' . repeat ( 200 ) +
'\x1B[0m' ;
expect ( largeTextWithAnsi . length ) . toBeGreaterThan ( 5000 ) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-09-10 21:20:40 -07:00
result . current . handleInput ( {
name : '' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : true ,
2025-09-10 21:20:40 -07:00
sequence : largeTextWithAnsi ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-09-10 21:20:40 -07:00
const resultText = getBufferState ( result ) . text ;
expect ( resultText ) . not . toContain ( '\x1B[31m' ) ;
expect ( resultText ) . not . toContain ( '\x1B[32m' ) ;
expect ( resultText ) . not . toContain ( '\x1B[0m' ) ;
expect ( resultText ) . toContain ( 'red text' ) ;
expect ( resultText ) . toContain ( 'green text' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not strip popular emojis' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-08-13 17:33:01 -07:00
const emojis = '🐍🐳🦀🦄' ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-08-13 17:33:01 -07:00
result . current . handleInput ( {
name : '' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : true ,
2025-08-13 17:33:01 -07:00
sequence : emojis ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-08-13 17:33:01 -07:00
expect ( getBufferState ( result ) . text ) . toBe ( emojis ) ;
} ) ;
} ) ;
2025-10-29 18:58:08 -07:00
describe ( 'inputFilter' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should filter input based on the provided filter function' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-10-29 18:58:08 -07:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
2025-10-29 18:58:08 -07:00
inputFilter : ( text ) = > text . replace ( /[^0-9]/g , '' ) ,
} ) ,
) ;
act ( ( ) = > result . current . insert ( 'a1b2c3' ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '123' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle empty result from filter' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-10-29 18:58:08 -07:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
2025-10-29 18:58:08 -07:00
inputFilter : ( text ) = > text . replace ( /[^0-9]/g , '' ) ,
} ) ,
) ;
act ( ( ) = > result . current . insert ( 'abc' ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( '' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should filter pasted text' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-10-29 18:58:08 -07:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
2025-10-29 18:58:08 -07:00
inputFilter : ( text ) = > text . toUpperCase ( ) ,
} ) ,
) ;
act ( ( ) = > result . current . insert ( 'hello' , { paste : true } ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'HELLO' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not filter newlines if they are allowed by the filter' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-10-29 18:58:08 -07:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
2025-10-29 18:58:08 -07:00
inputFilter : ( text ) = > text , // Allow everything including newlines
} ) ,
) ;
act ( ( ) = > result . current . insert ( 'a\nb' ) ) ;
// The insert function splits by newline and inserts separately if it detects them.
// If the filter allows them, they should be handled correctly by the subsequent logic in insert.
expect ( getBufferState ( result ) . text ) . toBe ( 'a\nb' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should filter before newline check in insert' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-10-29 18:58:08 -07:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
2025-10-29 18:58:08 -07:00
inputFilter : ( text ) = > text . replace ( /\n/g , '' ) , // Filter out newlines
} ) ,
) ;
act ( ( ) = > result . current . insert ( 'a\nb' ) ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'ab' ) ;
} ) ;
} ) ;
2025-08-13 17:33:01 -07:00
describe ( 'stripAnsi' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should correctly strip ANSI escape codes' , async ( ) = > {
2025-08-13 17:33:01 -07:00
const textWithAnsi = '\x1B[31mHello\x1B[0m World' ;
expect ( stripAnsi ( textWithAnsi ) ) . toBe ( 'Hello World' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle multiple ANSI codes' , async ( ) = > {
2025-08-13 17:33:01 -07:00
const textWithMultipleAnsi = '\x1B[1m\x1B[34mBold Blue\x1B[0m Text' ;
expect ( stripAnsi ( textWithMultipleAnsi ) ) . toBe ( 'Bold Blue Text' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not modify text without ANSI codes' , async ( ) = > {
2025-08-13 17:33:01 -07:00
const plainText = 'Plain text' ;
expect ( stripAnsi ( plainText ) ) . toBe ( 'Plain text' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle empty string' , async ( ) = > {
2025-08-13 17:33:01 -07:00
expect ( stripAnsi ( '' ) ) . toBe ( '' ) ;
} ) ;
2025-06-16 06:25:11 +00:00
} ) ;
2025-09-17 15:37:13 -07:00
describe ( 'Memoization' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should keep action references stable across re-renders' , async ( ) = > {
const { result , rerender } = await renderHook ( ( ) = >
2026-02-09 13:19:51 -08:00
useTextBuffer ( { viewport } ) ,
2025-09-17 15:37:13 -07:00
) ;
const initialInsert = result . current . insert ;
const initialBackspace = result . current . backspace ;
const initialMove = result . current . move ;
const initialHandleInput = result . current . handleInput ;
rerender ( ) ;
expect ( result . current . insert ) . toBe ( initialInsert ) ;
expect ( result . current . backspace ) . toBe ( initialBackspace ) ;
expect ( result . current . move ) . toBe ( initialMove ) ;
expect ( result . current . handleInput ) . toBe ( initialHandleInput ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should have memoized actions that operate on the latest state' , async ( ) = > {
const { result } = await renderHook ( ( ) = > useTextBuffer ( { viewport } ) ) ;
2025-09-17 15:37:13 -07:00
// Store a reference to the memoized insert function.
const memoizedInsert = result . current . insert ;
// Update the buffer state.
act ( ( ) = > {
result . current . insert ( 'hello' ) ;
} ) ;
expect ( getBufferState ( result ) . text ) . toBe ( 'hello' ) ;
// Now, call the original memoized function reference.
act ( ( ) = > {
memoizedInsert ( ' world' ) ;
} ) ;
// It should have operated on the updated state.
expect ( getBufferState ( result ) . text ) . toBe ( 'hello world' ) ;
} ) ;
} ) ;
2025-10-29 18:58:08 -07:00
describe ( 'singleLine mode' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should not insert a newline character when singleLine is true' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-10-29 18:58:08 -07:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
2025-10-29 18:58:08 -07:00
singleLine : true ,
} ) ,
) ;
act ( ( ) = > result . current . insert ( '\n' ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( '' ) ;
expect ( state . lines ) . toEqual ( [ '' ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not create a new line when newline() is called and singleLine is true' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-10-29 18:58:08 -07:00
useTextBuffer ( {
initialText : 'ab' ,
viewport ,
2026-02-09 13:19:51 -08:00
2025-10-29 18:58:08 -07:00
singleLine : true ,
} ) ,
) ;
act ( ( ) = > result . current . move ( 'end' ) ) ; // cursor at [0,2]
act ( ( ) = > result . current . newline ( ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'ab' ) ;
expect ( state . lines ) . toEqual ( [ 'ab' ] ) ;
expect ( state . cursor ) . toEqual ( [ 0 , 2 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not handle "Enter" key as newline when singleLine is true' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-10-29 18:58:08 -07:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
2025-10-29 18:58:08 -07:00
singleLine : true ,
} ) ,
) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-10-29 18:58:08 -07:00
result . current . handleInput ( {
2026-03-10 02:32:40 +00:00
name : 'enter' ,
2025-10-29 18:58:08 -07:00
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : true ,
2025-10-29 18:58:08 -07:00
sequence : '\r' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-10-29 18:58:08 -07:00
expect ( getBufferState ( result ) . lines ) . toEqual ( [ '' ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should not print anything for function keys when singleLine is true' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-11-10 10:56:05 -08:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
2025-11-10 10:56:05 -08:00
singleLine : true ,
} ) ,
) ;
2026-01-29 23:31:47 -08:00
act ( ( ) = > {
2025-11-10 10:56:05 -08:00
result . current . handleInput ( {
name : 'f1' ,
shift : false ,
2026-01-21 10:13:26 -08:00
alt : false ,
ctrl : false ,
cmd : false ,
2025-11-10 10:56:05 -08:00
insertable : false ,
sequence : '\u001bOP' ,
2026-01-29 23:31:47 -08:00
} ) ;
} ) ;
2025-11-10 10:56:05 -08:00
expect ( getBufferState ( result ) . lines ) . toEqual ( [ '' ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should strip newlines from pasted text when singleLine is true' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-10-29 18:58:08 -07:00
useTextBuffer ( {
viewport ,
2026-02-09 13:19:51 -08:00
2025-10-29 18:58:08 -07:00
singleLine : true ,
} ) ,
) ;
act ( ( ) = > result . current . insert ( 'hello\nworld' , { paste : true } ) ) ;
const state = getBufferState ( result ) ;
expect ( state . text ) . toBe ( 'helloworld' ) ;
expect ( state . lines ) . toEqual ( [ 'helloworld' ] ) ;
} ) ;
} ) ;
2025-05-20 16:50:32 -07:00
} ) ;
describe ( 'offsetToLogicalPos' , ( ) = > {
2025-11-03 23:40:57 +05:30
it . each ( [
{ text : 'any text' , offset : 0 , expected : [ 0 , 0 ] , desc : 'offset 0' } ,
{ text : 'hello' , offset : 0 , expected : [ 0 , 0 ] , desc : 'single line start' } ,
{ text : 'hello' , offset : 2 , expected : [ 0 , 2 ] , desc : 'single line middle' } ,
{ text : 'hello' , offset : 5 , expected : [ 0 , 5 ] , desc : 'single line end' } ,
{ text : 'hello' , offset : 10 , expected : [ 0 , 5 ] , desc : 'beyond end clamps' } ,
{
text : 'a\n\nc' ,
offset : 0 ,
expected : [ 0 , 0 ] ,
desc : 'empty lines - first char' ,
} ,
{
text : 'a\n\nc' ,
offset : 1 ,
expected : [ 0 , 1 ] ,
desc : 'empty lines - end of first' ,
} ,
{
text : 'a\n\nc' ,
offset : 2 ,
expected : [ 1 , 0 ] ,
desc : 'empty lines - empty line' ,
} ,
{
text : 'a\n\nc' ,
offset : 3 ,
expected : [ 2 , 0 ] ,
desc : 'empty lines - last line start' ,
} ,
{
text : 'a\n\nc' ,
offset : 4 ,
expected : [ 2 , 1 ] ,
desc : 'empty lines - last line end' ,
} ,
{
text : 'hello\n' ,
offset : 5 ,
expected : [ 0 , 5 ] ,
desc : 'newline end - before newline' ,
} ,
{
text : 'hello\n' ,
offset : 6 ,
expected : [ 1 , 0 ] ,
desc : 'newline end - after newline' ,
} ,
{
text : 'hello\n' ,
offset : 7 ,
expected : [ 1 , 0 ] ,
desc : 'newline end - beyond' ,
} ,
{
text : '\nhello' ,
offset : 0 ,
expected : [ 0 , 0 ] ,
desc : 'newline start - first line' ,
} ,
{
text : '\nhello' ,
offset : 1 ,
expected : [ 1 , 0 ] ,
desc : 'newline start - second line' ,
} ,
{
text : '\nhello' ,
offset : 3 ,
expected : [ 1 , 2 ] ,
desc : 'newline start - middle of second' ,
} ,
{ text : '' , offset : 0 , expected : [ 0 , 0 ] , desc : 'empty string at 0' } ,
{ text : '' , offset : 5 , expected : [ 0 , 0 ] , desc : 'empty string beyond' } ,
{
text : '你好\n世界' ,
offset : 0 ,
expected : [ 0 , 0 ] ,
desc : 'unicode - start' ,
} ,
{
text : '你好\n世界' ,
offset : 1 ,
expected : [ 0 , 1 ] ,
desc : 'unicode - after first char' ,
} ,
{
text : '你好\n世界' ,
offset : 2 ,
expected : [ 0 , 2 ] ,
desc : 'unicode - end first line' ,
} ,
{
text : '你好\n世界' ,
offset : 3 ,
expected : [ 1 , 0 ] ,
desc : 'unicode - second line start' ,
} ,
{
text : '你好\n世界' ,
offset : 4 ,
expected : [ 1 , 1 ] ,
desc : 'unicode - second line middle' ,
} ,
{
text : '你好\n世界' ,
offset : 5 ,
expected : [ 1 , 2 ] ,
desc : 'unicode - second line end' ,
} ,
{
text : '你好\n世界' ,
offset : 6 ,
expected : [ 1 , 2 ] ,
desc : 'unicode - beyond' ,
} ,
{
text : 'abc\ndef' ,
offset : 3 ,
expected : [ 0 , 3 ] ,
desc : 'at newline - end of line' ,
} ,
{
text : 'abc\ndef' ,
offset : 4 ,
expected : [ 1 , 0 ] ,
desc : 'at newline - after newline' ,
} ,
{ text : '🐶🐱' , offset : 0 , expected : [ 0 , 0 ] , desc : 'emoji - start' } ,
{ text : '🐶🐱' , offset : 1 , expected : [ 0 , 1 ] , desc : 'emoji - middle' } ,
{ text : '🐶🐱' , offset : 2 , expected : [ 0 , 2 ] , desc : 'emoji - end' } ,
2026-03-20 20:08:29 +00:00
] ) ( 'should handle $desc' , async ( { text , offset , expected } ) = > {
2025-11-03 23:40:57 +05:30
expect ( offsetToLogicalPos ( text , offset ) ) . toEqual ( expected ) ;
2025-05-20 16:50:32 -07:00
} ) ;
2025-11-03 23:40:57 +05:30
describe ( 'multi-line text' , ( ) = > {
2025-05-20 16:50:32 -07:00
const text = 'hello\nworld\n123' ;
2025-11-03 23:40:57 +05:30
it . each ( [
{ offset : 0 , expected : [ 0 , 0 ] , desc : 'start of first line' } ,
{ offset : 3 , expected : [ 0 , 3 ] , desc : 'middle of first line' } ,
{ offset : 5 , expected : [ 0 , 5 ] , desc : 'end of first line' } ,
{ offset : 6 , expected : [ 1 , 0 ] , desc : 'start of second line' } ,
{ offset : 8 , expected : [ 1 , 2 ] , desc : 'middle of second line' } ,
{ offset : 11 , expected : [ 1 , 5 ] , desc : 'end of second line' } ,
{ offset : 12 , expected : [ 2 , 0 ] , desc : 'start of third line' } ,
{ offset : 13 , expected : [ 2 , 1 ] , desc : 'middle of third line' } ,
{ offset : 15 , expected : [ 2 , 3 ] , desc : 'end of third line' } ,
{ offset : 20 , expected : [ 2 , 3 ] , desc : 'beyond end' } ,
] ) (
'should return $expected for $desc (offset $offset)' ,
( { offset , expected } ) = > {
expect ( offsetToLogicalPos ( text , offset ) ) . toEqual ( expected ) ;
} ,
) ;
2025-05-20 16:50:32 -07:00
} ) ;
2025-05-16 11:58:37 -07:00
} ) ;
2025-07-25 15:36:42 -07:00
describe ( 'logicalPosToOffset' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should convert row/col position to offset correctly' , async ( ) = > {
2025-07-25 15:36:42 -07:00
const lines = [ 'hello' , 'world' , '123' ] ;
// Line 0: "hello" (5 chars)
expect ( logicalPosToOffset ( lines , 0 , 0 ) ) . toBe ( 0 ) ; // Start of 'hello'
expect ( logicalPosToOffset ( lines , 0 , 3 ) ) . toBe ( 3 ) ; // 'l' in 'hello'
expect ( logicalPosToOffset ( lines , 0 , 5 ) ) . toBe ( 5 ) ; // End of 'hello'
// Line 1: "world" (5 chars), offset starts at 6 (5 + 1 for newline)
expect ( logicalPosToOffset ( lines , 1 , 0 ) ) . toBe ( 6 ) ; // Start of 'world'
expect ( logicalPosToOffset ( lines , 1 , 2 ) ) . toBe ( 8 ) ; // 'r' in 'world'
expect ( logicalPosToOffset ( lines , 1 , 5 ) ) . toBe ( 11 ) ; // End of 'world'
// Line 2: "123" (3 chars), offset starts at 12 (5 + 1 + 5 + 1)
expect ( logicalPosToOffset ( lines , 2 , 0 ) ) . toBe ( 12 ) ; // Start of '123'
expect ( logicalPosToOffset ( lines , 2 , 1 ) ) . toBe ( 13 ) ; // '2' in '123'
expect ( logicalPosToOffset ( lines , 2 , 3 ) ) . toBe ( 15 ) ; // End of '123'
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle empty lines' , async ( ) = > {
2025-07-25 15:36:42 -07:00
const lines = [ 'a' , '' , 'c' ] ;
expect ( logicalPosToOffset ( lines , 0 , 0 ) ) . toBe ( 0 ) ; // 'a'
expect ( logicalPosToOffset ( lines , 0 , 1 ) ) . toBe ( 1 ) ; // End of 'a'
expect ( logicalPosToOffset ( lines , 1 , 0 ) ) . toBe ( 2 ) ; // Empty line
expect ( logicalPosToOffset ( lines , 2 , 0 ) ) . toBe ( 3 ) ; // 'c'
expect ( logicalPosToOffset ( lines , 2 , 1 ) ) . toBe ( 4 ) ; // End of 'c'
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle single empty line' , async ( ) = > {
2025-07-25 15:36:42 -07:00
const lines = [ '' ] ;
expect ( logicalPosToOffset ( lines , 0 , 0 ) ) . toBe ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should be inverse of offsetToLogicalPos' , async ( ) = > {
2025-07-25 15:36:42 -07:00
const lines = [ 'hello' , 'world' , '123' ] ;
const text = lines . join ( '\n' ) ;
// Test round-trip conversion
for ( let offset = 0 ; offset <= text . length ; offset ++ ) {
const [ row , col ] = offsetToLogicalPos ( text , offset ) ;
const convertedOffset = logicalPosToOffset ( lines , row , col ) ;
expect ( convertedOffset ) . toBe ( offset ) ;
}
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle out-of-bounds positions' , async ( ) = > {
2025-07-25 15:36:42 -07:00
const lines = [ 'hello' ] ;
// Beyond end of line
expect ( logicalPosToOffset ( lines , 0 , 10 ) ) . toBe ( 5 ) ; // Clamps to end of line
// Beyond array bounds - should clamp to the last line
expect ( logicalPosToOffset ( lines , 5 , 0 ) ) . toBe ( 0 ) ; // Clamps to start of last line (row 0)
expect ( logicalPosToOffset ( lines , 5 , 10 ) ) . toBe ( 5 ) ; // Clamps to end of last line
} ) ;
} ) ;
2025-10-17 16:16:12 -07:00
const createTestState = (
lines : string [ ] ,
cursorRow : number ,
cursorCol : number ,
viewportWidth = 80 ,
) : TextBufferState = > {
const text = lines . join ( '\n' ) ;
let state = textBufferReducer ( initialState , {
type : 'set_text' ,
payload : text ,
} ) ;
state = textBufferReducer ( state , {
type : 'set_cursor' ,
payload : { cursorRow , cursorCol , preferredCol : null } ,
} ) ;
state = textBufferReducer ( state , {
type : 'set_viewport' ,
payload : { width : viewportWidth , height : 24 } ,
} ) ;
return state ;
} ;
2025-07-25 15:36:42 -07:00
describe ( 'textBufferReducer vim operations' , ( ) = > {
describe ( 'vim_delete_line' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should delete a single line including newline in multi-line text' , async ( ) = > {
2025-10-17 16:16:12 -07:00
const state = createTestState ( [ 'line1' , 'line2' , 'line3' ] , 1 , 2 ) ;
2025-07-25 15:36:42 -07:00
const action : TextBufferAction = {
type : 'vim_delete_line' ,
payload : { count : 1 } ,
} ;
2025-10-17 16:16:12 -07:00
const result = textBufferReducer ( state , action ) ;
2025-07-31 16:16:29 -07:00
expect ( result ) . toHaveOnlyValidCharacters ( ) ;
2025-07-25 15:36:42 -07:00
// After deleting line2, we should have line1 and line3, with cursor on line3 (now at index 1)
expect ( result . lines ) . toEqual ( [ 'line1' , 'line3' ] ) ;
expect ( result . cursorRow ) . toBe ( 1 ) ;
expect ( result . cursorCol ) . toBe ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should delete multiple lines when count > 1' , async ( ) = > {
2025-10-17 16:16:12 -07:00
const state = createTestState ( [ 'line1' , 'line2' , 'line3' , 'line4' ] , 1 , 0 ) ;
2025-07-25 15:36:42 -07:00
const action : TextBufferAction = {
type : 'vim_delete_line' ,
payload : { count : 2 } ,
} ;
2025-10-17 16:16:12 -07:00
const result = textBufferReducer ( state , action ) ;
2025-07-31 16:16:29 -07:00
expect ( result ) . toHaveOnlyValidCharacters ( ) ;
2025-07-25 15:36:42 -07:00
// Should delete line2 and line3, leaving line1 and line4
expect ( result . lines ) . toEqual ( [ 'line1' , 'line4' ] ) ;
expect ( result . cursorRow ) . toBe ( 1 ) ;
expect ( result . cursorCol ) . toBe ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should clear single line content when only one line exists' , async ( ) = > {
2025-10-17 16:16:12 -07:00
const state = createTestState ( [ 'only line' ] , 0 , 5 ) ;
2025-07-25 15:36:42 -07:00
const action : TextBufferAction = {
type : 'vim_delete_line' ,
payload : { count : 1 } ,
} ;
2025-10-17 16:16:12 -07:00
const result = textBufferReducer ( state , action ) ;
2025-07-31 16:16:29 -07:00
expect ( result ) . toHaveOnlyValidCharacters ( ) ;
2025-07-25 15:36:42 -07:00
// Should clear the line content but keep the line
expect ( result . lines ) . toEqual ( [ '' ] ) ;
expect ( result . cursorRow ) . toBe ( 0 ) ;
expect ( result . cursorCol ) . toBe ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle deleting the last line properly' , async ( ) = > {
2025-10-17 16:16:12 -07:00
const state = createTestState ( [ 'line1' , 'line2' ] , 1 , 0 ) ;
2025-07-25 15:36:42 -07:00
const action : TextBufferAction = {
type : 'vim_delete_line' ,
payload : { count : 1 } ,
} ;
2025-10-17 16:16:12 -07:00
const result = textBufferReducer ( state , action ) ;
2025-07-31 16:16:29 -07:00
expect ( result ) . toHaveOnlyValidCharacters ( ) ;
2025-07-25 15:36:42 -07:00
// Should delete the last line completely, not leave empty line
expect ( result . lines ) . toEqual ( [ 'line1' ] ) ;
expect ( result . cursorRow ) . toBe ( 0 ) ;
expect ( result . cursorCol ) . toBe ( 0 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle deleting all lines and maintain valid state for subsequent paste' , async ( ) = > {
2025-10-17 16:16:12 -07:00
const state = createTestState ( [ 'line1' , 'line2' , 'line3' , 'line4' ] , 0 , 0 ) ;
2025-07-25 15:36:42 -07:00
// Delete all 4 lines with 4dd
const deleteAction : TextBufferAction = {
type : 'vim_delete_line' ,
payload : { count : 4 } ,
} ;
2025-10-17 16:16:12 -07:00
const afterDelete = textBufferReducer ( state , deleteAction ) ;
2025-07-31 16:16:29 -07:00
expect ( afterDelete ) . toHaveOnlyValidCharacters ( ) ;
2025-07-25 15:36:42 -07:00
// After deleting all lines, should have one empty line
expect ( afterDelete . lines ) . toEqual ( [ '' ] ) ;
expect ( afterDelete . cursorRow ) . toBe ( 0 ) ;
expect ( afterDelete . cursorCol ) . toBe ( 0 ) ;
// Now paste multiline content - this should work correctly
const pasteAction : TextBufferAction = {
type : 'insert' ,
payload : 'new1\nnew2\nnew3\nnew4' ,
} ;
const afterPaste = textBufferReducer ( afterDelete , pasteAction ) ;
2025-07-31 16:16:29 -07:00
expect ( afterPaste ) . toHaveOnlyValidCharacters ( ) ;
2025-07-25 15:36:42 -07:00
// All lines including the first one should be present
expect ( afterPaste . lines ) . toEqual ( [ 'new1' , 'new2' , 'new3' , 'new4' ] ) ;
expect ( afterPaste . cursorRow ) . toBe ( 3 ) ;
expect ( afterPaste . cursorCol ) . toBe ( 4 ) ;
} ) ;
} ) ;
} ) ;
2025-08-11 11:58:32 -07:00
describe ( 'Unicode helper functions' , ( ) = > {
describe ( 'findWordEndInLine with Unicode' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should handle combining characters' , async ( ) = > {
2025-08-11 11:58:32 -07:00
// café with combining accent
const cafeWithCombining = 'cafe\u0301' ;
const result = findWordEndInLine ( cafeWithCombining + ' test' , 0 ) ;
expect ( result ) . toBe ( 3 ) ; // End of 'café' at base character 'e', not combining accent
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle precomposed characters with diacritics' , async ( ) = > {
2025-08-11 11:58:32 -07:00
// café with precomposed é (U+00E9)
const cafePrecomposed = 'café' ;
const result = findWordEndInLine ( cafePrecomposed + ' test' , 0 ) ;
expect ( result ) . toBe ( 3 ) ; // End of 'café' at precomposed character 'é'
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should return null when no word end found' , async ( ) = > {
2025-08-11 11:58:32 -07:00
const result = findWordEndInLine ( ' ' , 0 ) ;
expect ( result ) . toBeNull ( ) ; // No word end found in whitespace-only string string
} ) ;
} ) ;
describe ( 'findNextWordStartInLine with Unicode' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should handle right-to-left text' , async ( ) = > {
2025-08-11 11:58:32 -07:00
const result = findNextWordStartInLine ( 'hello مرحبا world' , 0 ) ;
expect ( result ) . toBe ( 6 ) ; // Start of Arabic word
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle Chinese characters' , async ( ) = > {
2025-08-11 11:58:32 -07:00
const result = findNextWordStartInLine ( 'hello 你好 world' , 0 ) ;
expect ( result ) . toBe ( 6 ) ; // Start of Chinese word
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should return null at end of line' , async ( ) = > {
2025-08-11 11:58:32 -07:00
const result = findNextWordStartInLine ( 'hello' , 10 ) ;
expect ( result ) . toBeNull ( ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle combining characters' , async ( ) = > {
2025-08-11 11:58:32 -07:00
// café with combining accent + next word
const textWithCombining = 'cafe\u0301 test' ;
const result = findNextWordStartInLine ( textWithCombining , 0 ) ;
expect ( result ) . toBe ( 6 ) ; // Start of 'test' after 'café ' (combining char makes string longer)
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle precomposed characters with diacritics' , async ( ) = > {
2025-08-11 11:58:32 -07:00
// café with precomposed é + next word
const textPrecomposed = 'café test' ;
const result = findNextWordStartInLine ( textPrecomposed , 0 ) ;
expect ( result ) . toBe ( 5 ) ; // Start of 'test' after 'café '
} ) ;
} ) ;
describe ( 'isWordCharStrict with Unicode' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should return true for ASCII word characters' , async ( ) = > {
2025-08-11 11:58:32 -07:00
expect ( isWordCharStrict ( 'a' ) ) . toBe ( true ) ;
expect ( isWordCharStrict ( 'Z' ) ) . toBe ( true ) ;
expect ( isWordCharStrict ( '0' ) ) . toBe ( true ) ;
expect ( isWordCharStrict ( '_' ) ) . toBe ( true ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should return false for punctuation' , async ( ) = > {
2025-08-11 11:58:32 -07:00
expect ( isWordCharStrict ( '.' ) ) . toBe ( false ) ;
expect ( isWordCharStrict ( ',' ) ) . toBe ( false ) ;
expect ( isWordCharStrict ( '!' ) ) . toBe ( false ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should return true for non-Latin scripts' , async ( ) = > {
2025-08-11 11:58:32 -07:00
expect ( isWordCharStrict ( '你' ) ) . toBe ( true ) ; // Chinese character
expect ( isWordCharStrict ( 'م' ) ) . toBe ( true ) ; // Arabic character
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should return false for whitespace' , async ( ) = > {
2025-08-11 11:58:32 -07:00
expect ( isWordCharStrict ( ' ' ) ) . toBe ( false ) ;
expect ( isWordCharStrict ( '\t' ) ) . toBe ( false ) ;
} ) ;
} ) ;
describe ( 'cpLen with Unicode' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should handle combining characters' , async ( ) = > {
2025-08-11 11:58:32 -07:00
expect ( cpLen ( 'é' ) ) . toBe ( 1 ) ; // Precomposed
expect ( cpLen ( 'e\u0301' ) ) . toBe ( 2 ) ; // e + combining acute
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle Chinese and Arabic text' , async ( ) = > {
2025-08-11 11:58:32 -07:00
expect ( cpLen ( 'hello 你好 world' ) ) . toBe ( 14 ) ; // 5 + 1 + 2 + 1 + 5 = 14
expect ( cpLen ( 'hello مرحبا world' ) ) . toBe ( 17 ) ;
} ) ;
} ) ;
2025-12-04 08:16:11 +08:00
describe ( 'useTextBuffer CJK Navigation' , ( ) = > {
const viewport = { width : 80 , height : 24 } ;
2026-03-20 20:08:29 +00:00
it ( 'should navigate by word in Chinese' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-12-04 08:16:11 +08:00
useTextBuffer ( {
initialText : '你好世界' ,
initialCursorOffset : 4 , // End of string
viewport ,
} ) ,
) ;
// Initial state: cursor at end (index 2 in code points if 4 is length? wait. length is 2 code points? No. '你好世界' length is 4.)
// '你好世界' length is 4. Code points length is 4.
// Move word left
act ( ( ) = > {
result . current . move ( 'wordLeft' ) ;
} ) ;
// Should be at start of "世界" (index 2)
// "你好世界" -> "你好" | "世界"
expect ( result . current . cursor [ 1 ] ) . toBe ( 2 ) ;
// Move word left again
act ( ( ) = > {
result . current . move ( 'wordLeft' ) ;
} ) ;
// Should be at start of "你好" (index 0)
expect ( result . current . cursor [ 1 ] ) . toBe ( 0 ) ;
// Move word left again (should stay at 0)
act ( ( ) = > {
result . current . move ( 'wordLeft' ) ;
} ) ;
expect ( result . current . cursor [ 1 ] ) . toBe ( 0 ) ;
// Move word right
act ( ( ) = > {
result . current . move ( 'wordRight' ) ;
} ) ;
// Should be at end of "你好" (index 2)
expect ( result . current . cursor [ 1 ] ) . toBe ( 2 ) ;
// Move word right again
act ( ( ) = > {
result . current . move ( 'wordRight' ) ;
} ) ;
// Should be at end of "世界" (index 4)
expect ( result . current . cursor [ 1 ] ) . toBe ( 4 ) ;
// Move word right again (should stay at end)
act ( ( ) = > {
result . current . move ( 'wordRight' ) ;
} ) ;
expect ( result . current . cursor [ 1 ] ) . toBe ( 4 ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should navigate mixed English and Chinese' , async ( ) = > {
const { result } = await renderHook ( ( ) = >
2025-12-04 08:16:11 +08:00
useTextBuffer ( {
initialText : 'Hello你好World' ,
initialCursorOffset : 10 , // End
viewport ,
} ) ,
) ;
// Hello (5) + 你好 (2) + World (5) = 12 chars.
// initialCursorOffset 10? 'Hello你好World'.length is 12.
// Let's set it to end.
act ( ( ) = > {
result . current . move ( 'end' ) ;
} ) ;
expect ( result . current . cursor [ 1 ] ) . toBe ( 12 ) ;
// wordLeft -> start of "World" (index 7)
act ( ( ) = > result . current . move ( 'wordLeft' ) ) ;
expect ( result . current . cursor [ 1 ] ) . toBe ( 7 ) ;
// wordLeft -> start of "你好" (index 5)
act ( ( ) = > result . current . move ( 'wordLeft' ) ) ;
expect ( result . current . cursor [ 1 ] ) . toBe ( 5 ) ;
// wordLeft -> start of "Hello" (index 0)
act ( ( ) = > result . current . move ( 'wordLeft' ) ) ;
expect ( result . current . cursor [ 1 ] ) . toBe ( 0 ) ;
// wordLeft -> start of line (should stay at 0)
act ( ( ) = > result . current . move ( 'wordLeft' ) ) ;
expect ( result . current . cursor [ 1 ] ) . toBe ( 0 ) ;
} ) ;
} ) ;
2025-08-11 11:58:32 -07:00
} ) ;
2025-12-23 12:46:09 -08:00
2026-02-12 18:27:56 -08:00
const mockPlatform = ( platform : string ) = > {
vi . stubGlobal (
'process' ,
Object . create ( process , {
platform : {
get : ( ) = > platform ,
} ,
} ) ,
) ;
} ;
2025-12-23 12:46:09 -08:00
describe ( 'Transformation Utilities' , ( ) = > {
2026-01-16 09:33:13 -08:00
afterEach ( ( ) = > {
vi . restoreAllMocks ( ) ;
2026-02-12 18:27:56 -08:00
vi . unstubAllGlobals ( ) ;
2026-01-16 09:33:13 -08:00
} ) ;
2025-12-23 12:46:09 -08:00
describe ( 'getTransformedImagePath' , ( ) = > {
2026-02-12 18:27:56 -08:00
beforeEach ( ( ) = > mockPlatform ( 'linux' ) ) ;
2026-03-20 20:08:29 +00:00
it ( 'should transform a simple image path' , async ( ) = > {
2025-12-23 12:46:09 -08:00
expect ( getTransformedImagePath ( '@test.png' ) ) . toBe ( '[Image test.png]' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle paths with directories' , async ( ) = > {
2025-12-23 12:46:09 -08:00
expect ( getTransformedImagePath ( '@path/to/image.jpg' ) ) . toBe (
'[Image image.jpg]' ,
) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should truncate long filenames' , async ( ) = > {
2025-12-23 12:46:09 -08:00
expect ( getTransformedImagePath ( '@verylongfilename1234567890.png' ) ) . toBe (
'[Image ...1234567890.png]' ,
) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle different image extensions' , async ( ) = > {
2025-12-23 12:46:09 -08:00
expect ( getTransformedImagePath ( '@test.jpg' ) ) . toBe ( '[Image test.jpg]' ) ;
expect ( getTransformedImagePath ( '@test.jpeg' ) ) . toBe ( '[Image test.jpeg]' ) ;
expect ( getTransformedImagePath ( '@test.gif' ) ) . toBe ( '[Image test.gif]' ) ;
expect ( getTransformedImagePath ( '@test.webp' ) ) . toBe ( '[Image test.webp]' ) ;
expect ( getTransformedImagePath ( '@test.svg' ) ) . toBe ( '[Image test.svg]' ) ;
expect ( getTransformedImagePath ( '@test.bmp' ) ) . toBe ( '[Image test.bmp]' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle POSIX-style forward-slash paths on any platform' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const input = '@C:/Users/foo/screenshots/image2x.png' ;
expect ( getTransformedImagePath ( input ) ) . toBe ( '[Image image2x.png]' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle escaped spaces in paths' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const input = '@path/to/my\\ file.png' ;
expect ( getTransformedImagePath ( input ) ) . toBe ( '[Image my file.png]' ) ;
} ) ;
} ) ;
describe ( 'getTransformationsForLine' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should find transformations in a line' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const line = 'Check out @test.png and @another.jpg' ;
const result = calculateTransformationsForLine ( line ) ;
expect ( result ) . toHaveLength ( 2 ) ;
expect ( result [ 0 ] ) . toMatchObject ( {
logicalText : '@test.png' ,
collapsedText : '[Image test.png]' ,
} ) ;
expect ( result [ 1 ] ) . toMatchObject ( {
logicalText : '@another.jpg' ,
collapsedText : '[Image another.jpg]' ,
} ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle no transformations' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const line = 'Just some regular text' ;
const result = calculateTransformationsForLine ( line ) ;
expect ( result ) . toEqual ( [ ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle empty line' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const result = calculateTransformationsForLine ( '' ) ;
expect ( result ) . toEqual ( [ ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should keep adjacent image paths as separate transformations' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const line = '@a.png@b.png@c.png' ;
const result = calculateTransformationsForLine ( line ) ;
expect ( result ) . toHaveLength ( 3 ) ;
expect ( result [ 0 ] . logicalText ) . toBe ( '@a.png' ) ;
expect ( result [ 1 ] . logicalText ) . toBe ( '@b.png' ) ;
expect ( result [ 2 ] . logicalText ) . toBe ( '@c.png' ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle multiple transformations in a row' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const line = '@a.png @b.png @c.png' ;
const result = calculateTransformationsForLine ( line ) ;
expect ( result ) . toHaveLength ( 3 ) ;
} ) ;
} ) ;
describe ( 'getTransformUnderCursor' , ( ) = > {
2026-01-22 07:05:38 -08:00
const transformations : Transformation [ ] = [
2025-12-23 12:46:09 -08:00
{
logStart : 5 ,
logEnd : 14 ,
logicalText : '@test.png' ,
collapsedText : '[Image @test.png]' ,
2026-01-22 07:05:38 -08:00
type : 'image' ,
2025-12-23 12:46:09 -08:00
} ,
{
logStart : 20 ,
logEnd : 31 ,
logicalText : '@another.jpg' ,
collapsedText : '[Image @another.jpg]' ,
2026-01-22 07:05:38 -08:00
type : 'image' ,
2025-12-23 12:46:09 -08:00
} ,
] ;
2026-03-20 20:08:29 +00:00
it ( 'should find transformation when cursor is inside it' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const result = getTransformUnderCursor ( 0 , 7 , [ transformations ] ) ;
expect ( result ) . toEqual ( transformations [ 0 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should find transformation when cursor is at start' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const result = getTransformUnderCursor ( 0 , 5 , [ transformations ] ) ;
expect ( result ) . toEqual ( transformations [ 0 ] ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should NOT find transformation when cursor is at end' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const result = getTransformUnderCursor ( 0 , 14 , [ transformations ] ) ;
2026-01-27 06:19:54 -08:00
expect ( result ) . toBeNull ( ) ;
2025-12-23 12:46:09 -08:00
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should return null when cursor is not on a transformation' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const result = getTransformUnderCursor ( 0 , 2 , [ transformations ] ) ;
expect ( result ) . toBeNull ( ) ;
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle empty transformations array' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const result = getTransformUnderCursor ( 0 , 5 , [ ] ) ;
expect ( result ) . toBeNull ( ) ;
} ) ;
2026-01-27 06:19:54 -08:00
2026-03-20 20:08:29 +00:00
it ( 'regression: should not find paste transformation when clicking one character after it' , async ( ) = > {
2026-01-27 06:19:54 -08:00
const pasteId = '[Pasted Text: 5 lines]' ;
const line = pasteId + ' suffix' ;
const transformations = calculateTransformationsForLine ( line ) ;
const pasteTransform = transformations . find ( ( t ) = > t . type === 'paste' ) ;
expect ( pasteTransform ) . toBeDefined ( ) ;
const endPos = pasteTransform ! . logEnd ;
// Position strictly at end should be null
expect ( getTransformUnderCursor ( 0 , endPos , [ transformations ] ) ) . toBeNull ( ) ;
// Position inside should be found
expect ( getTransformUnderCursor ( 0 , endPos - 1 , [ transformations ] ) ) . toEqual (
pasteTransform ,
) ;
} ) ;
2025-12-23 12:46:09 -08:00
} ) ;
describe ( 'calculateTransformedLine' , ( ) = > {
2026-03-20 20:08:29 +00:00
it ( 'should transform a line with one transformation' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const line = 'Check out @test.png' ;
const transformations = calculateTransformationsForLine ( line ) ;
const result = calculateTransformedLine ( line , 0 , [ 0 , 0 ] , transformations ) ;
expect ( result . transformedLine ) . toBe ( 'Check out [Image test.png]' ) ;
expect ( result . transformedToLogMap ) . toHaveLength ( 27 ) ; // Length includes all characters in the transformed line
// Test that we have proper mappings
expect ( result . transformedToLogMap [ 0 ] ) . toBe ( 0 ) ; // 'C'
expect ( result . transformedToLogMap [ 9 ] ) . toBe ( 9 ) ; // ' ' before transformation
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle cursor inside transformation' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const line = 'Check out @test.png' ;
const transformations = calculateTransformationsForLine ( line ) ;
// Cursor at '@' (position 10 in the line)
const result = calculateTransformedLine (
line ,
0 ,
[ 0 , 10 ] ,
transformations ,
) ;
// Should show full path when cursor is on it
expect ( result . transformedLine ) . toBe ( 'Check out @test.png' ) ;
// When expanded, each character maps to itself
expect ( result . transformedToLogMap [ 10 ] ) . toBe ( 10 ) ; // '@'
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle line with no transformations' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const line = 'Just some text' ;
const result = calculateTransformedLine ( line , 0 , [ 0 , 0 ] , [ ] ) ;
expect ( result . transformedLine ) . toBe ( line ) ;
// Each visual position should map directly to logical position + trailing
expect ( result . transformedToLogMap ) . toHaveLength ( 15 ) ; // 14 chars + 1 trailing
expect ( result . transformedToLogMap [ 0 ] ) . toBe ( 0 ) ;
expect ( result . transformedToLogMap [ 13 ] ) . toBe ( 13 ) ;
expect ( result . transformedToLogMap [ 14 ] ) . toBe ( 14 ) ; // Trailing position
} ) ;
2026-03-20 20:08:29 +00:00
it ( 'should handle empty line' , async ( ) = > {
2025-12-23 12:46:09 -08:00
const result = calculateTransformedLine ( '' , 0 , [ 0 , 0 ] , [ ] ) ;
expect ( result . transformedLine ) . toBe ( '' ) ;
expect ( result . transformedToLogMap ) . toEqual ( [ 0 ] ) ; // Just the trailing position
} ) ;
} ) ;
2026-01-16 09:33:13 -08:00
describe ( 'Layout Caching and Invalidation' , ( ) = > {
it . each ( [
{
desc : 'via setText' ,
actFn : ( result : { current : TextBuffer } ) = >
result . current . setText ( 'changed line' ) ,
expected : 'changed line' ,
} ,
{
desc : 'via replaceRange' ,
actFn : ( result : { current : TextBuffer } ) = >
result . current . replaceRange ( 0 , 0 , 0 , 13 , 'changed line' ) ,
expected : 'changed line' ,
} ,
] ) (
'should invalidate cache when line content changes $desc' ,
2026-03-19 17:05:33 +00:00
async ( { actFn , expected } ) = > {
2026-01-16 09:33:13 -08:00
const viewport = { width : 80 , height : 24 } ;
2026-03-19 17:05:33 +00:00
const { result } = await renderHookWithProviders ( ( ) = >
2026-01-16 09:33:13 -08:00
useTextBuffer ( {
initialText : 'original line' ,
viewport ,
2026-02-09 13:19:51 -08:00
escapePastedPaths : true ,
2026-01-16 09:33:13 -08:00
} ) ,
) ;
const originalLayout = result . current . visualLayout ;
act ( ( ) = > {
actFn ( result ) ;
} ) ;
expect ( result . current . visualLayout ) . not . toBe ( originalLayout ) ;
expect ( result . current . allVisualLines [ 0 ] ) . toBe ( expected ) ;
} ,
) ;
2026-03-19 17:05:33 +00:00
it ( 'should invalidate cache when viewport width changes' , async ( ) = > {
2026-01-16 09:33:13 -08:00
const viewport = { width : 80 , height : 24 } ;
2026-03-19 17:05:33 +00:00
const { result , rerender } = await renderHookWithProviders (
2026-01-16 09:33:13 -08:00
( { vp } ) = >
useTextBuffer ( {
initialText :
'a very long line that will wrap when the viewport is small' ,
viewport : vp ,
2026-02-09 13:19:51 -08:00
escapePastedPaths : true ,
2026-01-16 09:33:13 -08:00
} ) ,
{ initialProps : { vp : viewport } } ,
) ;
const originalLayout = result . current . visualLayout ;
// Shrink viewport to force wrapping change
rerender ( { vp : { width : 10 , height : 24 } } ) ;
expect ( result . current . visualLayout ) . not . toBe ( originalLayout ) ;
expect ( result . current . allVisualLines . length ) . toBeGreaterThan ( 1 ) ;
} ) ;
2026-03-19 17:05:33 +00:00
it ( 'should correctly handle cursor expansion/collapse in cached layout' , async ( ) = > {
2026-01-16 09:33:13 -08:00
const viewport = { width : 80 , height : 24 } ;
const text = 'Check @image.png here' ;
2026-03-19 17:05:33 +00:00
const { result } = await renderHookWithProviders ( ( ) = >
2026-01-16 09:33:13 -08:00
useTextBuffer ( {
initialText : text ,
viewport ,
2026-02-09 13:19:51 -08:00
escapePastedPaths : true ,
2026-01-16 09:33:13 -08:00
} ) ,
) ;
// Cursor at start (collapsed)
act ( ( ) = > {
result . current . moveToOffset ( 0 ) ;
} ) ;
expect ( result . current . allVisualLines [ 0 ] ) . toContain ( '[Image image.png]' ) ;
// Move cursor onto the @path (expanded)
act ( ( ) = > {
result . current . moveToOffset ( 7 ) ; // onto @
} ) ;
expect ( result . current . allVisualLines [ 0 ] ) . toContain ( '@image.png' ) ;
expect ( result . current . allVisualLines [ 0 ] ) . not . toContain (
'[Image image.png]' ,
) ;
// Move cursor away (collapsed again)
act ( ( ) = > {
result . current . moveToOffset ( 0 ) ;
} ) ;
expect ( result . current . allVisualLines [ 0 ] ) . toContain ( '[Image image.png]' ) ;
} ) ;
2026-03-19 17:05:33 +00:00
it ( 'should reuse cache for unchanged lines during editing' , async ( ) = > {
2026-01-16 09:33:13 -08:00
const viewport = { width : 80 , height : 24 } ;
const initialText = 'line 1\nline 2\nline 3' ;
2026-03-19 17:05:33 +00:00
const { result } = await renderHookWithProviders ( ( ) = >
2026-01-16 09:33:13 -08:00
useTextBuffer ( {
initialText ,
viewport ,
2026-02-09 13:19:51 -08:00
escapePastedPaths : true ,
2026-01-16 09:33:13 -08:00
} ) ,
) ;
const layout1 = result . current . visualLayout ;
// Edit line 1
act ( ( ) = > {
result . current . moveToOffset ( 0 ) ;
result . current . insert ( 'X' ) ;
} ) ;
const layout2 = result . current . visualLayout ;
expect ( layout2 ) . not . toBe ( layout1 ) ;
// Verify that visual lines for line 2 and 3 (indices 1 and 2 in visualLines)
// are identical in content if not in object reference (the arrays are rebuilt, but contents are cached)
expect ( result . current . allVisualLines [ 1 ] ) . toBe ( 'line 2' ) ;
expect ( result . current . allVisualLines [ 2 ] ) . toBe ( 'line 3' ) ;
} ) ;
} ) ;
2026-01-27 06:19:54 -08:00
describe ( 'Scroll Regressions' , ( ) = > {
const scrollViewport : Viewport = { width : 80 , height : 5 } ;
2026-03-20 20:08:29 +00:00
it ( 'should not show empty viewport when collapsing a large paste that was scrolled' , async ( ) = > {
2026-01-27 06:19:54 -08:00
const largeContent =
'line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10' ;
const placeholder = '[Pasted Text: 10 lines]' ;
2026-03-20 20:08:29 +00:00
const { result } = await renderHook ( ( ) = >
2026-01-27 06:19:54 -08:00
useTextBuffer ( {
initialText : placeholder ,
viewport : scrollViewport ,
} ) ,
) ;
// Setup: paste large content
act ( ( ) = > {
result . current . setText ( '' ) ;
result . current . insert ( largeContent , { paste : true } ) ;
} ) ;
// Expand it
act ( ( ) = > {
result . current . togglePasteExpansion ( placeholder , 0 , 0 ) ;
} ) ;
// Verify scrolled state
expect ( result . current . visualScrollRow ) . toBe ( 5 ) ;
// Collapse it
act ( ( ) = > {
result . current . togglePasteExpansion ( placeholder , 9 , 0 ) ;
} ) ;
// Verify viewport is NOT empty immediately (clamping in useMemo)
expect ( result . current . allVisualLines . length ) . toBe ( 1 ) ;
expect ( result . current . viewportVisualLines . length ) . toBe ( 1 ) ;
expect ( result . current . viewportVisualLines [ 0 ] ) . toBe ( placeholder ) ;
} ) ;
} ) ;
2025-12-23 12:46:09 -08:00
} ) ;