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

main
ts 105 lines 4.43 KB
Raw
1 /// <reference lib="dom" />
2 // Helpers for composer suggestion scenarios in short windows; the DOM lib types their page code.
3 import type { Browser } from "@e2e-dev/web";
4 import { expect, type Screen } from "e2e";
5
6 import type { T3 } from "./t3.ts";
7
8 const FILE_NAMES = Array.from(
9 { length: 40 },
10 (_, index) => `entry-${String(index + 1).padStart(2, "0")}.ts`,
11 );
12
13 /** Seeds a project with enough files to overflow the menu and opens a thread in it. */
14 export async function openThreadWithManyFiles(t3: T3) {
15 const files = Object.fromEntries(FILE_NAMES.map((name) => [`src/${name}`, `export {};\n`]));
16 const project = await t3.project("demo", files);
17 const thread = await t3.thread({ project, title: "Short window", messages: ["hello"] });
18 await t3.open(thread);
19 }
20
21 /** Types a fourteen-line draft, then opens the `@` file suggestions at its end. */
22 export async function openFileSuggestionsUnderTallDraft(browser: Browser, screen: Screen) {
23 // A phone-width window collapses the composer until it is tapped.
24 await screen.getByRole("button", "Expand composer").tap();
25 await screen.getByRole("textbox").tap();
26 for (let line = 1; line <= 14; line += 1) {
27 await browser.keyboard.type(`line ${line}`);
28 if (line < 14) await browser.keyboard.press("Shift+Enter");
29 }
30 await browser.keyboard.press("Control+End");
31 await browser.keyboard.type(" @");
32 await expect
33 .poll(() =>
34 browser.evaluate(
35 () => document.querySelectorAll('[data-composer-command-drawer] [role="option"]').length,
36 ),
37 )
38 .toBeGreaterThan(FILE_NAMES.length);
39 }
40
41 /** Wheels the suggestions from a point inside the visible drawer until they reach that end. */
42 export async function wheelSuggestions(browser: Browser, direction: "up" | "down") {
43 const point = await browser.evaluate(() => {
44 const layer = document.querySelector('[data-composer-drawer-layer="true"]');
45 if (!layer) return null;
46 const rect = layer.getBoundingClientRect();
47 const top = Math.max(rect.top, 0);
48 const bottom = Math.min(rect.bottom, window.innerHeight);
49 return { x: rect.left + rect.width / 2, y: top + (bottom - top) / 3 };
50 });
51 expect(point).not.toBeNull();
52 await browser.mouse.move(point!.x, point!.y);
53 // Wheel scrolling lands a frame or more later, so keep wheeling until the list is at its end.
54 await expect
55 .poll(async () => {
56 await browser.mouse.wheel(0, direction === "down" ? 4000 : -4000);
57 return browser.evaluate((direction) => {
58 const viewport = document.querySelector(
59 '[data-composer-command-drawer] [data-slot="scroll-area-viewport"]',
60 );
61 if (!viewport) return false;
62 return direction === "down"
63 ? viewport.scrollTop >= viewport.scrollHeight - viewport.clientHeight - 1
64 : viewport.scrollTop <= 0;
65 }, direction);
66 })
67 .toBe(true);
68 }
69
70 /**
71 * The first suggestion or last file row, and its center when the whole row is visible
72 * and a click there lands on it, otherwise `null`.
73 */
74 export const findHittableRow = (browser: Browser, which: "first" | "last-file") =>
75 browser.evaluate(
76 ({ which, names }) => {
77 const options = [
78 ...document.querySelectorAll<HTMLElement>('[data-composer-command-drawer] [role="option"]'),
79 ];
80 const row =
81 which === "first"
82 ? options[0]
83 : options.findLast((option) =>
84 names.some((name) => option.textContent?.startsWith(name)),
85 );
86 if (!row) return { label: null, point: null };
87 const text = row.textContent ?? "";
88 const label = names.find((name) => text.startsWith(name)) ?? text;
89 const rect = row.getBoundingClientRect();
90 const viewport = row.closest('[data-slot="scroll-area-viewport"]')?.getBoundingClientRect();
91 const top = Math.max(0, viewport?.top ?? 0);
92 const bottom = Math.min(window.innerHeight, viewport?.bottom ?? window.innerHeight);
93 if (rect.top < top - 1 || rect.bottom > bottom + 1) return { label, point: null };
94 const point = { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
95 const hit = document.elementFromPoint(point.x, point.y)?.closest('[role="option"]') === row;
96 return { label, point: hit ? point : null };
97 },
98 { which, names: [...FILE_NAMES, "README.md"] },
99 );
100
101 export async function clickAt(browser: Browser, point: { readonly x: number; readonly y: number }) {
102 await browser.mouse.move(point.x, point.y);
103 await browser.mouse.down();
104 await browser.mouse.up();
105 }