@native-sdk/cli 0.0.0 → 0.4.0

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 (367) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/bin/native.js +156 -0
  5. package/build/app.zig +716 -0
  6. package/build.zig +2593 -0
  7. package/build.zig.zon +24 -0
  8. package/native-sdk.d.ts +615 -0
  9. package/package.json +44 -8
  10. package/skill-data/automation/SKILL.md +248 -0
  11. package/skill-data/core/SKILL.md +249 -0
  12. package/skill-data/core/references/app-model-runtime.md +141 -0
  13. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  14. package/skill-data/core/references/frontend-assets.md +124 -0
  15. package/skill-data/core/references/project-anatomy.md +108 -0
  16. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  17. package/skill-data/native-ui/SKILL.md +1080 -0
  18. package/skills/native-sdk/SKILL.md +33 -0
  19. package/src/app_runner/root.zig +740 -0
  20. package/src/assets/root.zig +20 -0
  21. package/src/automation/protocol.zig +285 -0
  22. package/src/automation/root.zig +13 -0
  23. package/src/automation/server.zig +506 -0
  24. package/src/automation/snapshot.zig +1202 -0
  25. package/src/automation/watcher.zig +165 -0
  26. package/src/bridge/root.zig +507 -0
  27. package/src/debug/root.zig +246 -0
  28. package/src/embed/app_exports.zig +11 -0
  29. package/src/embed/c_api.zig +793 -0
  30. package/src/embed/c_exports.zig +10 -0
  31. package/src/embed/chrome.zig +168 -0
  32. package/src/embed/conversions.zig +359 -0
  33. package/src/embed/host.zig +960 -0
  34. package/src/embed/root.zig +81 -0
  35. package/src/embed/tests.zig +2552 -0
  36. package/src/embed/types.zig +382 -0
  37. package/src/embed/ui_host.zig +289 -0
  38. package/src/extensions/root.zig +197 -0
  39. package/src/frontend/root.zig +48 -0
  40. package/src/js/root.zig +65 -0
  41. package/src/platform/android/NativeSdkActivity.java +1370 -0
  42. package/src/platform/android/android_host.c +696 -0
  43. package/src/platform/android/files.zig +9 -0
  44. package/src/platform/android/native_sdk_app.h +237 -0
  45. package/src/platform/ios/files.zig +8 -0
  46. package/src/platform/ios/native_sdk_app.h +289 -0
  47. package/src/platform/ios/uikit_host.m +2739 -0
  48. package/src/platform/linux/cef_host.cpp +599 -0
  49. package/src/platform/linux/gtk_host.c +5362 -0
  50. package/src/platform/linux/gtk_host.h +265 -0
  51. package/src/platform/linux/root.zig +1519 -0
  52. package/src/platform/macos/appkit_host.h +604 -0
  53. package/src/platform/macos/appkit_host.m +10392 -0
  54. package/src/platform/macos/automation_host.h +14 -0
  55. package/src/platform/macos/automation_host.m +12 -0
  56. package/src/platform/macos/cef_host.mm +3028 -0
  57. package/src/platform/macos/root.zig +2260 -0
  58. package/src/platform/null_platform.zig +2232 -0
  59. package/src/platform/null_platform_tests.zig +670 -0
  60. package/src/platform/policy_values.zig +14 -0
  61. package/src/platform/root.zig +181 -0
  62. package/src/platform/types.zig +2681 -0
  63. package/src/platform/windows/cef_host.cpp +3 -0
  64. package/src/platform/windows/root.zig +1521 -0
  65. package/src/platform/windows/webview2_host.cpp +6230 -0
  66. package/src/primitives/app_dirs/root.zig +440 -0
  67. package/src/primitives/app_manifest/root.zig +109 -0
  68. package/src/primitives/app_manifest/tests.zig +758 -0
  69. package/src/primitives/app_manifest/types.zig +587 -0
  70. package/src/primitives/app_manifest/validation.zig +828 -0
  71. package/src/primitives/assets/root.zig +520 -0
  72. package/src/primitives/canvas/a11y_audit.zig +390 -0
  73. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  74. package/src/primitives/canvas/app_icon.zig +1590 -0
  75. package/src/primitives/canvas/chart.zig +392 -0
  76. package/src/primitives/canvas/chart_tests.zig +751 -0
  77. package/src/primitives/canvas/commands.zig +446 -0
  78. package/src/primitives/canvas/drawing.zig +309 -0
  79. package/src/primitives/canvas/equality.zig +316 -0
  80. package/src/primitives/canvas/events.zig +696 -0
  81. package/src/primitives/canvas/font_coverage.zig +118 -0
  82. package/src/primitives/canvas/font_ttf.zig +510 -0
  83. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  84. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  85. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  86. package/src/primitives/canvas/frame.zig +679 -0
  87. package/src/primitives/canvas/frame_metrics.zig +462 -0
  88. package/src/primitives/canvas/gpu.zig +639 -0
  89. package/src/primitives/canvas/hash.zig +112 -0
  90. package/src/primitives/canvas/icons/alert.svg +1 -0
  91. package/src/primitives/canvas/icons/archive.svg +1 -0
  92. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  93. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  94. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  95. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  96. package/src/primitives/canvas/icons/check.svg +1 -0
  97. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  98. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  99. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  100. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  101. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  102. package/src/primitives/canvas/icons/clock.svg +1 -0
  103. package/src/primitives/canvas/icons/copy.svg +1 -0
  104. package/src/primitives/canvas/icons/download.svg +1 -0
  105. package/src/primitives/canvas/icons/edit.svg +1 -0
  106. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  107. package/src/primitives/canvas/icons/external-link.svg +1 -0
  108. package/src/primitives/canvas/icons/eye.svg +1 -0
  109. package/src/primitives/canvas/icons/file-text.svg +1 -0
  110. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  111. package/src/primitives/canvas/icons/folder.svg +1 -0
  112. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  113. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  114. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  115. package/src/primitives/canvas/icons/info.svg +1 -0
  116. package/src/primitives/canvas/icons/menu.svg +1 -0
  117. package/src/primitives/canvas/icons/missing.svg +1 -0
  118. package/src/primitives/canvas/icons/moon.svg +1 -0
  119. package/src/primitives/canvas/icons/music.svg +1 -0
  120. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  121. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  122. package/src/primitives/canvas/icons/pause.svg +1 -0
  123. package/src/primitives/canvas/icons/play.svg +1 -0
  124. package/src/primitives/canvas/icons/plus.svg +1 -0
  125. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  126. package/src/primitives/canvas/icons/repeat.svg +1 -0
  127. package/src/primitives/canvas/icons/save.svg +1 -0
  128. package/src/primitives/canvas/icons/search.svg +1 -0
  129. package/src/primitives/canvas/icons/send.svg +1 -0
  130. package/src/primitives/canvas/icons/settings.svg +1 -0
  131. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  132. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  133. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  134. package/src/primitives/canvas/icons/sun.svg +1 -0
  135. package/src/primitives/canvas/icons/terminal.svg +1 -0
  136. package/src/primitives/canvas/icons/trash.svg +1 -0
  137. package/src/primitives/canvas/icons/volume.svg +1 -0
  138. package/src/primitives/canvas/icons/wrench.svg +1 -0
  139. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  140. package/src/primitives/canvas/icons/x.svg +1 -0
  141. package/src/primitives/canvas/icons.zig +249 -0
  142. package/src/primitives/canvas/layout_audit.zig +744 -0
  143. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  144. package/src/primitives/canvas/markdown.zig +1031 -0
  145. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  146. package/src/primitives/canvas/markdown_tests.zig +538 -0
  147. package/src/primitives/canvas/plan_key_index.zig +120 -0
  148. package/src/primitives/canvas/png.zig +355 -0
  149. package/src/primitives/canvas/reference.zig +1457 -0
  150. package/src/primitives/canvas/reference_blur.zig +104 -0
  151. package/src/primitives/canvas/reference_memo.zig +322 -0
  152. package/src/primitives/canvas/reference_paths.zig +184 -0
  153. package/src/primitives/canvas/reference_tests.zig +1650 -0
  154. package/src/primitives/canvas/render.zig +747 -0
  155. package/src/primitives/canvas/render_effects.zig +255 -0
  156. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  157. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  158. package/src/primitives/canvas/render_images.zig +247 -0
  159. package/src/primitives/canvas/render_layers.zig +260 -0
  160. package/src/primitives/canvas/render_paths.zig +347 -0
  161. package/src/primitives/canvas/render_resources.zig +51 -0
  162. package/src/primitives/canvas/render_tests.zig +3186 -0
  163. package/src/primitives/canvas/root.zig +744 -0
  164. package/src/primitives/canvas/serialization.zig +1528 -0
  165. package/src/primitives/canvas/svg_icon.zig +636 -0
  166. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  167. package/src/primitives/canvas/test_support.zig +422 -0
  168. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  169. package/src/primitives/canvas/tests.zig +30 -0
  170. package/src/primitives/canvas/text.zig +82 -0
  171. package/src/primitives/canvas/text_atlas.zig +408 -0
  172. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  173. package/src/primitives/canvas/text_interaction.zig +615 -0
  174. package/src/primitives/canvas/text_layout.zig +1069 -0
  175. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  176. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  177. package/src/primitives/canvas/text_layout_types.zig +128 -0
  178. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  179. package/src/primitives/canvas/text_metrics.zig +309 -0
  180. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  181. package/src/primitives/canvas/text_span_tests.zig +608 -0
  182. package/src/primitives/canvas/text_spans.zig +1010 -0
  183. package/src/primitives/canvas/text_tests.zig +1949 -0
  184. package/src/primitives/canvas/themes/geist.zig +470 -0
  185. package/src/primitives/canvas/tokens.zig +1522 -0
  186. package/src/primitives/canvas/ui.zig +2686 -0
  187. package/src/primitives/canvas/ui_markup.zig +3791 -0
  188. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  189. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  190. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  191. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  192. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  193. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  194. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  195. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  196. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  197. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  198. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  199. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  200. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  201. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  202. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  203. package/src/primitives/canvas/ui_provenance.zig +96 -0
  204. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  205. package/src/primitives/canvas/ui_schema.zig +825 -0
  206. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  207. package/src/primitives/canvas/ui_tests.zig +1432 -0
  208. package/src/primitives/canvas/vector.zig +919 -0
  209. package/src/primitives/canvas/vector_tests.zig +547 -0
  210. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  211. package/src/primitives/canvas/virtual_extents.zig +540 -0
  212. package/src/primitives/canvas/widget_access.zig +208 -0
  213. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  214. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  215. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  216. package/src/primitives/canvas/widget_layout.zig +2055 -0
  217. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  218. package/src/primitives/canvas/widget_metrics.zig +228 -0
  219. package/src/primitives/canvas/widget_render.zig +2534 -0
  220. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  221. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  222. package/src/primitives/canvas/widget_render_style.zig +626 -0
  223. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  224. package/src/primitives/canvas/widget_routing.zig +475 -0
  225. package/src/primitives/canvas/widget_runtime.zig +307 -0
  226. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  227. package/src/primitives/canvas/widget_semantics.zig +480 -0
  228. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  229. package/src/primitives/canvas/widget_tests.zig +8 -0
  230. package/src/primitives/canvas/widget_text_input.zig +351 -0
  231. package/src/primitives/canvas/widget_text_select.zig +147 -0
  232. package/src/primitives/canvas/widget_tree.zig +232 -0
  233. package/src/primitives/canvas/widgets.zig +1319 -0
  234. package/src/primitives/diagnostics/root.zig +445 -0
  235. package/src/primitives/geometry/root.zig +889 -0
  236. package/src/primitives/json/root.zig +272 -0
  237. package/src/primitives/platform_info/root.zig +450 -0
  238. package/src/primitives/trace/root.zig +569 -0
  239. package/src/root.zig +165 -0
  240. package/src/runtime/api.zig +431 -0
  241. package/src/runtime/async_bridge.zig +49 -0
  242. package/src/runtime/automation_commands.zig +686 -0
  243. package/src/runtime/automation_liveness_tests.zig +140 -0
  244. package/src/runtime/automation_snapshot.zig +246 -0
  245. package/src/runtime/automation_widget_dispatch.zig +617 -0
  246. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  247. package/src/runtime/bridge_payload.zig +194 -0
  248. package/src/runtime/bridge_permission_tests.zig +551 -0
  249. package/src/runtime/bridge_responses.zig +414 -0
  250. package/src/runtime/builtin_bridge.zig +554 -0
  251. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  252. package/src/runtime/canvas_font_tests.zig +410 -0
  253. package/src/runtime/canvas_fonts.zig +229 -0
  254. package/src/runtime/canvas_frame.zig +1734 -0
  255. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  256. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  257. package/src/runtime/canvas_frame_helpers.zig +414 -0
  258. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  259. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  260. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  261. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  262. package/src/runtime/canvas_frame_tests.zig +9 -0
  263. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  264. package/src/runtime/canvas_image_tests.zig +362 -0
  265. package/src/runtime/canvas_images.zig +224 -0
  266. package/src/runtime/canvas_limits.zig +317 -0
  267. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  268. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  269. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  270. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  271. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  272. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  273. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  274. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  275. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  276. package/src/runtime/canvas_widget_display.zig +677 -0
  277. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  278. package/src/runtime/canvas_widget_events.zig +1098 -0
  279. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  280. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  281. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  282. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  283. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  284. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  285. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  286. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  287. package/src/runtime/canvas_widget_state.zig +798 -0
  288. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  289. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  290. package/src/runtime/clock.zig +231 -0
  291. package/src/runtime/core.zig +927 -0
  292. package/src/runtime/effects.zig +4197 -0
  293. package/src/runtime/effects_audio_tests.zig +690 -0
  294. package/src/runtime/effects_clipboard_tests.zig +317 -0
  295. package/src/runtime/effects_fetch_tests.zig +1031 -0
  296. package/src/runtime/effects_file_tests.zig +407 -0
  297. package/src/runtime/effects_tests.zig +1251 -0
  298. package/src/runtime/flow.zig +1175 -0
  299. package/src/runtime/frame_profile.zig +221 -0
  300. package/src/runtime/gpu_surface_events.zig +639 -0
  301. package/src/runtime/launch_timing.zig +59 -0
  302. package/src/runtime/markdown_app_tests.zig +224 -0
  303. package/src/runtime/native_command_event_tests.zig +431 -0
  304. package/src/runtime/platform_bridge_tests.zig +8 -0
  305. package/src/runtime/platform_config_tests.zig +392 -0
  306. package/src/runtime/root.zig +150 -0
  307. package/src/runtime/runtime_event_tests.zig +1079 -0
  308. package/src/runtime/session_journal.zig +1522 -0
  309. package/src/runtime/session_record.zig +332 -0
  310. package/src/runtime/session_replay.zig +274 -0
  311. package/src/runtime/session_state.zig +30 -0
  312. package/src/runtime/session_tests.zig +399 -0
  313. package/src/runtime/shell_layout.zig +472 -0
  314. package/src/runtime/shell_layout_tests.zig +1246 -0
  315. package/src/runtime/shell_view_tests.zig +604 -0
  316. package/src/runtime/state.zig +110 -0
  317. package/src/runtime/system_services.zig +392 -0
  318. package/src/runtime/test_support.zig +79 -0
  319. package/src/runtime/tests.zig +30 -0
  320. package/src/runtime/ui_app.zig +3290 -0
  321. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  322. package/src/runtime/ui_app_provenance.zig +370 -0
  323. package/src/runtime/ui_app_tests.zig +3254 -0
  324. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  325. package/src/runtime/ui_app_window_tests.zig +442 -0
  326. package/src/runtime/validation.zig +206 -0
  327. package/src/runtime/view.zig +908 -0
  328. package/src/runtime/view_canvas.zig +850 -0
  329. package/src/runtime/view_widget_control.zig +433 -0
  330. package/src/runtime/view_widget_scroll.zig +360 -0
  331. package/src/runtime/view_widget_text.zig +275 -0
  332. package/src/runtime/view_widget_tree.zig +843 -0
  333. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  334. package/src/runtime/widget_bridge.zig +231 -0
  335. package/src/runtime/window_command_bridge_tests.zig +403 -0
  336. package/src/runtime/window_storage.zig +255 -0
  337. package/src/runtime/window_view_runtime.zig +727 -0
  338. package/src/runtime/window_views.zig +334 -0
  339. package/src/security/root.zig +118 -0
  340. package/src/tooling/android.zig +1172 -0
  341. package/src/tooling/assets.zig +128 -0
  342. package/src/tooling/buildgraph.zig +379 -0
  343. package/src/tooling/cef.zig +671 -0
  344. package/src/tooling/codesign.zig +172 -0
  345. package/src/tooling/components/identity_tests.zig +185 -0
  346. package/src/tooling/components/stepper.zig +113 -0
  347. package/src/tooling/components/timeline.native +27 -0
  348. package/src/tooling/components/timeline_item.zig +131 -0
  349. package/src/tooling/default_icon.icns +0 -0
  350. package/src/tooling/default_icon.png +0 -0
  351. package/src/tooling/dev.zig +172 -0
  352. package/src/tooling/doctor.zig +349 -0
  353. package/src/tooling/eject_components.zig +170 -0
  354. package/src/tooling/embedlib.zig +290 -0
  355. package/src/tooling/ios.zig +528 -0
  356. package/src/tooling/manifest.zig +1939 -0
  357. package/src/tooling/package.zig +2388 -0
  358. package/src/tooling/process_tree.zig +114 -0
  359. package/src/tooling/raw_manifest.zig +193 -0
  360. package/src/tooling/root.zig +22 -0
  361. package/src/tooling/templates.zig +3191 -0
  362. package/src/tooling/toolchain.zig +371 -0
  363. package/src/tooling/verbs.zig +204 -0
  364. package/src/tooling/web_engine.zig +135 -0
  365. package/src/tooling/xcodeproj.zig +462 -0
  366. package/src/window_state/root.zig +507 -0
  367. package/index.js +0 -2
