Skip to content

Commit 7c4c4f0

Browse files
committed
test(snapshot): pin viewport publication across runtime, state, digest, client (#3182)
Runtime command publishes what its producer measured and omits what it didn't; the session state carries the box so find-path consumers read the same one; the digest survives the tree collapse; serialization emits it once; the client re-reads it through the kernel guard so a broken payload stays absent.
1 parent 024a3a8 commit 7c4c4f0

6 files changed

Lines changed: 144 additions & 1 deletion

File tree

‎packages/platform-apple/src/__tests__/interactor-snapshot-viewport.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ test('an Apple runner capture publishes the app-window box its rects are measure
1111
const interactor = createAppleInteractor(
1212
IOS_SIMULATOR,
1313
{},
14-
{ hasLiveSession: () => true, runCommand: async () => runnerResultFor({ command: 'snapshot' }) },
14+
{
15+
hasLiveSession: () => true,
16+
runCommand: async () => runnerResultFor({ command: 'snapshot' }),
17+
},
1518
);
1619

1720
const result = await interactor.snapshot();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { test } from 'vitest';
2+
import assert from 'node:assert/strict';
3+
import { createAgentDeviceClient } from '../agent-device-client.ts';
4+
import { createTransport } from './client-transport-fixture.ts';
5+
6+
// The box a capture's rects are measured in reaches Node.js callers as one optional field on the
7+
// snapshot result (#3182). These cases own the client half of that wire: the pair survives verbatim,
8+
// and every payload the kernel guard refuses becomes the absence meaning "the producer measured no
9+
// box" — never the zero the broken producer wrote.
10+
11+
function clientAnswering(data: Record<string, unknown>) {
12+
const setup = createTransport(async () => ({ ok: true, data: { nodes: [], ...data } }));
13+
return createAgentDeviceClient(setup.config, { transport: setup.transport });
14+
}
15+
16+
test('client capture.snapshot preserves the viewport the producer published (#3182)', async () => {
17+
const client = clientAnswering({ truncated: false, viewport: { width: 1080, height: 2400 } });
18+
19+
assert.deepEqual((await client.capture.snapshot()).viewport, { width: 1080, height: 2400 });
20+
});
21+
22+
test('client capture.snapshot reports no viewport field for a capture that carried none (#3182)', async () => {
23+
const client = clientAnswering({ truncated: false });
24+
25+
assert.equal('viewport' in (await client.capture.snapshot()), false);
26+
});
27+
28+
test('client capture.snapshot restates an unusable viewport payload as absence (#3182)', async () => {
29+
for (const viewport of [
30+
'screen',
31+
[],
32+
{},
33+
{ width: 1080 },
34+
{ width: '1080', height: 2400 },
35+
// A zero is the claim a producer may never make; the client refuses to hand it on as a size.
36+
{ width: 0, height: 2400 },
37+
{ width: 1080, height: 0 },
38+
{ width: Number.NaN, height: 2400 },
39+
{
40+
width: Number.MAX_VALUE,
41+
height: Number.MAX_VALUE,
42+
x: -Number.MAX_VALUE / 2,
43+
y: -Number.MAX_VALUE / 2,
44+
},
45+
]) {
46+
const client = clientAnswering({ truncated: false, viewport });
47+
const result = await client.capture.snapshot();
48+
49+
assert.equal(result.viewport, undefined, `payload ${JSON.stringify(viewport)}`);
50+
assert.equal('viewport' in result, false, `payload ${JSON.stringify(viewport)}`);
51+
}
52+
});

‎src/commands/capture/runtime/snapshot.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,3 +807,30 @@ test('runtime snapshot warns when its tree was read on a surface still moving af
807807

808808
assert.deepEqual(result.warnings, [formatPostGestureOutcomeWarning(outcome)]);
809809
});
810+
811+
// The box a capture's rects are measured in is public output (#3182) for the same reason the
812+
// keyboard band is: `snapshot --json` is how a caller learns the surface to scale against, and a
813+
// producer that measured no box has to leave the key off rather than claim a screen it never read.
814+
815+
test('runtime snapshot publishes the viewport its producer measured', async () => {
816+
const device = createSnapshotOnlyDevice({
817+
nodes: [{ ref: 'e1', index: 0, depth: 0, type: 'Window', label: 'Home' }],
818+
backend: 'android',
819+
viewport: { width: 1080, height: 2400 },
820+
});
821+
822+
const result = await device.capture.snapshot({ session: 'default' });
823+
824+
assert.deepEqual(result.viewport, { width: 1080, height: 2400 });
825+
});
826+
827+
test('runtime snapshot leaves the viewport off when the producer measured no box', async () => {
828+
const device = createSnapshotOnlyDevice({
829+
nodes: [{ ref: 'e1', index: 0, depth: 0, type: 'Window', label: 'Home' }],
830+
backend: 'android',
831+
});
832+
833+
const result = await device.capture.snapshot({ session: 'default' });
834+
835+
assert.equal('viewport' in result, false);
836+
});

‎src/commands/output/snapshot-serialization.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,26 @@ test('serializeSnapshotResult includes snapshot diagnostics', () => {
9494
snapshotDiagnostics,
9595
});
9696
});
97+
98+
// #3182: the viewport rides the response once, beside the tree. An absent one stays absent rather
99+
// than serializing as an empty box.
100+
test('serializeSnapshotResult publishes the viewport beside the tree (#3182)', () => {
101+
const data = serializeSnapshotResult({
102+
nodes: [],
103+
truncated: false,
104+
viewport: { width: 390, height: 844 },
105+
identifiers: { session: 'qa' },
106+
});
107+
108+
assert.deepEqual(data.viewport, { width: 390, height: 844 });
109+
});
110+
111+
test('serializeSnapshotResult omits an absent viewport instead of minting one (#3182)', () => {
112+
const data = serializeSnapshotResult({
113+
nodes: [],
114+
truncated: false,
115+
identifiers: { session: 'qa' },
116+
});
117+
118+
assert.equal('viewport' in data, false);
119+
});

