|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { attachRefs, type RawSnapshotNode } from '../../utils/snapshot.ts'; |
| 4 | +import { buildSnapshotDiff } from '../snapshot-diff.ts'; |
| 5 | + |
| 6 | +function nodes(raw: RawSnapshotNode[]) { |
| 7 | + return attachRefs(raw); |
| 8 | +} |
| 9 | + |
| 10 | +test('buildSnapshotDiff reports unchanged lines when snapshots are equal', () => { |
| 11 | + const previous = nodes([ |
| 12 | + { index: 0, depth: 0, type: 'XCUIElementTypeWindow' }, |
| 13 | + { index: 1, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' }, |
| 14 | + ]); |
| 15 | + const current = nodes([ |
| 16 | + { index: 0, depth: 0, type: 'XCUIElementTypeWindow' }, |
| 17 | + { index: 1, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' }, |
| 18 | + ]); |
| 19 | + |
| 20 | + const diff = buildSnapshotDiff(previous, current); |
| 21 | + assert.equal(diff.summary.additions, 0); |
| 22 | + assert.equal(diff.summary.removals, 0); |
| 23 | + assert.equal(diff.summary.unchanged, 2); |
| 24 | + assert.deepEqual(diff.lines.map((line) => line.kind), ['unchanged', 'unchanged']); |
| 25 | +}); |
| 26 | + |
| 27 | +test('buildSnapshotDiff reports added and removed lines', () => { |
| 28 | + const previous = nodes([ |
| 29 | + { index: 0, depth: 0, type: 'XCUIElementTypeWindow' }, |
| 30 | + { index: 1, depth: 1, type: 'XCUIElementTypeStaticText', label: '67' }, |
| 31 | + { index: 2, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' }, |
| 32 | + ]); |
| 33 | + const current = nodes([ |
| 34 | + { index: 0, depth: 0, type: 'XCUIElementTypeWindow' }, |
| 35 | + { index: 1, depth: 1, type: 'XCUIElementTypeStaticText', label: '134' }, |
| 36 | + { index: 2, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' }, |
| 37 | + ]); |
| 38 | + |
| 39 | + const diff = buildSnapshotDiff(previous, current); |
| 40 | + assert.equal(diff.summary.additions, 1); |
| 41 | + assert.equal(diff.summary.removals, 1); |
| 42 | + assert.equal(diff.summary.unchanged, 2); |
| 43 | + assert.deepEqual(diff.lines.map((line) => line.kind), ['unchanged', 'removed', 'added', 'unchanged']); |
| 44 | +}); |
| 45 | + |
| 46 | +test('buildSnapshotDiff treats value changes as remove plus add', () => { |
| 47 | + const previous = nodes([{ index: 0, depth: 0, type: 'XCUIElementTypeTextField', label: 'Amount', value: '67' }]); |
| 48 | + const current = nodes([{ index: 0, depth: 0, type: 'XCUIElementTypeTextField', label: 'Amount', value: '134' }]); |
| 49 | + |
| 50 | + const diff = buildSnapshotDiff(previous, current); |
| 51 | + assert.equal(diff.summary.additions, 1); |
| 52 | + assert.equal(diff.summary.removals, 1); |
| 53 | + assert.equal(diff.summary.unchanged, 0); |
| 54 | + assert.deepEqual(diff.lines.map((line) => line.kind), ['removed', 'added']); |
| 55 | +}); |
| 56 | + |
| 57 | +test('buildSnapshotDiff preserves surrounding context ordering', () => { |
| 58 | + const previous = nodes([ |
| 59 | + { index: 0, depth: 0, type: 'XCUIElementTypeWindow' }, |
| 60 | + { index: 1, depth: 1, type: 'XCUIElementTypeStaticText', label: 'Count' }, |
| 61 | + { index: 2, depth: 1, type: 'XCUIElementTypeStaticText', label: '67' }, |
| 62 | + { index: 3, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' }, |
| 63 | + ]); |
| 64 | + const current = nodes([ |
| 65 | + { index: 0, depth: 0, type: 'XCUIElementTypeWindow' }, |
| 66 | + { index: 1, depth: 1, type: 'XCUIElementTypeStaticText', label: 'Count' }, |
| 67 | + { index: 2, depth: 1, type: 'XCUIElementTypeStaticText', label: '134' }, |
| 68 | + { index: 3, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' }, |
| 69 | + ]); |
| 70 | + |
| 71 | + const diff = buildSnapshotDiff(previous, current); |
| 72 | + assert.equal(diff.lines[0]?.kind, 'unchanged'); |
| 73 | + assert.equal(diff.lines[1]?.kind, 'unchanged'); |
| 74 | + assert.equal(diff.lines[2]?.kind, 'removed'); |
| 75 | + assert.equal(diff.lines[3]?.kind, 'added'); |
| 76 | + assert.equal(diff.lines[4]?.kind, 'unchanged'); |
| 77 | +}); |
0 commit comments