@@ -0,0 +1,506 @@
1
+ const std = @import("std");
2
+ const geometry = @import("geometry");
3
+ const protocol = @import("protocol.zig");
4
+ const snapshot = @import("snapshot.zig");
5
+
6
+ /// The dropbox protocol is filesystem-backed; targets without one
7
+ /// (freestanding wasm) publish and consume nothing. Comptime-known so
8
+ /// `std.Io.Dir.cwd()` (posix) is never analyzed on those targets.
9
+ const has_filesystem = switch (@import("builtin").os.tag) {
10
+ .freestanding, .emscripten => false,
11
+ else => true,
12
+ };
13
+
14
+ const snapshot_initial_capacity: usize = 16 * 1024;
15
+ const windows_initial_capacity: usize = 1024;
16
+
17
+ /// Longest possible queue entry name: `command-` + a u64 in decimal
18
+ /// (at most 20 digits) + `.txt`, comfortably under 64.
19
+ const max_queue_name_bytes: usize = 64;
20
+
21
+ pub const Server = struct {
22
+ io: std.Io,
23
+ directory: []const u8 = protocol.default_dir,
24
+ title: []const u8 = "native-sdk",
25
+ /// How long an incomplete queue entry (claimed name, line not fully
26
+ /// written yet — no trailing newline) may sit at the head before the
27
+ /// drain reaps it. A writer lands its whole line in ONE small write
28
+ /// immediately after claiming the name, so the incomplete state is
29
+ /// normally a microseconds-wide race the next frame resolves; an
30
+ /// entry still incomplete after this long has a dead writer behind
31
+ /// it, and leaving it in place would wedge the queue head forever
32
+ /// (strict FIFO means the drain never skips past it). Overridable so
33
+ /// tests can exercise the reap without real waiting.
34
+ abandoned_entry_reap_ns: i128 = 2 * std.time.ns_per_s,
35
+
36
+ pub fn init(io: std.Io, directory: []const u8, title: []const u8) Server {
37
+ return .{ .io = io, .directory = directory, .title = title };
38
+ }
39
+
40
+ pub fn publish(self: Server, input_value: snapshot.Input) !void {
41
+ if (!has_filesystem) return;
42
+ var cwd = std.Io.Dir.cwd();
43
+ try cwd.createDirPath(self.io, self.directory);
44
+ var writer = try std.Io.Writer.Allocating.initCapacity(std.heap.page_allocator, snapshot_initial_capacity);
45
+ defer writer.deinit();
46
+ try snapshot.writeText(input_value, &writer.writer);
47
+ var path_buffer: [256]u8 = undefined;
48
+ try writePath(self.io, self.path("snapshot.txt", &path_buffer), writer.written());
49
+ var a11y_writer = try std.Io.Writer.Allocating.initCapacity(std.heap.page_allocator, snapshot_initial_capacity);
50
+ defer a11y_writer.deinit();
51
+ try snapshot.writeA11yText(input_value, &a11y_writer.writer);
52
+ try writePath(self.io, self.path("accessibility.txt", &path_buffer), a11y_writer.written());
53
+ var windows_writer = try std.Io.Writer.Allocating.initCapacity(std.heap.page_allocator, windows_initial_capacity);
54
+ defer windows_writer.deinit();
55
+ for (input_value.windows) |window| {
56
+ try windows_writer.writer.print("window @w{d} \"{s}\" focused={any}\n", .{ window.id, window.title, window.focused });
57
+ }
58
+ try writePath(self.io, self.path("windows.txt", &path_buffer), windows_writer.written());
59
+ }
60
+
61
+ pub fn publishBridgeResponse(self: Server, response: []const u8) !void {
62
+ if (!has_filesystem) return;
63
+ var cwd = std.Io.Dir.cwd();
64
+ try cwd.createDirPath(self.io, self.directory);
65
+ var path_buffer: [256]u8 = undefined;
66
+ try writePath(self.io, self.path("bridge-response.txt", &path_buffer), response);
67
+ }
68
+
69
+ /// Response artifact for the `provenance` verb: the queried widget's
70
+ /// authored-markup record, or the teaching error saying why there is
71
+ /// none. One command in flight at a time, like the bridge response.
72
+ pub fn publishProvenanceResponse(self: Server, response: []const u8) !void {
73
+ if (!has_filesystem) return;
74
+ var cwd = std.Io.Dir.cwd();
75
+ try cwd.createDirPath(self.io, self.directory);
76
+ var path_buffer: [256]u8 = undefined;
77
+ try writePath(self.io, self.path("provenance.txt", &path_buffer), response);
78
+ }
79
+
80
+ /// Write a view screenshot artifact (`screenshot-<label>.png`). The
81
+ /// bytes land in a temporary file first and are renamed into place so
82
+ /// pollers never observe a partially written PNG.
83
+ pub fn publishScreenshot(self: Server, view_label: []const u8, png_bytes: []const u8) !void {
84
+ if (!has_filesystem) return;
85
+ var cwd = std.Io.Dir.cwd();
86
+ try cwd.createDirPath(self.io, self.directory);
87
+ var name_buffer: [128]u8 = undefined;
88
+ const name = try protocol.screenshotFileName(view_label, &name_buffer);
89
+ var temp_name_buffer: [160]u8 = undefined;
90
+ const temp_name = try std.fmt.bufPrint(&temp_name_buffer, "{s}.tmp", .{name});
91
+ var path_buffer: [256]u8 = undefined;
92
+ var temp_path_buffer: [256]u8 = undefined;
93
+ const final_path = self.path(name, &path_buffer);
94
+ const temp_path = self.path(temp_name, &temp_path_buffer);
95
+ try writePath(self.io, temp_path, png_bytes);
96
+ try std.Io.Dir.cwd().rename(temp_path, std.Io.Dir.cwd(), final_path, self.io);
97
+ }
98
+
99
+ /// Consume the OLDEST pending command in the dropbox queue — one
100
+ /// entry per call, so the runtime's one-command-per-frame cadence is
101
+ /// decided by the caller, not by how many commands landed. Deleting
102
+ /// the entry file IS the consumption ack a writer polls for.
103
+ ///
104
+ /// EVERY complete entry is consumed (deleted), even one that cannot
105
+ /// be dispatched: leaving an oversized or malformed line at the
106
+ /// queue head would strand it forever — strict FIFO means nothing
107
+ /// behind it could ever drain, and the arrival watcher would keep
108
+ /// waking the loop for an entry the drain can never retire. Ack
109
+ /// first, then report the failure as an error so the runtime records
110
+ /// it where snapshots surface it.
111
+ pub fn takeCommand(self: Server, buffer: []u8) !?protocol.Command {
112
+ if (!has_filesystem) return null;
113
+ var name_buffer: [max_queue_name_bytes]u8 = undefined;
114
+ const name = self.queueHeadName(&name_buffer) orelse return null;
115
+ var path_buffer: [256]u8 = undefined;
116
+ const entry_path = self.path(name, &path_buffer);
117
+ const bytes = readPath(self.io, entry_path, buffer) catch return null;
118
+ if (bytes.len == buffer.len) {
119
+ // The file filled the whole buffer, so the line on disk is at
120
+ // least this long — oversized however it ends. Ack (delete)
121
+ // and fail loudly rather than wedge the queue head.
122
+ try deletePathAllowingVanished(self.io, entry_path);
123
+ return error.CommandTooLarge;
124
+ }
125
+ if (bytes.len == 0 or bytes[bytes.len - 1] != '\n') {
126
+ // No trailing newline: the writer claimed the name but its
127
+ // single line-write has not landed yet. Strict FIFO forbids
128
+ // skipping ahead (a later complete entry must not dispatch
129
+ // before this one), so take nothing this turn — the arrival
130
+ // watcher wakes the loop again — and reap the entry only
131
+ // once it has been abandoned long enough that no live writer
132
+ // can still be behind it.
133
+ self.reapAbandonedEntry(entry_path);
134
+ return null;
135
+ }
136
+ try deletePathAllowingVanished(self.io, entry_path);
137
+ const line = std.mem.trim(u8, bytes, " \n\r\t");
138
+ // A whitespace-only entry is a writer bug with nothing to
139
+ // dispatch; it was deleted above so it cannot block the queue.
140
+ if (line.len == 0) return null;
141
+ return try protocol.Command.parse(line);
142
+ }
143
+
144
+ /// True when the dropbox queue holds at least one entry. The same
145
+ /// pending test `takeCommand` starts from, WITHOUT consuming: the
146
+ /// arrival watcher polls this from its own thread and nudges the
147
+ /// platform loop, and the drain on the loop thread stays the only
148
+ /// consumer, so command order and the one-command-per-frame cadence
149
+ /// are untouched. A name scan only — no entry content is read, so an
150
+ /// entry mid-write costs at most a frame or two of wakes with a
151
+ /// nothing-to-take drain, and an abandoned incomplete entry keeps
152
+ /// waking the loop until the drain's reap retires it.
153
+ pub fn hasPendingCommand(self: Server) bool {
154
+ if (!has_filesystem) return false;
155
+ var name_buffer: [max_queue_name_bytes]u8 = undefined;
156
+ return self.queueHeadName(&name_buffer) != null;
157
+ }
158
+
159
+ /// The queue entry name with the LOWEST claim sequence, or null when
160
+ /// the queue is empty. Lowest-first is the whole FIFO story: writers
161
+ /// claim strictly increasing sequences, so the smallest number
162
+ /// present is always the oldest unconsumed command. Every non-queue
163
+ /// file in the dropbox (snapshots, response artifacts, a stale v5
164
+ /// `command.txt`) fails the name parse and is invisible here.
165
+ fn queueHeadName(self: Server, name_buffer: []u8) ?[]const u8 {
166
+ var dir = std.Io.Dir.cwd().openDir(self.io, self.directory, .{ .iterate = true }) catch return null;
167
+ defer dir.close(self.io);
168
+ var head_sequence: ?u64 = null;
169
+ var head_len: usize = 0;
170
+ var iterator = dir.iterate();
171
+ while (iterator.next(self.io) catch null) |entry| {
172
+ if (entry.kind != .file) continue;
173
+ const sequence = protocol.queueFileSequence(entry.name) orelse continue;
174
+ if (head_sequence) |current| {
175
+ if (sequence >= current) continue;
176
+ }
177
+ if (entry.name.len > name_buffer.len) continue;
178
+ head_sequence = sequence;
179
+ head_len = entry.name.len;
180
+ @memcpy(name_buffer[0..entry.name.len], entry.name);
181
+ }
182
+ if (head_sequence == null) return null;
183
+ return name_buffer[0..head_len];
184
+ }
185
+
186
+ /// Delete an incomplete queue-head entry once its writer is provably
187
+ /// gone (mtime older than the reap budget). Best-effort on every
188
+ /// step: a stat or delete failure just leaves the entry for a later
189
+ /// turn, and the app under automation keeps running either way.
190
+ fn reapAbandonedEntry(self: Server, entry_path: []const u8) void {
191
+ const stat = std.Io.Dir.cwd().statFile(self.io, entry_path, .{}) catch return;
192
+ const now_ns: i128 = std.Io.Timestamp.now(self.io, .real).nanoseconds;
193
+ if (now_ns <= 0) return;
194
+ if (now_ns - stat.mtime.nanoseconds < self.abandoned_entry_reap_ns) return;
195
+ std.Io.Dir.cwd().deleteFile(self.io, entry_path) catch {};
196
+ }
197
+
198
+ fn path(self: Server, name: []const u8, buffer: []u8) []const u8 {
199
+ return std.fmt.bufPrint(buffer, "{s}/{s}", .{ self.directory, name }) catch unreachable;
200
+ }
201
+ };
202
+
203
+ fn writePath(io: std.Io, path: []const u8, bytes: []const u8) !void {
204
+ try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = bytes });
205
+ }
206
+
207
+ fn readPath(io: std.Io, path: []const u8, buffer: []u8) ![]const u8 {
208
+ var file = try std.Io.Dir.cwd().openFile(io, path, .{});
209
+ defer file.close(io);
210
+ return buffer[0..try file.readPositionalAll(io, buffer, 0)];
211
+ }
212
+
213
+ /// Delete a queue entry, tolerating one that already vanished (the
214
+ /// dropbox owner cleaned the directory between the head scan and here —
215
+ /// this drain is the only in-protocol deleter, so "already gone" only
216
+ /// ever means an external sweep, never a lost command). Any other delete
217
+ /// failure propagates: an undeletable head would redispatch the same
218
+ /// command every frame, and that must be loud.
219
+ fn deletePathAllowingVanished(io: std.Io, path: []const u8) !void {
220
+ std.Io.Dir.cwd().deleteFile(io, path) catch |err| switch (err) {
221
+ error.FileNotFound => {},
222
+ else => return err,
223
+ };
224
+ }
225
+
226
+ fn resetTestDirectory(io: std.Io, path: []const u8) !void {
227
+ var cwd = std.Io.Dir.cwd();
228
+ cwd.deleteTree(io, path) catch {};
229
+ try cwd.createDirPath(io, path);
230
+ }
231
+
232
+ test "server stores directory metadata" {
233
+ const server = Server.init(std.testing.io, ".zig-cache/test-webview-automation", "Test");
234
+ try std.testing.expectEqualStrings("Test", server.title);
235
+ }
236
+
237
+ test "server writes bridge response artifact" {
238
+ const server = Server.init(std.testing.io, ".zig-cache/test-webview-automation", "Test");
239
+ try server.publishBridgeResponse("{\"id\":\"1\",\"ok\":true}");
240
+
241
+ var buffer: [128]u8 = undefined;
242
+ var path_buffer: [256]u8 = undefined;
243
+ const bytes = try readPath(std.testing.io, server.path("bridge-response.txt", &path_buffer), &buffer);
244
+ try std.testing.expectEqualStrings("{\"id\":\"1\",\"ok\":true}", bytes);
245
+ }
246
+
247
+ test "server publishes large retained widget snapshots" {
248
+ const directory = ".zig-cache/test-webview-automation-large-snapshot";
249
+ try resetTestDirectory(std.testing.io, directory);
250
+ defer std.Io.Dir.cwd().deleteTree(std.testing.io, directory) catch {};
251
+
252
+ const windows = [_]snapshot.Window{.{
253
+ .title = "Large Widget Snapshot",
254
+ .bounds = geometry.RectF.init(0, 0, 1200, 760),
255
+ }};
256
+ var widgets: [80]snapshot.Widget = undefined;
257
+ for (&widgets, 0..) |*widget, index| {
258
+ widget.* = .{
259
+ .view_label = "components-canvas",
260
+ .id = 1000 + index,
261
+ .role = "textbox",
262
+ .name = "Retained component field with a descriptive accessible name",
263
+ .text_value = "native-sdk retained widget snapshot payload",
264
+ .bounds = geometry.RectF.init(@floatFromInt(index), @floatFromInt(index), 180, 28),
265
+ .actions = .{ .focus = true, .set_text = true, .set_selection = true },
266
+ .text_selection = .{ .start = 1, .end = 12 },
267
+ };
268
+ }
269
+
270
+ const server = Server.init(std.testing.io, directory, "Large");
271
+ try server.publish(.{
272
+ .windows = &windows,
273
+ .widgets = &widgets,
274
+ });
275
+
276
+ var path_buffer: [256]u8 = undefined;
277
+ var buffer: [32 * 1024]u8 = undefined;
278
+ const text = try readPath(std.testing.io, server.path("snapshot.txt", &path_buffer), &buffer);
279
+ try std.testing.expect(text.len > 4 * 1024);
280
+ try std.testing.expect(std.mem.indexOf(u8, text, "widget @w1/components-canvas#1079") != null);
281
+
282
+ const a11y = try readPath(std.testing.io, server.path("accessibility.txt", &path_buffer), &buffer);
283
+ try std.testing.expect(a11y.len > 4 * 1024);
284
+ try std.testing.expect(std.mem.indexOf(u8, a11y, "@w1/components-canvas#1079 role=textbox") != null);
285
+ }
286
+
287
+ test "server writes screenshot artifacts atomically" {
288
+ const directory = ".zig-cache/test-webview-automation-screenshot";
289
+ try resetTestDirectory(std.testing.io, directory);
290
+ defer std.Io.Dir.cwd().deleteTree(std.testing.io, directory) catch {};
291
+
292
+ const server = Server.init(std.testing.io, directory, "Test");
293
+ const png_bytes = "\x89PNG\r\n\x1a\nfake-png-payload";
294
+ try server.publishScreenshot("inbox-canvas", png_bytes);
295
+
296
+ var buffer: [64]u8 = undefined;
297
+ var path_buffer: [256]u8 = undefined;
298
+ const bytes = try readPath(std.testing.io, server.path("screenshot-inbox-canvas.png", &path_buffer), &buffer);
299
+ try std.testing.expectEqualStrings(png_bytes, bytes);
300
+
301
+ // No temporary file is left behind.
302
+ var temp_buffer: [64]u8 = undefined;
303
+ try std.testing.expectError(
304
+ error.FileNotFound,
305
+ readPath(std.testing.io, server.path("screenshot-inbox-canvas.png.tmp", &path_buffer), &temp_buffer),
306
+ );
307
+
308
+ // Republish overwrites the previous artifact.
309
+ try server.publishScreenshot("inbox-canvas", "\x89PNG\r\n\x1a\nsecond");
310
+ const second = try readPath(std.testing.io, server.path("screenshot-inbox-canvas.png", &path_buffer), &buffer);
311
+ try std.testing.expectEqualStrings("\x89PNG\r\n\x1a\nsecond", second);
312
+ }
313
+
314
+ /// Write one queue entry (`command-<sequence>.txt`) the way the CLI
315
+ /// does: whole line, trailing newline, single write.
316
+ fn writeQueueEntry(io: std.Io, server: Server, sequence: u64, line: []const u8) !void {
317
+ var name_buffer: [max_queue_name_bytes]u8 = undefined;
318
+ const name = try protocol.queueFileName(sequence, &name_buffer);
319
+ var path_buffer: [256]u8 = undefined;
320
+ try writePath(io, server.path(name, &path_buffer), line);
321
+ }
322
+
323
+ fn queueEntryExists(io: std.Io, server: Server, sequence: u64) bool {
324
+ var name_buffer: [max_queue_name_bytes]u8 = undefined;
325
+ const name = protocol.queueFileName(sequence, &name_buffer) catch return false;
326
+ var path_buffer: [256]u8 = undefined;
327
+ var buffer: [protocol.max_command_bytes]u8 = undefined;
328
+ _ = readPath(io, server.path(name, &path_buffer), &buffer) catch return false;
329
+ return true;
330
+ }
331
+
332
+ test "server dispatches rapid-fire commands in arrival order, one per turn" {
333
+ const directory = ".zig-cache/test-webview-automation-rapid-fire";
334
+ try resetTestDirectory(std.testing.io, directory);
335
+ defer std.Io.Dir.cwd().deleteTree(std.testing.io, directory) catch {};
336
+
337
+ const server = Server.init(std.testing.io, directory, "Test");
338
+ var command_buffer: [256]u8 = undefined;
339
+
340
+ // The field reproduction: three commands land back-to-back BEFORE the
341
+ // app drains any of them. The single-entry slot lost one of these to
342
+ // an overwrite; the queue must dispatch all three, oldest first, one
343
+ // per consumption turn.
344
+ try writeQueueEntry(std.testing.io, server, 1, "menu-command app.first\n");
345
+ try writeQueueEntry(std.testing.io, server, 2, "menu-command app.second\n");
346
+ try writeQueueEntry(std.testing.io, server, 3, "menu-command app.third\n");
347
+
348
+ const first = (try server.takeCommand(&command_buffer)).?;
349
+ try std.testing.expectEqual(protocol.Action.menu_command, first.action);
350
+ try std.testing.expectEqualStrings("app.first", first.value);
351
+ // One per turn: consuming the head deletes ITS entry (the ack a
352
+ // writer polls for) and leaves the younger two untouched.
353
+ try std.testing.expect(!queueEntryExists(std.testing.io, server, 1));
354
+ try std.testing.expect(queueEntryExists(std.testing.io, server, 2));
355
+ try std.testing.expect(queueEntryExists(std.testing.io, server, 3));
356
+
357
+ const second = (try server.takeCommand(&command_buffer)).?;
358
+ try std.testing.expectEqualStrings("app.second", second.value);
359
+ const third = (try server.takeCommand(&command_buffer)).?;
360
+ try std.testing.expectEqualStrings("app.third", third.value);
361
+ try std.testing.expect(try server.takeCommand(&command_buffer) == null);
362
+ try std.testing.expect(!server.hasPendingCommand());
363
+ }
364
+
365
+ test "server consumes queued automation commands" {
366
+ const directory = ".zig-cache/test-webview-automation-command";
367
+ try resetTestDirectory(std.testing.io, directory);
368
+ defer std.Io.Dir.cwd().deleteTree(std.testing.io, directory) catch {};
369
+
370
+ const server = Server.init(std.testing.io, directory, "Test");
371
+ var command_buffer: [256]u8 = undefined;
372
+
373
+ // Sequence numbers restart whenever the queue drains empty (writers
374
+ // pick max-present + 1), so ordering only ever compares COEXISTING
375
+ // entries — queue the verbs with arbitrary gaps to pin that shape.
376
+ try writeQueueEntry(std.testing.io, server, 1, "native-command app.refresh refresh-button\n");
377
+ const native_command = (try server.takeCommand(&command_buffer)).?;
378
+ try std.testing.expectEqual(protocol.Action.native_command, native_command.action);
379
+ try std.testing.expectEqualStrings("app.refresh refresh-button", native_command.value);
380
+ try std.testing.expect(try server.takeCommand(&command_buffer) == null);
381
+
382
+ try writeQueueEntry(std.testing.io, server, 1, "focus-next\n");
383
+ const focus_next = (try server.takeCommand(&command_buffer)).?;
384
+ try std.testing.expectEqual(protocol.Action.focus_next_view, focus_next.action);
385
+ try std.testing.expectEqualStrings("", focus_next.value);
386
+
387
+ try writeQueueEntry(std.testing.io, server, 7, "widget-action canvas 2 press\n");
388
+ const widget_action = (try server.takeCommand(&command_buffer)).?;
389
+ try std.testing.expectEqual(protocol.Action.widget_action, widget_action.action);
390
+ try std.testing.expectEqualStrings("canvas 2 press", widget_action.value);
391
+
392
+ try writeQueueEntry(std.testing.io, server, 2, "widget-click canvas 2\n");
393
+ const widget_click = (try server.takeCommand(&command_buffer)).?;
394
+ try std.testing.expectEqual(protocol.Action.widget_click, widget_click.action);
395
+ try std.testing.expectEqualStrings("canvas 2", widget_click.value);
396
+
397
+ try writeQueueEntry(std.testing.io, server, 3, "widget-drag canvas 2 0.25 0.82\n");
398
+ const widget_drag = (try server.takeCommand(&command_buffer)).?;
399
+ try std.testing.expectEqual(protocol.Action.widget_drag, widget_drag.action);
400
+ try std.testing.expectEqualStrings("canvas 2 0.25 0.82", widget_drag.value);
401
+
402
+ try writeQueueEntry(std.testing.io, server, 4, "widget-key canvas tab\n");
403
+ const widget_key = (try server.takeCommand(&command_buffer)).?;
404
+ try std.testing.expectEqual(protocol.Action.widget_key, widget_key.action);
405
+ try std.testing.expectEqualStrings("canvas tab", widget_key.value);
406
+
407
+ try writeQueueEntry(std.testing.io, server, 5, "tray-action 11\n");
408
+ const tray_action = (try server.takeCommand(&command_buffer)).?;
409
+ try std.testing.expectEqual(protocol.Action.tray_action, tray_action.action);
410
+ try std.testing.expectEqualStrings("11", tray_action.value);
411
+ }
412
+
413
+ test "server acks undispatchable command entries instead of stranding the queue" {
414
+ const directory = ".zig-cache/test-webview-automation-bad-command";
415
+ try resetTestDirectory(std.testing.io, directory);
416
+ defer std.Io.Dir.cwd().deleteTree(std.testing.io, directory) catch {};
417
+
418
+ const server = Server.init(std.testing.io, directory, "Test");
419
+ var command_buffer: [256]u8 = undefined;
420
+
421
+ // A malformed line is consumed (its entry deleted) with a loud
422
+ // error, never left at the queue head to block everything behind it
423
+ // — the command queued AFTER the bad one dispatches on the very next
424
+ // turn.
425
+ try writeQueueEntry(std.testing.io, server, 1, "no-such-verb whatever\n");
426
+ try writeQueueEntry(std.testing.io, server, 2, "menu-command app.after-bad\n");
427
+ try std.testing.expectError(error.InvalidCommand, server.takeCommand(&command_buffer));
428
+ try std.testing.expect(!queueEntryExists(std.testing.io, server, 1));
429
+ const after_bad = (try server.takeCommand(&command_buffer)).?;
430
+ try std.testing.expectEqualStrings("app.after-bad", after_bad.value);
431
+
432
+ // Same for a line larger than the caller's buffer.
433
+ var oversized_buffer: [64]u8 = undefined;
434
+ const oversized = "widget-key canvas " ++ "x" ** 256 ++ "\n";
435
+ try writeQueueEntry(std.testing.io, server, 3, oversized);
436
+ try std.testing.expectError(error.CommandTooLarge, server.takeCommand(&oversized_buffer));
437
+ try std.testing.expect(!queueEntryExists(std.testing.io, server, 3));
438
+ try std.testing.expect(try server.takeCommand(&command_buffer) == null);
439
+ }
440
+
441
+ test "server reports pending commands without consuming them" {
442
+ const directory = ".zig-cache/test-webview-automation-pending";
443
+ try resetTestDirectory(std.testing.io, directory);
444
+ defer std.Io.Dir.cwd().deleteTree(std.testing.io, directory) catch {};
445
+
446
+ const server = Server.init(std.testing.io, directory, "Test");
447
+ var path_buffer: [256]u8 = undefined;
448
+
449
+ // An empty dropbox is idle, and so is one holding only non-queue
450
+ // files — response artifacts and, pointedly, a retired-v5 writer's
451
+ // `command.txt` slot, which this protocol version must never consume
452
+ // (acking it would tell that stale CLI its command dispatched).
453
+ try std.testing.expect(!server.hasPendingCommand());
454
+ try writePath(std.testing.io, server.path("command.txt", &path_buffer), "widget-click canvas 7\n");
455
+ try writePath(std.testing.io, server.path("bridge-response.txt", &path_buffer), "{}");
456
+ try std.testing.expect(!server.hasPendingCommand());
457
+ var command_buffer: [256]u8 = undefined;
458
+ try std.testing.expect(try server.takeCommand(&command_buffer) == null);
459
+
460
+ // A queued entry is pending — and STAYS pending across probes; only
461
+ // takeCommand (the loop-thread drain) consumes it.
462
+ try writeQueueEntry(std.testing.io, server, 1, "widget-click canvas 7\n");
463
+ try std.testing.expect(server.hasPendingCommand());
464
+ try std.testing.expect(server.hasPendingCommand());
465
+ const command = (try server.takeCommand(&command_buffer)).?;
466
+ try std.testing.expectEqual(protocol.Action.widget_click, command.action);
467
+ try std.testing.expect(!server.hasPendingCommand());
468
+ }
469
+
470
+ test "server defers an incomplete queue head and reaps it once abandoned" {
471
+ const directory = ".zig-cache/test-webview-automation-incomplete";
472
+ try resetTestDirectory(std.testing.io, directory);
473
+ defer std.Io.Dir.cwd().deleteTree(std.testing.io, directory) catch {};
474
+
475
+ var server = Server.init(std.testing.io, directory, "Test");
476
+ var command_buffer: [256]u8 = undefined;
477
+
478
+ // A head entry without its trailing newline is a writer mid-claim:
479
+ // strict FIFO means the complete entry behind it must NOT jump the
480
+ // queue, and a fresh incomplete entry must not be reaped.
481
+ try writeQueueEntry(std.testing.io, server, 1, "menu-command app.first");
482
+ try writeQueueEntry(std.testing.io, server, 2, "menu-command app.second\n");
483
+ try std.testing.expect(try server.takeCommand(&command_buffer) == null);
484
+ try std.testing.expect(queueEntryExists(std.testing.io, server, 1));
485
+ // The name scan still reports pending, so the arrival watcher keeps
486
+ // waking the loop until the head resolves.
487
+ try std.testing.expect(server.hasPendingCommand());
488
+
489
+ // The writer finishes its line: both dispatch, oldest first.
490
+ try writeQueueEntry(std.testing.io, server, 1, "menu-command app.first\n");
491
+ const first = (try server.takeCommand(&command_buffer)).?;
492
+ try std.testing.expectEqualStrings("app.first", first.value);
493
+ const second = (try server.takeCommand(&command_buffer)).?;
494
+ try std.testing.expectEqualStrings("app.second", second.value);
495
+
496
+ // A DEAD writer's incomplete claim (older than the reap budget —
497
+ // forced to zero here so the test never sleeps) is deleted so the
498
+ // queue heals instead of wedging forever.
499
+ server.abandoned_entry_reap_ns = 0;
500
+ try writeQueueEntry(std.testing.io, server, 4, "menu-command app.orpha");
501
+ try writeQueueEntry(std.testing.io, server, 5, "menu-command app.alive\n");
502
+ try std.testing.expect(try server.takeCommand(&command_buffer) == null);
503
+ try std.testing.expect(!queueEntryExists(std.testing.io, server, 4));
504
+ const alive = (try server.takeCommand(&command_buffer)).?;
505
+ try std.testing.expectEqualStrings("app.alive", alive.value);
506
+ }