diff --git a/packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts b/packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts new file mode 100644 index 0000000000..90aede79a9 --- /dev/null +++ b/packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts @@ -0,0 +1,85 @@ +import assert from 'node:assert/strict'; +import { test } from 'vitest'; +import type { SnapshotNode } from '@agent-device/kernel/snapshot'; +import { formatRole } from '@agent-device/kernel/snapshot'; +import { buildSnapshotDiff } from '../snapshot-diff.ts'; +import { formatSnapshotLine } from '../snapshot-lines.ts'; + +function slider(value?: string, label = 'Brightness', type = 'Other'): SnapshotNode { + return { + ref: 'e3', + index: 0, + depth: 0, + type, + kind: formatRole(type), + label, + ...(value === undefined ? {} : { value }), + enabled: true, + hittable: true, + }; +} + +test.each([[false], [true]])( + 'a labeled non-editable node prints its differing value (summarize=%s)', + (summarizeTextSurfaces) => { + const line = formatSnapshotLine(slider('60%'), 0, false, undefined, { summarizeTextSurfaces }); + assert.equal(line, '@e3 [other] "Brightness" [value:"60%"]'); + }, +); + +test.each([ + ['no value', slider()], + ['value equal to the label', slider('Brightness')], + ['unlabeled node, value already shown as the label', slider('60%', '')], + ['editable node, value already shown as the label', slider('hello', 'Name', 'TextField')], + [ + 'checkable node, value already shown as its state', + { ...slider('1', 'Wi-Fi', 'Switch'), checked: true }, + ], +])('no value marker for %s', (_name, node) => { + const line = formatSnapshotLine(node, 0, false, undefined, { summarizeTextSurfaces: true }); + assert.doesNotMatch(line, /\[value:/); +}); + +test('a long value with quotes and backslashes is escaped and truncated', () => { + const value = `say "hi" C:\\dir ${'x'.repeat(60)}`; + const line = formatSnapshotLine(slider(value), 0, false, undefined, {}); + assert.equal( + line, + `@e3 [other] "Brightness" [value:"say \\"hi\\" C:\\\\dir ${'x'.repeat(29)}..."]`, + ); +}); + +test.each([ + [ + 'a labeled SecureTextField', + { ...slider('••••••••', 'Password', 'SecureTextField'), password: true }, + ], + ['a password node under a non-field role', { ...slider('hunter2', 'PIN'), password: true }], +])('%s never prints its value', (_name, node) => { + for (const summarizeTextSurfaces of [false, true]) { + const line = formatSnapshotLine(node, 0, false, undefined, { summarizeTextSurfaces }); + assert.doesNotMatch(line, /\[value:|••|hunter2/); + } + const diff = buildSnapshotDiff([node], [{ ...node, value: `${node.value}x` }]); + assert.equal(diff.summary.unchanged, 1); +}); + +test('a value-only change diffs as changed lines that read differently', () => { + const diff = buildSnapshotDiff([slider('60%')], [slider('96%')]); + const changed = diff.lines.filter((line) => line.kind !== 'unchanged'); + + assert.equal(diff.summary.unchanged, 0); + assert.equal(diff.summary.additions, 1); + assert.equal(diff.summary.removals, 1); + assert.match(changed[0]!.text, /\[value:"60%"\]/); + assert.match(changed.at(-1)!.text, /\[value:"96%"\]/); +}); + +test('a still value diffs as unchanged', () => { + const diff = buildSnapshotDiff([slider('60%')], [slider('60%')]); + assert.equal( + diff.lines.every((line) => line.kind === 'unchanged'), + true, + ); +}); diff --git a/packages/capture-kit/src/snapshot/snapshot-diff.ts b/packages/capture-kit/src/snapshot/snapshot-diff.ts index d494d76089..a83b99f632 100644 --- a/packages/capture-kit/src/snapshot/snapshot-diff.ts +++ b/packages/capture-kit/src/snapshot/snapshot-diff.ts @@ -6,6 +6,7 @@ import { formatRole, formatSnapshotLine, stateMarkers, + valueMarker, } from './snapshot-lines.ts'; export type SnapshotDiffResult = { @@ -31,6 +32,7 @@ function snapshotNodeToComparableLine(node: SnapshotNode, depthOverride?: number // The states the rendered line prints are the states the key compares, from one function, so a // fact the diff weighs is always visible in the line it prints. const statePart = stateMarkers(node).join(','); + const valuePart = valueMarker(node, role) ?? ''; const hittablePart = node.hittable === true ? 'hittable' : 'not-hittable'; const depthPart = String(depthOverride ?? node.depth ?? 0); // The rendered line carries the actions list, so the comparable key has to as @@ -38,7 +40,7 @@ function snapshotNodeToComparableLine(node: SnapshotNode, depthOverride?: number // silently differs from the baseline's. JSON-encoded because the names are // app-authored and may contain the field separator. const actionsPart = node.actions ? JSON.stringify(node.actions) : ''; - return [depthPart, role, textPart, statePart, hittablePart, actionsPart].join('|'); + return [depthPart, role, textPart, statePart, valuePart, hittablePart, actionsPart].join('|'); } export function buildSnapshotDiff( diff --git a/packages/capture-kit/src/snapshot/snapshot-lines.ts b/packages/capture-kit/src/snapshot/snapshot-lines.ts index b607222117..66c8567c65 100644 --- a/packages/capture-kit/src/snapshot/snapshot-lines.ts +++ b/packages/capture-kit/src/snapshot/snapshot-lines.ts @@ -145,8 +145,9 @@ export function displayLabel(node: SnapshotNode, type: string): string { return ''; } const value = node.value?.trim(); - if (isEditableRole(type)) { - if (value) return value; + if (isEditableRole(node, type)) { + // A secure field's value is the secret, or its masked length: the label stands in for it. + if (value && node.password !== true) return value; if (label) return label; } else if (label) { return label; @@ -178,8 +179,37 @@ export function stateMarkers(node: SnapshotNode): string[] { return markers; } -function isEditableRole(type: string): boolean { - return type === 'text-field' || type === 'text-view' || type === 'search'; +/** + * A labeled node prints its label, so a non-editable node's `value` (a slider's percentage, a picker + * wheel's selection) would otherwise never reach the text line. It rides along as metadata only when + * nothing else in the line carries it: editable nodes print the value as their label, an unlabeled + * node falls back to its value, a checkable control prints its state, and a secure field's value is + * never printed. Shared with the diff key so a value-only change reads as a changed pair, not as + * `+0 -0`. + */ +export function valueMarker(node: SnapshotNode, type: string): string | undefined { + if (isEditableRole(node, type) || node.checked !== undefined) return undefined; + const label = node.label?.trim(); + if (!label || shouldSuppressScrollContainerLabel(type, label)) return undefined; + const value = collapseControlCharacters(node.value ?? ''); + if (!value || value === collapseControlCharacters(label)) return undefined; + return `value:"${escapePreviewText(buildTextPreview(value))}"`; +} + +/** + * The one predicate for "this node's value is its text": `displayLabel` prints the value first for + * these nodes and `valueMarker` skips them, so the two cannot drift. Secure fields count, by role + * or by the producer's `password` / `editable` facts, whatever display role their type maps to. + */ +function isEditableRole(node: SnapshotNode, type: string): boolean { + return ( + type === 'text-field' || + type === 'text-view' || + type === 'search' || + type === 'securetextfield' || + node.editable === true || + node.password === true + ); } function shouldSuppressScrollContainerLabel(type: string, label: string): boolean { @@ -216,13 +246,15 @@ function buildLineMetadata( textSurface: { text: string; isLargeSurface: boolean; shouldSummarize: boolean }, ): string[] { const metadata = [...stateMarkers(node), ...(node.presentationHints ?? [])]; + const value = valueMarker(node, type); + if (value) metadata.push(value); if (!options.summarizeTextSurfaces) { return uniqueMetadata(metadata); } if (node.focused === true) metadata.push('focused'); - if (isEditableRole(type)) metadata.push('editable'); + if (isEditableRole(node, type)) metadata.push('editable'); const placeholder = collapseControlCharacters(node.placeholder ?? ''); - if (isEditableRole(type) && !trimText(node.label) && !trimText(node.value) && placeholder) { + if (isEditableRole(node, type) && !trimText(node.label) && !trimText(node.value) && placeholder) { metadata.push(`placeholder:"${escapePreviewText(placeholder)}"`); } if (looksScrollable(node, type)) metadata.push('scrollable');