Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

journey.ts

280 lines10.0 KB

1// @effect-diagnostics nodeBuiltinImport:off globalTimers:off - Host-side device automation.
2​
3import * as NodePath from "node:path";
4​
5import {
6 check,
7 type Check,
8 describeRect,
9 findLabel,
10 keyboardRect,
11 layoutChecks,
12 type Rect,
13 type Snapshot,
14 type SnapshotNode,
15 tabBarRect,
16} from "./checks.ts";
17import { BUNDLE_ID, delay, type Device } from "./device.ts";
18import { lagCheck } from "./video.ts";
19​
20export interface StepResult {
21 readonly name: string;
22 readonly checks: ReadonlyArray<Check>;
23 readonly screenshot?: string;
24 readonly video?: string;
25 readonly lagFrame?: string;
27​
28// Threads of the fixture's first environment (scripts/mobile-showcase-environment.ts).
29const THREAD_TITLE =
30 /Make remote coding feel local|Put the command center in your pocket|Tune the handoff haptics/;
31const BACK = /^(Back|Navigate up|Go back)$/i;
32​
33const backButton = (snapshot: Snapshot) =>
34 snapshot.nodes.find((node) => node.identifier === "BackButton") ?? findLabel(snapshot, BACK);
35const composer = (snapshot: Snapshot) =>
36 snapshot.nodes
37 .filter(
38 (node) =>
39 (node.kind === "text-field" || node.kind === "text-view" || node.editable === true) &&
40 node.rect &&
41 node.rect.y < snapshot.viewport.height &&
42 node.bundleId !== "com.android.systemui",
43 )
44 .toSorted((a, b) => b.rect!.y - a.rect!.y)[0];
45const threadRow = (snapshot: Snapshot) => findLabel(snapshot, THREAD_TITLE);
46​
47const inside = (rect: Rect | undefined, snapshot: Snapshot) =>
48 rect !== undefined &&
49 rect.x >= 0 &&
50 rect.y >= 0 &&
51 rect.x + rect.width <= snapshot.viewport.width + 1 &&
52 rect.y + rect.height <= snapshot.viewport.height + 1;
53​
54const topBand = (snapshot: Snapshot): Rect => ({
55 x: 0,
56 y: 0,
57 width: snapshot.viewport.width,
58 height: snapshot.viewport.height * 0.12,
59});
60​
61/**
62 * Walks one device through the app like a person would: thread list, a thread, scrolling,
63 * typing, back, settings, then the pose's own change (rotation or fold). Every screen gets
64 * the generic layout checks plus what that screen must show. Steps land in `steps` as they
65 * finish, so a journey that stops early still reports what it saw.
66 */
67export async function runJourney(
68 device: Device,
69 directory: string,
70 pair: () => Promise<string>,
71 steps: StepResult[],
72): Promise<void> {
73 const phone = device.pose.form === "phone";
74 const tap = async (node: SnapshotNode | undefined, label: string) => {
75 if (!node?.ref || !node.rect) throw new Error(`no ${label} to tap`);
76 // agent-device refuses refs it considers covered, such as on the unfolded Duo's inner
77 // display; the element's centre still takes the tap.
78 await device
79 .agent(["press", `@${node.ref}`])
80 .catch(() =>
81 device.agent([
82 "press",
83 String(Math.round(node.rect!.x + node.rect!.width / 2)),
84 String(Math.round(node.rect!.y + node.rect!.height / 2)),
85 ]),
86 );
87 };
88​
89 /** Screenshot and check the settled screen, and record it as a step. */
90 const capture = async (
91 name: string,
92 snapshot: Snapshot,
93 expectations: ReadonlyArray<Check> = [],
94 extra: Partial<StepResult> = {},
95 ) => {
96 const screenshot = NodePath.join(directory, `${steps.length + 1}-${name}.png`);
97 await device.screenshot(screenshot);
98 const step = {
99 name,
100 checks: [...layoutChecks(snapshot, BUNDLE_ID), ...expectations],
101 screenshot,
102 ...extra,
103 };
104 steps.push(step);
105 return step;
106 };
107​
108 // The app ignores the pairing link until its JS has loaded and shown the workspace. An
109 // error dialog on the way is a finding: record it, dismiss it, and keep going.
110 const loaded = (snapshot: Snapshot) =>
111 findLabel(snapshot, /Add environment|New (chat|thread)/i) !== undefined;
112 const errorDialog = (snapshot: Snapshot) =>
113 findLabel(snapshot, /^(Dismiss|OK|Close)$/) !== undefined
114 ? findLabel(snapshot, /^Could not|failed|went wrong/i)
115 : undefined;
116 for (let dialogs = 0; ; dialogs++) {
117 const launch = await device.waitFor(
118 (snapshot) => loaded(snapshot) || errorDialog(snapshot) !== undefined,
119 "the app to load",
120 180_000,
121 );
122 const dialog = errorDialog(launch);
123 if (!dialog || dialogs >= 3) break;
124 await capture("launch error", launch, [
125 check("no error dialog on launch", false, dialog.label ?? "error dialog"),
126 ]);
127 await device.agent(["press", `label="${findLabel(launch, /^(Dismiss|OK|Close)$/)!.label}"`]);
128 await delay(1_000);
129 }
130 let threads: Snapshot | null = null;
131 for (let attempt = 1; attempt <= 2 && !threads; attempt++) {
132 const pairing = new URLSearchParams({ pairingUrl: await pair(), autoConnect: "1" });
133 await device.openUrl(`t3code-dev://connections/new?${pairing}`);
134 threads = await device
135 .waitFor((snapshot) => threadRow(snapshot) !== undefined, "the paired thread list", 60_000)
136 .catch(() => null);
137 }
138 if (!threads) throw new Error("the app did not show the fixture threads after pairing");
139 // Rotated only once the app is up: a rotation during launch is lost.
140 await delay(1_000);
141 threads = await device.orient(device.pose.landscape ? "landscape" : "portrait");
142 const tabs = tabBarRect(threads);
143 // A bottom tab bar gives way to a pushed thread; the folded Duo's side rail stays put.
144 const bottomTabs = phone && tabs && tabs.width > tabs.height ? tabs : undefined;
145 const wide = threads.viewport.width > threads.viewport.height;
146 await capture("threads", threads, [
147 check(
148 device.pose.landscape ? "in landscape" : "in portrait",
149 wide === Boolean(device.pose.landscape),
150 `${Math.round(threads.viewport.width)}×${Math.round(threads.viewport.height)}`,
151 ),
152 check("thread list visible", threadRow(threads) !== undefined, "fixture threads"),
153 ...(phone ? [check("tab bar visible", inside(tabs, threads), describeRect(tabs))] : []),
154 ]);
155​
156 const opening = await device.recorded(directory, "open-thread", () =>
157 tap(threadRow(threads!), "thread row"),
158 );
159 const thread = await device.waitFor(
160 (snapshot) => composer(snapshot) !== undefined && threadRow(snapshot) !== undefined,
161 "the thread screen",
162 );
163 const threadShot = NodePath.join(directory, `${steps.length + 1}-thread.png`);
164 await device.screenshot(threadShot);
165 const openLag = bottomTabs
166 ? await lagCheck(
167 "no tabs over the entering thread (video)",
168 {
169 video: opening,
170 nav: topBand(threads),
171 chrome: bottomTabs,
172 viewportWidth: threads.viewport.width,
173 },
174 NodePath.join(directory, "open-thread-lag.png"),
175 )
176 : null;
177 steps.push({
178 name: "thread",
179 screenshot: threadShot,
180 video: opening,
181 ...(openLag?.frame ? { lagFrame: openLag.frame } : {}),
182 checks: [
183 ...layoutChecks(thread, BUNDLE_ID),
184 check(
185 "composer inside the screen",
186 inside(composer(thread)?.rect, thread),
187 describeRect(composer(thread)?.rect),
188 ),
189 ...(phone ? [check("back button visible", backButton(thread) !== undefined, "back")] : []),
190 ...(bottomTabs
191 ? [
192 check(
193 "tab bar hidden",
194 tabBarRect(thread) === undefined,
195 describeRect(tabBarRect(thread)),
196 ),
197 ]
198 : []),
199 ...(openLag ? [openLag.check] : []),
200 ],
201 });
202​
203 await device.agent(["scroll", "up"]).catch(() => undefined);
204 await delay(800);
205 await capture("scrolled up", await device.snapshot());
206 await device.agent(["scroll", "down"]).catch(() => undefined);
207 await delay(800);
208​
209 await tap(composer(await device.snapshot()), "composer");
210 await device.agent(["type", "Checking the composer above the keyboard"]);
211 await delay(1_200);
212 const typing = await device.snapshot();
213 await capture("typing", typing, [
214 check(
215 "composer visible while typing",
216 inside(composer(typing)?.rect, typing),
217 describeRect(composer(typing)?.rect),
218 ),
219 check(
220 "composer above the keyboard",
221 keyboardRect(typing) === undefined ||
222 (composer(typing)?.rect?.y ?? Infinity) < keyboardRect(typing)!.y,
223 `composer ${describeRect(composer(typing)?.rect)}, keyboard ${describeRect(keyboardRect(typing))}`,
224 ),
225 ]);
226 await device.agent(["keyboard", "dismiss"]).catch(() => undefined);
227 await delay(800);
228​
229 if (phone) {
230 const back = await device.recorded(directory, "back", async () => {
231 const current = await device.snapshot();
232 const button = backButton(current);
233 if (button) await tap(button, "back button");
234 else await device.agent(["back"]);
235 });
236 const list = await device.waitFor(
237 (snapshot) => backButton(snapshot) === undefined && threadRow(snapshot) !== undefined,
238 "the thread list",
239 );
240 const listShot = NodePath.join(directory, `${steps.length + 1}-back.png`);
241 await device.screenshot(listShot);
242 const backLag = bottomTabs
243 ? await lagCheck(
244 "tabs return with the list (video)",
245 {
246 video: back,
247 nav: topBand(list),
248 chrome: bottomTabs,
249 viewportWidth: list.viewport.width,
250 },
251 NodePath.join(directory, "back-lag.png"),
252 )
253 : null;
254 steps.push({
255 name: "back",
256 screenshot: listShot,
257 video: back,
258 ...(backLag?.frame ? { lagFrame: backLag.frame } : {}),
259 checks: [
260 ...layoutChecks(list, BUNDLE_ID),
261 check("tab bar back", inside(tabBarRect(list), list), describeRect(tabBarRect(list))),
262 ...(backLag ? [backLag.check] : []),
263 ],
264 });
265 }
266​
267 // The labelled tab is not always the hittable node; agent-device resolves the press.
268 await device.agent(["press", 'label="Settings"']);
269 await delay(1_500);
270 await capture("settings", await device.snapshot());
271​
272 if (device.pose.form === "tablet" && !device.pose.fold) {
273 await capture("rotated", await device.orient(device.pose.landscape ? "portrait" : "landscape"));
274 }
275 if (device.pose.fold) {
276 await device.agent(["fold", device.pose.fold === "open" ? "closed" : "open"]);
277 await delay(2_500);
278 await capture(device.pose.fold === "open" ? "folded" : "unfolded", await device.snapshot());
279 }