@fairyhunter13/opentui-core 0.1.113 → 0.1.114

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 (591) hide show
  1. package/dev/keypress-debug-renderer.ts +148 -0
  2. package/dev/keypress-debug.ts +43 -0
  3. package/dev/print-env-vars.ts +32 -0
  4. package/dev/test-tmux-graphics-334.sh +68 -0
  5. package/dev/thai-debug-test.ts +68 -0
  6. package/docs/development.md +144 -0
  7. package/package.json +62 -53
  8. package/scripts/build.ts +400 -0
  9. package/scripts/publish.ts +60 -0
  10. package/src/3d/SpriteResourceManager.ts +286 -0
  11. package/src/3d/SpriteUtils.ts +70 -0
  12. package/src/3d/TextureUtils.ts +196 -0
  13. package/src/3d/ThreeRenderable.ts +197 -0
  14. package/src/3d/WGPURenderer.ts +294 -0
  15. package/src/3d/animation/ExplodingSpriteEffect.ts +513 -0
  16. package/src/3d/animation/PhysicsExplodingSpriteEffect.ts +429 -0
  17. package/src/3d/animation/SpriteAnimator.ts +633 -0
  18. package/src/3d/animation/SpriteParticleGenerator.ts +435 -0
  19. package/src/3d/canvas.ts +464 -0
  20. package/src/3d/index.ts +12 -0
  21. package/src/3d/physics/PlanckPhysicsAdapter.ts +72 -0
  22. package/src/3d/physics/RapierPhysicsAdapter.ts +66 -0
  23. package/src/3d/physics/physics-interface.ts +31 -0
  24. package/src/3d/shaders/supersampling.wgsl +201 -0
  25. package/src/3d.ts +3 -0
  26. package/src/NativeSpanFeed.ts +300 -0
  27. package/src/Renderable.ts +1704 -0
  28. package/src/__snapshots__/buffer.test.ts.snap +28 -0
  29. package/src/animation/Timeline.test.ts +2709 -0
  30. package/src/animation/Timeline.ts +598 -0
  31. package/src/ansi.ts +18 -0
  32. package/src/benchmark/attenuation-benchmark.ts +81 -0
  33. package/src/benchmark/colormatrix-benchmark.ts +128 -0
  34. package/src/benchmark/gain-benchmark.ts +80 -0
  35. package/src/benchmark/latest-all-bench-run.json +707 -0
  36. package/src/benchmark/latest-async-bench-run.json +336 -0
  37. package/src/benchmark/latest-default-bench-run.json +657 -0
  38. package/src/benchmark/latest-large-bench-run.json +707 -0
  39. package/src/benchmark/latest-quick-bench-run.json +207 -0
  40. package/src/benchmark/markdown-benchmark.ts +1796 -0
  41. package/src/benchmark/native-span-feed-async-benchmark.ts +355 -0
  42. package/src/benchmark/native-span-feed-benchmark.md +56 -0
  43. package/src/benchmark/native-span-feed-benchmark.ts +596 -0
  44. package/src/benchmark/native-span-feed-compare.ts +280 -0
  45. package/src/benchmark/renderer-benchmark.ts +754 -0
  46. package/src/benchmark/text-table-benchmark.ts +948 -0
  47. package/src/buffer.test.ts +291 -0
  48. package/src/buffer.ts +554 -0
  49. package/src/console.test.ts +612 -0
  50. package/src/console.ts +1254 -0
  51. package/src/edit-buffer.test.ts +1769 -0
  52. package/src/edit-buffer.ts +411 -0
  53. package/src/editor-view.test.ts +1032 -0
  54. package/src/editor-view.ts +284 -0
  55. package/src/examples/ascii-font-selection-demo.ts +245 -0
  56. package/src/examples/assets/Water_2_M_Normal.jpg +0 -0
  57. package/src/examples/assets/concrete.png +0 -0
  58. package/src/examples/assets/crate.png +0 -0
  59. package/src/examples/assets/crate_emissive.png +0 -0
  60. package/src/examples/assets/forrest_background.png +0 -0
  61. package/src/examples/assets/hast-example.json +1018 -0
  62. package/src/examples/assets/heart.png +0 -0
  63. package/src/examples/assets/main_char_heavy_attack.png +0 -0
  64. package/src/examples/assets/main_char_idle.png +0 -0
  65. package/src/examples/assets/main_char_jump_end.png +0 -0
  66. package/src/examples/assets/main_char_jump_landing.png +0 -0
  67. package/src/examples/assets/main_char_jump_start.png +0 -0
  68. package/src/examples/assets/main_char_run_loop.png +0 -0
  69. package/src/examples/assets/roughness_map.jpg +0 -0
  70. package/src/examples/build.ts +115 -0
  71. package/src/examples/code-demo.ts +924 -0
  72. package/src/examples/console-demo.ts +358 -0
  73. package/src/examples/core-plugin-slots-demo.ts +759 -0
  74. package/src/examples/diff-demo.ts +701 -0
  75. package/src/examples/draggable-three-demo.ts +259 -0
  76. package/src/examples/editor-demo.ts +322 -0
  77. package/src/examples/extmarks-demo.ts +196 -0
  78. package/src/examples/focus-restore-demo.ts +310 -0
  79. package/src/examples/fonts.ts +245 -0
  80. package/src/examples/fractal-shader-demo.ts +268 -0
  81. package/src/examples/framebuffer-demo.ts +674 -0
  82. package/src/examples/full-unicode-demo.ts +241 -0
  83. package/src/examples/golden-star-demo.ts +933 -0
  84. package/src/examples/grayscale-buffer-demo.ts +249 -0
  85. package/src/examples/hast-syntax-highlighting-demo.ts +129 -0
  86. package/src/examples/index.ts +926 -0
  87. package/src/examples/input-demo.ts +377 -0
  88. package/src/examples/input-select-layout-demo.ts +425 -0
  89. package/src/examples/install.sh +143 -0
  90. package/src/examples/keypress-debug-demo.ts +452 -0
  91. package/src/examples/lib/HexList.ts +122 -0
  92. package/src/examples/lib/PaletteGrid.ts +125 -0
  93. package/src/examples/lib/standalone-keys.ts +25 -0
  94. package/src/examples/lib/tab-controller.ts +243 -0
  95. package/src/examples/lights-phong-demo.ts +290 -0
  96. package/src/examples/link-demo.ts +220 -0
  97. package/src/examples/live-state-demo.ts +480 -0
  98. package/src/examples/markdown-demo.ts +725 -0
  99. package/src/examples/mouse-interaction-demo.ts +428 -0
  100. package/src/examples/nested-zindex-demo.ts +357 -0
  101. package/src/examples/opacity-example.ts +235 -0
  102. package/src/examples/opentui-demo.ts +1057 -0
  103. package/src/examples/physx-planck-2d-demo.ts +623 -0
  104. package/src/examples/physx-rapier-2d-demo.ts +655 -0
  105. package/src/examples/relative-positioning-demo.ts +323 -0
  106. package/src/examples/scroll-example.ts +214 -0
  107. package/src/examples/scrollbox-mouse-test.ts +112 -0
  108. package/src/examples/scrollbox-overlay-hit-test.ts +206 -0
  109. package/src/examples/select-demo.ts +237 -0
  110. package/src/examples/shader-cube-demo.ts +1015 -0
  111. package/src/examples/simple-layout-example.ts +591 -0
  112. package/src/examples/slider-demo.ts +617 -0
  113. package/src/examples/split-mode-demo.ts +453 -0
  114. package/src/examples/sprite-animation-demo.ts +443 -0
  115. package/src/examples/sprite-particle-generator-demo.ts +486 -0
  116. package/src/examples/static-sprite-demo.ts +193 -0
  117. package/src/examples/sticky-scroll-example.ts +308 -0
  118. package/src/examples/styled-text-demo.ts +282 -0
  119. package/src/examples/tab-select-demo.ts +219 -0
  120. package/src/examples/terminal-title.ts +29 -0
  121. package/src/examples/terminal.ts +305 -0
  122. package/src/examples/text-node-demo.ts +416 -0
  123. package/src/examples/text-selection-demo.ts +377 -0
  124. package/src/examples/text-table-demo.ts +503 -0
  125. package/src/examples/text-truncation-demo.ts +481 -0
  126. package/src/examples/text-wrap.ts +757 -0
  127. package/src/examples/texture-loading-demo.ts +259 -0
  128. package/src/examples/timeline-example.ts +670 -0
  129. package/src/examples/transparency-demo.ts +400 -0
  130. package/src/examples/vnode-composition-demo.ts +404 -0
  131. package/src/examples/wide-grapheme-overlay-demo.ts +280 -0
  132. package/src/index.ts +24 -0
  133. package/src/lib/KeyHandler.integration.test.ts +292 -0
  134. package/src/lib/KeyHandler.stopPropagation.test.ts +289 -0
  135. package/src/lib/KeyHandler.test.ts +662 -0
  136. package/src/lib/KeyHandler.ts +222 -0
  137. package/src/lib/RGBA.test.ts +984 -0
  138. package/src/lib/RGBA.ts +204 -0
  139. package/src/lib/ascii.font.ts +330 -0
  140. package/src/lib/border.test.ts +83 -0
  141. package/src/lib/border.ts +170 -0
  142. package/src/lib/bunfs.test.ts +27 -0
  143. package/src/lib/bunfs.ts +18 -0
  144. package/src/lib/clipboard.test.ts +41 -0
  145. package/src/lib/clipboard.ts +47 -0
  146. package/src/lib/clock.ts +35 -0
  147. package/src/lib/data-paths.test.ts +133 -0
  148. package/src/lib/data-paths.ts +109 -0
  149. package/src/lib/debounce.ts +106 -0
  150. package/src/lib/detect-links.test.ts +98 -0
  151. package/src/lib/detect-links.ts +56 -0
  152. package/src/lib/env.test.ts +228 -0
  153. package/src/lib/env.ts +209 -0
  154. package/src/lib/extmarks-history.ts +51 -0
  155. package/src/lib/extmarks-multiwidth.test.ts +322 -0
  156. package/src/lib/extmarks.test.ts +3457 -0
  157. package/src/lib/extmarks.ts +843 -0
  158. package/src/lib/fonts/block.json +405 -0
  159. package/src/lib/fonts/grid.json +265 -0
  160. package/src/lib/fonts/huge.json +741 -0
  161. package/src/lib/fonts/pallet.json +314 -0
  162. package/src/lib/fonts/shade.json +591 -0
  163. package/src/lib/fonts/slick.json +321 -0
  164. package/src/lib/fonts/tiny.json +69 -0
  165. package/src/lib/hast-styled-text.ts +59 -0
  166. package/src/lib/index.ts +21 -0
  167. package/src/lib/keymapping.test.ts +317 -0
  168. package/src/lib/keymapping.ts +115 -0
  169. package/src/lib/objects-in-viewport.test.ts +787 -0
  170. package/src/lib/objects-in-viewport.ts +153 -0
  171. package/src/lib/output.capture.ts +58 -0
  172. package/src/lib/parse.keypress-kitty.protocol.test.ts +340 -0
  173. package/src/lib/parse.keypress-kitty.test.ts +663 -0
  174. package/src/lib/parse.keypress-kitty.ts +439 -0
  175. package/src/lib/parse.keypress.test.ts +1849 -0
  176. package/src/lib/parse.keypress.ts +397 -0
  177. package/src/lib/parse.mouse.test.ts +552 -0
  178. package/src/lib/parse.mouse.ts +232 -0
  179. package/src/lib/paste.ts +16 -0
  180. package/src/lib/queue.ts +65 -0
  181. package/src/lib/renderable.validations.test.ts +87 -0
  182. package/src/lib/renderable.validations.ts +83 -0
  183. package/src/lib/scroll-acceleration.ts +98 -0
  184. package/src/lib/selection.ts +240 -0
  185. package/src/lib/singleton.ts +28 -0
  186. package/src/lib/stdin-parser.test.ts +2290 -0
  187. package/src/lib/stdin-parser.ts +1810 -0
  188. package/src/lib/styled-text.ts +178 -0
  189. package/src/lib/terminal-capability-detection.test.ts +202 -0
  190. package/src/lib/terminal-capability-detection.ts +79 -0
  191. package/src/lib/terminal-palette.test.ts +878 -0
  192. package/src/lib/terminal-palette.ts +383 -0
  193. package/src/lib/tree-sitter/assets/README.md +118 -0
  194. package/src/lib/tree-sitter/assets/update.ts +334 -0
  195. package/src/lib/tree-sitter/assets.d.ts +9 -0
  196. package/src/lib/tree-sitter/cache.test.ts +273 -0
  197. package/src/lib/tree-sitter/client.test.ts +1165 -0
  198. package/src/lib/tree-sitter/client.ts +607 -0
  199. package/src/lib/tree-sitter/default-parsers.ts +86 -0
  200. package/src/lib/tree-sitter/download-utils.ts +148 -0
  201. package/src/lib/tree-sitter/index.ts +28 -0
  202. package/src/lib/tree-sitter/parser.worker.ts +1042 -0
  203. package/src/lib/tree-sitter/parsers-config.ts +81 -0
  204. package/src/lib/tree-sitter/resolve-ft.test.ts +55 -0
  205. package/src/lib/tree-sitter/resolve-ft.ts +189 -0
  206. package/src/lib/tree-sitter/types.ts +82 -0
  207. package/src/lib/tree-sitter-styled-text.test.ts +1253 -0
  208. package/src/lib/tree-sitter-styled-text.ts +306 -0
  209. package/src/lib/validate-dir-name.ts +55 -0
  210. package/src/lib/yoga.options.test.ts +628 -0
  211. package/src/lib/yoga.options.ts +346 -0
  212. package/src/plugins/core-slot.ts +579 -0
  213. package/src/plugins/registry.ts +402 -0
  214. package/src/plugins/types.ts +46 -0
  215. package/src/post/effects.ts +930 -0
  216. package/src/post/filters.ts +489 -0
  217. package/src/post/matrices.ts +288 -0
  218. package/src/renderables/ASCIIFont.ts +219 -0
  219. package/src/renderables/Box.test.ts +205 -0
  220. package/src/renderables/Box.ts +326 -0
  221. package/src/renderables/Code.test.ts +2062 -0
  222. package/src/renderables/Code.ts +357 -0
  223. package/src/renderables/Diff.regression.test.ts +226 -0
  224. package/src/renderables/Diff.test.ts +3101 -0
  225. package/src/renderables/Diff.ts +1211 -0
  226. package/src/renderables/EditBufferRenderable.test.ts +288 -0
  227. package/src/renderables/EditBufferRenderable.ts +1166 -0
  228. package/src/renderables/FrameBuffer.ts +47 -0
  229. package/src/renderables/Input.test.ts +1228 -0
  230. package/src/renderables/Input.ts +247 -0
  231. package/src/renderables/LineNumberRenderable.ts +724 -0
  232. package/src/renderables/Markdown.ts +1393 -0
  233. package/src/renderables/ScrollBar.ts +422 -0
  234. package/src/renderables/ScrollBox.ts +883 -0
  235. package/src/renderables/Select.test.ts +1033 -0
  236. package/src/renderables/Select.ts +524 -0
  237. package/src/renderables/Slider.test.ts +456 -0
  238. package/src/renderables/Slider.ts +342 -0
  239. package/src/renderables/TabSelect.test.ts +197 -0
  240. package/src/renderables/TabSelect.ts +455 -0
  241. package/src/renderables/Text.selection-buffer.test.ts +123 -0
  242. package/src/renderables/Text.test.ts +2660 -0
  243. package/src/renderables/Text.ts +147 -0
  244. package/src/renderables/TextBufferRenderable.ts +518 -0
  245. package/src/renderables/TextNode.test.ts +1058 -0
  246. package/src/renderables/TextNode.ts +325 -0
  247. package/src/renderables/TextTable.test.ts +1421 -0
  248. package/src/renderables/TextTable.ts +1344 -0
  249. package/src/renderables/Textarea.ts +430 -0
  250. package/src/renderables/TimeToFirstDraw.ts +89 -0
  251. package/src/renderables/__snapshots__/Code.test.ts.snap +13 -0
  252. package/src/renderables/__snapshots__/Diff.test.ts.snap +785 -0
  253. package/src/renderables/__snapshots__/Text.test.ts.snap +421 -0
  254. package/src/renderables/__snapshots__/TextTable.test.ts.snap +215 -0
  255. package/src/renderables/__tests__/LineNumberRenderable.scrollbox-simple.test.ts +144 -0
  256. package/src/renderables/__tests__/LineNumberRenderable.scrollbox.test.ts +816 -0
  257. package/src/renderables/__tests__/LineNumberRenderable.test.ts +1865 -0
  258. package/src/renderables/__tests__/LineNumberRenderable.wrapping.test.ts +85 -0
  259. package/src/renderables/__tests__/Markdown.code-colors.test.ts +242 -0
  260. package/src/renderables/__tests__/Markdown.test.ts +2518 -0
  261. package/src/renderables/__tests__/MultiRenderable.selection.test.ts +87 -0
  262. package/src/renderables/__tests__/Textarea.buffer.test.ts +682 -0
  263. package/src/renderables/__tests__/Textarea.destroyed-events.test.ts +675 -0
  264. package/src/renderables/__tests__/Textarea.editing.test.ts +2041 -0
  265. package/src/renderables/__tests__/Textarea.error-handling.test.ts +35 -0
  266. package/src/renderables/__tests__/Textarea.events.test.ts +738 -0
  267. package/src/renderables/__tests__/Textarea.highlights.test.ts +590 -0
  268. package/src/renderables/__tests__/Textarea.keybinding.test.ts +3149 -0
  269. package/src/renderables/__tests__/Textarea.paste.test.ts +357 -0
  270. package/src/renderables/__tests__/Textarea.rendering.test.ts +1866 -0
  271. package/src/renderables/__tests__/Textarea.scroll.test.ts +733 -0
  272. package/src/renderables/__tests__/Textarea.selection.test.ts +1590 -0
  273. package/src/renderables/__tests__/Textarea.stress.test.ts +670 -0
  274. package/src/renderables/__tests__/Textarea.undo-redo.test.ts +383 -0
  275. package/src/renderables/__tests__/Textarea.visual-lines.test.ts +310 -0
  276. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.code.test.ts.snap +221 -0
  277. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox-simple.test.ts.snap +89 -0
  278. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox.test.ts.snap +457 -0
  279. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.test.ts.snap +158 -0
  280. package/src/renderables/__tests__/__snapshots__/Textarea.rendering.test.ts.snap +387 -0
  281. package/src/renderables/__tests__/markdown-parser.test.ts +217 -0
  282. package/src/renderables/__tests__/renderable-test-utils.ts +60 -0
  283. package/src/renderables/composition/README.md +8 -0
  284. package/src/renderables/composition/VRenderable.ts +32 -0
  285. package/src/renderables/composition/constructs.ts +127 -0
  286. package/src/renderables/composition/vnode.ts +289 -0
  287. package/src/renderables/index.ts +23 -0
  288. package/src/renderables/markdown-parser.ts +66 -0
  289. package/src/renderer.ts +2681 -0
  290. package/src/runtime-plugin-support.ts +39 -0
  291. package/src/runtime-plugin.ts +615 -0
  292. package/src/syntax-style.test.ts +841 -0
  293. package/src/syntax-style.ts +257 -0
  294. package/src/testing/README.md +210 -0
  295. package/src/testing/capture-spans.test.ts +194 -0
  296. package/src/testing/integration.test.ts +276 -0
  297. package/src/testing/manual-clock.ts +117 -0
  298. package/src/testing/mock-keys.test.ts +1378 -0
  299. package/src/testing/mock-keys.ts +457 -0
  300. package/src/testing/mock-mouse.test.ts +218 -0
  301. package/src/testing/mock-mouse.ts +247 -0
  302. package/src/testing/mock-tree-sitter-client.ts +73 -0
  303. package/src/testing/spy.ts +13 -0
  304. package/src/testing/test-recorder.test.ts +415 -0
  305. package/src/testing/test-recorder.ts +145 -0
  306. package/src/testing/test-renderer.ts +132 -0
  307. package/src/testing.ts +7 -0
  308. package/src/tests/__snapshots__/absolute-positioning.snapshot.test.ts.snap +481 -0
  309. package/src/tests/__snapshots__/renderable.snapshot.test.ts.snap +19 -0
  310. package/src/tests/__snapshots__/scrollbox.test.ts.snap +29 -0
  311. package/src/tests/absolute-positioning.snapshot.test.ts +638 -0
  312. package/src/tests/allocator-stats.test.ts +38 -0
  313. package/src/tests/destroy-during-render.test.ts +200 -0
  314. package/src/tests/destroy-on-exit.fixture.ts +36 -0
  315. package/src/tests/destroy-on-exit.test.ts +41 -0
  316. package/src/tests/hover-cursor.test.ts +98 -0
  317. package/src/tests/native-span-feed-async.test.ts +173 -0
  318. package/src/tests/native-span-feed-close.test.ts +120 -0
  319. package/src/tests/native-span-feed-coverage.test.ts +227 -0
  320. package/src/tests/native-span-feed-edge-cases.test.ts +352 -0
  321. package/src/tests/native-span-feed-use-after-free.test.ts +45 -0
  322. package/src/tests/opacity.test.ts +123 -0
  323. package/src/tests/renderable.snapshot.test.ts +524 -0
  324. package/src/tests/renderable.test.ts +1281 -0
  325. package/src/tests/renderer.clock.test.ts +158 -0
  326. package/src/tests/renderer.console-startup.test.ts +185 -0
  327. package/src/tests/renderer.control.test.ts +425 -0
  328. package/src/tests/renderer.core-slot-binding.test.ts +952 -0
  329. package/src/tests/renderer.cursor.test.ts +26 -0
  330. package/src/tests/renderer.destroy-during-render.test.ts +147 -0
  331. package/src/tests/renderer.focus-restore.test.ts +257 -0
  332. package/src/tests/renderer.focus.test.ts +294 -0
  333. package/src/tests/renderer.idle.test.ts +219 -0
  334. package/src/tests/renderer.input.test.ts +2237 -0
  335. package/src/tests/renderer.kitty-flags.test.ts +195 -0
  336. package/src/tests/renderer.mouse.test.ts +1274 -0
  337. package/src/tests/renderer.palette.test.ts +629 -0
  338. package/src/tests/renderer.selection.test.ts +49 -0
  339. package/src/tests/renderer.slot-registry.test.ts +684 -0
  340. package/src/tests/renderer.useMouse.test.ts +47 -0
  341. package/src/tests/runtime-plugin-node-modules-cycle.fixture.ts +76 -0
  342. package/src/tests/runtime-plugin-node-modules-mjs.fixture.ts +43 -0
  343. package/src/tests/runtime-plugin-node-modules-no-bare-rewrite.fixture.ts +67 -0
  344. package/src/tests/runtime-plugin-node-modules-package-type-cache.fixture.ts +72 -0
  345. package/src/tests/runtime-plugin-node-modules-runtime-specifier.fixture.ts +44 -0
  346. package/src/tests/runtime-plugin-node-modules-scoped-package-bare-rewrite.fixture.ts +85 -0
  347. package/src/tests/runtime-plugin-path-alias.fixture.ts +43 -0
  348. package/src/tests/runtime-plugin-resolve-roots.fixture.ts +65 -0
  349. package/src/tests/runtime-plugin-support.fixture.ts +11 -0
  350. package/src/tests/runtime-plugin-support.test.ts +19 -0
  351. package/src/tests/runtime-plugin-windows-file-url.fixture.ts +30 -0
  352. package/src/tests/runtime-plugin.fixture.ts +40 -0
  353. package/src/tests/runtime-plugin.test.ts +354 -0
  354. package/src/tests/scrollbox-culling-bug.test.ts +114 -0
  355. package/src/tests/scrollbox-hitgrid-resize.test.ts +136 -0
  356. package/src/tests/scrollbox-hitgrid.test.ts +909 -0
  357. package/src/tests/scrollbox.test.ts +1530 -0
  358. package/src/tests/wrap-resize-perf.test.ts +276 -0
  359. package/src/tests/yoga-setters.test.ts +921 -0
  360. package/src/text-buffer-view.test.ts +705 -0
  361. package/src/text-buffer-view.ts +189 -0
  362. package/src/text-buffer.test.ts +347 -0
  363. package/src/text-buffer.ts +250 -0
  364. package/src/types.ts +161 -0
  365. package/src/utils.ts +88 -0
  366. package/src/zig/ansi.zig +268 -0
  367. package/src/zig/bench/README.md +50 -0
  368. package/src/zig/bench/buffer-draw-text-buffer_bench.zig +887 -0
  369. package/src/zig/bench/edit-buffer_bench.zig +476 -0
  370. package/src/zig/bench/native-span-feed_bench.zig +100 -0
  371. package/src/zig/bench/rope-markers_bench.zig +713 -0
  372. package/src/zig/bench/rope_bench.zig +514 -0
  373. package/src/zig/bench/styled-text_bench.zig +470 -0
  374. package/src/zig/bench/text-buffer-coords_bench.zig +362 -0
  375. package/src/zig/bench/text-buffer-view_bench.zig +459 -0
  376. package/src/zig/bench/text-chunk-graphemes_bench.zig +273 -0
  377. package/src/zig/bench/utf8_bench.zig +799 -0
  378. package/src/zig/bench-utils.zig +431 -0
  379. package/src/zig/bench.zig +217 -0
  380. package/src/zig/buffer-methods.zig +211 -0
  381. package/src/zig/buffer.zig +2281 -0
  382. package/src/zig/build.zig +289 -0
  383. package/src/zig/build.zig.zon +16 -0
  384. package/src/zig/edit-buffer.zig +825 -0
  385. package/src/zig/editor-view.zig +802 -0
  386. package/src/zig/event-bus.zig +13 -0
  387. package/src/zig/event-emitter.zig +65 -0
  388. package/src/zig/file-logger.zig +92 -0
  389. package/src/zig/grapheme.zig +599 -0
  390. package/src/zig/lib.zig +1854 -0
  391. package/src/zig/link.zig +333 -0
  392. package/src/zig/logger.zig +43 -0
  393. package/src/zig/mem-registry.zig +125 -0
  394. package/src/zig/native-span-feed-bench-lib.zig +7 -0
  395. package/src/zig/native-span-feed.zig +708 -0
  396. package/src/zig/renderer.zig +1393 -0
  397. package/src/zig/rope.zig +1220 -0
  398. package/src/zig/syntax-style.zig +161 -0
  399. package/src/zig/terminal.zig +987 -0
  400. package/src/zig/test.zig +72 -0
  401. package/src/zig/tests/README.md +18 -0
  402. package/src/zig/tests/buffer-methods_test.zig +1109 -0
  403. package/src/zig/tests/buffer_test.zig +2557 -0
  404. package/src/zig/tests/edit-buffer-history_test.zig +271 -0
  405. package/src/zig/tests/edit-buffer_test.zig +1689 -0
  406. package/src/zig/tests/editor-view_test.zig +3299 -0
  407. package/src/zig/tests/event-emitter_test.zig +249 -0
  408. package/src/zig/tests/grapheme_test.zig +1304 -0
  409. package/src/zig/tests/link_test.zig +190 -0
  410. package/src/zig/tests/mem-registry_test.zig +473 -0
  411. package/src/zig/tests/memory_leak_regression_test.zig +159 -0
  412. package/src/zig/tests/native-span-feed_test.zig +1264 -0
  413. package/src/zig/tests/renderer_test.zig +1017 -0
  414. package/src/zig/tests/rope-nested_test.zig +712 -0
  415. package/src/zig/tests/rope_fuzz_test.zig +238 -0
  416. package/src/zig/tests/rope_test.zig +2362 -0
  417. package/src/zig/tests/segment-merge.test.zig +148 -0
  418. package/src/zig/tests/syntax-style_test.zig +557 -0
  419. package/src/zig/tests/terminal_test.zig +754 -0
  420. package/src/zig/tests/text-buffer-drawing_test.zig +3237 -0
  421. package/src/zig/tests/text-buffer-highlights_test.zig +666 -0
  422. package/src/zig/tests/text-buffer-iterators_test.zig +776 -0
  423. package/src/zig/tests/text-buffer-segment_test.zig +320 -0
  424. package/src/zig/tests/text-buffer-selection_test.zig +1035 -0
  425. package/src/zig/tests/text-buffer-selection_viewport_test.zig +358 -0
  426. package/src/zig/tests/text-buffer-view_test.zig +3649 -0
  427. package/src/zig/tests/text-buffer_test.zig +2191 -0
  428. package/src/zig/tests/unicode-width-map.zon +3909 -0
  429. package/src/zig/tests/utf8_no_zwj_test.zig +260 -0
  430. package/src/zig/tests/utf8_test.zig +4057 -0
  431. package/src/zig/tests/utf8_wcwidth_cursor_test.zig +267 -0
  432. package/src/zig/tests/utf8_wcwidth_test.zig +357 -0
  433. package/src/zig/tests/word-wrap-editing_test.zig +498 -0
  434. package/src/zig/tests/wrap-cache-perf_test.zig +113 -0
  435. package/src/zig/text-buffer-iterators.zig +499 -0
  436. package/src/zig/text-buffer-segment.zig +404 -0
  437. package/src/zig/text-buffer-view.zig +1371 -0
  438. package/src/zig/text-buffer.zig +1180 -0
  439. package/src/zig/utf8.zig +1948 -0
  440. package/src/zig/utils.zig +9 -0
  441. package/src/zig-structs.ts +261 -0
  442. package/src/zig.ts +3884 -0
  443. package/tsconfig.build.json +24 -0
  444. package/tsconfig.json +27 -0
  445. package/3d/SpriteResourceManager.d.ts +0 -74
  446. package/3d/SpriteUtils.d.ts +0 -13
  447. package/3d/TextureUtils.d.ts +0 -24
  448. package/3d/ThreeRenderable.d.ts +0 -40
  449. package/3d/WGPURenderer.d.ts +0 -61
  450. package/3d/animation/ExplodingSpriteEffect.d.ts +0 -71
  451. package/3d/animation/PhysicsExplodingSpriteEffect.d.ts +0 -76
  452. package/3d/animation/SpriteAnimator.d.ts +0 -124
  453. package/3d/animation/SpriteParticleGenerator.d.ts +0 -62
  454. package/3d/canvas.d.ts +0 -44
  455. package/3d/index.d.ts +0 -12
  456. package/3d/physics/PlanckPhysicsAdapter.d.ts +0 -19
  457. package/3d/physics/RapierPhysicsAdapter.d.ts +0 -19
  458. package/3d/physics/physics-interface.d.ts +0 -27
  459. package/3d.d.ts +0 -2
  460. package/3d.js +0 -34041
  461. package/3d.js.map +0 -155
  462. package/LICENSE +0 -21
  463. package/NativeSpanFeed.d.ts +0 -41
  464. package/Renderable.d.ts +0 -334
  465. package/animation/Timeline.d.ts +0 -126
  466. package/ansi.d.ts +0 -13
  467. package/buffer.d.ts +0 -111
  468. package/console.d.ts +0 -144
  469. package/edit-buffer.d.ts +0 -98
  470. package/editor-view.d.ts +0 -73
  471. package/index-9vwc3fg6.js +0 -12260
  472. package/index-9vwc3fg6.js.map +0 -42
  473. package/index-dcj62y8t.js +0 -20614
  474. package/index-dcj62y8t.js.map +0 -67
  475. package/index-f7n39gpy.js +0 -411
  476. package/index-f7n39gpy.js.map +0 -10
  477. package/index.d.ts +0 -23
  478. package/index.js +0 -478
  479. package/index.js.map +0 -9
  480. package/lib/KeyHandler.d.ts +0 -61
  481. package/lib/RGBA.d.ts +0 -25
  482. package/lib/ascii.font.d.ts +0 -508
  483. package/lib/border.d.ts +0 -51
  484. package/lib/bunfs.d.ts +0 -7
  485. package/lib/clipboard.d.ts +0 -17
  486. package/lib/clock.d.ts +0 -15
  487. package/lib/data-paths.d.ts +0 -26
  488. package/lib/debounce.d.ts +0 -42
  489. package/lib/detect-links.d.ts +0 -6
  490. package/lib/env.d.ts +0 -42
  491. package/lib/extmarks-history.d.ts +0 -17
  492. package/lib/extmarks.d.ts +0 -89
  493. package/lib/hast-styled-text.d.ts +0 -17
  494. package/lib/index.d.ts +0 -21
  495. package/lib/keymapping.d.ts +0 -25
  496. package/lib/objects-in-viewport.d.ts +0 -24
  497. package/lib/output.capture.d.ts +0 -24
  498. package/lib/parse.keypress-kitty.d.ts +0 -2
  499. package/lib/parse.keypress.d.ts +0 -26
  500. package/lib/parse.mouse.d.ts +0 -30
  501. package/lib/paste.d.ts +0 -7
  502. package/lib/queue.d.ts +0 -15
  503. package/lib/renderable.validations.d.ts +0 -12
  504. package/lib/scroll-acceleration.d.ts +0 -43
  505. package/lib/selection.d.ts +0 -63
  506. package/lib/singleton.d.ts +0 -7
  507. package/lib/stdin-parser.d.ts +0 -87
  508. package/lib/styled-text.d.ts +0 -63
  509. package/lib/terminal-capability-detection.d.ts +0 -30
  510. package/lib/terminal-palette.d.ts +0 -50
  511. package/lib/tree-sitter/assets/update.d.ts +0 -11
  512. package/lib/tree-sitter/client.d.ts +0 -47
  513. package/lib/tree-sitter/default-parsers.d.ts +0 -2
  514. package/lib/tree-sitter/download-utils.d.ts +0 -21
  515. package/lib/tree-sitter/index.d.ts +0 -8
  516. package/lib/tree-sitter/parser.worker.d.ts +0 -1
  517. package/lib/tree-sitter/parsers-config.d.ts +0 -53
  518. package/lib/tree-sitter/resolve-ft.d.ts +0 -5
  519. package/lib/tree-sitter/types.d.ts +0 -82
  520. package/lib/tree-sitter-styled-text.d.ts +0 -14
  521. package/lib/validate-dir-name.d.ts +0 -1
  522. package/lib/yoga.options.d.ts +0 -32
  523. package/parser.worker.js +0 -899
  524. package/parser.worker.js.map +0 -12
  525. package/plugins/core-slot.d.ts +0 -72
  526. package/plugins/registry.d.ts +0 -42
  527. package/plugins/types.d.ts +0 -34
  528. package/post/effects.d.ts +0 -147
  529. package/post/filters.d.ts +0 -65
  530. package/post/matrices.d.ts +0 -20
  531. package/renderables/ASCIIFont.d.ts +0 -52
  532. package/renderables/Box.d.ts +0 -81
  533. package/renderables/Code.d.ts +0 -78
  534. package/renderables/Diff.d.ts +0 -142
  535. package/renderables/EditBufferRenderable.d.ts +0 -237
  536. package/renderables/FrameBuffer.d.ts +0 -16
  537. package/renderables/Input.d.ts +0 -67
  538. package/renderables/LineNumberRenderable.d.ts +0 -78
  539. package/renderables/Markdown.d.ts +0 -185
  540. package/renderables/ScrollBar.d.ts +0 -77
  541. package/renderables/ScrollBox.d.ts +0 -124
  542. package/renderables/Select.d.ts +0 -115
  543. package/renderables/Slider.d.ts +0 -47
  544. package/renderables/TabSelect.d.ts +0 -96
  545. package/renderables/Text.d.ts +0 -36
  546. package/renderables/TextBufferRenderable.d.ts +0 -105
  547. package/renderables/TextNode.d.ts +0 -91
  548. package/renderables/TextTable.d.ts +0 -140
  549. package/renderables/Textarea.d.ts +0 -63
  550. package/renderables/TimeToFirstDraw.d.ts +0 -24
  551. package/renderables/__tests__/renderable-test-utils.d.ts +0 -12
  552. package/renderables/composition/VRenderable.d.ts +0 -16
  553. package/renderables/composition/constructs.d.ts +0 -35
  554. package/renderables/composition/vnode.d.ts +0 -46
  555. package/renderables/index.d.ts +0 -23
  556. package/renderables/markdown-parser.d.ts +0 -10
  557. package/renderer.d.ts +0 -419
  558. package/runtime-plugin-support.d.ts +0 -3
  559. package/runtime-plugin-support.js +0 -29
  560. package/runtime-plugin-support.js.map +0 -10
  561. package/runtime-plugin.d.ts +0 -16
  562. package/runtime-plugin.js +0 -16
  563. package/runtime-plugin.js.map +0 -9
  564. package/syntax-style.d.ts +0 -54
  565. package/testing/manual-clock.d.ts +0 -17
  566. package/testing/mock-keys.d.ts +0 -81
  567. package/testing/mock-mouse.d.ts +0 -38
  568. package/testing/mock-tree-sitter-client.d.ts +0 -23
  569. package/testing/spy.d.ts +0 -7
  570. package/testing/test-recorder.d.ts +0 -61
  571. package/testing/test-renderer.d.ts +0 -23
  572. package/testing.d.ts +0 -6
  573. package/testing.js +0 -697
  574. package/testing.js.map +0 -15
  575. package/text-buffer-view.d.ts +0 -42
  576. package/text-buffer.d.ts +0 -67
  577. package/types.d.ts +0 -139
  578. package/utils.d.ts +0 -14
  579. package/zig-structs.d.ts +0 -155
  580. package/zig.d.ts +0 -353
  581. /package/{assets → src/lib/tree-sitter/assets}/javascript/highlights.scm +0 -0
  582. /package/{assets → src/lib/tree-sitter/assets}/javascript/tree-sitter-javascript.wasm +0 -0
  583. /package/{assets → src/lib/tree-sitter/assets}/markdown/highlights.scm +0 -0
  584. /package/{assets → src/lib/tree-sitter/assets}/markdown/injections.scm +0 -0
  585. /package/{assets → src/lib/tree-sitter/assets}/markdown/tree-sitter-markdown.wasm +0 -0
  586. /package/{assets → src/lib/tree-sitter/assets}/markdown_inline/highlights.scm +0 -0
  587. /package/{assets → src/lib/tree-sitter/assets}/markdown_inline/tree-sitter-markdown_inline.wasm +0 -0
  588. /package/{assets → src/lib/tree-sitter/assets}/typescript/highlights.scm +0 -0
  589. /package/{assets → src/lib/tree-sitter/assets}/typescript/tree-sitter-typescript.wasm +0 -0
  590. /package/{assets → src/lib/tree-sitter/assets}/zig/highlights.scm +0 -0
  591. /package/{assets → src/lib/tree-sitter/assets}/zig/tree-sitter-zig.wasm +0 -0