‎src/core/__tests__/snapshot-state.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,26 @@ test('buildSnapshotState leaves an unmeasured keyboard unclaimed instead of abse
647647

648648
expect('keyboard' in state).toBe(false);
649649
});
650+
651+
// #3182: the stored tree keeps the box the producer measured, so a consumer of the session snapshot —
652+
// the find path, a later diff — reads the same box the response published instead of inferring one
653+
// from the largest rect still on screen. A capture with no box leaves it absent rather than minting one.
654+
test('buildSnapshotState carries the producer viewport into the stored state (#3182)', () => {
655+
const state = buildSnapshotState(
656+
{
657+
nodes: [{ index: 0, type: 'Application' }],
658+
backend: 'android',
659+
producer: 'android-uiautomator',
660+
viewport: { width: 1080, height: 2400 },
661+
},
662+
undefined,
663+
);
664+
665+
expect(state.viewport).toEqual({ width: 1080, height: 2400 });
666+
});
667+
668+
test('buildSnapshotState leaves an unmeasured viewport absent (#3182)', () => {
669+
const state = buildSnapshotState({ nodes: [], backend: 'android' }, undefined);
670+
671+
expect('viewport' in state).toBe(false);
672+
});

‎src/daemon/__tests__/response-views.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ test('digest carries the foreground repair of the tree it collapsed', () => {
5656
expect(digest.targetActivation).toEqual(repair);
5757
});
5858

59+
/**
60+
* A digest drops the tree, so the box its rects were measured in (#3182) has to survive the
61+
* collapse — otherwise an agent reading the digest has to infer the screen from nothing again.
62+
*/
63+
test('digest carries the viewport of the tree it collapsed (#3182)', () => {
64+
const digest = snapshotView!(
65+
{ ...SNAPSHOT_DATA, viewport: { width: 390, height: 844 } },
66+
'digest',
67+
);
68+
expect(digest.viewport).toEqual({ width: 390, height: 844 });
69+
70+
const withoutViewport = snapshotView!(SNAPSHOT_DATA, 'digest');
71+
expect('viewport' in withoutViewport).toBe(false);
72+
});
73+
5974
test('digest tolerates missing/empty node trees', () => {
6075
const digest = snapshotView!({ truncated: true }, 'digest');
6176
expect(digest).toMatchObject({ nodeCount: 0, refs: [], truncated: true });

0 commit comments

Comments
 (0)