|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test, vi } from 'vitest'; |
| 3 | +import { AppError } from '@agent-device/kernel/errors'; |
| 4 | +import { |
| 5 | + acquireWebDriverIosSnapshot, |
| 6 | + captureWebDriverIosSnapshot, |
| 7 | + publishWebDriverIosSnapshot, |
| 8 | +} from './webdriver-ios-snapshot.ts'; |
| 9 | + |
| 10 | +const SOURCE = `<AppiumAUT> |
| 11 | + <XCUIElementTypeApplication type="XCUIElementTypeApplication" name="Example" label="Example" enabled="true" visible="true" x="0" y="0" width="390" height="844"> |
| 12 | + <XCUIElementTypeOther name="Content" x="0" y="0" width="390" height="844"> |
| 13 | + <XCUIElementTypeButton name="Continue" label="Continue" enabled="true" visible="true" x="24" y="700" width="160" height="48" /> |
| 14 | + </XCUIElementTypeOther> |
| 15 | + </XCUIElementTypeApplication> |
| 16 | +</AppiumAUT>`; |
| 17 | + |
| 18 | +test('Appium iOS snapshots acquire facts and publish regular output through the engine', async () => { |
| 19 | + const source = vi.fn(async () => SOURCE); |
| 20 | + const result = await captureWebDriverIosSnapshot({ source }, undefined, 'cloud-ios-1'); |
| 21 | + |
| 22 | + assert.equal(result.backend, 'xctest'); |
| 23 | + assert.equal(result.producer, 'appium-source'); |
| 24 | + assert.equal(result.truncated, false); |
| 25 | + assert.deepEqual( |
| 26 | + result.nodes?.map((node) => [node.type, node.label, node.parentIndex]), |
| 27 | + [ |
| 28 | + ['XCUIElementTypeApplication', 'Example', undefined], |
| 29 | + ['XCUIElementTypeOther', 'Content', 0], |
| 30 | + ['XCUIElementTypeButton', 'Continue', 1], |
| 31 | + ], |
| 32 | + ); |
| 33 | + assert.equal(result.nodes?.find((node) => node.label === 'Continue')?.hittable, false); |
| 34 | + assert.equal(source.mock.calls.length, 1); |
| 35 | + assert.ok(result.warnings?.some((warning) => warning.includes('hittability evidence'))); |
| 36 | + assert.equal(JSON.stringify(result).includes('iosSnapshot'), false); |
| 37 | + assert.equal(JSON.stringify(result).includes('apple-runner'), false); |
| 38 | +}); |
| 39 | + |
| 40 | +test('Appium iOS options become an engine plan and engine-owned projection', () => { |
| 41 | + const acquired = acquireWebDriverIosSnapshot(SOURCE, { |
| 42 | + raw: true, |
| 43 | + interactiveOnly: true, |
| 44 | + depth: 1, |
| 45 | + scope: 'Continue', |
| 46 | + customActions: true, |
| 47 | + }); |
| 48 | + |
| 49 | + assert.equal(acquired.input.stage, 'acquired'); |
| 50 | + assert.deepEqual(acquired.plan.narrowing, { |
| 51 | + depth: null, |
| 52 | + scope: null, |
| 53 | + interactiveOnly: false, |
| 54 | + }); |
| 55 | + assert.deepEqual(acquired.input.acquisition.hint, { |
| 56 | + projection: 'raw', |
| 57 | + rawTraversalDepth: null, |
| 58 | + regularPresentedDepth: null, |
| 59 | + interactiveOnly: false, |
| 60 | + customActions: true, |
| 61 | + acquisitionIntent: 'full', |
| 62 | + }); |
| 63 | + assert.deepEqual(acquired.input.acquisition.lineage, {}); |
| 64 | + assert.deepEqual(acquired.input.acquisition.viewport, { |
| 65 | + kind: 'reported', |
| 66 | + rect: { x: 0, y: 0, width: 390, height: 844 }, |
| 67 | + }); |
| 68 | + |
| 69 | + const published = publishWebDriverIosSnapshot(acquired); |
| 70 | + assert.deepEqual( |
| 71 | + published.result.nodes?.map((node) => [node.type, node.label, node.depth, node.parentIndex]), |
| 72 | + [['XCUIElementTypeButton', 'Continue', 0, undefined]], |
| 73 | + ); |
| 74 | + assert.deepEqual(published.publication.comparisonIdentity.lineage, {}); |
| 75 | +}); |
| 76 | + |
| 77 | +test('Appium iOS interactive requests stay provider-unpruned and use engine presentation', () => { |
| 78 | + const acquired = acquireWebDriverIosSnapshot(SOURCE, { interactiveOnly: true }, 'ios-1'); |
| 79 | + |
| 80 | + assert.equal(acquired.input.acquisition.hint.interactiveOnly, true); |
| 81 | + assert.equal(acquired.plan.narrowing.interactiveOnly, false); |
| 82 | + const published = publishWebDriverIosSnapshot(acquired); |
| 83 | + assert.equal( |
| 84 | + published.result.nodes?.some((node) => node.label === 'Continue'), |
| 85 | + true, |
| 86 | + ); |
| 87 | + assert.equal(published.result.nodes?.find((node) => node.label === 'Continue')?.hittable, false); |
| 88 | +}); |
| 89 | + |
| 90 | +test('Appium iOS regular presentation fails typed when page source has no viewport', () => { |
| 91 | + const acquired = acquireWebDriverIosSnapshot( |
| 92 | + '<AppiumAUT><XCUIElementTypeOther name="Content"><XCUIElementTypeButton name="Continue" enabled="true" x="0" y="0" width="100" height="40" /></XCUIElementTypeOther></AppiumAUT>', |
| 93 | + ); |
| 94 | + |
| 95 | + assert.deepEqual(acquired.input.acquisition.viewport, { |
| 96 | + kind: 'missing', |
| 97 | + reason: 'not-provided', |
| 98 | + }); |
| 99 | + assert.deepEqual(acquired.input.acquisition.residue, [ |
| 100 | + { kind: 'unavailable-fact', fact: 'hittability' }, |
| 101 | + { kind: 'missing-viewport', reason: 'not-provided' }, |
| 102 | + ]); |
| 103 | + assert.throws( |
| 104 | + () => publishWebDriverIosSnapshot(acquired), |
| 105 | + (error: unknown) => |
| 106 | + error instanceof AppError && |
| 107 | + error.details?.reason === 'missing-viewport' && |
| 108 | + !('iosSnapshotEngine' in (error.details ?? {})), |
| 109 | + ); |
| 110 | +}); |
| 111 | + |
| 112 | +test('Appium iOS does not promote a non-viewport root rectangle to viewport evidence', () => { |
| 113 | + const acquired = acquireWebDriverIosSnapshot( |
| 114 | + '<AppiumAUT><XCUIElementTypeOther name="Content" x="0" y="0" width="390" height="844"><XCUIElementTypeButton name="Continue" enabled="true" x="0" y="0" width="100" height="40" /></XCUIElementTypeOther></AppiumAUT>', |
| 115 | + ); |
| 116 | + |
| 117 | + assert.deepEqual(acquired.input.acquisition.viewport, { |
| 118 | + kind: 'missing', |
| 119 | + reason: 'not-provided', |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +test('Appium iOS regular presentation fails typed when root geometry is invalid', () => { |
| 124 | + const acquired = acquireWebDriverIosSnapshot( |
| 125 | + '<AppiumAUT><XCUIElementTypeApplication x="0" y="0" width="invalid" height="844"><XCUIElementTypeButton name="Continue" enabled="true" x="0" y="0" width="100" height="40" /></XCUIElementTypeApplication></AppiumAUT>', |
| 126 | + ); |
| 127 | + |
| 128 | + assert.deepEqual(acquired.input.acquisition.viewport, { |
| 129 | + kind: 'missing', |
| 130 | + reason: 'invalid', |
| 131 | + }); |
| 132 | + assert.throws( |
| 133 | + () => publishWebDriverIosSnapshot(acquired), |
| 134 | + (error: unknown) => error instanceof AppError && error.details?.reason === 'invalid-viewport', |
| 135 | + ); |
| 136 | +}); |
| 137 | + |
| 138 | +test('Appium iOS truncation is typed and disclosed at response level', async () => { |
| 139 | + const result = await captureWebDriverIosSnapshot( |
| 140 | + { source: async () => SOURCE.replace('<AppiumAUT>', '<AppiumAUT truncated="true">') }, |
| 141 | + { raw: true }, |
| 142 | + ); |
| 143 | + |
| 144 | + assert.equal(result.truncated, true); |
| 145 | + assert.deepEqual(result.warnings, [ |
| 146 | + 'Appium page source does not provide hittability evidence; regular snapshot nodes are not actionable.', |
| 147 | + 'Appium page source is truncated; the snapshot hierarchy may be incomplete.', |
| 148 | + ]); |
| 149 | +}); |
0 commit comments