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