@detiq/crawler 0.1.3

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 (412) hide show
  1. package/Dockerfile +49 -0
  2. package/README.md +11 -0
  3. package/dist/accessibility-auditor.d.ts +29 -0
  4. package/dist/accessibility-auditor.js +57 -0
  5. package/dist/accessibility-auditor.js.map +7 -0
  6. package/dist/ai-login.d.ts +32 -0
  7. package/dist/ai-login.js +177 -0
  8. package/dist/ai-login.js.map +7 -0
  9. package/dist/auth-token-rotator.d.ts +47 -0
  10. package/dist/auth-token-rotator.js +58 -0
  11. package/dist/auth-token-rotator.js.map +7 -0
  12. package/dist/auth.d.ts +20 -0
  13. package/dist/auth.js +329 -0
  14. package/dist/auth.js.map +7 -0
  15. package/dist/browser-extension.d.ts +37 -0
  16. package/dist/browser-extension.js +113 -0
  17. package/dist/browser-extension.js.map +7 -0
  18. package/dist/captcha-solver.d.ts +16 -0
  19. package/dist/captcha-solver.js +105 -0
  20. package/dist/captcha-solver.js.map +7 -0
  21. package/dist/color-contrast.d.ts +30 -0
  22. package/dist/color-contrast.js +78 -0
  23. package/dist/color-contrast.js.map +7 -0
  24. package/dist/content-chunker.d.ts +18 -0
  25. package/dist/content-chunker.js +152 -0
  26. package/dist/content-chunker.js.map +7 -0
  27. package/dist/content-dedup.d.ts +23 -0
  28. package/dist/content-dedup.js +60 -0
  29. package/dist/content-dedup.js.map +7 -0
  30. package/dist/cookie-consent.d.ts +25 -0
  31. package/dist/cookie-consent.js +78 -0
  32. package/dist/cookie-consent.js.map +7 -0
  33. package/dist/cookie-jar.d.ts +20 -0
  34. package/dist/cookie-jar.js +61 -0
  35. package/dist/cookie-jar.js.map +7 -0
  36. package/dist/crawler.d.ts +179 -0
  37. package/dist/crawler.js +3672 -0
  38. package/dist/crawler.js.map +7 -0
  39. package/dist/custom-js-injector.d.ts +42 -0
  40. package/dist/custom-js-injector.js +50 -0
  41. package/dist/custom-js-injector.js.map +7 -0
  42. package/dist/dark-mode-capture.d.ts +23 -0
  43. package/dist/dark-mode-capture.js +75 -0
  44. package/dist/dark-mode-capture.js.map +7 -0
  45. package/dist/dead-link-detector.d.ts +37 -0
  46. package/dist/dead-link-detector.js +50 -0
  47. package/dist/dead-link-detector.js.map +7 -0
  48. package/dist/element-hash.d.ts +9 -0
  49. package/dist/element-hash.js +9 -0
  50. package/dist/element-hash.js.map +7 -0
  51. package/dist/email-otp.d.ts +42 -0
  52. package/dist/email-otp.js +177 -0
  53. package/dist/email-otp.js.map +7 -0
  54. package/dist/engine-router.d.ts +47 -0
  55. package/dist/engine-router.js +94 -0
  56. package/dist/engine-router.js.map +7 -0
  57. package/dist/export/bulk-export.d.ts +26 -0
  58. package/dist/export/bulk-export.js +76 -0
  59. package/dist/export/bulk-export.js.map +7 -0
  60. package/dist/export/vector-export.d.ts +76 -0
  61. package/dist/export/vector-export.js +85 -0
  62. package/dist/export/vector-export.js.map +7 -0
  63. package/dist/extractors/advanced.d.ts +54 -0
  64. package/dist/extractors/advanced.js +245 -0
  65. package/dist/extractors/advanced.js.map +7 -0
  66. package/dist/extractors/artifact-envelope.d.ts +63 -0
  67. package/dist/extractors/artifact-envelope.js +63 -0
  68. package/dist/extractors/artifact-envelope.js.map +7 -0
  69. package/dist/failure-analysis-agent.d.ts +16 -0
  70. package/dist/failure-analysis-agent.js +40 -0
  71. package/dist/failure-analysis-agent.js.map +7 -0
  72. package/dist/feature-flag-detector.d.ts +30 -0
  73. package/dist/feature-flag-detector.js +55 -0
  74. package/dist/feature-flag-detector.js.map +7 -0
  75. package/dist/form-detector.d.ts +34 -0
  76. package/dist/form-detector.js +58 -0
  77. package/dist/form-detector.js.map +7 -0
  78. package/dist/frontier.d.ts +32 -0
  79. package/dist/frontier.js +83 -0
  80. package/dist/frontier.js.map +7 -0
  81. package/dist/gap-pass.d.ts +35 -0
  82. package/dist/gap-pass.js +81 -0
  83. package/dist/gap-pass.js.map +7 -0
  84. package/dist/graphql-detector.d.ts +39 -0
  85. package/dist/graphql-detector.js +86 -0
  86. package/dist/graphql-detector.js.map +7 -0
  87. package/dist/http-engine.d.ts +80 -0
  88. package/dist/http-engine.js +304 -0
  89. package/dist/http-engine.js.map +7 -0
  90. package/dist/incremental-diff.d.ts +59 -0
  91. package/dist/incremental-diff.js +61 -0
  92. package/dist/incremental-diff.js.map +7 -0
  93. package/dist/index.d.ts +35 -0
  94. package/dist/index.js +61 -0
  95. package/dist/index.js.map +7 -0
  96. package/dist/input-fuzzer.d.ts +30 -0
  97. package/dist/input-fuzzer.js +105 -0
  98. package/dist/input-fuzzer.js.map +7 -0
  99. package/dist/interactive-probe.d.ts +104 -0
  100. package/dist/interactive-probe.js +442 -0
  101. package/dist/interactive-probe.js.map +7 -0
  102. package/dist/job-queue.d.ts +171 -0
  103. package/dist/job-queue.js +2256 -0
  104. package/dist/job-queue.js.map +7 -0
  105. package/dist/js-coverage.d.ts +29 -0
  106. package/dist/js-coverage.js +56 -0
  107. package/dist/js-coverage.js.map +7 -0
  108. package/dist/js-router-detector.d.ts +17 -0
  109. package/dist/js-router-detector.js +90 -0
  110. package/dist/js-router-detector.js.map +7 -0
  111. package/dist/language-detector.d.ts +22 -0
  112. package/dist/language-detector.js +57 -0
  113. package/dist/language-detector.js.map +7 -0
  114. package/dist/link-classifier.d.ts +23 -0
  115. package/dist/link-classifier.js +40 -0
  116. package/dist/link-classifier.js.map +7 -0
  117. package/dist/live-view.d.ts +36 -0
  118. package/dist/live-view.js +100 -0
  119. package/dist/live-view.js.map +7 -0
  120. package/dist/logger.d.ts +2 -0
  121. package/dist/logger.js +10 -0
  122. package/dist/logger.js.map +7 -0
  123. package/dist/markdown-extract.d.ts +8 -0
  124. package/dist/markdown-extract.js +54 -0
  125. package/dist/markdown-extract.js.map +7 -0
  126. package/dist/native-crawler.d.ts +69 -0
  127. package/dist/native-crawler.js +839 -0
  128. package/dist/native-crawler.js.map +7 -0
  129. package/dist/network-observer.d.ts +49 -0
  130. package/dist/network-observer.js +149 -0
  131. package/dist/network-observer.js.map +7 -0
  132. package/dist/network-throttle.d.ts +25 -0
  133. package/dist/network-throttle.js +34 -0
  134. package/dist/network-throttle.js.map +7 -0
  135. package/dist/openapi-generator.d.ts +49 -0
  136. package/dist/openapi-generator.js +89 -0
  137. package/dist/openapi-generator.js.map +7 -0
  138. package/dist/pdf-extractor.d.ts +28 -0
  139. package/dist/pdf-extractor.js +81 -0
  140. package/dist/pdf-extractor.js.map +7 -0
  141. package/dist/performance-metrics.d.ts +53 -0
  142. package/dist/performance-metrics.js +83 -0
  143. package/dist/performance-metrics.js.map +7 -0
  144. package/dist/phash.d.ts +38 -0
  145. package/dist/phash.js +115 -0
  146. package/dist/phash.js.map +7 -0
  147. package/dist/pii-scrubber.d.ts +21 -0
  148. package/dist/pii-scrubber.js +75 -0
  149. package/dist/pii-scrubber.js.map +7 -0
  150. package/dist/playwright-recorder.d.ts +39 -0
  151. package/dist/playwright-recorder.js +123 -0
  152. package/dist/playwright-recorder.js.map +7 -0
  153. package/dist/policies/crawl-safety.d.ts +9 -0
  154. package/dist/policies/crawl-safety.js +37 -0
  155. package/dist/policies/crawl-safety.js.map +7 -0
  156. package/dist/profiles/domain-profile.d.ts +19 -0
  157. package/dist/profiles/domain-profile.js +140 -0
  158. package/dist/profiles/domain-profile.js.map +7 -0
  159. package/dist/proxy.d.ts +50 -0
  160. package/dist/proxy.js +165 -0
  161. package/dist/proxy.js.map +7 -0
  162. package/dist/rate-limiter.d.ts +31 -0
  163. package/dist/rate-limiter.js +64 -0
  164. package/dist/rate-limiter.js.map +7 -0
  165. package/dist/redirect-tracker.d.ts +37 -0
  166. package/dist/redirect-tracker.js +65 -0
  167. package/dist/redirect-tracker.js.map +7 -0
  168. package/dist/redis-frontier.d.ts +48 -0
  169. package/dist/redis-frontier.js +126 -0
  170. package/dist/redis-frontier.js.map +7 -0
  171. package/dist/redis-queue.d.ts +48 -0
  172. package/dist/redis-queue.js +165 -0
  173. package/dist/redis-queue.js.map +7 -0
  174. package/dist/response-cache.d.ts +38 -0
  175. package/dist/response-cache.js +140 -0
  176. package/dist/response-cache.js.map +7 -0
  177. package/dist/responsive-detector.d.ts +29 -0
  178. package/dist/responsive-detector.js +46 -0
  179. package/dist/responsive-detector.js.map +7 -0
  180. package/dist/retry-policy.d.ts +20 -0
  181. package/dist/retry-policy.js +54 -0
  182. package/dist/retry-policy.js.map +7 -0
  183. package/dist/robots-txt.d.ts +9 -0
  184. package/dist/robots-txt.js +77 -0
  185. package/dist/robots-txt.js.map +7 -0
  186. package/dist/screen-reader.d.ts +16 -0
  187. package/dist/screen-reader.js +46 -0
  188. package/dist/screen-reader.js.map +7 -0
  189. package/dist/screenshot-masker.d.ts +28 -0
  190. package/dist/screenshot-masker.js +62 -0
  191. package/dist/screenshot-masker.js.map +7 -0
  192. package/dist/seo-checker.d.ts +33 -0
  193. package/dist/seo-checker.js +91 -0
  194. package/dist/seo-checker.js.map +7 -0
  195. package/dist/session-capture.d.ts +35 -0
  196. package/dist/session-capture.js +267 -0
  197. package/dist/session-capture.js.map +7 -0
  198. package/dist/session-export.d.ts +49 -0
  199. package/dist/session-export.js +117 -0
  200. package/dist/session-export.js.map +7 -0
  201. package/dist/sitemap-writer.d.ts +21 -0
  202. package/dist/sitemap-writer.js +47 -0
  203. package/dist/sitemap-writer.js.map +7 -0
  204. package/dist/sitemap.d.ts +51 -0
  205. package/dist/sitemap.js +152 -0
  206. package/dist/sitemap.js.map +7 -0
  207. package/dist/skyvern.d.ts +56 -0
  208. package/dist/skyvern.js +129 -0
  209. package/dist/skyvern.js.map +7 -0
  210. package/dist/sqs-queue.d.ts +2 -0
  211. package/dist/sqs-queue.js +47 -0
  212. package/dist/sqs-queue.js.map +7 -0
  213. package/dist/sso-token.d.ts +29 -0
  214. package/dist/sso-token.js +76 -0
  215. package/dist/sso-token.js.map +7 -0
  216. package/dist/stealth/human-behavior.d.ts +20 -0
  217. package/dist/stealth/human-behavior.js +168 -0
  218. package/dist/stealth/human-behavior.js.map +7 -0
  219. package/dist/telemetry.d.ts +34 -0
  220. package/dist/telemetry.js +77 -0
  221. package/dist/telemetry.js.map +7 -0
  222. package/dist/test-executor.d.ts +67 -0
  223. package/dist/test-executor.js +311 -0
  224. package/dist/test-executor.js.map +7 -0
  225. package/dist/tls-fingerprint.d.ts +50 -0
  226. package/dist/tls-fingerprint.js +81 -0
  227. package/dist/tls-fingerprint.js.map +7 -0
  228. package/dist/url-pattern-completer.d.ts +28 -0
  229. package/dist/url-pattern-completer.js +87 -0
  230. package/dist/url-pattern-completer.js.map +7 -0
  231. package/dist/visual-diff-verdict.d.ts +40 -0
  232. package/dist/visual-diff-verdict.js +25 -0
  233. package/dist/visual-diff-verdict.js.map +7 -0
  234. package/dist/webhook-notifier.d.ts +30 -0
  235. package/dist/webhook-notifier.js +76 -0
  236. package/dist/webhook-notifier.js.map +7 -0
  237. package/dist/websocket-detector.d.ts +24 -0
  238. package/dist/websocket-detector.js +60 -0
  239. package/dist/websocket-detector.js.map +7 -0
  240. package/dist/worker.d.ts +1 -0
  241. package/dist/worker.js +33 -0
  242. package/dist/worker.js.map +7 -0
  243. package/package.json +45 -0
  244. package/src/accessibility-auditor.ts +102 -0
  245. package/src/ai-login.ts +218 -0
  246. package/src/auth-token-rotator.ts +89 -0
  247. package/src/auth.ts +396 -0
  248. package/src/browser-extension.ts +186 -0
  249. package/src/captcha-solver.ts +143 -0
  250. package/src/color-contrast.ts +116 -0
  251. package/src/content-chunker.ts +170 -0
  252. package/src/content-dedup.ts +99 -0
  253. package/src/cookie-consent.ts +106 -0
  254. package/src/cookie-jar.ts +91 -0
  255. package/src/crawler.ts +4305 -0
  256. package/src/custom-js-injector.ts +93 -0
  257. package/src/dark-mode-capture.ts +110 -0
  258. package/src/dead-link-detector.ts +76 -0
  259. package/src/element-hash.ts +16 -0
  260. package/src/email-otp.ts +252 -0
  261. package/src/engine-router.ts +141 -0
  262. package/src/export/bulk-export.ts +85 -0
  263. package/src/export/vector-export.ts +160 -0
  264. package/src/extractors/advanced.ts +283 -0
  265. package/src/extractors/artifact-envelope.ts +111 -0
  266. package/src/failure-analysis-agent.ts +49 -0
  267. package/src/feature-flag-detector.ts +97 -0
  268. package/src/form-detector.ts +103 -0
  269. package/src/frontier.test.ts +64 -0
  270. package/src/frontier.ts +112 -0
  271. package/src/gap-pass.ts +122 -0
  272. package/src/graphql-detector.ts +129 -0
  273. package/src/http-engine.ts +433 -0
  274. package/src/incremental-diff.ts +121 -0
  275. package/src/index.ts +35 -0
  276. package/src/input-fuzzer.ts +129 -0
  277. package/src/interactive-probe.ts +623 -0
  278. package/src/job-queue.test.ts +120 -0
  279. package/src/job-queue.ts +2701 -0
  280. package/src/js-coverage.ts +87 -0
  281. package/src/js-router-detector.ts +112 -0
  282. package/src/language-detector.ts +82 -0
  283. package/src/link-classifier.ts +65 -0
  284. package/src/live-view.ts +141 -0
  285. package/src/logger.ts +9 -0
  286. package/src/markdown-extract.test.ts +73 -0
  287. package/src/markdown-extract.ts +76 -0
  288. package/src/native-crawler.ts +1106 -0
  289. package/src/network-observer.ts +204 -0
  290. package/src/network-throttle.ts +69 -0
  291. package/src/openapi-generator.ts +135 -0
  292. package/src/pdf-extractor.ts +123 -0
  293. package/src/performance-metrics.ts +134 -0
  294. package/src/phash.ts +151 -0
  295. package/src/pii-scrubber.ts +99 -0
  296. package/src/playwright-recorder.ts +170 -0
  297. package/src/policies/crawl-safety.ts +46 -0
  298. package/src/profiles/domain-profile.ts +167 -0
  299. package/src/proxy.ts +238 -0
  300. package/src/rate-limiter.ts +87 -0
  301. package/src/redirect-tracker.ts +97 -0
  302. package/src/redis-frontier.ts +143 -0
  303. package/src/redis-queue.ts +208 -0
  304. package/src/response-cache.ts +150 -0
  305. package/src/responsive-detector.ts +92 -0
  306. package/src/retry-policy.ts +87 -0
  307. package/src/robots-txt.ts +96 -0
  308. package/src/screen-reader.ts +89 -0
  309. package/src/screenshot-masker.ts +98 -0
  310. package/src/seo-checker.ts +134 -0
  311. package/src/session-capture.ts +325 -0
  312. package/src/session-export.test.ts +75 -0
  313. package/src/session-export.ts +172 -0
  314. package/src/sitemap-writer.ts +65 -0
  315. package/src/sitemap.test.ts +131 -0
  316. package/src/sitemap.ts +224 -0
  317. package/src/skyvern.ts +199 -0
  318. package/src/sqs-queue.ts +49 -0
  319. package/src/sso-token.ts +120 -0
  320. package/src/stealth/human-behavior.ts +191 -0
  321. package/src/telemetry.ts +122 -0
  322. package/src/test-executor.test.ts +72 -0
  323. package/src/test-executor.ts +424 -0
  324. package/src/tls-fingerprint.ts +111 -0
  325. package/src/url-pattern-completer.ts +114 -0
  326. package/src/visual-diff-verdict.test.ts +80 -0
  327. package/src/visual-diff-verdict.ts +57 -0
  328. package/src/webhook-notifier.ts +109 -0
  329. package/src/websocket-detector.ts +83 -0
  330. package/src/worker.ts +48 -0
  331. package/tsconfig.json +13 -0
  332. package/types/accessibility-auditor.d.ts +29 -0
  333. package/types/ai-login.d.ts +32 -0
  334. package/types/auth-token-rotator.d.ts +47 -0
  335. package/types/auth.d.ts +20 -0
  336. package/types/browser-extension.d.ts +37 -0
  337. package/types/bull-worker.d.ts +6 -0
  338. package/types/captcha-solver.d.ts +16 -0
  339. package/types/color-contrast.d.ts +30 -0
  340. package/types/content-chunker.d.ts +18 -0
  341. package/types/content-dedup.d.ts +23 -0
  342. package/types/cookie-consent.d.ts +25 -0
  343. package/types/cookie-jar.d.ts +20 -0
  344. package/types/crawler.d.ts +179 -0
  345. package/types/custom-js-injector.d.ts +42 -0
  346. package/types/dark-mode-capture.d.ts +23 -0
  347. package/types/dead-link-detector.d.ts +37 -0
  348. package/types/element-hash.d.ts +9 -0
  349. package/types/email-otp.d.ts +42 -0
  350. package/types/engine-router.d.ts +47 -0
  351. package/types/export/bulk-export.d.ts +26 -0
  352. package/types/export/vector-export.d.ts +76 -0
  353. package/types/extractors/advanced.d.ts +54 -0
  354. package/types/extractors/artifact-envelope.d.ts +63 -0
  355. package/types/failure-analysis-agent.d.ts +16 -0
  356. package/types/feature-flag-detector.d.ts +30 -0
  357. package/types/form-detector.d.ts +34 -0
  358. package/types/frontier.d.ts +32 -0
  359. package/types/gap-pass.d.ts +35 -0
  360. package/types/graphql-detector.d.ts +39 -0
  361. package/types/http-engine.d.ts +80 -0
  362. package/types/incremental-diff.d.ts +59 -0
  363. package/types/index.d.ts +35 -0
  364. package/types/input-fuzzer.d.ts +30 -0
  365. package/types/interactive-probe.d.ts +104 -0
  366. package/types/job-queue.d.ts +171 -0
  367. package/types/js-coverage.d.ts +29 -0
  368. package/types/js-router-detector.d.ts +17 -0
  369. package/types/language-detector.d.ts +22 -0
  370. package/types/link-classifier.d.ts +23 -0
  371. package/types/live-view.d.ts +36 -0
  372. package/types/logger.d.ts +2 -0
  373. package/types/markdown-extract.d.ts +8 -0
  374. package/types/native-crawler.d.ts +69 -0
  375. package/types/network-observer.d.ts +49 -0
  376. package/types/network-throttle.d.ts +25 -0
  377. package/types/openapi-generator.d.ts +49 -0
  378. package/types/pdf-extractor.d.ts +28 -0
  379. package/types/performance-metrics.d.ts +53 -0
  380. package/types/phash.d.ts +38 -0
  381. package/types/pii-scrubber.d.ts +21 -0
  382. package/types/playwright-recorder.d.ts +39 -0
  383. package/types/policies/crawl-safety.d.ts +9 -0
  384. package/types/profiles/domain-profile.d.ts +19 -0
  385. package/types/proxy.d.ts +50 -0
  386. package/types/rate-limiter.d.ts +31 -0
  387. package/types/redirect-tracker.d.ts +37 -0
  388. package/types/redis-frontier.d.ts +48 -0
  389. package/types/redis-queue.d.ts +48 -0
  390. package/types/response-cache.d.ts +38 -0
  391. package/types/responsive-detector.d.ts +29 -0
  392. package/types/retry-policy.d.ts +20 -0
  393. package/types/robots-txt.d.ts +9 -0
  394. package/types/screen-reader.d.ts +16 -0
  395. package/types/screenshot-masker.d.ts +28 -0
  396. package/types/seo-checker.d.ts +33 -0
  397. package/types/session-capture.d.ts +38 -0
  398. package/types/session-export.d.ts +49 -0
  399. package/types/sitemap-writer.d.ts +21 -0
  400. package/types/sitemap.d.ts +51 -0
  401. package/types/skyvern.d.ts +56 -0
  402. package/types/sqs-queue.d.ts +2 -0
  403. package/types/sso-token.d.ts +29 -0
  404. package/types/stealth/human-behavior.d.ts +20 -0
  405. package/types/telemetry.d.ts +34 -0
  406. package/types/test-executor.d.ts +67 -0
  407. package/types/tls-fingerprint.d.ts +50 -0
  408. package/types/url-pattern-completer.d.ts +28 -0
  409. package/types/visual-diff-verdict.d.ts +40 -0
  410. package/types/webhook-notifier.d.ts +30 -0
  411. package/types/websocket-detector.d.ts +24 -0
  412. package/types/worker.d.ts +1 -0
