Skip to content

Commit 4e6c73d

Browse files
committed
fix: honor diff snapshot -i flatten mode
1 parent bb9fd12 commit 4e6c73d

3 files changed

Lines changed: 49 additions & 9 deletions

File tree

src/daemon/__tests__/snapshot-diff.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,25 @@ test('buildSnapshotDiff preserves surrounding context ordering', () => {
7575
assert.equal(diff.lines[3]?.kind, 'added');
7676
assert.equal(diff.lines[4]?.kind, 'unchanged');
7777
});
78+
79+
test('buildSnapshotDiff flatten option uses flat snapshot line shape', () => {
80+
const previous = nodes([
81+
{ index: 0, depth: 0, type: 'XCUIElementTypeWindow' },
82+
{ index: 1, depth: 1, type: 'XCUIElementTypeOther', label: '335' },
83+
{ index: 2, depth: 2, type: 'XCUIElementTypeStaticText', label: '335' },
84+
]);
85+
const current = nodes([
86+
{ index: 0, depth: 0, type: 'XCUIElementTypeWindow' },
87+
{ index: 1, depth: 1, type: 'XCUIElementTypeOther', label: '402' },
88+
{ index: 2, depth: 2, type: 'XCUIElementTypeStaticText', label: '402' },
89+
]);
90+
91+
const diff = buildSnapshotDiff(previous, current, { flatten: true });
92+
assert.equal(diff.summary.additions, 2);
93+
assert.equal(diff.summary.removals, 2);
94+
const changed = diff.lines.filter((line) => line.kind !== 'unchanged');
95+
assert.equal(changed.length, 4);
96+
for (const line of changed) {
97+
assert.equal(line.text.startsWith(' '), false);
98+
}
99+
});

src/daemon/handlers/snapshot.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export async function handleSnapshotCommands(params: {
104104
}
105105
const resolvedScope = resolveSnapshotScope(req.flags?.snapshotScope, session);
106106
if (!resolvedScope.ok) return resolvedScope.response;
107+
const flattenForDiff = req.flags?.snapshotInteractiveOnly === true;
107108

108109
return await withSessionlessRunnerCleanup(session, device, async () => {
109110
const appBundleId = session?.appBundleId;
@@ -118,7 +119,7 @@ export async function handleSnapshotCommands(params: {
118119
const currentSnapshot = capture.snapshot;
119120

120121
if (!session?.snapshot) {
121-
const unchanged = countSnapshotComparableLines(currentSnapshot.nodes);
122+
const unchanged = countSnapshotComparableLines(currentSnapshot.nodes, { flatten: flattenForDiff });
122123
const nextSession: SessionState = session
123124
? { ...session, snapshot: currentSnapshot }
124125
: {
@@ -154,7 +155,7 @@ export async function handleSnapshotCommands(params: {
154155
};
155156
}
156157

157-
const diff = buildSnapshotDiff(session.snapshot.nodes, currentSnapshot.nodes);
158+
const diff = buildSnapshotDiff(session.snapshot.nodes, currentSnapshot.nodes, { flatten: flattenForDiff });
158159
const nextSession: SessionState = { ...session, snapshot: currentSnapshot };
159160
recordIfSession(sessionStore, nextSession, req, {
160161
mode: 'snapshot',

src/daemon/snapshot-diff.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { SnapshotNode } from '../utils/snapshot.ts';
2-
import { buildSnapshotDisplayLines, displayLabel, formatRole } from '../utils/snapshot-lines.ts';
2+
import { buildSnapshotDisplayLines, displayLabel, formatRole, formatSnapshotLine } from '../utils/snapshot-lines.ts';
33

44
export type SnapshotDiffLine = {
55
kind: 'added' | 'removed' | 'unchanged';
@@ -17,6 +17,10 @@ export type SnapshotDiffResult = {
1717
lines: SnapshotDiffLine[];
1818
};
1919

20+
export type SnapshotDiffOptions = {
21+
flatten?: boolean;
22+
};
23+
2024
type SnapshotComparableLine = {
2125
text: string;
2226
comparable: string;
@@ -32,9 +36,13 @@ export function snapshotNodeToComparableLine(node: SnapshotNode, depthOverride?:
3236
return [depthPart, role, textPart, enabledPart, selectedPart, hittablePart].join('|');
3337
}
3438

35-
export function buildSnapshotDiff(previousNodes: SnapshotNode[], currentNodes: SnapshotNode[]): SnapshotDiffResult {
36-
const previous = snapshotNodesToLines(previousNodes);
37-
const current = snapshotNodesToLines(currentNodes);
39+
export function buildSnapshotDiff(
40+
previousNodes: SnapshotNode[],
41+
currentNodes: SnapshotNode[],
42+
options: SnapshotDiffOptions = {},
43+
): SnapshotDiffResult {
44+
const previous = snapshotNodesToLines(previousNodes, options);
45+
const current = snapshotNodesToLines(currentNodes, options);
3846
const lines = diffComparableLinesMyers(previous, current);
3947
const summary: SnapshotDiffSummary = { additions: 0, removals: 0, unchanged: 0 };
4048
for (const line of lines) {
@@ -45,11 +53,20 @@ export function buildSnapshotDiff(previousNodes: SnapshotNode[], currentNodes: S
4553
return { summary, lines };
4654
}
4755

48-
export function countSnapshotComparableLines(nodes: SnapshotNode[]): number {
49-
return snapshotNodesToLines(nodes).length;
56+
export function countSnapshotComparableLines(
57+
nodes: SnapshotNode[],
58+
options: SnapshotDiffOptions = {},
59+
): number {
60+
return snapshotNodesToLines(nodes, options).length;
5061
}
5162

52-
function snapshotNodesToLines(nodes: SnapshotNode[]): SnapshotComparableLine[] {
63+
function snapshotNodesToLines(nodes: SnapshotNode[], options: SnapshotDiffOptions): SnapshotComparableLine[] {
64+
if (options.flatten) {
65+
return nodes.map((node) => ({
66+
text: formatSnapshotLine(node, 0, false),
67+
comparable: snapshotNodeToComparableLine(node, 0),
68+
}));
69+
}
5370
return buildSnapshotDisplayLines(nodes).map((line) => ({
5471
text: line.text,
5572
comparable: snapshotNodeToComparableLine(line.node, line.depth),

0 commit comments

Comments
 (0)