The best way to control Claude Code, Codex and more from any computer or phone

main
ts 105 lines 3.94 KB
Raw
1 /// <reference lib="dom" />
2 // The DOM lib types the function this scenario runs in the page.
3 import { expect } from "e2e";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6
7 import { runPromise, test, WEB } from "./t3.ts";
8
9 // A raw-mode program that turns on bracketed paste, logs every byte it receives to `received`,
10 // and exits once the paste's closing marker arrives.
11 const PROBE = String.raw`import { appendFileSync, writeFileSync } from "node:fs";
12
13 const log = new URL("received", import.meta.url);
14 let tail = "";
15 let bytes = 0;
16
17 writeFileSync(log, "");
18 process.stdin.setRawMode(true);
19 process.stdin.on("data", (chunk) => {
20 appendFileSync(log, chunk);
21 bytes += chunk.length;
22 tail = (tail + chunk.toString("latin1")).slice(-6);
23 if (tail === "\x1b[201~") {
24 process.stdin.setRawMode(false);
25 process.stdout.write("\x1b[?2004l\r\nreceived " + bytes + " bytes\r\n");
26 process.exit(0);
27 }
28 });
29 process.stdout.write("\x1b[?2004h");
30 writeFileSync(new URL("ready", import.meta.url), "ready");
31 `;
32
33 // Over the 65,536-unit write limit, multiline, with a surrogate pair straddling the first
34 // 65,536-unit boundary of the bracketed paste (6 units of ESC[200~ come first).
35 const LINE = ' {"file":"src/日本語.ts","message":"café changes"},\n';
36 const EMOJI_LINE = ' {"user":"🧑🏽‍💻","note":"naïve 𝔘𝔫𝔦𝔠𝔬𝔡𝔢"},\n';
37 const HEAD = LINE.repeat(Math.ceil(65_529 / LINE.length)).slice(0, 65_529);
38 const PASTE = HEAD + "🧑🏽‍💻" + EMOJI_LINE.repeat(Math.ceil(34_000 / EMOJI_LINE.length));
39 const EXPECTED = Buffer.from("\x1b[200~" + PASTE + "\x1b[201~");
40 const compare = (received: Uint8Array) => ({
41 bytes: received.length,
42 intact: EXPECTED.equals(received),
43 });
44
45 test(
46 "A web terminal paste larger than one write reaches the program intact",
47 WEB,
48 async ({ t3, app, browser, screen }) => {
49 await browser.emulateMedia({ colorScheme: "dark" });
50 const project = await t3.project("demo", { "paste-probe.mjs": PROBE });
51 const thread = await t3.thread({ project, title: "Paste", messages: ["hello"] });
52 const read = (file: string) =>
53 runPromise(
54 Effect.gen(function* () {
55 const fs = yield* FileSystem.FileSystem;
56 return yield* fs.readFileString(`${project}/${file}`);
57 }).pipe(Effect.orElseSucceed(() => "")),
58 );
59 const received = () =>
60 runPromise(
61 Effect.gen(function* () {
62 const fs = yield* FileSystem.FileSystem;
63 return compare(yield* fs.readFile(`${project}/received`));
64 }).pipe(Effect.orElseSucceed(() => compare(new Uint8Array()))),
65 );
66 await t3.open(thread);
67
68 await screen.getByRole("button", "Toggle terminal drawer").tap();
69 const terminal = screen.getByLabel("Terminal input");
70
71 // Input sent before the shell runs is dropped, so retype until it lands.
72 await expect
73 .poll(
74 async () => {
75 await terminal.focus();
76 await browser.keyboard.type(`echo ready > '${project}/shell-ready'`);
77 await browser.keyboard.press("Enter");
78 return read("shell-ready");
79 },
80 { timeout: 30_000 },
81 )
82 .toBe("ready\n");
83
84 await terminal.focus();
85 await browser.keyboard.type(`'${process.execPath}' '${project}/paste-probe.mjs'`);
86 await browser.keyboard.press("Enter");
87 await expect.poll(() => read("ready"), { timeout: 15_000 }).toBe("ready");
88
89 await browser.evaluate((text) => {
90 const input = document.querySelector('[aria-label="Terminal input"]');
91 if (!input) throw new Error("no terminal input");
92 const clipboardData = new DataTransfer();
93 clipboardData.setData("text/plain", text);
94 input.dispatchEvent(
95 new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }),
96 );
97 return null;
98 }, PASTE);
99
100 await expect
101 .poll(received, { timeout: 20_000 })
102 .toEqual({ bytes: EXPECTED.length, intact: true });
103 await app.screenshot("pasted");
104 },
105 );