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

scenarios.ts

597 lines24.7 KB

1// @effect-diagnostics globalDate:off - scenario windows are wall-clock by nature.
2/**
3 * Scenarios drive one flow on the device and return metrics. Cases in `cases.ts` name a
4 * scenario and check its metrics, so one flow can serve several issues.
5 */
6import type { ThreadId } from "@t3tools/contracts";
7import type * as Crypto from "effect/Crypto";
8import * as Effect from "effect/Effect";
9import * as Schema from "effect/Schema";
10​
11import {
12 APP_ID,
13 SCHEME,
14 type AxNode,
15 type Device,
16 type FeedSample,
17 type ProbeState,
18} from "./device.ts";
19import { uuid, type EvalEnvironment } from "./environment.ts";
20import { feedMotion, frameStats, type Metrics } from "./grade.ts";
21​
22export type Pose = "phone" | "duo-closed" | "duo-open" | "ipad";
23​
24export interface ScenarioContext {
25 readonly env: EvalEnvironment;
26 readonly device: Device;
27 readonly pose: Pose;
28 readonly fixtures: { readonly longThread: ThreadId };
30​
31// Child thread ids carry `:`, so each segment is encoded.
32const threadPath = (ctx: ScenarioContext, threadId: string) =>
33 `threads/${encodeURIComponent(ctx.env.environmentId)}/${encodeURIComponent(threadId)}`;
34​
35const isThread = (state: ProbeState, threadId: string) =>
36 state.route?.name === "Thread" && state.route.params.threadId === threadId;
37​
38const openThread = Effect.fn("scenario.openThread")(function* (
39 ctx: ScenarioContext,
40 threadId: string,
41) {
42 yield* ctx.device.open(threadPath(ctx, threadId));
43 return yield* ctx.device.waitFor(
44 `thread ${threadId} with a ready feed`,
45 (state) => isThread(state, threadId) && state.feed?.threadId === threadId && state.feed.ready,
46 20_000,
47 );
48});
49​
50/** Screen size in points, and the x of the thread column (the right column in split view). */
51const geometry = Effect.fn("scenario.geometry")(function* (ctx: ScenarioContext) {
52 const nodes = yield* ctx.device.nodes;
53 const rect = nodes[0]?.rect ?? { x: 0, y: 0, width: 400, height: 870 };
54 const state = yield* ctx.device.state;
55 return {
56 width: rect.width,
57 height: rect.height,
58 threadX: Math.round(rect.width * (state.route?.columns ? 0.72 : 0.5)),
59 };
60});
61​
62const findNode = Effect.fn("scenario.findNode")(function* (
63 ctx: ScenarioContext,
64 description: string,
65 match: (node: { label?: string; value?: string; kind?: string; editable?: boolean }) => boolean,
66) {
67 const node = (yield* ctx.device.nodes).find(match);
68 if (!node?.rect) return yield* new ScenarioFailure({ message: `No ${description} on screen.` });
69 return node;
70});
71​
72const composerNode = (ctx: ScenarioContext) =>
73 findNode(
74 ctx,
75 "composer text view",
76 (node) => node.kind === "text-view" && node.editable === true,
77 );
78​
79/** The text the native editor shows, read from the accessibility tree rather than React props. */
80const composerText = Effect.fn("scenario.composerText")(function* (ctx: ScenarioContext) {
81 const node = yield* composerNode(ctx);
82 return node.value ?? node.label ?? "";
83});
84​
85/** A fresh settled thread, so drafts left by earlier samples never mix in. */
86const freshThread = Effect.fn("scenario.freshThread")(function* (
87 ctx: ScenarioContext,
88 prompt = "Hi. [[eval:reply]]",
89) {
90 const threadId = yield* ctx.env.launch(`Fresh ${(yield* uuid).slice(0, 6)}`, prompt);
91 if (!prompt.includes("[[eval:stream")) yield* ctx.env.settled(threadId);
92 return threadId;
93});
94​
95/** Replaces the composer's text, the way a person would after clearing it. */
96const typeInComposer = Effect.fn("scenario.typeInComposer")(function* (
97 ctx: ScenarioContext,
98 text: string,
99) {
100 const { x, y } = ctx.device.center(yield* composerNode(ctx));
101 yield* ctx.device.run(["fill", String(x), String(y), text]);
102 // The composer slides up with the keyboard; a tap before it settles lands on the keyboard.
103 yield* ctx.device.waitFor("the keyboard", (state) => state.composer.keyboard, 10_000);
104 yield* Effect.sleep("1 second");
105});
106​
107const countUserText = (
108 projection: { readonly messages: ReadonlyArray<{ readonly text?: string }> },
109 text: string,
110) => projection.messages.filter((message) => message.text === text).length;
111​
112/** Starts a scripted reply in `threadId` and waits until the app shows it streaming. */
113const startStream = Effect.fn("scenario.startStream")(function* (
114 ctx: ScenarioContext,
115 threadId: ThreadId,
116 script: string,
117) {
118 const before = (yield* ctx.device.state).feed?.content ?? 0;
119 yield* ctx.env.send(threadId, `Write the long report. ${script}`);
120 yield* ctx.device.waitFor(
121 "the reply to start streaming in the feed",
122 (state) => (state.feed?.content ?? 0) > before + 200,
123 20_000,
124 );
125});
126​
127/** Records frames (and the visible feed) while `action` runs; the action's failure is not the metric. */
128/** Records frames (and the visible feed) while `action` runs; stops recording even if it fails. */
129const recordFor = <E, R>(
130 ctx: ScenarioContext,
131 withFeed: boolean,
132 action: Effect.Effect<unknown, E, R>,
133) =>
134 ctx.device.recordStart(withFeed).pipe(
135 Effect.andThen(Effect.exit(action)),
136 Effect.flatMap((exit) =>
137 ctx.device.recordStop.pipe(Effect.flatMap((recording) => Effect.as(exit, recording))),
138 ),
139 );
140​
141/** JS frame times jitter, so the display's refresh rate comes from the UI thread's vsync times. */
142const frameMetrics = (recording: { js: ReadonlyArray<number>; ui: ReadonlyArray<number> }) => {
143 const ui = frameStats(recording.ui, "ui");
144 const refreshMs = typeof ui.uiRefreshHz === "number" ? 1000 / ui.uiRefreshHz : undefined;
145 return { ...frameStats(recording.js, "js", refreshMs), ...ui };
146};
147​
148/** Every scenario fails with this one error, so the table has a single function type. */
149export class ScenarioFailure extends Schema.TaggedError<ScenarioFailure>()("ScenarioFailure", {
150 message: Schema.String,
151}) {}
152​
153const failure = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
154 Effect.mapError(
155 effect,
156 (error) =>
157 new ScenarioFailure({ message: error instanceof Error ? error.message : String(error) }),
158 );
159​
160type Scenario = (ctx: ScenarioContext) => Effect.Effect<Metrics, ScenarioFailure, Crypto.Crypto>;
161​
162export const SCENARIOS = {
163 /** Six upward flings in the long settled thread. */
164 scrollUpLongThread: Effect.fn("scenario.scrollUpLongThread")(function* (ctx: ScenarioContext) {
165 yield* openThread(ctx, ctx.fixtures.longThread);
166 const { height, threadX } = yield* geometry(ctx);
167 const recording = yield* recordFor(
168 ctx,
169 true,
170 Effect.gen(function* () {
171 for (let fling = 0; fling < 6; fling++)
172 yield* ctx.device.swipe(threadX, Math.round(height * 0.35), Math.round(height * 0.8));
173 yield* Effect.sleep("1 second");
174 }),
175 );
176 return { ...feedMotion(recording.feed, 1), ...frameMetrics(recording) } satisfies Metrics;
177 }, failure),
178​
179 /** A long reply streams into the open thread for eight seconds. */
180 streamInView: Effect.fn("scenario.streamInView")(function* (ctx: ScenarioContext) {
181 yield* openThread(ctx, ctx.fixtures.longThread);
182 yield* startStream(ctx, ctx.fixtures.longThread, "[[eval:stream:4000:25]]");
183 const recording = yield* recordFor(ctx, true, Effect.sleep("8 seconds"));
184 const after = yield* ctx.device.state;
185 yield* ctx.env.interruptAll();
186 const motion = feedMotion(recording.feed, 0);
187 return {
188 ...motion,
189 ...frameMetrics(recording),
190 settledFromEndPx: settledFromEnd(recording.feed),
191 contentGrewPx: (recording.feed.at(-1)?.content ?? 0) - (recording.feed[0]?.content ?? 0),
192 followingAtEnd: after.feed?.following === true,
193 } satisfies Metrics;
194 }, failure),
195​
196 /** Types and sends a message from the composer at the end of the long thread. */
197 sendInLongThread: Effect.fn("scenario.sendInLongThread")(function* (ctx: ScenarioContext) {
198 yield* openThread(ctx, ctx.fixtures.longThread);
199 const text = `Follow-up ${(yield* uuid).slice(0, 8)} [[eval:reply]]`;
200 yield* typeInComposer(ctx, text);
201 const recording = yield* recordFor(
202 ctx,
203 true,
204 Effect.gen(function* () {
205 yield* ctx.device.press('label="Send"');
206 yield* Effect.sleep("4 seconds");
207 }),
208 );
209 const after = yield* ctx.device.state;
210 const nativeText = yield* composerText(ctx);
211 const projection = yield* ctx.env.settled(ctx.fixtures.longThread);
212 const motion = feedMotion(recording.feed, 0);
213 return {
214 maxFromEndPx: motion.maxFromEndPx ?? null,
215 finalFromEndPx: motion.finalFromEndPx ?? null,
216 serverMessages: countUserText(projection, text),
217 draftCleared: after.composer.draft === "",
218 editorCleared: nativeText === "",
219 } satisfies Metrics;
220 }, failure),
221​
222 /** A thread in the background finishes while the long thread sits mid-scroll. */
223 backgroundFinish: Effect.fn("scenario.backgroundFinish")(function* (ctx: ScenarioContext) {
224 const key = `bg-${(yield* uuid).slice(0, 8)}`;
225 const other = yield* ctx.env.launch("Background work", `Run the job. [[eval:hold:${key}]]`);
226 yield* openThread(ctx, ctx.fixtures.longThread);
227 const { height, threadX } = yield* geometry(ctx);
228 for (let fling = 0; fling < 2; fling++)
229 yield* ctx.device.swipe(threadX, Math.round(height * 0.4), Math.round(height * 0.7));
230 yield* waitForStill(ctx);
231 const recording = yield* recordFor(
232 ctx,
233 true,
234 Effect.gen(function* () {
235 yield* ctx.env.release(key);
236 yield* ctx.env.settled(other);
237 yield* Effect.sleep("2 seconds");
238 }),
239 );
240 return { ...feedMotion(recording.feed, 0) } satisfies Metrics;
241 }, failure),
242​
243 /** Types in a streaming thread, switches to another thread, and comes back. */
244 draftThreadSwitch: Effect.fn("scenario.draftThreadSwitch")(function* (ctx: ScenarioContext) {
245 const text = `Draft ${(yield* uuid).slice(0, 8)}`;
246 const other = yield* freshThread(ctx);
247 const streaming = yield* freshThread(ctx, "Write the long report. [[eval:stream:4000:25]]");
248 yield* openThread(ctx, streaming);
249 yield* typeInComposer(ctx, text);
250 yield* openThread(ctx, other);
251 yield* Effect.sleep("1 second");
252 const onOther = yield* ctx.device.state;
253 const otherText = yield* composerText(ctx);
254 yield* openThread(ctx, streaming);
255 yield* Effect.sleep("1 second");
256 const back = yield* ctx.device.state;
257 const backText = yield* composerText(ctx);
258 yield* ctx.env.interruptAll();
259 const otherKey = `${ctx.env.environmentId}:${other}`;
260 return {
261 draftKept: back.composer.draft === text,
262 editorShowsDraft: backText === text,
263 leakedIntoOtherDraft: (onOther.composer.drafts[otherKey] ?? "") !== "",
264 leakedIntoOtherEditor: otherText.includes(text),
265 } satisfies Metrics;
266 }, failure),
267​
268 /** Types a draft, sends the app to the background, and brings it back. */
269 draftBackground: Effect.fn("scenario.draftBackground")(function* (ctx: ScenarioContext) {
270 const text = `Draft ${(yield* uuid).slice(0, 8)}`;
271 const threadId = yield* freshThread(ctx);
272 yield* openThread(ctx, threadId);
273 yield* typeInComposer(ctx, text);
274 yield* ctx.device.home;
275 yield* Effect.sleep("3 seconds");
276 // A link back to the thread foregrounds the running app; a plain open would reload its JS.
277 yield* ctx.device.run([
278 "open",
279 APP_ID,
280 "--launch-url",
281 `${SCHEME}://${threadPath(ctx, threadId)}`,
282 ]);
283 const back = yield* ctx.device.waitFor("the thread after foreground", (state) =>
284 isThread(state, threadId),
285 );
286 return {
287 draftKept: back.composer.draft === text,
288 editorShowsDraft: (yield* composerText(ctx)) === text,
289 } satisfies Metrics;
290 }, failure),
291​
292 /** Taps the middle of the composer's text area. */
293 tapComposer: Effect.fn("scenario.tapComposer")(function* (ctx: ScenarioContext) {
294 const threadId = yield* freshThread(ctx);
295 yield* openThread(ctx, threadId);
296 const node = yield* composerNode(ctx);
297 const { x, y } = ctx.device.center(node);
298 yield* ctx.device.pressAt(x, y);
299 yield* Effect.sleep("1500 millis");
300 const after = yield* ctx.device.state;
301 return {
302 stayedOnThread: isThread(after, threadId),
303 editorFocused:
304 after.composer.editors.some((editor) => editor.expanded) && after.composer.keyboard,
305 } satisfies Metrics;
306 }, failure),
307​
308 /** Types `/eval`, picks the workspace's `eval-notes` skill, and checks the editor keeps focus. */
309 skillPick: Effect.fn("scenario.skillPick")(function* (ctx: ScenarioContext) {
310 yield* openThread(ctx, yield* freshThread(ctx));
311 // Short screens fit one menu row above the keyboard, so filter the menu to one row.
312 yield* typeInComposer(ctx, "/eval");
313 const composer = yield* composerNode(ctx);
314 // Menu rows read "<icon>, skill:<name>, <description>". Skills arrive with a workspace
315 // snapshot the draft change requests.
316 let item: AxNode | undefined;
317 for (let attempt = 0; attempt < 10 && !item?.rect; attempt++) {
318 yield* Effect.sleep("1 second");
319 item = (yield* ctx.device.nodes).find(
320 (node) =>
321 node.rect !== undefined &&
322 node.rect.y + node.rect.height <= composer.rect!.y &&
323 node.label?.includes(", skill:eval-notes") === true,
324 );
325 }
326 if (!item?.rect)
327 return yield* new ScenarioFailure({ message: "The / menu did not list skill:eval-notes." });
328 const { x, y } = ctx.device.center(item);
329 yield* ctx.device.pressAt(x, y);
330 yield* Effect.sleep("1500 millis");
331 const after = yield* ctx.device.state;
332 return {
333 editorFocused:
334 after.composer.editors.some((editor) => editor.expanded) && after.composer.keyboard,
335 } satisfies Metrics;
336 }, failure),
337​
338 /** Sends from a thread whose provider instance the server no longer has. */
339 failedSend: Effect.fn("scenario.failedSend")(function* (ctx: ScenarioContext) {
340 const { threadId, lock } = yield* ctx.env.launchLockable("Locked thread");
341 yield* openThread(ctx, threadId);
342 const text = `Will fail ${(yield* uuid).slice(0, 8)}`;
343 yield* typeInComposer(ctx, text);
344 yield* lock;
345 // The app learns of the setting a moment after the server commits it.
346 yield* Effect.sleep("2 seconds");
347 // Either outcome is honest: Send turns off, or the rejected send shows an error.
348 const sendBlocked =
349 (yield* ctx.device.nodes).find((node) => node.label === "Send")?.enabled === false;
350 if (!sendBlocked) yield* ctx.device.press('label="Send"');
351 const shown = sendBlocked
352 ? false
353 : (yield* ctx.device
354 .waitFor("a composer error", (state) => state.composer.error !== null, 8_000)
355 .pipe(Effect.option))._tag === "Some";
356 return {
357 sendBlocked,
358 errorShown: shown,
359 silentFailure: !sendBlocked && !shown,
360 } satisfies Metrics;
361 }, failure),
362​
363 /** A working thread finishes while Home is open. */
364 workingShelfFinish: Effect.fn("scenario.workingShelfFinish")(function* (ctx: ScenarioContext) {
365 const key = `shelf-${(yield* uuid).slice(0, 8)}`;
366 const threadId = yield* ctx.env.launch("Shelf work", `Run the job. [[eval:hold:${key}]]`);
367 const working = yield* ctx.device.waitFor(
368 "the thread to show as working",
369 (state) => state.threads[threadId] === "working",
370 );
371 yield* ctx.env.release(key);
372 yield* ctx.env.settled(threadId);
373 // The shelf keeps a finished row for WORKING_EXIT_GRACE_MS (2 s) on purpose.
374 yield* Effect.sleep("5 seconds");
375 const after = yield* ctx.device.state;
376 return {
377 shelfShownWhileWorking: shelfCount(working) > 0,
378 shelfCountAfter: shelfCount(after),
379 // For diagnosis: threads the app itself calls working, beside the shelf's count.
380 appWorkingThreads: Object.values(after.threads).filter((status) => status === "working")
381 .length,
382 statusAfter: after.threads[threadId] ?? "missing",
383 } satisfies Metrics;
384 }, failure),
385​
386 /** A thread asks a question while Home is open. */
387 pendingQuestion: Effect.fn("scenario.pendingQuestion")(function* (ctx: ScenarioContext) {
388 const threadId = yield* ctx.env.launch("Question", `Ask me first. [[eval:question]]`);
389 yield* ctx.env.waitForPendingInput(threadId);
390 yield* Effect.sleep("2 seconds");
391 const after = yield* ctx.device.state;
392 yield* ctx.env.interruptAll();
393 return {
394 status: after.threads[threadId] ?? "missing",
395 shelfCount: shelfCount(after),
396 } satisfies Metrics;
397 }, failure),
398​
399 /** A thread runs two subagents; Home and the thread must not look idle. */
400 subagentsRunning: Effect.fn("scenario.subagentsRunning")(function* (ctx: ScenarioContext) {
401 const key = `sub-${(yield* uuid).slice(0, 8)}`;
402 const threadId = yield* ctx.env.launch("Subagents", `Fan out. [[eval:subagents:${key}]]`);
403 yield* ctx.env.waitForSubagents(threadId);
404 yield* Effect.sleep("2 seconds");
405 const home = yield* ctx.device.state;
406 yield* openThread(ctx, threadId);
407 yield* Effect.sleep("1500 millis");
408 const thread = yield* ctx.device.state;
409 yield* ctx.env.release(key);
410 yield* ctx.env.settled(threadId);
411 return {
412 status: home.threads[threadId] ?? "missing",
413 threadShowsStatus: thread.floatingStatus !== null,
414 } satisfies Metrics;
415 }, failure),
416​
417 /** Scrolls a finished subagent's child thread. */
418 openSubagentThread: Effect.fn("scenario.openSubagentThread")(function* (ctx: ScenarioContext) {
419 const key = `child-${(yield* uuid).slice(0, 8)}`;
420 const threadId = yield* ctx.env.launch("Parent", `Fan out. [[eval:subagents:${key}]]`);
421 yield* ctx.env.waitForSubagents(threadId);
422 yield* ctx.env.release(key);
423 const projection = yield* ctx.env.settled(threadId);
424 const child = projection.subagents.find(
425 (subagent) => subagent.childThreadId !== null,
426 )?.childThreadId;
427 if (!child)
428 return yield* new ScenarioFailure({ message: "The subagent made no child thread." });
429 yield* openThread(ctx, child);
430 const { height, threadX } = yield* geometry(ctx);
431 const recording = yield* recordFor(
432 ctx,
433 true,
434 Effect.gen(function* () {
435 yield* ctx.device.swipe(threadX, Math.round(height * 0.7), Math.round(height * 0.3));
436 yield* ctx.device.swipe(threadX, Math.round(height * 0.3), Math.round(height * 0.7));
437 }),
438 );
439 return {
440 movedPx: feedMotion(recording.feed, 0).movedPx ?? 0,
441 ...frameMetrics(recording),
442 } satisfies Metrics;
443 }, failure),
444​
445 /** Long-presses a row on Home and picks Pin. */
446 pinFromLongPress: Effect.fn("scenario.pinFromLongPress")(function* (ctx: ScenarioContext) {
447 const title = `Pin target ${(yield* uuid).slice(0, 6)}`;
448 const target = yield* ctx.env.launch(title, "Hi. [[eval:reply]]");
449 yield* ctx.env.settled(target);
450 yield* waitForRow(ctx, title);
451 // A just-inserted row can swallow the first long-press while it animates in.
452 for (let attempt = 0; attempt < 2; attempt++) {
453 yield* ctx.device.longPress(rowSelector(title));
454 yield* Effect.sleep("1 second");
455 if ((yield* ctx.device.nodes).some((node) => node.label === "Pin")) break;
456 }
457 yield* ctx.device.press('label="Pin"');
458 yield* Effect.sleep("2 seconds");
459 const shell = yield* ctx.env.shell();
460 const pinned = shell.threads
461 .filter((thread) => thread.pinnedAt != null)
462 .map((thread) => thread.id);
463 for (const id of pinned) yield* ctx.env.unpin(id);
464 return {
465 targetPinned: pinned.includes(target),
466 otherPinned: pinned.filter((id) => id !== target).length,
467 } satisfies Metrics;
468 }, failure),
469​
470 /** Opens a link to a thread created after the app loaded its list. */
471 linkToNewThread: Effect.fn("scenario.linkToNewThread")(function* (ctx: ScenarioContext) {
472 const threadId = yield* ctx.env.launch("Linked thread", "Hi. [[eval:reply]]");
473 yield* ctx.device.open(threadPath(ctx, threadId));
474 yield* Effect.sleep("4 seconds");
475 const after = yield* ctx.device.state;
476 return { opensTarget: isThread(after, threadId) } satisfies Metrics;
477 }, failure),
478​
479 /** Opens a link to an archived thread. */
480 linkToArchivedThread: Effect.fn("scenario.linkToArchivedThread")(function* (
481 ctx: ScenarioContext,
482 ) {
483 const threadId = yield* ctx.env.launch("Archived thread", "Hi. [[eval:reply]]");
484 yield* ctx.env.settled(threadId);
485 yield* ctx.env.archive(threadId);
486 yield* Effect.sleep("1 second");
487 yield* ctx.device.open(threadPath(ctx, threadId));
488 yield* Effect.sleep("4 seconds");
489 const after = yield* ctx.device.state;
490 return { opensTarget: isThread(after, threadId) } satisfies Metrics;
491 }, failure),
492​
493 /** Taps a row on Home. */
494 openFromList: Effect.fn("scenario.openFromList")(function* (ctx: ScenarioContext) {
495 const title = `Open me ${(yield* uuid).slice(0, 6)}`;
496 const target = yield* ctx.env.launch(title, "Hi. [[eval:reply]]");
497 yield* ctx.env.settled(target);
498 yield* waitForRow(ctx, title);
499 // A just-inserted row animates in, and a tap during that misses it.
500 yield* Effect.sleep("1 second");
501 yield* ctx.device.press(rowSelector(title));
502 const after = yield* ctx.device
503 .waitFor("the tapped thread", (state) => isThread(state, target))
504 .pipe(Effect.option);
505 return { opensTarget: after._tag === "Some" } satisfies Metrics;
506 }, failure),
507​
508 /** On a folded Duo: open a thread from the list, go back, and open another. */
509 duoBackThenOpen: Effect.fn("scenario.duoBackThenOpen")(function* (ctx: ScenarioContext) {
510 const tag = (yield* uuid).slice(0, 6);
511 const first = yield* ctx.env.launch(`Duo first ${tag}`, "Hi. [[eval:reply]]");
512 const next = yield* ctx.env.launch(`Duo next ${tag}`, "Hi. [[eval:reply]]");
513 yield* ctx.env.settled(first);
514 yield* ctx.env.settled(next);
515 yield* waitForRow(ctx, `Duo first ${tag}`);
516 yield* ctx.device.press(rowSelector(`Duo first ${tag}`));
517 yield* ctx.device.waitFor("the first thread", (state) => isThread(state, first));
518 yield* ctx.device.back;
519 yield* ctx.device.waitFor("Home", (state) => state.route?.name === "Home");
520 yield* ctx.device.press(rowSelector(`Duo next ${tag}`));
521 yield* Effect.sleep("2 seconds");
522 const after = yield* ctx.device.state;
523 // A thread can mount in a hidden column, so its title must sit inside the visible screen.
524 const nodes = yield* ctx.device.nodes;
525 const screen = nodes[0]?.rect;
526 // The header title is a text node inside a navigation bar; a Home row with the title is not.
527 const bars = nodes.flatMap((node) =>
528 node.kind === "navigation-bar" && node.rect ? [node.rect] : [],
529 );
530 const title = nodes.find(
531 (node) =>
532 node.label === `Duo next ${tag}` &&
533 bars.some((bar) => node.rect && node.rect.y >= bar.y && node.rect.y < bar.y + bar.height),
534 )?.rect;
535 return {
536 opensTarget: isThread(after, next),
537 titleOnScreen:
538 screen !== undefined &&
539 title !== undefined &&
540 title.x >= screen.x &&
541 title.x + title.width <= screen.x + screen.width,
542 } satisfies Metrics;
543 }, failure),
544} satisfies Record<string, Scenario>;
545​
546export type ScenarioName = keyof typeof SCENARIOS;
547​
548/** Row rects are relative to their list, so rows are tapped by label, not by point. */
549function rowSelector(title: string) {
550 return `label="${title}"`;
552​
553/** Waits until fling momentum ends: the feed's offset holds still across half a second. */
554const waitForStill = Effect.fn("scenario.waitForStill")(function* (ctx: ScenarioContext) {
555 let previous = -1;
556 for (let attempt = 0; attempt < 20; attempt++) {
557 const scroll = (yield* ctx.device.state).feed?.scroll ?? -1;
558 if (scroll === previous) return;
559 previous = scroll;
560 yield* Effect.sleep("500 millis");
561 }
562});
563​
564/** Largest distance from the end on frames where the content did not just grow. */
565function settledFromEnd(samples: ReadonlyArray<FeedSample>) {
566 let worst = 0;
567 for (let index = 1; index < samples.length; index++)
568 if (samples[index]!.content === samples[index - 1]!.content)
569 worst = Math.max(worst, samples[index]!.fromEnd ?? 0);
570 return worst;
572​
573/** Waits until a row with this title is on screen. */
574const waitForRow = Effect.fn("scenario.waitForRow")(function* (
575 ctx: ScenarioContext,
576 title: string,
577) {
578 for (let attempt = 0; attempt < 20; attempt++) {
579 const nodes = yield* ctx.device.nodes;
580 if (nodes.some((node) => node.label?.includes(title))) return;
581 // A crowded list can hold the new row below the fold; scroll to the top once.
582 if (attempt === 4) yield* ctx.device.run(["scroll", "top"]).pipe(Effect.ignore);
583 // A finished row can stay folded in the Working shelf; open it once if it is collapsed.
584 const shelf = nodes.find((node) => /^\d+ working threads?$/.test(node.label ?? ""));
585 const collapsed = (yield* ctx.device.state).homeList?.some(
586 (entry) => entry.type === "v2-working-shelf" && entry.expanded === false,
587 );
588 if (attempt === 10 && collapsed && shelf?.label)
589 yield* ctx.device.press(`label="${shelf.label}"`);
590 yield* Effect.sleep("500 millis");
591 }
592 return yield* new ScenarioFailure({ message: `Row "${title}" never appeared on Home.` });
593});
594​
595function shelfCount(state: ProbeState) {
596 return state.homeList?.find((entry) => entry.type === "v2-working-shelf")?.count ?? 0;