@native-sdk/cli 0.0.0 → 0.4.1

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 (373) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/assets/icon.icns +0 -0
  5. package/assets/icon.ico +0 -0
  6. package/assets/icon.png +0 -0
  7. package/assets/icon.svg +15 -0
  8. package/assets/native-sdk.entitlements +10 -0
  9. package/assets/native-sdk.manifest +12 -0
  10. package/bin/native.js +156 -0
  11. package/build/app.zig +716 -0
  12. package/build.zig +2593 -0
  13. package/build.zig.zon +24 -0
  14. package/native-sdk.d.ts +615 -0
  15. package/package.json +45 -8
  16. package/skill-data/automation/SKILL.md +248 -0
  17. package/skill-data/core/SKILL.md +249 -0
  18. package/skill-data/core/references/app-model-runtime.md +141 -0
  19. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  20. package/skill-data/core/references/frontend-assets.md +124 -0
  21. package/skill-data/core/references/project-anatomy.md +108 -0
  22. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  23. package/skill-data/native-ui/SKILL.md +1080 -0
  24. package/skills/native-sdk/SKILL.md +33 -0
  25. package/src/app_runner/root.zig +740 -0
  26. package/src/assets/root.zig +20 -0
  27. package/src/automation/protocol.zig +285 -0
  28. package/src/automation/root.zig +13 -0
  29. package/src/automation/server.zig +506 -0
  30. package/src/automation/snapshot.zig +1202 -0
  31. package/src/automation/watcher.zig +165 -0
  32. package/src/bridge/root.zig +507 -0
  33. package/src/debug/root.zig +246 -0
  34. package/src/embed/app_exports.zig +11 -0
  35. package/src/embed/c_api.zig +793 -0
  36. package/src/embed/c_exports.zig +10 -0
  37. package/src/embed/chrome.zig +168 -0
  38. package/src/embed/conversions.zig +359 -0
  39. package/src/embed/host.zig +960 -0
  40. package/src/embed/root.zig +81 -0
  41. package/src/embed/tests.zig +2552 -0
  42. package/src/embed/types.zig +382 -0
  43. package/src/embed/ui_host.zig +289 -0
  44. package/src/extensions/root.zig +197 -0
  45. package/src/frontend/root.zig +48 -0
  46. package/src/js/root.zig +65 -0
  47. package/src/platform/android/NativeSdkActivity.java +1370 -0
  48. package/src/platform/android/android_host.c +696 -0
  49. package/src/platform/android/files.zig +9 -0
  50. package/src/platform/android/native_sdk_app.h +237 -0
  51. package/src/platform/ios/files.zig +8 -0
  52. package/src/platform/ios/native_sdk_app.h +289 -0
  53. package/src/platform/ios/uikit_host.m +2739 -0
  54. package/src/platform/linux/cef_host.cpp +599 -0
  55. package/src/platform/linux/gtk_host.c +5362 -0
  56. package/src/platform/linux/gtk_host.h +265 -0
  57. package/src/platform/linux/root.zig +1519 -0
  58. package/src/platform/macos/appkit_host.h +604 -0
  59. package/src/platform/macos/appkit_host.m +10392 -0
  60. package/src/platform/macos/automation_host.h +14 -0
  61. package/src/platform/macos/automation_host.m +12 -0
  62. package/src/platform/macos/cef_host.mm +3028 -0
  63. package/src/platform/macos/root.zig +2260 -0
  64. package/src/platform/null_platform.zig +2232 -0
  65. package/src/platform/null_platform_tests.zig +670 -0
  66. package/src/platform/policy_values.zig +14 -0
  67. package/src/platform/root.zig +181 -0
  68. package/src/platform/types.zig +2681 -0
  69. package/src/platform/windows/cef_host.cpp +3 -0
  70. package/src/platform/windows/root.zig +1521 -0
  71. package/src/platform/windows/webview2_host.cpp +6230 -0
  72. package/src/primitives/app_dirs/root.zig +440 -0
  73. package/src/primitives/app_manifest/root.zig +109 -0
  74. package/src/primitives/app_manifest/tests.zig +758 -0
  75. package/src/primitives/app_manifest/types.zig +587 -0
  76. package/src/primitives/app_manifest/validation.zig +828 -0
  77. package/src/primitives/assets/root.zig +520 -0
  78. package/src/primitives/canvas/a11y_audit.zig +390 -0
  79. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  80. package/src/primitives/canvas/app_icon.zig +1590 -0
  81. package/src/primitives/canvas/chart.zig +392 -0
  82. package/src/primitives/canvas/chart_tests.zig +751 -0
  83. package/src/primitives/canvas/commands.zig +446 -0
  84. package/src/primitives/canvas/drawing.zig +309 -0
  85. package/src/primitives/canvas/equality.zig +316 -0
  86. package/src/primitives/canvas/events.zig +696 -0
  87. package/src/primitives/canvas/font_coverage.zig +118 -0
  88. package/src/primitives/canvas/font_ttf.zig +510 -0
  89. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  90. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  91. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  92. package/src/primitives/canvas/frame.zig +679 -0
  93. package/src/primitives/canvas/frame_metrics.zig +462 -0
  94. package/src/primitives/canvas/gpu.zig +639 -0
  95. package/src/primitives/canvas/hash.zig +112 -0
  96. package/src/primitives/canvas/icons/alert.svg +1 -0
  97. package/src/primitives/canvas/icons/archive.svg +1 -0
  98. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  99. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  100. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  101. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  102. package/src/primitives/canvas/icons/check.svg +1 -0
  103. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  104. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  105. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  106. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  107. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  108. package/src/primitives/canvas/icons/clock.svg +1 -0
  109. package/src/primitives/canvas/icons/copy.svg +1 -0
  110. package/src/primitives/canvas/icons/download.svg +1 -0
  111. package/src/primitives/canvas/icons/edit.svg +1 -0
  112. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  113. package/src/primitives/canvas/icons/external-link.svg +1 -0
  114. package/src/primitives/canvas/icons/eye.svg +1 -0
  115. package/src/primitives/canvas/icons/file-text.svg +1 -0
  116. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  117. package/src/primitives/canvas/icons/folder.svg +1 -0
  118. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  119. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  120. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  121. package/src/primitives/canvas/icons/info.svg +1 -0
  122. package/src/primitives/canvas/icons/menu.svg +1 -0
  123. package/src/primitives/canvas/icons/missing.svg +1 -0
  124. package/src/primitives/canvas/icons/moon.svg +1 -0
  125. package/src/primitives/canvas/icons/music.svg +1 -0
  126. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  127. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  128. package/src/primitives/canvas/icons/pause.svg +1 -0
  129. package/src/primitives/canvas/icons/play.svg +1 -0
  130. package/src/primitives/canvas/icons/plus.svg +1 -0
  131. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  132. package/src/primitives/canvas/icons/repeat.svg +1 -0
  133. package/src/primitives/canvas/icons/save.svg +1 -0
  134. package/src/primitives/canvas/icons/search.svg +1 -0
  135. package/src/primitives/canvas/icons/send.svg +1 -0
  136. package/src/primitives/canvas/icons/settings.svg +1 -0
  137. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  138. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  139. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  140. package/src/primitives/canvas/icons/sun.svg +1 -0
  141. package/src/primitives/canvas/icons/terminal.svg +1 -0
  142. package/src/primitives/canvas/icons/trash.svg +1 -0
  143. package/src/primitives/canvas/icons/volume.svg +1 -0
  144. package/src/primitives/canvas/icons/wrench.svg +1 -0
  145. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  146. package/src/primitives/canvas/icons/x.svg +1 -0
  147. package/src/primitives/canvas/icons.zig +249 -0
  148. package/src/primitives/canvas/layout_audit.zig +744 -0
  149. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  150. package/src/primitives/canvas/markdown.zig +1031 -0
  151. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  152. package/src/primitives/canvas/markdown_tests.zig +538 -0
  153. package/src/primitives/canvas/plan_key_index.zig +120 -0
  154. package/src/primitives/canvas/png.zig +355 -0
  155. package/src/primitives/canvas/reference.zig +1457 -0
  156. package/src/primitives/canvas/reference_blur.zig +104 -0
  157. package/src/primitives/canvas/reference_memo.zig +322 -0
  158. package/src/primitives/canvas/reference_paths.zig +184 -0
  159. package/src/primitives/canvas/reference_tests.zig +1650 -0
  160. package/src/primitives/canvas/render.zig +747 -0
  161. package/src/primitives/canvas/render_effects.zig +255 -0
  162. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  163. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  164. package/src/primitives/canvas/render_images.zig +247 -0
  165. package/src/primitives/canvas/render_layers.zig +260 -0
  166. package/src/primitives/canvas/render_paths.zig +347 -0
  167. package/src/primitives/canvas/render_resources.zig +51 -0
  168. package/src/primitives/canvas/render_tests.zig +3186 -0
  169. package/src/primitives/canvas/root.zig +744 -0
  170. package/src/primitives/canvas/serialization.zig +1528 -0
  171. package/src/primitives/canvas/svg_icon.zig +636 -0
  172. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  173. package/src/primitives/canvas/test_support.zig +422 -0
  174. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  175. package/src/primitives/canvas/tests.zig +30 -0
  176. package/src/primitives/canvas/text.zig +82 -0
  177. package/src/primitives/canvas/text_atlas.zig +408 -0
  178. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  179. package/src/primitives/canvas/text_interaction.zig +615 -0
  180. package/src/primitives/canvas/text_layout.zig +1069 -0
  181. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  182. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  183. package/src/primitives/canvas/text_layout_types.zig +128 -0
  184. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  185. package/src/primitives/canvas/text_metrics.zig +309 -0
  186. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  187. package/src/primitives/canvas/text_span_tests.zig +608 -0
  188. package/src/primitives/canvas/text_spans.zig +1010 -0
  189. package/src/primitives/canvas/text_tests.zig +1949 -0
  190. package/src/primitives/canvas/themes/geist.zig +470 -0
  191. package/src/primitives/canvas/tokens.zig +1522 -0
  192. package/src/primitives/canvas/ui.zig +2686 -0
  193. package/src/primitives/canvas/ui_markup.zig +3791 -0
  194. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  195. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  196. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  197. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  198. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  199. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  200. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  201. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  202. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  203. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  204. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  205. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  206. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  207. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  208. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  209. package/src/primitives/canvas/ui_provenance.zig +96 -0
  210. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  211. package/src/primitives/canvas/ui_schema.zig +825 -0
  212. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  213. package/src/primitives/canvas/ui_tests.zig +1432 -0
  214. package/src/primitives/canvas/vector.zig +919 -0
  215. package/src/primitives/canvas/vector_tests.zig +547 -0
  216. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  217. package/src/primitives/canvas/virtual_extents.zig +540 -0
  218. package/src/primitives/canvas/widget_access.zig +208 -0
  219. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  220. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  221. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  222. package/src/primitives/canvas/widget_layout.zig +2055 -0
  223. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  224. package/src/primitives/canvas/widget_metrics.zig +228 -0
  225. package/src/primitives/canvas/widget_render.zig +2534 -0
  226. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  227. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  228. package/src/primitives/canvas/widget_render_style.zig +626 -0
  229. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  230. package/src/primitives/canvas/widget_routing.zig +475 -0
  231. package/src/primitives/canvas/widget_runtime.zig +307 -0
  232. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  233. package/src/primitives/canvas/widget_semantics.zig +480 -0
  234. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  235. package/src/primitives/canvas/widget_tests.zig +8 -0
  236. package/src/primitives/canvas/widget_text_input.zig +351 -0
  237. package/src/primitives/canvas/widget_text_select.zig +147 -0
  238. package/src/primitives/canvas/widget_tree.zig +232 -0
  239. package/src/primitives/canvas/widgets.zig +1319 -0
  240. package/src/primitives/diagnostics/root.zig +445 -0
  241. package/src/primitives/geometry/root.zig +889 -0
  242. package/src/primitives/json/root.zig +272 -0
  243. package/src/primitives/platform_info/root.zig +450 -0
  244. package/src/primitives/trace/root.zig +569 -0
  245. package/src/root.zig +165 -0
  246. package/src/runtime/api.zig +431 -0
  247. package/src/runtime/async_bridge.zig +49 -0
  248. package/src/runtime/automation_commands.zig +686 -0
  249. package/src/runtime/automation_liveness_tests.zig +140 -0
  250. package/src/runtime/automation_snapshot.zig +246 -0
  251. package/src/runtime/automation_widget_dispatch.zig +617 -0
  252. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  253. package/src/runtime/bridge_payload.zig +194 -0
  254. package/src/runtime/bridge_permission_tests.zig +551 -0
  255. package/src/runtime/bridge_responses.zig +414 -0
  256. package/src/runtime/builtin_bridge.zig +554 -0
  257. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  258. package/src/runtime/canvas_font_tests.zig +410 -0
  259. package/src/runtime/canvas_fonts.zig +229 -0
  260. package/src/runtime/canvas_frame.zig +1734 -0
  261. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  262. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  263. package/src/runtime/canvas_frame_helpers.zig +414 -0
  264. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  265. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  266. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  267. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  268. package/src/runtime/canvas_frame_tests.zig +9 -0
  269. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  270. package/src/runtime/canvas_image_tests.zig +362 -0
  271. package/src/runtime/canvas_images.zig +224 -0
  272. package/src/runtime/canvas_limits.zig +317 -0
  273. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  274. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  275. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  276. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  277. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  278. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  279. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  280. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  281. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  282. package/src/runtime/canvas_widget_display.zig +677 -0
  283. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  284. package/src/runtime/canvas_widget_events.zig +1098 -0
  285. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  286. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  287. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  288. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  289. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  290. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  291. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  292. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  293. package/src/runtime/canvas_widget_state.zig +798 -0
  294. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  295. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  296. package/src/runtime/clock.zig +231 -0
  297. package/src/runtime/core.zig +927 -0
  298. package/src/runtime/effects.zig +4197 -0
  299. package/src/runtime/effects_audio_tests.zig +690 -0
  300. package/src/runtime/effects_clipboard_tests.zig +317 -0
  301. package/src/runtime/effects_fetch_tests.zig +1031 -0
  302. package/src/runtime/effects_file_tests.zig +407 -0
  303. package/src/runtime/effects_tests.zig +1251 -0
  304. package/src/runtime/flow.zig +1175 -0
  305. package/src/runtime/frame_profile.zig +221 -0
  306. package/src/runtime/gpu_surface_events.zig +639 -0
  307. package/src/runtime/launch_timing.zig +59 -0
  308. package/src/runtime/markdown_app_tests.zig +224 -0
  309. package/src/runtime/native_command_event_tests.zig +431 -0
  310. package/src/runtime/platform_bridge_tests.zig +8 -0
  311. package/src/runtime/platform_config_tests.zig +392 -0
  312. package/src/runtime/root.zig +150 -0
  313. package/src/runtime/runtime_event_tests.zig +1079 -0
  314. package/src/runtime/session_journal.zig +1522 -0
  315. package/src/runtime/session_record.zig +332 -0
  316. package/src/runtime/session_replay.zig +274 -0
  317. package/src/runtime/session_state.zig +30 -0
  318. package/src/runtime/session_tests.zig +399 -0
  319. package/src/runtime/shell_layout.zig +472 -0
  320. package/src/runtime/shell_layout_tests.zig +1246 -0
  321. package/src/runtime/shell_view_tests.zig +604 -0
  322. package/src/runtime/state.zig +110 -0
  323. package/src/runtime/system_services.zig +392 -0
  324. package/src/runtime/test_support.zig +79 -0
  325. package/src/runtime/tests.zig +30 -0
  326. package/src/runtime/ui_app.zig +3290 -0
  327. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  328. package/src/runtime/ui_app_provenance.zig +370 -0
  329. package/src/runtime/ui_app_tests.zig +3254 -0
  330. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  331. package/src/runtime/ui_app_window_tests.zig +442 -0
  332. package/src/runtime/validation.zig +206 -0
  333. package/src/runtime/view.zig +908 -0
  334. package/src/runtime/view_canvas.zig +850 -0
  335. package/src/runtime/view_widget_control.zig +433 -0
  336. package/src/runtime/view_widget_scroll.zig +360 -0
  337. package/src/runtime/view_widget_text.zig +275 -0
  338. package/src/runtime/view_widget_tree.zig +843 -0
  339. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  340. package/src/runtime/widget_bridge.zig +231 -0
  341. package/src/runtime/window_command_bridge_tests.zig +403 -0
  342. package/src/runtime/window_storage.zig +255 -0
  343. package/src/runtime/window_view_runtime.zig +727 -0
  344. package/src/runtime/window_views.zig +334 -0
  345. package/src/security/root.zig +118 -0
  346. package/src/tooling/android.zig +1172 -0
  347. package/src/tooling/assets.zig +128 -0
  348. package/src/tooling/buildgraph.zig +379 -0
  349. package/src/tooling/cef.zig +671 -0
  350. package/src/tooling/codesign.zig +172 -0
  351. package/src/tooling/components/identity_tests.zig +185 -0
  352. package/src/tooling/components/stepper.zig +113 -0
  353. package/src/tooling/components/timeline.native +27 -0
  354. package/src/tooling/components/timeline_item.zig +131 -0
  355. package/src/tooling/default_icon.icns +0 -0
  356. package/src/tooling/default_icon.png +0 -0
  357. package/src/tooling/dev.zig +172 -0
  358. package/src/tooling/doctor.zig +349 -0
  359. package/src/tooling/eject_components.zig +170 -0
  360. package/src/tooling/embedlib.zig +290 -0
  361. package/src/tooling/ios.zig +528 -0
  362. package/src/tooling/manifest.zig +1939 -0
  363. package/src/tooling/package.zig +2388 -0
  364. package/src/tooling/process_tree.zig +114 -0
  365. package/src/tooling/raw_manifest.zig +193 -0
  366. package/src/tooling/root.zig +22 -0
  367. package/src/tooling/templates.zig +3191 -0
  368. package/src/tooling/toolchain.zig +371 -0
  369. package/src/tooling/verbs.zig +204 -0
  370. package/src/tooling/web_engine.zig +135 -0
  371. package/src/tooling/xcodeproj.zig +462 -0
  372. package/src/window_state/root.zig +507 -0
  373. package/index.js +0 -2
