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 | // One raw-mode process logs every byte the terminal sends to `received` and steps on exact |
| 10 | // input: DECRQM replies mark when 1004 is on or off, and `x` ends it. |
| 11 | const PROBE = String.raw`import { appendFileSync, writeFileSync } from "node:fs"; |
| 12 | |
| 13 | const log = new URL("received", import.meta.url); |
| 14 | const ENABLED = "\x1b[?1004;1$y"; |
| 15 | const REPORTS = "\x1b[O\x1b[I"; |
| 16 | const DISABLED = "\x1b[?1004;2$y"; |
| 17 | let received = ""; |
| 18 | |
| 19 | writeFileSync(log, ""); |
| 20 | process.stdin.setRawMode(true); |
| 21 | process.stdin.on("data", (chunk) => { |
| 22 | appendFileSync(log, chunk); |
| 23 | received += chunk.toString("latin1"); |
| 24 | if (received === ENABLED + REPORTS) process.stdout.write("\x1b[?1004l\x1b[?1004$p"); |
| 25 | if (received === ENABLED + REPORTS + DISABLED + "x") { |
| 26 | process.stdin.setRawMode(false); |
| 27 | process.exit(0); |
| 28 | } |
| 29 | }); |
| 30 | process.stdout.write("\x1b[?1004h\x1b[?1004$p"); |
| 31 | `; |
| 32 | |
| 33 | const ENABLED = "\x1b[?1004;1$y"; |
| 34 | const REPORTS = "\x1b[O\x1b[I"; |
| 35 | const DISABLED = "\x1b[?1004;2$y"; |
| 36 | |
| 37 | test( |
| 38 | "The web terminal reports focus changes only while a program enables DEC 1004", |
| 39 | WEB, |
| 40 | async ({ t3, app, browser, screen }) => { |
| 41 | const project = await t3.project("demo", { "focus-probe.mjs": PROBE }); |
| 42 | const thread = await t3.thread({ project, title: "Focus", messages: ["hello"] }); |
| 43 | const read = (file: string) => |
| 44 | runPromise( |
| 45 | Effect.gen(function* () { |
| 46 | const fs = yield* FileSystem.FileSystem; |
| 47 | return yield* fs.readFileString(`${project}/${file}`); |
| 48 | }).pipe(Effect.orElseSucceed(() => "")), |
| 49 | ); |
| 50 | await t3.open(thread); |
| 51 | await browser.evaluate(() => { |
| 52 | localStorage.setItem("t3code:theme", "dark"); |
| 53 | return null; |
| 54 | }); |
| 55 | await t3.open(thread); |
| 56 | |
| 57 | await screen.getByRole("button", "Toggle terminal drawer").tap(); |
| 58 | const terminal = screen.getByLabel("Terminal input"); |
| 59 | const composer = screen.getByRole("textbox", "Message Focus"); |
| 60 | |
| 61 | // Input sent before the shell runs is dropped, so retype until it lands. |
| 62 | await expect |
| 63 | .poll( |
| 64 | async () => { |
| 65 | await terminal.focus(); |
| 66 | await browser.keyboard.type(`echo ready > '${project}/shell-ready'`); |
| 67 | await browser.keyboard.press("Enter"); |
| 68 | return read("shell-ready"); |
| 69 | }, |
| 70 | { timeout: 30_000 }, |
| 71 | ) |
| 72 | .toBe("ready\n"); |
| 73 | |
| 74 | await terminal.focus(); |
| 75 | await browser.keyboard.type(`'${process.execPath}' '${project}/focus-probe.mjs'`); |
| 76 | await browser.keyboard.press("Enter"); |
| 77 | await expect.poll(() => read("received"), { timeout: 15_000 }).toBe(ENABLED); |
| 78 | |
| 79 | await composer.tap(); |
| 80 | await terminal.focus(); |
| 81 | await app.screenshot("enabled"); |
| 82 | await expect |
| 83 | .poll(() => read("received"), { timeout: 10_000 }) |
| 84 | .toBe(ENABLED + REPORTS + DISABLED); |
| 85 | |
| 86 | await composer.tap(); |
| 87 | await terminal.focus(); |
| 88 | await browser.keyboard.type("x"); |
| 89 | await app.screenshot("disabled"); |
| 90 | await expect |
| 91 | .poll(() => read("received"), { timeout: 10_000 }) |
| 92 | .toBe(ENABLED + REPORTS + DISABLED + "x"); |
| 93 | }, |
| 94 | ); |