@@ -9,16 +9,26 @@ import type { NativeSyntheticEvent } from "react-native";
99import { expect , it , vi } from "vite-plus/test" ;
1010
1111import type { NativeTerminalSurfaceProps } from "./nativeTerminalModule" ;
12+ import { TERMINAL_WRITE_MAX_LENGTH } from "./terminalInput" ;
13+ import { createTerminalPasteSession } from "./terminalPaste" ;
1214
1315const mocks = vi . hoisted ( ( ) => ( {
1416 revision : null as number | null ,
1517 initialBuffers : [ ] as string [ ] ,
1618 mounts : vi . fn ( ) ,
1719 unmounts : vi . fn ( ) ,
1820 write : vi . fn ( async ( _data : string , _reset : boolean ) => { } ) ,
21+ paste : ( _text : string ) => { } ,
1922} ) ) ;
2023
21- function RawView ( { ref : nativeRef , initialBuffer, onResize } : NativeTerminalSurfaceProps ) {
24+ function RawView ( { ref : nativeRef , initialBuffer, onResize, onPaste } : NativeTerminalSurfaceProps ) {
25+ useLayoutEffect ( ( ) => {
26+ mocks . paste = ( text ) =>
27+ onPaste ?.( { nativeEvent : { text } } as NativeSyntheticEvent < { text : string } > ) ;
28+ return ( ) => {
29+ mocks . paste = ( ) => { } ;
30+ } ;
31+ } , [ onPaste ] ) ;
2232 useImperativeHandle ( nativeRef , ( ) => ( { writeOutput : mocks . write } ) ) ;
2333 useLayoutEffect ( ( ) => {
2434 mocks . mounts ( ) ;
@@ -81,6 +91,11 @@ function setup(revision: number | null) {
8191 async render (
8292 state : typeof EMPTY_TERMINAL_BUFFER_STATE ,
8393 terminalKey = "environment:thread:term-1" ,
94+ input : {
95+ isRunning ?: boolean ;
96+ readOnly ?: boolean ;
97+ onPaste ?: ( text : string ) => void ;
98+ } = { } ,
8499 ) {
85100 await act ( async ( ) => {
86101 root . render (
@@ -97,6 +112,7 @@ function setup(revision: number | null) {
97112 isRunning
98113 onInput = { ( ) => { } }
99114 onResize = { ( ) => { } }
115+ { ...input }
100116 /> ,
101117 ) ;
102118 } ) ;
@@ -151,3 +167,39 @@ it.each([null, 1])(
151167 }
152168 } ,
153169) ;
170+
171+ it . each ( [
172+ { isRunning : true , readOnly : false } ,
173+ { isRunning : true , readOnly : true } ,
174+ { isRunning : false , readOnly : false } ,
175+ ] ) ( "native paste preserves text and write limits for %j" , async ( { isRunning, readOnly } ) => {
176+ await using harness = setup ( 1 ) ;
177+ const session = createTerminalPasteSession ( ) ;
178+ session . reset ( true ) ;
179+ const writes : string [ ] = [ ] ;
180+ let paste : Promise < void > | undefined ;
181+ await harness . render ( EMPTY_TERMINAL_BUFFER_STATE , undefined , {
182+ isRunning,
183+ readOnly,
184+ onPaste : ( text ) => {
185+ paste = session . paste ( {
186+ readText : async ( ) => text ,
187+ write : async ( data ) => {
188+ writes . push ( data ) ;
189+ return true ;
190+ } ,
191+ onReadError : ( cause ) => {
192+ throw cause ;
193+ } ,
194+ } ) ;
195+ } ,
196+ } ) ;
197+ mocks . paste ( `one\ntwo\u0000${ "x" . repeat ( TERMINAL_WRITE_MAX_LENGTH ) } ` ) ;
198+ await paste ;
199+ expect ( writes . join ( "" ) ) . toBe (
200+ isRunning && ! readOnly ? `one\rtwo ${ "x" . repeat ( TERMINAL_WRITE_MAX_LENGTH ) } ` : "" ,
201+ ) ;
202+ expect ( writes . map ( ( chunk ) => chunk . length ) ) . toEqual (
203+ isRunning && ! readOnly ? [ TERMINAL_WRITE_MAX_LENGTH , 8 ] : [ ] ,
204+ ) ;
205+ } ) ;
0 commit comments