|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import { |
| 3 | + buildUnchangedSnapshotMetadata, |
| 4 | + ensureSnapshotPresentationKey, |
| 5 | +} from '../snapshot-unchanged.ts'; |
| 6 | +import type { SnapshotState } from '../../utils/snapshot.ts'; |
| 7 | + |
| 8 | +function snapshot( |
| 9 | + label: string, |
| 10 | + overrides: Partial<SnapshotState> = {}, |
| 11 | + options: Parameters<typeof ensureSnapshotPresentationKey>[1] = {}, |
| 12 | +): SnapshotState { |
| 13 | + return ensureSnapshotPresentationKey( |
| 14 | + { |
| 15 | + nodes: [ |
| 16 | + { |
| 17 | + ref: 'e1', |
| 18 | + index: 0, |
| 19 | + depth: 0, |
| 20 | + type: 'Button', |
| 21 | + label, |
| 22 | + pid: 1234, |
| 23 | + hittable: true, |
| 24 | + }, |
| 25 | + ], |
| 26 | + createdAt: 1_000, |
| 27 | + backend: 'xctest', |
| 28 | + ...overrides, |
| 29 | + }, |
| 30 | + options, |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +test('unchanged metadata ignores refs and volatile process ids', () => { |
| 35 | + const previous = snapshot('Create'); |
| 36 | + const current = snapshot('Create', { |
| 37 | + nodes: [{ ...previous.nodes[0]!, ref: 'e99', pid: 5678 }], |
| 38 | + }); |
| 39 | + |
| 40 | + expect(buildUnchangedSnapshotMetadata({ previous, current, options: {} })).toMatchObject({ |
| 41 | + nodeCount: 1, |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +test('unchanged metadata detects visible label changes', () => { |
| 46 | + expect( |
| 47 | + buildUnchangedSnapshotMetadata({ |
| 48 | + previous: snapshot('Create'), |
| 49 | + current: snapshot('Send'), |
| 50 | + options: {}, |
| 51 | + }), |
| 52 | + ).toBeUndefined(); |
| 53 | +}); |
| 54 | + |
| 55 | +test('unchanged metadata requires comparison-safe snapshots', () => { |
| 56 | + expect( |
| 57 | + buildUnchangedSnapshotMetadata({ |
| 58 | + previous: snapshot('Create', { comparisonSafe: false }), |
| 59 | + current: snapshot('Create'), |
| 60 | + options: {}, |
| 61 | + }), |
| 62 | + ).toBeUndefined(); |
| 63 | + |
| 64 | + expect( |
| 65 | + buildUnchangedSnapshotMetadata({ |
| 66 | + previous: snapshot('Create'), |
| 67 | + current: snapshot('Create', { comparisonSafe: false }), |
| 68 | + options: {}, |
| 69 | + }), |
| 70 | + ).toBeUndefined(); |
| 71 | +}); |
| 72 | + |
| 73 | +test('unchanged metadata requires matching presentation key and identity', () => { |
| 74 | + const previous = snapshot('Create', { createdAt: 1_000 }); |
| 75 | + const current = snapshot('Create', { createdAt: 3_500 }); |
| 76 | + |
| 77 | + expect( |
| 78 | + buildUnchangedSnapshotMetadata({ |
| 79 | + previous, |
| 80 | + current: snapshot('Create', { createdAt: 3_500 }, { scope: 'Composer' }), |
| 81 | + options: { scope: 'Composer' }, |
| 82 | + }), |
| 83 | + ).toBeUndefined(); |
| 84 | + |
| 85 | + expect( |
| 86 | + buildUnchangedSnapshotMetadata({ |
| 87 | + previous, |
| 88 | + current, |
| 89 | + options: {}, |
| 90 | + identity: { |
| 91 | + previousAppBundleId: 'com.example.before', |
| 92 | + currentAppBundleId: 'com.example.after', |
| 93 | + }, |
| 94 | + }), |
| 95 | + ).toBeUndefined(); |
| 96 | + |
| 97 | + expect( |
| 98 | + buildUnchangedSnapshotMetadata({ |
| 99 | + previous: snapshot( |
| 100 | + 'Create', |
| 101 | + { createdAt: 1_000 }, |
| 102 | + { interactiveOnly: true, scope: 'Composer' }, |
| 103 | + ), |
| 104 | + current: snapshot( |
| 105 | + 'Create', |
| 106 | + { createdAt: 3_500 }, |
| 107 | + { interactiveOnly: true, scope: 'Composer' }, |
| 108 | + ), |
| 109 | + options: { interactiveOnly: true, scope: 'Composer' }, |
| 110 | + identity: { |
| 111 | + previousAppBundleId: 'com.example.app', |
| 112 | + currentAppBundleId: 'com.example.app', |
| 113 | + }, |
| 114 | + }), |
| 115 | + ).toMatchObject({ ageMs: 2_500, nodeCount: 1, interactiveOnly: true, scope: 'Composer' }); |
| 116 | +}); |
| 117 | + |
| 118 | +test('unchanged metadata trims scope in compact output metadata', () => { |
| 119 | + expect( |
| 120 | + buildUnchangedSnapshotMetadata({ |
| 121 | + previous: snapshot('Create', { createdAt: 1_000 }, { scope: ' Composer ' }), |
| 122 | + current: snapshot('Create', { createdAt: 3_500 }, { scope: ' Composer ' }), |
| 123 | + options: { scope: ' Composer ' }, |
| 124 | + }), |
| 125 | + ).toMatchObject({ scope: 'Composer' }); |
| 126 | +}); |
| 127 | + |
| 128 | +test('force-full and raw snapshots do not emit unchanged metadata', () => { |
| 129 | + const previous = snapshot('Create'); |
| 130 | + const current = snapshot('Create'); |
| 131 | + |
| 132 | + expect( |
| 133 | + buildUnchangedSnapshotMetadata({ previous, current, options: { forceFull: true } }), |
| 134 | + ).toBeUndefined(); |
| 135 | + expect( |
| 136 | + buildUnchangedSnapshotMetadata({ previous, current, options: { raw: true } }), |
| 137 | + ).toBeUndefined(); |
| 138 | +}); |
0 commit comments