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

main
ts 91 lines 3.32 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 process prints rows, then on `b` sets the default background with OSC 11.
10 const PROGRAM = String.raw`import { writeFileSync } from "node:fs";
11
12 const state = new URL("state", import.meta.url);
13 process.stdin.setRawMode(true);
14 process.stdout.write("\x1b[2J\x1b[HROW ONE\r\nROW TWO\r\nROW THREE\r\nROW FOUR\r\n");
15 writeFileSync(state, "ready");
16 process.stdin.on("data", (chunk) => {
17 const key = chunk.toString();
18 if (key === "b") {
19 process.stdout.write("\x1b]11;rgb:44/55/66\x07", () => writeFileSync(state, "changed"));
20 }
21 if (key === "x") {
22 process.stdin.setRawMode(false);
23 process.exit(0);
24 }
25 });
26 `;
27
28 test(
29 "A program setting the terminal's default background repaints the whole screen",
30 WEB,
31 async ({ t3, app, browser, screen }) => {
32 await browser.emulateMedia({ colorScheme: "dark" });
33 const project = await t3.project("colors", { "program.mjs": PROGRAM });
34 const thread = await t3.thread({ project, title: "Colors", messages: ["hello"] });
35 const read = (file: string) =>
36 runPromise(
37 Effect.gen(function* () {
38 const fs = yield* FileSystem.FileSystem;
39 return yield* fs.readFileString(`${project}/${file}`);
40 }).pipe(Effect.orElseSucceed(() => "")),
41 );
42 await t3.open(thread);
43 await screen.getByRole("button", "Toggle terminal drawer").tap();
44 const terminal = screen.getByLabel("Terminal input");
45
46 // Input sent before the shell runs is dropped, so retype until it lands.
47 await expect
48 .poll(
49 async () => {
50 await terminal.focus();
51 await browser.keyboard.type(`echo ready > '${project}/shell-ready'`);
52 await browser.keyboard.press("Enter");
53 return read("shell-ready");
54 },
55 { timeout: 30_000 },
56 )
57 .toBe("ready\n");
58
59 await terminal.focus();
60 await browser.keyboard.type(`'${process.execPath}' '${project}/program.mjs'`);
61 await browser.keyboard.press("Enter");
62 await expect.poll(() => read("state"), { timeout: 15_000 }).toBe("ready");
63 await app.screenshot("before-background");
64
65 await terminal.focus();
66 await browser.keyboard.type("b");
67 await expect.poll(() => read("state"), { timeout: 15_000 }).toBe("changed");
68 await app.screenshot("after-background");
69
70 // Sample the padding above the rows, a cell beside the printed rows, and the canvas bottom.
71 const pixels = await browser.evaluate(() => {
72 const canvas = document
73 .querySelector('[aria-label="Terminal input"]')!
74 .parentElement!.querySelector("canvas")!;
75 const context = canvas.getContext("2d")!;
76 const at = (x: number, y: number) => [...context.getImageData(x, y, 1, 1).data];
77 return {
78 padding: at(canvas.width - 2, 1),
79 row: at(canvas.width - 20, Math.floor(canvas.height / 2)),
80 bottom: at(canvas.width - 2, canvas.height - 1),
81 };
82 });
83 await browser.keyboard.type("x");
84 const programBackground = [68, 85, 102, 255];
85 expect(pixels).toEqual({
86 padding: programBackground,
87 row: programBackground,
88 bottom: programBackground,
89 });
90 },
91 );