@buoy-gg/highlight-updates 7.0.40 → 7.0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +11 -67
  2. package/lib/commonjs/highlight-updates/HighlightUpdatesOverlay.js +1 -281
  3. package/lib/commonjs/highlight-updates/components/HighlightFilterView.js +1 -1474
  4. package/lib/commonjs/highlight-updates/components/HighlightUpdatesModal.js +1 -641
  5. package/lib/commonjs/highlight-updates/components/IdentifierBadge.js +1 -273
  6. package/lib/commonjs/highlight-updates/components/IsolatedRenderList.js +1 -188
  7. package/lib/commonjs/highlight-updates/components/ModalHeaderContent.js +1 -293
  8. package/lib/commonjs/highlight-updates/components/RenderCauseBadge.js +1 -508
  9. package/lib/commonjs/highlight-updates/components/RenderDetailView.js +1 -846
  10. package/lib/commonjs/highlight-updates/components/RenderHistoryViewer.js +1 -915
  11. package/lib/commonjs/highlight-updates/components/RenderListItem.js +1 -235
  12. package/lib/commonjs/highlight-updates/components/RendersCopySettingsView.js +1 -469
  13. package/lib/commonjs/highlight-updates/components/StatsDisplay.js +1 -73
  14. package/lib/commonjs/highlight-updates/types/copySettings.js +1 -107
  15. package/lib/commonjs/highlight-updates/utils/CommitProfiler.js +1 -446
  16. package/lib/commonjs/highlight-updates/utils/HighlightUpdatesController.js +1 -1920
  17. package/lib/commonjs/highlight-updates/utils/PerformanceLogger.js +26 -359
  18. package/lib/commonjs/highlight-updates/utils/ProfilerInterceptor.js +1 -371
  19. package/lib/commonjs/highlight-updates/utils/RenderCauseDetector.js +5 -1840
  20. package/lib/commonjs/highlight-updates/utils/RenderTracker.js +1 -952
  21. package/lib/commonjs/highlight-updates/utils/ViewTypeMapper.js +1 -264
  22. package/lib/commonjs/highlight-updates/utils/copySettingsStorage.js +1 -32
  23. package/lib/commonjs/highlight-updates/utils/devtoolsComponentNames.js +1 -188
  24. package/lib/commonjs/highlight-updates/utils/namedComposite.js +1 -41
  25. package/lib/commonjs/highlight-updates/utils/ownStateChanged.js +1 -103
  26. package/lib/commonjs/highlight-updates/utils/rawEventEmitter.js +1 -50
  27. package/lib/commonjs/highlight-updates/utils/rawEventEmitter.web.js +1 -0
  28. package/lib/commonjs/highlight-updates/utils/renderExportFormatter.js +1 -58
  29. package/lib/commonjs/highlight-updates/utils/rendersExportFormatter.js +1 -485
  30. package/lib/commonjs/highlight-updates/utils/screenElements.js +1 -1248
  31. package/lib/commonjs/highlight-updates/utils/tapMatch.js +1 -83
  32. package/lib/commonjs/highlight-updates/utils/testIDs.js +1 -68
  33. package/lib/commonjs/highlight-updates/utils/touchCapture.js +1 -277
  34. package/lib/commonjs/index.js +1 -429
  35. package/lib/commonjs/preset.js +1 -278
  36. package/lib/commonjs/sync/highlightUpdatesSyncAdapter.js +1 -235
  37. package/lib/module/highlight-updates/HighlightUpdatesOverlay.js +1 -274
  38. package/lib/module/highlight-updates/components/HighlightFilterView.js +1 -1468
  39. package/lib/module/highlight-updates/components/HighlightUpdatesModal.js +1 -634
  40. package/lib/module/highlight-updates/components/IdentifierBadge.js +1 -265
  41. package/lib/module/highlight-updates/components/IsolatedRenderList.js +1 -183
  42. package/lib/module/highlight-updates/components/ModalHeaderContent.js +1 -288
  43. package/lib/module/highlight-updates/components/RenderCauseBadge.js +1 -499
  44. package/lib/module/highlight-updates/components/RenderDetailView.js +1 -840
  45. package/lib/module/highlight-updates/components/RenderHistoryViewer.js +1 -908
  46. package/lib/module/highlight-updates/components/RenderListItem.js +1 -230
  47. package/lib/module/highlight-updates/components/RendersCopySettingsView.js +1 -465
  48. package/lib/module/highlight-updates/components/StatsDisplay.js +1 -70
  49. package/lib/module/highlight-updates/types/copySettings.js +1 -102
  50. package/lib/module/highlight-updates/utils/CommitProfiler.js +1 -442
  51. package/lib/module/highlight-updates/utils/HighlightUpdatesController.js +1 -1916
  52. package/lib/module/highlight-updates/utils/PerformanceLogger.js +26 -353
  53. package/lib/module/highlight-updates/utils/ProfilerInterceptor.js +1 -358
  54. package/lib/module/highlight-updates/utils/RenderCauseDetector.js +5 -1830
  55. package/lib/module/highlight-updates/utils/RenderTracker.js +1 -949
  56. package/lib/module/highlight-updates/utils/ViewTypeMapper.js +1 -255
  57. package/lib/module/highlight-updates/utils/copySettingsStorage.js +1 -26
  58. package/lib/module/highlight-updates/utils/devtoolsComponentNames.js +1 -178
  59. package/lib/module/highlight-updates/utils/namedComposite.js +1 -36
  60. package/lib/module/highlight-updates/utils/ownStateChanged.js +1 -98
  61. package/lib/module/highlight-updates/utils/rawEventEmitter.js +1 -46
  62. package/lib/module/highlight-updates/utils/rawEventEmitter.web.js +1 -0
  63. package/lib/module/highlight-updates/utils/renderExportFormatter.js +1 -54
  64. package/lib/module/highlight-updates/utils/rendersExportFormatter.js +1 -478
  65. package/lib/module/highlight-updates/utils/screenElements.js +1 -1242
  66. package/lib/module/highlight-updates/utils/tapMatch.js +1 -76
  67. package/lib/module/highlight-updates/utils/testIDs.js +1 -60
  68. package/lib/module/highlight-updates/utils/touchCapture.js +1 -270
  69. package/lib/module/index.js +1 -130
  70. package/lib/module/preset.js +1 -272
  71. package/lib/module/sync/highlightUpdatesSyncAdapter.js +1 -230
  72. package/lib/typescript/highlight-updates/utils/HighlightUpdatesController.d.ts +0 -26
  73. package/lib/typescript/highlight-updates/utils/rawEventEmitter.web.d.ts +3 -0
  74. package/lib/typescript/highlight-updates/utils/screenElements.d.ts +0 -19
  75. package/lib/web/index.mjs +38 -0
  76. package/package.json +34 -14
  77. package/lib/typescript/highlight-updates/HighlightUpdatesOverlay.d.ts.map +0 -1
  78. package/lib/typescript/highlight-updates/components/HighlightFilterView.d.ts.map +0 -1
  79. package/lib/typescript/highlight-updates/components/HighlightUpdatesModal.d.ts.map +0 -1
  80. package/lib/typescript/highlight-updates/components/IdentifierBadge.d.ts.map +0 -1
  81. package/lib/typescript/highlight-updates/components/IsolatedRenderList.d.ts.map +0 -1
  82. package/lib/typescript/highlight-updates/components/ModalHeaderContent.d.ts.map +0 -1
  83. package/lib/typescript/highlight-updates/components/RenderCauseBadge.d.ts.map +0 -1
  84. package/lib/typescript/highlight-updates/components/RenderDetailView.d.ts.map +0 -1
  85. package/lib/typescript/highlight-updates/components/RenderHistoryViewer.d.ts.map +0 -1
  86. package/lib/typescript/highlight-updates/components/RenderListItem.d.ts.map +0 -1
  87. package/lib/typescript/highlight-updates/components/RendersCopySettingsView.d.ts.map +0 -1
  88. package/lib/typescript/highlight-updates/components/StatsDisplay.d.ts.map +0 -1
  89. package/lib/typescript/highlight-updates/types/copySettings.d.ts.map +0 -1
  90. package/lib/typescript/highlight-updates/utils/CommitProfiler.d.ts.map +0 -1
  91. package/lib/typescript/highlight-updates/utils/HighlightUpdatesController.d.ts.map +0 -1
  92. package/lib/typescript/highlight-updates/utils/PerformanceLogger.d.ts.map +0 -1
  93. package/lib/typescript/highlight-updates/utils/ProfilerInterceptor.d.ts.map +0 -1
  94. package/lib/typescript/highlight-updates/utils/RenderCauseDetector.d.ts.map +0 -1
  95. package/lib/typescript/highlight-updates/utils/RenderTracker.d.ts.map +0 -1
  96. package/lib/typescript/highlight-updates/utils/ViewTypeMapper.d.ts.map +0 -1
  97. package/lib/typescript/highlight-updates/utils/copySettingsStorage.d.ts.map +0 -1
  98. package/lib/typescript/highlight-updates/utils/devtoolsComponentNames.d.ts.map +0 -1
  99. package/lib/typescript/highlight-updates/utils/namedComposite.d.ts.map +0 -1
  100. package/lib/typescript/highlight-updates/utils/ownStateChanged.d.ts.map +0 -1
  101. package/lib/typescript/highlight-updates/utils/rawEventEmitter.d.ts.map +0 -1
  102. package/lib/typescript/highlight-updates/utils/renderExportFormatter.d.ts.map +0 -1
  103. package/lib/typescript/highlight-updates/utils/rendersExportFormatter.d.ts.map +0 -1
  104. package/lib/typescript/highlight-updates/utils/screenElements.d.ts.map +0 -1
  105. package/lib/typescript/highlight-updates/utils/tapMatch.d.ts.map +0 -1
  106. package/lib/typescript/highlight-updates/utils/testIDs.d.ts.map +0 -1
  107. package/lib/typescript/highlight-updates/utils/touchCapture.d.ts.map +0 -1
  108. package/lib/typescript/index.d.ts.map +0 -1
  109. package/lib/typescript/preset.d.ts.map +0 -1
  110. package/lib/typescript/sync/highlightUpdatesSyncAdapter.d.ts.map +0 -1
