Skip to content

Commit 8297839

Browse files
committed
test(snapshot): prove each Android/viewport producer claim (#3182)
Kernel: the construction guard and the wire re-read refuse zero, non-finite, and the infinite sentinel as absence. Android: both helper transports read the display pair, snapshotAndroid publishes the display beside an empty tree, the publication adapter carries it, and the backend metadata keeps no second copy.
1 parent e9fe901 commit 8297839

5 files changed

Lines changed: 288 additions & 2 deletions

File tree

‎packages/kernel/src/rect.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
isPositiveFiniteRect,
88
isRectVisibleInViewport,
99
pickLargestRect,
10+
readSnapshotViewportSize,
11+
snapshotViewportSizeFrom,
1012
} from './rect.ts';
1113

1214
const VIEWPORT: Rect = { x: 0, y: 0, width: 300, height: 500 };
@@ -60,6 +62,57 @@ test('the sentinel would survive any check that only looks at components and ext
6062
);
6163
});
6264

65+
// #3182: the viewport a response publishes has exactly one construction path and exactly one wire
66+
// re-read, and both answer a box they cannot accept with absence. A zero the producer answered with
67+
// has to become "unknown", never a claim that the screen has no width.
68+
test('snapshotViewportSizeFrom publishes only a box the rect guard accepts (#3182)', () => {
69+
assert.deepEqual(snapshotViewportSizeFrom({ x: 12, y: -40, width: 390, height: 844 }), {
70+
width: 390,
71+
height: 844,
72+
});
73+
assert.equal(snapshotViewportSizeFrom(undefined), undefined, 'a producer that read nothing');
74+
assert.equal(
75+
snapshotViewportSizeFrom({ x: 0, y: 0, width: 0, height: 844 }),
76+
undefined,
77+
'a zero width is unknown, never a screen of no size',
78+
);
79+
assert.equal(
80+
snapshotViewportSizeFrom({ x: 0, y: 0, width: Number.NaN, height: 844 }),
81+
undefined,
82+
'a non-finite extent',
83+
);
84+
assert.equal(snapshotViewportSizeFrom(CG_RECT_INFINITE), undefined, 'the failed-read sentinel');
85+
});
86+
87+
test('readSnapshotViewportSize accepts only a guard-approved pair from a wire payload (#3182)', () => {
88+
assert.deepEqual(readSnapshotViewportSize({ width: 1080, height: 2400 }), {
89+
width: 1080,
90+
height: 2400,
91+
});
92+
for (const unusable of [
93+
undefined,
94+
null,
95+
'screen',
96+
[],
97+
{},
98+
{ width: 1080 },
99+
{ width: '1080', height: 2400 },
100+
{ width: 0, height: 2400 },
101+
{ width: 1080, height: -1 },
102+
{ width: Number.NaN, height: 2400 },
103+
]) {
104+
assert.equal(readSnapshotViewportSize(unusable), undefined, String(unusable));
105+
}
106+
// A producer shipping the failed-read box whole ships the sentinel origin beside maximal extents;
107+
// an origin on the wire is inspected, not flattened to (0, 0).
108+
assert.equal(
109+
readSnapshotViewportSize({ ...CG_RECT_INFINITE }),
110+
undefined,
111+
'the infinite sentinel with its origin',
112+
);
113+
assert.equal(readSnapshotViewportSize({ width: 1, height: 1, x: '0' }), undefined);
114+
});
115+
63116
test('containsPoint is inclusive on every edge and requires all four bounds', () => {
64117
assert.equal(containsPoint(VIEWPORT, 0, 0), true);
65118
assert.equal(containsPoint(VIEWPORT, 300, 500), true);

‎packages/kernel/src/rect.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,20 @@ export function snapshotViewportSizeFrom(box: Rect | undefined): SnapshotViewpor
5757
* `{ width, height }` pair the guard accepts survives, and anything else is the absence that means
5858
* unknown. A reader that trusted the payload could otherwise hand a consumer the `width: 0` a broken
5959
* producer wrote, which is the claim {@link snapshotViewportSizeFrom} exists to make unrepresentable.
60+
*
61+
* The published shape carries no origin, but a producer shipping the failed-read box it still holds
62+
* would ship the `CGRectInfinite` origin beside the maximal extents; when an `x`/`y` is present the
63+
* guard sees it, so the sentinel #2891 exists for is refused here too rather than being flattened
64+
* into the largest finite box on the wire.
6065
*/
6166
export function readSnapshotViewportSize(value: unknown): SnapshotViewportSize | undefined {
6267
if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined;
63-
const { width, height } = value as Record<string, unknown>;
68+
const { x, y, width, height } = value as Record<string, unknown>;
6469
if (typeof width !== 'number' || typeof height !== 'number') return undefined;
65-
return isPositiveFiniteRect({ x: 0, y: 0, width, height }) ? { width, height } : undefined;
70+
if (x !== undefined && typeof x !== 'number') return undefined;
71+
if (y !== undefined && typeof y !== 'number') return undefined;
72+
const box = { x: x ?? 0, y: y ?? 0, width, height };
73+
return isPositiveFiniteRect(box) ? { width, height } : undefined;
6674
}
6775

6876
export function rectContains(container: Rect, nested: Rect): boolean {
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import assert from 'node:assert/strict';
2+
import { afterEach, beforeEach, test } from 'vitest';
3+
import './test-utils/android-host-test-setup.ts';
4+
import {
5+
androidSnapshotPublicationInput,
6+
androidSnapshotViewportFromHelperMetadata,
7+
} from '../snapshot-capture.ts';
8+
import { snapshotAndroid } from '../snapshot.ts';
9+
import { resetAndroidSnapshotHelperInstallCache } from '../snapshot-helper-install.ts';
10+
import { resetAndroidSnapshotHelperSessions } from '../snapshot-helper-session-lifecycle.ts';
11+
import type { AndroidAdbExecutor } from '../snapshot-helper.ts';
12+
import {
13+
androidSnapshotQualityDevice as device,
14+
androidSnapshotQualityHelperArtifact as helperArtifact,
15+
} from './snapshot-quality-fixtures.ts';
16+
17+
const SCREEN_XML = '<hierarchy><node text="row" bounds="[0,0][1080,200]" /></hierarchy>';
18+
19+
beforeEach(async () => {
20+
await resetAndroidSnapshotHelperSessions();
21+
resetAndroidSnapshotHelperInstallCache();
22+
});
23+
24+
afterEach(async () => {
25+
await resetAndroidSnapshotHelperSessions();
26+
});
27+
28+
function helperAdbServing(display: { width?: number; height?: number } = {}): AndroidAdbExecutor {
29+
const displayKeys =
30+
display.width !== undefined && display.height !== undefined
31+
? [
32+
`INSTRUMENTATION_RESULT: displayWidth=${display.width}`,
33+
`INSTRUMENTATION_RESULT: displayHeight=${display.height}`,
34+
]
35+
: [];
36+
const stdout = [
37+
'INSTRUMENTATION_STATUS: agentDeviceProtocol=android-snapshot-helper-v1',
38+
'INSTRUMENTATION_STATUS: helperApiVersion=1',
39+
'INSTRUMENTATION_STATUS: outputFormat=uiautomator-xml',
40+
'INSTRUMENTATION_STATUS: chunkIndex=0',
41+
'INSTRUMENTATION_STATUS: chunkCount=1',
42+
`INSTRUMENTATION_STATUS: payloadBase64=${Buffer.from(SCREEN_XML, 'utf8').toString('base64')}`,
43+
'INSTRUMENTATION_STATUS_CODE: 1',
44+
'INSTRUMENTATION_RESULT: agentDeviceProtocol=android-snapshot-helper-v1',
45+
'INSTRUMENTATION_RESULT: helperApiVersion=1',
46+
'INSTRUMENTATION_RESULT: ok=true',
47+
'INSTRUMENTATION_RESULT: outputFormat=uiautomator-xml',
48+
'INSTRUMENTATION_RESULT: waitForIdleTimeoutMs=0',
49+
'INSTRUMENTATION_RESULT: timeoutMs=8000',
50+
'INSTRUMENTATION_RESULT: maxDepth=128',
51+
'INSTRUMENTATION_RESULT: maxNodes=5000',
52+
'INSTRUMENTATION_RESULT: rootPresent=true',
53+
'INSTRUMENTATION_RESULT: captureMode=interactive-windows',
54+
'INSTRUMENTATION_RESULT: windowCount=1',
55+
'INSTRUMENTATION_RESULT: nodeCount=1',
56+
'INSTRUMENTATION_RESULT: truncated=false',
57+
'INSTRUMENTATION_RESULT: elapsedMs=12',
58+
'INSTRUMENTATION_RESULT: pixelDensity=2.625',
59+
...displayKeys,
60+
'INSTRUMENTATION_CODE: 0',
61+
].join('\n');
62+
return async (args) => {
63+
if (args.includes('--show-versioncode')) {
64+
return {
65+
exitCode: 0,
66+
stdout: 'package:com.callstack.agentdevice.snapshothelper versionCode:13004',
67+
stderr: '',
68+
};
69+
}
70+
if (args.includes('instrument')) {
71+
return { exitCode: 0, stdout, stderr: '' };
72+
}
73+
throw new Error(`unexpected helper adb args: ${args.join(' ')}`);
74+
};
75+
}
76+
77+
// #3182: the published viewport comes from the display the helper measured, not from the tree, so a
78+
// capture with content and a capture of an empty screen answer the same question.
79+
test('snapshotAndroid publishes the display the helper measured (#3182)', async () => {
80+
const capture = await snapshotAndroid(device, {
81+
helperAdb: helperAdbServing({ width: 1080, height: 2400 }),
82+
helperArtifact,
83+
});
84+
85+
assert.deepEqual(capture.viewport, { width: 1080, height: 2400 });
86+
// The response publishes the fact once. The raw display pair stays on the helper transport and the
87+
// backend metadata the response carries, so a consumer cannot read two copies of it.
88+
assert.equal(
89+
'displayWidth' in (capture.androidSnapshot as Record<string, unknown>),
90+
false,
91+
'the backend metadata carries no second copy of the display read',
92+
);
93+
});
94+
95+
test('the Android viewport survives the publication adapter into the daemon capture (#3182)', async () => {
96+
const capture = await snapshotAndroid(device, {
97+
helperAdb: helperAdbServing({ width: 1080, height: 2400 }),
98+
helperArtifact,
99+
});
100+
101+
assert.deepEqual(androidSnapshotPublicationInput(capture).viewport, {
102+
width: 1080,
103+
height: 2400,
104+
});
105+
});
106+
107+
test('a helper with no display read leaves the Android viewport absent, never zero (#3182)', async () => {
108+
const capture = await snapshotAndroid(device, {
109+
helperAdb: helperAdbServing(),
110+
helperArtifact,
111+
});
112+
113+
assert.equal(capture.viewport, undefined);
114+
assert.equal('viewport' in capture, false, 'absent is not the same as an empty box');
115+
});
116+
117+
// The consequence the display read buys: the box is a property of the device, so the capture that
118+
// reports nothing about the tree can still report the screen its (absent) bounds belonged to.
119+
test('a presentation-failed Android capture still publishes the display it read (#3182)', async () => {
120+
const capture = await snapshotAndroid(device, {
121+
helperAdb: helperAdbServing({ width: 1080, height: 2400 }),
122+
helperArtifact,
123+
androidPresentation: { deadlineAtMs: 100, now: () => 100 },
124+
});
125+
126+
assert.deepEqual(capture.nodes, [], 'the projection was discarded');
127+
assert.deepEqual(capture.viewport, { width: 1080, height: 2400 });
128+
});
129+
130+
test('androidSnapshotViewportFromHelperMetadata refuses an unusable display read (#3182)', () => {
131+
assert.deepEqual(
132+
androidSnapshotViewportFromHelperMetadata({
133+
outputFormat: 'uiautomator-xml',
134+
displayWidth: 1080,
135+
displayHeight: 2400,
136+
}),
137+
{ width: 1080, height: 2400 },
138+
);
139+
for (const unusable of [
140+
{},
141+
{ displayWidth: 0, displayHeight: 2400 },
142+
{ displayWidth: 1080, displayHeight: 0 },
143+
{ displayWidth: -1, displayHeight: 2400 },
144+
{ displayWidth: Number.NaN, displayHeight: 2400 },
145+
{ displayWidth: Number.POSITIVE_INFINITY, displayHeight: 2400 },
146+
]) {
147+
assert.equal(
148+
androidSnapshotViewportFromHelperMetadata({ outputFormat: 'uiautomator-xml', ...unusable }),
149+
undefined,
150+
JSON.stringify(unusable),
151+
);
152+
}
153+
});

‎packages/platform-android/src/__tests__/snapshot-helper-session-protocol.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,38 @@ test('parses the session envelope and snapshot metadata', () => {
3737
rootPresent: undefined,
3838
truncated: undefined,
3939
elapsedMs: undefined,
40+
displayWidth: undefined,
41+
displayHeight: undefined,
4042
},
4143
});
4244
});
4345

