Skip to content

Commit 0406ad1

Browse files
committed
test(mobile): cover native terminal paste delivery
1 parent 8d7e9c6 commit 0406ad1

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

‎apps/mobile/modules/t3-terminal/ios/T3TerminalView.swift‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ private final class TerminalInputField: UITextField {
143143
super.deleteBackward()
144144
}
145145

146-
/// ⌘V, the iPad shortcut bar, and the edit menu land here. Inserting the text instead
147-
/// would send it as typed input, skipping paste encoding and write chunking.
146+
// System pastes must bypass typed input to preserve paste encoding and chunking.
148147
override func paste(_ sender: Any?) {
149148
onPaste?()
150149
}
@@ -778,8 +777,6 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate, UIEditMenuInte
778777
onInput(["data": data])
779778
}
780779

781-
/// Reads the clipboard inside the user's paste action, so iOS does not ask for paste
782-
/// permission. JS sends the text through the same paste path as the toolbar button.
783780
private func emitPaste() {
784781
guard !readOnly, let text = UIPasteboard.general.string, !text.isEmpty else { return }
785782
onPaste(["text": text])

‎apps/mobile/src/features/terminal/NativeTerminalSurface.test.tsx‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@ import type { NativeSyntheticEvent } from "react-native";
99
import { expect, it, vi } from "vite-plus/test";
1010

1111
import type { NativeTerminalSurfaceProps } from "./nativeTerminalModule";
12+
import { TERMINAL_WRITE_MAX_LENGTH } from "./terminalInput";
13+
import { createTerminalPasteSession } from "./terminalPaste";
1214

1315
const 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

Comments
 (0)