|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import type { SnapshotNode } from '../kernel/snapshot.ts'; |
| 4 | +import { buildNodes } from '../__tests__/test-utils/snapshot-builders.ts'; |
| 5 | +import { buildSelectorChainForNode } from './build.ts'; |
| 6 | + |
| 7 | +function findByLabel(nodes: SnapshotNode[], label: string): SnapshotNode { |
| 8 | + const found = nodes.find((node) => node.label === label); |
| 9 | + if (!found) throw new Error(`fixture missing node with label ${label}`); |
| 10 | + return found; |
| 11 | +} |
| 12 | + |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | +// #1269: a recorded id only leads the chain when it is unique in the |
| 15 | +// record-time tree. `android:id/title` — a shared Android framework |
| 16 | +// resource id present on every titled list row — is the measured repro |
| 17 | +// shape; matchCount reflects how many nodes in `nodes` carry the id. |
| 18 | +// --------------------------------------------------------------------------- |
| 19 | + |
| 20 | +function androidSettingsListFixture(): SnapshotNode[] { |
| 21 | + return buildNodes([ |
| 22 | + { index: 0, type: 'FrameLayout', depth: 0 }, |
| 23 | + { index: 1, type: 'RecyclerView', depth: 1, parentIndex: 0 }, |
| 24 | + // Each row is a label-less LinearLayout wrapper (real Android list shape) |
| 25 | + // whose title TextView shares the same framework resource id. |
| 26 | + { index: 2, type: 'LinearLayout', depth: 2, parentIndex: 1 }, |
| 27 | + { |
| 28 | + index: 3, |
| 29 | + type: 'TextView', |
| 30 | + identifier: 'android:id/title', |
| 31 | + label: 'Network & internet', |
| 32 | + rect: { x: 0, y: 100, width: 300, height: 48 }, |
| 33 | + depth: 3, |
| 34 | + parentIndex: 2, |
| 35 | + }, |
| 36 | + { index: 4, type: 'LinearLayout', depth: 2, parentIndex: 1 }, |
| 37 | + { |
| 38 | + index: 5, |
| 39 | + type: 'TextView', |
| 40 | + identifier: 'android:id/title', |
| 41 | + label: 'Connected devices', |
| 42 | + rect: { x: 0, y: 148, width: 300, height: 48 }, |
| 43 | + depth: 3, |
| 44 | + parentIndex: 4, |
| 45 | + }, |
| 46 | + { index: 6, type: 'LinearLayout', depth: 2, parentIndex: 1 }, |
| 47 | + { |
| 48 | + index: 7, |
| 49 | + type: 'TextView', |
| 50 | + identifier: 'android:id/title', |
| 51 | + label: 'Apps', |
| 52 | + rect: { x: 0, y: 196, width: 300, height: 48 }, |
| 53 | + depth: 3, |
| 54 | + parentIndex: 6, |
| 55 | + }, |
| 56 | + ]); |
| 57 | +} |
| 58 | + |
| 59 | +test('buildSelectorChainForNode: a shared Android framework id (matchCount > 1) is demoted — chain leads with role+label, not id', () => { |
| 60 | + const nodes = androidSettingsListFixture(); |
| 61 | + const row = findByLabel(nodes, 'Network & internet'); |
| 62 | + const chain = buildSelectorChainForNode(row, 'android', { action: 'get', nodes }); |
| 63 | + assert.deepEqual(chain, [ |
| 64 | + 'role="textview" label="Network & internet"', |
| 65 | + 'label="Network & internet"', |
| 66 | + ]); |
| 67 | + assert.ok( |
| 68 | + !chain.some((entry) => entry.startsWith('id=')), |
| 69 | + 'a non-unique id must never lead (or appear in) the recorded selector chain', |
| 70 | + ); |
| 71 | +}); |
| 72 | + |
| 73 | +test('buildSelectorChainForNode: an RN FlatList reusing one testID across rows (iOS/RN, matchCount > 1) is demoted the same way', () => { |
| 74 | + // RN's common FlatList antipattern: `renderItem` assigns the SAME testID to |
| 75 | + // every row. On iOS this surfaces as one accessibility identifier shared by |
| 76 | + // every cell — the same non-selective-id class as the Android framework |
| 77 | + // resource id above, reproduced on a different platform to prove the rule |
| 78 | + // is capture-time uniqueness, not an `android:id/*` namespace check. |
| 79 | + const nodes = buildNodes([ |
| 80 | + { index: 0, type: 'Application', depth: 0 }, |
| 81 | + { index: 1, type: 'ScrollView', identifier: 'contacts-list', depth: 1, parentIndex: 0 }, |
| 82 | + { |
| 83 | + index: 2, |
| 84 | + type: 'Cell', |
| 85 | + identifier: 'contact-row', |
| 86 | + label: 'Ada Lovelace', |
| 87 | + rect: { x: 0, y: 0, width: 320, height: 60 }, |
| 88 | + depth: 2, |
| 89 | + parentIndex: 1, |
| 90 | + }, |
| 91 | + { |
| 92 | + index: 3, |
| 93 | + type: 'Cell', |
| 94 | + identifier: 'contact-row', |
| 95 | + label: 'Grace Hopper', |
| 96 | + rect: { x: 0, y: 60, width: 320, height: 60 }, |
| 97 | + depth: 2, |
| 98 | + parentIndex: 1, |
| 99 | + }, |
| 100 | + { |
| 101 | + index: 4, |
| 102 | + type: 'Cell', |
| 103 | + identifier: 'contact-row', |
| 104 | + label: 'Katherine Johnson', |
| 105 | + rect: { x: 0, y: 120, width: 320, height: 60 }, |
| 106 | + depth: 2, |
| 107 | + parentIndex: 1, |
| 108 | + }, |
| 109 | + ]); |
| 110 | + const row = findByLabel(nodes, 'Grace Hopper'); |
| 111 | + const chain = buildSelectorChainForNode(row, 'ios', { action: 'get', nodes }); |
| 112 | + assert.deepEqual(chain, ['role="cell" label="Grace Hopper"', 'label="Grace Hopper"']); |
| 113 | + assert.ok(!chain.some((entry) => entry.startsWith('id='))); |
| 114 | +}); |
| 115 | + |
| 116 | +test('buildSelectorChainForNode: a unique id still leads the chain (existing already-clean behavior is preserved)', () => { |
| 117 | + const nodes = buildNodes([ |
| 118 | + { index: 0, type: 'Window', depth: 0 }, |
| 119 | + { |
| 120 | + index: 1, |
| 121 | + type: 'Button', |
| 122 | + identifier: 'save', |
| 123 | + label: 'Save', |
| 124 | + rect: { x: 10, y: 10, width: 40, height: 20 }, |
| 125 | + depth: 1, |
| 126 | + parentIndex: 0, |
| 127 | + }, |
| 128 | + ]); |
| 129 | + const node = findByLabel(nodes, 'Save'); |
| 130 | + const chain = buildSelectorChainForNode(node, 'ios', { action: 'get', nodes }); |
| 131 | + assert.deepEqual(chain, ['id="save"', 'role="button" label="Save"', 'label="Save"']); |
| 132 | +}); |
| 133 | + |
| 134 | +test('buildSelectorChainForNode: without a record-time tree, an id is trusted as-is (back-compat default)', () => { |
| 135 | + const nodes = buildNodes([ |
| 136 | + { |
| 137 | + index: 0, |
| 138 | + type: 'Button', |
| 139 | + identifier: 'save', |
| 140 | + label: 'Save', |
| 141 | + rect: { x: 10, y: 10, width: 40, height: 20 }, |
| 142 | + depth: 0, |
| 143 | + }, |
| 144 | + ]); |
| 145 | + const chain = buildSelectorChainForNode(nodes[0]!, 'ios', { action: 'get' }); |
| 146 | + assert.deepEqual(chain, ['id="save"', 'role="button" label="Save"', 'label="Save"']); |
| 147 | +}); |
0 commit comments