@@ -0,0 +1,1264 @@
1
+ const std = @import("std");
2
+ const testing = std.testing;
3
+ const raw = @import("../native-span-feed.zig");
4
+
5
+ fn testOptions(chunk_size: u32, initial_chunks: u32, auto_commit: bool) raw.Options {
6
+ return testOptionsFull(chunk_size, initial_chunks, 0, auto_commit);
7
+ }
8
+
9
+ fn testOptionsFull(chunk_size: u32, initial_chunks: u32, max_bytes: u64, auto_commit: bool) raw.Options {
10
+ return .{
11
+ .chunk_size = chunk_size,
12
+ .initial_chunks = initial_chunks,
13
+ .max_bytes = max_bytes,
14
+ .growth_policy = @intFromEnum(raw.GrowthPolicy.grow),
15
+ .auto_commit_on_full = if (auto_commit) 1 else 0,
16
+ .span_queue_capacity = 0,
17
+ };
18
+ }
19
+
20
+ fn drainAllSpans(stream: *raw.Stream) u64 {
21
+ var buf: [256]raw.SpanInfo = undefined;
22
+ var total: u64 = 0;
23
+ while (true) {
24
+ const count = stream.drainSpans(&buf);
25
+ if (count == 0) break;
26
+ var i: u32 = 0;
27
+ while (i < count) : (i += 1) {
28
+ total += buf[i].len;
29
+ stream.markSpanConsumed(buf[i]);
30
+ }
31
+ }
32
+ return total;
33
+ }
34
+
35
+ test "Stream - create and destroy with testing allocator" {
36
+ const stream = try raw.Stream.create(testing.allocator, testOptions(1024, 2, true));
37
+ defer stream.destroy();
38
+
39
+ const stats = stream.getStats();
40
+ try testing.expectEqual(@as(u32, 2), stats.chunks);
41
+ try testing.expectEqual(@as(u64, 0), stats.bytes_written);
42
+ try testing.expectEqual(@as(u64, 0), stats.spans_committed);
43
+ }
44
+
45
+ test "Stream - create with default options" {
46
+ const stream = try raw.Stream.create(testing.allocator, null);
47
+ defer stream.destroy();
48
+
49
+ const stats = stream.getStats();
50
+ try testing.expect(stats.chunks >= 1);
51
+ }
52
+
53
+ test "Stream - write and commit produces span with correct byte count" {
54
+ const stream = try raw.Stream.create(testing.allocator, testOptions(1024, 2, false));
55
+ defer stream.destroy();
56
+
57
+ const data = "hello world";
58
+ try stream.write(data);
59
+ try stream.commit();
60
+
61
+ const stats = stream.getStats();
62
+ try testing.expectEqual(@as(u64, data.len), stats.bytes_written);
63
+ try testing.expectEqual(@as(u64, 1), stats.spans_committed);
64
+
65
+ const drained = drainAllSpans(stream);
66
+ try testing.expectEqual(@as(u64, data.len), drained);
67
+ }
68
+
69
+ test "Stream - write with auto_commit fills chunk and commits automatically" {
70
+ const chunk_size: u32 = 64;
71
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 2, true));
72
+ defer stream.destroy();
73
+
74
+ const data = [_]u8{'A'} ** 64;
75
+ try stream.write(&data);
76
+
77
+ const stats = stream.getStats();
78
+ try testing.expectEqual(@as(u64, 64), stats.bytes_written);
79
+ try testing.expectEqual(@as(u64, 1), stats.spans_committed);
80
+ }
81
+
82
+ test "Stream - write spanning multiple chunks with auto_commit" {
83
+ const chunk_size: u32 = 64;
84
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 2, true));
85
+ defer stream.destroy();
86
+
87
+ const data = [_]u8{'B'} ** 150;
88
+ try stream.write(&data);
89
+
90
+ const stats = stream.getStats();
91
+ try testing.expectEqual(@as(u64, 150), stats.bytes_written);
92
+ try testing.expectEqual(@as(u64, 2), stats.spans_committed);
93
+
94
+ try stream.commit();
95
+ const stats2 = stream.getStats();
96
+ try testing.expectEqual(@as(u64, 3), stats2.spans_committed);
97
+
98
+ const drained = drainAllSpans(stream);
99
+ try testing.expectEqual(@as(u64, 150), drained);
100
+ }
101
+
102
+ test "Stream - write returns NoSpace when auto_commit disabled and data exceeds chunk" {
103
+ const chunk_size: u32 = 64;
104
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 2, false));
105
+ defer stream.destroy();
106
+
107
+ const data = [_]u8{'C'} ** 65;
108
+ const result = stream.write(&data);
109
+ try testing.expectError(raw.StreamError.NoSpace, result);
110
+ }
111
+
112
+ test "Stream - write exactly fills chunk without auto_commit succeeds" {
113
+ const chunk_size: u32 = 64;
114
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 2, false));
115
+ defer stream.destroy();
116
+
117
+ const exact = [_]u8{'A'} ** 64;
118
+ try stream.write(&exact);
119
+
120
+ const stats = stream.getStats();
121
+ try testing.expectEqual(@as(u64, 64), stats.bytes_written);
122
+
123
+ try stream.commit();
124
+ _ = drainAllSpans(stream);
125
+
126
+ try stream.write("B");
127
+ try stream.commit();
128
+
129
+ const stats2 = stream.getStats();
130
+ try testing.expectEqual(@as(u64, 65), stats2.bytes_written);
131
+ try testing.expectEqual(@as(u64, 2), stats2.spans_committed);
132
+ }
133
+
134
+ test "Stream - written data matches drained span content" {
135
+ const chunk_size: u32 = 256;
136
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 2, false));
137
+ defer stream.destroy();
138
+
139
+ const data = "the quick brown fox jumps over the lazy dog";
140
+ try stream.write(data);
141
+ try stream.commit();
142
+
143
+ var buf: [16]raw.SpanInfo = undefined;
144
+ const count = stream.drainSpans(&buf);
145
+ try testing.expectEqual(@as(u32, 1), count);
146
+
147
+ const span = buf[0];
148
+ const slice = span.slice();
149
+ try testing.expectEqualStrings(data, slice);
150
+ stream.markSpanConsumed(buf[0]);
151
+ }
152
+
153
+ test "Stream - reserve and commitReserved round-trip" {
154
+ const chunk_size: u32 = 256;
155
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 2, false));
156
+ defer stream.destroy();
157
+
158
+ const info = try stream.reserve(10);
159
+ try testing.expect(info.len >= 10);
160
+
161
+ const dest = info.slice();
162
+ @memcpy(dest[0..5], "hello");
163
+
164
+ try stream.commitReserved(5);
165
+
166
+ const stats = stream.getStats();
167
+ try testing.expectEqual(@as(u64, 5), stats.bytes_written);
168
+ try testing.expectEqual(@as(u64, 1), stats.spans_committed);
169
+
170
+ const drained = drainAllSpans(stream);
171
+ try testing.expectEqual(@as(u64, 5), drained);
172
+ }
173
+
174
+ test "Stream - reserve returns Busy if already reserved" {
175
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 2, false));
176
+ defer stream.destroy();
177
+
178
+ _ = try stream.reserve(1);
179
+ const result = stream.reserve(1);
180
+ try testing.expectError(raw.StreamError.Busy, result);
181
+
182
+ try stream.commitReserved(0);
183
+ }
184
+
185
+ test "Stream - reserve returns Busy if pending data exists" {
186
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 2, false));
187
+ defer stream.destroy();
188
+
189
+ try stream.write("some data");
190
+ const result = stream.reserve(1);
191
+ try testing.expectError(raw.StreamError.Busy, result);
192
+ }
193
+
194
+ test "Stream - write returns Busy while reservation is active" {
195
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 2, false));
196
+ defer stream.destroy();
197
+
198
+ _ = try stream.reserve(1);
199
+ const result = stream.write("data");
200
+ try testing.expectError(raw.StreamError.Busy, result);
201
+
202
+ try stream.commitReserved(0);
203
+ }
204
+
205
+ test "Stream - write to closed stream returns Invalid" {
206
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 2, false));
207
+ defer stream.destroy();
208
+
209
+ try stream.close();
210
+ const result = stream.write("data");
211
+ try testing.expectError(raw.StreamError.Invalid, result);
212
+ }
213
+
214
+ test "Stream - double close does not error" {
215
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 2, false));
216
+ defer stream.destroy();
217
+
218
+ try stream.close();
219
+ try stream.close();
220
+ }
221
+
222
+ test "Stream - consecutive writes without auto_commit preserves all data" {
223
+ // Regression: auto_commit off must not drop pending data across writes.
224
+
225
+ const chunk_size: u32 = 64;
226
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 2, false));
227
+ defer stream.destroy();
228
+
229
+ const first = [_]u8{'A'} ** 64;
230
+ try stream.write(&first);
231
+
232
+ var stats = stream.getStats();
233
+ try testing.expectEqual(@as(u64, 64), stats.bytes_written);
234
+
235
+ const second = "BBBB";
236
+ try stream.write(second);
237
+
238
+ stats = stream.getStats();
239
+ try testing.expectEqual(@as(u64, 68), stats.bytes_written);
240
+ try testing.expectEqual(@as(u64, 1), stats.spans_committed);
241
+ try stream.commit();
242
+ stats = stream.getStats();
243
+ try testing.expectEqual(@as(u64, 2), stats.spans_committed);
244
+
245
+ const drained = drainAllSpans(stream);
246
+ try testing.expectEqual(@as(u64, 68), drained);
247
+ }
248
+
249
+ test "Stream - write that exactly fills chunk then write more (no auto_commit)" {
250
+ const chunk_size: u32 = 32;
251
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 2, false));
252
+ defer stream.destroy();
253
+
254
+ const fill = [_]u8{'X'} ** 32;
255
+ try stream.write(&fill);
256
+
257
+ try stream.write("Y");
258
+ try stream.commit();
259
+
260
+ const stats = stream.getStats();
261
+ try testing.expectEqual(@as(u64, 33), stats.bytes_written);
262
+ try testing.expectEqual(@as(u64, 2), stats.spans_committed);
263
+ var buf: [16]raw.SpanInfo = undefined;
264
+ const count = stream.drainSpans(&buf);
265
+ try testing.expectEqual(@as(u32, 2), count);
266
+
267
+ const span1 = buf[0].slice();
268
+ try testing.expectEqual(@as(usize, 32), span1.len);
269
+ try testing.expectEqual(@as(u8, 'X'), span1[0]);
270
+ try testing.expectEqual(@as(u8, 'X'), span1[31]);
271
+
272
+ const span2 = buf[1].slice();
273
+ try testing.expectEqualStrings("Y", span2);
274
+
275
+ stream.markSpanConsumed(buf[0]);
276
+ stream.markSpanConsumed(buf[1]);
277
+ }
278
+
279
+ test "Stream - multiple chunk transitions without auto_commit" {
280
+ const chunk_size: u32 = 16;
281
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, false));
282
+ defer stream.destroy();
283
+
284
+ try stream.write("AAAAAAAAAAAAAAAA");
285
+ try stream.write("BBBBBBBBBBBBBBBB");
286
+ try stream.write("CCCCCCCC");
287
+
288
+ var stats = stream.getStats();
289
+ try testing.expectEqual(@as(u64, 40), stats.bytes_written);
290
+ try testing.expectEqual(@as(u64, 2), stats.spans_committed);
291
+ try stream.commit();
292
+ stats = stream.getStats();
293
+ try testing.expectEqual(@as(u64, 3), stats.spans_committed);
294
+
295
+ const drained = drainAllSpans(stream);
296
+ try testing.expectEqual(@as(u64, 40), drained);
297
+ }
298
+
299
+ test "Stream - commit after small write should allow reuse of remaining chunk space" {
300
+ // Regression: commit must not burn remaining chunk space.
301
+
302
+ const chunk_size: u32 = 256;
303
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, false));
304
+ defer stream.destroy();
305
+
306
+ try stream.write("0123456789");
307
+ try stream.commit();
308
+
309
+ var stats = stream.getStats();
310
+ try testing.expectEqual(@as(u64, 10), stats.bytes_written);
311
+ try testing.expectEqual(@as(u64, 1), stats.spans_committed);
312
+ try testing.expectEqual(@as(u32, 1), stats.chunks);
313
+
314
+ var buf: [16]raw.SpanInfo = undefined;
315
+ const count = stream.drainSpans(&buf);
316
+ try testing.expectEqual(@as(u32, 1), count);
317
+ stream.markSpanConsumed(buf[0]);
318
+
319
+ try stream.write("abcdefghij");
320
+ try stream.commit();
321
+
322
+ stats = stream.getStats();
323
+ try testing.expectEqual(@as(u64, 20), stats.bytes_written);
324
+ try testing.expectEqual(@as(u64, 2), stats.spans_committed);
325
+ try testing.expectEqual(@as(u32, 1), stats.chunks);
326
+
327
+ const count2 = stream.drainSpans(&buf);
328
+ try testing.expectEqual(@as(u32, 1), count2);
329
+ const span = buf[0];
330
+ try testing.expectEqual(@as(u32, 10), span.offset);
331
+ try testing.expectEqual(@as(u32, 10), span.len);
332
+ stream.markSpanConsumed(buf[0]);
333
+ }
334
+
335
+ test "Stream - repeated small write+commit should not force chunk growth" {
336
+ // Regression: small write+commit should not force chunk growth.
337
+
338
+ const chunk_size: u32 = 1024;
339
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, false));
340
+ defer stream.destroy();
341
+
342
+ var i: u32 = 0;
343
+ while (i < 4) : (i += 1) {
344
+ try stream.write("12345678");
345
+ try stream.commit();
346
+ }
347
+
348
+ const stats = stream.getStats();
349
+ try testing.expectEqual(@as(u64, 32), stats.bytes_written);
350
+ try testing.expectEqual(@as(u64, 4), stats.spans_committed);
351
+
352
+ try testing.expectEqual(@as(u32, 1), stats.chunks);
353
+
354
+ const drained = drainAllSpans(stream);
355
+ try testing.expectEqual(@as(u64, 32), drained);
356
+ }
357
+
358
+ test "Stream - max_bytes returns MaxBytes when limit is reached" {
359
+ const stream = try raw.Stream.create(testing.allocator, testOptionsFull(32, 2, 64, false));
360
+ defer stream.destroy();
361
+
362
+ try testing.expectEqual(@as(u32, 2), stream.getStats().chunks);
363
+
364
+ const fill1 = [_]u8{'A'} ** 32;
365
+ try stream.write(&fill1);
366
+ try stream.commit();
367
+
368
+ const fill2 = [_]u8{'B'} ** 32;
369
+ try stream.write(&fill2);
370
+ try stream.commit();
371
+
372
+ const result = stream.write("C");
373
+ try testing.expectError(raw.StreamError.MaxBytes, result);
374
+ }
375
+
376
+ test "Stream - max_bytes allows reuse after draining" {
377
+ const stream = try raw.Stream.create(testing.allocator, testOptionsFull(32, 2, 64, false));
378
+ defer stream.destroy();
379
+
380
+ const fill1 = [_]u8{'A'} ** 32;
381
+ try stream.write(&fill1);
382
+ try stream.commit();
383
+ const fill2 = [_]u8{'B'} ** 32;
384
+ try stream.write(&fill2);
385
+ try stream.commit();
386
+
387
+ _ = drainAllSpans(stream);
388
+ const fill3 = [_]u8{'C'} ** 32;
389
+ try stream.write(&fill3);
390
+ try stream.commit();
391
+
392
+ try testing.expectEqual(@as(u64, 96), stream.getStats().bytes_written);
393
+ try testing.expectEqual(@as(u32, 2), stream.getStats().chunks);
394
+ }
395
+
396
+ test "Stream - auto_commit with max_bytes works when consumer keeps up" {
397
+ const stream = try raw.Stream.create(testing.allocator, testOptionsFull(32, 2, 64, true));
398
+ defer stream.destroy();
399
+
400
+ const fill1 = [_]u8{'A'} ** 32;
401
+ try stream.write(&fill1);
402
+
403
+ _ = drainAllSpans(stream);
404
+
405
+ const fill2 = [_]u8{'B'} ** 32;
406
+ try stream.write(&fill2);
407
+
408
+ _ = drainAllSpans(stream);
409
+
410
+ const fill3 = [_]u8{'C'} ** 32;
411
+ try stream.write(&fill3);
412
+
413
+ try testing.expectEqual(@as(u64, 96), stream.getStats().bytes_written);
414
+ try testing.expectEqual(@as(u32, 2), stream.getStats().chunks);
415
+
416
+ _ = drainAllSpans(stream);
417
+ }
418
+
419
+ test "Stream - auto_commit with max_bytes should handle write spanning chunk boundary" {
420
+ // Regression: auto_commit must not fail when continuing across a boundary.
421
+
422
+ const stream = try raw.Stream.create(testing.allocator, testOptionsFull(32, 2, 64, true));
423
+ defer stream.destroy();
424
+
425
+ const data = [_]u8{'X'} ** 64;
426
+ try stream.write(&data);
427
+
428
+ try testing.expectEqual(@as(u64, 64), stream.getStats().bytes_written);
429
+ try testing.expect(stream.getStats().spans_committed >= 1);
430
+
431
+ _ = drainAllSpans(stream);
432
+ }
433
+
434
+ test "Stream - memory growth under pressure allocates new chunks" {
435
+ const stream = try raw.Stream.create(testing.allocator, testOptions(64, 1, true));
436
+ defer stream.destroy();
437
+
438
+ try testing.expectEqual(@as(u32, 1), stream.getStats().chunks);
439
+
440
+ var i: usize = 0;
441
+ while (i < 10) : (i += 1) {
442
+ const data = [_]u8{@intCast(i)} ** 64;
443
+ try stream.write(&data);
444
+ }
445
+
446
+ const stats = stream.getStats();
447
+ try testing.expectEqual(@as(u64, 640), stats.bytes_written);
448
+ try testing.expect(stats.chunks >= 10);
449
+
450
+ const drained = drainAllSpans(stream);
451
+ try testing.expectEqual(@as(u64, 640), drained);
452
+ }
453
+
454
+ fn blockOptions(chunk_size: u32, initial_chunks: u32, auto_commit: bool) raw.Options {
455
+ return .{
456
+ .chunk_size = chunk_size,
457
+ .initial_chunks = initial_chunks,
458
+ .max_bytes = 0,
459
+ .growth_policy = @intFromEnum(raw.GrowthPolicy.block),
460
+ .auto_commit_on_full = if (auto_commit) 1 else 0,
461
+ .span_queue_capacity = 0,
462
+ };
463
+ }
464
+
465
+ test "Stream - growth_policy=block prevents new chunk allocation" {
466
+ const chunk_size: u32 = 64;
467
+ const stream = try raw.Stream.create(testing.allocator, blockOptions(chunk_size, 2, false));
468
+ defer stream.destroy();
469
+
470
+ try testing.expectEqual(@as(u32, 2), stream.getStats().chunks);
471
+
472
+ const fill1 = [_]u8{'A'} ** 64;
473
+ try stream.write(&fill1);
474
+ try stream.commit();
475
+
476
+ const fill2 = [_]u8{'B'} ** 64;
477
+ try stream.write(&fill2);
478
+ try stream.commit();
479
+
480
+ const result = stream.write("C");
481
+ try testing.expectError(raw.StreamError.NoSpace, result);
482
+
483
+ try testing.expectEqual(@as(u32, 2), stream.getStats().chunks);
484
+ }
485
+
486
+ test "Stream - growth_policy=block allows reuse after draining" {
487
+ const chunk_size: u32 = 64;
488
+ const stream = try raw.Stream.create(testing.allocator, blockOptions(chunk_size, 2, false));
489
+ defer stream.destroy();
490
+
491
+ try stream.write(&([_]u8{'A'} ** 64));
492
+ try stream.commit();
493
+ try stream.write(&([_]u8{'B'} ** 64));
494
+ try stream.commit();
495
+
496
+ _ = drainAllSpans(stream);
497
+ try stream.write(&([_]u8{'C'} ** 64));
498
+ try stream.commit();
499
+
500
+ try testing.expectEqual(@as(u64, 192), stream.getStats().bytes_written);
501
+ try testing.expectEqual(@as(u32, 2), stream.getStats().chunks);
502
+ }
503
+
504
+ test "Stream - growth_policy=block with auto_commit returns NoSpace when pool exhausted" {
505
+ const chunk_size: u32 = 32;
506
+ const stream = try raw.Stream.create(testing.allocator, blockOptions(chunk_size, 2, true));
507
+ defer stream.destroy();
508
+
509
+ try stream.write(&([_]u8{'X'} ** 64));
510
+
511
+ const result = stream.write("Y");
512
+ try testing.expectError(raw.StreamError.NoSpace, result);
513
+
514
+ try testing.expectEqual(@as(u32, 2), stream.getStats().chunks);
515
+ }
516
+
517
+ test "Stream - span ring overflow returns NoSpace" {
518
+ const chunk_size: u32 = 4096;
519
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, false));
520
+ defer stream.destroy();
521
+
522
+ var i: u32 = 0;
523
+ while (i < 4096) : (i += 1) {
524
+ try stream.write("x");
525
+ try stream.commit();
526
+ }
527
+
528
+ try testing.expectEqual(@as(u32, 4096), stream.getStats().pending_spans);
529
+
530
+ try stream.write("y");
531
+ const result = stream.commit();
532
+ try testing.expectError(raw.StreamError.NoSpace, result);
533
+ }
534
+
535
+ test "Stream - span ring recovers after draining" {
536
+ const chunk_size: u32 = 4096;
537
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, false));
538
+ defer stream.destroy();
539
+
540
+ var i: u32 = 0;
541
+ while (i < 4096) : (i += 1) {
542
+ try stream.write("x");
543
+ try stream.commit();
544
+ }
545
+
546
+ _ = drainAllSpans(stream);
547
+ try testing.expectEqual(@as(u32, 0), stream.getStats().pending_spans);
548
+
549
+ try stream.write("z");
550
+ try stream.commit();
551
+ try testing.expectEqual(@as(u32, 1), stream.getStats().pending_spans);
552
+
553
+ _ = drainAllSpans(stream);
554
+ }
555
+
556
+ test "Stream - custom span_queue_capacity is respected" {
557
+ var opts = testOptions(4096, 1, false);
558
+ opts.span_queue_capacity = 8;
559
+ const stream = try raw.Stream.create(testing.allocator, opts);
560
+ defer stream.destroy();
561
+
562
+ var i: u32 = 0;
563
+ while (i < 8) : (i += 1) {
564
+ try stream.write("x");
565
+ try stream.commit();
566
+ }
567
+ try testing.expectEqual(@as(u32, 8), stream.getStats().pending_spans);
568
+
569
+ try stream.write("y");
570
+ const result = stream.commit();
571
+ try testing.expectError(raw.StreamError.NoSpace, result);
572
+
573
+ _ = drainAllSpans(stream);
574
+ try testing.expectEqual(@as(u32, 0), stream.getStats().pending_spans);
575
+
576
+ try stream.write("z");
577
+ try stream.commit();
578
+ try testing.expectEqual(@as(u32, 1), stream.getStats().pending_spans);
579
+ }
580
+
581
+ test "Stream - large span_queue_capacity works" {
582
+ var opts = testOptions(4096, 1, false);
583
+ opts.span_queue_capacity = 8192;
584
+ const stream = try raw.Stream.create(testing.allocator, opts);
585
+ defer stream.destroy();
586
+
587
+ var i: u32 = 0;
588
+ while (i < 5000) : (i += 1) {
589
+ try stream.write("x");
590
+ try stream.commit();
591
+ }
592
+ try testing.expectEqual(@as(u32, 5000), stream.getStats().pending_spans);
593
+
594
+ _ = drainAllSpans(stream);
595
+ try testing.expectEqual(@as(u32, 0), stream.getStats().pending_spans);
596
+ }
597
+
598
+ test "Stream - span_queue_capacity zero defaults to 4096" {
599
+ var opts = testOptions(4096, 1, false);
600
+ opts.span_queue_capacity = 0;
601
+ const stream = try raw.Stream.create(testing.allocator, opts);
602
+ defer stream.destroy();
603
+
604
+ var i: u32 = 0;
605
+ while (i < 4096) : (i += 1) {
606
+ try stream.write("x");
607
+ try stream.commit();
608
+ }
609
+ try testing.expectEqual(@as(u32, 4096), stream.getStats().pending_spans);
610
+
611
+ try stream.write("y");
612
+ const result = stream.commit();
613
+ try testing.expectError(raw.StreamError.NoSpace, result);
614
+ }
615
+
616
+ test "Stream - data integrity across many chunks with auto_commit" {
617
+ const chunk_size: u32 = 64;
618
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, true));
619
+ defer stream.destroy();
620
+
621
+ var source: [1024]u8 = undefined;
622
+ for (&source, 0..) |*b, idx| {
623
+ b.* = @intCast(idx % 256);
624
+ }
625
+
626
+ try stream.write(&source);
627
+ try stream.commit();
628
+ var received: [1024]u8 = undefined;
629
+ var offset: usize = 0;
630
+
631
+ var buf: [256]raw.SpanInfo = undefined;
632
+ while (true) {
633
+ const count = stream.drainSpans(&buf);
634
+ if (count == 0) break;
635
+ var i: u32 = 0;
636
+ while (i < count) : (i += 1) {
637
+ const span = buf[i];
638
+ const slice = span.slice();
639
+ @memcpy(received[offset .. offset + slice.len], slice);
640
+ offset += slice.len;
641
+ stream.markSpanConsumed(buf[i]);
642
+ }
643
+ }
644
+
645
+ try testing.expectEqual(@as(usize, 1024), offset);
646
+ try testing.expectEqualSlices(u8, &source, &received);
647
+ }
648
+
649
+ test "Stream - data integrity with reserve across multiple chunks" {
650
+ const chunk_size: u32 = 64;
651
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, false));
652
+ defer stream.destroy();
653
+
654
+ var written: [256]u8 = undefined;
655
+ var w_offset: usize = 0;
656
+
657
+ while (w_offset < 256) {
658
+ const info = try stream.reserve(1);
659
+ const dest = info.slice();
660
+ const to_write = @min(dest.len, 256 - w_offset);
661
+ var j: usize = 0;
662
+ while (j < to_write) : (j += 1) {
663
+ const val: u8 = @intCast((w_offset + j) % 256);
664
+ dest[j] = val;
665
+ written[w_offset + j] = val;
666
+ }
667
+ try stream.commitReserved(@intCast(to_write));
668
+ w_offset += to_write;
669
+ }
670
+
671
+ var received: [256]u8 = undefined;
672
+ var r_offset: usize = 0;
673
+
674
+ var buf: [64]raw.SpanInfo = undefined;
675
+ while (true) {
676
+ const count = stream.drainSpans(&buf);
677
+ if (count == 0) break;
678
+ var i: u32 = 0;
679
+ while (i < count) : (i += 1) {
680
+ const slice = buf[i].slice();
681
+ @memcpy(received[r_offset .. r_offset + slice.len], slice);
682
+ r_offset += slice.len;
683
+ stream.markSpanConsumed(buf[i]);
684
+ }
685
+ }
686
+
687
+ try testing.expectEqual(@as(usize, 256), r_offset);
688
+ try testing.expectEqualSlices(u8, &written, &received);
689
+ }
690
+
691
+ test "Stream - reserve on closed stream returns Invalid" {
692
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
693
+ defer stream.destroy();
694
+
695
+ try stream.close();
696
+ const result = stream.reserve(1);
697
+ try testing.expectError(raw.StreamError.Invalid, result);
698
+ }
699
+
700
+ test "Stream - commit on closed stream returns Invalid" {
701
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
702
+ defer stream.destroy();
703
+
704
+ try stream.close();
705
+ const result = stream.commit();
706
+ try testing.expectError(raw.StreamError.Invalid, result);
707
+ }
708
+
709
+ test "Stream - commitReserved on closed stream returns Invalid" {
710
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
711
+ defer stream.destroy();
712
+
713
+ try stream.close();
714
+ const result = stream.commitReserved(0);
715
+ try testing.expectError(raw.StreamError.Invalid, result);
716
+ }
717
+
718
+ test "Stream - commitReserved with len exceeding reserved returns NoSpace" {
719
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
720
+ defer stream.destroy();
721
+
722
+ const info = try stream.reserve(1);
723
+ const result = stream.commitReserved(info.len + 1);
724
+ try testing.expectError(raw.StreamError.NoSpace, result);
725
+ }
726
+
727
+ test "Stream - commitReserved without active reservation returns Invalid" {
728
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
729
+ defer stream.destroy();
730
+
731
+ const result = stream.commitReserved(0);
732
+ try testing.expectError(raw.StreamError.Invalid, result);
733
+ }
734
+
735
+ test "Stream - reserve with min_len larger than chunk returns NoSpace" {
736
+ const stream = try raw.Stream.create(testing.allocator, testOptions(64, 1, false));
737
+ defer stream.destroy();
738
+
739
+ const result = stream.reserve(65);
740
+ try testing.expectError(raw.StreamError.NoSpace, result);
741
+ }
742
+
743
+ test "Stream - empty write is a no-op" {
744
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
745
+ defer stream.destroy();
746
+
747
+ try stream.write("");
748
+ try testing.expectEqual(@as(u64, 0), stream.getStats().bytes_written);
749
+ }
750
+
751
+ test "Stream - commit with no pending data is a no-op" {
752
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
753
+ defer stream.destroy();
754
+
755
+ try stream.commit();
756
+ try testing.expectEqual(@as(u64, 0), stream.getStats().spans_committed);
757
+ }
758
+
759
+ test "Stream - drain with no spans returns zero" {
760
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
761
+ defer stream.destroy();
762
+
763
+ var buf: [16]raw.SpanInfo = undefined;
764
+ const count = stream.drainSpans(&buf);
765
+ try testing.expectEqual(@as(u32, 0), count);
766
+ }
767
+
768
+ test "Stream - close with pending data auto-commits" {
769
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
770
+ defer stream.destroy();
771
+
772
+ try stream.write("pending data");
773
+ try stream.close();
774
+
775
+ try testing.expectEqual(@as(u64, 1), stream.getStats().spans_committed);
776
+
777
+ const drained = drainAllSpans(stream);
778
+ try testing.expectEqual(@as(u64, 12), drained);
779
+ }
780
+
781
+ test "Stream - setOptions on closed stream returns Invalid" {
782
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
783
+ defer stream.destroy();
784
+
785
+ try stream.close();
786
+ const result = stream.setOptions(testOptions(128, 1, true));
787
+ try testing.expectError(raw.StreamError.Invalid, result);
788
+ }
789
+
790
+ test "Stream - setOptions ignores chunk_size (immutable after creation)" {
791
+ const stream = try raw.Stream.create(testing.allocator, testOptions(64, 1, true));
792
+ defer stream.destroy();
793
+
794
+ const fill1 = [_]u8{'A'} ** 64;
795
+ try stream.write(&fill1);
796
+
797
+ try stream.setOptions(testOptions(128, 1, true));
798
+
799
+ const fill2 = [_]u8{'B'} ** 64;
800
+ try stream.write(&fill2);
801
+
802
+ try stream.commit();
803
+
804
+ const stats = stream.getStats();
805
+ try testing.expectEqual(@as(u64, 128), stats.bytes_written);
806
+
807
+ const drained = drainAllSpans(stream);
808
+ try testing.expectEqual(@as(u64, 128), drained);
809
+ }
810
+
811
+ test "Stream - setOptions enables auto_commit mid-stream" {
812
+ const stream = try raw.Stream.create(testing.allocator, testOptions(64, 2, false));
813
+ defer stream.destroy();
814
+
815
+ try stream.write(&([_]u8{'A'} ** 32));
816
+ try testing.expectEqual(@as(u64, 0), stream.getStats().spans_committed);
817
+ try stream.commit();
818
+ try testing.expectEqual(@as(u64, 1), stream.getStats().spans_committed);
819
+
820
+ _ = drainAllSpans(stream);
821
+ try stream.setOptions(testOptions(64, 2, true));
822
+ try stream.write(&([_]u8{'B'} ** 64));
823
+ try testing.expectEqual(@as(u64, 2), stream.getStats().spans_committed);
824
+
825
+ _ = drainAllSpans(stream);
826
+ }
827
+
828
+ test "Stream - pending data survives failed commit (ring full)" {
829
+ const chunk_size: u32 = 4096;
830
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, false));
831
+ defer stream.destroy();
832
+
833
+ var i: u32 = 0;
834
+ while (i < 4096) : (i += 1) {
835
+ try stream.write("x");
836
+ try stream.commit();
837
+ }
838
+
839
+ try stream.write("important");
840
+ const result = stream.commit();
841
+ try testing.expectError(raw.StreamError.NoSpace, result);
842
+
843
+ _ = drainAllSpans(stream);
844
+ try stream.commit();
845
+
846
+ const stats = stream.getStats();
847
+ try testing.expectEqual(@as(u64, 4096 + 9), stats.bytes_written);
848
+ try testing.expectEqual(@as(u64, 4097), stats.spans_committed);
849
+
850
+ var buf: [16]raw.SpanInfo = undefined;
851
+ const count = stream.drainSpans(&buf);
852
+ try testing.expectEqual(@as(u32, 1), count);
853
+ const slice = buf[0].slice();
854
+ try testing.expectEqualStrings("important", slice);
855
+ stream.markSpanConsumed(buf[0]);
856
+ }
857
+
858
+ test "Stream - close with active reservation returns Busy" {
859
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
860
+ defer stream.destroy();
861
+
862
+ _ = try stream.reserve(1);
863
+
864
+ const result = stream.close();
865
+ try testing.expectError(raw.StreamError.Busy, result);
866
+
867
+ try testing.expectEqual(false, stream.closed);
868
+ try stream.commitReserved(0);
869
+ try stream.close();
870
+ }
871
+
872
+ test "Stream - destroy without close commits pending data" {
873
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
874
+
875
+ try stream.write("before destroy");
876
+
877
+ const stats = stream.getStats();
878
+ try testing.expectEqual(@as(u64, 14), stats.bytes_written);
879
+ try testing.expectEqual(@as(u64, 0), stats.spans_committed);
880
+
881
+ stream.destroy();
882
+ }
883
+
884
+ test "Stream - write error mid-loop preserves already-committed spans" {
885
+ const stream = try raw.Stream.create(testing.allocator, testOptionsFull(32, 2, 64, true));
886
+ defer stream.destroy();
887
+
888
+ const data = [_]u8{'Z'} ** 96;
889
+ const result = stream.write(&data);
890
+ try testing.expectError(raw.StreamError.MaxBytes, result);
891
+
892
+ const stats = stream.getStats();
893
+ try testing.expectEqual(@as(u64, 2), stats.spans_committed);
894
+
895
+ const drained = drainAllSpans(stream);
896
+ try testing.expectEqual(@as(u64, 64), drained);
897
+ }
898
+
899
+ test "Stream - bytes_written matches total drained across all operations" {
900
+ const stream = try raw.Stream.create(testing.allocator, testOptions(64, 1, true));
901
+ defer stream.destroy();
902
+
903
+ try stream.write("short");
904
+ try stream.commit();
905
+ try stream.write(&([_]u8{'M'} ** 64));
906
+ try stream.write(&([_]u8{'L'} ** 200));
907
+
908
+ try stream.commit();
909
+
910
+ const stats = stream.getStats();
911
+ const drained = drainAllSpans(stream);
912
+
913
+ try testing.expectEqual(stats.bytes_written, drained);
914
+ }
915
+
916
+ var data_available_count: u32 = 0;
917
+
918
+ fn countingCallback(_: usize, event_id: u32, _: usize, _: u64) callconv(.c) void {
919
+ if (event_id == @intFromEnum(raw.EventId.DataAvailable)) {
920
+ data_available_count += 1;
921
+ }
922
+ }
923
+
924
+ test "Stream - write returning NoSpace emits DataAvailable exactly once" {
925
+ // Regression: NoSpace path must not double-emit DataAvailable.
926
+ data_available_count = 0;
927
+ const stream = try raw.Stream.create(testing.allocator, testOptions(64, 2, false));
928
+ defer stream.destroy();
929
+
930
+ stream.setCallback(&countingCallback);
931
+ try stream.attach();
932
+ data_available_count = 0;
933
+ const first = [_]u8{'A'} ** 64;
934
+ try stream.write(&first);
935
+ const result = stream.write(&([_]u8{'B'} ** 65));
936
+ try testing.expectError(raw.StreamError.NoSpace, result);
937
+ try testing.expectEqual(@as(u32, 1), data_available_count);
938
+ }
939
+
940
+ test "Stream - hasPendingSpans reflects state correctly" {
941
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
942
+ defer stream.destroy();
943
+
944
+ try testing.expect(!stream.hasPendingSpans());
945
+
946
+ try stream.write("data");
947
+ try stream.commit();
948
+ try testing.expect(stream.hasPendingSpans());
949
+
950
+ _ = drainAllSpans(stream);
951
+ try testing.expect(!stream.hasPendingSpans());
952
+ }
953
+
954
+ var drain_during_write_stream: ?*raw.Stream = null;
955
+ var drain_during_write_total: u64 = 0;
956
+
957
+ fn drainingCallback(stream_ptr: usize, event_id: u32, _: usize, _: u64) callconv(.c) void {
958
+ if (event_id != @intFromEnum(raw.EventId.DataAvailable)) return;
959
+ const s = drain_during_write_stream orelse return;
960
+ if (@intFromPtr(s) != stream_ptr) return;
961
+
962
+ var buf: [64]raw.SpanInfo = undefined;
963
+ while (true) {
964
+ const count = s.drainSpans(&buf);
965
+ if (count == 0) break;
966
+ var i: u32 = 0;
967
+ while (i < count) : (i += 1) {
968
+ drain_during_write_total += buf[i].len;
969
+ s.markSpanConsumed(buf[i]);
970
+ }
971
+ }
972
+ }
973
+
974
+ test "Stream - synchronous drain during write does not corrupt state" {
975
+ drain_during_write_stream = null;
976
+ drain_during_write_total = 0;
977
+
978
+ const chunk_size: u32 = 64;
979
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 2, true));
980
+ defer stream.destroy();
981
+
982
+ stream.setCallback(&drainingCallback);
983
+ try stream.attach();
984
+ drain_during_write_stream = stream;
985
+ drain_during_write_total = 0;
986
+
987
+ const data = [_]u8{'D'} ** 256;
988
+ try stream.write(&data);
989
+
990
+ try stream.commit();
991
+ var buf: [64]raw.SpanInfo = undefined;
992
+ while (true) {
993
+ const count = stream.drainSpans(&buf);
994
+ if (count == 0) break;
995
+ var i: u32 = 0;
996
+ while (i < count) : (i += 1) {
997
+ drain_during_write_total += buf[i].len;
998
+ stream.markSpanConsumed(buf[i]);
999
+ }
1000
+ }
1001
+
1002
+ try testing.expectEqual(@as(u64, 256), drain_during_write_total);
1003
+ try testing.expectEqual(@as(u64, 256), stream.getStats().bytes_written);
1004
+
1005
+ drain_during_write_stream = null;
1006
+ }
1007
+
1008
+ test "Stream - span ring wrapping near u32 max" {
1009
+ // Position near u32 max to exercise wrapping without huge loops.
1010
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 2, false));
1011
+ defer stream.destroy();
1012
+
1013
+ const near_max: u32 = std.math.maxInt(u32) - 5;
1014
+ stream.span_ring.head = near_max;
1015
+ stream.span_ring.tail = near_max;
1016
+
1017
+ var i: u32 = 0;
1018
+ while (i < 10) : (i += 1) {
1019
+ try stream.write("data");
1020
+ try stream.commit();
1021
+ }
1022
+
1023
+ try testing.expectEqual(@as(u32, 10), stream.span_ring.count());
1024
+
1025
+ var buf: [16]raw.SpanInfo = undefined;
1026
+ const count = stream.drainSpans(&buf);
1027
+ try testing.expectEqual(@as(u32, 10), count);
1028
+
1029
+ try testing.expectEqual(@as(u32, 0), stream.span_ring.count());
1030
+ try testing.expectEqual(near_max +% 10, stream.span_ring.head);
1031
+ try testing.expectEqual(near_max +% 10, stream.span_ring.tail);
1032
+
1033
+ try testing.expectEqualStrings("data", buf[9].slice());
1034
+ for (buf[0..count]) |span| {
1035
+ stream.markSpanConsumed(span);
1036
+ }
1037
+ }
1038
+
1039
+ test "Stream - commitReserved with zero length produces no span" {
1040
+ const stream = try raw.Stream.create(testing.allocator, testOptions(256, 1, false));
1041
+ defer stream.destroy();
1042
+
1043
+ _ = try stream.reserve(1);
1044
+ try stream.commitReserved(0);
1045
+
1046
+ try testing.expectEqual(@as(u64, 0), stream.getStats().spans_committed);
1047
+ try testing.expectEqual(@as(u64, 0), stream.getStats().bytes_written);
1048
+ try testing.expect(!stream.hasPendingSpans());
1049
+
1050
+ try stream.write("after");
1051
+ try stream.commit();
1052
+
1053
+ try testing.expectEqual(@as(u64, 1), stream.getStats().spans_committed);
1054
+ try testing.expectEqual(@as(u64, 5), stream.getStats().bytes_written);
1055
+
1056
+ const drained = drainAllSpans(stream);
1057
+ try testing.expectEqual(@as(u64, 5), drained);
1058
+ }
1059
+
1060
+ test "Stream - write exactly chunk_size * N with auto_commit commits all, no dangling pending" {
1061
+ const chunk_size: u32 = 64;
1062
+ const n: usize = 5;
1063
+ const total = chunk_size * n;
1064
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 2, true));
1065
+ defer stream.destroy();
1066
+
1067
+ const data = [_]u8{'E'} ** total;
1068
+ try stream.write(&data);
1069
+
1070
+ const stats = stream.getStats();
1071
+
1072
+ try testing.expectEqual(@as(u64, n), stats.spans_committed);
1073
+ try testing.expectEqual(@as(u64, total), stats.bytes_written);
1074
+
1075
+ try stream.commit();
1076
+ try testing.expectEqual(@as(u64, n), stream.getStats().spans_committed);
1077
+
1078
+ const drained = drainAllSpans(stream);
1079
+ try testing.expectEqual(@as(u64, total), drained);
1080
+ }
1081
+
1082
+ test "Stream - state buffer reallocation preserves active span refcounts" {
1083
+ const chunk_size: u32 = 64;
1084
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, false));
1085
+ defer stream.destroy();
1086
+
1087
+ const first = [_]u8{'F'} ** 64;
1088
+ try stream.write(&first);
1089
+ try stream.commit();
1090
+
1091
+ try testing.expectEqual(@as(u8, 1), stream.stateBuffer()[0]);
1092
+
1093
+ var i: usize = 0;
1094
+ while (i < 3) : (i += 1) {
1095
+ const filler = [_]u8{@intCast(i + 0x10)} ** 64;
1096
+ try stream.write(&filler);
1097
+ try stream.commit();
1098
+ }
1099
+
1100
+ try testing.expect(stream.getStats().chunks >= 4);
1101
+ try testing.expectEqual(@as(u8, 1), stream.stateBuffer()[0]);
1102
+
1103
+ const drained = drainAllSpans(stream);
1104
+ try testing.expectEqual(@as(u64, 256), drained);
1105
+
1106
+ try testing.expectEqual(@as(u8, 0), stream.stateBuffer()[0]);
1107
+ }
1108
+
1109
+ test "Stream - state_buffer caps at 255 and advances to new chunk" {
1110
+ // Refcount saturation should force a new chunk.
1111
+ const chunk_size: u32 = 4096;
1112
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, false));
1113
+ defer stream.destroy();
1114
+
1115
+ var i: u32 = 0;
1116
+ while (i < 260) : (i += 1) {
1117
+ try stream.write("x");
1118
+ try stream.commit();
1119
+ }
1120
+
1121
+ try testing.expectEqual(@as(u8, 255), stream.stateBuffer()[0]);
1122
+ try testing.expect(stream.getStats().chunks >= 2);
1123
+ var buf: [64]raw.SpanInfo = undefined;
1124
+ var drain_count: u32 = 0;
1125
+ while (true) {
1126
+ const count = stream.drainSpans(&buf);
1127
+ if (count == 0) break;
1128
+ var j: u32 = 0;
1129
+ while (j < count) : (j += 1) {
1130
+ stream.markSpanConsumed(buf[j]);
1131
+ drain_count += 1;
1132
+
1133
+ if (drain_count == 254) {
1134
+ try testing.expectEqual(@as(u8, 1), stream.stateBuffer()[0]);
1135
+ }
1136
+ }
1137
+ }
1138
+
1139
+ try testing.expectEqual(@as(u32, 260), drain_count);
1140
+ try testing.expectEqual(@as(u8, 0), stream.stateBuffer()[0]);
1141
+ }
1142
+
1143
+ test "Stream - refcount saturation must not cause data corruption" {
1144
+ // Regression: refcount saturation must not allow reuse that corrupts data.
1145
+
1146
+ const chunk_size: u32 = 256;
1147
+ const stream = try raw.Stream.create(testing.allocator, testOptions(chunk_size, 1, false));
1148
+ defer stream.destroy();
1149
+
1150
+ var i: u32 = 0;
1151
+ while (i < 256) : (i += 1) {
1152
+ const byte = [1]u8{@intCast(i % 256)};
1153
+ try stream.write(&byte);
1154
+ try stream.commit();
1155
+ }
1156
+
1157
+ try testing.expectEqual(@as(u8, 255), stream.stateBuffer()[0]);
1158
+
1159
+ var buf: [64]raw.SpanInfo = undefined;
1160
+ var drained: u32 = 0;
1161
+ var data_index: u32 = 0;
1162
+ while (drained < 255) {
1163
+ const want: u32 = @intCast(@min(buf.len, 255 - drained));
1164
+ const count = stream.drainSpans(buf[0..want]);
1165
+ if (count == 0) break;
1166
+ var j: u32 = 0;
1167
+ while (j < count) : (j += 1) {
1168
+ const slice = buf[j].slice();
1169
+ try testing.expectEqual(@as(usize, 1), slice.len);
1170
+ try testing.expectEqual(@as(u8, @intCast(data_index % 256)), slice[0]);
1171
+ stream.markSpanConsumed(buf[j]);
1172
+ data_index += 1;
1173
+ drained += 1;
1174
+ }
1175
+ }
1176
+ try testing.expectEqual(@as(u32, 255), drained);
1177
+
1178
+ try testing.expectEqual(@as(u8, 0), stream.stateBuffer()[0]);
1179
+ const overwrite = [_]u8{'Z'} ** 128;
1180
+ try stream.write(&overwrite);
1181
+ try stream.commit();
1182
+
1183
+ const count = stream.drainSpans(&buf);
1184
+ try testing.expect(count >= 1);
1185
+
1186
+ var found = false;
1187
+ var j: u32 = 0;
1188
+ while (j < count) : (j += 1) {
1189
+ const slice = buf[j].slice();
1190
+ if (slice.len == 1) {
1191
+ try testing.expectEqual(@as(u8, 255), slice[0]);
1192
+ found = true;
1193
+ stream.markSpanConsumed(buf[j]);
1194
+ } else {
1195
+ stream.markSpanConsumed(buf[j]);
1196
+ }
1197
+ }
1198
+ try testing.expect(found);
1199
+ }
1200
+
1201
+ // Regression: addChunkLocked error paths must not leak or desync state.
1202
+
1203
+ test "addChunkLocked must not leak chunk data when ArrayList append fails" {
1204
+ // Sweep failing allocations to ensure no leaks.
1205
+ const chunk_size: u32 = 64;
1206
+ const initial_chunks: u32 = 9;
1207
+
1208
+ var counter = std.testing.FailingAllocator.init(std.heap.page_allocator, .{});
1209
+ {
1210
+ const s = raw.Stream.create(counter.allocator(), testOptions(chunk_size, initial_chunks, false)) catch
1211
+ return error.TestUnexpectedResult;
1212
+ s.destroy();
1213
+ }
1214
+ const create_allocs = counter.allocations;
1215
+
1216
+ const configs = [_]struct { resize_fail: usize }{
1217
+ .{ .resize_fail = std.math.maxInt(usize) },
1218
+ .{ .resize_fail = 0 },
1219
+ };
1220
+
1221
+ for (configs) |cfg| {
1222
+ var fi: usize = 0;
1223
+ while (fi <= create_allocs + 2) : (fi += 1) {
1224
+ var fa = std.testing.FailingAllocator.init(testing.allocator, .{
1225
+ .fail_index = fi,
1226
+ .resize_fail_index = cfg.resize_fail,
1227
+ });
1228
+
1229
+ const result = raw.Stream.create(fa.allocator(), testOptions(chunk_size, initial_chunks, false));
1230
+ if (result) |stream| {
1231
+ stream.destroy();
1232
+ } else |_| {}
1233
+ }
1234
+ }
1235
+ }
1236
+
1237
+ test "addChunkLocked must not leak chunk data during initial create" {
1238
+ // Regression: failing append must not leak chunk data.
1239
+
1240
+ var failing = std.testing.FailingAllocator.init(testing.allocator, .{
1241
+ .fail_index = 4,
1242
+ });
1243
+
1244
+ const result = raw.Stream.create(failing.allocator(), testOptions(64, 1, false));
1245
+ try testing.expectError(raw.StreamError.OutOfMemory, result);
1246
+ }
1247
+
1248
+ test "addChunkLocked must keep state buffer consistent with chunk count" {
1249
+ // Invariant: state_capacity must track chunks.items.len.
1250
+
1251
+ var failing = std.testing.FailingAllocator.init(std.heap.page_allocator, .{
1252
+ .fail_index = 6,
1253
+ });
1254
+
1255
+ const stream = raw.Stream.create(failing.allocator(), testOptions(64, 1, false)) catch
1256
+ return error.TestUnexpectedResult;
1257
+ defer stream.destroy();
1258
+
1259
+ stream.write(&([_]u8{'A'} ** 64)) catch return error.TestUnexpectedResult;
1260
+ stream.commit() catch return error.TestUnexpectedResult;
1261
+ const result = stream.write("x");
1262
+ try testing.expectError(raw.StreamError.OutOfMemory, result);
1263
+ try testing.expect(stream.state_capacity >= stream.chunks.items.len);
1264
+ }