The best way to control Claude Code, Codex and more from any computer or phone
| 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 turns on application cursor and keypad modes as ncurses keypad(TRUE) does, |
| 10 | // asks for DECCKM so its reply marks when the terminal has applied them, and logs what it reads. |
| 11 | const PROBE = String.raw`import { appendFileSync, writeFileSync } from "node:fs"; |
| 12 | |
| 13 | const log = new URL("received", import.meta.url); |
| 14 | writeFileSync(log, ""); |
| 15 | process.stdin.setRawMode(true); |
| 16 | process.stdin.on("data", (chunk) => { |
| 17 | appendFileSync(log, chunk); |
| 18 | if (chunk.includes("x")) { |
| 19 | process.stdout.write("\x1b[?1l\x1b>"); |
| 20 | process.stdin.setRawMode(false); |
| 21 | process.exit(0); |
| 22 | } |
| 23 | }); |
| 24 | process.stdout.write("\x1b[?1h\x1b=\x1b[?1$p"); |
| 25 | `; |
| 26 | |
| 27 | const READY = "\x1b[?1;1$y"; |
| 28 | // Keypad keys as a browser reports them with NumLock off: the physical digit, the navigation key. |
| 29 | const KEYPAD = [ |
| 30 | { code: "Numpad8", key: "ArrowUp" }, |
| 31 | { code: "Numpad1", key: "End" }, |
| 32 | { code: "NumpadDecimal", key: "Delete" }, |
| 33 | ]; |
| 34 | |
| 35 | test( |
| 36 | "NumLock-off keypad navigation keys reach a terminal program", |
| 37 | WEB, |
| 38 | async ({ t3, app, browser, screen }) => { |
| 39 | const project = await t3.project("demo", { "keypad-probe.mjs": PROBE }); |
| 40 | const thread = await t3.thread({ project, title: "Keypad", messages: ["hello"] }); |
| 41 | const read = (file: string) => |
| 42 | runPromise( |
| 43 | Effect.gen(function* () { |
| 44 | const fs = yield* FileSystem.FileSystem; |
| 45 | return yield* fs.readFileString(`${project}/${file}`); |
| 46 | }).pipe(Effect.orElseSucceed(() => "")), |
| 47 | ); |
| 48 | await t3.open(thread); |
| 49 | |
| 50 | await screen.getByRole("button", "Toggle terminal drawer").tap(); |
| 51 | const terminal = screen.getByLabel("Terminal input"); |
| 52 | |
| 53 | // Input sent before the shell runs is dropped, so retype until it lands. |
| 54 | await expect |
| 55 | .poll( |
| 56 | async () => { |
| 57 | await terminal.focus(); |
| 58 | await browser.keyboard.type(`echo ready > '${project}/shell-ready'`); |
| 59 | await browser.keyboard.press("Enter"); |
| 60 | return read("shell-ready"); |
| 61 | }, |
| 62 | { timeout: 30_000 }, |
| 63 | ) |
| 64 | .toBe("ready\n"); |
| 65 | |
| 66 | await terminal.focus(); |
| 67 | await browser.keyboard.type(`'${process.execPath}' '${project}/keypad-probe.mjs'`); |
| 68 | await browser.keyboard.press("Enter"); |
| 69 | await expect.poll(() => read("received"), { timeout: 15_000 }).toBe(READY); |
| 70 | |
| 71 | // The e2e keyboard cannot press a keypad key with NumLock off, so send the events Chromium reports. |
| 72 | await browser.evaluate((keys) => { |
| 73 | const input = document.activeElement; |
| 74 | if (!input) throw new Error("Terminal input lost focus"); |
| 75 | for (const init of keys) { |
| 76 | for (const type of ["keydown", "keyup"]) { |
| 77 | const options = { ...init, location: 3, bubbles: true, cancelable: true }; |
| 78 | input.dispatchEvent(new KeyboardEvent(type, options)); |
| 79 | } |
| 80 | } |
| 81 | return null; |
| 82 | }, KEYPAD); |
| 83 | await browser.keyboard.type("x"); |
| 84 | await app.screenshot("after-keypad"); |
| 85 | |
| 86 | await expect |
| 87 | .poll(() => read("received"), { timeout: 10_000 }) |
| 88 | .toBe(`${READY}\x1bOA\x1bOF\x1b[3~x`); |
| 89 | }, |
| 90 | ); |