@buoy-gg/highlight-updates 7.0.35 → 7.0.38

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 (107) hide show
  1. package/README.md +28 -6
  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 -466
  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 +1 -359
  18. package/lib/commonjs/highlight-updates/utils/ProfilerInterceptor.js +1 -371
  19. package/lib/commonjs/highlight-updates/utils/RenderCauseDetector.js +1 -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/renderExportFormatter.js +1 -58
  28. package/lib/commonjs/highlight-updates/utils/rendersExportFormatter.js +1 -485
  29. package/lib/commonjs/highlight-updates/utils/screenElements.js +1 -1046
  30. package/lib/commonjs/highlight-updates/utils/tapMatch.js +1 -58
  31. package/lib/commonjs/highlight-updates/utils/testIDs.js +1 -68
  32. package/lib/commonjs/highlight-updates/utils/touchCapture.js +1 -277
  33. package/lib/commonjs/index.js +1 -429
  34. package/lib/commonjs/preset.js +1 -278
  35. package/lib/commonjs/sync/highlightUpdatesSyncAdapter.js +1 -235
  36. package/lib/module/highlight-updates/HighlightUpdatesOverlay.js +1 -274
  37. package/lib/module/highlight-updates/components/HighlightFilterView.js +1 -1468
  38. package/lib/module/highlight-updates/components/HighlightUpdatesModal.js +1 -634
  39. package/lib/module/highlight-updates/components/IdentifierBadge.js +1 -265
  40. package/lib/module/highlight-updates/components/IsolatedRenderList.js +1 -183
  41. package/lib/module/highlight-updates/components/ModalHeaderContent.js +1 -288
  42. package/lib/module/highlight-updates/components/RenderCauseBadge.js +1 -499
  43. package/lib/module/highlight-updates/components/RenderDetailView.js +1 -840
  44. package/lib/module/highlight-updates/components/RenderHistoryViewer.js +1 -908
  45. package/lib/module/highlight-updates/components/RenderListItem.js +1 -230
  46. package/lib/module/highlight-updates/components/RendersCopySettingsView.js +1 -462
  47. package/lib/module/highlight-updates/components/StatsDisplay.js +1 -70
  48. package/lib/module/highlight-updates/types/copySettings.js +1 -102
  49. package/lib/module/highlight-updates/utils/CommitProfiler.js +1 -442
  50. package/lib/module/highlight-updates/utils/HighlightUpdatesController.js +1 -1916
  51. package/lib/module/highlight-updates/utils/PerformanceLogger.js +1 -353
  52. package/lib/module/highlight-updates/utils/ProfilerInterceptor.js +1 -358
  53. package/lib/module/highlight-updates/utils/RenderCauseDetector.js +1 -1830
  54. package/lib/module/highlight-updates/utils/RenderTracker.js +1 -949
  55. package/lib/module/highlight-updates/utils/ViewTypeMapper.js +1 -255
  56. package/lib/module/highlight-updates/utils/copySettingsStorage.js +1 -26
  57. package/lib/module/highlight-updates/utils/devtoolsComponentNames.js +1 -178
  58. package/lib/module/highlight-updates/utils/namedComposite.js +1 -36
  59. package/lib/module/highlight-updates/utils/ownStateChanged.js +1 -98
  60. package/lib/module/highlight-updates/utils/rawEventEmitter.js +1 -46
  61. package/lib/module/highlight-updates/utils/renderExportFormatter.js +1 -54
  62. package/lib/module/highlight-updates/utils/rendersExportFormatter.js +1 -478
  63. package/lib/module/highlight-updates/utils/screenElements.js +1 -1040
  64. package/lib/module/highlight-updates/utils/tapMatch.js +1 -52
  65. package/lib/module/highlight-updates/utils/testIDs.js +1 -60
  66. package/lib/module/highlight-updates/utils/touchCapture.js +1 -270
  67. package/lib/module/index.js +1 -130
  68. package/lib/module/preset.js +1 -272
  69. package/lib/module/sync/highlightUpdatesSyncAdapter.js +1 -230
  70. package/lib/typescript/highlight-updates/utils/screenElements.d.ts +52 -1
  71. package/lib/typescript/highlight-updates/utils/tapMatch.d.ts +11 -0
  72. package/lib/typescript/sync/highlightUpdatesSyncAdapter.d.ts +1 -1
  73. package/package.json +8 -8
  74. package/lib/typescript/highlight-updates/HighlightUpdatesOverlay.d.ts.map +0 -1
  75. package/lib/typescript/highlight-updates/components/HighlightFilterView.d.ts.map +0 -1
  76. package/lib/typescript/highlight-updates/components/HighlightUpdatesModal.d.ts.map +0 -1
  77. package/lib/typescript/highlight-updates/components/IdentifierBadge.d.ts.map +0 -1
  78. package/lib/typescript/highlight-updates/components/IsolatedRenderList.d.ts.map +0 -1
  79. package/lib/typescript/highlight-updates/components/ModalHeaderContent.d.ts.map +0 -1
  80. package/lib/typescript/highlight-updates/components/RenderCauseBadge.d.ts.map +0 -1
  81. package/lib/typescript/highlight-updates/components/RenderDetailView.d.ts.map +0 -1
  82. package/lib/typescript/highlight-updates/components/RenderHistoryViewer.d.ts.map +0 -1
  83. package/lib/typescript/highlight-updates/components/RenderListItem.d.ts.map +0 -1
  84. package/lib/typescript/highlight-updates/components/RendersCopySettingsView.d.ts.map +0 -1
  85. package/lib/typescript/highlight-updates/components/StatsDisplay.d.ts.map +0 -1
  86. package/lib/typescript/highlight-updates/types/copySettings.d.ts.map +0 -1
  87. package/lib/typescript/highlight-updates/utils/CommitProfiler.d.ts.map +0 -1
  88. package/lib/typescript/highlight-updates/utils/HighlightUpdatesController.d.ts.map +0 -1
  89. package/lib/typescript/highlight-updates/utils/PerformanceLogger.d.ts.map +0 -1
  90. package/lib/typescript/highlight-updates/utils/ProfilerInterceptor.d.ts.map +0 -1
  91. package/lib/typescript/highlight-updates/utils/RenderCauseDetector.d.ts.map +0 -1
  92. package/lib/typescript/highlight-updates/utils/RenderTracker.d.ts.map +0 -1
  93. package/lib/typescript/highlight-updates/utils/ViewTypeMapper.d.ts.map +0 -1
  94. package/lib/typescript/highlight-updates/utils/copySettingsStorage.d.ts.map +0 -1
  95. package/lib/typescript/highlight-updates/utils/devtoolsComponentNames.d.ts.map +0 -1
  96. package/lib/typescript/highlight-updates/utils/namedComposite.d.ts.map +0 -1
  97. package/lib/typescript/highlight-updates/utils/ownStateChanged.d.ts.map +0 -1
  98. package/lib/typescript/highlight-updates/utils/rawEventEmitter.d.ts.map +0 -1
  99. package/lib/typescript/highlight-updates/utils/renderExportFormatter.d.ts.map +0 -1
  100. package/lib/typescript/highlight-updates/utils/rendersExportFormatter.d.ts.map +0 -1
  101. package/lib/typescript/highlight-updates/utils/screenElements.d.ts.map +0 -1
  102. package/lib/typescript/highlight-updates/utils/tapMatch.d.ts.map +0 -1
  103. package/lib/typescript/highlight-updates/utils/testIDs.d.ts.map +0 -1
  104. package/lib/typescript/highlight-updates/utils/touchCapture.d.ts.map +0 -1
  105. package/lib/typescript/index.d.ts.map +0 -1
  106. package/lib/typescript/preset.d.ts.map +0 -1
  107. package/lib/typescript/sync/highlightUpdatesSyncAdapter.d.ts.map +0 -1