46+
// The session transport reads the display pair exactly like the one-shot transport does (#3182),
47+
// and a header that never arrived stays an absence rather than becoming a zero.
48+
test('parses the session display extent beside its density and absent headers stay absent (#3182)', () => {
49+
const xml = '<hierarchy><node text="catalog" /></hierarchy>';
50+
const withDisplay = sessionResponse({
51+
requestId: 'snapshot-1',
52+
xml,
53+
metadata: { pixelDensity: '2.625', displayWidth: '1080', displayHeight: '2400' },
54+
});
55+
56+
const parsed = parseAndroidSnapshotHelperSessionSnapshotResponse(withDisplay, 'snapshot-1');
57+
58+
assert.equal(parsed.metadata.pixelDensity, 2.625);
59+
assert.equal(parsed.metadata.displayWidth, 1080);
60+
assert.equal(parsed.metadata.displayHeight, 2400);
61+
62+
const withoutDisplay = sessionResponse({
63+
requestId: 'snapshot-1',
64+
xml,
65+
metadata: { pixelDensity: '2.625' },
66+
});
67+
const sparse = parseAndroidSnapshotHelperSessionSnapshotResponse(withoutDisplay, 'snapshot-1');
68+
assert.equal(sparse.metadata.displayWidth, undefined);
69+
assert.equal(sparse.metadata.displayHeight, undefined);
70+
});
71+
4472
test('rejects stale and truncated session snapshot responses', () => {
4573
const response = sessionResponse({
4674
requestId: 'snapshot-old',

‎packages/platform-android/src/__tests__/snapshot-helper.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,54 @@ test('parseAndroidSnapshotHelperOutput reconstructs XML chunks and metadata', ()
8282
truncated: false,
8383
elapsedMs: 42,
8484
pixelDensity: undefined,
85+
displayWidth: undefined,
86+
displayHeight: undefined,
8587
transport: 'instrumentation',
8688
});
8789
});
8890

91+
// The helper publishes the display's extent beside its density (#3182). Both transports read the
92+
// same pair, and a helper that answered with nothing usable must leave the keys absent rather than
93+
// carry a zero the host would have to second-guess.
94+
test('readHelperMetadata carries the helper display extent beside its density (#3182)', () => {
95+
const xml = '<hierarchy><node text="row" /></hierarchy>';
96+
const output = helperOutput({
97+
chunks: [xml],
98+
result: {
99+
ok: 'true',
100+
helperApiVersion: '1',
101+
outputFormat: 'uiautomator-xml',
102+
pixelDensity: '2.625',
103+
displayWidth: '1080',
104+
displayHeight: '2400',
105+
},
106+
});
107+
108+
const parsed = parseAndroidSnapshotHelperOutput(output);
109+
110+
assert.equal(parsed.metadata.pixelDensity, 2.625);
111+
assert.equal(parsed.metadata.displayWidth, 1080);
112+
assert.equal(parsed.metadata.displayHeight, 2400);
113+
});
114+
115+
test('readHelperMetadata leaves the display extent absent when the helper omits it (#3182)', () => {
116+
const xml = '<hierarchy><node text="row" /></hierarchy>';
117+
const output = helperOutput({
118+
chunks: [xml],
119+
result: {
120+
ok: 'true',
121+
helperApiVersion: '1',
122+
outputFormat: 'uiautomator-xml',
123+
pixelDensity: '2.625',
124+
},
125+
});
126+
127+
const parsed = parseAndroidSnapshotHelperOutput(output);
128+
129+
assert.equal(parsed.metadata.displayWidth, undefined);
130+
assert.equal(parsed.metadata.displayHeight, undefined);
131+
});
132+
89133
test('parseAndroidSnapshotHelperOutput decodes UTF-8 across byte chunk boundaries', () => {
90134
const xml = '<hierarchy><node text="Save 👍" /></hierarchy>';
91135
const bytes = Buffer.from(xml, 'utf8');

0 commit comments

Comments
 (0)