Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions packages/capture-kit/src/ios-snapshot-engine/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
deriveIosCaptureHint,
} from '@agent-device/capture-kit/ios-snapshot-planning';
import { toIosSnapshotEngineErrorDetails } from './types.ts';
import { validateIosPayload } from './invariants.ts';
import {
IosSnapshotEngineError,
buildIosInteractiveSnapshotPresentation,
Expand Down Expand Up @@ -196,6 +197,7 @@ test('engine error details preserve typed public context', () => {
field: 'viewport',
index: 4,
parentIndex: 2,
indexTree: 'acquired',
frame: { x: 0, y: 0, width: 10, height: 10 },
clip: { x: 1, y: 2, width: 3, height: 4 },
projection: 'regular',
Expand All @@ -206,12 +208,128 @@ test('engine error details preserve typed public context', () => {
field: 'viewport',
index: 4,
parentIndex: 2,
indexTree: 'acquired',
frame: { x: 0, y: 0, width: 10, height: 10 },
clip: { x: 1, y: 2, width: 3, height: 4 },
projection: 'regular',
});
});

// #4701: one capture is numbered several times over — the raw acquisition, the tree the Apple runner
// published to the host, and the tree the host publishes after its own compaction — and every
// renumbering shifts a node. A refusal that reported a bare `index` asked the reader to guess which
// tree to open, while the recovery advice in the same message family points at a different tree than
// the refusal was counted in. Every refusal that names a node now names its index space.
test('a payload refusal names the runner payload as its index space, for either projection', () => {
for (const interactiveOnly of [false, true]) {
const request = createIosSnapshotRequest({ interactiveOnly });
const payload = escapedRowNodes();

assert.throws(
() => presentIosSnapshot(runnerPresentedInput(request, payload), request),
(error: unknown) => {
assert.ok(error instanceof IosSnapshotEngineError);
assert.equal(error.reason, 'regular-node-outside-cumulative-clip');
// The number is the payload's own: the escaping node sits at that index in the tree the
// producer published, which is the tree this refusal was counted in.
assert.equal(error.details.index, 3);
assert.equal(payload[error.details.index!]?.label, 'Escaping row');
assert.equal(error.details.indexTree, 'runner-payload');
return true;
},
);
}
});

test('a malformed acquired graph names the acquired tree the caller can list', () => {
const request = createIosSnapshotRequest();
const malformed = acquisition(request, [
node(0, 'Application', 'App', viewport),
{ ...node(1, 'Button', 'Broken', { x: 1, y: 1, width: 10, height: 10 }, 0), parentIndex: 99 },
]);

assert.throws(
() => publishIosSnapshot({ stage: 'acquired', acquisition: malformed }, request),
(error: unknown) => {
assert.ok(error instanceof IosSnapshotEngineError);
assert.equal(error.reason, 'malformed-graph');
// An unscoped `snapshot --raw` lists this very tree, so naming it makes both numbers usable.
assert.equal(error.details.indexTree, 'acquired');
assert.equal(error.details.index, 1);
assert.equal(error.details.parentIndex, 99);
return true;
},
);
});

test('each payload check reports an index that resolves inside the tree it names', () => {
// Every engine call site hands the check the tree it validated: `acquired` for the raw acquisition,
// `presented` for the tree about to be published, and the two runner payloads. Whichever it is, the
// reported index resolves to the refusing node in the named tree (#4701).
for (const indexTree of [
'acquired',
'presented',
'runner-payload',
'runner-quality-payload',
] as const) {
const nodes = escapedRowNodes();
assert.throws(
() => validateIosPayload(nodes, indexTree, 'regular', viewport, 'cursor-projected'),
(error: unknown) => {
assert.ok(error instanceof IosSnapshotEngineError);
assert.equal(error.details.indexTree, indexTree);
assert.equal(nodes[error.details.index!]?.label, 'Escaping row');
return true;
},
);
}
});

test('a quality-payload refusal names the quality payload as its index space', () => {
const request = createIosSnapshotRequest();
const input: IosSnapshotInput = {
stage: 'presented',
presentation: {
producer: 'apple-runner',
intent: 'full',
payload: { nodes: ordinaryTreeNodes(), truncated: false },
qualityPayload: { nodes: escapedRowNodes(), truncated: false, scope: null },
},
validation: validationFacts(request),
};

// The quality payload is reindexed and listed by no projection, so the reason alone left the
// number pointing at a tree the reader could not name.
assert.throws(
() => presentIosSnapshot(input, request),
(error: unknown) => {
assert.ok(error instanceof IosSnapshotEngineError);
assert.equal(error.reason, 'invalid-quality-payload');
assert.equal(error.details.index, 3);
assert.equal(error.details.indexTree, 'runner-quality-payload');
return true;
},
);
});

test('a refusal that names no node carries no index space', () => {
const request = createIosSnapshotRequest();
const missingViewport: IosSnapshotAcquisition = {
...acquisition(request, nestedNodes()),
viewport: { kind: 'missing', reason: 'not-provided' },
};

assert.throws(
() => publishIosSnapshot({ stage: 'acquired', acquisition: missingViewport }, request),
(error: unknown) => {
assert.ok(error instanceof IosSnapshotEngineError);
assert.equal(error.reason, 'missing-viewport');
assert.equal('indexTree' in error.details, false);
return true;
},
);
});

test('viewport evidence prefers reported application and window roots', () => {
assert.deepEqual(
resolveIosViewportEvidenceFromRoots([
Expand Down Expand Up @@ -472,6 +590,21 @@ function acquiredInput(request: IosSnapshotRequest, nodes: RawSnapshotNode[]): I
return { stage: 'acquired', acquisition: acquisition(request, nodes) };
}

function runnerPresentedInput(
request: IosSnapshotRequest,
nodes: RawSnapshotNode[],
): IosSnapshotInput {
return {
stage: 'presented',
presentation: {
producer: 'apple-runner',
intent: 'full',
payload: { nodes, truncated: false },
},
validation: validationFacts(request),
};
}

function validationFacts(request: IosSnapshotRequest): IosSnapshotValidationFacts {
return {
presentationKey: buildIosSnapshotPresentationKey(request),
Expand All @@ -492,6 +625,16 @@ function nestedNodes(): RawSnapshotNode[] {
];
}

/** A list whose last actionable row is inside the viewport but outside the list's clip (#4701). */
function escapedRowNodes(): RawSnapshotNode[] {
return [
node(0, 'Application', 'App', viewport),
node(1, 'ScrollView', 'List', { x: 0, y: 40, width: 320, height: 160 }, 0, 1),
node(2, 'Button', 'First row', { x: 8, y: 48, width: 304, height: 32 }, 1, 2),
node(3, 'Button', 'Escaping row', { x: 8, y: 200, width: 304, height: 32 }, 1, 2),
];
}

/** A UIKit-shaped tree over which a presentation is up: a second container carrying a dimming view. */
function presentedTreeNodes(): RawSnapshotNode[] {
return [
Expand Down
3 changes: 2 additions & 1 deletion packages/capture-kit/src/ios-snapshot-engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function presentAcquiredSnapshot(
assertCaptureHintMatches(acquisition, expectedHint);

if (request.projection === 'raw') {
validateIosSnapshotGraph(acquisition.nodes);
validateIosSnapshotGraph(acquisition.nodes, 'acquired');
const sourceNodes = acquisition.nodes.map((raw) => ({ raw, sourceIndex: raw.index }));
const projected = projectIosSnapshot({
nodes: sourceNodes,
Expand Down Expand Up @@ -152,6 +152,7 @@ function presentAcquiredSnapshot(
};
const validation = validateIosPayload(
compacted.nodes,
'presented',
'regular',
viewport,
foldPolicy,
Expand Down
3 changes: 2 additions & 1 deletion packages/capture-kit/src/ios-snapshot-engine/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function foldIosSnapshot(
stats: IosSnapshotPresentationStats;
qualityNotes?: SnapshotQualityNote[];
} {
validateIosSnapshotGraph(nodes);
// The fold interprets one acquisition's tree, so a refusal it raises counts in that tree.
validateIosSnapshotGraph(nodes, 'acquired');
const modalContained = collectModalContainedIndexes(nodes);
const presentable = nodes.filter((node) => !modalContained.has(node.index));
const hasChildren = buildChildPresence(presentable);
Expand Down
16 changes: 12 additions & 4 deletions packages/capture-kit/src/ios-snapshot-engine/graph.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import type { RawSnapshotNode } from '@agent-device/kernel/snapshot';
import { IosSnapshotEngineError } from './types.ts';
import type { IosSnapshotIndexTree } from './types.ts';

export function validateIosSnapshotGraph(nodes: readonly RawSnapshotNode[]): void {
/**
* @param indexTree the tree `nodes` is numbered in, so a refusal naming a node states which of a
* capture's several numberings its `index` counts in (#4701).
*/
export function validateIosSnapshotGraph(
nodes: readonly RawSnapshotNode[],
indexTree: IosSnapshotIndexTree,
): void {
const positions = new Map<number, number>();
for (const [position, node] of nodes.entries()) {
if (!Number.isInteger(node.index) || node.index < 0 || positions.has(node.index)) {
throw new IosSnapshotEngineError(
'malformed-graph',
'iOS snapshot graph contains a duplicate or invalid node index',
{ index: node.index },
{ index: node.index, indexTree },
);
}
positions.set(node.index, position);
if (node.depth !== undefined && (!Number.isInteger(node.depth) || node.depth < 0)) {
throw new IosSnapshotEngineError(
'malformed-graph',
'iOS snapshot graph contains an invalid node depth',
{ index: node.index, field: 'depth' },
{ index: node.index, indexTree, field: 'depth' },
);
}
}
Expand All @@ -28,7 +36,7 @@ export function validateIosSnapshotGraph(nodes: readonly RawSnapshotNode[]): voi
throw new IosSnapshotEngineError(
'malformed-graph',
'iOS snapshot graph parent must precede its child',
{ index: node.index, parentIndex: node.parentIndex },
{ index: node.index, indexTree, parentIndex: node.parentIndex },
);
}
}
Expand Down
Loading
Loading