@@ -1,1046 +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
- * Collect fiber roots across ALL renderers. A single renderer id is not safe
51
- * to assume: secondary reconcilers (react-native-skia, react-native-svg, …)
52
- * register their own renderer, sometimes first as id 1, whose roots contain
53
- * none of the tappable app UI.
54
- */
55
- function getAllFiberRoots() {
56
- const hook = getReactDevToolsHook();
57
- if (!hook) return [];
58
- const roots = [];
59
- try {
60
- if (hook.renderers && typeof hook.renderers.forEach === "function") {
61
- hook.renderers.forEach((_r, rendererId) => {
62
- try {
63
- const set = hook.getFiberRoots?.(rendererId);
64
- if (set) set.forEach(root => roots.push(root));
65
- } catch {
66
- /* ignore a single renderer failing */
67
- }
68
- });
69
- }
70
- if (roots.length === 0 && hook.getFiberRoots) {
71
- const legacy = hook.getFiberRoots(1);
72
- if (legacy) legacy.forEach(root => roots.push(root));
73
- }
74
- } catch {
75
- /* ignore */
76
- }
77
- return roots;
78
- }
79
- function isHiddenOffscreen(fiber) {
80
- return fiber.tag === OffscreenComponent && fiber.memoizedState !== null;
81
- }
82
- function isInactiveScreen(fiber) {
83
- // react-native-screens: activityState 0/1 = inactive/transitioning.
84
- const as = fiber.memoizedProps?.activityState;
85
- if (as === 0 || as === 1) return true;
86
- // pagers
87
- if (fiber.memoizedProps?.isPageFocused === false) return true;
88
- return false;
89
- }
90
- function traverseFiberTree(fiber, callback, depth = 0, visited = new Set()) {
91
- if (!fiber || visited.has(fiber) || depth > 500) return;
92
- visited.add(fiber);
93
- if (isHiddenOffscreen(fiber) || isInactiveScreen(fiber)) {
94
- // Prune the whole subtree, but keep walking siblings.
95
- if (fiber.sibling) traverseFiberTree(fiber.sibling, callback, depth, visited);
96
- return;
97
- }
98
- callback(fiber);
99
- if (fiber.child) traverseFiberTree(fiber.child, callback, depth + 1, visited);
100
- if (fiber.sibling) traverseFiberTree(fiber.sibling, callback, depth, visited);
101
- }
102
-
103
- // --- Fiber → host instance / name / props --------------------------------
104
-
105
- /** Get a measurable public instance from a host fiber's stateNode. */
106
- function getPublicInstance(stateNode) {
107
- if (!stateNode) return null;
108
- const node = stateNode;
109
- if (node.canonical?.publicInstance) return node.canonical.publicInstance;
110
- if (node.canonical && typeof node.canonical.measure === "function") {
111
- return node.canonical;
112
- }
113
- if (typeof node.measure === "function" || typeof node.measureInWindow === "function") {
114
- return node;
115
- }
116
- return null;
117
- }
118
- function getNativeTag(stateNode) {
119
- if (stateNode == null) return null;
120
- const n = stateNode;
121
- if (typeof n.__nativeTag === "number") return n.__nativeTag;
122
- if (typeof n._nativeTag === "number") return n._nativeTag;
123
- if (typeof n.nativeTag === "number") return n.nativeTag;
124
- if (n.canonical) {
125
- if (typeof n.canonical.__nativeTag === "number") return n.canonical.__nativeTag;
126
- if (typeof n.canonical._nativeTag === "number") return n.canonical._nativeTag;
127
- if (typeof n.canonical.nativeTag === "number") return n.canonical.nativeTag;
128
- }
129
- return null;
130
- }
131
-
132
- /** The first host node in a fiber's own subtree (self first, then descendants). */
133
- function findHostInstance(fiber) {
134
- const selfInst = getPublicInstance(fiber.stateNode);
135
- if (selfInst) {
136
- return {
137
- instance: selfInst,
138
- nativeTag: getNativeTag(fiber.stateNode),
139
- stateNode: fiber.stateNode
140
- };
141
- }
142
-
143
- // DFS restricted to this fiber's subtree (child + siblings-of-children).
144
- const stack = [];
145
- if (fiber.child) stack.push(fiber.child);
146
- let guard = 0;
147
- while (stack.length > 0 && guard < 4000) {
148
- guard++;
149
- const f = stack.pop();
150
- if (!f) continue;
151
- const inst = getPublicInstance(f.stateNode);
152
- if (inst) return {
153
- instance: inst,
154
- nativeTag: getNativeTag(f.stateNode),
155
- stateNode: f.stateNode
156
- };
157
- if (f.child) stack.push(f.child);
158
- if (f.sibling) stack.push(f.sibling);
159
- }
160
- return null;
161
- }
162
-
163
- /**
164
- * Walk up from a fiber to the nearest ScrollView-like instance (exposes
165
- * scrollTo). Mirrors HighlightUpdatesController.findScrollAncestor.
166
- */
167
- function findScrollAncestor(fiber) {
168
- let cur = fiber;
169
- let guard = 0;
170
- while (cur && guard++ < 200) {
171
- const inst = cur.stateNode;
172
- if (inst && typeof inst.scrollTo === "function") {
173
- const nativeRef = typeof inst.getNativeScrollRef === "function" && inst.getNativeScrollRef() || null;
174
- const innerNode = typeof inst.getInnerViewNode === "function" && inst.getInnerViewNode() || typeof inst.getScrollableNode === "function" && inst.getScrollableNode() || inst;
175
- return {
176
- instance: inst,
177
- nativeRef,
178
- innerNode
179
- };
180
- }
181
- cur = cur.return;
182
- }
183
- return null;
184
- }
185
-
186
- /**
187
- * Best-effort scroll of an ancestor ScrollView so the target sits near the top
188
- * of its viewport. Returns true only if a scroll was issued. Never throws.
189
- * Ported from HighlightUpdatesController.scrollDescendantIntoView.
190
- */
191
- async function scrollIntoView(targetFiber, publicInstance, stateNode, margin) {
192
- try {
193
- const ancestor = findScrollAncestor(targetFiber);
194
- if (!ancestor) return false;
195
- const inst = publicInstance;
196
- let top = null;
197
-
198
- // measureLayout can invoke NEITHER callback on some architectures — race
199
- // every call against a short timeout so the await can't hang.
200
- const withTimeout = run => new Promise(resolve => {
201
- let done = false;
202
- const settle = v => {
203
- if (done) return;
204
- done = true;
205
- resolve(v);
206
- };
207
- const timer = setTimeout(() => settle(null), 600);
208
- try {
209
- run(v => {
210
- clearTimeout(timer);
211
- settle(v);
212
- });
213
- } catch {
214
- clearTimeout(timer);
215
- settle(null);
216
- }
217
- });
218
-
219
- // Fabric: ref.measureLayout against the inner view ref.
220
- if (ancestor.nativeRef && typeof inst.measureLayout === "function") {
221
- top = await withTimeout(resolve => inst.measureLayout(ancestor.nativeRef, (_left, t) => resolve(t), () => resolve(null)));
222
- }
223
-
224
- // Legacy: UIManager.measureLayout with numeric node handles.
225
- if (top == null) {
226
- const targetHandle = (0, _reactNative.findNodeHandle)(publicInstance);
227
- const innerHandle = typeof ancestor.innerNode === "number" ? ancestor.innerNode : (0, _reactNative.findNodeHandle)(ancestor.innerNode);
228
- if (targetHandle != null && innerHandle != null) {
229
- top = await withTimeout(resolve => _reactNative.UIManager.measureLayout(targetHandle, innerHandle, () => resolve(null), (_left, t) => resolve(t)));
230
- }
231
- }
232
- if (top == null) return false;
233
- ancestor.instance.scrollTo({
234
- y: Math.max(0, top - margin),
235
- animated: false
236
- });
237
- await new Promise(r => setTimeout(r, 120)); // let scroll + layout settle
238
- return true;
239
- } catch {
240
- return false;
241
- }
242
- }
243
- function getComponentName(fiber) {
244
- const t = fiber?.type;
245
- if (!t) return null;
246
- if (typeof t === "string") return t;
247
- return t.displayName || t.name || null;
248
- }
249
-
250
- /** Walk up to the nearest user-defined component name (skips host/internal). */
251
- function getOwningComponentName(fiber) {
252
- let cur = fiber?._debugOwner || fiber?.return;
253
- let depth = 0;
254
- while (cur && depth < 30) {
255
- depth++;
256
- const name = getComponentName(cur);
257
- if (name && !INTERNAL_NAMES.has(name) && typeof cur.type !== "string") {
258
- return name;
259
- }
260
- cur = cur.return;
261
- }
262
- return null;
263
- }
264
- function getProps(fiber) {
265
- return fiber?.memoizedProps ?? fiber?.pendingProps ?? null;
266
- }
267
- function getVisibleText(props) {
268
- if (!props) return undefined;
269
- const pick = v => {
270
- if (typeof v === "string") return v.trim() ? v.trim().slice(0, 80) : undefined;
271
- // Numeric children are common (counters, badges) — coerce them too.
272
- if (typeof v === "number" && Number.isFinite(v)) return String(v);
273
- return undefined;
274
- };
275
- return pick(props.children) ?? pick(props.title) ?? pick(props.label) ?? pick(props.placeholder) ?? undefined;
276
- }
277
-
278
- /**
279
- * Gather visible text from a fiber's descendants (its own label), stopping at
280
- * nested interactive subtrees so a button doesn't absorb another button's text.
281
- * Lets an icon-button whose label is a child <Text> (e.g. a dial icon) still be
282
- * matched/shown by that label — the button host measures even when the inner
283
- * Text host doesn't.
284
- */
285
- function collectDescendantText(root, maxDepth) {
286
- const parts = [];
287
- const visit = (f, depth) => {
288
- if (!f || depth > maxDepth || parts.length >= 4) return;
289
- // Don't cross into a nested interactive element (another button's label).
290
- if (f !== root) {
291
- const p = getProps(f);
292
- if (p && (typeof p.onPress === "function" || typeof p.onLongPress === "function")) return;
293
- }
294
- const t = getVisibleText(getProps(f));
295
- if (t && !parts.includes(t)) parts.push(t);
296
- if (f.child) visit(f.child, depth + 1);
297
- if (f !== root && f.sibling) visit(f.sibling, depth); // siblings stay in subtree
298
- };
299
- if (root.child) visit(root.child, 1);
300
- const joined = parts.join(" ").trim();
301
- return joined ? joined.slice(0, 80) : undefined;
302
- }
303
-
304
- /** Nearest onPress / onLongPress handlers at or above a fiber. */
305
-
306
- function findPressHandler(fiber) {
307
- let cur = fiber;
308
- let depth = 0;
309
- let press = null;
310
- let longPress = null;
311
- while (cur && depth < 25 && !(press && longPress)) {
312
- const p = getProps(cur);
313
- if (p) {
314
- if (!press && typeof p.onPress === "function") press = p.onPress;
315
- if (!longPress && typeof p.onLongPress === "function") longPress = p.onLongPress;
316
- }
317
- cur = cur.return;
318
- depth++;
319
- }
320
- if (press) return {
321
- handler: press,
322
- kind: "onPress",
323
- longPress: longPress ?? undefined
324
- };
325
- return longPress ? {
326
- handler: longPress,
327
- kind: "onLongPress"
328
- } : null;
329
- }
330
-
331
- // --- Measurement ----------------------------------------------------------
332
-
333
- /** Absolute on-screen rect in points (or null), with a hang guard. */
334
- function measureInstance(instance) {
335
- return new Promise(resolve => {
336
- let settled = false;
337
- const done = r => {
338
- if (settled) return;
339
- settled = true;
340
- resolve(r);
341
- };
342
- // Detached / off-screen nodes can fire neither callback.
343
- setTimeout(() => done(null), 1000);
344
- try {
345
- if (typeof instance.measureInWindow === "function") {
346
- instance.measureInWindow((x, y, width, height) => {
347
- if (width > 0 && height > 0) done({
348
- x,
349
- y,
350
- width,
351
- height
352
- });else done(null);
353
- });
354
- } else if (typeof instance.measure === "function") {
355
- instance.measure((_x, _y, width, height, pageX, pageY) => {
356
- if (width > 0 && height > 0 && pageX != null && pageY != null) {
357
- done({
358
- x: pageX,
359
- y: pageY,
360
- width,
361
- height
362
- });
363
- } else {
364
- done(null);
365
- }
366
- });
367
- } else {
368
- done(null);
369
- }
370
- } catch {
371
- done(null);
372
- }
373
- });
374
- }
375
-
376
- // --- Public types ---------------------------------------------------------
377
-
378
- // Internal record carries the live fiber refs that `describeScreen`'s
379
- // serializable result drops.
380
-
381
- // --- Collection -----------------------------------------------------------
382
-
383
- function collectCandidates() {
384
- const roots = getAllFiberRoots();
385
- if (roots.length === 0) return [];
386
- const out = [];
387
- const seenInstances = new Set();
388
- for (const root of roots) {
389
- traverseFiberTree(root.current, fiber => {
390
- const props = getProps(fiber);
391
- if (!props) return;
392
- const hasPress = typeof props.onPress === "function" || typeof props.onLongPress === "function";
393
- const hasValue = typeof props.onValueChange === "function";
394
- const hasText = typeof props.onChangeText === "function";
395
- const testID = props.testID || undefined;
396
- const label = props.accessibilityLabel || undefined;
397
- const role = props.accessibilityRole || props.role || undefined;
398
- let text = getVisibleText(props);
399
- const roleInteractive = role === "button" || role === "link" || role === "menuitem" || role === "tab" || role === "switch" || role === "checkbox";
400
-
401
- // A fiber earns a row only if it carries a real signal.
402
- if (!hasPress && !hasValue && !hasText && !testID && !label && !text && !roleInteractive) {
403
- return;
404
- }
405
-
406
- // Resolve how this element is driven. A value/text control is a leaf
407
- // (Switch/Slider/TextInput) whose OWN handler wins over an ancestor
408
- // onPress; otherwise fall back to the nearest onPress up the tree.
409
- let press = null;
410
- let valueControl = null;
411
- let textControl = null;
412
- if (hasValue) {
413
- const current = props.value;
414
- const numeric = typeof current === "number" || typeof props.minimumValue === "number";
415
- valueControl = {
416
- handler: props.onValueChange,
417
- kind: numeric ? "slider" : "toggle",
418
- current,
419
- min: typeof props.minimumValue === "number" ? props.minimumValue : undefined,
420
- max: typeof props.maximumValue === "number" ? props.maximumValue : undefined
421
- };
422
- } else if (hasText) {
423
- textControl = {
424
- handler: props.onChangeText,
425
- current: typeof props.value === "string" ? props.value : undefined
426
- };
427
- } else {
428
- press = findPressHandler(fiber);
429
- }
430
- const rawName = getComponentName(fiber);
431
- // Prefer a user component name over a bare host/internal wrapper name.
432
- let name = rawName ?? "Unknown";
433
- if (!rawName || INTERNAL_NAMES.has(rawName) || typeof fiber.type === "string") {
434
- name = getOwningComponentName(fiber) ?? rawName ?? "Unknown";
435
- }
436
- const host = findHostInstance(fiber);
437
- if (!host && !testID) return; // unmeasurable & unnamed → skip
438
-
439
- // Interactive element with no own text/label (icon button, dial icon) —
440
- // adopt a descendant <Text> as its label so it's matchable by that text.
441
- if (!text && !label && hasPress) {
442
- text = collectDescendantText(fiber, 6);
443
- }
444
- const instance = host?.instance ?? null;
445
- // De-dup: two fibers resolving to the same host instance (e.g. Pressable
446
- // and its inner View) collapse to one row — keep the more interactive.
447
- if (instance && seenInstances.has(instance)) {
448
- const existing = out.find(c => c.instance === instance);
449
- if (existing) {
450
- if (!existing.press && press) existing.press = press;else if (existing.press && press?.longPress && !existing.press.longPress) {
451
- existing.press.longPress = press.longPress;
452
- }
453
- if (!existing.valueControl && valueControl) existing.valueControl = valueControl;
454
- if (!existing.textControl && textControl) existing.textControl = textControl;
455
- if (!existing.testID && testID) existing.testID = testID;
456
- if (!existing.label && label) existing.label = label;
457
- if (!existing.text && text) existing.text = text;
458
- return;
459
- }
460
- }
461
- if (instance) seenInstances.add(instance);
462
- out.push({
463
- nativeTag: host?.nativeTag ?? null,
464
- name,
465
- role,
466
- testID,
467
- label,
468
- text,
469
- instance,
470
- stateNode: host?.stateNode ?? null,
471
- fiber,
472
- press,
473
- valueControl,
474
- textControl,
475
- rect: null
476
- });
477
- });
478
- }
479
- return out;
480
- }
481
- async function measureCandidates(candidates) {
482
- await Promise.all(candidates.map(async c => {
483
- if (c.instance) c.rect = await measureInstance(c.instance);
484
- }));
485
- }
486
-
487
- // --- describeScreen -------------------------------------------------------
488
-
489
- async function describeScreen() {
490
- const candidates = collectCandidates();
491
- await measureCandidates(candidates);
492
- const win = _reactNative.Dimensions.get("window");
493
- const screenW = Math.round(win.width) || 1;
494
- const screenH = Math.round(win.height) || 1;
495
- const elements = [];
496
- for (const c of candidates) {
497
- const r = c.rect;
498
- // Keep on-screen elements; drop rect-less rows unless they carry a testID.
499
- if (!r && !c.testID) continue;
500
- let tap;
501
- let frame;
502
- if (r) {
503
- const cx = r.x + r.width / 2;
504
- const cy = r.y + r.height / 2;
505
- // Drop previous Expo Router screens (centers well outside the window).
506
- // Keep a 10% slack so the floating menu / home-indicator-clipped rows stay.
507
- if (cy < -screenH * 0.1 || cy > screenH * 1.1 || cx < -screenW * 0.1 || cx > screenW * 1.1) {
508
- continue;
509
- }
510
- tap = {
511
- x: round2(cx / screenW),
512
- y: round2(cy / screenH)
513
- };
514
- frame = {
515
- x: round2(r.x / screenW),
516
- y: round2(r.y / screenH),
517
- width: round2(r.width / screenW),
518
- height: round2(r.height / screenH)
519
- };
520
- }
521
- const control = c.valueControl ? c.valueControl.kind : c.textControl ? "text" : undefined;
522
- const value = c.valueControl ? c.valueControl.current : c.textControl?.current;
523
- elements.push({
524
- nativeTag: c.nativeTag,
525
- name: c.name,
526
- role: c.role,
527
- testID: c.testID,
528
- label: c.label,
529
- text: c.text,
530
- interactive: !!(c.press || c.valueControl || c.textControl),
531
- longPressable: c.press && (c.press.kind === "onLongPress" || c.press.longPress) ? true : undefined,
532
- control,
533
- value,
534
- tap,
535
- frame
536
- });
537
- }
538
-
539
- // Reading order: top-to-bottom, then left-to-right.
540
- elements.sort((a, b) => {
541
- const ay = a.tap?.y ?? 2;
542
- const by = b.tap?.y ?? 2;
543
- if (Math.abs(ay - by) > 0.02) return ay - by;
544
- return (a.tap?.x ?? 2) - (b.tap?.x ?? 2);
545
- });
546
- return {
547
- screen: {
548
- width: screenW,
549
- height: screenH
550
- },
551
- count: elements.length,
552
- elements
553
- };
554
- }
555
- function round2(n) {
556
- return Math.round(n * 100) / 100;
557
- }
558
-
559
- // --- tapElement -----------------------------------------------------------
560
-
561
- function fieldsOf(c) {
562
- return {
563
- nativeTag: c.nativeTag,
564
- testID: c.testID,
565
- label: c.label,
566
- text: c.text,
567
- name: c.name
568
- };
569
- }
570
- function matchScore(c, params) {
571
- return (0, _tapMatch.scoreTapMatch)(fieldsOf(c), params);
572
- }
573
- function tapPoint(rect, screenW, screenH) {
574
- if (!rect || !screenW || !screenH) return undefined;
575
- return {
576
- x: (rect.x + rect.width / 2) / screenW,
577
- y: (rect.y + rect.height / 2) / screenH
578
- };
579
- }
580
- function makeSyntheticPressEvent(rect) {
581
- const pageX = rect ? rect.x + rect.width / 2 : 0;
582
- const pageY = rect ? rect.y + rect.height / 2 : 0;
583
- const nativeEvent = {
584
- locationX: rect ? rect.width / 2 : 0,
585
- locationY: rect ? rect.height / 2 : 0,
586
- pageX,
587
- pageY,
588
- timestamp: 0,
589
- target: null,
590
- identifier: 1,
591
- touches: [],
592
- changedTouches: []
593
- };
594
- return {
595
- nativeEvent,
596
- persist: () => {},
597
- preventDefault: () => {},
598
- stopPropagation: () => {},
599
- isDefaultPrevented: () => false,
600
- isPropagationStopped: () => false,
601
- target: null,
602
- currentTarget: null,
603
- type: "press",
604
- timeStamp: 0
605
- };
606
- }
607
- async function tapElement(params) {
608
- const candidates = collectCandidates();
609
- if (candidates.length === 0) {
610
- return {
611
- tapped: false,
612
- reason: "No React fiber roots / elements found on screen."
613
- };
614
- }
615
-
616
- // Rank; only elements with a reachable press handler are tappable.
617
- const ranked = candidates.map(c => ({
618
- c,
619
- score: matchScore(c, params)
620
- })).filter(r => r.score > 0).sort((a, b) => b.score - a.score);
621
- const isInteractive = c => !!(c.press || c.valueControl || c.textControl);
622
- if (ranked.length === 0) {
623
- return {
624
- tapped: false,
625
- reason: "No element matched. Call describe_screen to see available elements (match by testID, text, or name).",
626
- candidates: candidates.filter(isInteractive).slice(0, 12).map(c => ({
627
- nativeTag: c.nativeTag,
628
- name: c.name,
629
- testID: c.testID,
630
- text: c.text
631
- }))
632
- };
633
- }
634
-
635
- // Measure the top matches so an on-screen DialIcon beats an off-screen nav
636
- // chip whose testID happens to contain the same substring.
637
- const win = _reactNative.Dimensions.get("window");
638
- const screenW = win.width || 1;
639
- const screenH = win.height || 1;
640
- const top = ranked.slice(0, 15);
641
- await Promise.all(top.map(async r => {
642
- if (r.c.instance && !r.c.rect) r.c.rect = await measureInstance(r.c.instance);
643
- }));
644
- for (const r of top) {
645
- r.score = (0, _tapMatch.scoreTapMatch)({
646
- ...fieldsOf(r.c),
647
- tap: tapPoint(r.c.rect, screenW, screenH)
648
- }, params);
649
- }
650
- top.sort((a, b) => b.score - a.score);
651
-
652
- // Fall back to the FULL ranked list for an interactive match — the top-15
653
- // re-score is only a tiebreak, and on a busy screen the pressable match can
654
- // sit below 15 text-score peers.
655
- const target = top.find(r => isInteractive(r.c)) ?? ranked.find(r => isInteractive(r.c)) ?? top[0];
656
- const c = target.c;
657
- if (!isInteractive(c)) {
658
- return {
659
- tapped: false,
660
- 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.`,
661
- matched: {
662
- nativeTag: c.nativeTag,
663
- name: c.name,
664
- testID: c.testID,
665
- label: c.label,
666
- text: c.text,
667
- via: "onPress"
668
- }
669
- };
670
- }
671
-
672
- // Measure the target so we can decide whether to scroll it on-screen (and,
673
- // for a press, give the synthetic event real coordinates).
674
- if (c.instance && !c.rect) c.rect = await measureInstance(c.instance);
675
-
676
- // Bring it into view if it's off-screen and scrolling is wanted. The action
677
- // doesn't NEED this (handlers are invoked directly), but it keeps the element
678
- // visible for the human watching / a follow-up describe.
679
- let scrolled = false;
680
- // A failed measure (rect == null) counts as off-screen: the node may be in
681
- // a virtualized list that hasn't materialized it, and scrolling can help.
682
- const offScreen = c.rect == null || !(0, _tapMatch.isRectCenterOnScreen)(c.rect, screenW, screenH);
683
- if (offScreen && params.scrollIntoView !== false && c.instance) {
684
- scrolled = await scrollIntoView(c.fiber, c.instance, c.stateNode, 80);
685
- if (scrolled) c.rect = await measureInstance(c.instance); // fresh coords
686
- }
687
- const base = {
688
- nativeTag: c.nativeTag,
689
- name: c.name,
690
- testID: c.testID,
691
- label: c.label,
692
- text: c.text
693
- };
694
- try {
695
- if (c.press) {
696
- if (params.longPress) {
697
- const lp = c.press.kind === "onLongPress" ? c.press.handler : c.press.longPress;
698
- if (!lp) {
699
- return {
700
- tapped: false,
701
- scrolled,
702
- reason: `Matched "${c.name}"${c.text ? ` ("${c.text}")` : ""} but it has no onLongPress handler — call tap_element without longPress to tap it normally.`,
703
- matched: {
704
- ...base,
705
- via: "onPress"
706
- }
707
- };
708
- }
709
- lp(makeSyntheticPressEvent(c.rect));
710
- return {
711
- tapped: true,
712
- scrolled,
713
- matched: {
714
- ...base,
715
- via: "onLongPress"
716
- }
717
- };
718
- }
719
- c.press.handler(makeSyntheticPressEvent(c.rect));
720
- return {
721
- tapped: true,
722
- scrolled,
723
- matched: {
724
- ...base,
725
- via: c.press.kind
726
- }
727
- };
728
- }
729
- if (c.valueControl) {
730
- const vc = c.valueControl;
731
- let next;
732
- if (vc.kind === "toggle") {
733
- next = typeof params.value === "boolean" ? params.value : !vc.current;
734
- } else {
735
- const min = vc.min ?? 0;
736
- const max = vc.max ?? 1;
737
- if (typeof params.value === "number") {
738
- next = Math.min(max, Math.max(min, params.value));
739
- } else {
740
- // No target given → move to the far end so the change is visible.
741
- next = vc.current === max ? min : max;
742
- }
743
- }
744
- vc.handler(next);
745
- return {
746
- tapped: true,
747
- scrolled,
748
- matched: {
749
- ...base,
750
- via: "onValueChange",
751
- value: next
752
- }
753
- };
754
- }
755
- // textControl
756
- const next = params.text ?? (typeof params.value === "string" ? params.value : "");
757
- c.textControl.handler(next);
758
- return {
759
- tapped: true,
760
- scrolled,
761
- matched: {
762
- ...base,
763
- via: "onChangeText",
764
- value: next
765
- }
766
- };
767
- } catch (e) {
768
- return {
769
- tapped: false,
770
- scrolled,
771
- reason: `handler threw: ${e instanceof Error ? e.message : String(e)}`,
772
- matched: {
773
- ...base,
774
- via: c.press ? c.press.kind : c.valueControl ? "onValueChange" : "onChangeText"
775
- }
776
- };
777
- }
778
- }
779
-
780
- // --- waitFor --------------------------------------------------------------
781
-
782
- const WAIT_DEFAULT_MS = 5_000;
783
- const WAIT_MAX_MS = 30_000;
784
- const WAIT_MIN_POLL_MS = 50;
785
- const WAIT_DEFAULT_POLL_MS = 150;
786
-
787
- /**
788
- * Block until an element appears (or disappears), then report how long it took.
789
- *
790
- * WHY THIS EXISTS. `navigate` returns the moment the route is pushed, not when
791
- * the new screen has data on it, so "go to /shop then tap Fire Grill" reliably
792
- * tapped a loading skeleton. A remote driver solves that with a wait step
793
- * (MCP's `run_flow` has `waitFor`/`waitMs`); anything driving the tools from
794
- * INSIDE the app had nothing, and its only option was to re-run
795
- * `describeScreen` in a loop — which for an agent means burning turns against a
796
- * step budget, and for a Scenario means a fixed sleep that is either flaky or
797
- * slow.
798
- *
799
- * MATCHING IS `tapElement`'S, EXACTLY. Same `scoreTapMatch`, same fields, so
800
- * "wait for X" and "tap X" can never disagree about what X is. A wait that
801
- * resolves on an element the tap then cannot find is worse than no wait.
802
- *
803
- * PRESENCE MEANS ON SCREEN. An identity scan alone would resolve against a
804
- * previous Expo Router screen still mounted behind the current one — the exact
805
- * false positive `describeScreen` drops by rect. So a candidate that matches by
806
- * name is measured before it counts, and only the matches are measured: the
807
- * per-poll cost stays one fiber walk plus a handful of `measureInWindow`
808
- * calls, not a full `describeScreen`.
809
- *
810
- * The poll floor is deliberate. This runs on the JS thread the app is drawing
811
- * on, and a caller asking for `pollMs: 0` would starve the very render it is
812
- * waiting for.
813
- */
814
- async function waitFor(params) {
815
- const hasTarget = params.nativeTag != null || !!params.testID?.trim() || !!params.query?.trim();
816
- if (!hasTarget) {
817
- return {
818
- ok: false,
819
- waitedMs: 0,
820
- polls: 0,
821
- reason: "Nothing to wait for — pass nativeTag, testID or query."
822
- };
823
- }
824
- const timeoutMs = Math.min(Math.max(params.timeoutMs ?? WAIT_DEFAULT_MS, 0), WAIT_MAX_MS);
825
- const pollMs = Math.max(params.pollMs ?? WAIT_DEFAULT_POLL_MS, WAIT_MIN_POLL_MS);
826
- const wantGone = params.gone === true;
827
- const started = Date.now();
828
- let polls = 0;
829
- for (;;) {
830
- polls += 1;
831
- const hit = await findOnScreen(params);
832
- if (wantGone ? !hit : !!hit) {
833
- return {
834
- ok: true,
835
- waitedMs: Date.now() - started,
836
- polls,
837
- matched: hit ?? undefined
838
- };
839
- }
840
- if (Date.now() - started >= timeoutMs) {
841
- return {
842
- ok: false,
843
- waitedMs: Date.now() - started,
844
- polls,
845
- reason: wantGone ? `Still on screen after ${timeoutMs}ms.` : `Not on screen after ${timeoutMs}ms. Call describeScreen to see what is.`,
846
- matched: hit ?? undefined
847
- };
848
- }
849
- await new Promise(resolve => setTimeout(resolve, pollMs));
850
- }
851
- }
852
-
853
- /** One scan: rank by identity, then confirm the best match is really visible. */
854
- async function findOnScreen(params) {
855
- const ranked = collectCandidates().map(c => ({
856
- c,
857
- score: matchScore(c, params)
858
- })).filter(r => r.score > 0).sort((a, b) => b.score - a.score)
859
- // Only the plausible few get measured; a broad query can match dozens.
860
- .slice(0, 8);
861
- if (ranked.length === 0) return null;
862
- await measureCandidates(ranked.map(r => r.c));
863
- const win = _reactNative.Dimensions.get("window");
864
- const screenW = Math.round(win.width) || 1;
865
- const screenH = Math.round(win.height) || 1;
866
- for (const {
867
- c
868
- } of ranked) {
869
- if (!(0, _tapMatch.isRectCenterOnScreen)(c.rect, screenW, screenH)) continue;
870
- return {
871
- nativeTag: c.nativeTag,
872
- name: c.name,
873
- testID: c.testID,
874
- label: c.label,
875
- text: c.text,
876
- interactive: !!(c.press || c.valueControl || c.textControl)
877
- };
878
- }
879
- return null;
880
- }
881
-
882
- // --- resolveTouchTarget ---------------------------------------------------
883
-
884
- /**
885
- * What a recorded touch landed on, in the vocabulary `tapElement` searches by.
886
- *
887
- * Deliberately the SAME fields `describeScreen` emits: a macro recorded from a
888
- * human's finger has to replay through `tapElement`, and the only way to
889
- * guarantee that is for the recorder to name the element exactly the way the
890
- * matcher will look for it. Anything richer here is a selector that records
891
- * fine and then can't be found again.
892
- */
893
-
894
- /**
895
- * Resolve the native tag from a raw touch event to the element a HUMAN would
896
- * say they tapped.
897
- *
898
- * Synchronous on purpose. The tag arrives on `topTouchStart`, and by the time
899
- * an async resolve came back the press handler may already have navigated —
900
- * leaving the recorder to name the element on the screen it moved TO. Everything
901
- * here is a fiber walk (no measure), which is what makes that affordable.
902
- *
903
- * Returns null when the tag belongs to nothing interesting (the background, a
904
- * scroll surface): the recorder treats that as "not a step", which is exactly
905
- * right — a tap on empty space is not something to replay.
906
- */
907
- /**
908
- * Is this fiber inside Buoy's own overlay?
909
- *
910
- * `FloatingDevTools` marks its single root View with `__buoyDevToolsRoot`, and
911
- * everything the tool draws — the bubble, the dial, every modal, every tool
912
- * overlay — is under it. Walking the whole chain (rather than a fixed depth) is
913
- * the point: a control deep inside a tool's modal is still the tool.
914
- */
915
- function isBuoyChrome(fiber) {
916
- let cur = fiber;
917
- let depth = 0;
918
- while (cur && depth < 400) {
919
- if (getProps(cur)?.__buoyDevToolsRoot) return true;
920
- cur = cur.return;
921
- depth++;
922
- }
923
- return false;
924
- }
925
-
926
- /**
927
- * Climb from the node that was touched to the element that OWNS the press.
928
- *
929
- * A finger lands on the deepest host view, which for a button is the `<Text>`
930
- * inside it. That Text is a legitimate candidate — `findPressHandler` walks up
931
- * and finds the button's `onPress`, so it looks drivable — but it carries the
932
- * TEXT's identity: no testID, and "visible text" that is whatever string
933
- * happens to be in that node.
934
- *
935
- * Left alone, every recorded step is named after a fragment of copy and
936
- * replayed by fuzzy-matching that fragment. In the Poké Mart flow, all six
937
- * steps came back as `Text` with no testID at all, so "tap the Charizard row"
938
- * was recorded as "The signature. Flame-broiled, wings on, worth the wait." —
939
- * the brittlest possible selector, chosen when `shop-item-charizard` was one
940
- * fiber up.
941
- *
942
- * So we walk to the fiber that DECLARES `onPress` and take the candidate there.
943
- * Stopping at the declaring fiber matters: keep climbing and a row inside a
944
- * pressable list resolves to the list.
945
- */
946
- function preferHandlerOwner(match, byFiber) {
947
- // Already the owner (or a Switch/TextInput, whose handler is its own).
948
- const own = getProps(match.fiber);
949
- if (typeof own?.onPress === "function" || typeof own?.onValueChange === "function" || typeof own?.onChangeText === "function") {
950
- return match;
951
- }
952
-
953
- // 1. Find the fiber that actually declares the handler.
954
- let declaring = null;
955
- let cur = match.fiber?.return;
956
- let depth = 0;
957
- while (cur && depth < 25) {
958
- if (typeof getProps(cur)?.onPress === "function") {
959
- declaring = cur;
960
- break;
961
- }
962
- cur = cur.return;
963
- depth++;
964
- }
965
- if (!declaring) return match;
966
-
967
- // 2. Take the candidate AT that fiber, or the first one above it.
968
- //
969
- // Stopping exactly at the declaring fiber isn't enough: a `<Pressable>`
970
- // declares onPress but is de-duped into the component that renders it
971
- // (`PrimaryButton`), and de-duping keeps the OUTERMOST fiber of everything
972
- // sharing a host instance — which is where the testID ends up. Climbing a
973
- // few levels past the handler is what turns "Text: Add to bag" into
974
- // "PrimaryButton [testID=shop-add-to-bag]". The bound keeps a row inside a
975
- // pressable list from resolving to the list.
976
- let probe = declaring;
977
- for (let i = 0; i < 6 && probe; i++) {
978
- const candidate = byFiber.get(probe);
979
- if (candidate) return candidate;
980
- probe = probe.return;
981
- }
982
- return match;
983
- }
984
- function resolveTouchTarget(nativeTag) {
985
- const candidates = collectCandidates();
986
- if (candidates.length === 0) return null;
987
- const byFiber = new Map();
988
- for (const c of candidates) byFiber.set(c.fiber, c);
989
-
990
- // Fast path: the touched host IS a candidate's host.
991
- const direct = candidates.find(c => c.nativeTag === nativeTag);
992
-
993
- // Otherwise find the touched fiber and walk up to the nearest candidate. The
994
- // touch lands on the deepest host view (the <Text> inside the button), so the
995
- // interesting element is almost always an ancestor.
996
- let touched = null;
997
- if (!direct) {
998
- for (const root of getAllFiberRoots()) {
999
- traverseFiberTree(root.current, fiber => {
1000
- if (touched) return;
1001
- if (fiber.stateNode && getNativeTag(fiber.stateNode) === nativeTag) {
1002
- touched = fiber;
1003
- }
1004
- });
1005
- if (touched) break;
1006
- }
1007
- }
1008
- let match = direct ?? null;
1009
- if (!match && touched) {
1010
- let cur = touched;
1011
- let depth = 0;
1012
- // Prefer an ancestor that is actually drivable; fall back to the nearest
1013
- // named one so an unlabeled tap still records something a human can read.
1014
- let named = null;
1015
- while (cur && depth < 25) {
1016
- const c = byFiber.get(cur);
1017
- if (c) {
1018
- if (c.press || c.valueControl || c.textControl) {
1019
- match = c;
1020
- break;
1021
- }
1022
- if (!named) named = c;
1023
- }
1024
- cur = cur.return;
1025
- depth++;
1026
- }
1027
- if (!match) match = named;
1028
- }
1029
- if (!match) return null;
1030
- match = preferHandlerOwner(match, byFiber);
1031
- if (!match.press && !match.valueControl && !match.textControl) return null;
1032
- // Buoy's own chrome is not part of the app under test. Without this, the tap
1033
- // that stops a recording is recorded as its final step.
1034
- if (isBuoyChrome(match.fiber)) return null;
1035
- const control = match.valueControl ? match.valueControl.kind : match.textControl ? "text" : "press";
1036
- return {
1037
- nativeTag: match.nativeTag,
1038
- name: match.name,
1039
- role: match.role,
1040
- testID: match.testID,
1041
- label: match.label,
1042
- text: match.text,
1043
- control,
1044
- value: match.valueControl ? match.valueControl.current : match.textControl ? match.textControl.current : undefined
1045
- };
1046
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.describeScreen=describeScreen,exports.resolveTouchTarget=resolveTouchTarget,exports.tapElement=tapElement,exports.waitFor=waitFor;var _reactNative=require("react-native"),_tapMatch=require("./tapMatch");const HostComponent=5,OffscreenComponent=22,INTERNAL_NAMES=new Set(["View","RCTView","ScrollView","RCTScrollView","RCTScrollContentView","SafeAreaView","RCTSafeAreaView","AndroidHorizontalScrollContentView","KeyboardAvoidingView","Pressable","PlatformPressable","TouchableOpacity","TouchableHighlight","TouchableWithoutFeedback","TouchableNativeFeedback","AnimatedComponent","Wrapper"]);function getReactDevToolsHook(){return"undefined"==typeof global?null:global.__REACT_DEVTOOLS_GLOBAL_HOOK__??null}let commitCount=0,commitCounterInstalled=!1;function ensureCommitCounter(){if(commitCounterInstalled)return;const e=getReactDevToolsHook();if(!e)return;const t=e.onCommitFiberRoot;e.onCommitFiberRoot=function(...n){return commitCount++,"function"==typeof t?t.apply(e,n):void 0},commitCounterInstalled=!0}const TAP_SETTLE_MS=250;function makeChromeCheck(){const e=new Map;return t=>{const n=[];let o=t,r=0,a=!1;for(;o&&r<400;){const t=e.get(o);if(void 0!==t){a=t;break}if(getProps(o)?.__buoyDevToolsRoot){a=!0,e.set(o,!0);break}n.push(o),o=o.return,r++}for(const t of n)e.set(t,a);return a}}function getAllFiberRoots(){const e=getReactDevToolsHook();if(!e)return[];const t=[];try{if(e.renderers&&"function"==typeof e.renderers.forEach&&e.renderers.forEach((n,o)=>{try{const n=e.getFiberRoots?.(o);n&&n.forEach(e=>t.push(e))}catch{}}),0===t.length&&e.getFiberRoots){const n=e.getFiberRoots(1);n&&n.forEach(e=>t.push(e))}}catch{}return t}function isHiddenOffscreen(e){return 22===e.tag&&null!==e.memoizedState}function isInactiveScreen(e){const t=e.memoizedProps?.activityState;return 0===t||1===t||!1===e.memoizedProps?.isPageFocused}function traverseFiberTree(e,t,n=0,o=new Set){!e||o.has(e)||n>500||(o.add(e),isHiddenOffscreen(e)||isInactiveScreen(e)||(t(e),e.child&&traverseFiberTree(e.child,t,n+1,o)),e.sibling&&traverseFiberTree(e.sibling,t,n,o))}function getPublicInstance(e){if(!e)return null;const t=e;return t.canonical?.publicInstance?t.canonical.publicInstance:t.canonical&&"function"==typeof t.canonical.measure?t.canonical:"function"==typeof t.measure||"function"==typeof t.measureInWindow?t:null}function getNativeTag(e){if(null==e)return null;const t=e;if("number"==typeof t.__nativeTag)return t.__nativeTag;if("number"==typeof t._nativeTag)return t._nativeTag;if("number"==typeof t.nativeTag)return t.nativeTag;if(t.canonical){if("number"==typeof t.canonical.__nativeTag)return t.canonical.__nativeTag;if("number"==typeof t.canonical._nativeTag)return t.canonical._nativeTag;if("number"==typeof t.canonical.nativeTag)return t.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=[];e.child&&n.push(e.child);let o=0;for(;n.length>0&&o<4e3;){o++;const e=n.pop();if(!e)continue;const t=getPublicInstance(e.stateNode);if(t)return{instance:t,nativeTag:getNativeTag(e.stateNode),stateNode:e.stateNode};e.child&&n.push(e.child),e.sibling&&n.push(e.sibling)}return null}function findScrollAncestor(e){let t=e,n=0;for(;t&&n++<200;){const e=t.stateNode;if(e&&"function"==typeof e.scrollTo){const t="function"==typeof e.getNativeScrollRef&&e.getNativeScrollRef()||null,n="function"==typeof e.getInnerViewNode&&e.getInnerViewNode()||"function"==typeof e.getScrollableNode&&e.getScrollableNode()||e;return{instance:e,nativeRef:t,innerNode:n}}t=t.return}return null}async function scrollIntoView(e,t,n,o){try{const n=findScrollAncestor(e);if(!n)return!1;const r=t;let a=null;const i=e=>new Promise(t=>{let n=!1;const o=e=>{n||(n=!0,t(e))},r=setTimeout(()=>o(null),600);try{e(e=>{clearTimeout(r),o(e)})}catch{clearTimeout(r),o(null)}});if(n.nativeRef&&"function"==typeof r.measureLayout&&(a=await i(e=>r.measureLayout(n.nativeRef,(t,n)=>e(n),()=>e(null)))),null==a){const e=(0,_reactNative.findNodeHandle)(t),o="number"==typeof n.innerNode?n.innerNode:(0,_reactNative.findNodeHandle)(n.innerNode);null!=e&&null!=o&&(a=await i(t=>_reactNative.UIManager.measureLayout(e,o,()=>t(null),(e,n)=>t(n))))}return null!=a&&(n.instance.scrollTo({y:Math.max(0,a-o),animated:!1}),await new Promise(e=>setTimeout(e,120)),!0)}catch{return!1}}function getComponentName(e){const t=e?.type;return t?"string"==typeof t?t:t.displayName||t.name||null:null}function getOwningComponentName(e){let t=e?._debugOwner||e?.return,n=0;for(;t&&n<30;){n++;const e=getComponentName(t);if(e&&!INTERNAL_NAMES.has(e)&&"string"!=typeof t.type)return e;t=t.return}return null}function getProps(e){return e?.memoizedProps??e?.pendingProps??null}function flattenText(e,t=0){if("string"==typeof e)return e;if("number"==typeof e&&Number.isFinite(e))return String(e);if(Array.isArray(e)&&t<4){const n=[];for(const o of e){const e=flattenText(o,t+1);e&&n.push(e)}return n.length>0?n.join(""):void 0}}function normalizeVisibleText(e){return e.replace(/\s+/g," ").trim()}function getVisibleText(e){if(!e)return;const t=e=>{const t=flattenText(e);if(void 0===t)return;const n=normalizeVisibleText(t);return n?n.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=[],o=(r,a)=>{if(!r||a>t||n.length>=4)return;if(r!==e){const e=getProps(r);if(e&&("function"==typeof e.onPress||"function"==typeof e.onLongPress))return}const i=getVisibleText(getProps(r));i&&!n.includes(i)&&n.push(i),r.child&&o(r.child,a+1),r!==e&&r.sibling&&o(r.sibling,a)};e.child&&o(e.child,1);const r=n.join(" ").trim();return r?r.slice(0,80):void 0}function findPressHandler(e){let t=e,n=0,o=null,r=null;for(;t&&n<25&&(!o||!r);){const e=getProps(t);e&&(o||"function"!=typeof e.onPress||(o=e.onPress),r||"function"!=typeof e.onLongPress||(r=e.onLongPress)),t=t.return,n++}return o?{handler:o,kind:"onPress",longPress:r??void 0}:r?{handler:r,kind:"onLongPress"}:null}function measureInstance(e){return new Promise(t=>{let n=!1;const o=e=>{n||(n=!0,t(e))};setTimeout(()=>o(null),1e3);try{"function"==typeof e.measureInWindow?e.measureInWindow((e,t,n,r)=>{o(n>0&&r>0?{x:e,y:t,width:n,height:r}:null)}):"function"==typeof e.measure?e.measure((e,t,n,r,a,i)=>{o(n>0&&r>0&&null!=a&&null!=i?{x:a,y:i,width:n,height:r}:null)}):o(null)}catch{o(null)}})}function collectCandidates(){const e=getAllFiberRoots();if(0===e.length)return[];const t=[],n=new Map,o=new Map,r=makeChromeCheck();for(const a of e)traverseFiberTree(a.current,e=>{const a=getProps(e);if(!a)return;const i="function"==typeof a.onPress||"function"==typeof a.onLongPress,s="function"==typeof a.onValueChange,l="function"==typeof a.onChangeText,c=a.testID||void 0,u=a.accessibilityLabel||void 0,f=a.accessibilityRole||a.role||void 0;let d=getVisibleText(a);if(!(i||s||l||c||u||d||"button"===f||"link"===f||"menuitem"===f||"tab"===f||"switch"===f||"checkbox"===f))return;let m=null,g=null,p=null;if(s){const e=a.value,t="number"==typeof e||"number"==typeof a.minimumValue;g={handler:a.onValueChange,kind:t?"slider":"toggle",current:e,min:"number"==typeof a.minimumValue?a.minimumValue:void 0,max:"number"==typeof a.maximumValue?a.maximumValue:void 0}}else l?p={handler:a.onChangeText,current:"string"==typeof a.value?a.value:void 0}:m=findPressHandler(e);const h=getComponentName(e);let v=h??"Unknown";h&&!INTERNAL_NAMES.has(h)&&"string"!=typeof e.type||(v=getOwningComponentName(e)??h??"Unknown");const T=findHostInstance(e);if(!T&&!c)return;d||u||!i||(d=collectDescendantText(e,6));const b=T?.instance??null,y=T?.nativeTag??null,C=(b?n.get(b):void 0)??(null!=y?o.get(y):void 0);if(C)return!C.press&&m?C.press=m:C.press&&m?.longPress&&!C.press.longPress&&(C.press.longPress=m.longPress),!C.valueControl&&g&&(C.valueControl=g),!C.textControl&&p&&(C.textControl=p),!C.testID&&c&&(C.testID=c),!C.label&&u&&(C.label=u),!C.text&&d&&(C.text=d),b&&!n.has(b)&&n.set(b,C),void(null==y||o.has(y)||o.set(y,C));const w={nativeTag:y,name:v,role:f,testID:c,label:u,text:d,instance:b,stateNode:T?.stateNode??null,fiber:e,press:m,valueControl:g,textControl:p,rect:null,buoy:r(e)};t.push(w),b&&n.set(b,w),null!=y&&o.set(y,w)});return t}function explicitlyTargets(e,t){return null!=t.nativeTag?e.nativeTag===t.nativeTag:!(!t.testID||!e.testID)&&e.testID.toLowerCase()===t.testID.toLowerCase()}function appCandidates(e){const t=collectCandidates();return e.includeBuoy?t:t.filter(t=>!t.buoy||explicitlyTargets(t,e))}async function measureCandidates(e){await Promise.all(e.map(async e=>{e.instance&&(e.rect=await measureInstance(e.instance))}))}async function describeScreen(e={}){const t=collectCandidates(),n=e.includeBuoy?t:t.filter(e=>!e.buoy),o=t.length-n.length;await measureCandidates(n);const r=_reactNative.Dimensions.get("window"),a=Math.round(r.width)||1,i=Math.round(r.height)||1,s=[];for(const e of n){const t=e.rect;if(!t&&!e.testID)continue;let n,o;if(t){const e=t.x+t.width/2,r=t.y+t.height/2;if(r<.1*-i||r>1.1*i||e<.1*-a||e>1.1*a)continue;n={x:round2(e/a),y:round2(r/i)},o={x:round2(t.x/a),y:round2(t.y/i),width:round2(t.width/a),height:round2(t.height/i)}}const r=e.valueControl?e.valueControl.kind:e.textControl?"text":void 0,l=e.valueControl?e.valueControl.current:e.textControl?.current;s.push({nativeTag:e.nativeTag,name:e.name,role:e.role,testID:e.testID,label:e.label,text:e.text,interactive:!!(e.press||e.valueControl||e.textControl),longPressable:!(!e.press||"onLongPress"!==e.press.kind&&!e.press.longPress)||void 0,control:r,value:l,tap:n,frame:o})}return s.sort((e,t)=>{const n=e.tap?.y??2,o=t.tap?.y??2;return Math.abs(n-o)>.02?n-o:(e.tap?.x??2)-(t.tap?.x??2)}),{screen:{width:a,height:i},count:s.length,elements:s,...o>0?{hiddenBuoy:o}:{}}}function round2(e){return Math.round(100*e)/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{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,n=e?e.y+e.height/2:0;return{nativeEvent:{locationX:e?e.width/2:0,locationY:e?e.height/2:0,pageX:t,pageY:n,timestamp:0,target:null,identifier:1,touches:[],changedTouches:[]},persist:()=>{},preventDefault:()=>{},stopPropagation:()=>{},isDefaultPrevented:()=>!1,isPropagationStopped:()=>!1,target:null,currentTarget:null,type:"press",timeStamp:0}}async function tapElement(e){const t=appCandidates(e);if(0===t.length)return{tapped:!1,reason:"No React fiber roots / elements found on screen."};const n=t.map(t=>({c:t,score:matchScore(t,e)})).filter(e=>e.score>0).sort((e,t)=>t.score-e.score),o=e=>!!(e.press||e.valueControl||e.textControl);if(0===n.length)return{tapped:!1,reason:"No element matched. Call describe_screen to see available elements (match by testID, text, or name).",candidates:t.filter(o).slice(0,12).map(e=>({nativeTag:e.nativeTag,name:e.name,testID:e.testID,text:e.text}))};const r=_reactNative.Dimensions.get("window"),a=r.width||1,i=r.height||1,s=n.slice(0,15);await Promise.all(s.map(async e=>{e.c.instance&&!e.c.rect&&(e.c.rect=await measureInstance(e.c.instance))}));for(const t of s)t.score=(0,_tapMatch.scoreTapMatch)({...fieldsOf(t.c),tap:tapPoint(t.c.rect,a,i)},e);s.sort((e,t)=>t.score-e.score);const l=s.find(e=>o(e.c))??n.find(e=>o(e.c))??s[0],c=l.c,u=(e,t)=>e.press&&e.press.handler===t.press?.handler||e.valueControl&&e.valueControl.handler===t.valueControl?.handler||e.textControl&&e.textControl.handler===t.textControl?.handler,f=s.filter(e=>e!==l&&o(e.c)&&e.score===l.score).filter(e=>!u(e.c,c)).filter((e,t,n)=>n.findIndex(t=>u(t.c,e.c))===t).slice(0,3).map(e=>({nativeTag:e.c.nativeTag,name:e.c.name,testID:e.c.testID,text:e.c.text,tap:tapPoint(e.c.rect,a,i)}));if(!o(c))return{tapped:!1,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.`,matched:{nativeTag:c.nativeTag,name:c.name,testID:c.testID,label:c.label,text:c.text,via:"onPress"}};c.instance&&!c.rect&&(c.rect=await measureInstance(c.instance));let d=!1;(null==c.rect||!(0,_tapMatch.isRectCenterOnScreen)(c.rect,a,i))&&!1!==e.scrollIntoView&&c.instance&&(d=await scrollIntoView(c.fiber,c.instance,c.stateNode,80),d&&(c.rect=await measureInstance(c.instance)));const m={nativeTag:c.nativeTag,name:c.name,testID:c.testID,label:c.label,text:c.text};ensureCommitCounter();const g=commitCount,p=Date.now(),h=async e=>(await new Promise(e=>setTimeout(e,TAP_SETTLE_MS)),{tapped:!0,scrolled:d,matched:e,effect:{commits:commitCount-g,observedMs:Date.now()-p},...f.length?{alsoMatched:f}:{}});try{if(c.press){if(e.longPress){const e="onLongPress"===c.press.kind?c.press.handler:c.press.longPress;return e?(e(makeSyntheticPressEvent(c.rect)),await h({...m,via:"onLongPress"})):{tapped:!1,scrolled:d,reason:`Matched "${c.name}"${c.text?` ("${c.text}")`:""} but it has no onLongPress handler — call tap_element without longPress to tap it normally.`,matched:{...m,via:"onPress"}}}return c.press.handler(makeSyntheticPressEvent(c.rect)),await h({...m,via:c.press.kind})}if(c.valueControl){const t=c.valueControl;let n;if("toggle"===t.kind)n="boolean"==typeof e.value?e.value:!t.current;else{const o=t.min??0,r=t.max??1;n="number"==typeof e.value?Math.min(r,Math.max(o,e.value)):t.current===r?o:r}return t.handler(n),await h({...m,via:"onValueChange",value:n})}const t=e.text??("string"==typeof e.value?e.value:"");return c.textControl.handler(t),await h({...m,via:"onChangeText",value:t})}catch(e){return{tapped:!1,scrolled:d,reason:`handler threw: ${e instanceof Error?e.message:String(e)}`,matched:{...m,via:c.press?c.press.kind:c.valueControl?"onValueChange":"onChangeText"}}}}const WAIT_DEFAULT_MS=5e3,WAIT_MAX_MS=3e4,WAIT_MIN_POLL_MS=50,WAIT_DEFAULT_POLL_MS=150;async function waitFor(e){if(null==e.nativeTag&&!String(e.testID??"").trim()&&!String(e.query??"").trim())return{ok:!1,waitedMs:0,polls:0,reason:"Nothing to wait for — pass nativeTag, testID or query."};const t=Math.min(Math.max(e.timeoutMs??WAIT_DEFAULT_MS,0),WAIT_MAX_MS),n=Math.max(e.pollMs??WAIT_DEFAULT_POLL_MS,WAIT_MIN_POLL_MS),o=!0===e.gone,r=Date.now();let a=0;for(;;){a+=1;const{hit:i,closest:s}=await findOnScreen(e);if(o?!i:i)return{ok:!0,waitedMs:Date.now()-r,polls:a,matched:i??void 0};if(Date.now()-r>=t){const e=s?.length?s:void 0,n=o?`Still on screen after ${t}ms.`:e?`Not on screen after ${t}ms — ${1===e.length?"one element":`${e.length} elements`} matched but ${e.some(e=>e.onScreen)?"not well enough":"none is visible"}. See \`closest\`; do not retry with different words, call describeScreen.`:`Not on screen after ${t}ms, and nothing matched those words at all. Call describeScreen to see what is there.`;return{ok:!1,waitedMs:Date.now()-r,polls:a,reason:n,matched:i??void 0,closest:e}}await new Promise(e=>setTimeout(e,n))}}async function findOnScreen(e){const t=appCandidates(e).map(t=>({c:t,score:matchScore(t,e)})).filter(e=>e.score>0).sort((e,t)=>t.score-e.score).slice(0,8);if(0===t.length)return{hit:null,closest:void 0};await measureCandidates(t.map(e=>e.c));const n=_reactNative.Dimensions.get("window"),o=Math.round(n.width)||1,r=Math.round(n.height)||1;let a=null;for(const{c:e}of t)if((0,_tapMatch.isRectCenterOnScreen)(e.rect,o,r)){a={nativeTag:e.nativeTag,name:e.name,testID:e.testID,label:e.label,text:e.text,interactive:!!(e.press||e.valueControl||e.textControl)};break}return{hit:a,closest:t.slice(0,3).map(({c:e})=>({name:e.name,testID:e.testID,text:e.text,onScreen:(0,_tapMatch.isRectCenterOnScreen)(e.rect,o,r)}))}}function isBuoyChrome(e){let t=e,n=0;for(;t&&n<400;){if(getProps(t)?.__buoyDevToolsRoot)return!0;t=t.return,n++}return!1}function preferHandlerOwner(e,t){const n=getProps(e.fiber);if("function"==typeof n?.onPress||"function"==typeof n?.onValueChange||"function"==typeof n?.onChangeText)return e;let o=null,r=e.fiber?.return,a=0;for(;r&&a<25;){if("function"==typeof getProps(r)?.onPress){o=r;break}r=r.return,a++}if(!o)return e;let i=o;for(let e=0;e<6&&i;e++){const e=t.get(i);if(e)return e;i=i.return}return e}function resolveTouchTarget(e){const t=collectCandidates();if(0===t.length)return null;const n=new Map;for(const e of t)n.set(e.fiber,e);const o=t.find(t=>t.nativeTag===e);let r=null;if(!o)for(const t of getAllFiberRoots())if(traverseFiberTree(t.current,t=>{r||t.stateNode&&getNativeTag(t.stateNode)===e&&(r=t)}),r)break;let a=o??null;if(!a&&r){let e=r,t=0,o=null;for(;e&&t<25;){const r=n.get(e);if(r){if(r.press||r.valueControl||r.textControl){a=r;break}o||(o=r)}e=e.return,t++}a||(a=o)}if(!a)return null;if(a=preferHandlerOwner(a,n),!a.press&&!a.valueControl&&!a.textControl)return null;if(isBuoyChrome(a.fiber))return null;const i=a.valueControl?a.valueControl.kind:a.textControl?"text":"press";return{nativeTag:a.nativeTag,name:a.name,role:a.role,testID:a.testID,label:a.label,text:a.text,control:i,value:a.valueControl?a.valueControl.current:a.textControl?a.textControl.current:void 0}}