|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { |
| 4 | + findSelectorChainMatch, |
| 5 | + formatSelectorFailure, |
| 6 | + isNodeEditable, |
| 7 | + isNodeVisible, |
| 8 | + isSelectorToken, |
| 9 | + parseSelectorChain, |
| 10 | + resolveSelectorChain, |
| 11 | + tryParseSelectorChain, |
| 12 | + type Selector, |
| 13 | + type SelectorChain, |
| 14 | + type SelectorDiagnostics, |
| 15 | + type SelectorResolution, |
| 16 | +} from '../selectors.ts'; |
| 17 | +import type { SnapshotNode } from '../utils/snapshot.ts'; |
| 18 | + |
| 19 | +const nodes: SnapshotNode[] = [ |
| 20 | + { |
| 21 | + ref: 'e1', |
| 22 | + index: 0, |
| 23 | + type: 'android.widget.Button', |
| 24 | + label: 'Continue', |
| 25 | + rect: { x: 0, y: 0, width: 120, height: 48 }, |
| 26 | + enabled: true, |
| 27 | + }, |
| 28 | + { |
| 29 | + ref: 'e2', |
| 30 | + index: 1, |
| 31 | + type: 'android.widget.EditText', |
| 32 | + label: 'Email', |
| 33 | + rect: { x: 0, y: 64, width: 200, height: 48 }, |
| 34 | + enabled: true, |
| 35 | + }, |
| 36 | +]; |
| 37 | + |
| 38 | +test('public selector subpath exposes platform-aware matching helpers', () => { |
| 39 | + const chain: SelectorChain = parseSelectorChain('role=button label="Continue" visible=true'); |
| 40 | + const firstSelector: Selector = chain.selectors[0]; |
| 41 | + assert.equal(firstSelector.raw, 'role=button label="Continue" visible=true'); |
| 42 | + assert.equal(tryParseSelectorChain(chain.raw)?.raw, chain.raw); |
| 43 | + assert.equal(isSelectorToken('visible=true'), true); |
| 44 | + |
| 45 | + const match = findSelectorChainMatch(nodes, chain, { |
| 46 | + platform: 'android', |
| 47 | + requireRect: true, |
| 48 | + }); |
| 49 | + assert.ok(match); |
| 50 | + assert.equal(match.matches, 1); |
| 51 | + |
| 52 | + const resolved: SelectorResolution | null = resolveSelectorChain(nodes, chain, { |
| 53 | + platform: 'android', |
| 54 | + requireRect: true, |
| 55 | + }); |
| 56 | + assert.equal(resolved?.node.ref, 'e1'); |
| 57 | + |
| 58 | + assert.equal(isNodeVisible(nodes[0]), true); |
| 59 | + assert.equal(isNodeEditable(nodes[1], 'android'), true); |
| 60 | +}); |
| 61 | + |
| 62 | +test('public selector diagnostics format failures', () => { |
| 63 | + const chain = parseSelectorChain('label=Missing'); |
| 64 | + const diagnostics: SelectorDiagnostics[] = [{ selector: 'label=Missing', matches: 0 }]; |
| 65 | + |
| 66 | + assert.equal( |
| 67 | + formatSelectorFailure(chain, diagnostics, { unique: false }), |
| 68 | + 'Selector did not match (label=Missing -> 0)', |
| 69 | + ); |
| 70 | +}); |
0 commit comments