@@ -0,0 +1,1106 @@
1
+ /**
2
+ * ZeTa Native App Crawler — Appium-based crawler for Android APK and iOS IPA.
3
+ *
4
+ * Architecture:
5
+ * Appium Server (running at APPIUM_SERVER_URL) → WebDriverIO session
6
+ * → UiAutomator2 (Android) / XCUITest (iOS)
7
+ * → accessibility tree XML → ZeTa Element records
8
+ *
9
+ * TODO: requires Appium server running at APPIUM_SERVER_URL env var
10
+ * TODO: requires `webdriverio` package — add "webdriverio": "^9.x" to package.json
11
+ *
12
+ * Device farm support:
13
+ * - local_emulator: Appium server + local emulator/simulator
14
+ * - aws_device_farm: presigned S3 URL uploaded to Device Farm, session via WDIO
15
+ * - browserstack: BrowserStack Automate Appium endpoint
16
+ */
17
+
18
+ import fs from 'node:fs/promises';
19
+ import path from 'node:path';
20
+ import os from 'node:os';
21
+ import { withTenant } from '@detiq/database';
22
+ import { AppBrain, getTenantStorageConfig, uploadScreenshotToTenantStorage, type TenantStorageConfig } from '@detiq/app-brain';
23
+ import { config } from '@detiq/core';
24
+ import { logger } from './logger.js';
25
+
26
+ // ---------------------------------------------------------------------------
27
+ // Public types
28
+ // ---------------------------------------------------------------------------
29
+
30
+ export interface NativeCrawlerOptions {
31
+ tenantId: string;
32
+ projectId: string;
33
+ jobId: string;
34
+ platform: 'android' | 'ios' | 'windows' | 'macos';
35
+ appS3Path: string; // s3://bucket/path/app.apk or s3://bucket/path/app.ipa
36
+ packageName?: string; // com.example.app (Android) or bundle ID (iOS)
37
+ deviceModel?: string; // optional device preference
38
+ osVersion?: string; // Appium platformVersion (e.g. "14.0" for Android, "17.0" for iOS)
39
+ appActivity?: string; // Android: activity to launch (e.g. ".MainActivity")
40
+ appWaitActivity?: string; // Android: activity to wait for after launch
41
+ deviceFarm: 'local_emulator' | 'aws_device_farm' | 'browserstack';
42
+ // Device farm connection credentials (from workspace settings, overrides env vars)
43
+ appiumServerUrl?: string;
44
+ browserstackUsername?: string;
45
+ browserstackAccessKey?: string;
46
+ awsAccessKeyId?: string;
47
+ awsSecretAccessKey?: string;
48
+ awsRegion?: string;
49
+ awsDeviceFarmProjectArn?: string;
50
+ // Auth credentials for Gap 1: native app login
51
+ credentials?: {
52
+ username?: string;
53
+ password?: string;
54
+ usernameFieldLabel?: string; // hint if default detection fails, e.g. "Email"
55
+ passwordFieldLabel?: string;
56
+ loginButtonLabel?: string;
57
+ };
58
+ // Vision fallback AI config for Gap 2: when accessibility tree is empty
59
+ visionLlmConfig?: { provider: string; model: string; apiKey?: string; baseUrl?: string } | null;
60
+ // Gesture config for Gap 3
61
+ gestureConfig?: {
62
+ enableSwipe?: boolean; // default true
63
+ enableLongPress?: boolean; // default true
64
+ };
65
+ // MITM proxy for Gap 4 (when available)
66
+ mitmProxyPort?: number;
67
+ }
68
+
69
+ /** A native UI element extracted from the accessibility tree. */
70
+ interface NativeElement {
71
+ role: string;
72
+ label: string;
73
+ bounds: NativeBounds;
74
+ platform: 'android' | 'ios' | 'windows' | 'macos';
75
+ resourceId?: string; // Android: resource-id attribute
76
+ className?: string; // Android: widget class / iOS: XCUIElement type
77
+ }
78
+
79
+ interface NativeBounds {
80
+ x: number;
81
+ y: number;
82
+ width: number;
83
+ height: number;
84
+ }
85
+
86
+ // ---------------------------------------------------------------------------
87
+ // Internal helpers
88
+ // ---------------------------------------------------------------------------
89
+
90
+ /**
91
+ * Build Appium desired capabilities for the given platform.
92
+ * localAppPath is the local path after downloading from S3 (or a presigned URL
93
+ * for device farms that fetch the binary themselves).
94
+ */
95
+ function buildCapabilities(opts: NativeCrawlerOptions, localAppPath: string): Record<string, unknown> {
96
+ const appiumServerUrl = opts.appiumServerUrl ?? process.env.APPIUM_SERVER_URL ?? 'http://127.0.0.1:4723';
97
+ logger.info({ appiumServerUrl, platform: opts.platform }, 'Building Appium capabilities');
98
+
99
+ // BrowserStack early-return: https://hub-cloud.browserstack.com/wd/hub
100
+ if (opts.deviceFarm === 'browserstack' && (opts.platform === 'android' || opts.platform === 'ios')) {
101
+ const bstackOptions = {
102
+ userName: opts.browserstackUsername ?? process.env.BROWSERSTACK_USERNAME ?? '',
103
+ accessKey: opts.browserstackAccessKey ?? process.env.BROWSERSTACK_ACCESS_KEY ?? '',
104
+ deviceName: opts.deviceModel ?? (opts.platform === 'android' ? 'Google Pixel 7' : 'iPhone 15'),
105
+ osVersion: opts.osVersion ?? (opts.platform === 'android' ? '13.0' : '17'),
106
+ realMobile: true,
107
+ projectName: 'ZeTa-QIP',
108
+ buildName: `${opts.platform}-crawl`,
109
+ sessionName: opts.jobId,
110
+ };
111
+ if (opts.platform === 'android') {
112
+ return {
113
+ 'platformName': 'android',
114
+ 'appium:automationName': 'UiAutomator2',
115
+ 'appium:app': localAppPath,
116
+ 'appium:newCommandTimeout': 120,
117
+ 'appium:autoGrantPermissions': true,
118
+ ...(opts.appActivity ? { 'appium:appActivity': opts.appActivity } : {}),
119
+ ...(opts.packageName ? { 'appium:appPackage': opts.packageName } : {}),
120
+ 'bstack:options': bstackOptions,
121
+ };
122
+ }
123
+ return {
124
+ 'platformName': 'ios',
125
+ 'appium:automationName': 'XCUITest',
126
+ 'appium:app': localAppPath,
127
+ 'appium:newCommandTimeout': 120,
128
+ ...(opts.packageName ? { 'appium:bundleId': opts.packageName } : {}),
129
+ 'bstack:options': bstackOptions,
130
+ };
131
+ }
132
+
133
+ if (opts.platform === 'android') {
134
+ return {
135
+ 'appium:platformName': 'Android',
136
+ 'appium:deviceName': opts.deviceModel ?? 'Pixel 7',
137
+ 'appium:app': localAppPath,
138
+ 'appium:automationName': 'UiAutomator2',
139
+ 'appium:autoGrantPermissions': true,
140
+ 'appium:newCommandTimeout': 120,
141
+ 'appium:platformVersion': opts.osVersion ?? '',
142
+ ...(opts.appActivity ? { 'appium:appActivity': opts.appActivity } : {}),
143
+ ...(opts.appWaitActivity ? { 'appium:appWaitActivity': opts.appWaitActivity } : {}),
144
+ 'appium:eventTimings': true, // capture launch time telemetry
145
+ ...(opts.packageName ? { 'appium:appPackage': opts.packageName } : {}),
146
+ };
147
+ }
148
+
149
+ if (opts.platform === 'windows') {
150
+ return {
151
+ 'platformName': 'Windows',
152
+ 'appium:automationName': 'Windows',
153
+ 'appium:app': localAppPath,
154
+ 'appium:deviceName': 'WindowsPC',
155
+ 'appium:newCommandTimeout': 120,
156
+ ...(opts.packageName ? { 'appium:appTopLevelWindow': opts.packageName } : {}),
157
+ };
158
+ }
159
+
160
+ if (opts.platform === 'macos') {
161
+ return {
162
+ 'platformName': 'mac',
163
+ 'appium:automationName': 'Mac2',
164
+ 'appium:app': localAppPath,
165
+ 'appium:deviceName': 'Mac',
166
+ 'appium:newCommandTimeout': 120,
167
+ ...(opts.packageName ? { 'appium:bundleId': opts.packageName } : {}),
168
+ };
169
+ }
170
+
171
+ // iOS
172
+ return {
173
+ 'appium:platformName': 'iOS',
174
+ 'appium:deviceName': opts.deviceModel ?? 'iPhone 15',
175
+ 'appium:app': localAppPath,
176
+ 'appium:automationName': 'XCUITest',
177
+ 'appium:newCommandTimeout': 120,
178
+ 'appium:platformVersion': opts.osVersion ?? '',
179
+ ...(opts.packageName ? { 'appium:bundleId': opts.packageName } : {}),
180
+ };
181
+ }
182
+
183
+ /**
184
+ * Upload a locally downloaded app binary to BrowserStack App Automate.
185
+ * Returns the bs:// app URL to use as the Appium capability value.
186
+ */
187
+ async function uploadToBrowserStack(localPath: string, opts: NativeCrawlerOptions): Promise<string> {
188
+ const username = opts.browserstackUsername ?? process.env.BROWSERSTACK_USERNAME ?? '';
189
+ const accessKey = opts.browserstackAccessKey ?? process.env.BROWSERSTACK_ACCESS_KEY ?? '';
190
+ const credentials = Buffer.from(`${username}:${accessKey}`).toString('base64');
191
+
192
+ const fileBuffer = await fs.readFile(localPath);
193
+ const filename = localPath.split('/').pop() ?? 'app.apk';
194
+
195
+ const form = new (global as any).FormData();
196
+ form.append('file', new Blob([new Uint8Array(fileBuffer)]), filename);
197
+
198
+ logger.info({ localPath, filename }, 'Uploading app binary to BrowserStack');
199
+ const response = await fetch('https://api-cloud.browserstack.com/app-automate/upload', {
200
+ method: 'POST',
201
+ headers: { Authorization: `Basic ${credentials}` },
202
+ body: form,
203
+ });
204
+
205
+ if (!response.ok) {
206
+ throw new Error(`BrowserStack upload failed: ${response.status} ${await response.text()}`);
207
+ }
208
+ const json = (await response.json()) as { app_url?: string };
209
+ if (!json.app_url) throw new Error('BrowserStack upload did not return app_url');
210
+ logger.info({ appUrl: json.app_url }, 'App uploaded to BrowserStack');
211
+ return json.app_url; // e.g. "bs://1234abc..."
212
+ }
213
+
214
+ /**
215
+ * Download app binary from S3 to a local temp file.
216
+ * For BrowserStack, also uploads the binary to App Automate and returns the bs:// URL.
217
+ * Returns the local file path (or bs:// URL for BrowserStack).
218
+ */
219
+ async function downloadAppFromS3(appS3Path: string, platform: 'android' | 'ios' | 'windows' | 'macos', opts: NativeCrawlerOptions, storageCfg?: TenantStorageConfig | null): Promise<string> {
220
+ // Local disk fallback — APK stored on-server when no cloud storage is configured
221
+ if (appS3Path.startsWith('local://')) {
222
+ const localPath = appS3Path.slice('local://'.length);
223
+ logger.info({ localPath }, 'Using locally stored app binary');
224
+ if (opts.deviceFarm === 'browserstack') {
225
+ return uploadToBrowserStack(localPath, opts);
226
+ }
227
+ return localPath;
228
+ }
229
+
230
+ // Parse s3://bucket/key
231
+ const withoutScheme = appS3Path.replace(/^s3:\/\//, '');
232
+ const slashIdx = withoutScheme.indexOf('/');
233
+ if (slashIdx === -1) {
234
+ throw new Error(`Invalid S3 path: ${appS3Path}. Expected format: s3://bucket/path/to/file`);
235
+ }
236
+ const bucket = withoutScheme.slice(0, slashIdx);
237
+ const key = withoutScheme.slice(slashIdx + 1);
238
+ const extMap: Record<string, string> = { android: '.apk', ios: '.ipa', windows: '.exe', macos: '.app' };
239
+ const ext = extMap[platform] ?? '';
240
+ const tmpPath = path.join(os.tmpdir(), `zeta-native-${Date.now()}${ext}`);
241
+
242
+ logger.info({ bucket, key, tmpPath, provider: storageCfg?.provider ?? 'env' }, 'Downloading app binary from storage');
243
+
244
+ const { S3Client, GetObjectCommand } = await import('@aws-sdk/client-s3' as any);
245
+ const isR2 = storageCfg?.provider === 'R2';
246
+ const s3ClientCfg: Record<string, any> = {
247
+ region: isR2 ? 'auto' : (storageCfg?.region ?? process.env.AWS_REGION ?? 'us-east-1'),
248
+ };
249
+ if (storageCfg?.accessKeyId && storageCfg?.secretAccessKey) {
250
+ s3ClientCfg.credentials = { accessKeyId: storageCfg.accessKeyId, secretAccessKey: storageCfg.secretAccessKey };
251
+ }
252
+ if (isR2 && storageCfg?.accountId) {
253
+ s3ClientCfg.endpoint = `https://${storageCfg.accountId}.r2.cloudflarestorage.com`;
254
+ s3ClientCfg.forcePathStyle = true;
255
+ }
256
+ const s3 = new S3Client(s3ClientCfg);
257
+ const response = await s3.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
258
+
259
+ // Stream the body to disk
260
+ const { Readable } = await import('node:stream');
261
+ const { pipeline } = await import('node:stream/promises');
262
+ const fh = await fs.open(tmpPath, 'w');
263
+ try {
264
+ await pipeline(Readable.from(response.Body as any), fh.createWriteStream());
265
+ } finally {
266
+ await fh.close();
267
+ }
268
+
269
+ logger.info({ tmpPath }, 'App binary downloaded');
270
+
271
+ // For BrowserStack, upload the binary and return the bs:// URL
272
+ if (opts.deviceFarm === 'browserstack') {
273
+ return uploadToBrowserStack(tmpPath, opts);
274
+ }
275
+ return tmpPath;
276
+ }
277
+
278
+ /**
279
+ * Parse the Appium XML page source into a flat list of NativeElement records.
280
+ *
281
+ * Android UiAutomator2 XML looks like:
282
+ * <node text="Login" resource-id="com.example:id/btn_login"
283
+ * class="android.widget.Button" bounds="[100,400][980,520]"
284
+ * clickable="true" enabled="true" />
285
+ *
286
+ * iOS XCUITest XML looks like:
287
+ * <XCUIElementTypeButton name="Login" label="Login"
288
+ * x="50" y="300" width="280" height="44"
289
+ * enabled="true" visible="true" />
290
+ */
291
+ function parseAccessibilityTree(xmlSource: string, platform: 'android' | 'ios' | 'windows' | 'macos'): NativeElement[] {
292
+ const elements: NativeElement[] = [];
293
+
294
+ if (platform === 'windows') {
295
+ // WinAppDriver returns UIAutomation XML like:
296
+ // <Button Name="OK" AutomationId="OkButton" IsEnabled="true" IsOffscreen="false"/>
297
+ const WIN_INTERACTIVE = new Set(['Button', 'Edit', 'ComboBox', 'CheckBox', 'RadioButton', 'Slider', 'ListItem', 'MenuItem', 'Hyperlink']);
298
+ const nodeRegex = /<(\w+)\s([^>]*)\/>/g;
299
+ let match: RegExpExecArray | null;
300
+ while ((match = nodeRegex.exec(xmlSource)) !== null) {
301
+ const typeName = match[1] ?? '';
302
+ if (!WIN_INTERACTIVE.has(typeName)) continue;
303
+ const attrs = match[2] ?? '';
304
+ const get = (name: string) => new RegExp(`\\b${name}="([^"]*)"`, 'i').exec(attrs)?.[1] ?? '';
305
+ if (get('IsEnabled') === 'false' || get('IsOffscreen') === 'true') continue;
306
+ const label = get('Name') || get('AutomationId') || '';
307
+ if (!label) continue;
308
+ const role = winTypeToRole(typeName);
309
+ // BoundingRectangle="x:0 y:0 width:100 height:30" or similar
310
+ const brStr = get('BoundingRectangle');
311
+ const brMatch = /x:(\d+)\s+y:(\d+)\s+width:(\d+)\s+height:(\d+)/i.exec(brStr);
312
+ const bounds = brMatch
313
+ ? { x: parseInt(brMatch[1]), y: parseInt(brMatch[2]), width: parseInt(brMatch[3]), height: parseInt(brMatch[4]) }
314
+ : { x: 0, y: 0, width: 100, height: 30 };
315
+ elements.push({ role, label, bounds, platform: 'windows' as any, className: typeName });
316
+ }
317
+ }
318
+
319
+ if (platform === 'macos') {
320
+ const MAC_INTERACTIVE = new Set([
321
+ 'XCUIElementTypeButton', 'XCUIElementTypeTextField', 'XCUIElementTypeSecureTextField',
322
+ 'XCUIElementTypeCheckBox', 'XCUIElementTypeRadioButton', 'XCUIElementTypePopUpButton',
323
+ 'XCUIElementTypeComboBox', 'XCUIElementTypeSlider', 'XCUIElementTypeMenuItem',
324
+ 'XCUIElementTypeSearchField', 'XCUIElementTypeLink',
325
+ ]);
326
+ const nodeRegex = /<(XCUIElementType\w+)\s([^>]*)\/>/g;
327
+ let match: RegExpExecArray | null;
328
+ while ((match = nodeRegex.exec(xmlSource)) !== null) {
329
+ const typeName = match[1] ?? '';
330
+ if (!MAC_INTERACTIVE.has(typeName)) continue;
331
+ const attrs = match[2] ?? '';
332
+ const get = (name: string) => new RegExp(`\\b${name}="([^"]*)"`, 'i').exec(attrs)?.[1] ?? '';
333
+ if (get('enabled') === 'false') continue;
334
+ const label = get('title') || get('label') || get('value') || '';
335
+ if (!label) continue;
336
+ const x = parseFloat(get('x') || '0');
337
+ const y = parseFloat(get('y') || '0');
338
+ const width = parseFloat(get('width') || '0');
339
+ const height = parseFloat(get('height') || '0');
340
+ const role = macTypeToRole(typeName);
341
+ elements.push({ role, label, bounds: { x, y, width, height }, platform: 'macos' as any, className: typeName });
342
+ }
343
+ }
344
+
345
+ if (platform === 'android') {
346
+ // Match interactive node elements
347
+ const INTERACTIVE_CLASSES = new Set([
348
+ 'android.widget.Button',
349
+ 'android.widget.EditText',
350
+ 'android.widget.CheckBox',
351
+ 'android.widget.RadioButton',
352
+ 'android.widget.Switch',
353
+ 'android.widget.Spinner',
354
+ 'android.widget.TextView', // tappable text
355
+ 'android.widget.ImageButton',
356
+ 'android.widget.ToggleButton',
357
+ 'androidx.recyclerview.widget.RecyclerView',
358
+ 'com.google.android.material.bottomnavigation.BottomNavigationItemView',
359
+ 'android.widget.ImageView', // tappable images (nav icons)
360
+ ]);
361
+
362
+ // Simple regex-based node extraction — avoids adding an XML parser dependency.
363
+ // This is intentionally minimal; a full implementation would use a proper XML parser.
364
+ const nodeRegex = /<node\s([^>]*)\/?>/g;
365
+ let match: RegExpExecArray | null;
366
+ while ((match = nodeRegex.exec(xmlSource)) !== null) {
367
+ const attrs = match[1] ?? '';
368
+ const get = (name: string): string => {
369
+ const m = new RegExp(`\\b${name}="([^"]*)"`, 'i').exec(attrs);
370
+ return m ? m[1] ?? '' : '';
371
+ };
372
+
373
+ const className = get('class');
374
+ const clickable = get('clickable') === 'true';
375
+ const enabled = get('enabled') === 'true';
376
+
377
+ if (!enabled) continue;
378
+ if (!clickable && !INTERACTIVE_CLASSES.has(className)) continue;
379
+
380
+ const label = get('content-desc') || get('text') || get('resource-id').split('/').pop() || '';
381
+ if (!label) continue;
382
+
383
+ const resourceId = get('resource-id');
384
+ const boundsStr = get('bounds'); // "[x1,y1][x2,y2]"
385
+ const bounds = parseAndroidBounds(boundsStr);
386
+ if (!bounds) continue;
387
+
388
+ const role = classToRole(className);
389
+ elements.push({ role, label, bounds, platform: 'android', resourceId, className });
390
+ }
391
+ } else {
392
+ // iOS — XCUITest element types
393
+ const INTERACTIVE_TYPES = new Set([
394
+ 'XCUIElementTypeButton',
395
+ 'XCUIElementTypeTextField',
396
+ 'XCUIElementTypeSecureTextField',
397
+ 'XCUIElementTypeSwitch',
398
+ 'XCUIElementTypeSlider',
399
+ 'XCUIElementTypeCheckBox',
400
+ 'XCUIElementTypeRadioButton',
401
+ 'XCUIElementTypePickerWheel',
402
+ 'XCUIElementTypeSearchField',
403
+ 'XCUIElementTypeCell',
404
+ 'XCUIElementTypeLink',
405
+ 'XCUIElementTypeTabBarItem',
406
+ 'XCUIElementTypeNavigationBar',
407
+ ]);
408
+
409
+ const nodeRegex = /<(XCUIElementType\w+)\s([^>]*)\/>/g;
410
+ let match: RegExpExecArray | null;
411
+ while ((match = nodeRegex.exec(xmlSource)) !== null) {
412
+ const typeName = match[1] ?? '';
413
+ if (!INTERACTIVE_TYPES.has(typeName)) continue;
414
+
415
+ const attrs = match[2] ?? '';
416
+ const get = (name: string): string => {
417
+ const m = new RegExp(`\\b${name}="([^"]*)"`, 'i').exec(attrs);
418
+ return m ? m[1] ?? '' : '';
419
+ };
420
+
421
+ if (get('enabled') === 'false') continue;
422
+
423
+ const label = get('label') || get('name') || get('value') || '';
424
+ if (!label) continue;
425
+
426
+ const x = parseFloat(get('x') || '0');
427
+ const y = parseFloat(get('y') || '0');
428
+ const width = parseFloat(get('width') || '0');
429
+ const height = parseFloat(get('height') || '0');
430
+ if (width === 0 && height === 0) continue;
431
+
432
+ const role = xcuiTypeToRole(typeName);
433
+ elements.push({ role, label, bounds: { x, y, width, height }, platform: 'ios', className: typeName });
434
+ }
435
+ }
436
+
437
+ return elements;
438
+ }
439
+
440
+ /** Parse Android bounds string "[x1,y1][x2,y2]" → NativeBounds */
441
+ function parseAndroidBounds(boundsStr: string): NativeBounds | null {
442
+ const m = /\[(\d+),(\d+)\]\[(\d+),(\d+)\]/.exec(boundsStr);
443
+ if (!m) return null;
444
+ const x1 = parseInt(m[1] ?? '0', 10);
445
+ const y1 = parseInt(m[2] ?? '0', 10);
446
+ const x2 = parseInt(m[3] ?? '0', 10);
447
+ const y2 = parseInt(m[4] ?? '0', 10);
448
+ return { x: x1, y: y1, width: x2 - x1, height: y2 - y1 };
449
+ }
450
+
451
+ /** Map Android widget class names to ZeTa element roles */
452
+ function classToRole(className: string): string {
453
+ if (className.includes('Button')) return 'button';
454
+ if (className.includes('EditText')) return 'input';
455
+ if (className.includes('CheckBox')) return 'checkbox';
456
+ if (className.includes('RadioButton')) return 'radio';
457
+ if (className.includes('Switch') || className.includes('Toggle')) return 'switch';
458
+ if (className.includes('Spinner')) return 'select';
459
+ if (className.includes('TextView')) return 'text';
460
+ return className.split('.').pop()?.toLowerCase() ?? 'unknown';
461
+ }
462
+
463
+ /** Map XCUITest element type strings to ZeTa element roles */
464
+ function xcuiTypeToRole(xcuiType: string): string {
465
+ if (xcuiType.includes('Button')) return 'button';
466
+ if (xcuiType.includes('TextField')) return 'input';
467
+ if (xcuiType.includes('SecureTextField')) return 'input';
468
+ if (xcuiType.includes('Switch')) return 'switch';
469
+ if (xcuiType.includes('Slider')) return 'slider';
470
+ if (xcuiType.includes('CheckBox')) return 'checkbox';
471
+ if (xcuiType.includes('RadioButton')) return 'radio';
472
+ if (xcuiType.includes('PickerWheel')) return 'select';
473
+ if (xcuiType.includes('SearchField')) return 'input';
474
+ return xcuiType.replace('XCUIElementType', '').toLowerCase();
475
+ }
476
+
477
+ /** Map WinAppDriver UIAutomation element type names to ZeTa element roles */
478
+ function winTypeToRole(winType: string): string {
479
+ if (winType === 'Button') return 'button';
480
+ if (winType === 'Edit') return 'input';
481
+ if (winType === 'ComboBox') return 'select';
482
+ if (winType === 'CheckBox') return 'checkbox';
483
+ if (winType === 'RadioButton') return 'radio';
484
+ if (winType === 'Slider') return 'slider';
485
+ if (winType === 'ListItem') return 'option';
486
+ if (winType === 'MenuItem') return 'menuitem';
487
+ if (winType === 'Hyperlink') return 'link';
488
+ return winType.toLowerCase();
489
+ }
490
+
491
+ /** Map Mac2Driver XCUIElementType strings to ZeTa element roles */
492
+ function macTypeToRole(xcuiType: string): string {
493
+ if (xcuiType.includes('Button')) return 'button';
494
+ if (xcuiType.includes('SecureTextField')) return 'input';
495
+ if (xcuiType.includes('TextField')) return 'input';
496
+ if (xcuiType.includes('CheckBox')) return 'checkbox';
497
+ if (xcuiType.includes('RadioButton')) return 'radio';
498
+ if (xcuiType.includes('PopUpButton')) return 'select';
499
+ if (xcuiType.includes('ComboBox')) return 'select';
500
+ if (xcuiType.includes('Slider')) return 'slider';
501
+ if (xcuiType.includes('MenuItem')) return 'menuitem';
502
+ if (xcuiType.includes('SearchField')) return 'input';
503
+ if (xcuiType.includes('Link')) return 'link';
504
+ return xcuiType.replace('XCUIElementType', '').toLowerCase();
505
+ }
506
+
507
+ // ─── Gap 1: Native Auth ──────────────────────────────────────────────────────
508
+
509
+ /** Detect if the current screen is a login/auth screen and fill credentials. */
510
+ async function attemptNativeLogin(
511
+ driver: any,
512
+ platform: 'android' | 'ios' | 'windows' | 'macos',
513
+ credentials: NonNullable<NativeCrawlerOptions['credentials']>,
514
+ ): Promise<boolean> {
515
+ try {
516
+ const pageXml: string = await driver.getPageSource();
517
+ const elements = parseAccessibilityTree(pageXml, platform);
518
+
519
+ const inputEls = elements.filter((e) => e.role === 'input');
520
+ if (inputEls.length === 0) return false;
521
+
522
+ // Detect login screen heuristic: has at least one text input and one
523
+ // "password" input (SecureTextField on iOS, password type on Android)
524
+ const isPasswordField = (e: NativeElement) =>
525
+ e.className?.toLowerCase().includes('secure') ||
526
+ e.className?.toLowerCase().includes('password') ||
527
+ e.label?.toLowerCase().match(/password|passcode|pin|secret/i) != null;
528
+
529
+ const hasPasswordInput = elements.some(isPasswordField);
530
+ if (!hasPasswordInput && inputEls.length < 2) return false;
531
+
532
+ logger.info({ platform }, 'Native auth: login screen detected — filling credentials');
533
+
534
+ // Fill username field: first non-password input
535
+ const unLabel = credentials.usernameFieldLabel?.toLowerCase() ?? '';
536
+ const usernameEl = inputEls.find((e) => {
537
+ if (isPasswordField(e)) return false;
538
+ if (unLabel) return e.label?.toLowerCase().includes(unLabel);
539
+ return e.label?.toLowerCase().match(/email|username|user|login|phone|mobile/i) != null || true;
540
+ }) ?? inputEls[0];
541
+
542
+ if (usernameEl && credentials.username) {
543
+ const uSelector = usernameEl.resourceId
544
+ ? `id=${usernameEl.resourceId}`
545
+ : `~${usernameEl.label}`;
546
+ try {
547
+ const uEl = await driver.$(uSelector);
548
+ await uEl.click();
549
+ await driver.pause(300);
550
+ await uEl.clearValue();
551
+ await uEl.setValue(credentials.username);
552
+ } catch { /* non-fatal */ }
553
+ }
554
+
555
+ // Fill password field
556
+ const pwEl = elements.find(isPasswordField);
557
+ if (pwEl && credentials.password) {
558
+ const pSelector = pwEl.resourceId ? `id=${pwEl.resourceId}` : `~${pwEl.label}`;
559
+ try {
560
+ const pEl = await driver.$(pSelector);
561
+ await pEl.click();
562
+ await driver.pause(300);
563
+ await pEl.clearValue();
564
+ await pEl.setValue(credentials.password);
565
+ } catch { /* non-fatal */ }
566
+ }
567
+
568
+ // Dismiss keyboard then tap login button
569
+ try { await driver.hideKeyboard(); } catch { /* not always available */ }
570
+ await driver.pause(500);
571
+
572
+ const loginLabel = credentials.loginButtonLabel ?? '';
573
+ const loginBtn = elements.find((e) =>
574
+ e.role === 'button' &&
575
+ (loginLabel
576
+ ? e.label?.toLowerCase().includes(loginLabel.toLowerCase())
577
+ : e.label?.toLowerCase().match(/log\s?in|sign\s?in|submit|continue|next|connect/i) != null)
578
+ );
579
+ if (loginBtn) {
580
+ const btnSelector = loginBtn.resourceId ? `id=${loginBtn.resourceId}` : `~${loginBtn.label}`;
581
+ try {
582
+ await driver.$(btnSelector).click();
583
+ await driver.pause(3000); // wait for auth response
584
+ logger.info({ platform }, 'Native auth: login button tapped — waiting for app');
585
+ return true;
586
+ } catch { /* non-fatal */ }
587
+ }
588
+ return false;
589
+ } catch (err) {
590
+ logger.warn({ err }, 'Native auth attempt failed — continuing without login');
591
+ return false;
592
+ }
593
+ }
594
+
595
+ // ─── Gap 3: Gesture Navigation ───────────────────────────────────────────────
596
+
597
+ /** Perform a swipe gesture on the screen. */
598
+ async function performSwipe(
599
+ driver: any,
600
+ platform: 'android' | 'ios' | 'windows' | 'macos',
601
+ direction: 'left' | 'right' | 'up' | 'down',
602
+ ): Promise<void> {
603
+ try {
604
+ const size: { width: number; height: number } = await driver.getWindowSize();
605
+ const { width: W, height: H } = size;
606
+ const midX = Math.round(W / 2);
607
+ const midY = Math.round(H / 2);
608
+
609
+ if (platform === 'android') {
610
+ // Use W3C actions for Android
611
+ const startX = direction === 'left' ? Math.round(W * 0.8) : direction === 'right' ? Math.round(W * 0.2) : midX;
612
+ const startY = direction === 'up' ? Math.round(H * 0.8) : direction === 'down' ? Math.round(H * 0.2) : midY;
613
+ const endX = direction === 'left' ? Math.round(W * 0.2) : direction === 'right' ? Math.round(W * 0.8) : midX;
614
+ const endY = direction === 'up' ? Math.round(H * 0.2) : direction === 'down' ? Math.round(H * 0.8) : midY;
615
+ await driver.touchAction([
616
+ { action: 'press', x: startX, y: startY },
617
+ { action: 'wait', ms: 300 },
618
+ { action: 'moveTo', x: endX, y: endY },
619
+ { action: 'release' },
620
+ ]);
621
+ } else if (platform === 'ios') {
622
+ await driver.execute('mobile: swipe', { direction });
623
+ } else {
624
+ // Windows/macOS: use pointer actions
625
+ await driver.execute('mobile: scroll', { direction });
626
+ }
627
+ await driver.pause(500);
628
+ } catch { /* gesture not supported on this platform/driver — non-fatal */ }
629
+ }
630
+
631
+ /** Long-press an element by its selector. */
632
+ async function performLongPress(driver: any, selector: string, durationMs = 1200): Promise<void> {
633
+ try {
634
+ const el = await driver.$(selector);
635
+ await driver.touchAction([
636
+ { action: 'longPress', element: el, ms: durationMs },
637
+ { action: 'release' },
638
+ ]);
639
+ await driver.pause(600);
640
+ } catch { /* non-fatal */ }
641
+ }
642
+
643
+ // ─── Gap 2: Vision Fallback ───────────────────────────────────────────────────
644
+
645
+ /** When the accessibility tree yields zero interactive elements, use LLM vision to describe the screen. */
646
+ async function visionFallbackDescribeScreen(
647
+ screenshotBase64: string,
648
+ llmConfig: { provider: string; model: string; apiKey?: string; baseUrl?: string },
649
+ ): Promise<{ screenDescription: string; suggestedElements: Array<{ role: string; label: string }> }> {
650
+ try {
651
+ const Anthropic = (await import('@anthropic-ai/sdk')).default;
652
+ if (!llmConfig.apiKey) throw new Error('No LLM API key configured. Configure an Anthropic key in Workspace → Settings → AI Provider.');
653
+ const client = new Anthropic({ apiKey: llmConfig.apiKey });
654
+ const response = await client.messages.create({
655
+ model: llmConfig.model ?? 'claude-haiku-4-5-20251001',
656
+ max_tokens: 512,
657
+ messages: [{
658
+ role: 'user',
659
+ content: [
660
+ { type: 'image', source: { type: 'base64', media_type: 'image/png', data: screenshotBase64 } },
661
+ { type: 'text', text: 'This is a screenshot from a mobile app. The accessibility tree returned no elements. Describe what you see on screen in 1-2 sentences, then list ALL interactive UI elements (buttons, inputs, links, toggles) as JSON: [{"role":"button","label":"Sign In"},...]' },
662
+ ],
663
+ }],
664
+ });
665
+ const text = (response.content[0] as any)?.text ?? '';
666
+ const jsonMatch = text.match(/\[[\s\S]*\]/);
667
+ const elements = jsonMatch ? JSON.parse(jsonMatch[0]) : [];
668
+ return { screenDescription: text.split('[')[0]?.trim() ?? '', suggestedElements: elements };
669
+ } catch (err) {
670
+ logger.warn({ err }, 'Vision fallback LLM call failed');
671
+ }
672
+ return { screenDescription: 'Screen captured (accessibility tree empty — vision fallback unavailable)', suggestedElements: [] };
673
+ }
674
+
675
+ // ─── Gap 4: MITM Proxy API Capture ───────────────────────────────────────────
676
+
677
+ /** Start a lightweight proxy server to intercept native app API traffic. */
678
+ async function startMitmProxy(port: number): Promise<{ captured: Array<{ method: string; url: string; status?: number }>; stop: () => void }> {
679
+ const captured: Array<{ method: string; url: string; status?: number }> = [];
680
+ try {
681
+ const net = await import('net');
682
+ // Simple TCP intercept marker — full MITM requires SSL cert installation on device.
683
+ // This records connection attempts; full response interception requires device trust cert.
684
+ const server = net.createServer((socket) => {
685
+ socket.once('data', (data) => {
686
+ const raw = data.toString('utf8', 0, 512);
687
+ const match = /^(GET|POST|PUT|DELETE|PATCH|HEAD)\s+(https?:\/\/[^\s]+)/i.exec(raw);
688
+ if (match) captured.push({ method: match[1]!, url: match[2]! });
689
+ socket.destroy();
690
+ });
691
+ });
692
+ server.listen(port, '127.0.0.1');
693
+ const stop = () => server.close();
694
+ logger.info({ port }, 'MITM proxy listener started (connection-level only — install device trust cert for full interception)');
695
+ return { captured, stop };
696
+ } catch (err) {
697
+ logger.warn({ err }, 'MITM proxy start failed — API capture disabled');
698
+ return { captured, stop: () => {} };
699
+ }
700
+ }
701
+
702
+ /** Update the PlatformJob status/progress in DB — non-fatal. */
703
+ async function updateProgress(tenantId: string, jobId: string, progress: number, step: string): Promise<void> {
704
+ try {
705
+ await withTenant(tenantId, (tx) =>
706
+ tx.platformJob.update({
707
+ where: { id: jobId },
708
+ data: { progress, result: { step } as any },
709
+ })
710
+ );
711
+ } catch (err) {
712
+ logger.warn({ err }, 'Failed to update job progress (non-fatal)');
713
+ }
714
+ }
715
+
716
+ // ---------------------------------------------------------------------------
717
+ // Main export
718
+ // ---------------------------------------------------------------------------
719
+
720
+ /**
721
+ * Run a native app crawl session via Appium.
722
+ *
723
+ * Flow:
724
+ * 1. Download APK/IPA from S3 to local temp dir
725
+ * 2. Build Appium WebDriverIO capabilities for the target platform
726
+ * 3. Create WebDriverIO remote session (Appium server at APPIUM_SERVER_URL)
727
+ * 4. For each discovered screen:
728
+ * a. Get page source XML → parse elements
729
+ * b. Take screenshot → save to screenshotDir → persist via AppBrain
730
+ * c. Save screen + elements to DB
731
+ * d. Tap nav elements to discover next screens
732
+ * 5. Update PlatformJob as DONE (or FAILED on error)
733
+ */
734
+ export async function runNativeCrawl(opts: NativeCrawlerOptions): Promise<void> {
735
+ const { tenantId, projectId, jobId, platform } = opts;
736
+ const screenshotDir = path.join(config.screenshotDir, projectId);
737
+ await fs.mkdir(screenshotDir, { recursive: true });
738
+
739
+ let tmpAppPath: string | null = null;
740
+
741
+ // Mark job RUNNING
742
+ await withTenant(tenantId, (tx) =>
743
+ tx.platformJob.update({
744
+ where: { id: jobId },
745
+ data: { status: 'RUNNING', startedAt: new Date() },
746
+ })
747
+ );
748
+
749
+ const tenantStorageCfg = await getTenantStorageConfig(tenantId).catch(() => null);
750
+
751
+ try {
752
+ // ── Step 1: Download app binary from S3 / R2 ────────────────────────────
753
+ await updateProgress(tenantId, jobId, 5, 'Downloading app binary');
754
+ tmpAppPath = await downloadAppFromS3(opts.appS3Path, platform, opts, tenantStorageCfg);
755
+
756
+ // ── Step 2: Build Appium capabilities ───────────────────────────────────
757
+ await updateProgress(tenantId, jobId, 10, 'Building Appium session capabilities');
758
+ const capabilities = buildCapabilities(opts, tmpAppPath);
759
+
760
+ // ── Step 3: Start WebDriverIO session ───────────────────────────────────
761
+ // TODO: add "webdriverio": "^9.x" to packages/crawler/package.json
762
+ // TODO: requires Appium server running at APPIUM_SERVER_URL env var
763
+ await updateProgress(tenantId, jobId, 15, 'Starting Appium session');
764
+
765
+ const appiumServerUrl = opts.appiumServerUrl ?? process.env.APPIUM_SERVER_URL ?? 'http://127.0.0.1:4723';
766
+
767
+ // Pre-check connectivity before wdio throws an opaque EAI_AGAIN
768
+ try {
769
+ const probe = await fetch(`${appiumServerUrl}/status`, { signal: AbortSignal.timeout(5000) });
770
+ if (!probe.ok) throw new Error(`HTTP ${probe.status}`);
771
+ } catch (e: any) {
772
+ throw new Error(
773
+ `Appium server at ${appiumServerUrl} is not reachable (${e.message}). ` +
774
+ `Start it with: cd /opt/zeta-ai && docker compose -f docker-compose.prod.yml -f docker-compose.appium.yml up -d appium`
775
+ );
776
+ }
777
+
778
+ // TODO: add "webdriverio": "^9.x" to packages/crawler/package.json before deploying.
779
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
780
+ const wdio: any = await import('webdriverio' as any);
781
+ const driver = await wdio.remote({
782
+ hostname: new URL(appiumServerUrl).hostname,
783
+ port: parseInt(new URL(appiumServerUrl).port || '4723', 10),
784
+ path: '/',
785
+ capabilities: capabilities as any,
786
+ logLevel: 'warn',
787
+ });
788
+
789
+ logger.info({ platform, deviceFarm: opts.deviceFarm }, 'Appium session started');
790
+
791
+ try {
792
+ // Update DB with Appium session ID
793
+ const sessionId: string = driver.sessionId;
794
+ await withTenant(tenantId, (tx) =>
795
+ (tx as any).platformJob.update({
796
+ where: { id: jobId },
797
+ data: { appiumSessionId: sessionId },
798
+ })
799
+ );
800
+
801
+ // ── Step 4: Crawl screens ─────────────────────────────────────────────
802
+ await updateProgress(tenantId, jobId, 20, 'App launched — extracting initial screen');
803
+
804
+ // Gap 4: Start MITM proxy for API traffic capture
805
+ let mitmStop: (() => void) | null = null;
806
+ const mitmCaptured: Array<{ method: string; url: string; status?: number }> = [];
807
+ if (opts.mitmProxyPort) {
808
+ const mitm = await startMitmProxy(opts.mitmProxyPort);
809
+ mitmStop = mitm.stop;
810
+ mitmCaptured.push(...mitm.captured);
811
+ }
812
+
813
+ // Allow the app to reach its initial state
814
+ await driver.pause(2000);
815
+
816
+ // Gap 1: Attempt native login before BFS begins
817
+ if (opts.credentials?.username && opts.credentials?.password) {
818
+ const loggedIn = await attemptNativeLogin(driver, platform, opts.credentials);
819
+ if (loggedIn) await driver.pause(2500); // wait for post-login navigation
820
+ }
821
+
822
+ // Capture launch screen then navigate forward via discoverable nav elements.
823
+ const MAX_SCREENS = 30;
824
+ let screensFound = 0;
825
+ const tappedSelectors = new Set<string>();
826
+
827
+ for (let i = 0; i < MAX_SCREENS; i++) {
828
+ const pct = 20 + Math.round((i / MAX_SCREENS) * 60);
829
+ await updateProgress(tenantId, jobId, pct, `Capturing screen ${i + 1}`);
830
+
831
+ // a. Get screen title / name heuristic
832
+ let screenName = `Screen ${i + 1}`;
833
+ try {
834
+ // Android: use current activity name; iOS: use app title from accessibility tree
835
+ // Windows/macOS: use driver.getTitle() for the window title
836
+ if (platform === 'android') {
837
+ const activity: string = await (driver as any).getCurrentActivity();
838
+ screenName = activity.split('.').pop() ?? screenName;
839
+ } else if (platform === 'ios') {
840
+ // iOS: attempt to read the navigation bar title
841
+ const titleEl = await driver.$('~NavigationBarTitle').catch(() => null);
842
+ if (titleEl) {
843
+ const titleText = await (titleEl as any).getText().catch(() => '');
844
+ if (titleText) screenName = titleText;
845
+ }
846
+ } else {
847
+ // windows / macos: use window title
848
+ const winTitle: string = await driver.getTitle().catch(() => '');
849
+ if (winTitle) screenName = winTitle;
850
+ }
851
+ } catch { /* non-fatal — use default name */ }
852
+
853
+ // b. Take screenshot
854
+ const screenshotBase64: string = await driver.takeScreenshot();
855
+ const screenshotBuffer = Buffer.from(screenshotBase64, 'base64');
856
+
857
+ // DPR normalization: resize physical pixels → logical pixels
858
+ let screenshotBuf = screenshotBuffer;
859
+ try {
860
+ const sharp = (await import('sharp')).default;
861
+ const meta = await sharp(screenshotBuffer).metadata();
862
+ const logicalSize: { width: number; height: number } = await (driver as any).getWindowSize().catch(() => ({ width: meta.width ?? 0, height: meta.height ?? 0 }));
863
+ const physW = meta.width ?? 0;
864
+ const logW = logicalSize.width;
865
+ if (physW > 0 && logW > 0 && physW > logW) {
866
+ const logH = Math.round((meta.height ?? physW) * (logW / physW));
867
+ screenshotBuf = Buffer.from(await sharp(screenshotBuffer).resize(logW, logH, { fit: 'fill' }).png().toBuffer());
868
+ }
869
+ } catch { /* sharp unavailable or resize failed — use raw buffer */ }
870
+
871
+ // Save screen record first (to get screen.id)
872
+ const screen = await AppBrain.saveScreen(tenantId, projectId, screenName);
873
+ const shotPath = path.join(screenshotDir, `${screen.id}-native-v${screen.version}.png`);
874
+ await fs.writeFile(shotPath, screenshotBuf);
875
+ await AppBrain.saveScreenshot(tenantId, projectId, screen.id, shotPath, screen.version, 'NATIVE');
876
+ if (tenantStorageCfg) {
877
+ const s3Key = `${tenantId}/${projectId}/screenshots/${screen.id}-native-v${screen.version}.png`;
878
+ await uploadScreenshotToTenantStorage(tenantStorageCfg, shotPath, s3Key).catch(() => {});
879
+ }
880
+
881
+ // c. Parse accessibility tree → save elements
882
+ // Scroll down to reveal below-fold elements, then scroll back up before parsing
883
+ try {
884
+ if (platform === 'android') {
885
+ await driver.execute('mobile: scroll', { direction: 'down' });
886
+ } else if (platform === 'ios') {
887
+ await driver.execute('mobile: scroll', { direction: 'down', element: null });
888
+ }
889
+ await driver.pause(400);
890
+ } catch { /* scroll not always available — non-fatal */ }
891
+
892
+ const pageXml: string = await driver.getPageSource();
893
+ const nativeElements = parseAccessibilityTree(pageXml, platform);
894
+ try {
895
+ if (platform === 'android') {
896
+ await driver.execute('mobile: scroll', { direction: 'up' });
897
+ } else if (platform === 'ios') {
898
+ await driver.execute('mobile: scroll', { direction: 'up', element: null });
899
+ }
900
+ await driver.pause(300);
901
+ } catch { /* non-fatal */ }
902
+ logger.info({ screenName, elements: nativeElements.length }, 'Native screen parsed');
903
+
904
+ // Gap 2: Vision fallback when accessibility tree is empty (Flutter / game engines)
905
+ if (nativeElements.length === 0 && opts.visionLlmConfig) {
906
+ logger.info({ screenName }, 'Empty accessibility tree — triggering vision LLM fallback');
907
+ const { suggestedElements } = await visionFallbackDescribeScreen(screenshotBase64, opts.visionLlmConfig);
908
+ for (const el of suggestedElements) {
909
+ nativeElements.push({
910
+ role: el.role, label: el.label, resourceId: '', className: '',
911
+ bounds: { x: 0, y: 0, width: 0, height: 0 }, platform,
912
+ });
913
+ }
914
+ }
915
+
916
+ for (const el of nativeElements) {
917
+ const boundingRect = {
918
+ x: Math.round(el.bounds.x),
919
+ y: Math.round(el.bounds.y),
920
+ width: Math.round(el.bounds.width),
921
+ height: Math.round(el.bounds.height),
922
+ };
923
+ const notes = JSON.stringify({
924
+ ...(el.resourceId ? { resourceId: el.resourceId } : {}),
925
+ ...(el.className ? { className: el.className } : {}),
926
+ platform: el.platform,
927
+ });
928
+ await AppBrain.saveElement(
929
+ tenantId,
930
+ projectId,
931
+ screen.id,
932
+ el.label, // meaning
933
+ el.role,
934
+ undefined, // expectedData
935
+ notes,
936
+ undefined, // text
937
+ true, // isInteractive
938
+ boundingRect,
939
+ undefined, // ariaState
940
+ undefined, // parentLandmark
941
+ 'NATIVE', // viewport
942
+ );
943
+ }
944
+
945
+ screensFound++;
946
+
947
+ // d. Navigate to next screen — basic BFS via nav-like elements
948
+ const navElements = nativeElements.filter(el =>
949
+ ['button', 'text'].includes(el.role) ||
950
+ el.label?.match(/menu|home|back|next|continue|tab|nav|dashboard|settings|profile|more|list|search|help|about/i)
951
+ );
952
+
953
+ for (const navEl of navElements.slice(0, 15)) {
954
+ if (screensFound >= MAX_SCREENS) break;
955
+ try {
956
+ // Build a selector for this element
957
+ let selector: string;
958
+ if (platform === 'android' && navEl.resourceId) {
959
+ selector = `id=${navEl.resourceId}`;
960
+ } else if (navEl.label) {
961
+ selector = `~${navEl.label}`;
962
+ } else {
963
+ continue;
964
+ }
965
+
966
+ if (tappedSelectors.has(selector)) continue;
967
+ tappedSelectors.add(selector);
968
+
969
+ // Capture pre-tap screen identity
970
+ let preActivity = '';
971
+ try {
972
+ if (platform === 'android') {
973
+ preActivity = await (driver as any).getCurrentActivity();
974
+ } else {
975
+ preActivity = await driver.getTitle();
976
+ }
977
+ } catch { /* non-fatal */ }
978
+
979
+ await driver.$(selector).click();
980
+ await driver.pause(1500);
981
+
982
+ // Android crash detection via logcat
983
+ if (platform === 'android') {
984
+ try {
985
+ const logs: any[] = await (driver as any).getLogs('logcat');
986
+ const crashLog = logs.find((l: any) =>
987
+ typeof l.message === 'string' && /FATAL EXCEPTION|Process.*has died|ANR in/i.test(l.message)
988
+ );
989
+ if (crashLog) {
990
+ logger.error({ jobId, crash: String(crashLog.message).slice(0, 500) }, 'App crash detected during crawl');
991
+ break; // stop navigating from this screen
992
+ }
993
+ } catch { /* logcat not available on all environments — non-fatal */ }
994
+ }
995
+ // iOS crash log detection
996
+ if (platform === 'ios') {
997
+ const crashLogs: any[] = await (driver as any).getLogs('crashlog').catch(() => []);
998
+ if (crashLogs.length > 0 && crashLogs.some((l: any) => l.message?.includes('Exception') || l.message?.includes('crash'))) {
999
+ logger.warn({ screenName }, 'iOS crash log detected — stopping crawl');
1000
+ break;
1001
+ }
1002
+ }
1003
+
1004
+ // Check if screen changed
1005
+ let postActivity = '';
1006
+ try {
1007
+ if (platform === 'android') {
1008
+ postActivity = await (driver as any).getCurrentActivity();
1009
+ } else {
1010
+ postActivity = await driver.getTitle();
1011
+ }
1012
+ } catch { /* non-fatal */ }
1013
+
1014
+ if (postActivity === preActivity) continue; // same screen — skip
1015
+
1016
+ // Check MAX_SCREENS before saving the new screen
1017
+ if (screensFound >= MAX_SCREENS) break;
1018
+
1019
+ // New screen — capture it
1020
+ const newScreenName = postActivity
1021
+ ? postActivity.split('.').pop() ?? `Screen ${screensFound + 1}`
1022
+ : `Screen ${screensFound + 1}`;
1023
+ const newScreenshot: string = await driver.takeScreenshot();
1024
+ const newScreen = await AppBrain.saveScreen(tenantId, projectId, newScreenName);
1025
+ const newShotPath = path.join(screenshotDir, `${newScreen.id}-native-v${newScreen.version}.png`);
1026
+ await fs.writeFile(newShotPath, Buffer.from(newScreenshot, 'base64'));
1027
+ await AppBrain.saveScreenshot(tenantId, projectId, newScreen.id, newShotPath, newScreen.version, 'NATIVE');
1028
+ if (tenantStorageCfg) {
1029
+ const s3Key = `${tenantId}/${projectId}/screenshots/${newScreen.id}-native-v${newScreen.version}.png`;
1030
+ await uploadScreenshotToTenantStorage(tenantStorageCfg, newShotPath, s3Key).catch(() => {});
1031
+ }
1032
+
1033
+ const newXml: string = await driver.getPageSource();
1034
+ const newElements = parseAccessibilityTree(newXml, platform);
1035
+ for (const el of newElements) {
1036
+ await AppBrain.saveElement(
1037
+ tenantId, projectId, newScreen.id,
1038
+ el.label, el.role, undefined,
1039
+ JSON.stringify({ ...(el.resourceId ? { resourceId: el.resourceId } : {}), ...(el.className ? { className: el.className } : {}), platform: el.platform }),
1040
+ undefined, true,
1041
+ { x: Math.round(el.bounds.x), y: Math.round(el.bounds.y), width: Math.round(el.bounds.width), height: Math.round(el.bounds.height) },
1042
+ undefined, undefined, 'NATIVE',
1043
+ );
1044
+ }
1045
+ screensFound++;
1046
+
1047
+ // Navigate back so subsequent nav attempts start from a known state
1048
+ try { await driver.back(); await driver.pause(800); } catch { /* non-fatal */ }
1049
+ } catch { /* element may have disappeared or tap failed — skip */ }
1050
+ }
1051
+
1052
+ // Gap 3: Gesture-based discovery — swipe to reveal off-screen content
1053
+ if (opts.gestureConfig?.enableSwipe && screensFound < MAX_SCREENS) {
1054
+ for (const dir of ['left', 'right', 'up', 'down'] as const) {
1055
+ await performSwipe(driver, platform, dir);
1056
+ const postSwipeXml = await driver.getPageSource().catch(() => '');
1057
+ if (postSwipeXml && postSwipeXml !== pageXml) break; // new content found
1058
+ }
1059
+ }
1060
+
1061
+ // Nav pass complete for this screen — continue BFS to next iteration.
1062
+ }
1063
+
1064
+ // ── Step 5: Mark job DONE ─────────────────────────────────────────────
1065
+ if (mitmStop) mitmStop();
1066
+ await driver.deleteSession();
1067
+ await updateProgress(tenantId, jobId, 99, `Crawl complete — ${screensFound} screen(s) captured`);
1068
+ await withTenant(tenantId, (tx) =>
1069
+ tx.platformJob.update({
1070
+ where: { id: jobId },
1071
+ data: {
1072
+ status: 'DONE',
1073
+ progress: 100,
1074
+ finishedAt: new Date(),
1075
+ result: { screensFound, platform } as any,
1076
+ },
1077
+ })
1078
+ );
1079
+ logger.info({ jobId, screensFound, platform }, 'Native crawl complete');
1080
+
1081
+ } catch (sessionErr) {
1082
+ // Best-effort: clean up Appium session before re-throwing
1083
+ try { await driver.deleteSession(); } catch { /* ignore */ }
1084
+ throw sessionErr;
1085
+ }
1086
+
1087
+ } catch (err: any) {
1088
+ logger.error({ jobId, err: String(err) }, 'Native crawl failed');
1089
+ await withTenant(tenantId, (tx) =>
1090
+ tx.platformJob.update({
1091
+ where: { id: jobId },
1092
+ data: {
1093
+ status: 'FAILED',
1094
+ error: String(err?.message ?? err),
1095
+ finishedAt: new Date(),
1096
+ },
1097
+ })
1098
+ ).catch(() => {});
1099
+ throw err;
1100
+ } finally {
1101
+ // Clean up local temp file
1102
+ if (tmpAppPath) {
1103
+ fs.unlink(tmpAppPath).catch(() => {});
1104
+ }
1105
+ }
1106
+ }