@@ -0,0 +1,1051 @@
1
+ const support = @import("test_support.zig");
2
+ const std = support.std;
3
+ const geometry = support.geometry;
4
+ const canvas = support.canvas;
5
+ const automation = support.automation;
6
+ const platform = support.platform;
7
+ const App = support.App;
8
+ const TestHarness = support.TestHarness;
9
+ const max_canvas_commands_per_view = support.max_canvas_commands_per_view;
10
+ const canvasFrameScratchStorage = support.canvasFrameScratchStorage;
11
+ const CanvasPresentationMode = support.CanvasPresentationMode;
12
+ const CanvasPresentationResult = support.CanvasPresentationResult;
13
+
14
+ // ---------------------------------------------------------------------------
15
+ // Reference retained-store host: a Zig twin of the AppKit host's command
16
+ // dictionary (appkit_host.m, presentGpuPacketObject) that applies the v2
17
+ // binary wire byte-for-byte. It retains each command's ENCODED bytes
18
+ // keyed by retain key plus the draw-order vector, so the golden test can
19
+ // compare "state after a sequence of patches" against "state after a full
20
+ // re-present" at the wire-byte level — stronger than any field-wise
21
+ // comparison, and immune to a decoder papering over drift.
22
+
23
+ const TestRetainedHost = struct {
24
+ allocator: std.mem.Allocator,
25
+ commands: std.AutoHashMap(u64, []u8),
26
+ order: std.ArrayList(u64),
27
+ generation: u64 = 0,
28
+ valid: bool = false,
29
+
30
+ fn init(allocator: std.mem.Allocator) TestRetainedHost {
31
+ return .{
32
+ .allocator = allocator,
33
+ .commands = std.AutoHashMap(u64, []u8).init(allocator),
34
+ .order = .empty,
35
+ };
36
+ }
37
+
38
+ fn deinit(self: *TestRetainedHost) void {
39
+ var it = self.commands.valueIterator();
40
+ while (it.next()) |bytes| self.allocator.free(bytes.*);
41
+ self.commands.deinit();
42
+ self.order.deinit(self.allocator);
43
+ }
44
+
45
+ fn clearCommands(self: *TestRetainedHost) void {
46
+ var it = self.commands.valueIterator();
47
+ while (it.next()) |bytes| self.allocator.free(bytes.*);
48
+ self.commands.clearRetainingCapacity();
49
+ self.order.clearRetainingCapacity();
50
+ }
51
+
52
+ fn putCommand(self: *TestRetainedHost, key: u64, bytes: []const u8) !void {
53
+ const copy = try self.allocator.dupe(u8, bytes);
54
+ if (try self.commands.fetchPut(key, copy)) |previous| self.allocator.free(previous.value);
55
+ }
56
+
57
+ /// Apply one wire payload the way the AppKit host does: keyed `clear`
58
+ /// under a nonzero generation rebuilds the store, `patch` edits it
59
+ /// (refusals drop it), anything else invalidates it.
60
+ fn apply(self: *TestRetainedHost, payload: []const u8) !void {
61
+ var cursor = BinaryCursor{ .bytes = payload };
62
+ try cursor.expectBytes("NSGP");
63
+ try std.testing.expectEqual(@as(u8, canvas.binary_packet_version), try cursor.readU8());
64
+ const load_action = try cursor.readU8();
65
+ const flags = try cursor.readU8();
66
+ _ = try cursor.readU8(); // reserved
67
+ const generation = try cursor.readU64();
68
+ if (flags & 0x01 != 0) try cursor.skip(16); // scissor
69
+ if (flags & 0x02 != 0) { // v3 dirty rect list
70
+ const dirty_rect_count = try cursor.readU32();
71
+ try std.testing.expect(dirty_rect_count >= 1);
72
+ try std.testing.expect(dirty_rect_count <= canvas.max_canvas_frame_dirty_rects);
73
+ try cursor.skip(@as(usize, dirty_rect_count) * 16);
74
+ }
75
+ // images + image actions
76
+ const image_count = try cursor.readU32();
77
+ try cursor.skip(@as(usize, image_count) * 24);
78
+ const action_count = try cursor.readU32();
79
+ try cursor.skip(@as(usize, action_count) * 21);
80
+
81
+ switch (load_action) {
82
+ 2 => { // clear: keyed full present rebuilds the store
83
+ const command_count = try cursor.readU32();
84
+ self.clearCommands();
85
+ for (0..command_count) |_| {
86
+ const key = try cursor.readU64();
87
+ const command_bytes = try cursor.command();
88
+ try self.putCommand(key, command_bytes);
89
+ try self.order.append(self.allocator, key);
90
+ }
91
+ self.generation = generation;
92
+ self.valid = generation != 0;
93
+ if (!self.valid) self.clearCommands();
94
+ },
95
+ 3 => { // patch: edit script against the retained store
96
+ try std.testing.expect(self.valid);
97
+ try std.testing.expectEqual(self.generation, generation);
98
+ const evict_count = try cursor.readU32();
99
+ for (0..evict_count) |_| {
100
+ const key = try cursor.readU64();
101
+ const removed = self.commands.fetchRemove(key) orelse return error.PatchEvictMissing;
102
+ self.allocator.free(removed.value);
103
+ }
104
+ const upsert_count = try cursor.readU32();
105
+ for (0..upsert_count) |_| {
106
+ const key = try cursor.readU64();
107
+ const command_bytes = try cursor.command();
108
+ try self.putCommand(key, command_bytes);
109
+ }
110
+ const order_count = try cursor.readU32();
111
+ try std.testing.expectEqual(self.commands.count(), order_count);
112
+ self.order.clearRetainingCapacity();
113
+ for (0..order_count) |_| {
114
+ const key = try cursor.readU64();
115
+ if (!self.commands.contains(key)) return error.PatchOrderMissing;
116
+ try self.order.append(self.allocator, key);
117
+ }
118
+ },
119
+ else => { // load subset (or unknown): the glass moved past the store
120
+ self.valid = false;
121
+ self.clearCommands();
122
+ return;
123
+ },
124
+ }
125
+ try std.testing.expectEqual(payload.len, cursor.offset);
126
+ }
127
+
128
+ /// Byte-for-byte equality of retained state (order + per-key encoded
129
+ /// command bytes).
130
+ fn expectEqualState(self: *const TestRetainedHost, other: *const TestRetainedHost) !void {
131
+ try std.testing.expect(self.valid and other.valid);
132
+ try std.testing.expectEqual(self.commands.count(), other.commands.count());
133
+ try std.testing.expectEqualSlices(u64, self.order.items, other.order.items);
134
+ for (self.order.items) |key| {
135
+ const mine = self.commands.get(key) orelse return error.RetainedKeyMissing;
136
+ const theirs = other.commands.get(key) orelse return error.RetainedKeyMissing;
137
+ try std.testing.expectEqualSlices(u8, mine, theirs);
138
+ }
139
+ }
140
+ };
141
+
142
+ /// Bounds-checked reader over the v2 wire with a structural command
143
+ /// skipper, so the retained store can slice each command's exact encoded
144
+ /// bytes without decoding their content.
145
+ const BinaryCursor = struct {
146
+ bytes: []const u8,
147
+ offset: usize = 0,
148
+
149
+ fn expectBytes(self: *BinaryCursor, expected: []const u8) !void {
150
+ if (self.offset + expected.len > self.bytes.len) return error.Truncated;
151
+ try std.testing.expectEqualSlices(u8, expected, self.bytes[self.offset..][0..expected.len]);
152
+ self.offset += expected.len;
153
+ }
154
+
155
+ fn skip(self: *BinaryCursor, count: usize) !void {
156
+ if (self.offset + count > self.bytes.len) return error.Truncated;
157
+ self.offset += count;
158
+ }
159
+
160
+ fn readU8(self: *BinaryCursor) !u8 {
161
+ if (self.offset + 1 > self.bytes.len) return error.Truncated;
162
+ defer self.offset += 1;
163
+ return self.bytes[self.offset];
164
+ }
165
+
166
+ fn readU32(self: *BinaryCursor) !u32 {
167
+ if (self.offset + 4 > self.bytes.len) return error.Truncated;
168
+ defer self.offset += 4;
169
+ return std.mem.readInt(u32, self.bytes[self.offset..][0..4], .little);
170
+ }
171
+
172
+ fn readU64(self: *BinaryCursor) !u64 {
173
+ if (self.offset + 8 > self.bytes.len) return error.Truncated;
174
+ defer self.offset += 8;
175
+ return std.mem.readInt(u64, self.bytes[self.offset..][0..8], .little);
176
+ }
177
+
178
+ /// Skip one encoded command, returning its exact byte slice.
179
+ fn command(self: *BinaryCursor) ![]const u8 {
180
+ const start = self.offset;
181
+ _ = try self.readU8(); // kind
182
+ const flags = try self.readU8();
183
+ try self.skip(16 + 4 + 4); // bounds + opacity + stroke_width
184
+ if (flags & 0x01 != 0) try self.skip(8); // id
185
+ if (flags & 0x02 != 0) try self.skip(16); // clip
186
+ if (flags & 0x04 != 0) try self.skip(24); // transform
187
+ if (flags & 0x08 != 0) { // shape
188
+ switch (try self.readU8()) {
189
+ 1 => try self.skip(16),
190
+ 2 => try self.skip(32),
191
+ 3 => try self.skip(36),
192
+ 4 => try self.skip(20),
193
+ 5 => {
194
+ const element_count = try self.readU32();
195
+ for (0..element_count) |_| {
196
+ const verb = try self.readU8();
197
+ const point_count: usize = switch (verb) {
198
+ 0, 1 => 1,
199
+ 2 => 2,
200
+ 3 => 3,
201
+ 4 => 0,
202
+ else => return error.UnknownShapeVerb,
203
+ };
204
+ try self.skip(point_count * 8);
205
+ }
206
+ },
207
+ else => return error.UnknownShapeTag,
208
+ }
209
+ }
210
+ if (flags & 0x10 != 0) { // paint
211
+ switch (try self.readU8()) {
212
+ 1 => try self.skip(16),
213
+ 2 => {
214
+ try self.skip(16); // start + end
215
+ const stop_count = try self.readU32();
216
+ try self.skip(@as(usize, stop_count) * 20);
217
+ },
218
+ else => return error.UnknownPaintTag,
219
+ }
220
+ }
221
+ if (flags & 0x20 != 0) { // image
222
+ try self.skip(8); // image id
223
+ const has_src = try self.readU8();
224
+ if (has_src != 0) try self.skip(16);
225
+ try self.skip(16 + 4 + 1 + 1 + 16); // dst + opacity + fit + sampling + radius
226
+ }
227
+ if (flags & 0x40 != 0) { // text
228
+ try self.skip(8 + 4 + 8 + 16); // font + size + origin + color
229
+ const text_len = try self.readU32();
230
+ try self.skip(text_len);
231
+ const has_layout = try self.readU8();
232
+ if (has_layout != 0) {
233
+ try self.skip(4 + 4 + 1 + 1); // max width + line height + wrap + align
234
+ const has_lines = try self.readU8();
235
+ if (has_lines != 0) {
236
+ const line_count = try self.readU32();
237
+ for (0..line_count) |_| {
238
+ try self.skip(4 + 4); // x + baseline
239
+ const line_len = try self.readU32();
240
+ try self.skip(line_len);
241
+ }
242
+ }
243
+ }
244
+ }
245
+ if (flags & 0x80 != 0) { // effect
246
+ switch (try self.readU8()) {
247
+ 1 => try self.skip(64),
248
+ 2 => try self.skip(20),
249
+ else => return error.UnknownEffectTag,
250
+ }
251
+ }
252
+ return self.bytes[start..self.offset];
253
+ }
254
+ };
255
+
256
+ // ---------------------------------------------------------------------------
257
+ // Shared fixtures
258
+
259
+ const PatchHarnessApp = struct {
260
+ fn app(self: *@This()) App {
261
+ return .{ .context = self, .name = "gpu-canvas-patch", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
262
+ }
263
+ };
264
+
265
+ const patch_surface_width: f32 = 320;
266
+ const patch_surface_height: f32 = 240;
267
+
268
+ fn createPatchHarness(app_state: *PatchHarnessApp) !*TestHarness() {
269
+ const harness = try TestHarness().create(std.testing.allocator, .{});
270
+ errdefer harness.destroy(std.testing.allocator);
271
+ harness.null_platform.gpu_surfaces = true;
272
+ harness.null_platform.gpu_surface_packet_binary = true;
273
+ try harness.start(app_state.app());
274
+ _ = try harness.runtime.createView(.{
275
+ .window_id = 1,
276
+ .label = "canvas",
277
+ .kind = .gpu_surface,
278
+ .frame = geometry.RectF.init(0, 0, patch_surface_width, patch_surface_height),
279
+ });
280
+ return harness;
281
+ }
282
+
283
+ const PresentBuffers = struct {
284
+ gpu_commands: []canvas.CanvasGpuCommand,
285
+ packet_buffer: []u8,
286
+ pixels: []u8,
287
+ scratch: []u8,
288
+
289
+ fn init(allocator: std.mem.Allocator) !PresentBuffers {
290
+ const pixel_len: usize = @as(usize, @intFromFloat(patch_surface_width)) * @as(usize, @intFromFloat(patch_surface_height)) * 4;
291
+ return .{
292
+ .gpu_commands = try allocator.alloc(canvas.CanvasGpuCommand, max_canvas_commands_per_view),
293
+ .packet_buffer = try allocator.alloc(u8, platform.max_gpu_surface_packet_binary_bytes),
294
+ .pixels = try allocator.alloc(u8, pixel_len),
295
+ .scratch = try allocator.alloc(u8, pixel_len),
296
+ };
297
+ }
298
+
299
+ fn deinit(self: *PresentBuffers, allocator: std.mem.Allocator) void {
300
+ allocator.free(self.gpu_commands);
301
+ allocator.free(self.packet_buffer);
302
+ allocator.free(self.pixels);
303
+ allocator.free(self.scratch);
304
+ }
305
+ };
306
+
307
+ fn presentFrame(harness: anytype, buffers: *PresentBuffers, frame_index: u64) !CanvasPresentationResult {
308
+ return harness.runtime.presentNextCanvasFrame(1, "canvas", .{
309
+ .frame_index = frame_index,
310
+ .timestamp_ns = frame_index * 16_000,
311
+ .surface_size = geometry.SizeF.init(patch_surface_width, patch_surface_height),
312
+ .scale = 1,
313
+ }, canvasFrameScratchStorage(&harness.runtime), buffers.gpu_commands, buffers.packet_buffer, buffers.pixels, buffers.scratch, canvas.Color.rgb8(15, 23, 42), null);
314
+ }
315
+
316
+ fn lastBinaryPayload(harness: anytype) []const u8 {
317
+ return harness.null_platform.gpu_surface_packet_present_binary_storage[0..harness.null_platform.gpu_surface_packet_present_binary_len];
318
+ }
319
+
320
+ /// A message-list-shaped scene: per message a keyed bubble rect and a
321
+ /// keyed wrapped text run, plus one unkeyed separator line near the top
322
+ /// (the synthetic-key degradation case rides the golden script too).
323
+ fn buildScriptScene(
324
+ step: usize,
325
+ message_count: usize,
326
+ scroll_offset: f32,
327
+ text_storage: *[16][64]u8,
328
+ commands: *[40]canvas.CanvasCommand,
329
+ ) []const canvas.CanvasCommand {
330
+ var count: usize = 0;
331
+ commands[count] = .{ .draw_line = .{
332
+ .from = geometry.PointF.init(8, 4 - scroll_offset),
333
+ .to = geometry.PointF.init(patch_surface_width - 8, 4 - scroll_offset),
334
+ .stroke = .{ .width = 1, .fill = .{ .color = canvas.Color.rgb8(71, 85, 105) } },
335
+ } };
336
+ count += 1;
337
+ for (0..message_count) |row| {
338
+ const y: f32 = @as(f32, @floatFromInt(row)) * 28 + 12 - scroll_offset;
339
+ commands[count] = .{ .fill_rounded_rect = .{
340
+ .id = @intCast(1_000 + row),
341
+ .rect = geometry.RectF.init(8, y, patch_surface_width - 16, 24),
342
+ .radius = canvas.Radius.all(6),
343
+ // The "toggle": message 0's bubble flips color from step 1 on.
344
+ .fill = .{ .color = if (row == 0 and step >= 1) canvas.Color.rgb8(37, 99, 235) else canvas.Color.rgb8(30, 41, 59) },
345
+ } };
346
+ count += 1;
347
+ const text = std.fmt.bufPrint(&text_storage[row], "message {d} body{s}", .{
348
+ row,
349
+ // The "text edit": message 1's body grows a suffix from step 3 on.
350
+ if (row == 1 and step >= 3) " (edited)" else "",
351
+ }) catch unreachable;
352
+ commands[count] = .{ .draw_text = .{
353
+ .id = @intCast(2_000 + row),
354
+ .font_id = 1,
355
+ .size = 13,
356
+ .origin = geometry.PointF.init(16, y + 16),
357
+ .color = canvas.Color.rgb8(226, 232, 240),
358
+ .text = text,
359
+ .text_layout = .{ .max_width = patch_surface_width - 32, .line_height = 15, .wrap = .word },
360
+ } };
361
+ count += 1;
362
+ }
363
+ return commands[0..count];
364
+ }
365
+
366
+ // ---------------------------------------------------------------------------
367
+
368
+ test "golden: a scripted patch sequence leaves host retained state identical to full re-presents" {
369
+ // Two engines drive the SAME scripted interaction (toggle, scroll
370
+ // step, text edit, message eviction+insertion). Engine A's host
371
+ // applies patches; engine B's host refuses them, so every B present
372
+ // is a full keyed rebuild. After every step the two retained stores
373
+ // must match byte-for-byte — command encodings AND draw order.
374
+ var app_a: PatchHarnessApp = .{};
375
+ const harness_a = try createPatchHarness(&app_a);
376
+ defer harness_a.destroy(std.testing.allocator);
377
+ var app_b: PatchHarnessApp = .{};
378
+ const harness_b = try createPatchHarness(&app_b);
379
+ defer harness_b.destroy(std.testing.allocator);
380
+ harness_b.null_platform.gpu_surface_packet_binary_patch = false;
381
+
382
+ var buffers = try PresentBuffers.init(std.testing.allocator);
383
+ defer buffers.deinit(std.testing.allocator);
384
+
385
+ var host_a = TestRetainedHost.init(std.testing.allocator);
386
+ defer host_a.deinit();
387
+ var host_b = TestRetainedHost.init(std.testing.allocator);
388
+ defer host_b.deinit();
389
+
390
+ var text_storage_a: [16][64]u8 = undefined;
391
+ var text_storage_b: [16][64]u8 = undefined;
392
+ var commands_a: [40]canvas.CanvasCommand = undefined;
393
+ var commands_b: [40]canvas.CanvasCommand = undefined;
394
+
395
+ // step 0: baseline; 1: toggle; 2: scroll step; 3: text edit;
396
+ // 4: evict message 5, keep the rest (list shrinks).
397
+ const steps = [_]struct { message_count: usize, scroll: f32 }{
398
+ .{ .message_count = 6, .scroll = 0 },
399
+ .{ .message_count = 6, .scroll = 0 },
400
+ .{ .message_count = 6, .scroll = 12 },
401
+ .{ .message_count = 6, .scroll = 12 },
402
+ .{ .message_count = 5, .scroll = 12 },
403
+ };
404
+
405
+ for (steps, 0..) |step, step_index| {
406
+ _ = try harness_a.runtime.setCanvasDisplayList(1, "canvas", .{
407
+ .commands = buildScriptScene(step_index, step.message_count, step.scroll, &text_storage_a, &commands_a),
408
+ });
409
+ _ = try harness_b.runtime.setCanvasDisplayList(1, "canvas", .{
410
+ .commands = buildScriptScene(step_index, step.message_count, step.scroll, &text_storage_b, &commands_b),
411
+ });
412
+
413
+ const result_a = try presentFrame(harness_a, &buffers, 10 + step_index);
414
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, result_a.mode);
415
+ try host_a.apply(lastBinaryPayload(harness_a));
416
+ const result_b = try presentFrame(harness_b, &buffers, 10 + step_index);
417
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, result_b.mode);
418
+ try host_b.apply(lastBinaryPayload(harness_b));
419
+
420
+ // A patches after its baseline — except the scroll step, where
421
+ // EVERY command moved and a patch would exceed the full present,
422
+ // so the engine deliberately re-baselines. B rebuilds every step.
423
+ const expected_action: u8 = if (step_index == 0 or step_index == 2) 2 else canvas.binary_packet_load_action_patch;
424
+ try std.testing.expectEqual(expected_action, harness_a.null_platform.gpu_surface_packet_present_binary_load_action);
425
+ try std.testing.expectEqual(@as(u8, 2), harness_b.null_platform.gpu_surface_packet_present_binary_load_action);
426
+
427
+ try host_a.expectEqualState(&host_b);
428
+ }
429
+
430
+ // The eviction step really evicted: message 5's bubble + text keys
431
+ // are gone from the patched store.
432
+ try std.testing.expect(host_a.commands.get(1_005) == null);
433
+ try std.testing.expect(host_a.commands.get(2_005) == null);
434
+ try std.testing.expect(harness_a.runtime.views[0].gpu_present_patch_evict_count >= 2);
435
+ }
436
+
437
+ test "patch presents carry only the change and the snapshot reports the mode" {
438
+ var app_state: PatchHarnessApp = .{};
439
+ const harness = try createPatchHarness(&app_state);
440
+ defer harness.destroy(std.testing.allocator);
441
+ var buffers = try PresentBuffers.init(std.testing.allocator);
442
+ defer buffers.deinit(std.testing.allocator);
443
+
444
+ var text_storage: [16][64]u8 = undefined;
445
+ var commands: [40]canvas.CanvasCommand = undefined;
446
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
447
+ .commands = buildScriptScene(0, 6, 0, &text_storage, &commands),
448
+ });
449
+ const baseline = try presentFrame(harness, &buffers, 20);
450
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, baseline.mode);
451
+ const full_bytes = harness.null_platform.gpu_surface_packet_present_binary_len;
452
+ try std.testing.expectEqual(@as(u8, 2), harness.null_platform.gpu_surface_packet_present_binary_load_action);
453
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.full, harness.runtime.views[0].gpu_present_packet_mode);
454
+ try std.testing.expect(harness.runtime.views[0].canvas_packet_baseline_valid);
455
+
456
+ // One toggled bubble color = one upsert riding a small patch.
457
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
458
+ .commands = buildScriptScene(1, 6, 0, &text_storage, &commands),
459
+ });
460
+ const toggled = try presentFrame(harness, &buffers, 21);
461
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, toggled.mode);
462
+ try std.testing.expectEqual(canvas.binary_packet_load_action_patch, harness.null_platform.gpu_surface_packet_present_binary_load_action);
463
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.gpu_surface_packet_present_binary_patch_count);
464
+ const view = &harness.runtime.views[0];
465
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.patch, view.gpu_present_packet_mode);
466
+ try std.testing.expectEqual(@as(usize, 1), view.gpu_present_patch_upsert_count);
467
+ try std.testing.expectEqual(@as(usize, 0), view.gpu_present_patch_evict_count);
468
+ try std.testing.expect(view.gpu_present_patch_bytes > 0);
469
+ try std.testing.expect(view.gpu_present_patch_bytes < full_bytes);
470
+ try std.testing.expectEqual(platform.GpuPresentFallbackReason.none, view.gpu_present_fallback_reason);
471
+
472
+ // The automation snapshot line carries the incremental telemetry.
473
+ var snapshot_buffer: [32768]u8 = undefined;
474
+ var snapshot_writer = std.Io.Writer.fixed(&snapshot_buffer);
475
+ try automation.snapshot.writeText(harness.runtime.automationSnapshot("Patch"), &snapshot_writer);
476
+ try std.testing.expect(std.mem.indexOf(u8, snapshot_writer.buffered(), "present_mode=patch") != null);
477
+ try std.testing.expect(std.mem.indexOf(u8, snapshot_writer.buffered(), "present_patch_upserts=1") != null);
478
+ try std.testing.expect(std.mem.indexOf(u8, snapshot_writer.buffered(), "present_retained_commands=13") != null);
479
+ }
480
+
481
+ test "rebuild dirty bounds derive from the patch edit script, not the window" {
482
+ // A Msg-driven rebuild replaces the whole display list; the presented
483
+ // summary cannot compare content, so historically the frame's dirty
484
+ // bounds degraded to the full window even when the retained-patch
485
+ // diff knew exactly one command changed. With a valid baseline the
486
+ // planner now derives dirty bounds from that SAME edit script.
487
+ var app_state: PatchHarnessApp = .{};
488
+ const harness = try createPatchHarness(&app_state);
489
+ defer harness.destroy(std.testing.allocator);
490
+ var buffers = try PresentBuffers.init(std.testing.allocator);
491
+ defer buffers.deinit(std.testing.allocator);
492
+
493
+ // 72 keyed rects in a 8-wide grid.
494
+ var rects: [72]canvas.CanvasCommand = undefined;
495
+ const buildGrid = struct {
496
+ fn rectAt(index: usize) geometry.RectF {
497
+ const col: f32 = @floatFromInt(index % 8);
498
+ const row: f32 = @floatFromInt(index / 8);
499
+ return geometry.RectF.init(col * 38 + 2, row * 24 + 2, 30, 18);
500
+ }
501
+ };
502
+ for (&rects, 0..) |*command, index| {
503
+ command.* = .{ .fill_rect = .{
504
+ .id = @intCast(3_000 + index),
505
+ .rect = buildGrid.rectAt(index),
506
+ .fill = .{ .color = canvas.Color.rgb8(30, 41, 59) },
507
+ } };
508
+ }
509
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
510
+ const baseline = try presentFrame(harness, &buffers, 60);
511
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, baseline.mode);
512
+ try std.testing.expect(harness.runtime.views[0].canvas_packet_baseline_valid);
513
+
514
+ // Rebuild with ONE color change: dirty is that command's rect, the
515
+ // present is a one-upsert patch, and the wire scissor matches.
516
+ rects[13].fill_rect.fill = .{ .color = canvas.Color.rgb8(37, 99, 235) };
517
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
518
+ const toggled = try presentFrame(harness, &buffers, 61);
519
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, toggled.mode);
520
+ try std.testing.expectEqualDeep(buildGrid.rectAt(13), toggled.frame.dirty_bounds.?);
521
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.patch, harness.runtime.views[0].gpu_present_packet_mode);
522
+ try std.testing.expectEqual(@as(usize, 1), harness.runtime.views[0].gpu_present_patch_upsert_count);
523
+
524
+ // Rebuild that MOVES a command: dirty covers old and new extents.
525
+ const moved_from = buildGrid.rectAt(20);
526
+ const moved_to = geometry.RectF.init(moved_from.x + 60, moved_from.y + 30, 30, 18);
527
+ rects[20].fill_rect.rect = moved_to;
528
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
529
+ const moved = try presentFrame(harness, &buffers, 62);
530
+ try std.testing.expectEqualDeep(geometry.RectF.unionWith(moved_from, moved_to), moved.frame.dirty_bounds.?);
531
+
532
+ // Identical rebuild (revision bumps, content does not): the edit
533
+ // script is empty, so nothing presents at all.
534
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
535
+ const unchanged = try presentFrame(harness, &buffers, 63);
536
+ try std.testing.expectEqual(CanvasPresentationMode.skipped, unchanged.mode);
537
+
538
+ // Reordering unchanged commands defeats a bounds union (overlap
539
+ // pixels depend on z-order): refinement refuses and the frame keeps
540
+ // the conservative summary dirty covering the keyed scene.
541
+ const swap = rects[0];
542
+ rects[0] = rects[1];
543
+ rects[1] = swap;
544
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
545
+ const reordered = try presentFrame(harness, &buffers, 64);
546
+ try std.testing.expect(reordered.frame.dirty_bounds != null);
547
+ try std.testing.expect(reordered.frame.dirty_bounds.?.width > 200);
548
+ try std.testing.expect(reordered.frame.dirty_bounds.?.height > 100);
549
+ }
550
+
551
+ test "rebuild dirty bounds derive from the edit script on small command lists too" {
552
+ // Refinement must have NO minimum command count: the index-vs-scan
553
+ // floor elsewhere trades lookup cost with byte-identical results,
554
+ // but refusing refinement changes the dirty AREA — a scene one
555
+ // command under a floor would pay a full-window re-raster on every
556
+ // Msg rebuild exactly where the derivation is cheapest. A real app
557
+ // regressed this way when a styling round shrank its retained plan
558
+ // from 64 to 62 commands and every click's present became a
559
+ // full-surface repaint.
560
+ var app_state: PatchHarnessApp = .{};
561
+ const harness = try createPatchHarness(&app_state);
562
+ defer harness.destroy(std.testing.allocator);
563
+ var buffers = try PresentBuffers.init(std.testing.allocator);
564
+ defer buffers.deinit(std.testing.allocator);
565
+
566
+ // 12 keyed rects — far below any index floor — in a 4-wide grid
567
+ // that fits the 320x240 harness surface.
568
+ var rects: [12]canvas.CanvasCommand = undefined;
569
+ const buildRow = struct {
570
+ fn rectAt(index: usize) geometry.RectF {
571
+ const col: f32 = @floatFromInt(index % 4);
572
+ const row: f32 = @floatFromInt(index / 4);
573
+ return geometry.RectF.init(col * 44 + 2, row * 24 + 2, 36, 20);
574
+ }
575
+ };
576
+ for (&rects, 0..) |*command, index| {
577
+ command.* = .{ .fill_rect = .{
578
+ .id = @intCast(5_000 + index),
579
+ .rect = buildRow.rectAt(index),
580
+ .fill = .{ .color = canvas.Color.rgb8(30, 41, 59) },
581
+ } };
582
+ }
583
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
584
+ const baseline = try presentFrame(harness, &buffers, 70);
585
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, baseline.mode);
586
+ try std.testing.expect(harness.runtime.views[0].canvas_packet_baseline_valid);
587
+
588
+ // Rebuild with ONE color change: dirty is that command's rect, not
589
+ // the window, and the present is a one-upsert patch.
590
+ rects[7].fill_rect.fill = .{ .color = canvas.Color.rgb8(37, 99, 235) };
591
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
592
+ const toggled = try presentFrame(harness, &buffers, 71);
593
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, toggled.mode);
594
+ try std.testing.expectEqualDeep(buildRow.rectAt(7), toggled.frame.dirty_bounds.?);
595
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.patch, harness.runtime.views[0].gpu_present_packet_mode);
596
+ try std.testing.expectEqual(@as(usize, 1), harness.runtime.views[0].gpu_present_patch_upsert_count);
597
+ }
598
+
599
+ test "a host that refuses patches gets a full resync in the same frame" {
600
+ var app_state: PatchHarnessApp = .{};
601
+ const harness = try createPatchHarness(&app_state);
602
+ defer harness.destroy(std.testing.allocator);
603
+ var buffers = try PresentBuffers.init(std.testing.allocator);
604
+ defer buffers.deinit(std.testing.allocator);
605
+
606
+ var text_storage: [16][64]u8 = undefined;
607
+ var commands: [40]canvas.CanvasCommand = undefined;
608
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
609
+ .commands = buildScriptScene(0, 4, 0, &text_storage, &commands),
610
+ });
611
+ _ = try presentFrame(harness, &buffers, 30);
612
+ try std.testing.expect(harness.runtime.views[0].canvas_packet_baseline_valid);
613
+
614
+ // Model retained state loss: the host refuses the patch; the SAME
615
+ // frame must land as a full keyed present on the packet path (no
616
+ // pixel fallback), with the refusal recorded in the running
617
+ // fallback-frame counter and the sticky reason cleared by the
618
+ // successful resync.
619
+ harness.null_platform.gpu_surface_packet_binary_patch = false;
620
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
621
+ .commands = buildScriptScene(1, 4, 0, &text_storage, &commands),
622
+ });
623
+ const resynced = try presentFrame(harness, &buffers, 31);
624
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, resynced.mode);
625
+ try std.testing.expectEqual(@as(u8, 2), harness.null_platform.gpu_surface_packet_present_binary_load_action);
626
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.gpu_surface_present_count);
627
+ const view = &harness.runtime.views[0];
628
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.full, view.gpu_present_packet_mode);
629
+ try std.testing.expectEqual(platform.GpuPresentFallbackReason.none, view.gpu_present_fallback_reason);
630
+ try std.testing.expectEqual(@as(usize, 1), view.gpu_present_fallback_frame_count);
631
+ try std.testing.expectEqual(platform.GpuPresentPath.packet, view.gpu_present_path);
632
+
633
+ // Patches recover the moment the host applies them again (a text
634
+ // edit touching one command; a scroll-everything step would
635
+ // deliberately re-baseline instead).
636
+ harness.null_platform.gpu_surface_packet_binary_patch = true;
637
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
638
+ .commands = buildScriptScene(3, 4, 0, &text_storage, &commands),
639
+ });
640
+ _ = try presentFrame(harness, &buffers, 32);
641
+ try std.testing.expectEqual(canvas.binary_packet_load_action_patch, harness.null_platform.gpu_surface_packet_present_binary_load_action);
642
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.patch, view.gpu_present_packet_mode);
643
+ }
644
+
645
+ test "a patch that outgrows the transport buffer resolves to a full present" {
646
+ // When ALMOST every command changes, the patch (upserts + the
647
+ // order vector) encodes larger than the keyed full present; a
648
+ // transport buffer sized exactly for the full encode forces the
649
+ // patch-overflow branch, and the frame must land as a FULL packet
650
+ // present — never a pixel fallback, never a truncated patch.
651
+ // (When EVERY command changes the engine skips the patch attempt
652
+ // outright — the golden test's scroll step pins that heuristic.)
653
+ var app_state: PatchHarnessApp = .{};
654
+ const harness = try createPatchHarness(&app_state);
655
+ defer harness.destroy(std.testing.allocator);
656
+ var buffers = try PresentBuffers.init(std.testing.allocator);
657
+ defer buffers.deinit(std.testing.allocator);
658
+
659
+ var rects: [12]canvas.CanvasCommand = undefined;
660
+ for (0..rects.len) |index| {
661
+ rects[index] = .{ .fill_rect = .{
662
+ .id = @intCast(100 + index),
663
+ .rect = geometry.RectF.init(@floatFromInt(index * 8), 8, 8, 8),
664
+ .fill = .{ .color = canvas.Color.rgb8(30, 41, 59) },
665
+ } };
666
+ }
667
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
668
+ _ = try presentFrame(harness, &buffers, 40);
669
+ const full_bytes = harness.null_platform.gpu_surface_packet_present_binary_len;
670
+
671
+ // Change all but one rect's color: same encoded size per command, so
672
+ // the full present still fits the exact-size buffer while the patch
673
+ // (11 of 12 re-encoded + the order vector on top) cannot.
674
+ for (0..rects.len - 1) |index| {
675
+ rects[index].fill_rect.fill = .{ .color = canvas.Color.rgb8(37, 99, 235) };
676
+ }
677
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
678
+ const exact_buffer = try std.testing.allocator.alloc(u8, full_bytes);
679
+ defer std.testing.allocator.free(exact_buffer);
680
+ const result = try harness.runtime.presentNextCanvasFrame(1, "canvas", .{
681
+ .frame_index = 41,
682
+ .timestamp_ns = 41 * 16_000,
683
+ .surface_size = geometry.SizeF.init(patch_surface_width, patch_surface_height),
684
+ .scale = 1,
685
+ }, canvasFrameScratchStorage(&harness.runtime), buffers.gpu_commands, exact_buffer, buffers.pixels, buffers.scratch, canvas.Color.rgb8(15, 23, 42), null);
686
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, result.mode);
687
+ try std.testing.expectEqual(@as(u8, 2), harness.null_platform.gpu_surface_packet_present_binary_load_action);
688
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.full, harness.runtime.views[0].gpu_present_packet_mode);
689
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.gpu_surface_present_count);
690
+ }
691
+
692
+ test "a JSON present invalidates the retained baseline so the next binary present is full" {
693
+ var app_state: PatchHarnessApp = .{};
694
+ const harness = try createPatchHarness(&app_state);
695
+ defer harness.destroy(std.testing.allocator);
696
+ var buffers = try PresentBuffers.init(std.testing.allocator);
697
+ defer buffers.deinit(std.testing.allocator);
698
+
699
+ var text_storage: [16][64]u8 = undefined;
700
+ var commands: [40]canvas.CanvasCommand = undefined;
701
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
702
+ .commands = buildScriptScene(0, 3, 0, &text_storage, &commands),
703
+ });
704
+ _ = try presentFrame(harness, &buffers, 50);
705
+ try std.testing.expect(harness.runtime.views[0].canvas_packet_baseline_valid);
706
+
707
+ // Binary host disappears for a frame: the JSON present bypasses the
708
+ // retained protocol, so the baseline drops on the engine side too.
709
+ harness.null_platform.gpu_surface_packet_binary = false;
710
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
711
+ .commands = buildScriptScene(1, 3, 0, &text_storage, &commands),
712
+ });
713
+ _ = try presentFrame(harness, &buffers, 51);
714
+ try std.testing.expect(!harness.runtime.views[0].canvas_packet_baseline_valid);
715
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.full, harness.runtime.views[0].gpu_present_packet_mode);
716
+
717
+ // Binary returns: the next present must be a FULL rebuild, not a
718
+ // patch against state the host no longer holds.
719
+ harness.null_platform.gpu_surface_packet_binary = true;
720
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
721
+ .commands = buildScriptScene(2, 3, 4, &text_storage, &commands),
722
+ });
723
+ _ = try presentFrame(harness, &buffers, 52);
724
+ try std.testing.expectEqual(@as(u8, 2), harness.null_platform.gpu_surface_packet_present_binary_load_action);
725
+ try std.testing.expect(harness.runtime.views[0].canvas_packet_baseline_valid);
726
+ }
727
+
728
+ test "chat-transcript-shaped interactions ride small patches" {
729
+ // The 200-message transcript shape from the heavy-frame packet test,
730
+ // driven through the interactions that dominate a chat view: a
731
+ // hover/toggle color flip, a single text edit, and a message append.
732
+ // Each must ride a patch that is a small fraction of the full
733
+ // present; a scroll step (every command moves) deliberately
734
+ // re-baselines with a full present instead of shipping a
735
+ // bigger-than-full patch.
736
+ const TestApp = struct {
737
+ fn app(self: *@This()) App {
738
+ return .{ .context = self, .name = "gpu-canvas-chat-patch", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
739
+ }
740
+ };
741
+
742
+ const harness = try TestHarness().create(std.testing.allocator, .{});
743
+ defer harness.destroy(std.testing.allocator);
744
+ harness.null_platform.gpu_surfaces = true;
745
+ harness.null_platform.gpu_surface_packet_binary = true;
746
+ var app_state: TestApp = .{};
747
+ try harness.start(app_state.app());
748
+
749
+ const surface_width: f32 = 480;
750
+ const row_count: usize = 200;
751
+ const row_height: f32 = 16;
752
+ const surface_height: f32 = @as(f32, @floatFromInt(row_count)) * row_height + 64;
753
+ _ = try harness.runtime.createView(.{
754
+ .window_id = 1,
755
+ .label = "canvas",
756
+ .kind = .gpu_surface,
757
+ .frame = geometry.RectF.init(0, 0, surface_width, surface_height),
758
+ });
759
+
760
+ var text_storage: [row_count][144]u8 = undefined;
761
+ var glyph_storage: [row_count][40]canvas.Glyph = undefined;
762
+ var command_storage: [row_count * 2]canvas.CanvasCommand = undefined;
763
+
764
+ const Scene = struct {
765
+ fn build(
766
+ texts: *[row_count][144]u8,
767
+ glyphs: *[row_count][40]canvas.Glyph,
768
+ commands: *[row_count * 2]canvas.CanvasCommand,
769
+ visible_rows: usize,
770
+ scroll: f32,
771
+ toggled_row: ?usize,
772
+ edited_row: ?usize,
773
+ ) []const canvas.CanvasCommand {
774
+ var count: usize = 0;
775
+ for (0..visible_rows) |row| {
776
+ const text = std.fmt.bufPrint(&texts[row], "message {d:0>4}: the quick brown fox jumps over the lazy dog while the transcript keeps growing line after line{s}", .{
777
+ row,
778
+ if (edited_row != null and edited_row.? == row) " (edited)" else "",
779
+ }) catch unreachable;
780
+ for (0..glyphs[row].len) |glyph_index| {
781
+ glyphs[row][glyph_index] = .{
782
+ .id = @intCast(32 + (glyph_index * 7 + row) % 90),
783
+ .x = @floatFromInt(glyph_index * 7),
784
+ .y = 0,
785
+ .advance = 7,
786
+ .text_start = glyph_index,
787
+ .text_len = 1,
788
+ };
789
+ }
790
+ const y: f32 = @as(f32, @floatFromInt(row)) * row_height + 8 - scroll;
791
+ commands[count] = .{ .fill_rounded_rect = .{
792
+ .id = @intCast(10_000 + row),
793
+ .rect = geometry.RectF.init(8, y - 4, surface_width - 16, row_height - 2),
794
+ .radius = canvas.Radius.all(6),
795
+ .fill = .{ .color = if (toggled_row != null and toggled_row.? == row) canvas.Color.rgb8(37, 99, 235) else canvas.Color.rgb8(30, 41, 59) },
796
+ } };
797
+ count += 1;
798
+ commands[count] = .{ .draw_text = .{
799
+ .id = @intCast(20_000 + row),
800
+ .font_id = 1,
801
+ .size = 13,
802
+ .origin = geometry.PointF.init(16, y + 8),
803
+ .color = canvas.Color.rgb8(226, 232, 240),
804
+ .text = text,
805
+ .glyphs = &glyphs[row],
806
+ .text_layout = .{ .max_width = surface_width - 40, .line_height = 15, .wrap = .word },
807
+ } };
808
+ count += 1;
809
+ }
810
+ return commands[0..count];
811
+ }
812
+ };
813
+
814
+ const gpu_commands = try std.testing.allocator.alloc(canvas.CanvasGpuCommand, max_canvas_commands_per_view);
815
+ defer std.testing.allocator.free(gpu_commands);
816
+ const packet_buffer = try std.testing.allocator.alloc(u8, platform.max_gpu_surface_packet_binary_bytes);
817
+ defer std.testing.allocator.free(packet_buffer);
818
+ const pixel_len: usize = @as(usize, @intFromFloat(surface_width)) * @as(usize, @intFromFloat(surface_height)) * 4;
819
+ const pixels = try std.testing.allocator.alloc(u8, pixel_len);
820
+ defer std.testing.allocator.free(pixels);
821
+ const scratch = try std.testing.allocator.alloc(u8, pixel_len);
822
+ defer std.testing.allocator.free(scratch);
823
+
824
+ const present = struct {
825
+ fn frame(h: anytype, gpu: []canvas.CanvasGpuCommand, buffer: []u8, px: []u8, sc: []u8, w: f32, hgt: f32, frame_index: u64) !CanvasPresentationResult {
826
+ return h.runtime.presentNextCanvasFrame(1, "canvas", .{
827
+ .frame_index = frame_index,
828
+ .timestamp_ns = frame_index * 16_000,
829
+ .surface_size = geometry.SizeF.init(w, hgt),
830
+ .scale = 1,
831
+ }, canvasFrameScratchStorage(&h.runtime), gpu, buffer, px, sc, canvas.Color.rgb8(15, 23, 42), null);
832
+ }
833
+ };
834
+
835
+ // Baseline: 199 messages, full keyed present.
836
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
837
+ .commands = Scene.build(&text_storage, &glyph_storage, &command_storage, 199, 0, null, null),
838
+ });
839
+ _ = try present.frame(harness, gpu_commands, packet_buffer, pixels, scratch, surface_width, surface_height, 60);
840
+ const full_bytes = harness.null_platform.gpu_surface_packet_present_binary_len;
841
+ try std.testing.expectEqual(@as(u8, 2), harness.null_platform.gpu_surface_packet_present_binary_load_action);
842
+ const view = &harness.runtime.views[0];
843
+
844
+ // (a) One bubble toggled — a switch/hover-shaped frame.
845
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
846
+ .commands = Scene.build(&text_storage, &glyph_storage, &command_storage, 199, 0, 3, null),
847
+ });
848
+ _ = try present.frame(harness, gpu_commands, packet_buffer, pixels, scratch, surface_width, surface_height, 61);
849
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.patch, view.gpu_present_packet_mode);
850
+ try std.testing.expectEqual(@as(usize, 1), view.gpu_present_patch_upsert_count);
851
+ const toggle_patch_bytes = view.gpu_present_patch_bytes;
852
+ try std.testing.expect(toggle_patch_bytes * 10 < full_bytes);
853
+
854
+ // (b) One text edit.
855
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
856
+ .commands = Scene.build(&text_storage, &glyph_storage, &command_storage, 199, 0, 3, 7),
857
+ });
858
+ _ = try present.frame(harness, gpu_commands, packet_buffer, pixels, scratch, surface_width, surface_height, 62);
859
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.patch, view.gpu_present_packet_mode);
860
+ try std.testing.expectEqual(@as(usize, 1), view.gpu_present_patch_upsert_count);
861
+ const edit_patch_bytes = view.gpu_present_patch_bytes;
862
+ try std.testing.expect(edit_patch_bytes * 10 < full_bytes);
863
+
864
+ // (c) One message appended — the dominant chat-transcript frame shape.
865
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
866
+ .commands = Scene.build(&text_storage, &glyph_storage, &command_storage, 200, 0, 3, 7),
867
+ });
868
+ _ = try present.frame(harness, gpu_commands, packet_buffer, pixels, scratch, surface_width, surface_height, 63);
869
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.patch, view.gpu_present_packet_mode);
870
+ try std.testing.expectEqual(@as(usize, 2), view.gpu_present_patch_upsert_count);
871
+ const append_patch_bytes = view.gpu_present_patch_bytes;
872
+ try std.testing.expect(append_patch_bytes * 10 < full_bytes);
873
+
874
+ // (d) A scroll step moves every command: the engine re-baselines with
875
+ // a full present rather than shipping a bigger-than-full patch.
876
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
877
+ .commands = Scene.build(&text_storage, &glyph_storage, &command_storage, 200, row_height, 3, 7),
878
+ });
879
+ _ = try present.frame(harness, gpu_commands, packet_buffer, pixels, scratch, surface_width, surface_height, 64);
880
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.full, view.gpu_present_packet_mode);
881
+ try std.testing.expectEqual(@as(u8, 2), harness.null_platform.gpu_surface_packet_present_binary_load_action);
882
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.gpu_surface_present_count);
883
+
884
+ // ...and patches resume on the next sparse change.
885
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{
886
+ .commands = Scene.build(&text_storage, &glyph_storage, &command_storage, 200, row_height, 5, 7),
887
+ });
888
+ _ = try present.frame(harness, gpu_commands, packet_buffer, pixels, scratch, surface_width, surface_height, 65);
889
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.patch, view.gpu_present_packet_mode);
890
+ }
891
+
892
+ test "command fingerprints cover every encoded field" {
893
+ // The patch differ trusts fingerprints as "byte-identical wire
894
+ // encoding": a field that encodes but does not hash would ship stale
895
+ // pixels. One mutation per encoded field must change the
896
+ // fingerprint.
897
+ const stops = [_]canvas.GradientStop{
898
+ .{ .offset = 0, .color = canvas.Color.rgb8(255, 255, 255) },
899
+ .{ .offset = 1, .color = canvas.Color.rgb8(37, 99, 235) },
900
+ };
901
+ const glyphs = [_]canvas.Glyph{.{ .id = 4, .x = 1, .y = 2, .advance = 7 }};
902
+ const base = canvas.CanvasGpuCommand{
903
+ .command_index = 3,
904
+ .id = 41,
905
+ .kind = .draw_text,
906
+ .bounds = geometry.RectF.init(1, 2, 3, 4),
907
+ .opacity = 0.5,
908
+ .stroke_width = 2,
909
+ .clip = geometry.RectF.init(0, 0, 10, 10),
910
+ .transform = canvas.Affine.translate(1, 1),
911
+ .shape = .{ .rect = geometry.RectF.init(5, 5, 6, 6) },
912
+ .paint = .{ .linear_gradient = .{ .start = geometry.PointF.init(0, 0), .end = geometry.PointF.init(1, 1), .stops = &stops } },
913
+ .image = .{ .image_id = 9, .dst = geometry.RectF.init(0, 0, 4, 4) },
914
+ .text = .{ .font_id = 1, .size = 13, .origin = geometry.PointF.init(2, 12), .color = canvas.Color.rgb8(1, 2, 3), .text = "hello", .glyphs = &glyphs, .text_layout = .{ .max_width = 100, .line_height = 15, .wrap = .word } },
915
+ .effect = .{ .shadow = .{ .rect = geometry.RectF.init(0, 0, 4, 4), .blur = 2 } },
916
+ };
917
+ const base_fingerprint = canvas.canvasGpuCommandFingerprint(base);
918
+
919
+ var changed = base;
920
+ changed.kind = .fill_rect_solid;
921
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
922
+ changed = base;
923
+ changed.bounds = geometry.RectF.init(1, 2, 3, 5);
924
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
925
+ changed = base;
926
+ changed.opacity = 0.75;
927
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
928
+ changed = base;
929
+ changed.stroke_width = 3;
930
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
931
+ changed = base;
932
+ changed.id = 42;
933
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
934
+ changed = base;
935
+ changed.clip = geometry.RectF.init(0, 0, 11, 10);
936
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
937
+ changed = base;
938
+ changed.transform = canvas.Affine.translate(2, 1);
939
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
940
+ changed = base;
941
+ changed.shape = .{ .rect = geometry.RectF.init(5, 5, 7, 6) };
942
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
943
+ changed = base;
944
+ changed.paint = .{ .color = canvas.Color.rgb8(1, 1, 1) };
945
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
946
+ changed = base;
947
+ changed.image = .{ .image_id = 10, .dst = geometry.RectF.init(0, 0, 4, 4) };
948
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
949
+ changed = base;
950
+ changed.text.?.text = "hellp";
951
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
952
+ changed = base;
953
+ changed.text.?.color = canvas.Color.rgb8(1, 2, 4);
954
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
955
+ changed = base;
956
+ changed.text.?.text_layout = .{ .max_width = 101, .line_height = 15, .wrap = .word };
957
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
958
+ const changed_glyphs = [_]canvas.Glyph{.{ .id = 5, .x = 1, .y = 2, .advance = 7 }};
959
+ changed = base;
960
+ changed.text.?.glyphs = &changed_glyphs;
961
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
962
+ changed = base;
963
+ changed.effect = .{ .shadow = .{ .rect = geometry.RectF.init(0, 0, 4, 4), .blur = 3 } };
964
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
965
+
966
+ // Keyed commands retain under their ObjectId; unkeyed commands get a
967
+ // synthetic key that changes when their index or content moves.
968
+ try std.testing.expectEqual(@as(u64, 41), canvas.canvasGpuPacketCommandKey(base, base_fingerprint));
969
+ var unkeyed = base;
970
+ unkeyed.id = null;
971
+ const unkeyed_fingerprint = canvas.canvasGpuCommandFingerprint(unkeyed);
972
+ const synthetic = canvas.canvasGpuPacketCommandKey(unkeyed, unkeyed_fingerprint);
973
+ var moved = unkeyed;
974
+ moved.command_index = 4;
975
+ try std.testing.expect(canvas.canvasGpuPacketCommandKey(moved, unkeyed_fingerprint) != synthetic);
976
+ }
977
+
978
+ test "pixel presents adopt a dirty-refinement baseline only for opted-in hosts and never feed patches" {
979
+ // The mobile embed host presents through the CPU pixel path only and
980
+ // opts in (`Options.pixel_present_retained_baseline`): pixel presents
981
+ // then keep the keyed mirror alive so a one-command rebuild's dirty
982
+ // bounds refine to that command. The mirror is marked pixel-adopted,
983
+ // and a packet present that arrives against it must resync FULL —
984
+ // no host ever retained a dictionary for a pixel present.
985
+ var app_state: PatchHarnessApp = .{};
986
+ const harness = try createPatchHarness(&app_state);
987
+ defer harness.destroy(std.testing.allocator);
988
+ var buffers = try PresentBuffers.init(std.testing.allocator);
989
+ defer buffers.deinit(std.testing.allocator);
990
+ harness.runtime.options.pixel_present_retained_baseline = true;
991
+
992
+ // 72 keyed rects (past the small-list refinement gate) in a 8-wide grid.
993
+ var rects: [72]canvas.CanvasCommand = undefined;
994
+ const buildGrid = struct {
995
+ fn rectAt(index: usize) geometry.RectF {
996
+ const col: f32 = @floatFromInt(index % 8);
997
+ const row: f32 = @floatFromInt(index / 8);
998
+ return geometry.RectF.init(col * 38 + 2, row * 24 + 2, 30, 18);
999
+ }
1000
+ };
1001
+ for (&rects, 0..) |*command, index| {
1002
+ command.* = .{ .fill_rect = .{
1003
+ .id = @intCast(3_000 + index),
1004
+ .rect = buildGrid.rectAt(index),
1005
+ .fill = .{ .color = canvas.Color.rgb8(30, 41, 59) },
1006
+ } };
1007
+ }
1008
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
1009
+ _ = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
1010
+ .frame_index = 70,
1011
+ .timestamp_ns = 70 * 16_000,
1012
+ .surface_size = geometry.SizeF.init(patch_surface_width, patch_surface_height),
1013
+ .scale = 1,
1014
+ }, canvasFrameScratchStorage(&harness.runtime), buffers.pixels, buffers.scratch, canvas.Color.rgb8(15, 23, 42));
1015
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.gpu_surface_present_count);
1016
+ try std.testing.expect(harness.runtime.views[0].canvas_packet_baseline_valid);
1017
+ try std.testing.expect(harness.runtime.views[0].canvas_packet_baseline_pixels);
1018
+
1019
+ // One changed rect: the next PIXEL present's dirty bounds are that
1020
+ // command's rect (the pixel-adopted mirror refined them), so a
1021
+ // mobile host uploads the rect, not the surface.
1022
+ rects[13].fill_rect.fill = .{ .color = canvas.Color.rgb8(37, 99, 235) };
1023
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
1024
+ const toggled = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
1025
+ .frame_index = 71,
1026
+ .timestamp_ns = 71 * 16_000,
1027
+ .surface_size = geometry.SizeF.init(patch_surface_width, patch_surface_height),
1028
+ .scale = 1,
1029
+ }, canvasFrameScratchStorage(&harness.runtime), buffers.pixels, buffers.scratch, canvas.Color.rgb8(15, 23, 42));
1030
+ try std.testing.expect(!toggled.full_repaint);
1031
+ try std.testing.expectEqualDeep(buildGrid.rectAt(13), toggled.dirty_bounds.?);
1032
+ try std.testing.expectEqualDeep(buildGrid.rectAt(13), harness.null_platform.gpu_surface_present_dirty_bounds.?);
1033
+
1034
+ // A packet present against the pixel-adopted mirror resyncs FULL —
1035
+ // the patch gate refuses it even though the baseline is valid.
1036
+ rects[14].fill_rect.fill = .{ .color = canvas.Color.rgb8(220, 38, 38) };
1037
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
1038
+ const packet_resync = try presentFrame(harness, &buffers, 72);
1039
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, packet_resync.mode);
1040
+ try std.testing.expectEqual(@as(u8, 2), harness.null_platform.gpu_surface_packet_present_binary_load_action);
1041
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.full, harness.runtime.views[0].gpu_present_packet_mode);
1042
+ try std.testing.expect(!harness.runtime.views[0].canvas_packet_baseline_pixels);
1043
+
1044
+ // The packet-adopted baseline behaves exactly as before: the next
1045
+ // one-command change rides a patch.
1046
+ rects[15].fill_rect.fill = .{ .color = canvas.Color.rgb8(22, 163, 74) };
1047
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &rects });
1048
+ _ = try presentFrame(harness, &buffers, 73);
1049
+ try std.testing.expectEqual(canvas.binary_packet_load_action_patch, harness.null_platform.gpu_surface_packet_present_binary_load_action);
1050
+ try std.testing.expectEqual(platform.GpuPresentPacketMode.patch, harness.runtime.views[0].gpu_present_packet_mode);
1051
+ }