Files
software-mansion__argent/packages/tool-server/test/flows/flow-pinch.test.ts
Sebastian Flajszer 1e69c90981 feat(ios-device): add physical iOS support (#944)
# Physical iOS device support

Try it now (cabled iPhone, unlocked, Developer Mode on):

```bash
argent run list-devices                                            # copy the 25-char UDID
argent run launch-app --udid <UDID> --bundleId com.apple.Preferences
```

First launch builds and signs the on-device runner: 3 to 5 minutes, keep
the phone unlocked. Every later start: seconds, from cache. No signing
setup needed if an Apple ID is signed into Xcode; the team is detected
from the keychain.

## What this adds

- Physical iPhone support, on by default: a cabled iPhone simply appears
in `list-devices`. iPhone-only: physical iPads are skipped at discovery.
- 15 tools ported to hardware. Flows replay on device.
- Zero-config signing from the keychain; `ARGENT_IOS_TEAM_ID` is an
override, nothing is persisted.
- Self-contained npm install: the runner's Swift sources ship in the
package. New package `@argent/ios-device-runner` (an XCUITest bundle
hosting an HTTP command server on the device), new tool-server backend
under `src/utils/ios-device/`.
- The one always-on cost: `list-devices` on macOS now runs physical
discovery in parallel with the other platform probes, under the existing
8s devicectl timeout; off-macOS it early-returns.

## Suggested review order

Rough estimate: half a day for the full pass.

1. `packages/ios-device-runner/PROTOCOL.md`, then `README.md` (wire
contract and reliability model, ~15 min)
2. `packages/tool-server/src/utils/ios-device/` (devicectl, usbmux,
team-detect, runner-build, runner-commands: the core)
3. `packages/tool-server/src/blueprints/ios-device-runner.ts` (service
lifecycle, crash recovery)
4. Per-tool device arms and the capability gates
(`ios-physical-device-classification.test.ts` is the ratchet)
5. Docs page, the two new skills, the Swift runner sources

## Requirements

1. macOS host with full Xcode (iOS platform installed); uses `xcrun
devicectl` and `xcodebuild`.
2. An Apple ID signed into Xcode; automatic signing mints the runner's
provisioning profile.
3. USB cable only. Wi-Fi is deliberately unsupported: the transport is
usbmux, and a Wi-Fi-only device lists as `paired`, not `connected`.
4. iPhone paired with and trusting the Mac, Developer Mode on, A12/2018
or newer (modern 8+16-hex UDID, the CoreDevice floor). Legacy 40-hex
UDIDs are not classified as iOS.
5. Signing resolves as: `ARGENT_IOS_TEAM_ID` in the tool-server's
environment (explicit override, CI) -> keychain detection (newest
identity first, memoized per server run, never persisted). One
certificate: nothing to do. Several teams: newest wins; the first tool
result names the pick, the runner-ups, and the override. No certificate:
the error walks through Xcode > Settings > Accounts and Manage
Certificates > + > Apple Development, then retry; it never names the env
var, since a team id alone cannot sign. Bundle ids derive per team
(`com.argent.runner.t<teamid>`). `ARGENT_IOS_RUNNER_PROJECT` overrides
the project path for unusual layouts.

Build artifacts cache under `~/.argent/ios-device-runner`, keyed by
runner sources, Xcode/SDK version, and signing. A team with no
registered devices self-heals: the failed generic build retries against
the connected phone, which registers it.

## How it works

1. `xcrun devicectl` (CoreDevice) is the control plane: discovery,
readiness, app lifecycle. Readiness gates on the wired transport, so an
unplugged phone is refused before any build starts.
2. A signed XCUITest runner is built lazily with the resolved team,
started via `xcodebuild test-without-building`, and parks as an HTTP
command server for 24h. On an npm install the Xcode project is the copy
shipped inside the package.
3. The listener binds to the device's loopback interface; the host
reaches it over usbmuxd with a dependency-free `node:net` client.
4. Mutating commands are journaled by `commandId`; a lost reply is
resolved by asking `status` for the journaled fate, never by replay
(at-most-once delivery).
5. Routing hangs off `resolveDevice` UDID-shape classification plus a
per-tool capability allowlist. CLI and MCP layers are untouched; outside
tool-server, docs, and the runner package the diff is failure-code
constants (registry), two agent skills plus rule updates (skills), the
pack step (packages/argent), and the e2e smoke script.

## Supported tools

| Tool | Status | Notes |
| ----------------- | --------- |
------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| launch-app | Supported | Plain `devicectl` launch. System UI
(SpringBoard/Spotlight) registers a session only; no launch attempt.
Carries a note when signing is not resolvable yet. |
| restart-app | Supported | System UI bundle ids rejected before any
device contact. |
| reinstall-app | Supported | Same system UI pre-flight. |
| screenshot | Supported | Served through the runner; device-wide, needs
no registered app. (No host-side devicectl screenshot exists on any
toolchain; that dead route was removed.) |
| describe | Supported | AX tree snapshot via the runner. Snapshots the
session app's process, so another process's dialogs are not in the tree.
A backgrounded session app fails with an actionable error instead of
being silently re-fronted. Focus polling is off for this source; typing
uses a fixed settle. |
| keyboard | Supported | Types into the focused element; typing with
nothing focused is refused with "tap the text field first". `enter` and
`backspace` are the named keys (both exist as software-keyboard
characters; there is no per-keycode HID surface on hardware).
Whitespace-only keys rejected, not silently "typed". |
| gesture-tap | Supported | `clickCount > 1` dispatches as one native
multi-tap gesture, not separate taps. |
| gesture-swipe | Supported | Single-finger drag between two points. |
| gesture-custom | Supported | Two shapes only: press-hold and straight
single-finger drag. Second finger or `Move` waypoints rejected with
authoring guidance, before any runner starts. |
| button | Supported | `home`, `volumeUp`, `volumeDown`, `actionButton`
(volume buttons are simulator-unavailable in the SDK, not
device-unavailable). `power`/`appSwitch` have no public XCUIDevice API
and reject before device contact; a button the model lacks gets a typed
refusal via `hasHardwareButton`. |
| await-ui-element | Supported | Polls the same runner snapshot describe
returns; waits and taps see identical frames. Degrades to `cause:
"unreadable"` with a note on a backgrounded app, never a raw throw. |
| await-screen-idle | Supported | Same describe path; resolves
not-settled instead of throwing, so a follow-up screenshot still runs. |
| run-sequence | Supported | Outer gate only; each step pre-flights its
own capability. |
| open-url | Supported | devicectl launches the receiving app with the
URL as its launch payload. http(s) defaults to Safari; any other scheme
needs the new optional `bundleId` param (hardware cannot resolve a
scheme's handler system-wide). |
| screenshot-diff | Supported | Live captures through the runner
(full-resolution, device-wide, no registered app needed); the diff
itself runs on the host. Baselines are per device model/resolution;
`rotation` is ignored on hardware. |

Flows run through these tools; the device flow tree gets the same
`subtreeText` hoisting as every other platform, so container-scoped
`text` asserts work.

## Not supported (400 at the capability gate)

1. gesture-pinch, gesture-rotate: no two-finger coordinate API on
hardware. Pinch/rotate flow directives also fail upfront with a clear
error.
2. screen-recording-start/stop (video): simulator-only capture pipeline.
3. rotate, paste, shake, settings-permissions: simulator-only backends,
not ported (paste also hits the iOS 16+ cross-app paste permission
prompt).
4. native-devtools, debugger, profiler families: simulator-side
instrumentation (DYLD injection does not exist on hardware).
5. Physical iPads: filtered at discovery for now.

A test ratchet (`ios-physical-device-classification.test.ts`) keeps
`apple.device` declarations in sync with the ported list.

## Core contracts

1. Observation never mutates the screen: a backgrounded session app
makes `describe`/`await-*` fail with an error naming the three ways out
(screenshot, launch-app, or launch-app com.apple.springboard). Mutating
commands do re-front, and their result carries `reactivated: true`.
2. The runner listener is unauthenticated; mitigation is the
loopback-only bind.
3. `list-devices` states: `connected` (cabled now, usable) vs `paired`
(unreachable, never auto-bound). Wired transport alone decides; a
Wi-Fi-migrated CoreDevice tunnel does not count.
4. The multi-team signing pick rides the first affected tool result as a
one-shot note (the pre-existing notes channel). The `list-devices`
result shape is unchanged from main: devices and avds, nothing else.
5. Rejections are typed 400 validation errors end to end, so a batch
flow run skips one failing flow instead of aborting.

Also: the runner is a first-class device-owned service
(`stop-simulator-server` and `stop-all-simulator-servers` reap it,
detached xcodebuild child included; a crashed runner respawns on the
next call). A passcode-locked phone refuses launches; the devicectl
10002 hint also covers a pending system prompt on an unlocked phone.
Build failures surface xcodebuild's own `error:` lines with signing
hints (no registered devices, free-team app-id cap, missing
provisioning, the `errSecInternalComponent` partition-list fix). An app
XCTest reports as `.unknown` (a swipe-killed foreign app) is refused
with launch-app guidance. Status-bar pinning is skipped on hardware;
pixel-settle masks the top band.

## Testing

- Unit coverage, hardware-independent: UDID classification and
capability routing; usbmux framing and transport; devicectl list/hints
parsing incl. the silent iPhone-only filter; keychain detection against
real certificate fixtures; runner build/cache self-heals (poisoned
cache, concurrent checkout, fresh-team retry); crash/journal recovery;
the backgrounded-observation and reactivated contracts; keyboard guards
incl. the not-focused refusal; system-UI pre-flight; per-tool device
arms.
- Manual smoke on a real device: `scripts/e2e-ios-physical-device.mjs`.

## Docs and skills

1. `features/physical-ios-devices.mdx`: the user-facing walkthrough
(zero-config signing with the sign-in fallback, prerequisites,
app-scoped model, troubleshooting).
2. `fundamentals/supported-platforms.mdx`: physical iPhones in the
platform matrix (iPads not supported yet).
3. `reference/tools.mdx` and `reference/configuration.mdx`: list-devices
states and the `ARGENT_IOS_TEAM_ID` override.
4. Two skills ship with the package via `argent init`:
`argent-ios-device-setup` (setup ladder plus a troubleshooting table
keyed to real error texts) and `argent-ios-device-interact` (the
app-scoped hardware contract, supported subset with alternatives,
gesture recipes, pitfalls). The agent rule and sibling skills route to
them.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
  * Added experimental USB-connected physical iPhone support.
* Physical iPhones now support discovery, app lifecycle actions, URL
opening, accessibility snapshots, screenshots, taps, swipes, custom
gestures, keyboard input, waits, flows, and screenshot comparisons.
* Added automatic signing-team detection with optional
`ARGENT_IOS_TEAM_ID` configuration.
* Added clear handling for paired or unavailable devices and unsupported
hardware actions.

* **Documentation**
* Added setup, troubleshooting, capability, and physical-device usage
guidance.
* Clarified that physical iPads and several simulator-only tools and
gestures remain unsupported.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-03 14:33:11 +02:00

490 lines
19 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import type { Registry } from "@argent/registry";
import type { DescribeNode, DescribeTreeData } from "../../src/tools/describe/contract";
// Serve the flow tree directly: flows resolve selectors against the platform's
// full-hierarchy source and hard-fail rather than degrade to the AX tree, so
// these unit tests stub the tree fetch itself.
let currentTree: () => DescribeNode;
vi.mock("../../src/tools/flows/flow-tree", async (importOriginal) => ({
...(await importOriginal<typeof import("../../src/tools/flows/flow-tree")>()),
fetchFlowTree: vi.fn(
async (): Promise<DescribeTreeData> => ({
tree: currentTree(),
source: "native-devtools",
})
),
}));
import { createRunFlowTool, type FlowRunResult } from "../../src/tools/flows/flow-run";
import { MIN_VIABLE_TRAVEL } from "../../src/tools/flows/flow-pinch-geometry";
import { serializeFlow, parseFlow } from "../../src/tools/flows/flow-utils";
const DEVICE = "00000000-0000-0000-0000-0000000000ab"; // iOS UDID shape
let tmpDir: string;
function n(partial: Partial<DescribeNode> & { frame: DescribeNode["frame"] }): DescribeNode {
return { role: "AXOther", children: [], ...partial };
}
function screen(children: DescribeNode[]): DescribeNode {
return n({ role: "AXWindow", frame: { x: 0, y: 0, width: 1, height: 1 }, children });
}
/** Registry that records every gesture tool invocation's args. */
function mockRegistry(calls: Array<{ tool: string; args: Record<string, unknown> }>): Registry {
return {
invokeTool: vi.fn(async (id: string, args: Record<string, unknown>) => {
if (id === "list-devices") return { devices: [] };
calls.push({ tool: id, args });
return { ok: true };
}),
getTool: vi.fn(() => ({ inputSchema: { properties: { udid: {} } } })),
} as unknown as Registry;
}
async function writeFlow(name: string, yaml: Parameters<typeof serializeFlow>[0]): Promise<void> {
const dir = path.join(tmpDir, ".argent", "flows");
await fs.mkdir(dir, { recursive: true });
await fs.writeFile(path.join(dir, `${name}.yaml`), serializeFlow(yaml), "utf8");
}
function asRun(r: FlowRunResult | { notice: string }): FlowRunResult {
if (!("steps" in r)) throw new Error(`expected a run result, got notice: ${r.notice}`);
return r;
}
async function run(
name: string,
device = DEVICE
): Promise<FlowRunResult & { calls: Array<{ tool: string; args: Record<string, unknown> }> }> {
const calls: Array<{ tool: string; args: Record<string, unknown> }> = [];
const tool = createRunFlowTool(mockRegistry(calls));
const result = asRun(await tool.execute({}, { name, project_root: tmpDir, device }));
return Object.assign(result, { calls });
}
beforeEach(async () => {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "flow-pinch-"));
currentTree = () => screen([]);
});
afterEach(async () => {
await fs.rm(tmpDir, { recursive: true, force: true });
});
describe("pinch: parse/serialize", () => {
it("round-trips the scale-only and on+scale spellings", () => {
const flow = {
executionPrerequisite: "",
steps: [
{ kind: "pinch" as const, scale: 2 },
{ kind: "pinch" as const, scale: 0.5 },
{ kind: "pinch" as const, selector: { text: "Map", loose: true }, scale: 3 },
{ kind: "pinch" as const, selector: { identifier: "map" }, scale: 20 },
],
};
expect(parseFlow(serializeFlow(flow)).steps).toEqual(flow.steps);
});
it("emits `on` before `scale`", () => {
const yaml = serializeFlow({
executionPrerequisite: "",
steps: [{ kind: "pinch", selector: { text: "Map", loose: true }, scale: 3 }],
});
expect(yaml).toMatch(/pinch:\n\s+on: Map\n\s+scale: 3/);
});
it("parses the options map with the usual selector sugar", () => {
const steps = parseFlow(
"steps:\n" +
' - pinch: { on: "Map", scale: 3 }\n' +
" - pinch: { on: { id: map }, scale: 0.5 }\n" +
" - pinch: { scale: 2 }\n"
).steps;
expect(steps).toEqual([
{ kind: "pinch", selector: { text: "Map", loose: true }, scale: 3 },
{ kind: "pinch", selector: { identifier: "map" }, scale: 0.5 },
{ kind: "pinch", scale: 2 },
]);
});
it("rejects a non-map body with the options-map hint, not a scale type error", () => {
for (const bad of ['"Map"', "2", "null"]) {
let message = "";
try {
parseFlow(`steps:\n - pinch: ${bad}\n`);
} catch (err) {
message = err instanceof Error ? err.message : String(err);
}
expect(message).toMatch(/pinch takes an options map/);
expect(message).not.toMatch(/scale must be/);
}
});
it("rejects top-level selector fields with the nested-selector hint", () => {
expect(() => parseFlow('steps:\n - pinch: { text: "Map", scale: 2 }\n')).toThrow(
/takes a nested selector/i
);
expect(() => parseFlow("steps:\n - pinch: { id: map, scale: 2 }\n")).toThrow(
/takes a nested selector/i
);
});
it("rejects unknown keys with a typo suggestion", () => {
expect(() => parseFlow("steps:\n - pinch: { on: Map, scale: 2, angle: 45 }\n")).toThrow(
/unknown key `angle`.*allowed keys: on, scale/i
);
expect(() => parseFlow("steps:\n - pinch: { on: Map, scal: 2 }\n")).toThrow(
/did you mean `scale`/i
);
});
it("rejects invalid scales (validity only — no magnitude cap)", () => {
for (const bad of ["0", "1", "-1", ".inf", '"2"']) {
expect(() => parseFlow(`steps:\n - pinch: { on: Map, scale: ${bad} }\n`)).toThrow(
/pinch\.scale must be a finite number > 0 and ≠ 1/
);
}
expect(() => parseFlow("steps:\n - pinch: { on: Map }\n")).toThrow(
/pinch\.scale must be a finite number/
);
// No policy cap: an extreme scale parses fine.
expect(parseFlow("steps:\n - pinch: { scale: 1000000 }\n").steps).toEqual([
{ kind: "pinch", scale: 1000000 },
]);
});
});
describe("pinch: execution", () => {
it("dispatches gesture-pinch at the element center with the full requested ratio", async () => {
currentTree = () =>
screen([n({ label: "Map", frame: { x: 0.1, y: 0.3, width: 0.8, height: 0.4 } })]);
await writeFlow("pinch-map", {
executionPrerequisite: "",
steps: [{ kind: "pinch", selector: { text: "Map", loose: true }, scale: 4 }],
});
const result = await run("pinch-map");
expect(result.ok).toBe(true);
expect(result.calls).toHaveLength(1);
const { tool, args } = result.calls[0]!;
expect(tool).toBe("gesture-pinch");
expect(args.udid).toBe(DEVICE);
expect(args.centerX).toBeCloseTo(0.5, 9);
expect(args.centerY).toBeCloseTo(0.5, 9);
expect(args.angle).toBe(0);
expect((args.endDistance as number) / (args.startDistance as number)).toBeCloseTo(4, 9);
// Centered target: the centroid never needs to drift.
expect(args).not.toHaveProperty("endCenterX");
expect(args).not.toHaveProperty("endCenterY");
});
it("defaults to the screen center and span when no selector is given, not to a node on screen", async () => {
// Non-empty on purpose: against an empty tree a pinch that wrongly resolved
// a frame would find none and fall back to the center anyway, so nothing
// told the two apart. This node would give centerX 0.2, centerY 0.75 and a
// 0.24 start span instead.
currentTree = () =>
screen([n({ label: "Panel", frame: { x: 0.05, y: 0.7, width: 0.3, height: 0.1 } })]);
await writeFlow("pinch-center", {
executionPrerequisite: "",
steps: [{ kind: "pinch", scale: 0.5 }],
});
const result = await run("pinch-center");
expect(result.ok).toBe(true);
const args = result.calls[0]!.args;
expect(args.centerX).toBeCloseTo(0.5, 9);
expect(args.centerY).toBeCloseTo(0.5, 9);
// Zoom out: fingers travel together from the edge-safe start span.
expect(args.startDistance).toBeCloseTo(0.84, 9);
expect(args.endDistance).toBeCloseTo(0.42, 9);
});
it("chains scale 20 into 3 equal-ratio gestures separated by settle delays", async () => {
await writeFlow("pinch-20", {
executionPrerequisite: "",
steps: [{ kind: "pinch", scale: 20 }],
});
const t0 = Date.now();
const result = await run("pinch-20");
const elapsed = Date.now() - t0;
expect(result.ok).toBe(true);
expect(result.calls.map((c) => c.tool)).toEqual([
"gesture-pinch",
"gesture-pinch",
"gesture-pinch",
]);
for (const { args } of result.calls) {
expect((args.endDistance as number) / (args.startDistance as number)).toBeCloseTo(
20 ** (1 / 3),
9
);
}
// Two inter-gesture recognizer resets of 250ms each.
expect(elapsed).toBeGreaterThanOrEqual(450);
});
it("never lets target size alter the gesture count", async () => {
currentTree = () =>
screen([n({ label: "Tiny", frame: { x: 0.49, y: 0.49, width: 0.02, height: 0.01 } })]);
await writeFlow("pinch-tiny-20", {
executionPrerequisite: "",
steps: [{ kind: "pinch", selector: { text: "Tiny", loose: true }, scale: 20 }],
});
const result = await run("pinch-tiny-20");
expect(result.ok).toBe(true);
expect(result.calls.filter((c) => c.tool === "gesture-pinch")).toHaveLength(3);
});
it("forwards the drifted end centroid on the moving axis only, for a corner target", async () => {
currentTree = () =>
screen([n({ label: "Corner", frame: { x: 0, y: 0.86, width: 0.3, height: 0.14 } })]);
await writeFlow("pinch-corner", {
executionPrerequisite: "",
steps: [{ kind: "pinch", selector: { text: "Corner", loose: true }, scale: 4 }],
});
const result = await run("pinch-corner");
expect(result.ok).toBe(true);
const args = result.calls[0]!.args;
expect(args.angle).toBe(0); // parallel to the violated bottom edge
expect(args.centerX).toBeCloseTo(0.15, 9);
expect(args.centerY).toBeCloseTo(0.93, 9);
expect((args.endDistance as number) / (args.startDistance as number)).toBeCloseTo(4, 9);
expect(args.endCenterX).toBeCloseTo(0.3, 9); // drifted inward off the corner
expect(args).not.toHaveProperty("endCenterY"); // fixed perpendicular axis
// Final pointers stay inside the screen inset.
const half = (args.endDistance as number) / 2;
expect((args.endCenterX as number) - half).toBeGreaterThanOrEqual(0.02 - 1e-9);
expect((args.endCenterX as number) + half).toBeLessThanOrEqual(0.98 + 1e-9);
});
it("forwards vertical centroid drift for a tall target near the bottom edge", async () => {
currentTree = () =>
screen([n({ label: "Portrait", frame: { x: 0.475, y: 0.55, width: 0.05, height: 0.45 } })]);
await writeFlow("pinch-portrait", {
executionPrerequisite: "",
steps: [{ kind: "pinch", selector: { text: "Portrait", loose: true }, scale: 4 }],
});
const result = await run("pinch-portrait");
expect(result.ok).toBe(true);
const args = result.calls[0]!.args;
expect(args.angle).toBe(90);
expect(args.centerX).toBeCloseTo(0.5, 9);
expect(args.centerY).toBeCloseTo(0.775, 9);
expect(args.startDistance).toBeCloseTo(0.24, 9);
expect(args.endDistance).toBeCloseTo(0.96, 9);
expect(args.endCenterY).toBeCloseTo(0.5, 9);
expect(args).not.toHaveProperty("endCenterX");
});
it("picks the platform's guards: same edge target pinches horizontally on iOS, vertically on Android", async () => {
currentTree = () =>
screen([n({ label: "Edge", frame: { x: 0, y: 0.45, width: 0.3, height: 0.1 } })]);
await writeFlow("pinch-edge", {
executionPrerequisite: "",
steps: [{ kind: "pinch", selector: { text: "Edge", loose: true }, scale: 4 }],
});
const ios = await run("pinch-edge");
expect(ios.calls[0]!.args.angle).toBe(0);
const android = await run("pinch-edge", "emulator-5554");
expect(android.calls[0]!.args.angle).toBe(90);
});
it("prefers a perceptible edge-risky gesture over a sub-floor safe one", async () => {
// Wide thin target centered inside Android's 0.13 left guard: the vertical
// candidate is axis-safe but its travel is a recognizer no-op, so the
// substantial horizontal candidate must win despite the guard violation.
currentTree = () =>
screen([n({ label: "Strip", frame: { x: 0, y: 0.4975, width: 0.2, height: 0.005 } })]);
await writeFlow("pinch-strip", {
executionPrerequisite: "",
steps: [{ kind: "pinch", selector: { text: "Strip", loose: true }, scale: 4 }],
});
const result = await run("pinch-strip", "emulator-5554");
expect(result.ok).toBe(true);
const args = result.calls[0]!.args;
expect(args.angle).toBe(0);
expect(args.startDistance).toBeCloseTo(0.16, 9);
expect(args.endDistance).toBeCloseTo(0.64, 9);
expect((args.endDistance as number) - (args.startDistance as number)).toBeGreaterThanOrEqual(
MIN_VIABLE_TRAVEL
);
});
it("falls back to safety-rank ordering when every candidate is sub-floor", async () => {
// Tiny target inside the iOS left guard: neither axis clears the travel
// floor, so the vertical (axis-safe) candidate wins on safety rank and the
// gesture is still dispatched, never rejected.
currentTree = () =>
screen([n({ label: "Dot", frame: { x: 0.045, y: 0.4975, width: 0.01, height: 0.005 } })]);
await writeFlow("pinch-dot", {
executionPrerequisite: "",
steps: [{ kind: "pinch", selector: { text: "Dot", loose: true }, scale: 4 }],
});
const result = await run("pinch-dot");
expect(result.ok).toBe(true);
expect(result.calls).toHaveLength(1);
const args = result.calls[0]!.args;
expect(args.angle).toBe(90);
expect((args.endDistance as number) / (args.startDistance as number)).toBeCloseTo(4, 9);
expect((args.endDistance as number) - (args.startDistance as number)).toBeLessThan(
MIN_VIABLE_TRAVEL
);
});
it("fails with the scroll-to hint when the target never appears", async () => {
currentTree = () => screen([]);
await writeFlow("pinch-missing", {
executionPrerequisite: "",
steps: [{ kind: "pinch", selector: { text: "Map", loose: true }, scale: 2 }],
});
const result = await run("pinch-missing");
expect(result.ok).toBe(false);
expect(result.steps[0]).toMatchObject({ kind: "pinch", status: "fail" });
expect(result.steps[0].reason).toMatch(/add a scroll-to step/i);
expect(result.calls).toHaveLength(0);
}, 15000);
});
describe("pinch: gating", () => {
it("rejects pinch on vega with the touch-directive message shape", async () => {
await writeFlow("pinch-vega", {
executionPrerequisite: "",
steps: [{ kind: "pinch", scale: 2 }],
});
const result = await run("pinch-vega", "amazon-4a27df03c9777152");
expect(result.steps[0]).toMatchObject({ kind: "pinch", status: "fail" });
expect(result.steps[0].reason).toMatch(
/pinch is a touch directive and Vega is remote-driven — move focus with `tool: tv-remote`/
);
expect(result.calls).toHaveLength(0);
});
it("rejects pinch on chromium for the missing pinch mapping, not a missing backend", async () => {
await writeFlow("pinch-chromium", {
executionPrerequisite: "",
steps: [{ kind: "pinch", scale: 2 }],
});
const result = await run("pinch-chromium", "chromium-cdp-9222");
expect(result.steps[0]).toMatchObject({ kind: "pinch", status: "fail" });
expect(result.steps[0].reason).toMatch(/no uniform pinch-zoom mapping/);
expect(result.steps[0].reason).toMatch(/ctrl\+wheel/);
expect(result.steps[0].reason).not.toMatch(/no backend/i);
expect(result.calls).toHaveLength(0);
});
it("rejects pinch on a physical iOS device without paying the settle or the selector auto-wait", async () => {
// Selector against an empty tree: without the upfront guard this step
// would burn waitForFrame's full auto-wait before failing. Counting tree
// reads proves neither that wait nor a gesture settle ran.
let reads = 0;
currentTree = () => {
reads += 1;
return screen([]);
};
await writeFlow("pinch-ios-device", {
executionPrerequisite: "",
steps: [{ kind: "pinch", selector: { text: "Map", loose: true }, scale: 2 }],
});
const result = await run("pinch-ios-device", "00008110-000978540290401E");
expect(result.steps[0]).toMatchObject({ kind: "pinch", status: "fail" });
expect(result.steps[0].reason).toMatch(/pinch is unsupported on a physical iOS device/);
expect(result.steps[0].reason).toMatch(/no two-finger coordinate API on hardware/);
expect(result.steps[0].reason).toMatch(/run this flow on a simulator/);
expect(result.steps[0].reason).not.toMatch(/simulator-server/);
expect(result.calls).toHaveLength(0);
expect(reads).toBe(0);
});
it("leaves tap on a physical iOS device untouched", async () => {
currentTree = () =>
screen([n({ label: "Zoom in", frame: { x: 0.4, y: 0.4, width: 0.2, height: 0.1 } })]);
await writeFlow("tap-ios-device", {
executionPrerequisite: "",
steps: [{ kind: "tap", selector: { text: "Zoom in", loose: true } }],
});
const result = await run("tap-ios-device", "00008110-000978540290401E");
expect(result.ok).toBe(true);
expect(result.calls.map((c) => c.tool)).toEqual(["gesture-tap"]);
});
it("leaves tap on chromium untouched", async () => {
currentTree = () =>
screen([n({ label: "Zoom in", frame: { x: 0.4, y: 0.4, width: 0.2, height: 0.1 } })]);
await writeFlow("tap-chromium", {
executionPrerequisite: "",
steps: [{ kind: "tap", selector: { text: "Zoom in", loose: true } }],
});
const result = await run("tap-chromium", "chromium-cdp-9222");
expect(result.ok).toBe(true);
expect(result.calls.map((c) => c.tool)).toEqual(["gesture-tap"]);
});
});
describe("pinch: abort", () => {
it("stops the chain mid-decomposition: aborted outcome, no further gestures", async () => {
const controller = new AbortController();
const calls: string[] = [];
const registry = {
invokeTool: vi.fn(async (id: string) => {
calls.push(id);
if (id === "list-devices") return { devices: [] };
// Cancel the run during the FIRST sub-gesture: the inter-gesture gap
// must then swallow the remaining two.
if (id === "gesture-pinch") controller.abort();
return { ok: true };
}),
getTool: vi.fn(() => ({ inputSchema: { properties: { udid: {} } } })),
} as unknown as Registry;
await writeFlow("pinch-abort", {
executionPrerequisite: "",
steps: [{ kind: "pinch", scale: 20 }],
});
const result = asRun(
await createRunFlowTool(registry).execute(
{},
{ name: "pinch-abort", project_root: tmpDir, device: DEVICE },
{ signal: controller.signal } as never
)
);
expect(result.steps.map((s) => `${s.kind}:${s.status}`)).toEqual(["pinch:skip"]);
expect(result.steps[0].reason).toBe("run aborted");
expect(calls.filter((c) => c === "gesture-pinch")).toHaveLength(1);
});
});