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

journey.ts

264 lines9.1 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 = (node: SnapshotNode | undefined, label: string) => {
75 if (!node?.ref) throw new Error(`no ${label} to tap`);
76 return device.agent(["press", `@${node.ref}`]);
77 };
78​
79 /** Screenshot and check the settled screen, and record it as a step. */
80 const capture = async (
81 name: string,
82 snapshot: Snapshot,
83 expectations: ReadonlyArray<Check> = [],
84 extra: Partial<StepResult> = {},
85 ) => {
86 const screenshot = NodePath.join(directory, `${steps.length + 1}-${name}.png`);
87 await device.screenshot(screenshot);
88 const step = {
89 name,
90 checks: [...layoutChecks(snapshot, BUNDLE_ID), ...expectations],
91 screenshot,
92 ...extra,
93 };
94 steps.push(step);
95 return step;
96 };
97​
98 // The app ignores the pairing link until its JS has loaded and shown the workspace. An
99 // error dialog on the way is a finding: record it, dismiss it, and keep going.
100 const loaded = (snapshot: Snapshot) =>
101 findLabel(snapshot, /Add environment|New (chat|thread)/i) !== undefined;
102 const errorDialog = (snapshot: Snapshot) =>
103 findLabel(snapshot, /^(Dismiss|OK|Close)$/) !== undefined
104 ? findLabel(snapshot, /^Could not|failed|went wrong/i)
105 : undefined;
106 for (let dialogs = 0; ; dialogs++) {
107 const launch = await device.waitFor(
108 (snapshot) => loaded(snapshot) || errorDialog(snapshot) !== undefined,
109 "the app to load",
110 180_000,
111 );
112 const dialog = errorDialog(launch);
113 if (!dialog || dialogs >= 3) break;
114 await capture("launch error", launch, [
115 check("no error dialog on launch", false, dialog.label ?? "error dialog"),
116 ]);
117 await device.agent(["press", `label="${findLabel(launch, /^(Dismiss|OK|Close)$/)!.label}"`]);
118 await delay(1_000);
119 }
120 let threads: Snapshot | null = null;
121 for (let attempt = 1; attempt <= 2 && !threads; attempt++) {
122 const pairing = new URLSearchParams({ pairingUrl: await pair(), autoConnect: "1" });
123 await device.openUrl(`t3code-dev://connections/new?${pairing}`);
124 threads = await device
125 .waitFor((snapshot) => threadRow(snapshot) !== undefined, "the paired thread list", 60_000)
126 .catch(() => null);
127 }
128 if (!threads) throw new Error("the app did not show the fixture threads after pairing");
129 await delay(1_000);
130 threads = await device.snapshot();
131 const tabs = tabBarRect(threads);
132 await capture("threads", threads, [
133 check("thread list visible", threadRow(threads) !== undefined, "fixture threads"),
134 ...(phone ? [check("tab bar visible", inside(tabs, threads), describeRect(tabs))] : []),
135 ]);
136​
137 const opening = await device.recorded(directory, "open-thread", () =>
138 tap(threadRow(threads!), "thread row"),
139 );
140 const thread = await device.waitFor(
141 (snapshot) => composer(snapshot) !== undefined && threadRow(snapshot) !== undefined,
142 "the thread screen",
143 );
144 const threadShot = NodePath.join(directory, `${steps.length + 1}-thread.png`);
145 await device.screenshot(threadShot);
146 const openLag =
147 phone && tabs
148 ? await lagCheck(
149 "no tabs over the entering thread (video)",
150 {
151 video: opening,
152 nav: topBand(threads),
153 chrome: tabs,
154 viewportWidth: threads.viewport.width,
155 },
156 NodePath.join(directory, "open-thread-lag.png"),
157 )
158 : null;
159 steps.push({
160 name: "thread",
161 screenshot: threadShot,
162 video: opening,
163 ...(openLag?.frame ? { lagFrame: openLag.frame } : {}),
164 checks: [
165 ...layoutChecks(thread, BUNDLE_ID),
166 check(
167 "composer inside the screen",
168 inside(composer(thread)?.rect, thread),
169 describeRect(composer(thread)?.rect),
170 ),
171 ...(phone
172 ? [
173 check("back button visible", backButton(thread) !== undefined, "back"),
174 check(
175 "tab bar hidden",
176 tabBarRect(thread) === undefined,
177 describeRect(tabBarRect(thread)),
178 ),
179 ]
180 : []),
181 ...(openLag ? [openLag.check] : []),
182 ],
183 });
184​
185 await device.agent(["scroll", "up"]).catch(() => undefined);
186 await delay(800);
187 await capture("scrolled up", await device.snapshot());
188 await device.agent(["scroll", "down"]).catch(() => undefined);
189 await delay(800);
190​
191 await tap(composer(await device.snapshot()), "composer");
192 await device.agent(["type", "Checking the composer above the keyboard"]);
193 await delay(1_200);
194 const typing = await device.snapshot();
195 await capture("typing", typing, [
196 check(
197 "composer visible while typing",
198 inside(composer(typing)?.rect, typing),
199 describeRect(composer(typing)?.rect),
200 ),
201 check(
202 "composer above the keyboard",
203 keyboardRect(typing) === undefined ||
204 (composer(typing)?.rect?.y ?? Infinity) < keyboardRect(typing)!.y,
205 `composer ${describeRect(composer(typing)?.rect)}, keyboard ${describeRect(keyboardRect(typing))}`,
206 ),
207 ]);
208 await device.agent(["keyboard", "dismiss"]).catch(() => undefined);
209 await delay(800);
210​
211 if (phone) {
212 const back = await device.recorded(directory, "back", async () => {
213 const current = await device.snapshot();
214 const button = backButton(current);
215 if (button) await tap(button, "back button");
216 else await device.agent(["back"]);
217 });
218 const list = await device.waitFor(
219 (snapshot) => backButton(snapshot) === undefined && threadRow(snapshot) !== undefined,
220 "the thread list",
221 );
222 const listShot = NodePath.join(directory, `${steps.length + 1}-back.png`);
223 await device.screenshot(listShot);
224 const backLag = tabs
225 ? await lagCheck(
226 "tabs return with the list (video)",
227 {
228 video: back,
229 nav: topBand(list),
230 chrome: tabs,
231 viewportWidth: list.viewport.width,
232 },
233 NodePath.join(directory, "back-lag.png"),
234 )
235 : null;
236 steps.push({
237 name: "back",
238 screenshot: listShot,
239 video: back,
240 ...(backLag?.frame ? { lagFrame: backLag.frame } : {}),
241 checks: [
242 ...layoutChecks(list, BUNDLE_ID),
243 check("tab bar back", inside(tabBarRect(list), list), describeRect(tabBarRect(list))),
244 ...(backLag ? [backLag.check] : []),
245 ],
246 });
247 }
248​
249 // The labelled tab is not always the hittable node; agent-device resolves the press.
250 await device.agent(["press", 'label="Settings"']);
251 await delay(1_500);
252 await capture("settings", await device.snapshot());
253​
254 if (device.pose.form === "tablet" && !device.pose.fold) {
255 await device.agent(["orientation", device.pose.landscape ? "portrait" : "landscape-left"]);
256 await delay(2_000);
257 await capture("rotated", await device.snapshot());
258 }
259 if (device.pose.fold) {
260 await device.agent(["fold", device.pose.fold === "open" ? "closed" : "open"]);
261 await delay(2_500);
262 await capture(device.pose.fold === "open" ? "folded" : "unfolded", await device.snapshot());
263 }