|
| 1 | +import type { SnapshotRuntimeAcquiredResult } from '@agent-device/contracts/interactor-types'; |
| 2 | +import type { |
| 3 | + IosAcquisitionResidue, |
| 4 | + IosProviderAcquisitionProducer, |
| 5 | + IosSnapshotProducer, |
| 6 | + IosSnapshotProducerCapabilities, |
| 7 | + IosSnapshotLineage, |
| 8 | + IosViewportEvidence, |
| 9 | +} from '@agent-device/contracts/ios-snapshot'; |
| 10 | +import { normalizeType } from '@agent-device/contracts/snapshot'; |
| 11 | +import { isPositiveFiniteRect } from '@agent-device/kernel/rect'; |
| 12 | +import type { RawSnapshotNode, Rect } from '@agent-device/kernel/snapshot'; |
| 13 | + |
| 14 | +const ACQUIRED_PRODUCER_CAPABILITY_DEFAULTS = { |
| 15 | + stage: 'acquired', |
| 16 | + acquisitionDepth: { |
| 17 | + rawTraversal: { kind: 'incomplete' }, |
| 18 | + regularPresented: { kind: 'incomplete' }, |
| 19 | + }, |
| 20 | + scopeCompleteness: 'incomplete', |
| 21 | + interactiveQueryCompleteness: 'incomplete', |
| 22 | + viewportEvidence: 'available', |
| 23 | + hittabilityEvidence: 'unavailable', |
| 24 | + truncationEvidence: 'unavailable', |
| 25 | + presentationOwner: 'ios-snapshot-engine', |
| 26 | +} as const; |
| 27 | + |
| 28 | +const IOS_SNAPSHOT_PRODUCER_CAPABILITY_VALUES = { |
| 29 | + 'apple-runner': { |
| 30 | + producer: 'apple-runner', |
| 31 | + stage: 'presented', |
| 32 | + acquisitionDepth: { |
| 33 | + rawTraversal: { kind: 'not-applicable' }, |
| 34 | + regularPresented: { kind: 'not-applicable' }, |
| 35 | + }, |
| 36 | + scopeCompleteness: 'complete', |
| 37 | + interactiveQueryCompleteness: 'complete', |
| 38 | + viewportEvidence: 'available', |
| 39 | + hittabilityEvidence: 'available', |
| 40 | + truncationEvidence: 'available', |
| 41 | + presentationOwner: 'ios-snapshot-engine', |
| 42 | + }, |
| 43 | + 'simulator-ax-bridge': { |
| 44 | + ...ACQUIRED_PRODUCER_CAPABILITY_DEFAULTS, |
| 45 | + producer: 'simulator-ax-bridge', |
| 46 | + acquisitionDepth: { |
| 47 | + rawTraversal: { kind: 'complete' }, |
| 48 | + regularPresented: { kind: 'incomplete' }, |
| 49 | + }, |
| 50 | + hittabilityEvidence: 'available', |
| 51 | + truncationEvidence: 'available', |
| 52 | + presentationOwner: 'snapshot-state', |
| 53 | + }, |
| 54 | + 'appium-source': { |
| 55 | + ...ACQUIRED_PRODUCER_CAPABILITY_DEFAULTS, |
| 56 | + producer: 'appium-source', |
| 57 | + }, |
| 58 | + 'limrun-ios-tree': { |
| 59 | + ...ACQUIRED_PRODUCER_CAPABILITY_DEFAULTS, |
| 60 | + producer: 'limrun-ios-tree', |
| 61 | + }, |
| 62 | +} as const satisfies Record<IosSnapshotProducer, IosSnapshotProducerCapabilities>; |
| 63 | + |
| 64 | +export const IOS_SNAPSHOT_PRODUCER_CAPABILITIES: Readonly< |
| 65 | + Record<IosSnapshotProducer, IosSnapshotProducerCapabilities> |
| 66 | +> = Object.freeze(IOS_SNAPSHOT_PRODUCER_CAPABILITY_VALUES); |
| 67 | + |
| 68 | +function deriveIosSnapshotAcquisitionResidue( |
| 69 | + producer: IosSnapshotProducerCapabilities, |
| 70 | + viewport: IosViewportEvidence, |
| 71 | +): readonly IosAcquisitionResidue[] { |
| 72 | + const residue: IosAcquisitionResidue[] = []; |
| 73 | + if (producer.hittabilityEvidence === 'unavailable') { |
| 74 | + residue.push({ kind: 'unavailable-fact', fact: 'hittability' }); |
| 75 | + } |
| 76 | + if ( |
| 77 | + producer.stage === 'acquired' && |
| 78 | + (producer.acquisitionDepth.rawTraversal.kind === 'incomplete' || |
| 79 | + producer.acquisitionDepth.regularPresented.kind === 'incomplete') |
| 80 | + ) { |
| 81 | + residue.push({ kind: 'unavailable-fact', fact: 'acquisition-depth' }); |
| 82 | + } |
| 83 | + if (producer.truncationEvidence === 'unavailable') { |
| 84 | + residue.push({ kind: 'unavailable-fact', fact: 'truncation' }); |
| 85 | + } |
| 86 | + if (viewport.kind === 'missing') { |
| 87 | + residue.push({ kind: 'missing-viewport', reason: viewport.reason }); |
| 88 | + } |
| 89 | + return Object.freeze(residue); |
| 90 | +} |
| 91 | + |
| 92 | +export function createIosSnapshotAcquisition( |
| 93 | + input: Readonly<{ |
| 94 | + producer: IosProviderAcquisitionProducer; |
| 95 | + nodes: readonly RawSnapshotNode[]; |
| 96 | + viewport: IosViewportEvidence; |
| 97 | + lineage: IosSnapshotLineage; |
| 98 | + }>, |
| 99 | +): SnapshotRuntimeAcquiredResult { |
| 100 | + const producer = IOS_SNAPSHOT_PRODUCER_CAPABILITIES[input.producer]; |
| 101 | + return { |
| 102 | + stage: 'acquired', |
| 103 | + acquisition: { |
| 104 | + producer: input.producer, |
| 105 | + intent: 'full', |
| 106 | + nodes: input.nodes, |
| 107 | + viewport: input.viewport, |
| 108 | + lineage: input.lineage, |
| 109 | + residue: deriveIosSnapshotAcquisitionResidue(producer, input.viewport), |
| 110 | + }, |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +type IosSnapshotViewportRoot = Readonly<{ |
| 115 | + type?: string; |
| 116 | + rect?: Rect; |
| 117 | + rectStatus?: 'reported' | 'invalid' | 'not-provided'; |
| 118 | +}>; |
| 119 | + |
| 120 | +export function resolveIosViewportEvidenceFromRoots( |
| 121 | + roots: readonly IosSnapshotViewportRoot[], |
| 122 | + options: Readonly<{ fallbackToLargestRoot?: boolean }> = {}, |
| 123 | +): IosViewportEvidence | undefined { |
| 124 | + const viewportRoots = roots.filter(isViewportRoot); |
| 125 | + const candidates = |
| 126 | + viewportRoots.length > 0 || options.fallbackToLargestRoot !== true ? viewportRoots : roots; |
| 127 | + const root = [...candidates].sort(compareViewportRoots)[0]; |
| 128 | + if (!root) return undefined; |
| 129 | + if (isPositiveFiniteRect(root.rect)) return { kind: 'reported', rect: root.rect }; |
| 130 | + return { |
| 131 | + kind: 'missing', |
| 132 | + reason: |
| 133 | + root.rectStatus === 'invalid' || (root.rectStatus === undefined && root.rect !== undefined) |
| 134 | + ? 'invalid' |
| 135 | + : 'not-provided', |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +function isViewportRoot(root: IosSnapshotViewportRoot): boolean { |
| 140 | + const type = normalizeType(root.type ?? ''); |
| 141 | + return type === 'application' || type === 'window'; |
| 142 | +} |
| 143 | + |
| 144 | +function compareViewportRoots( |
| 145 | + left: IosSnapshotViewportRoot, |
| 146 | + right: IosSnapshotViewportRoot, |
| 147 | +): number { |
| 148 | + const status = rootGeometryRank(right.rectStatus) - rootGeometryRank(left.rectStatus); |
| 149 | + return status || rectArea(right.rect) - rectArea(left.rect); |
| 150 | +} |
| 151 | + |
| 152 | +function rootGeometryRank(status: IosSnapshotViewportRoot['rectStatus']): number { |
| 153 | + return status === 'reported' ? 2 : status === 'invalid' ? 1 : 0; |
| 154 | +} |
| 155 | + |
| 156 | +function rectArea(rect: Rect | undefined): number { |
| 157 | + return rect && isPositiveFiniteRect(rect) ? rect.width * rect.height : 0; |
| 158 | +} |
0 commit comments