-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathoutput.ts
More file actions
539 lines (496 loc) · 20.1 KB
/
output.ts
File metadata and controls
539 lines (496 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import path from 'node:path';
import { AppError, normalizeError, type NormalizedError } from './errors.ts';
import { buildSnapshotDisplayLines, formatSnapshotLine } from './snapshot-lines.ts';
import type { SnapshotNode, SnapshotVisibility } from './snapshot.ts';
import { displayNodeLabel } from './snapshot-tree.ts';
import type { ScreenshotDiffResult } from './screenshot-diff.ts';
import type { ScreenshotDiffRegion } from './screenshot-diff-regions.ts';
import { styleText } from 'node:util';
import { buildMobileSnapshotPresentation } from './mobile-snapshot-semantics.ts';
type JsonResult =
| { success: true; data?: unknown }
| {
success: false;
error: {
code: string;
message: string;
hint?: string;
diagnosticId?: string;
logPath?: string;
details?: Record<string, unknown>;
};
};
export function printJson(result: JsonResult): void {
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
}
export function printHumanError(
err: AppError | NormalizedError,
options: { showDetails?: boolean } = {},
): void {
const normalized = err instanceof AppError ? normalizeError(err) : err;
process.stderr.write(`Error (${normalized.code}): ${normalized.message}\n`);
if (normalized.hint) {
process.stderr.write(`Hint: ${normalized.hint}\n`);
}
if (normalized.diagnosticId) {
process.stderr.write(`Diagnostic ID: ${normalized.diagnosticId}\n`);
}
if (normalized.logPath) {
process.stderr.write(`Diagnostics Log: ${normalized.logPath}\n`);
}
if (options.showDetails && normalized.details) {
process.stderr.write(`${JSON.stringify(normalized.details, null, 2)}\n`);
}
}
type SnapshotDiffLine = {
kind?: 'added' | 'removed' | 'unchanged';
text?: string;
};
export function formatSnapshotText(
data: Record<string, unknown>,
options: { raw?: boolean; flatten?: boolean } = {},
): string {
const rawNodes = data.nodes;
const nodes = Array.isArray(rawNodes) ? (rawNodes as SnapshotNode[]) : [];
const backend = typeof data.backend === 'string' ? data.backend : undefined;
const visiblePresentation =
options.raw || backend === 'macos-helper' ? null : buildMobileSnapshotPresentation(nodes);
const truncated = Boolean(data.truncated);
const appName = typeof data.appName === 'string' ? data.appName : undefined;
const appBundleId = typeof data.appBundleId === 'string' ? data.appBundleId : undefined;
const meta: string[] = [];
if (appName) meta.push(`Page: ${appName}`);
if (appBundleId) meta.push(`App: ${appBundleId}`);
const displayedNodes = visiblePresentation?.nodes ?? nodes;
const visibility =
options.raw || backend === 'macos-helper'
? null
: readSnapshotVisibility(data, visiblePresentation, displayedNodes.length, nodes.length);
const header = visibility?.partial
? visibility.totalNodeCount > visibility.visibleNodeCount
? `Snapshot: ${visibility.visibleNodeCount} visible nodes (${visibility.totalNodeCount} total)${truncated ? ' (truncated)' : ''}`
: `Snapshot: ${visibility.visibleNodeCount} visible nodes${truncated ? ' (truncated)' : ''}`
: `Snapshot: ${nodes.length} nodes${truncated ? ' (truncated)' : ''}`;
const prefix = meta.length > 0 ? `${meta.join('\n')}\n` : '';
const notices = buildSnapshotNotices(data, nodes, options);
const noticesBlock = notices.length > 0 ? `${notices.join('\n')}\n` : '';
if (nodes.length === 0) {
return `${prefix}${header}\n${noticesBlock}`;
}
if (options.raw) {
const rawLines = nodes.map((node) => JSON.stringify(node));
return `${prefix}${header}\n${noticesBlock}${rawLines.join('\n')}\n`;
}
if (options.flatten) {
const flatLines = buildFlattenedSnapshotDisplayLines(displayedNodes);
const summaryBlock =
visiblePresentation && visiblePresentation.summaryLines.length > 0
? `\n${visiblePresentation.summaryLines.join('\n')}`
: '';
return `${prefix}${header}\n${noticesBlock}${flatLines.join('\n')}${summaryBlock}\n`;
}
const lines = renderSnapshotDisplayLines(
buildSnapshotDisplayLines(displayedNodes, { summarizeTextSurfaces: true }),
);
const summaryBlock =
visiblePresentation && visiblePresentation.summaryLines.length > 0
? `\n${visiblePresentation.summaryLines.join('\n')}`
: '';
return `${prefix}${header}\n${noticesBlock}${lines.join('\n')}${summaryBlock}\n`;
}
function readSnapshotVisibility(
data: Record<string, unknown>,
visiblePresentation: ReturnType<typeof buildMobileSnapshotPresentation> | null,
displayedNodeCount: number,
totalNodeCount: number,
): SnapshotVisibility | null {
const candidate = data.visibility;
if (candidate && typeof candidate === 'object') {
const visibility = candidate as Partial<SnapshotVisibility>;
if (
typeof visibility.partial === 'boolean' &&
typeof visibility.visibleNodeCount === 'number' &&
typeof visibility.totalNodeCount === 'number' &&
Array.isArray(visibility.reasons)
) {
return {
partial: visibility.partial,
visibleNodeCount: visibility.visibleNodeCount,
totalNodeCount: visibility.totalNodeCount,
reasons: visibility.reasons.filter(
(reason): reason is SnapshotVisibility['reasons'][number] => typeof reason === 'string',
),
};
}
}
const hiddenCount = visiblePresentation?.hiddenCount ?? 0;
const hasExplicitHiddenContentHints = visiblePresentation
? visiblePresentation.nodes.some((node) => node.hiddenContentAbove || node.hiddenContentBelow)
: false;
if (hiddenCount > 0) {
return {
partial: true,
visibleNodeCount: displayedNodeCount,
totalNodeCount,
reasons: ['offscreen-nodes'],
};
}
if (hasExplicitHiddenContentHints) {
return {
partial: true,
visibleNodeCount: displayedNodeCount,
totalNodeCount: displayedNodeCount,
reasons: [],
};
}
return null;
}
export function formatSnapshotDiffText(data: Record<string, unknown>): string {
const baselineInitialized = data.baselineInitialized === true;
const summaryRaw = (data.summary ?? {}) as Record<string, unknown>;
const additions = toNumber(summaryRaw.additions);
const removals = toNumber(summaryRaw.removals);
const unchanged = toNumber(summaryRaw.unchanged);
const useColor = supportsColor();
const notices = readSnapshotWarnings(data);
const noticesBlock = notices.length > 0 ? `${notices.join('\n')}\n` : '';
if (baselineInitialized) {
return `${noticesBlock}Baseline initialized (${unchanged} lines).\n`;
}
const rawLines = Array.isArray(data.lines) ? (data.lines as SnapshotDiffLine[]) : [];
const contextLines = applyContextWindow(rawLines, 1);
const lines = contextLines.map((line) => {
const text = typeof line.text === 'string' ? line.text : '';
if (line.kind === 'added') {
const prefix = text.startsWith(' ') ? `+${text}` : `+ ${text}`;
return useColor ? colorize(prefix, 'green') : prefix;
}
if (line.kind === 'removed') {
const prefix = text.startsWith(' ') ? `-${text}` : `- ${text}`;
return useColor ? colorize(prefix, 'red') : prefix;
}
return useColor ? colorize(text, 'dim') : text;
});
const body = lines.length > 0 ? `${lines.join('\n')}\n` : '';
if (!useColor) {
return `${noticesBlock}${body}${additions} additions, ${removals} removals, ${unchanged} unchanged\n`;
}
const summary = [
`${colorize(String(additions), 'green')} additions`,
`${colorize(String(removals), 'red')} removals`,
`${colorize(String(unchanged), 'dim')} unchanged`,
].join(', ');
return `${noticesBlock}${body}${summary}\n`;
}
export function formatScreenshotDiffText(data: ScreenshotDiffResult): string {
const useColor = supportsColor();
const match = data.match === true;
const differentPixels = toNumber(data.differentPixels);
const totalPixels = toNumber(data.totalPixels);
const mismatchPercentage = toNumber(data.mismatchPercentage);
const diffPath = data.diffPath;
const dimensionMismatch = data.dimensionMismatch;
const lines: string[] = [];
if (match) {
const indicator = useColor ? colorize('✓', 'green') : '✓';
lines.push(`${indicator} Screenshots match.`);
} else if (dimensionMismatch) {
const indicator = useColor ? colorize('✗', 'red') : '✗';
const expected = dimensionMismatch.expected;
const actual = dimensionMismatch.actual;
lines.push(
`${indicator} Screenshots have different dimensions: ` +
`expected ${expected?.width}x${expected?.height}, ` +
`got ${actual?.width}x${actual?.height}`,
);
} else {
const indicator = useColor ? colorize('✗', 'red') : '✗';
const pctLabel =
mismatchPercentage === 0 && differentPixels > 0 ? '<0.01' : String(mismatchPercentage);
const summary = `${pctLabel}% pixels differ`;
lines.push(`${indicator} ${useColor ? colorize(summary, 'red') : summary}`);
}
if (diffPath && !match) {
const relativePath = toRelativePath(diffPath);
const label = useColor ? colorize('Diff image:', 'dim') : 'Diff image:';
const displayPath = useColor ? colorize(relativePath, 'green') : relativePath;
lines.push(` ${label} ${displayPath}`);
}
if (data.currentOverlayPath && !match) {
const relativePath = toRelativePath(data.currentOverlayPath);
const label = useColor ? colorize('Current overlay:', 'dim') : 'Current overlay:';
const displayPath = useColor ? colorize(relativePath, 'green') : relativePath;
const refCount = toNumber(data.currentOverlayRefCount);
const refSuffix = refCount > 0 ? ` (${refCount} refs)` : '';
lines.push(` ${label} ${displayPath}${refSuffix}`);
}
if (!match && !dimensionMismatch) {
const diffCount = useColor ? colorize(String(differentPixels), 'red') : String(differentPixels);
lines.push(` ${diffCount} different / ${totalPixels} total pixels`);
}
const hints = !match && !dimensionMismatch ? formatScreenshotDiffHints(data) : [];
if (hints.length > 0) {
lines.push(` ${formatMuted('Hints:', useColor)}`);
for (const hint of hints) lines.push(` - ${hint}`);
}
const regions = Array.isArray(data.regions) ? data.regions : [];
if (!match && !dimensionMismatch && regions.length > 0) {
lines.push(` ${formatMuted('Changed regions:', useColor)}`);
for (const region of regions.slice(0, 5)) {
const share =
region.shareOfDiffPercentage === 0 && region.differentPixels > 0
? '<0.01'
: String(region.shareOfDiffPercentage);
const rect = region.rect;
lines.push(
` ${region.index}. ${region.location} x=${rect.x} y=${rect.y} ` +
`${rect.width}x${rect.height}, ${share}% of diff, change=${region.dominantChange}`,
);
const detailLine = formatScreenshotRegionDetails(region);
if (detailLine) {
lines.push(` ${detailLine}`);
}
const bestMatch = region.currentOverlayMatches?.[0];
if (bestMatch) {
const label = bestMatch.label ? ` "${bestMatch.label}"` : '';
lines.push(
` overlaps @${bestMatch.ref}${label}, ` +
`${bestMatch.regionCoveragePercentage}% of region`,
);
}
}
}
const ocrMatches = data.ocr?.matches ?? [];
if (!match && !dimensionMismatch && ocrMatches.length > 0) {
const shownOcrMatches = ocrMatches.slice(0, 8);
lines.push(
` ${formatMuted(
`OCR text deltas (${data.ocr?.provider}; baselineBlocks=${data.ocr?.baselineBlocks} ` +
`currentBlocks=${data.ocr?.currentBlocks}; showing ${shownOcrMatches.length}/${ocrMatches.length}; px):`,
useColor,
)}`,
);
lines.push(
` ${formatMuted(
'item | text | movePx | sizeDeltaPx | bboxBaseline | bboxCurrent | confidence | issueHint',
useColor,
)}`,
);
for (const [index, ocrMatch] of shownOcrMatches.entries()) {
const delta = ocrMatch.delta;
lines.push(
` ${index + 1} | ${JSON.stringify(ocrMatch.text)} | ` +
`${formatSignedPixels(delta.x)},${formatSignedPixels(delta.y)} | ` +
`${formatSignedPixels(delta.width)},${formatSignedPixels(delta.height)} | ` +
`${formatRect(ocrMatch.baselineRect)} | ${formatRect(ocrMatch.currentRect)} | ` +
`${ocrMatch.confidence} | ` +
`${ocrMatch.possibleTextMetricMismatch ? 'ocr-bbox-size-change' : '-'}`,
);
}
}
const nonTextDeltas = data.nonTextDeltas ?? [];
if (!match && !dimensionMismatch && nonTextDeltas.length > 0) {
const shownNonTextDeltas = nonTextDeltas.slice(0, 8);
lines.push(
` ${formatMuted(
`Non-text visual deltas (showing ${shownNonTextDeltas.length}/${nonTextDeltas.length}; px):`,
useColor,
)}`,
);
lines.push(
` ${formatMuted('item | region | slot | kind | bboxCurrent | nearestText', useColor)}`,
);
for (const delta of shownNonTextDeltas) {
lines.push(
` ${delta.index} | ${delta.regionIndex ? `r${delta.regionIndex}` : '-'} | ` +
`${delta.slot} | ${delta.likelyKind} | ${formatRect(delta.rect)} | ` +
`${delta.nearestText ? JSON.stringify(delta.nearestText) : '-'}`,
);
}
}
return `${lines.join('\n')}\n`;
}
function formatRect(rect: { x: number; y: number; width: number; height: number }): string {
return `x=${rect.x},y=${rect.y},w=${rect.width},h=${rect.height}`;
}
function formatSignedPixels(value: number): string {
return value > 0 ? `+${value}` : String(value);
}
function formatScreenshotDiffHints(data: ScreenshotDiffResult): string[] {
const hints: string[] = [];
const clusters = data.ocr?.movementClusters ?? [];
for (const cluster of clusters.slice(0, 2)) {
hints.push(
`text movement cluster: ${formatQuotedList(cluster.texts)} dx=${formatRange(cluster.xRange)}px ` +
`dy=${formatRange(cluster.yRange)}px`,
);
}
const controlDeltas = (data.nonTextDeltas ?? [])
.filter((delta) => ['icon', 'toggle', 'chevron'].includes(delta.likelyKind))
.slice(0, 3);
if (controlDeltas.length > 0) {
hints.push(`non-text controls: ${controlDeltas.map(formatNonTextHint).join('; ')}`);
}
const boundaryDeltas = (data.nonTextDeltas ?? [])
.filter((delta) => delta.likelyKind === 'separator')
.slice(0, 2);
if (boundaryDeltas.length > 0) {
hints.push(`non-text boundaries: ${boundaryDeltas.map(formatNonTextHint).join('; ')}`);
}
return hints.slice(0, 6);
}
function formatNonTextHint(delta: {
likelyKind: string;
nearestText?: string;
regionIndex?: number;
}): string {
const anchor = delta.nearestText ? ` near ${JSON.stringify(delta.nearestText)}` : '';
const region = delta.regionIndex ? ` r${delta.regionIndex}` : '';
return `${delta.likelyKind}${anchor}${region}`;
}
function formatRange(range: { min: number; max: number }): string {
return range.min === range.max
? formatSignedPixels(range.min)
: `${formatSignedPixels(range.min)}..${formatSignedPixels(range.max)}`;
}
function formatQuotedList(values: string[]): string {
const shown = values.slice(0, 4).map((value) => JSON.stringify(value));
const suffix = values.length > shown.length ? ` +${values.length - shown.length} more` : '';
return `${shown.join(', ')}${suffix}`;
}
function formatScreenshotRegionDetails(region: ScreenshotDiffRegion): string | null {
const details = [
region.size ? `size=${region.size}` : null,
region.shape ? `shape=${region.shape}` : null,
typeof region.densityPercentage === 'number' ? `density=${region.densityPercentage}%` : null,
region.averageBaselineColorHex && region.averageCurrentColorHex
? `avgColor=${region.averageBaselineColorHex}->${region.averageCurrentColorHex}`
: null,
typeof region.baselineLuminance === 'number' && typeof region.currentLuminance === 'number'
? `luminance=${region.baselineLuminance}->${region.currentLuminance}`
: null,
].filter((entry): entry is string => entry !== null);
return details.length > 0 ? details.join(' ') : null;
}
function toRelativePath(filePath: string): string {
const cwd = process.cwd();
const relativePath = path.relative(cwd, filePath);
if (relativePath === '' || (!relativePath.startsWith('..') && !path.isAbsolute(relativePath))) {
return relativePath === '' ? '.' : `.${path.sep}${relativePath}`;
}
return filePath;
}
function toNumber(value: unknown): number {
return typeof value === 'number' && Number.isFinite(value) ? value : 0;
}
function applyContextWindow(lines: SnapshotDiffLine[], contextWindow: number): SnapshotDiffLine[] {
if (lines.length === 0) return lines;
const changedIndices = lines
.map((line, index) => ({ index, kind: line.kind }))
.filter((entry) => entry.kind === 'added' || entry.kind === 'removed')
.map((entry) => entry.index);
if (changedIndices.length === 0) return lines;
const keep = new Array<boolean>(lines.length).fill(false);
for (const index of changedIndices) {
const start = Math.max(0, index - contextWindow);
const end = Math.min(lines.length - 1, index + contextWindow);
for (let i = start; i <= end; i += 1) {
keep[i] = true;
}
}
return lines.filter((_, index) => keep[index]);
}
function supportsColor(): boolean {
const forceColor = process.env.FORCE_COLOR;
if (typeof forceColor === 'string') {
return forceColor !== '0';
}
if (typeof process.env.NO_COLOR === 'string') {
return false;
}
return Boolean(process.stdout.isTTY);
}
function colorize(text: string, format: Parameters<typeof styleText>[0]): string {
return styleText(format, text);
}
function formatMuted(text: string, useColor: boolean): string {
return useColor ? colorize(text, 'dim') : text;
}
function buildSnapshotNotices(
data: Record<string, unknown>,
nodes: SnapshotNode[],
options: { raw?: boolean; flatten?: boolean },
): string[] {
const notices = readSnapshotWarnings(data);
if (!options.raw && detectPossibleRepeatedNavSubtree(nodes)) {
notices.push('Warning: possible repeated nav subtree detected.');
}
return notices;
}
function readSnapshotWarnings(data: Record<string, unknown>): string[] {
const rawWarnings = data.warnings;
if (!Array.isArray(rawWarnings)) {
return [];
}
return rawWarnings.filter(
(entry): entry is string => typeof entry === 'string' && entry.length > 0,
);
}
// Detects likely duplicated navigation chrome (e.g. bottom tab bars rendered once
// per tab). Thresholds: ≥20 total nodes to avoid false positives on tiny trees,
// and ≥8 cumulative duplicate-signature nodes to surface the warning.
function detectPossibleRepeatedNavSubtree(nodes: SnapshotNode[]): boolean {
if (nodes.length < 20) {
return false;
}
const counts = new Map<string, number>();
for (const node of nodes) {
const type = (node.type ?? '').toLowerCase();
const label = displayNodeLabel(node).trim().toLowerCase();
if (!label) continue;
const signature = `${type}|${label}`;
counts.set(signature, (counts.get(signature) ?? 0) + 1);
}
let duplicateCount = 0;
for (const count of counts.values()) {
if (count > 1) {
duplicateCount += count;
}
}
return duplicateCount >= 8;
}
function renderSnapshotDisplayLines(lines: ReturnType<typeof buildSnapshotDisplayLines>): string[] {
return lines.flatMap((line) => [line.text, ...readHiddenContentHintLines(line)]);
}
function buildFlattenedSnapshotDisplayLines(nodes: SnapshotNode[]): string[] {
return buildSnapshotDisplayLines(nodes, { summarizeTextSurfaces: true }).flatMap((line) => [
formatSnapshotLine(line.node, 0, false, line.type, { summarizeTextSurfaces: true }),
...readHiddenContentHintLines({ ...line, depth: 0 }),
]);
}
function readHiddenContentHintLines(
line: ReturnType<typeof buildSnapshotDisplayLines>[number],
): string[] {
const target = hintTargetLabel(line.type);
if (!target) {
return [];
}
const hints: string[] = [];
if (line.node.hiddenContentAbove) {
hints.push(`[content above ${target} hidden]`);
}
if (line.node.hiddenContentBelow) {
hints.push(`[content below ${target} hidden]`);
}
if (hints.length === 0) {
return [];
}
const indent = ' '.repeat(line.depth + 1);
return hints.map((hint) => `${indent}${hint}`);
}
function hintTargetLabel(type: string): string | null {
if (type === 'scroll-area' || type === 'list' || type === 'collection' || type === 'table') {
return type;
}
return null;
}