@@ -1,1248 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.describeScreen = describeScreen;
7
- exports.resolveTouchTarget = resolveTouchTarget;
8
- exports.tapElement = tapElement;
9
- exports.waitFor = waitFor;
10
- var _reactNative = require("react-native");
11
- var _tapMatch = require("./tapMatch");
12
- /**
13
- * On-device screen element discovery + in-JS tap.
14
- *
15
- * This powers the MCP "describe_screen" / "tap_element" tools. It walks the
16
- * live React fiber tree (all renderers), collects the meaningful / interactive
17
- * elements on the current screen, and measures each one's on-screen rect — the
18
- * same idea as the desktop screenshot tool's component-locate, but producing a
19
- * whole-screen list with normalized tap coordinates instead of a single crop.
20
- *
21
- * Tapping is done PURELY in JS: we resolve the target element, find the nearest
22
- * `onPress` handler up its fiber chain, and invoke it with a synthetic press
23
- * event. No screenshots, no OS-level touch injection, and it works on physical
24
- * devices — because Buoy runs INSIDE the app (unlike external simulator drivers
25
- * that have to inject an OS touch at a pixel coordinate).
26
- *
27
- * Fiber traversal is adapted from image-overlay/utils/fiberScanner.ts and
28
- * debug-borders/utils/fiberTreeTraversal.js; the fiber-access triad matches the
29
- * rest of highlight-updates (Fabric / Paper / RN-CLI-dev).
30
- */
31
-
32
- /* eslint-disable @typescript-eslint/no-explicit-any */
33
-
34
- // --- React fiber tag / renderer plumbing ---------------------------------
35
-
36
- const HostComponent = 5;
37
- const OffscreenComponent = 22;
38
-
39
- // Component names that are pure infrastructure and never worth listing on
40
- // their own. Kept small on purpose — a candidate still needs a real signal
41
- // (onPress / testID / label / text / role) to be emitted, so this only
42
- // suppresses the *name* of wrappers that happen to also carry one.
43
- const INTERNAL_NAMES = new Set(["View", "RCTView", "ScrollView", "RCTScrollView", "RCTScrollContentView", "SafeAreaView", "RCTSafeAreaView", "AndroidHorizontalScrollContentView", "KeyboardAvoidingView", "Pressable", "PlatformPressable", "TouchableOpacity", "TouchableHighlight", "TouchableWithoutFeedback", "TouchableNativeFeedback", "AnimatedComponent", "Wrapper"]);
44
- function getReactDevToolsHook() {
45
- if (typeof global === "undefined") return null;
46
- return global.__REACT_DEVTOOLS_GLOBAL_HOOK__ ?? null;
47
- }
48
-
49
- /**
50
- * React commits observed since this counter was installed.
51
- *
52
- * `tapElement` reads it either side of firing a handler to say whether
53
- * ANYTHING re-rendered afterwards. Without that a tap that matched a button
54
- * on the screen BENEATH the current one (a stack keeps it mounted) came back
55
- * `tapped:true` — the handler really did run — and an agent then built its
56
- * next three turns on a sign-in that never happened. Zero commits inside the
57
- * settle window is the honest signal: the handler ran and the app did nothing
58
- * visible with it.
59
- *
60
- * Installed once and chained, the same way CommitProfiler wraps the hook;
61
- * counting is the only work it does on the commit path.
62
- */
63
- let commitCount = 0;
64
- let commitCounterInstalled = false;
65
- function ensureCommitCounter() {
66
- if (commitCounterInstalled) return;
67
- const hook = getReactDevToolsHook();
68
- if (!hook) return;
69
- const original = hook.onCommitFiberRoot;
70
- hook.onCommitFiberRoot = function (...args) {
71
- commitCount++;
72
- return typeof original === "function" ? original.apply(hook, args) : undefined;
73
- };
74
- commitCounterInstalled = true;
75
- }
76
-
77
- /** How long a tap waits to see the app react before reporting. */
78
- const TAP_SETTLE_MS = 250;
79
-
80
- /**
81
- * Per-walk memo of "is this fiber under Buoy's own overlay". Every candidate
82
- * asks, and they share ancestors, so the answer is cached along the path
83
- * walked rather than recomputed 400 levels deep per row.
84
- */
85
- function makeChromeCheck() {
86
- const memo = new Map();
87
- return fiber => {
88
- const path = [];
89
- let cur = fiber;
90
- let depth = 0;
91
- let inChrome = false;
92
- while (cur && depth < 400) {
93
- const known = memo.get(cur);
94
- if (known !== undefined) {
95
- inChrome = known;
96
- break;
97
- }
98
- if (getProps(cur)?.__buoyDevToolsRoot) {
99
- inChrome = true;
100
- memo.set(cur, true);
101
- break;
102
- }
103
- path.push(cur);
104
- cur = cur.return;
105
- depth++;
106
- }
107
- for (const f of path) memo.set(f, inChrome);
108
- return inChrome;
109
- };
110
- }
111
-
112
- /**
113
- * Collect fiber roots across ALL renderers. A single renderer id is not safe
114
- * to assume: secondary reconcilers (react-native-skia, react-native-svg, …)
115
- * register their own renderer, sometimes first as id 1, whose roots contain
116
- * none of the tappable app UI.
117
- */
118
- function getAllFiberRoots() {
119
- const hook = getReactDevToolsHook();
120
- if (!hook) return [];
121
- const roots = [];
122
- try {
123
- if (hook.renderers && typeof hook.renderers.forEach === "function") {
124
- hook.renderers.forEach((_r, rendererId) => {
125
- try {
126
- const set = hook.getFiberRoots?.(rendererId);
127
- if (set) set.forEach(root => roots.push(root));
128
- } catch {
129
- /* ignore a single renderer failing */
130
- }
131
- });
132
- }
133
- if (roots.length === 0 && hook.getFiberRoots) {
134
- const legacy = hook.getFiberRoots(1);
135
- if (legacy) legacy.forEach(root => roots.push(root));
136
- }
137
- } catch {
138
- /* ignore */
139
- }
140
- return roots;
141
- }
142
- function isHiddenOffscreen(fiber) {
143
- return fiber.tag === OffscreenComponent && fiber.memoizedState !== null;
144
- }
145
- function isInactiveScreen(fiber) {
146
- // react-native-screens: activityState 0/1 = inactive/transitioning.
147
- const as = fiber.memoizedProps?.activityState;
148
- if (as === 0 || as === 1) return true;
149
- // pagers
150
- if (fiber.memoizedProps?.isPageFocused === false) return true;
151
- return false;
152
- }
153
- function traverseFiberTree(fiber, callback, depth = 0, visited = new Set()) {
154
- if (!fiber || visited.has(fiber) || depth > 500) return;
155
- visited.add(fiber);
156
- if (isHiddenOffscreen(fiber) || isInactiveScreen(fiber)) {
157
- // Prune the whole subtree, but keep walking siblings.
158
- if (fiber.sibling) traverseFiberTree(fiber.sibling, callback, depth, visited);
159
- return;
160
- }
161
- callback(fiber);
162
- if (fiber.child) traverseFiberTree(fiber.child, callback, depth + 1, visited);
163
- if (fiber.sibling) traverseFiberTree(fiber.sibling, callback, depth, visited);
164
- }
165
-
166
- // --- Fiber → host instance / name / props --------------------------------
167
-
168
- /** Get a measurable public instance from a host fiber's stateNode. */
169
- function getPublicInstance(stateNode) {
170
- if (!stateNode) return null;
171
- const node = stateNode;
172
- if (node.canonical?.publicInstance) return node.canonical.publicInstance;
173
- if (node.canonical && typeof node.canonical.measure === "function") {
174
- return node.canonical;
175
- }
176
- if (typeof node.measure === "function" || typeof node.measureInWindow === "function") {
177
- return node;
178
- }
179
- return null;
180
- }
181
- function getNativeTag(stateNode) {
182
- if (stateNode == null) return null;
183
- const n = stateNode;
184
- if (typeof n.__nativeTag === "number") return n.__nativeTag;
185
- if (typeof n._nativeTag === "number") return n._nativeTag;
186
- if (typeof n.nativeTag === "number") return n.nativeTag;
187
- if (n.canonical) {
188
- if (typeof n.canonical.__nativeTag === "number") return n.canonical.__nativeTag;
189
- if (typeof n.canonical._nativeTag === "number") return n.canonical._nativeTag;
190
- if (typeof n.canonical.nativeTag === "number") return n.canonical.nativeTag;
191
- }
192
- return null;
193
- }
194
-
195
- /** The first host node in a fiber's own subtree (self first, then descendants). */
196
- function findHostInstance(fiber) {
197
- const selfInst = getPublicInstance(fiber.stateNode);
198
- if (selfInst) {
199
- return {
200
- instance: selfInst,
201
- nativeTag: getNativeTag(fiber.stateNode),
202
- stateNode: fiber.stateNode
203
- };
204
- }
205
-
206
- // DFS restricted to this fiber's subtree (child + siblings-of-children).
207
- const stack = [];
208
- if (fiber.child) stack.push(fiber.child);
209
- let guard = 0;
210
- while (stack.length > 0 && guard < 4000) {
211
- guard++;
212
- const f = stack.pop();
213
- if (!f) continue;
214
- const inst = getPublicInstance(f.stateNode);
215
- if (inst) return {
216
- instance: inst,
217
- nativeTag: getNativeTag(f.stateNode),
218
- stateNode: f.stateNode
219
- };
220
- if (f.child) stack.push(f.child);
221
- if (f.sibling) stack.push(f.sibling);
222
- }
223
- return null;
224
- }
225
-
226
- /**
227
- * Walk up from a fiber to the nearest ScrollView-like instance (exposes
228
- * scrollTo). Mirrors HighlightUpdatesController.findScrollAncestor.
229
- */
230
- function findScrollAncestor(fiber) {
231
- let cur = fiber;
232
- let guard = 0;
233
- while (cur && guard++ < 200) {
234
- const inst = cur.stateNode;
235
- if (inst && typeof inst.scrollTo === "function") {
236
- const nativeRef = typeof inst.getNativeScrollRef === "function" && inst.getNativeScrollRef() || null;
237
- const innerNode = typeof inst.getInnerViewNode === "function" && inst.getInnerViewNode() || typeof inst.getScrollableNode === "function" && inst.getScrollableNode() || inst;
238
- return {
239
- instance: inst,
240
- nativeRef,
241
- innerNode
242
- };
243
- }
244
- cur = cur.return;
245
- }
246
- return null;
247
- }
248
-
249
- /**
250
- * Best-effort scroll of an ancestor ScrollView so the target sits near the top
251
- * of its viewport. Returns true only if a scroll was issued. Never throws.
252
- * Ported from HighlightUpdatesController.scrollDescendantIntoView.
253
- */
254
- async function scrollIntoView(targetFiber, publicInstance, stateNode, margin) {
255
- try {
256
- const ancestor = findScrollAncestor(targetFiber);
257
- if (!ancestor) return false;
258
- const inst = publicInstance;
259
- let top = null;
260
-
261
- // measureLayout can invoke NEITHER callback on some architectures — race
262
- // every call against a short timeout so the await can't hang.
263
- const withTimeout = run => new Promise(resolve => {
264
- let done = false;
265
- const settle = v => {
266
- if (done) return;
267
- done = true;
268
- resolve(v);
269
- };
270
- const timer = setTimeout(() => settle(null), 600);
271
- try {
272
- run(v => {
273
- clearTimeout(timer);
274
- settle(v);
275
- });
276
- } catch {
277
- clearTimeout(timer);
278
- settle(null);
279
- }
280
- });
281
-
282
- // Fabric: ref.measureLayout against the inner view ref.
283
- if (ancestor.nativeRef && typeof inst.measureLayout === "function") {
284
- top = await withTimeout(resolve => inst.measureLayout(ancestor.nativeRef, (_left, t) => resolve(t), () => resolve(null)));
285
- }
286
-
287
- // Legacy: UIManager.measureLayout with numeric node handles.
288
- if (top == null) {
289
- const targetHandle = (0, _reactNative.findNodeHandle)(publicInstance);
290
- const innerHandle = typeof ancestor.innerNode === "number" ? ancestor.innerNode : (0, _reactNative.findNodeHandle)(ancestor.innerNode);
291
- if (targetHandle != null && innerHandle != null) {
292
- top = await withTimeout(resolve => _reactNative.UIManager.measureLayout(targetHandle, innerHandle, () => resolve(null), (_left, t) => resolve(t)));
293
- }
294
- }
295
- if (top == null) return false;
296
- ancestor.instance.scrollTo({
297
- y: Math.max(0, top - margin),
298
- animated: false
299
- });
300
- await new Promise(r => setTimeout(r, 120)); // let scroll + layout settle
301
- return true;
302
- } catch {
303
- return false;
304
- }
305
- }
306
- function getComponentName(fiber) {
307
- const t = fiber?.type;
308
- if (!t) return null;
309
- if (typeof t === "string") return t;
310
- return t.displayName || t.name || null;
311
- }
312
-
313
- /** Walk up to the nearest user-defined component name (skips host/internal). */
314
- function getOwningComponentName(fiber) {
315
- let cur = fiber?._debugOwner || fiber?.return;
316
- let depth = 0;
317
- while (cur && depth < 30) {
318
- depth++;
319
- const name = getComponentName(cur);
320
- if (name && !INTERNAL_NAMES.has(name) && typeof cur.type !== "string") {
321
- return name;
322
- }
323
- cur = cur.return;
324
- }
325
- return null;
326
- }
327
- function getProps(fiber) {
328
- return fiber?.memoizedProps ?? fiber?.pendingProps ?? null;
329
- }
330
-
331
- /**
332
- * The text of a `children` value, INCLUDING one assembled from several pieces.
333
- *
334
- * `<Text>{value} {label}</Text>` compiles to `children: ["1,450", " ", "Points"]`
335
- * and `Check Back Soon{"\n"}For Available Offers` to three strings. This used
336
- * to accept a lone string or number and nothing else, so EVERY composed label
337
- * in an app was invisible to describeScreen, tapElement and waitFor — while
338
- * native accessibility, which concatenates the same children, could read them.
339
- * Measured on a live QA run: five waits failed against an empty state that was
340
- * on screen the whole time, and the agent then reported the app was missing
341
- * one; a points balance could not be found by its own number.
342
- *
343
- * Nested React ELEMENTS are not walked here — `collectDescendantText` owns
344
- * that, with its stop-at-a-nested-button rule — so this stays a leaf join, and
345
- * adjacent pieces are concatenated exactly as React renders them.
346
- */
347
- function flattenText(value, depth = 0) {
348
- if (typeof value === "string") return value;
349
- if (typeof value === "number" && Number.isFinite(value)) return String(value);
350
- if (Array.isArray(value) && depth < 4) {
351
- const parts = [];
352
- for (const child of value) {
353
- const part = flattenText(child, depth + 1);
354
- if (part) parts.push(part);
355
- }
356
- return parts.length > 0 ? parts.join("") : undefined;
357
- }
358
- return undefined;
359
- }
360
-
361
- /** What a person reads: newlines and runs of spaces collapse to one space. */
362
- function normalizeVisibleText(value) {
363
- return value.replace(/\s+/g, " ").trim();
364
- }
365
- function getVisibleText(props) {
366
- if (!props) return undefined;
367
- const pick = v => {
368
- const flat = flattenText(v);
369
- if (flat === undefined) return undefined;
370
- const text = normalizeVisibleText(flat);
371
- return text ? text.slice(0, 80) : undefined;
372
- };
373
- return pick(props.children) ?? pick(props.title) ?? pick(props.label) ?? pick(props.placeholder) ?? undefined;
374
- }
375
-
376
- /**
377
- * Gather visible text from a fiber's descendants (its own label), stopping at
378
- * nested interactive subtrees so a button doesn't absorb another button's text.
379
- * Lets an icon-button whose label is a child <Text> (e.g. a dial icon) still be
380
- * matched/shown by that label — the button host measures even when the inner
381
- * Text host doesn't.
382
- */
383
- function collectDescendantText(root, maxDepth) {
384
- const parts = [];
385
- const visit = (f, depth) => {
386
- if (!f || depth > maxDepth || parts.length >= 4) return;
387
- // Don't cross into a nested interactive element (another button's label).
388
- if (f !== root) {
389
- const p = getProps(f);
390
- if (p && (typeof p.onPress === "function" || typeof p.onLongPress === "function")) return;
391
- }
392
- const t = getVisibleText(getProps(f));
393
- if (t && !parts.includes(t)) parts.push(t);
394
- if (f.child) visit(f.child, depth + 1);
395
- if (f !== root && f.sibling) visit(f.sibling, depth); // siblings stay in subtree
396
- };
397
- if (root.child) visit(root.child, 1);
398
- const joined = parts.join(" ").trim();
399
- return joined ? joined.slice(0, 80) : undefined;
400
- }
401
-
402
- /** Nearest onPress / onLongPress handlers at or above a fiber. */
403
-
404
- function findPressHandler(fiber) {
405
- let cur = fiber;
406
- let depth = 0;
407
- let press = null;
408
- let longPress = null;
409
- while (cur && depth < 25 && !(press && longPress)) {
410
- const p = getProps(cur);
411
- if (p) {
412
- if (!press && typeof p.onPress === "function") press = p.onPress;
413
- if (!longPress && typeof p.onLongPress === "function") longPress = p.onLongPress;
414
- }
415
- cur = cur.return;
416
- depth++;
417
- }
418
- if (press) return {
419
- handler: press,
420
- kind: "onPress",
421
- longPress: longPress ?? undefined
422
- };
423
- return longPress ? {
424
- handler: longPress,
425
- kind: "onLongPress"
426
- } : null;
427
- }
428
-
429
- // --- Measurement ----------------------------------------------------------
430
-
431
- /** Absolute on-screen rect in points (or null), with a hang guard. */
432
- function measureInstance(instance) {
433
- return new Promise(resolve => {
434
- let settled = false;
435
- const done = r => {
436
- if (settled) return;
437
- settled = true;
438
- resolve(r);
439
- };
440
- // Detached / off-screen nodes can fire neither callback.
441
- setTimeout(() => done(null), 1000);
442
- try {
443
- if (typeof instance.measureInWindow === "function") {
444
- instance.measureInWindow((x, y, width, height) => {
445
- if (width > 0 && height > 0) done({
446
- x,
447
- y,
448
- width,
449
- height
450
- });else done(null);
451
- });
452
- } else if (typeof instance.measure === "function") {
453
- instance.measure((_x, _y, width, height, pageX, pageY) => {
454
- if (width > 0 && height > 0 && pageX != null && pageY != null) {
455
- done({
456
- x: pageX,
457
- y: pageY,
458
- width,
459
- height
460
- });
461
- } else {
462
- done(null);
463
- }
464
- });
465
- } else {
466
- done(null);
467
- }
468
- } catch {
469
- done(null);
470
- }
471
- });
472
- }
473
-
474
- // --- Public types ---------------------------------------------------------
475
-
476
- // Internal record carries the live fiber refs that `describeScreen`'s
477
- // serializable result drops.
478
-
479
- // --- Collection -----------------------------------------------------------
480
-
481
- function collectCandidates() {
482
- const roots = getAllFiberRoots();
483
- if (roots.length === 0) return [];
484
- const out = [];
485
- /**
486
- * De-dup indexes. Two fibers resolving to the same host node collapse to one
487
- * row — a Pressable and its inner View, but ALSO the same native view reached
488
- * twice by the walk, which produces two different `instance` objects for one
489
- * `nativeTag`. Measured on a live QA run: a 141-row screen dump held 70
490
- * distinct elements, every one listed twice, and the agent told the user
491
- * their layout had duplicate elements. Keyed both ways so either collision
492
- * collapses, and as Maps because the old `out.find(…)` was a linear scan per
493
- * candidate on a list that can run to hundreds.
494
- */
495
- const byInstance = new Map();
496
- const byTag = new Map();
497
- const inBuoyChrome = makeChromeCheck();
498
- for (const root of roots) {
499
- traverseFiberTree(root.current, fiber => {
500
- const props = getProps(fiber);
501
- if (!props) return;
502
- const hasPress = typeof props.onPress === "function" || typeof props.onLongPress === "function";
503
- const hasValue = typeof props.onValueChange === "function";
504
- const hasText = typeof props.onChangeText === "function";
505
- const testID = props.testID || undefined;
506
- const label = props.accessibilityLabel || undefined;
507
- const role = props.accessibilityRole || props.role || undefined;
508
- let text = getVisibleText(props);
509
- const roleInteractive = role === "button" || role === "link" || role === "menuitem" || role === "tab" || role === "switch" || role === "checkbox";
510
-
511
- // A fiber earns a row only if it carries a real signal.
512
- if (!hasPress && !hasValue && !hasText && !testID && !label && !text && !roleInteractive) {
513
- return;
514
- }
515
-
516
- // Resolve how this element is driven. A value/text control is a leaf
517
- // (Switch/Slider/TextInput) whose OWN handler wins over an ancestor
518
- // onPress; otherwise fall back to the nearest onPress up the tree.
519
- let press = null;
520
- let valueControl = null;
521
- let textControl = null;
522
- if (hasValue) {
523
- const current = props.value;
524
- const numeric = typeof current === "number" || typeof props.minimumValue === "number";
525
- valueControl = {
526
- handler: props.onValueChange,
527
- kind: numeric ? "slider" : "toggle",
528
- current,
529
- min: typeof props.minimumValue === "number" ? props.minimumValue : undefined,
530
- max: typeof props.maximumValue === "number" ? props.maximumValue : undefined
531
- };
532
- } else if (hasText) {
533
- textControl = {
534
- handler: props.onChangeText,
535
- current: typeof props.value === "string" ? props.value : undefined
536
- };
537
- } else {
538
- press = findPressHandler(fiber);
539
- }
540
- const rawName = getComponentName(fiber);
541
- // Prefer a user component name over a bare host/internal wrapper name.
542
- let name = rawName ?? "Unknown";
543
- if (!rawName || INTERNAL_NAMES.has(rawName) || typeof fiber.type === "string") {
544
- name = getOwningComponentName(fiber) ?? rawName ?? "Unknown";
545
- }
546
- const host = findHostInstance(fiber);
547
- if (!host && !testID) return; // unmeasurable & unnamed → skip
548
-
549
- // Interactive element with no own text/label (icon button, dial icon) —
550
- // adopt a descendant <Text> as its label so it's matchable by that text.
551
- if (!text && !label && hasPress) {
552
- text = collectDescendantText(fiber, 6);
553
- }
554
- const instance = host?.instance ?? null;
555
- const nativeTag = host?.nativeTag ?? null;
556
- const existing = (instance ? byInstance.get(instance) : undefined) ?? (nativeTag != null ? byTag.get(nativeTag) : undefined);
557
- if (existing) {
558
- // Keep the more interactive / better-labelled of the two.
559
- if (!existing.press && press) existing.press = press;else if (existing.press && press?.longPress && !existing.press.longPress) {
560
- existing.press.longPress = press.longPress;
561
- }
562
- if (!existing.valueControl && valueControl) existing.valueControl = valueControl;
563
- if (!existing.textControl && textControl) existing.textControl = textControl;
564
- if (!existing.testID && testID) existing.testID = testID;
565
- if (!existing.label && label) existing.label = label;
566
- if (!existing.text && text) existing.text = text;
567
- // Index the alias too, so a third sighting through either key finds it.
568
- if (instance && !byInstance.has(instance)) byInstance.set(instance, existing);
569
- if (nativeTag != null && !byTag.has(nativeTag)) byTag.set(nativeTag, existing);
570
- return;
571
- }
572
- const candidate = {
573
- nativeTag,
574
- name,
575
- role,
576
- testID,
577
- label,
578
- text,
579
- instance,
580
- stateNode: host?.stateNode ?? null,
581
- fiber,
582
- press,
583
- valueControl,
584
- textControl,
585
- rect: null,
586
- buoy: inBuoyChrome(fiber)
587
- };
588
- out.push(candidate);
589
- if (instance) byInstance.set(instance, candidate);
590
- if (nativeTag != null) byTag.set(nativeTag, candidate);
591
- });
592
- }
593
- return out;
594
- }
595
-
596
- /**
597
- * Whether the caller named THIS element, rather than described one like it.
598
- * An exact nativeTag or testID is a deliberate choice, and is honoured even
599
- * inside Buoy's own chrome — that is how a test or an editor agent drives a
600
- * Buoy tool on purpose. A fuzzy `query` is not.
601
- */
602
- function explicitlyTargets(c, params) {
603
- if (params.nativeTag != null) return c.nativeTag === params.nativeTag;
604
- if (params.testID && c.testID) return c.testID.toLowerCase() === params.testID.toLowerCase();
605
- return false;
606
- }
607
-
608
- /**
609
- * The app under test, not the tool looking at it.
610
- *
611
- * Buoy runs INSIDE the app, so its own sheet is in the same fiber tree as the
612
- * screens it inspects — and Ask Buoy's activity log echoes whatever the model
613
- * is searching for back as visible text. Left in, that echo was the best
614
- * match for the next fuzzy tap or wait, so every text-driven action landed on
615
- * Buoy's own panel and nothing in the app moved. The agent diagnosed it
616
- * itself, four chats in ("every text-based tap and wait I've done this
617
- * session has been landing on Buoy's panel"), and still could not escape it.
618
- *
619
- * `includeBuoy` lists the chrome for callers that want it; an exact id gets
620
- * through regardless (see `explicitlyTargets`).
621
- */
622
- function appCandidates(params) {
623
- const all = collectCandidates();
624
- if (params.includeBuoy) return all;
625
- return all.filter(c => !c.buoy || explicitlyTargets(c, params));
626
- }
627
- async function measureCandidates(candidates) {
628
- await Promise.all(candidates.map(async c => {
629
- if (c.instance) c.rect = await measureInstance(c.instance);
630
- }));
631
- }
632
-
633
- // --- describeScreen -------------------------------------------------------
634
-
635
- async function describeScreen(params = {}) {
636
- const all = collectCandidates();
637
- const candidates = params.includeBuoy ? all : all.filter(c => !c.buoy);
638
- const hiddenBuoy = all.length - candidates.length;
639
- await measureCandidates(candidates);
640
- const win = _reactNative.Dimensions.get("window");
641
- const screenW = Math.round(win.width) || 1;
642
- const screenH = Math.round(win.height) || 1;
643
- const elements = [];
644
- for (const c of candidates) {
645
- const r = c.rect;
646
- // Keep on-screen elements; drop rect-less rows unless they carry a testID.
647
- if (!r && !c.testID) continue;
648
- let tap;
649
- let frame;
650
- if (r) {
651
- const cx = r.x + r.width / 2;
652
- const cy = r.y + r.height / 2;
653
- // Drop previous Expo Router screens (centers well outside the window).
654
- // Keep a 10% slack so the floating menu / home-indicator-clipped rows stay.
655
- if (cy < -screenH * 0.1 || cy > screenH * 1.1 || cx < -screenW * 0.1 || cx > screenW * 1.1) {
656
- continue;
657
- }
658
- tap = {
659
- x: round2(cx / screenW),
660
- y: round2(cy / screenH)
661
- };
662
- frame = {
663
- x: round2(r.x / screenW),
664
- y: round2(r.y / screenH),
665
- width: round2(r.width / screenW),
666
- height: round2(r.height / screenH)
667
- };
668
- }
669
- const control = c.valueControl ? c.valueControl.kind : c.textControl ? "text" : undefined;
670
- const value = c.valueControl ? c.valueControl.current : c.textControl?.current;
671
- elements.push({
672
- nativeTag: c.nativeTag,
673
- name: c.name,
674
- role: c.role,
675
- testID: c.testID,
676
- label: c.label,
677
- text: c.text,
678
- interactive: !!(c.press || c.valueControl || c.textControl),
679
- longPressable: c.press && (c.press.kind === "onLongPress" || c.press.longPress) ? true : undefined,
680
- control,
681
- value,
682
- tap,
683
- frame
684
- });
685
- }
686
-
687
- // Reading order: top-to-bottom, then left-to-right.
688
- elements.sort((a, b) => {
689
- const ay = a.tap?.y ?? 2;
690
- const by = b.tap?.y ?? 2;
691
- if (Math.abs(ay - by) > 0.02) return ay - by;
692
- return (a.tap?.x ?? 2) - (b.tap?.x ?? 2);
693
- });
694
- return {
695
- screen: {
696
- width: screenW,
697
- height: screenH
698
- },
699
- count: elements.length,
700
- elements,
701
- ...(hiddenBuoy > 0 ? {
702
- hiddenBuoy
703
- } : {})
704
- };
705
- }
706
- function round2(n) {
707
- return Math.round(n * 100) / 100;
708
- }
709
-
710
- // --- tapElement -----------------------------------------------------------
711
-
712
- function fieldsOf(c) {
713
- return {
714
- nativeTag: c.nativeTag,
715
- testID: c.testID,
716
- label: c.label,
717
- text: c.text,
718
- name: c.name
719
- };
720
- }
721
- function matchScore(c, params) {
722
- return (0, _tapMatch.scoreTapMatch)(fieldsOf(c), params);
723
- }
724
- function tapPoint(rect, screenW, screenH) {
725
- if (!rect || !screenW || !screenH) return undefined;
726
- return {
727
- x: (rect.x + rect.width / 2) / screenW,
728
- y: (rect.y + rect.height / 2) / screenH
729
- };
730
- }
731
- function makeSyntheticPressEvent(rect) {
732
- const pageX = rect ? rect.x + rect.width / 2 : 0;
733
- const pageY = rect ? rect.y + rect.height / 2 : 0;
734
- const nativeEvent = {
735
- locationX: rect ? rect.width / 2 : 0,
736
- locationY: rect ? rect.height / 2 : 0,
737
- pageX,
738
- pageY,
739
- timestamp: 0,
740
- target: null,
741
- identifier: 1,
742
- touches: [],
743
- changedTouches: []
744
- };
745
- return {
746
- nativeEvent,
747
- persist: () => {},
748
- preventDefault: () => {},
749
- stopPropagation: () => {},
750
- isDefaultPrevented: () => false,
751
- isPropagationStopped: () => false,
752
- target: null,
753
- currentTarget: null,
754
- type: "press",
755
- timeStamp: 0
756
- };
757
- }
758
- async function tapElement(params) {
759
- const candidates = appCandidates(params);
760
- if (candidates.length === 0) {
761
- return {
762
- tapped: false,
763
- reason: "No React fiber roots / elements found on screen."
764
- };
765
- }
766
-
767
- // Rank; only elements with a reachable press handler are tappable.
768
- const ranked = candidates.map(c => ({
769
- c,
770
- score: matchScore(c, params)
771
- })).filter(r => r.score > 0).sort((a, b) => b.score - a.score);
772
- const isInteractive = c => !!(c.press || c.valueControl || c.textControl);
773
- if (ranked.length === 0) {
774
- return {
775
- tapped: false,
776
- reason: "No element matched. Call describe_screen to see available elements (match by testID, text, or name).",
777
- candidates: candidates.filter(isInteractive).slice(0, 12).map(c => ({
778
- nativeTag: c.nativeTag,
779
- name: c.name,
780
- testID: c.testID,
781
- text: c.text
782
- }))
783
- };
784
- }
785
-
786
- // Measure the top matches so an on-screen DialIcon beats an off-screen nav
787
- // chip whose testID happens to contain the same substring.
788
- const win = _reactNative.Dimensions.get("window");
789
- const screenW = win.width || 1;
790
- const screenH = win.height || 1;
791
- const top = ranked.slice(0, 15);
792
- await Promise.all(top.map(async r => {
793
- if (r.c.instance && !r.c.rect) r.c.rect = await measureInstance(r.c.instance);
794
- }));
795
- for (const r of top) {
796
- r.score = (0, _tapMatch.scoreTapMatch)({
797
- ...fieldsOf(r.c),
798
- tap: tapPoint(r.c.rect, screenW, screenH)
799
- }, params);
800
- }
801
- top.sort((a, b) => b.score - a.score);
802
-
803
- // Fall back to the FULL ranked list for an interactive match — the top-15
804
- // re-score is only a tiebreak, and on a busy screen the pressable match can
805
- // sit below 15 text-score peers.
806
- const target = top.find(r => isInteractive(r.c)) ?? ranked.find(r => isInteractive(r.c)) ?? top[0];
807
- const c = target.c;
808
-
809
- // Everything else that matched just as well. A fuzzy "SIGN IN" on a stack
810
- // that still has the home screen mounted underneath has two equal answers,
811
- // and the caller has to know that before trusting whichever one fired.
812
- const sameControl = (a, b) => a.press && a.press.handler === b.press?.handler || a.valueControl && a.valueControl.handler === b.valueControl?.handler || a.textControl && a.textControl.handler === b.textControl?.handler;
813
- const alsoMatched = top.filter(r => r !== target && isInteractive(r.c) && r.score === target.score)
814
- // The label Text inside the tapped button matches its own button's text
815
- // and inherits its onPress: same control, not a rival.
816
- .filter(r => !sameControl(r.c, c)).filter((r, i, arr) => arr.findIndex(o => sameControl(o.c, r.c)) === i).slice(0, 3).map(r => ({
817
- nativeTag: r.c.nativeTag,
818
- name: r.c.name,
819
- testID: r.c.testID,
820
- text: r.c.text,
821
- tap: tapPoint(r.c.rect, screenW, screenH)
822
- }));
823
- if (!isInteractive(c)) {
824
- return {
825
- tapped: false,
826
- reason: `Matched "${c.name}"${c.text ? ` ("${c.text}")` : ""} but it has no onPress / onValueChange / onChangeText handler (e.g. a react-native-gesture-handler GestureDetector). This element can't be driven in JS.`,
827
- matched: {
828
- nativeTag: c.nativeTag,
829
- name: c.name,
830
- testID: c.testID,
831
- label: c.label,
832
- text: c.text,
833
- via: "onPress"
834
- }
835
- };
836
- }
837
-
838
- // Measure the target so we can decide whether to scroll it on-screen (and,
839
- // for a press, give the synthetic event real coordinates).
840
- if (c.instance && !c.rect) c.rect = await measureInstance(c.instance);
841
-
842
- // Bring it into view if it's off-screen and scrolling is wanted. The action
843
- // doesn't NEED this (handlers are invoked directly), but it keeps the element
844
- // visible for the human watching / a follow-up describe.
845
- let scrolled = false;
846
- // A failed measure (rect == null) counts as off-screen: the node may be in
847
- // a virtualized list that hasn't materialized it, and scrolling can help.
848
- const offScreen = c.rect == null || !(0, _tapMatch.isRectCenterOnScreen)(c.rect, screenW, screenH);
849
- if (offScreen && params.scrollIntoView !== false && c.instance) {
850
- scrolled = await scrollIntoView(c.fiber, c.instance, c.stateNode, 80);
851
- if (scrolled) c.rect = await measureInstance(c.instance); // fresh coords
852
- }
853
- const base = {
854
- nativeTag: c.nativeTag,
855
- name: c.name,
856
- testID: c.testID,
857
- label: c.label,
858
- text: c.text
859
- };
860
-
861
- // Fire, then watch. The handler is the app's own; what it did — or did
862
- // not do — shows up as commits (or none) in the settle window.
863
- ensureCommitCounter();
864
- // Read BEFORE the handler runs: a synchronous setState commits inside it.
865
- const c0 = commitCount;
866
- const t0 = Date.now();
867
- const fired = async matched => {
868
- await new Promise(resolve => setTimeout(resolve, TAP_SETTLE_MS));
869
- return {
870
- tapped: true,
871
- scrolled,
872
- matched,
873
- effect: {
874
- commits: commitCount - c0,
875
- observedMs: Date.now() - t0
876
- },
877
- ...(alsoMatched.length ? {
878
- alsoMatched
879
- } : {})
880
- };
881
- };
882
- try {
883
- if (c.press) {
884
- if (params.longPress) {
885
- const lp = c.press.kind === "onLongPress" ? c.press.handler : c.press.longPress;
886
- if (!lp) {
887
- return {
888
- tapped: false,
889
- scrolled,
890
- reason: `Matched "${c.name}"${c.text ? ` ("${c.text}")` : ""} but it has no onLongPress handler — call tap_element without longPress to tap it normally.`,
891
- matched: {
892
- ...base,
893
- via: "onPress"
894
- }
895
- };
896
- }
897
- lp(makeSyntheticPressEvent(c.rect));
898
- return await fired({
899
- ...base,
900
- via: "onLongPress"
901
- });
902
- }
903
- c.press.handler(makeSyntheticPressEvent(c.rect));
904
- return await fired({
905
- ...base,
906
- via: c.press.kind
907
- });
908
- }
909
- if (c.valueControl) {
910
- const vc = c.valueControl;
911
- let next;
912
- if (vc.kind === "toggle") {
913
- next = typeof params.value === "boolean" ? params.value : !vc.current;
914
- } else {
915
- const min = vc.min ?? 0;
916
- const max = vc.max ?? 1;
917
- if (typeof params.value === "number") {
918
- next = Math.min(max, Math.max(min, params.value));
919
- } else {
920
- // No target given → move to the far end so the change is visible.
921
- next = vc.current === max ? min : max;
922
- }
923
- }
924
- vc.handler(next);
925
- return await fired({
926
- ...base,
927
- via: "onValueChange",
928
- value: next
929
- });
930
- }
931
- // textControl
932
- const next = params.text ?? (typeof params.value === "string" ? params.value : "");
933
- c.textControl.handler(next);
934
- return await fired({
935
- ...base,
936
- via: "onChangeText",
937
- value: next
938
- });
939
- } catch (e) {
940
- return {
941
- tapped: false,
942
- scrolled,
943
- reason: `handler threw: ${e instanceof Error ? e.message : String(e)}`,
944
- matched: {
945
- ...base,
946
- via: c.press ? c.press.kind : c.valueControl ? "onValueChange" : "onChangeText"
947
- }
948
- };
949
- }
950
- }
951
-
952
- // --- waitFor --------------------------------------------------------------
953
-
954
- const WAIT_DEFAULT_MS = 5_000;
955
- const WAIT_MAX_MS = 30_000;
956
- const WAIT_MIN_POLL_MS = 50;
957
- const WAIT_DEFAULT_POLL_MS = 150;
958
-
959
- /**
960
- * Block until an element appears (or disappears), then report how long it took.
961
- *
962
- * WHY THIS EXISTS. `navigate` returns the moment the route is pushed, not when
963
- * the new screen has data on it, so "go to /shop then tap Fire Grill" reliably
964
- * tapped a loading skeleton. A remote driver solves that with a wait step
965
- * (MCP's `run_flow` has `waitFor`/`waitMs`); anything driving the tools from
966
- * INSIDE the app had nothing, and its only option was to re-run
967
- * `describeScreen` in a loop — which for an agent means burning turns against a
968
- * step budget, and for a Scenario means a fixed sleep that is either flaky or
969
- * slow.
970
- *
971
- * MATCHING IS `tapElement`'S, EXACTLY. Same `scoreTapMatch`, same fields, so
972
- * "wait for X" and "tap X" can never disagree about what X is. A wait that
973
- * resolves on an element the tap then cannot find is worse than no wait.
974
- *
975
- * PRESENCE MEANS ON SCREEN. An identity scan alone would resolve against a
976
- * previous Expo Router screen still mounted behind the current one — the exact
977
- * false positive `describeScreen` drops by rect. So a candidate that matches by
978
- * name is measured before it counts, and only the matches are measured: the
979
- * per-poll cost stays one fiber walk plus a handful of `measureInWindow`
980
- * calls, not a full `describeScreen`.
981
- *
982
- * The poll floor is deliberate. This runs on the JS thread the app is drawing
983
- * on, and a caller asking for `pollMs: 0` would starve the very render it is
984
- * waiting for.
985
- */
986
- async function waitFor(params) {
987
- // Coerced, not optional-chained: a model sends `query: 1450` often enough,
988
- // and `.trim()` on a number throws before the wait ever starts.
989
- const hasTarget = params.nativeTag != null || !!String(params.testID ?? "").trim() || !!String(params.query ?? "").trim();
990
- if (!hasTarget) {
991
- return {
992
- ok: false,
993
- waitedMs: 0,
994
- polls: 0,
995
- reason: "Nothing to wait for — pass nativeTag, testID or query."
996
- };
997
- }
998
- const timeoutMs = Math.min(Math.max(params.timeoutMs ?? WAIT_DEFAULT_MS, 0), WAIT_MAX_MS);
999
- const pollMs = Math.max(params.pollMs ?? WAIT_DEFAULT_POLL_MS, WAIT_MIN_POLL_MS);
1000
- const wantGone = params.gone === true;
1001
- const started = Date.now();
1002
- let polls = 0;
1003
- for (;;) {
1004
- polls += 1;
1005
- const {
1006
- hit,
1007
- closest
1008
- } = await findOnScreen(params);
1009
- if (wantGone ? !hit : !!hit) {
1010
- return {
1011
- ok: true,
1012
- waitedMs: Date.now() - started,
1013
- polls,
1014
- matched: hit ?? undefined
1015
- };
1016
- }
1017
- if (Date.now() - started >= timeoutMs) {
1018
- const nearMisses = closest?.length ? closest : undefined;
1019
- // Say which of the two failures this is. "Nothing is called that" and
1020
- // "it exists but is not visible" need opposite next moves, and a caller
1021
- // that cannot tell them apart tries the same wait with new words.
1022
- const why = wantGone ? `Still on screen after ${timeoutMs}ms.` : nearMisses ? `Not on screen after ${timeoutMs}ms — ${nearMisses.length === 1 ? "one element" : `${nearMisses.length} elements`} matched but ${nearMisses.some(n => n.onScreen) ? "not well enough" : "none is visible"}. See \`closest\`; do not retry with different words, call describeScreen.` : `Not on screen after ${timeoutMs}ms, and nothing matched those words at all. Call describeScreen to see what is there.`;
1023
- return {
1024
- ok: false,
1025
- waitedMs: Date.now() - started,
1026
- polls,
1027
- reason: why,
1028
- matched: hit ?? undefined,
1029
- closest: nearMisses
1030
- };
1031
- }
1032
- await new Promise(resolve => setTimeout(resolve, pollMs));
1033
- }
1034
- }
1035
-
1036
- /** One scan: rank by identity, then confirm the best match is really visible. */
1037
- async function findOnScreen(params) {
1038
- const ranked = appCandidates(params).map(c => ({
1039
- c,
1040
- score: matchScore(c, params)
1041
- })).filter(r => r.score > 0).sort((a, b) => b.score - a.score)
1042
- // Only the plausible few get measured; a broad query can match dozens.
1043
- .slice(0, 8);
1044
- if (ranked.length === 0) return {
1045
- hit: null,
1046
- closest: undefined
1047
- };
1048
- await measureCandidates(ranked.map(r => r.c));
1049
- const win = _reactNative.Dimensions.get("window");
1050
- const screenW = Math.round(win.width) || 1;
1051
- const screenH = Math.round(win.height) || 1;
1052
- let hit = null;
1053
- for (const {
1054
- c
1055
- } of ranked) {
1056
- if (!(0, _tapMatch.isRectCenterOnScreen)(c.rect, screenW, screenH)) continue;
1057
- hit = {
1058
- nativeTag: c.nativeTag,
1059
- name: c.name,
1060
- testID: c.testID,
1061
- label: c.label,
1062
- text: c.text,
1063
- interactive: !!(c.press || c.valueControl || c.textControl)
1064
- };
1065
- break;
1066
- }
1067
-
1068
- // Built from the same measured ranking, so a failed wait can say what it
1069
- // nearly matched and whether that thing was visible.
1070
- const closest = ranked.slice(0, 3).map(({
1071
- c
1072
- }) => ({
1073
- name: c.name,
1074
- testID: c.testID,
1075
- text: c.text,
1076
- onScreen: (0, _tapMatch.isRectCenterOnScreen)(c.rect, screenW, screenH)
1077
- }));
1078
- return {
1079
- hit,
1080
- closest
1081
- };
1082
- }
1083
-
1084
- // --- resolveTouchTarget ---------------------------------------------------
1085
-
1086
- /**
1087
- * What a recorded touch landed on, in the vocabulary `tapElement` searches by.
1088
- *
1089
- * Deliberately the SAME fields `describeScreen` emits: a macro recorded from a
1090
- * human's finger has to replay through `tapElement`, and the only way to
1091
- * guarantee that is for the recorder to name the element exactly the way the
1092
- * matcher will look for it. Anything richer here is a selector that records
1093
- * fine and then can't be found again.
1094
- */
1095
-
1096
- /**
1097
- * Resolve the native tag from a raw touch event to the element a HUMAN would
1098
- * say they tapped.
1099
- *
1100
- * Synchronous on purpose. The tag arrives on `topTouchStart`, and by the time
1101
- * an async resolve came back the press handler may already have navigated —
1102
- * leaving the recorder to name the element on the screen it moved TO. Everything
1103
- * here is a fiber walk (no measure), which is what makes that affordable.
1104
- *
1105
- * Returns null when the tag belongs to nothing interesting (the background, a
1106
- * scroll surface): the recorder treats that as "not a step", which is exactly
1107
- * right — a tap on empty space is not something to replay.
1108
- */
1109
- /**
1110
- * Is this fiber inside Buoy's own overlay?
1111
- *
1112
- * `FloatingDevTools` marks its single root View with `__buoyDevToolsRoot`, and
1113
- * everything the tool draws — the bubble, the dial, every modal, every tool
1114
- * overlay — is under it. Walking the whole chain (rather than a fixed depth) is
1115
- * the point: a control deep inside a tool's modal is still the tool.
1116
- */
1117
- function isBuoyChrome(fiber) {
1118
- let cur = fiber;
1119
- let depth = 0;
1120
- while (cur && depth < 400) {
1121
- if (getProps(cur)?.__buoyDevToolsRoot) return true;
1122
- cur = cur.return;
1123
- depth++;
1124
- }
1125
- return false;
1126
- }
1127
-
1128
- /**
1129
- * Climb from the node that was touched to the element that OWNS the press.
1130
- *
1131
- * A finger lands on the deepest host view, which for a button is the `<Text>`
1132
- * inside it. That Text is a legitimate candidate — `findPressHandler` walks up
1133
- * and finds the button's `onPress`, so it looks drivable — but it carries the
1134
- * TEXT's identity: no testID, and "visible text" that is whatever string
1135
- * happens to be in that node.
1136
- *
1137
- * Left alone, every recorded step is named after a fragment of copy and
1138
- * replayed by fuzzy-matching that fragment. In the Poké Mart flow, all six
1139
- * steps came back as `Text` with no testID at all, so "tap the Charizard row"
1140
- * was recorded as "The signature. Flame-broiled, wings on, worth the wait." —
1141
- * the brittlest possible selector, chosen when `shop-item-charizard` was one
1142
- * fiber up.
1143
- *
1144
- * So we walk to the fiber that DECLARES `onPress` and take the candidate there.
1145
- * Stopping at the declaring fiber matters: keep climbing and a row inside a
1146
- * pressable list resolves to the list.
1147
- */
1148
- function preferHandlerOwner(match, byFiber) {
1149
- // Already the owner (or a Switch/TextInput, whose handler is its own).
1150
- const own = getProps(match.fiber);
1151
- if (typeof own?.onPress === "function" || typeof own?.onValueChange === "function" || typeof own?.onChangeText === "function") {
1152
- return match;
1153
- }
1154
-
1155
- // 1. Find the fiber that actually declares the handler.
1156
- let declaring = null;
1157
- let cur = match.fiber?.return;
1158
- let depth = 0;
1159
- while (cur && depth < 25) {
1160
- if (typeof getProps(cur)?.onPress === "function") {
1161
- declaring = cur;
1162
- break;
1163
- }
1164
- cur = cur.return;
1165
- depth++;
1166
- }
1167
- if (!declaring) return match;
1168
-
1169
- // 2. Take the candidate AT that fiber, or the first one above it.
1170
- //
1171
- // Stopping exactly at the declaring fiber isn't enough: a `<Pressable>`
1172
- // declares onPress but is de-duped into the component that renders it
1173
- // (`PrimaryButton`), and de-duping keeps the OUTERMOST fiber of everything
1174
- // sharing a host instance — which is where the testID ends up. Climbing a
1175
- // few levels past the handler is what turns "Text: Add to bag" into
1176
- // "PrimaryButton [testID=shop-add-to-bag]". The bound keeps a row inside a
1177
- // pressable list from resolving to the list.
1178
- let probe = declaring;
1179
- for (let i = 0; i < 6 && probe; i++) {
1180
- const candidate = byFiber.get(probe);
1181
- if (candidate) return candidate;
1182
- probe = probe.return;
1183
- }
1184
- return match;
1185
- }
1186
- function resolveTouchTarget(nativeTag) {
1187
- const candidates = collectCandidates();
1188
- if (candidates.length === 0) return null;
1189
- const byFiber = new Map();
1190
- for (const c of candidates) byFiber.set(c.fiber, c);
1191
-
1192
- // Fast path: the touched host IS a candidate's host.
1193
- const direct = candidates.find(c => c.nativeTag === nativeTag);
1194
-
1195
- // Otherwise find the touched fiber and walk up to the nearest candidate. The
1196
- // touch lands on the deepest host view (the <Text> inside the button), so the
1197
- // interesting element is almost always an ancestor.
1198
- let touched = null;
1199
- if (!direct) {
1200
- for (const root of getAllFiberRoots()) {
1201
- traverseFiberTree(root.current, fiber => {
1202
- if (touched) return;
1203
- if (fiber.stateNode && getNativeTag(fiber.stateNode) === nativeTag) {
1204
- touched = fiber;
1205
- }
1206
- });
1207
- if (touched) break;
1208
- }
1209
- }
1210
- let match = direct ?? null;
1211
- if (!match && touched) {
1212
- let cur = touched;
1213
- let depth = 0;
1214
- // Prefer an ancestor that is actually drivable; fall back to the nearest
1215
- // named one so an unlabeled tap still records something a human can read.
1216
- let named = null;
1217
- while (cur && depth < 25) {
1218
- const c = byFiber.get(cur);
1219
- if (c) {
1220
- if (c.press || c.valueControl || c.textControl) {
1221
- match = c;
1222
- break;
1223
- }
1224
- if (!named) named = c;
1225
- }
1226
- cur = cur.return;
1227
- depth++;
1228
- }
1229
- if (!match) match = named;
1230
- }
1231
- if (!match) return null;
1232
- match = preferHandlerOwner(match, byFiber);
1233
- if (!match.press && !match.valueControl && !match.textControl) return null;
1234
- // Buoy's own chrome is not part of the app under test. Without this, the tap
1235
- // that stops a recording is recorded as its final step.
1236
- if (isBuoyChrome(match.fiber)) return null;
1237
- const control = match.valueControl ? match.valueControl.kind : match.textControl ? "text" : "press";
1238
- return {
1239
- nativeTag: match.nativeTag,
1240
- name: match.name,
1241
- role: match.role,
1242
- testID: match.testID,
1243
- label: match.label,
1244
- text: match.text,
1245
- control,
1246
- value: match.valueControl ? match.valueControl.current : match.textControl ? match.textControl.current : undefined
1247
- };
1248
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.describeScreen=describeScreen;exports.resolveTouchTarget=resolveTouchTarget;exports.tapElement=tapElement;exports.waitFor=waitFor;var _utils=require("@buoy-gg/shared-ui/utils");var _reactNative=require("react-native");var _tapMatch=require("./tapMatch");const HostComponent=5;const OffscreenComponent=22;const INTERNAL_NAMES=new Set(["View","RCTView","ScrollView","RCTScrollView","RCTScrollContentView","SafeAreaView","RCTSafeAreaView","AndroidHorizontalScrollContentView","KeyboardAvoidingView","Pressable","PlatformPressable","TouchableOpacity","TouchableHighlight","TouchableWithoutFeedback","TouchableNativeFeedback","AnimatedComponent","Wrapper"]);function getReactDevToolsHook(){if(typeof global==="undefined")return null;return global.__REACT_DEVTOOLS_GLOBAL_HOOK__??null}let commitCount=0;let commitCounterInstalled=false;function ensureCommitCounter(){if(commitCounterInstalled)return;const e=getReactDevToolsHook();if(!e)return;const t=e.onCommitFiberRoot;e.onCommitFiberRoot=function(...n){commitCount++;return typeof t==="function"?t.apply(e,n):void 0};commitCounterInstalled=true}const TAP_SETTLE_MS=250;function makeChromeCheck(){const e=new Map;return t=>{const n=[];let r=t;let a=0;let s=false;while(r&&a<400){const u=e.get(r);if(u!==void 0){s=u;break}if(getProps(r)?.__buoyDevToolsRoot){s=true;e.set(r,true);break}n.push(r);r=r.return;a++}for(const u of n)e.set(u,s);return s}}function getAllFiberRoots(){const e=getReactDevToolsHook();if(!e)return[];const t=[];try{if(e.renderers&&typeof e.renderers.forEach==="function"){e.renderers.forEach((n,r)=>{try{const a=e.getFiberRoots?.(r);if(a)a.forEach(s=>t.push(s))}catch{}})}if(t.length===0&&e.getFiberRoots){const n=e.getFiberRoots(1);if(n)n.forEach(r=>t.push(r))}}catch{}return t}function isHiddenOffscreen(e){return e.tag===OffscreenComponent&&e.memoizedState!==null}function isInactiveScreen(e){const t=e.memoizedProps?.activityState;if(t===0||t===1)return true;if(e.memoizedProps?.isPageFocused===false)return true;return false}function traverseFiberTree(e,t,n=0,r=new Set){if(!e||r.has(e)||n>500)return;r.add(e);if(isHiddenOffscreen(e)||isInactiveScreen(e)){if(e.sibling)traverseFiberTree(e.sibling,t,n,r);return}t(e);if(e.child)traverseFiberTree(e.child,t,n+1,r);if(e.sibling)traverseFiberTree(e.sibling,t,n,r)}function getPublicInstance(e){const t=(0,_utils.getDOMPublicInstance)(e);if(t)return t;if(!e)return null;const n=e;if(n.canonical?.publicInstance)return n.canonical.publicInstance;if(n.canonical&&typeof n.canonical.measure==="function"){return n.canonical}if(typeof n.measure==="function"||typeof n.measureInWindow==="function"){return n}return null}function getNativeTag(e){const t=(0,_utils.getDOMNodeHandle)(e);if(t!==null)return t;if(e==null)return null;const n=e;if(typeof n.__nativeTag==="number")return n.__nativeTag;if(typeof n._nativeTag==="number")return n._nativeTag;if(typeof n.nativeTag==="number")return n.nativeTag;if(n.canonical){if(typeof n.canonical.__nativeTag==="number")return n.canonical.__nativeTag;if(typeof n.canonical._nativeTag==="number")return n.canonical._nativeTag;if(typeof n.canonical.nativeTag==="number")return n.canonical.nativeTag}return null}function findHostInstance(e){const t=getPublicInstance(e.stateNode);if(t){return{instance:t,nativeTag:getNativeTag(e.stateNode),stateNode:e.stateNode}}const n=[];if(e.child)n.push(e.child);let r=0;while(n.length>0&&r<4e3){r++;const a=n.pop();if(!a)continue;const s=getPublicInstance(a.stateNode);if(s)return{instance:s,nativeTag:getNativeTag(a.stateNode),stateNode:a.stateNode};if(a.child)n.push(a.child);if(a.sibling)n.push(a.sibling)}return null}function findScrollAncestor(e){let t=e;let n=0;while(t&&n++<200){const r=t.stateNode;if(r&&typeof r.scrollTo==="function"){const a=typeof r.getNativeScrollRef==="function"&&r.getNativeScrollRef()||null;const s=typeof r.getInnerViewNode==="function"&&r.getInnerViewNode()||typeof r.getScrollableNode==="function"&&r.getScrollableNode()||r;return{instance:r,nativeRef:a,innerNode:s}}t=t.return}return null}async function scrollIntoView(e,t,n,r){try{if((0,_utils.isDOMElement)(n)){n.scrollIntoView({block:"nearest",inline:"nearest",behavior:"instant"});await new Promise(l=>setTimeout(l,16));return true}const a=findScrollAncestor(e);if(!a)return false;const s=t;let u=null;const o=l=>new Promise(i=>{let f=false;const h=y=>{if(f)return;f=true;i(y)};const g=setTimeout(()=>h(null),600);try{l(y=>{clearTimeout(g);h(y)})}catch{clearTimeout(g);h(null)}});if(a.nativeRef&&typeof s.measureLayout==="function"){u=await o(l=>s.measureLayout(a.nativeRef,(i,f)=>l(f),()=>l(null)))}if(u==null){const l=(0,_reactNative.findNodeHandle)(t);const i=typeof a.innerNode==="number"?a.innerNode:(0,_reactNative.findNodeHandle)(a.innerNode);if(l!=null&&i!=null){u=await o(f=>_reactNative.UIManager.measureLayout(l,i,()=>f(null),(h,g)=>f(g)))}}if(u==null)return false;a.instance.scrollTo({y:Math.max(0,u-r),animated:false});await new Promise(l=>setTimeout(l,120));return true}catch{return false}}function getComponentName(e){const t=e?.type;if(!t)return null;if(typeof t==="string")return t;return t.displayName||t.name||null}function getOwningComponentName(e){let t=e?._debugOwner||e?.return;let n=0;while(t&&n<30){n++;const r=getComponentName(t);if(r&&!INTERNAL_NAMES.has(r)&&typeof t.type!=="string"){return r}t=t.return}return null}function getProps(e){return(0,_utils.getDOMControlProps)(e?.stateNode,e?.memoizedProps??e?.pendingProps??null)}function flattenText(e,t=0){if(typeof e==="string")return e;if(typeof e==="number"&&Number.isFinite(e))return String(e);if(Array.isArray(e)&&t<4){const n=[];for(const r of e){const a=flattenText(r,t+1);if(a)n.push(a)}return n.length>0?n.join(""):void 0}return void 0}function normalizeVisibleText(e){return e.replace(/\s+/g," ").trim()}function getVisibleText(e){if(!e)return void 0;const t=n=>{const r=flattenText(n);if(r===void 0)return void 0;const a=normalizeVisibleText(r);return a?a.slice(0,80):void 0};return t(e.children)??t(e.title)??t(e.label)??t(e.placeholder)??void 0}function collectDescendantText(e,t){const n=[];const r=(s,u)=>{if(!s||u>t||n.length>=4)return;if(s!==e){const l=getProps(s);if(l&&(typeof l.onPress==="function"||typeof l.onLongPress==="function"))return}const o=getVisibleText(getProps(s));if(o&&!n.includes(o))n.push(o);if(s.child)r(s.child,u+1);if(s!==e&&s.sibling)r(s.sibling,u)};if(e.child)r(e.child,1);const a=n.join(" ").trim();return a?a.slice(0,80):void 0}function findPressHandler(e){let t=e;let n=0;let r=null;let a=null;while(t&&n<25&&!(r&&a)){const s=getProps(t);if(s){if(!r&&typeof s.onPress==="function")r=s.onPress;if(!a&&typeof s.onLongPress==="function")a=s.onLongPress}t=t.return;n++}if(r)return{handler:r,kind:"onPress",longPress:a??void 0};return a?{handler:a,kind:"onLongPress"}:null}function measureInstance(e){return new Promise(t=>{let n=false;const r=a=>{if(n)return;n=true;t(a)};setTimeout(()=>r(null),1e3);try{if(typeof e.measureInWindow==="function"){e.measureInWindow((a,s,u,o)=>{if(u>0&&o>0)r({x:a,y:s,width:u,height:o});else r(null)})}else if(typeof e.measure==="function"){e.measure((a,s,u,o,l,i)=>{if(u>0&&o>0&&l!=null&&i!=null){r({x:l,y:i,width:u,height:o})}else{r(null)}})}else{r(null)}}catch{r(null)}})}const SPINNER_HOSTS=new Set(["RCTActivityIndicatorView","AndroidProgressBar"]);function collectCandidates(){const e=getAllFiberRoots();if(e.length===0)return[];const t=[];const n=new Map;const r=new Map;const a=makeChromeCheck();for(const s of e){traverseFiberTree(s.current,u=>{const o=getProps(u);if(!o)return;const l=typeof o.onPress==="function"||typeof o.onLongPress==="function";const i=typeof o.onValueChange==="function";const f=typeof o.onChangeText==="function";const h=o.testID||void 0;const g=o.accessibilityLabel||void 0;const y=o.accessibilityRole||o.role||void 0;let p=getVisibleText(o);const b=y==="button"||y==="link"||y==="menuitem"||y==="tab"||y==="switch"||y==="checkbox";const I=typeof u.type==="string"&&SPINNER_HOSTS.has(u.type)&&o.animating!==false;if(!l&&!i&&!f&&!h&&!g&&!p&&!b&&!I){return}let v=null;let c=null;let d=null;if(i){const _=o.value;const S=typeof _==="number"||typeof o.minimumValue==="number";c={handler:o.onValueChange,kind:S?"slider":"toggle",current:_,min:typeof o.minimumValue==="number"?o.minimumValue:void 0,max:typeof o.maximumValue==="number"?o.maximumValue:void 0}}else if(f){d={handler:o.onChangeText,current:typeof o.value==="string"?o.value:void 0}}else{v=findPressHandler(u)}const w=getComponentName(u);let P=w??"Unknown";if(!w||INTERNAL_NAMES.has(w)||typeof u.type==="string"){P=getOwningComponentName(u)??w??"Unknown"}const T=findHostInstance(u);if(!T&&!h)return;if(!p&&!g&&l){p=collectDescendantText(u,6)}const x=T?.instance??null;const C=T?.nativeTag??null;const m=(x?n.get(x):void 0)??(C!=null?r.get(C):void 0);if(m){if(!m.press&&v)m.press=v;else if(m.press&&v?.longPress&&!m.press.longPress){m.press.longPress=v.longPress}if(!m.valueControl&&c)m.valueControl=c;if(!m.textControl&&d)m.textControl=d;if(!m.testID&&h)m.testID=h;if(!m.label&&g)m.label=g;if(!m.text&&p)m.text=p;if(x&&!n.has(x))n.set(x,m);if(C!=null&&!r.has(C))r.set(C,m);return}const D={nativeTag:C,name:P,role:y??(I?"progressbar":void 0),testID:h,label:g,text:p,instance:x,stateNode:T?.stateNode??null,fiber:u,press:v,valueControl:c,textControl:d,rect:null,buoy:a(u)};t.push(D);if(x)n.set(x,D);if(C!=null)r.set(C,D)})}return t}function explicitlyTargets(e,t){if(t.nativeTag!=null)return e.nativeTag===t.nativeTag;if(t.testID&&e.testID)return e.testID.toLowerCase()===t.testID.toLowerCase();return false}function appCandidates(e){const t=collectCandidates();if(e.includeBuoy)return t;return t.filter(n=>!n.buoy||explicitlyTargets(n,e))}async function measureCandidates(e){await Promise.all(e.map(async t=>{if(t.instance)t.rect=await measureInstance(t.instance)}))}async function describeScreen(e={}){const t=collectCandidates();const n=e.includeBuoy?t:t.filter(l=>!l.buoy);const r=t.length-n.length;await measureCandidates(n);const a=_reactNative.Dimensions.get("window");const s=Math.round(a.width)||1;const u=Math.round(a.height)||1;const o=[];for(const l of n){const i=l.rect;if(!i&&!l.testID)continue;let f;let h;if(i){const p=i.x+i.width/2;const b=i.y+i.height/2;if(b<-u*.1||b>u*1.1||p<-s*.1||p>s*1.1){continue}f={x:round2(p/s),y:round2(b/u)};h={x:round2(i.x/s),y:round2(i.y/u),width:round2(i.width/s),height:round2(i.height/u)}}const g=l.valueControl?l.valueControl.kind:l.textControl?"text":void 0;const y=l.valueControl?l.valueControl.current:l.textControl?.current;o.push({nativeTag:l.nativeTag,name:l.name,role:l.role,testID:l.testID,label:l.label,text:l.text,interactive:!!(l.press||l.valueControl||l.textControl),longPressable:l.press&&(l.press.kind==="onLongPress"||l.press.longPress)?true:void 0,control:g,value:y,tap:f,frame:h})}o.sort((l,i)=>{const f=l.tap?.y??2;const h=i.tap?.y??2;if(Math.abs(f-h)>.02)return f-h;return(l.tap?.x??2)-(i.tap?.x??2)});return{screen:{width:s,height:u},count:o.length,elements:o,...r>0?{hiddenBuoy:r}:{}}}function round2(e){return Math.round(e*100)/100}function fieldsOf(e){return{nativeTag:e.nativeTag,testID:e.testID,label:e.label,text:e.text,name:e.name}}function matchScore(e,t){return(0,_tapMatch.scoreTapMatch)(fieldsOf(e),t)}function tapPoint(e,t,n){if(!e||!t||!n)return void 0;return{x:(e.x+e.width/2)/t,y:(e.y+e.height/2)/n}}function makeSyntheticPressEvent(e){const t=e?e.x+e.width/2:0;const n=e?e.y+e.height/2:0;const r={locationX:e?e.width/2:0,locationY:e?e.height/2:0,pageX:t,pageY:n,timestamp:0,target:null,identifier:1,touches:[],changedTouches:[]};return{nativeEvent:r,persist:()=>{},preventDefault:()=>{},stopPropagation:()=>{},isDefaultPrevented:()=>false,isPropagationStopped:()=>false,target:null,currentTarget:null,type:"press",timeStamp:0}}async function tapElement(e){const t=appCandidates(e);if(t.length===0){return{tapped:false,reason:"No React fiber roots / elements found on screen."}}const n=t.map(c=>({c,score:matchScore(c,e)})).filter(c=>c.score>0).sort((c,d)=>d.score-c.score);const r=c=>!!(c.press||c.valueControl||c.textControl);if(n.length===0){return{tapped:false,reason:"No element matched. Call describe_screen to see available elements (match by testID, text, or name).",candidates:t.filter(r).slice(0,12).map(c=>({nativeTag:c.nativeTag,name:c.name,testID:c.testID,text:c.text}))}}const a=_reactNative.Dimensions.get("window");const s=a.width||1;const u=a.height||1;const o=n.slice(0,15);await Promise.all(o.map(async c=>{if(c.c.instance&&!c.c.rect)c.c.rect=await measureInstance(c.c.instance)}));for(const c of o){c.score=(0,_tapMatch.scoreTapMatch)({...fieldsOf(c.c),tap:tapPoint(c.c.rect,s,u)},e)}o.sort((c,d)=>d.score-c.score);const l=o.find(c=>r(c.c))??n.find(c=>r(c.c))??o[0];const i=l.c;const f=(c,d)=>c.press&&c.press.handler===d.press?.handler||c.valueControl&&c.valueControl.handler===d.valueControl?.handler||c.textControl&&c.textControl.handler===d.textControl?.handler;const h=o.filter(c=>c!==l&&r(c.c)&&c.score===l.score).filter(c=>!f(c.c,i)).filter((c,d,w)=>w.findIndex(P=>f(P.c,c.c))===d).slice(0,3).map(c=>({nativeTag:c.c.nativeTag,name:c.c.name,testID:c.c.testID,text:c.c.text,tap:tapPoint(c.c.rect,s,u)}));if(!r(i)){return{tapped:false,reason:`Matched "${i.name}"${i.text?` ("${i.text}")`:""} but it has no onPress / onValueChange / onChangeText handler (e.g. a react-native-gesture-handler GestureDetector). This element can't be driven in JS.`,matched:{nativeTag:i.nativeTag,name:i.name,testID:i.testID,label:i.label,text:i.text,via:"onPress"}}}if(i.instance&&!i.rect)i.rect=await measureInstance(i.instance);let g=false;const y=i.rect==null||!(0,_tapMatch.isRectCenterOnScreen)(i.rect,s,u);if(y&&e.scrollIntoView!==false&&i.instance){g=await scrollIntoView(i.fiber,i.instance,i.stateNode,80);if(g)i.rect=await measureInstance(i.instance)}const p={nativeTag:i.nativeTag,name:i.name,testID:i.testID,label:i.label,text:i.text};ensureCommitCounter();const b=commitCount;const I=Date.now();const v=async c=>{await new Promise(d=>setTimeout(d,TAP_SETTLE_MS));return{tapped:true,scrolled:g,matched:c,effect:{commits:commitCount-b,observedMs:Date.now()-I},...h.length?{alsoMatched:h}:{}}};try{if(i.press){if(e.longPress){const d=i.press.kind==="onLongPress"?i.press.handler:i.press.longPress;if(!d){return{tapped:false,scrolled:g,reason:`Matched "${i.name}"${i.text?` ("${i.text}")`:""} but it has no onLongPress handler \u2014 call tap_element without longPress to tap it normally.`,matched:{...p,via:"onPress"}}}d(makeSyntheticPressEvent(i.rect));return await v({...p,via:"onLongPress"})}i.press.handler(makeSyntheticPressEvent(i.rect));return await v({...p,via:i.press.kind})}if(i.valueControl){const d=i.valueControl;let w;if(d.kind==="toggle"){w=typeof e.value==="boolean"?e.value:!d.current}else{const P=d.min??0;const T=d.max??1;if(typeof e.value==="number"){w=Math.min(T,Math.max(P,e.value))}else{w=d.current===T?P:T}}d.handler(w);return await v({...p,via:"onValueChange",value:w})}const c=e.text??(typeof e.value==="string"?e.value:"");i.textControl.handler(c);return await v({...p,via:"onChangeText",value:c})}catch(c){return{tapped:false,scrolled:g,reason:`handler threw: ${c instanceof Error?c.message:String(c)}`,matched:{...p,via:i.press?i.press.kind:i.valueControl?"onValueChange":"onChangeText"}}}}const WAIT_DEFAULT_MS=5e3;const WAIT_MAX_MS=3e4;const WAIT_MIN_POLL_MS=50;const WAIT_DEFAULT_POLL_MS=150;async function waitFor(e){const t=e.nativeTag!=null||!!String(e.testID??"").trim()||!!String(e.query??"").trim();if(!t){return{ok:false,waitedMs:0,polls:0,reason:"Nothing to wait for \u2014 pass nativeTag, testID or query."}}const n=Math.min(Math.max(e.timeoutMs??WAIT_DEFAULT_MS,0),WAIT_MAX_MS);const r=Math.max(e.pollMs??WAIT_DEFAULT_POLL_MS,WAIT_MIN_POLL_MS);const a=e.gone===true;const s=Date.now();let u=0;for(;;){u+=1;const{hit:o,closest:l}=await findOnScreen(e);if(a?!o:!!o){return{ok:true,waitedMs:Date.now()-s,polls:u,matched:o??void 0}}if(Date.now()-s>=n){const i=l?.length?l:void 0;const f=a?`Still on screen after ${n}ms.`:i?`Not on screen after ${n}ms \u2014 ${i.length===1?"one element":`${i.length} elements`} matched but ${i.some(h=>h.onScreen)?"not well enough":"none is visible"}. See \`closest\`; do not retry with different words, call describeScreen.`:`Not on screen after ${n}ms, and nothing matched those words at all. Call describeScreen to see what is there.`;return{ok:false,waitedMs:Date.now()-s,polls:u,reason:f,matched:o??void 0,closest:i}}await new Promise(i=>setTimeout(i,r))}}async function findOnScreen(e){const t=appCandidates(e).map(o=>({c:o,score:matchScore(o,e)})).filter(o=>o.score>0).sort((o,l)=>l.score-o.score).slice(0,8);if(t.length===0)return{hit:null,closest:void 0};await measureCandidates(t.map(o=>o.c));const n=_reactNative.Dimensions.get("window");const r=Math.round(n.width)||1;const a=Math.round(n.height)||1;let s=null;for(const{c:o}of t){if(!(0,_tapMatch.isRectCenterOnScreen)(o.rect,r,a))continue;s={nativeTag:o.nativeTag,name:o.name,testID:o.testID,label:o.label,text:o.text,interactive:!!(o.press||o.valueControl||o.textControl)};break}const u=t.slice(0,3).map(({c:o})=>({name:o.name,testID:o.testID,text:o.text,onScreen:(0,_tapMatch.isRectCenterOnScreen)(o.rect,r,a)}));return{hit:s,closest:u}}function isBuoyChrome(e){let t=e;let n=0;while(t&&n<400){if(getProps(t)?.__buoyDevToolsRoot)return true;t=t.return;n++}return false}function preferHandlerOwner(e,t){const n=getProps(e.fiber);if(typeof n?.onPress==="function"||typeof n?.onValueChange==="function"||typeof n?.onChangeText==="function"){return e}let r=null;let a=e.fiber?.return;let s=0;while(a&&s<25){if(typeof getProps(a)?.onPress==="function"){r=a;break}a=a.return;s++}if(!r)return e;let u=r;for(let o=0;o<6&&u;o++){const l=t.get(u);if(l)return l;u=u.return}return e}function resolveTouchTarget(e){const t=collectCandidates();if(t.length===0)return null;const n=new Map;for(const o of t)n.set(o.fiber,o);const r=t.find(o=>o.nativeTag===e);let a=null;if(!r){for(const o of getAllFiberRoots()){traverseFiberTree(o.current,l=>{if(a)return;if(l.stateNode&&getNativeTag(l.stateNode)===e){a=l}});if(a)break}}let s=r??null;if(!s&&a){let o=a;let l=0;let i=null;while(o&&l<25){const f=n.get(o);if(f){if(f.press||f.valueControl||f.textControl){s=f;break}if(!i)i=f}o=o.return;l++}if(!s)s=i}if(!s)return null;s=preferHandlerOwner(s,n);if(!s.press&&!s.valueControl&&!s.textControl)return null;if(isBuoyChrome(s.fiber))return null;const u=s.valueControl?s.valueControl.kind:s.textControl?"text":"press";return{nativeTag:s.nativeTag,name:s.name,role:s.role,testID:s.testID,label:s.label,text:s.text,control:u,value:s.valueControl?s.valueControl.current:s.textControl?s.textControl.current:void 0}}