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 | import * as Path from "effect/Path"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | |
| 9 | import { APP_DIR, T3_HOME } from "../e2e.config.ts"; |
| 10 | import { run } from "./shell.ts"; |
| 11 | import { runPromise, test, WEB } from "./t3.ts"; |
| 12 | |
| 13 | const decodeRuntime = Schema.decodeUnknownEffect( |
| 14 | Schema.fromJsonString(Schema.Struct({ pid: Schema.Int })), |
| 15 | ); |
| 16 | |
| 17 | /** The scenario's own server: the pid its T3 home records, running from the app checkout. */ |
| 18 | const scenarioServerPid = Effect.gen(function* () { |
| 19 | const fs = yield* FileSystem.FileSystem; |
| 20 | const path = yield* Path.Path; |
| 21 | const runtime = yield* Effect.firstSuccessOf( |
| 22 | ["userdata", "dev"].map((dir) => |
| 23 | fs.readFileString(path.join(T3_HOME, dir, "server-runtime.json")), |
| 24 | ), |
| 25 | ); |
| 26 | const { pid } = yield* decodeRuntime(runtime); |
| 27 | const cwd = yield* fs.readLink(`/proc/${pid}/cwd`); |
| 28 | if (!cwd.startsWith(path.resolve(APP_DIR))) { |
| 29 | return yield* Effect.die(new Error(`pid ${pid} runs in ${cwd}, not ${APP_DIR}`)); |
| 30 | } |
| 31 | return pid; |
| 32 | }); |
| 33 | |
| 34 | /** |
| 35 | * Linux only: leaves the server no free file descriptor, so node-pty's forkpty(3) fails as it does when a |
| 36 | * host runs out. Returns the restore; only this process's soft limit changes. |
| 37 | */ |
| 38 | const exhaustFileDescriptors = Effect.fn("scenario.exhaustFileDescriptors")(function* ( |
| 39 | pid: number, |
| 40 | ) { |
| 41 | const limits = yield* run("prlimit", [ |
| 42 | `--pid=${pid}`, |
| 43 | "--nofile", |
| 44 | "--raw", |
| 45 | "--noheadings", |
| 46 | "--output=SOFT,HARD", |
| 47 | ]); |
| 48 | const [soft = "", hard = ""] = limits.stdout.trim().split(/\s+/); |
| 49 | yield* run("prlimit", [`--pid=${pid}`, `--nofile=0:${hard}`]); |
| 50 | return run("prlimit", [`--pid=${pid}`, `--nofile=${soft}:${hard}`]); |
| 51 | }); |
| 52 | |
| 53 | test( |
| 54 | "A terminal whose shell cannot start says why, also after its drawer reopens", |
| 55 | WEB, |
| 56 | async ({ t3, app, browser, screen }) => { |
| 57 | const project = await t3.project("demo"); |
| 58 | const thread = await t3.thread({ project, title: "Shell", messages: ["hello"] }); |
| 59 | await t3.open(thread); |
| 60 | const pid = await runPromise(scenarioServerPid); |
| 61 | |
| 62 | const toggle = screen.getByRole("button", "Toggle terminal drawer"); |
| 63 | const terminal = screen.getByLabel("Terminal input"); |
| 64 | // The canvas has no DOM text: select all and read what the terminal's copy hands over. |
| 65 | const terminalText = async () => { |
| 66 | await browser.evaluate(() => { |
| 67 | delete document.body.dataset.terminalCopy; |
| 68 | window.addEventListener( |
| 69 | "copy", |
| 70 | (event) => { |
| 71 | document.body.dataset.terminalCopy = event.clipboardData?.getData("text/plain") ?? ""; |
| 72 | }, |
| 73 | { once: true }, |
| 74 | ); |
| 75 | return null; |
| 76 | }); |
| 77 | await terminal.focus(); |
| 78 | await browser.keyboard.press("Control+Shift+A"); |
| 79 | await browser.keyboard.press("Control+Shift+C"); |
| 80 | return browser.evaluate(() => document.body.dataset.terminalCopy ?? ""); |
| 81 | }; |
| 82 | |
| 83 | await expect(toggle).toBeVisible(); |
| 84 | const restore = await runPromise(exhaustFileDescriptors(pid)); |
| 85 | try { |
| 86 | await toggle.tap(); |
| 87 | await expect.poll(terminalText, { timeout: 15_000 }).toContain("Failed to spawn PTY process"); |
| 88 | } finally { |
| 89 | await runPromise(restore); |
| 90 | } |
| 91 | await app.screenshot("startup-failure"); |
| 92 | |
| 93 | await toggle.tap(); |
| 94 | await expect(terminal).toBeHidden(); |
| 95 | await toggle.tap(); |
| 96 | await expect.poll(terminalText, { timeout: 15_000 }).toContain("Failed to spawn PTY process"); |
| 97 | await app.screenshot("after-reopen"); |
| 98 | }, |
| 99 | ); |