@xincli/opentui-core 0.4.6

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 (476) hide show
  1. package/README.md +63 -0
  2. package/dev/keypress-debug-renderer.ts +148 -0
  3. package/dev/keypress-debug.ts +43 -0
  4. package/dev/print-env-vars.ts +32 -0
  5. package/dev/test-tmux-graphics-334.sh +68 -0
  6. package/dev/test-tmux-theme-queries.sh +151 -0
  7. package/dev/thai-debug-test.ts +68 -0
  8. package/dev/theme-mode.ts +290 -0
  9. package/docs/development.md +144 -0
  10. package/package.json +102 -0
  11. package/prebuilt/aarch64-android/libopentui.so +0 -0
  12. package/scripts/build-android.ts +226 -0
  13. package/scripts/build-native-termux.sh +525 -0
  14. package/scripts/build.ts +530 -0
  15. package/scripts/dist-test.ts +312 -0
  16. package/scripts/package-prebuilt.ts +110 -0
  17. package/scripts/publish.ts +60 -0
  18. package/scripts/test-node-hook.mjs +16 -0
  19. package/scripts/test-node.ts +241 -0
  20. package/scripts/vendor-deps.sh +73 -0
  21. package/src/NativeSpanFeed.ts +375 -0
  22. package/src/Renderable.ts +1878 -0
  23. package/src/__snapshots__/buffer.test.ts.snap +28 -0
  24. package/src/animation/Timeline.test.ts +2709 -0
  25. package/src/animation/Timeline.ts +598 -0
  26. package/src/ansi.ts +22 -0
  27. package/src/audio.ts +479 -0
  28. package/src/benchmark/attenuation-benchmark.ts +81 -0
  29. package/src/benchmark/audio-playback-benchmark.ts +344 -0
  30. package/src/benchmark/box-draw-benchmark.ts +1042 -0
  31. package/src/benchmark/colormatrix-benchmark.ts +123 -0
  32. package/src/benchmark/gain-benchmark.ts +80 -0
  33. package/src/benchmark/latest-all-bench-run.json +707 -0
  34. package/src/benchmark/latest-async-bench-run.json +336 -0
  35. package/src/benchmark/latest-default-bench-run.json +657 -0
  36. package/src/benchmark/latest-large-bench-run.json +707 -0
  37. package/src/benchmark/latest-quick-bench-run.json +207 -0
  38. package/src/benchmark/layout-benchmark.ts +2547 -0
  39. package/src/benchmark/markdown-benchmark.ts +1796 -0
  40. package/src/benchmark/native-span-feed-async-benchmark.ts +355 -0
  41. package/src/benchmark/native-span-feed-benchmark.md +56 -0
  42. package/src/benchmark/native-span-feed-benchmark.ts +596 -0
  43. package/src/benchmark/native-span-feed-compare.ts +280 -0
  44. package/src/benchmark/render-traversal-benchmark.ts +928 -0
  45. package/src/benchmark/text-buffer-render-benchmark.ts +874 -0
  46. package/src/benchmark/text-table-benchmark.ts +948 -0
  47. package/src/buffer.test.ts +292 -0
  48. package/src/buffer.ts +564 -0
  49. package/src/console.test.ts +629 -0
  50. package/src/console.ts +1253 -0
  51. package/src/edit-buffer.test.ts +1852 -0
  52. package/src/edit-buffer.ts +417 -0
  53. package/src/editor-view.test.ts +1056 -0
  54. package/src/editor-view.ts +290 -0
  55. package/src/index.ts +25 -0
  56. package/src/lib/KeyHandler.integration.test.ts +292 -0
  57. package/src/lib/KeyHandler.stopPropagation.test.ts +289 -0
  58. package/src/lib/KeyHandler.test.ts +662 -0
  59. package/src/lib/KeyHandler.ts +222 -0
  60. package/src/lib/RGBA.test.ts +60 -0
  61. package/src/lib/RGBA.ts +343 -0
  62. package/src/lib/ascii.font.ts +330 -0
  63. package/src/lib/border.test.ts +83 -0
  64. package/src/lib/border.ts +170 -0
  65. package/src/lib/bunfs.test.ts +27 -0
  66. package/src/lib/bunfs.ts +18 -0
  67. package/src/lib/clipboard.test.ts +63 -0
  68. package/src/lib/clipboard.ts +41 -0
  69. package/src/lib/clock.ts +35 -0
  70. package/src/lib/data-paths.test.ts +133 -0
  71. package/src/lib/data-paths.ts +109 -0
  72. package/src/lib/debounce.ts +106 -0
  73. package/src/lib/detect-links.test.ts +98 -0
  74. package/src/lib/detect-links.ts +56 -0
  75. package/src/lib/env.test.ts +228 -0
  76. package/src/lib/env.ts +209 -0
  77. package/src/lib/extmarks-history.ts +51 -0
  78. package/src/lib/extmarks-multiwidth.test.ts +324 -0
  79. package/src/lib/extmarks.test.ts +3547 -0
  80. package/src/lib/extmarks.ts +861 -0
  81. package/src/lib/fonts/block.json +405 -0
  82. package/src/lib/fonts/grid.json +265 -0
  83. package/src/lib/fonts/huge.json +741 -0
  84. package/src/lib/fonts/pallet.json +314 -0
  85. package/src/lib/fonts/shade.json +591 -0
  86. package/src/lib/fonts/slick.json +321 -0
  87. package/src/lib/fonts/tiny.json +69 -0
  88. package/src/lib/hast-styled-text.ts +59 -0
  89. package/src/lib/index.ts +22 -0
  90. package/src/lib/keybinding.internal.test.ts +342 -0
  91. package/src/lib/keybinding.internal.ts +182 -0
  92. package/src/lib/objects-in-viewport.test.ts +789 -0
  93. package/src/lib/objects-in-viewport.ts +153 -0
  94. package/src/lib/output.capture.ts +58 -0
  95. package/src/lib/parse.keypress-kitty.protocol.test.ts +347 -0
  96. package/src/lib/parse.keypress-kitty.test.ts +712 -0
  97. package/src/lib/parse.keypress-kitty.ts +470 -0
  98. package/src/lib/parse.keypress.test.ts +2020 -0
  99. package/src/lib/parse.keypress.ts +480 -0
  100. package/src/lib/parse.mouse.test.ts +552 -0
  101. package/src/lib/parse.mouse.ts +232 -0
  102. package/src/lib/paste.ts +18 -0
  103. package/src/lib/queue.ts +65 -0
  104. package/src/lib/render-geometry.ts +36 -0
  105. package/src/lib/renderable.validations.test.ts +87 -0
  106. package/src/lib/renderable.validations.ts +83 -0
  107. package/src/lib/scroll-acceleration.ts +98 -0
  108. package/src/lib/selection.ts +261 -0
  109. package/src/lib/singleton.ts +34 -0
  110. package/src/lib/stdin-parser.test.ts +2439 -0
  111. package/src/lib/stdin-parser.ts +2007 -0
  112. package/src/lib/styled-text.ts +178 -0
  113. package/src/lib/terminal-capability-detection.test.ts +226 -0
  114. package/src/lib/terminal-capability-detection.ts +98 -0
  115. package/src/lib/terminal-palette.test.ts +954 -0
  116. package/src/lib/terminal-palette.ts +441 -0
  117. package/src/lib/tree-sitter/assets/README.md +119 -0
  118. package/src/lib/tree-sitter/assets/javascript/highlights.scm +205 -0
  119. package/src/lib/tree-sitter/assets/javascript/tree-sitter-javascript.wasm +0 -0
  120. package/src/lib/tree-sitter/assets/markdown/highlights.scm +150 -0
  121. package/src/lib/tree-sitter/assets/markdown/injections.scm +27 -0
  122. package/src/lib/tree-sitter/assets/markdown/tree-sitter-markdown.wasm +0 -0
  123. package/src/lib/tree-sitter/assets/markdown_inline/highlights.scm +115 -0
  124. package/src/lib/tree-sitter/assets/markdown_inline/tree-sitter-markdown_inline.wasm +0 -0
  125. package/src/lib/tree-sitter/assets/typescript/highlights.scm +604 -0
  126. package/src/lib/tree-sitter/assets/typescript/tree-sitter-typescript.wasm +0 -0
  127. package/src/lib/tree-sitter/assets/update.ts +360 -0
  128. package/src/lib/tree-sitter/assets/zig/highlights.scm +284 -0
  129. package/src/lib/tree-sitter/assets/zig/tree-sitter-zig.wasm +0 -0
  130. package/src/lib/tree-sitter/assets.d.ts +9 -0
  131. package/src/lib/tree-sitter/cache.test.ts +291 -0
  132. package/src/lib/tree-sitter/client.test.ts +1434 -0
  133. package/src/lib/tree-sitter/client.ts +847 -0
  134. package/src/lib/tree-sitter/default-parsers.ts +133 -0
  135. package/src/lib/tree-sitter/download-utils.ts +148 -0
  136. package/src/lib/tree-sitter/index.ts +75 -0
  137. package/src/lib/tree-sitter/parser.worker.ts +1098 -0
  138. package/src/lib/tree-sitter/parsers-config.ts +81 -0
  139. package/src/lib/tree-sitter/resolve-ft.test.ts +55 -0
  140. package/src/lib/tree-sitter/resolve-ft.ts +189 -0
  141. package/src/lib/tree-sitter/types.ts +132 -0
  142. package/src/lib/tree-sitter/update-assets.ts +10 -0
  143. package/src/lib/tree-sitter-styled-text.test.ts +1253 -0
  144. package/src/lib/tree-sitter-styled-text.ts +314 -0
  145. package/src/lib/validate-dir-name.ts +55 -0
  146. package/src/lib/yoga.options.test.ts +628 -0
  147. package/src/lib/yoga.options.ts +346 -0
  148. package/src/native-event-worker-repro.test.ts +48 -0
  149. package/src/native-event-worker-repro.worker.ts +9 -0
  150. package/src/native-handle.test.ts +221 -0
  151. package/src/platform/ffi.test.ts +516 -0
  152. package/src/platform/ffi.ts +653 -0
  153. package/src/platform/runtime.test.ts +52 -0
  154. package/src/platform/runtime.ts +121 -0
  155. package/src/platform/test.ts +45 -0
  156. package/src/platform/worker-handler-cleanup.fixture.ts +12 -0
  157. package/src/platform/worker-onmessage-startup.fixture.ts +10 -0
  158. package/src/platform/worker-startup.fixture.ts +9 -0
  159. package/src/platform/worker.node-test.ts +153 -0
  160. package/src/platform/worker.ts +425 -0
  161. package/src/plugins/core-slot.ts +579 -0
  162. package/src/plugins/registry.ts +402 -0
  163. package/src/plugins/types.ts +46 -0
  164. package/src/post/effects.ts +948 -0
  165. package/src/post/filters.ts +512 -0
  166. package/src/post/matrices.ts +288 -0
  167. package/src/renderables/ASCIIFont.ts +219 -0
  168. package/src/renderables/Box.test.ts +474 -0
  169. package/src/renderables/Box.ts +353 -0
  170. package/src/renderables/Code.test.ts +2388 -0
  171. package/src/renderables/Code.ts +555 -0
  172. package/src/renderables/Diff.regression.test.ts +381 -0
  173. package/src/renderables/Diff.test.ts +3212 -0
  174. package/src/renderables/Diff.ts +1278 -0
  175. package/src/renderables/EditBufferRenderable.test.ts +288 -0
  176. package/src/renderables/EditBufferRenderable.ts +1152 -0
  177. package/src/renderables/FrameBuffer.ts +47 -0
  178. package/src/renderables/Input.test.ts +1310 -0
  179. package/src/renderables/Input.ts +273 -0
  180. package/src/renderables/LineNumberRenderable.ts +725 -0
  181. package/src/renderables/Markdown.ts +2144 -0
  182. package/src/renderables/ScrollBar.ts +423 -0
  183. package/src/renderables/ScrollBox.ts +903 -0
  184. package/src/renderables/Select.test.ts +1134 -0
  185. package/src/renderables/Select.ts +535 -0
  186. package/src/renderables/Slider.test.ts +456 -0
  187. package/src/renderables/Slider.ts +342 -0
  188. package/src/renderables/TabSelect.test.ts +238 -0
  189. package/src/renderables/TabSelect.ts +447 -0
  190. package/src/renderables/Text.selection-buffer.test.ts +118 -0
  191. package/src/renderables/Text.test.ts +2662 -0
  192. package/src/renderables/Text.ts +147 -0
  193. package/src/renderables/TextBufferRenderable.ts +504 -0
  194. package/src/renderables/TextNode.test.ts +1137 -0
  195. package/src/renderables/TextNode.ts +379 -0
  196. package/src/renderables/TextTable.test.ts +1411 -0
  197. package/src/renderables/TextTable.ts +1434 -0
  198. package/src/renderables/Textarea.ts +425 -0
  199. package/src/renderables/TimeToFirstDraw.ts +89 -0
  200. package/src/renderables/__snapshots__/Code.test.ts.snap +13 -0
  201. package/src/renderables/__snapshots__/Diff.test.ts.snap +785 -0
  202. package/src/renderables/__snapshots__/Text.test.ts.snap +421 -0
  203. package/src/renderables/__snapshots__/TextTable.test.ts.snap +215 -0
  204. package/src/renderables/__tests__/Code.test.ts +55 -0
  205. package/src/renderables/__tests__/LineNumberRenderable.scrollbox-simple.test.ts +144 -0
  206. package/src/renderables/__tests__/LineNumberRenderable.scrollbox.test.ts +827 -0
  207. package/src/renderables/__tests__/LineNumberRenderable.test.ts +1906 -0
  208. package/src/renderables/__tests__/LineNumberRenderable.wrapping.test.ts +85 -0
  209. package/src/renderables/__tests__/Markdown.code-colors.test.ts +288 -0
  210. package/src/renderables/__tests__/Markdown.test.ts +4175 -0
  211. package/src/renderables/__tests__/MultiRenderable.selection.test.ts +87 -0
  212. package/src/renderables/__tests__/Textarea.buffer.test.ts +683 -0
  213. package/src/renderables/__tests__/Textarea.destroyed-events.test.ts +675 -0
  214. package/src/renderables/__tests__/Textarea.editing.test.ts +2120 -0
  215. package/src/renderables/__tests__/Textarea.error-handling.test.ts +35 -0
  216. package/src/renderables/__tests__/Textarea.events.test.ts +738 -0
  217. package/src/renderables/__tests__/Textarea.highlights.test.ts +590 -0
  218. package/src/renderables/__tests__/Textarea.keybinding.test.ts +3275 -0
  219. package/src/renderables/__tests__/Textarea.paste.test.ts +357 -0
  220. package/src/renderables/__tests__/Textarea.rendering.test.ts +1927 -0
  221. package/src/renderables/__tests__/Textarea.scroll.test.ts +733 -0
  222. package/src/renderables/__tests__/Textarea.selection.test.ts +1589 -0
  223. package/src/renderables/__tests__/Textarea.stress.test.ts +670 -0
  224. package/src/renderables/__tests__/Textarea.undo-redo.test.ts +383 -0
  225. package/src/renderables/__tests__/Textarea.visual-lines.test.ts +310 -0
  226. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.code.test.ts.snap +221 -0
  227. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox-simple.test.ts.snap +89 -0
  228. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox.test.ts.snap +457 -0
  229. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.test.ts.snap +158 -0
  230. package/src/renderables/__tests__/__snapshots__/Textarea.rendering.test.ts.snap +387 -0
  231. package/src/renderables/__tests__/markdown-parser.test.ts +241 -0
  232. package/src/renderables/__tests__/renderable-test-utils.ts +60 -0
  233. package/src/renderables/composition/README.md +8 -0
  234. package/src/renderables/composition/VRenderable.ts +32 -0
  235. package/src/renderables/composition/constructs.ts +127 -0
  236. package/src/renderables/composition/vnode.ts +277 -0
  237. package/src/renderables/index.ts +23 -0
  238. package/src/renderables/markdown-parser.ts +79 -0
  239. package/src/renderer-theme-mode.ts +199 -0
  240. package/src/renderer.ts +4975 -0
  241. package/src/runtime-plugin-support-configure.ts +63 -0
  242. package/src/runtime-plugin-support.ts +13 -0
  243. package/src/runtime-plugin.ts +626 -0
  244. package/src/specs/terminal-startup.md +64 -0
  245. package/src/syntax-style.test.ts +841 -0
  246. package/src/syntax-style.ts +274 -0
  247. package/src/testing/README.md +219 -0
  248. package/src/testing/bun-test-node.ts +880 -0
  249. package/src/testing/capture-spans.test.ts +216 -0
  250. package/src/testing/integration.test.ts +276 -0
  251. package/src/testing/manual-clock.ts +117 -0
  252. package/src/testing/mock-keys.test.ts +1378 -0
  253. package/src/testing/mock-keys.ts +457 -0
  254. package/src/testing/mock-mouse.test.ts +218 -0
  255. package/src/testing/mock-mouse.ts +247 -0
  256. package/src/testing/mock-tree-sitter-client.ts +81 -0
  257. package/src/testing/spy.ts +13 -0
  258. package/src/testing/terminal-capabilities.ts +47 -0
  259. package/src/testing/test-recorder.test.ts +410 -0
  260. package/src/testing/test-recorder.ts +133 -0
  261. package/src/testing/test-renderer.ts +375 -0
  262. package/src/testing/test-renderer.wait.test.ts +278 -0
  263. package/src/testing/test-streams.ts +31 -0
  264. package/src/testing.ts +9 -0
  265. package/src/tests/__snapshots__/absolute-positioning.snapshot.test.ts.snap +481 -0
  266. package/src/tests/__snapshots__/renderable.snapshot.test.ts.snap +19 -0
  267. package/src/tests/__snapshots__/scrollbox.test.ts.snap +29 -0
  268. package/src/tests/absolute-positioning.snapshot.test.ts +638 -0
  269. package/src/tests/allocator-stats.test.ts +53 -0
  270. package/src/tests/audio.test.ts +303 -0
  271. package/src/tests/destroy-during-render.test.ts +200 -0
  272. package/src/tests/destroy-on-exit.fixture.ts +36 -0
  273. package/src/tests/destroy-on-exit.test.ts +62 -0
  274. package/src/tests/ffi-borrowed-pointer-callsites.test.ts +153 -0
  275. package/src/tests/hover-cursor.test.ts +98 -0
  276. package/src/tests/native-backed-measurement-lifecycle.test.ts +133 -0
  277. package/src/tests/native-backed-measurement-parity.test.ts +322 -0
  278. package/src/tests/native-span-feed-async.test.ts +173 -0
  279. package/src/tests/native-span-feed-close.test.ts +120 -0
  280. package/src/tests/native-span-feed-coverage.test.ts +227 -0
  281. package/src/tests/native-span-feed-edge-cases.test.ts +384 -0
  282. package/src/tests/native-span-feed-use-after-free.test.ts +45 -0
  283. package/src/tests/opacity.test.ts +123 -0
  284. package/src/tests/renderable.snapshot.test.ts +524 -0
  285. package/src/tests/renderable.test.ts +1851 -0
  286. package/src/tests/renderer.clock.test.ts +270 -0
  287. package/src/tests/renderer.console-startup.test.ts +2151 -0
  288. package/src/tests/renderer.control.test.ts +458 -0
  289. package/src/tests/renderer.core-slot-binding.test.ts +962 -0
  290. package/src/tests/renderer.cursor.test.ts +26 -0
  291. package/src/tests/renderer.custom-stdout.test.ts +1112 -0
  292. package/src/tests/renderer.destroy-during-render.test.ts +147 -0
  293. package/src/tests/renderer.focus-restore.test.ts +257 -0
  294. package/src/tests/renderer.focus.test.ts +329 -0
  295. package/src/tests/renderer.idle.test.ts +219 -0
  296. package/src/tests/renderer.input.test.ts +2615 -0
  297. package/src/tests/renderer.kitty-flags.test.ts +195 -0
  298. package/src/tests/renderer.lifecycle.test.ts +34 -0
  299. package/src/tests/renderer.mouse.test.ts +1315 -0
  300. package/src/tests/renderer.notifications.test.ts +27 -0
  301. package/src/tests/renderer.palette.test.ts +1199 -0
  302. package/src/tests/renderer.remote.test.ts +247 -0
  303. package/src/tests/renderer.render-stats.test.ts +146 -0
  304. package/src/tests/renderer.scrollback-surface.test.ts +474 -0
  305. package/src/tests/renderer.selection.test.ts +138 -0
  306. package/src/tests/renderer.slot-registry.test.ts +684 -0
  307. package/src/tests/renderer.tracker.test.ts +202 -0
  308. package/src/tests/renderer.useMouse.test.ts +83 -0
  309. package/src/tests/runtime-plugin-node-modules-cycle.fixture.ts +76 -0
  310. package/src/tests/runtime-plugin-node-modules-import-like-string.fixture.ts +48 -0
  311. package/src/tests/runtime-plugin-node-modules-mjs.fixture.ts +43 -0
  312. package/src/tests/runtime-plugin-node-modules-no-bare-rewrite.fixture.ts +67 -0
  313. package/src/tests/runtime-plugin-node-modules-package-type-cache.fixture.ts +72 -0
  314. package/src/tests/runtime-plugin-node-modules-runtime-specifier.fixture.ts +44 -0
  315. package/src/tests/runtime-plugin-node-modules-scoped-package-bare-rewrite.fixture.ts +85 -0
  316. package/src/tests/runtime-plugin-path-alias.fixture.ts +43 -0
  317. package/src/tests/runtime-plugin-resolve-roots.fixture.ts +65 -0
  318. package/src/tests/runtime-plugin-support-configure.fixture.ts +31 -0
  319. package/src/tests/runtime-plugin-support-late-addition.fixture.ts +17 -0
  320. package/src/tests/runtime-plugin-support.fixture.ts +11 -0
  321. package/src/tests/runtime-plugin-support.test.ts +54 -0
  322. package/src/tests/runtime-plugin-windows-file-url.fixture.ts +30 -0
  323. package/src/tests/runtime-plugin.fixture.ts +40 -0
  324. package/src/tests/runtime-plugin.test.ts +369 -0
  325. package/src/tests/scrollbox-culling-bug.test.ts +114 -0
  326. package/src/tests/scrollbox-hitgrid-resize.test.ts +136 -0
  327. package/src/tests/scrollbox-hitgrid.test.ts +909 -0
  328. package/src/tests/scrollbox.test.ts +1895 -0
  329. package/src/tests/wrap-resize-perf.test.ts +276 -0
  330. package/src/tests/yoga-api-coverage.test.ts +409 -0
  331. package/src/tests/yoga-callback-stress.test.ts +122 -0
  332. package/src/tests/yoga-ffi.test.ts +117 -0
  333. package/src/tests/yoga-setters.test.ts +921 -0
  334. package/src/tests/yoga-upstream/YGAlignBaselineTest.test.ts +124 -0
  335. package/src/tests/yoga-upstream/YGComputedBorderTest.test.ts +27 -0
  336. package/src/tests/yoga-upstream/YGComputedMarginTest.test.ts +27 -0
  337. package/src/tests/yoga-upstream/YGComputedPaddingTest.test.ts +27 -0
  338. package/src/tests/yoga-upstream/YGDirtiedTest.test.ts +157 -0
  339. package/src/tests/yoga-upstream/YGErrataTest.test.ts +40 -0
  340. package/src/tests/yoga-upstream/YGFlexBasisAuto.test.ts +23 -0
  341. package/src/tests/yoga-upstream/YGHasNewLayout.test.ts +81 -0
  342. package/src/tests/yoga-upstream/YGMeasureCacheTest.test.ts +31 -0
  343. package/src/tests/yoga-upstream/YGMeasureTest.test.ts +70 -0
  344. package/src/tests/yoga-upstream/generated/YGAbsolutePositionTest.test.ts +1864 -0
  345. package/src/tests/yoga-upstream/generated/YGAlignContentTest.test.ts +5829 -0
  346. package/src/tests/yoga-upstream/generated/YGAlignItemsTest.test.ts +2444 -0
  347. package/src/tests/yoga-upstream/generated/YGAlignSelfTest.test.ts +293 -0
  348. package/src/tests/yoga-upstream/generated/YGAndroidNewsFeed.test.ts +315 -0
  349. package/src/tests/yoga-upstream/generated/YGAspectRatioTest.test.ts +231 -0
  350. package/src/tests/yoga-upstream/generated/YGAutoTest.test.ts +347 -0
  351. package/src/tests/yoga-upstream/generated/YGBorderTest.test.ts +256 -0
  352. package/src/tests/yoga-upstream/generated/YGBoxSizingTest.test.ts +2747 -0
  353. package/src/tests/yoga-upstream/generated/YGDimensionTest.test.ts +129 -0
  354. package/src/tests/yoga-upstream/generated/YGDisplayContentsTest.test.ts +109 -0
  355. package/src/tests/yoga-upstream/generated/YGDisplayTest.test.ts +1097 -0
  356. package/src/tests/yoga-upstream/generated/YGFlexDirectionTest.test.ts +4562 -0
  357. package/src/tests/yoga-upstream/generated/YGFlexTest.test.ts +677 -0
  358. package/src/tests/yoga-upstream/generated/YGFlexWrapTest.test.ts +2071 -0
  359. package/src/tests/yoga-upstream/generated/YGGapTest.test.ts +3303 -0
  360. package/src/tests/yoga-upstream/generated/YGIntrinsicSizeTest.test.ts +582 -0
  361. package/src/tests/yoga-upstream/generated/YGJustifyContentTest.test.ts +2150 -0
  362. package/src/tests/yoga-upstream/generated/YGMarginTest.test.ts +1963 -0
  363. package/src/tests/yoga-upstream/generated/YGMinMaxDimensionTest.test.ts +1446 -0
  364. package/src/tests/yoga-upstream/generated/YGPaddingTest.test.ts +354 -0
  365. package/src/tests/yoga-upstream/generated/YGPercentageTest.test.ts +1760 -0
  366. package/src/tests/yoga-upstream/generated/YGRoundingTest.test.ts +1161 -0
  367. package/src/tests/yoga-upstream/generated/YGSizeOverflowTest.test.ts +207 -0
  368. package/src/tests/yoga-upstream/generated/YGStaticPositionTest.test.ts +6338 -0
  369. package/src/tests/yoga-upstream/tools/MeasureCounter.ts +54 -0
  370. package/src/tests/yoga-upstream/tools/utils.ts +81 -0
  371. package/src/text-buffer-view.test.ts +722 -0
  372. package/src/text-buffer-view.ts +193 -0
  373. package/src/text-buffer.test.ts +512 -0
  374. package/src/text-buffer.ts +250 -0
  375. package/src/types.ts +199 -0
  376. package/src/utils.ts +88 -0
  377. package/src/yoga.ts +1146 -0
  378. package/src/zig/ansi.zig +502 -0
  379. package/src/zig/audio.zig +1206 -0
  380. package/src/zig/bench/README.md +50 -0
  381. package/src/zig/bench/buffer-color-blending_bench.zig +350 -0
  382. package/src/zig/bench/buffer-draw-box_bench.zig +786 -0
  383. package/src/zig/bench/buffer-draw-text-buffer_bench.zig +894 -0
  384. package/src/zig/bench/edit-buffer_bench.zig +476 -0
  385. package/src/zig/bench/editor-view_bench.zig +169 -0
  386. package/src/zig/bench/native-span-feed_bench.zig +107 -0
  387. package/src/zig/bench/rope-markers_bench.zig +713 -0
  388. package/src/zig/bench/rope_bench.zig +514 -0
  389. package/src/zig/bench/styled-text_bench.zig +476 -0
  390. package/src/zig/bench/text-buffer-coords_bench.zig +362 -0
  391. package/src/zig/bench/text-buffer-view_bench.zig +459 -0
  392. package/src/zig/bench/text-chunk-graphemes_bench.zig +272 -0
  393. package/src/zig/bench/utf8_bench.zig +799 -0
  394. package/src/zig/bench-utils.zig +431 -0
  395. package/src/zig/bench.zig +223 -0
  396. package/src/zig/buffer-methods.zig +215 -0
  397. package/src/zig/buffer.zig +2515 -0
  398. package/src/zig/build.zig +700 -0
  399. package/src/zig/build.zig.zon +19 -0
  400. package/src/zig/edit-buffer.zig +900 -0
  401. package/src/zig/editor-view.zig +812 -0
  402. package/src/zig/event-bus.zig +28 -0
  403. package/src/zig/event-emitter.zig +65 -0
  404. package/src/zig/file-logger.zig +92 -0
  405. package/src/zig/grapheme.zig +838 -0
  406. package/src/zig/handles.zig +267 -0
  407. package/src/zig/lib.zig +2813 -0
  408. package/src/zig/link.zig +349 -0
  409. package/src/zig/logger.zig +43 -0
  410. package/src/zig/mem-registry.zig +126 -0
  411. package/src/zig/miniaudio_shim.c +21 -0
  412. package/src/zig/native-renderable.zig +112 -0
  413. package/src/zig/native-span-feed-bench-lib.zig +7 -0
  414. package/src/zig/native-span-feed.zig +747 -0
  415. package/src/zig/renderer-output.zig +677 -0
  416. package/src/zig/renderer.zig +2106 -0
  417. package/src/zig/rope.zig +1220 -0
  418. package/src/zig/split-scrollback.zig +88 -0
  419. package/src/zig/syntax-style.zig +174 -0
  420. package/src/zig/terminal.zig +1652 -0
  421. package/src/zig/test.zig +82 -0
  422. package/src/zig/tests/README.md +18 -0
  423. package/src/zig/tests/ansi_test.zig +18 -0
  424. package/src/zig/tests/audio_test.zig +439 -0
  425. package/src/zig/tests/buffer-methods_test.zig +1111 -0
  426. package/src/zig/tests/buffer_test.zig +2926 -0
  427. package/src/zig/tests/edit-buffer-history_test.zig +299 -0
  428. package/src/zig/tests/edit-buffer_test.zig +1688 -0
  429. package/src/zig/tests/editor-view_test.zig +3454 -0
  430. package/src/zig/tests/event-emitter_test.zig +249 -0
  431. package/src/zig/tests/grapheme_test.zig +1329 -0
  432. package/src/zig/tests/handles_test.zig +123 -0
  433. package/src/zig/tests/link_test.zig +220 -0
  434. package/src/zig/tests/mem-registry_test.zig +473 -0
  435. package/src/zig/tests/memory_leak_regression_test.zig +234 -0
  436. package/src/zig/tests/native-renderable_test.zig +16 -0
  437. package/src/zig/tests/native-span-feed_test.zig +1266 -0
  438. package/src/zig/tests/renderer_test.zig +2604 -0
  439. package/src/zig/tests/rope-nested_test.zig +712 -0
  440. package/src/zig/tests/rope_fuzz_test.zig +238 -0
  441. package/src/zig/tests/rope_test.zig +2362 -0
  442. package/src/zig/tests/segment-merge.test.zig +148 -0
  443. package/src/zig/tests/split-scrollback_test.zig +70 -0
  444. package/src/zig/tests/syntax-style_test.zig +558 -0
  445. package/src/zig/tests/terminal_test.zig +1489 -0
  446. package/src/zig/tests/test-renderer.zig +126 -0
  447. package/src/zig/tests/text-buffer-drawing_test.zig +3291 -0
  448. package/src/zig/tests/text-buffer-highlights_test.zig +706 -0
  449. package/src/zig/tests/text-buffer-iterators_test.zig +776 -0
  450. package/src/zig/tests/text-buffer-segment_test.zig +320 -0
  451. package/src/zig/tests/text-buffer-selection_test.zig +1056 -0
  452. package/src/zig/tests/text-buffer-selection_viewport_test.zig +358 -0
  453. package/src/zig/tests/text-buffer-view_test.zig +3672 -0
  454. package/src/zig/tests/text-buffer_test.zig +2191 -0
  455. package/src/zig/tests/unicode-width-map.zon +3909 -0
  456. package/src/zig/tests/utf8_no_zwj_test.zig +260 -0
  457. package/src/zig/tests/utf8_test.zig +4060 -0
  458. package/src/zig/tests/utf8_wcwidth_cursor_test.zig +267 -0
  459. package/src/zig/tests/utf8_wcwidth_test.zig +357 -0
  460. package/src/zig/tests/word-wrap-editing_test.zig +498 -0
  461. package/src/zig/tests/wrap-cache-perf_test.zig +113 -0
  462. package/src/zig/tests/yoga_test.zig +65 -0
  463. package/src/zig/text-buffer-iterators.zig +496 -0
  464. package/src/zig/text-buffer-segment.zig +403 -0
  465. package/src/zig/text-buffer-view.zig +1428 -0
  466. package/src/zig/text-buffer.zig +1272 -0
  467. package/src/zig/utf8.zig +1954 -0
  468. package/src/zig/utils.zig +12 -0
  469. package/src/zig/uucode-stub.zig +76 -0
  470. package/src/zig/vendor/miniaudio/miniaudio.h +93468 -0
  471. package/src/zig/yoga.zig +658 -0
  472. package/src/zig-structs.ts +381 -0
  473. package/src/zig.ts +5260 -0
  474. package/tsconfig.build.json +17 -0
  475. package/tsconfig.json +27 -0
  476. package/tsconfig.node-test.json +138 -0
@@ -0,0 +1,1137 @@
1
+ import { describe, expect, it, spyOn } from "bun:test"
2
+ import { TextNodeRenderable, isTextNodeRenderable } from "./TextNode.js"
3
+ import { RGBA } from "../lib/RGBA.js"
4
+ import { StyledText, red, bold, t } from "../lib/styled-text.js"
5
+
6
+ describe("TextNodeRenderable", () => {
7
+ describe("Constructor and Options", () => {
8
+ it("should create TextNode with default options", () => {
9
+ const node = new TextNodeRenderable({})
10
+
11
+ expect(node.fg).toBeUndefined()
12
+ expect(node.bg).toBeUndefined()
13
+ expect(node.attributes).toBe(0)
14
+ expect(node.children).toEqual([])
15
+ })
16
+
17
+ it("should create TextNode with custom options", () => {
18
+ const fgColor = RGBA.fromInts(255, 0, 0, 255)
19
+ const bgColor = RGBA.fromInts(0, 0, 255, 255)
20
+ const attributes = 1
21
+
22
+ const node = new TextNodeRenderable({
23
+ fg: fgColor,
24
+ bg: bgColor,
25
+ attributes,
26
+ })
27
+
28
+ expect(node.fg).toEqual(fgColor)
29
+ expect(node.bg).toEqual(bgColor)
30
+ expect(node.attributes).toBe(attributes)
31
+ })
32
+
33
+ it("should parse color strings in constructor", () => {
34
+ const node = new TextNodeRenderable({
35
+ fg: "#ff0000",
36
+ bg: "blue",
37
+ })
38
+
39
+ expect(node.fg?.r).toBe(1)
40
+ expect(node.fg?.g).toBe(0)
41
+ expect(node.fg?.b).toBe(0)
42
+ expect(node.fg?.a).toBe(1)
43
+ expect(node.bg?.r).toBe(0)
44
+ expect(node.bg?.g).toBe(0)
45
+ expect(node.bg?.b).toBe(1)
46
+ expect(node.bg?.a).toBe(1)
47
+ })
48
+
49
+ it("should preserve RGBA intent constructors in constructor and setters", () => {
50
+ const node = new TextNodeRenderable({
51
+ fg: RGBA.fromIndex(6),
52
+ bg: RGBA.defaultBackground(),
53
+ })
54
+
55
+ expect(node.fg).toBeDefined()
56
+ expect(node.bg).toBeDefined()
57
+ expect(node.fg).toBeInstanceOf(RGBA)
58
+ expect(node.bg).toBeInstanceOf(RGBA)
59
+ expect(node.fg!.intent).toBe("indexed")
60
+ expect(node.fg!.slot).toBe(6)
61
+ expect(node.bg!.intent).toBe("default")
62
+
63
+ node.fg = RGBA.defaultForeground()
64
+ node.bg = RGBA.fromIndex(4)
65
+
66
+ expect(node.fg).toBeInstanceOf(RGBA)
67
+ expect(node.bg).toBeInstanceOf(RGBA)
68
+ expect(node.fg!.intent).toBe("default")
69
+ expect(node.bg!.intent).toBe("indexed")
70
+ expect(node.bg!.slot).toBe(4)
71
+ })
72
+
73
+ it("should handle undefined colors", () => {
74
+ const node = new TextNodeRenderable({
75
+ fg: undefined,
76
+ bg: undefined,
77
+ })
78
+
79
+ expect(node.fg).toBeUndefined()
80
+ expect(node.bg).toBeUndefined()
81
+ })
82
+ })
83
+
84
+ describe("Type Guard", () => {
85
+ it("should identify TextNodeRenderable instances", () => {
86
+ const node = new TextNodeRenderable({})
87
+ const plainObject = {}
88
+
89
+ expect(isTextNodeRenderable(node)).toBe(true)
90
+ expect(isTextNodeRenderable(plainObject)).toBe(false)
91
+ expect(isTextNodeRenderable(null)).toBe(false)
92
+ expect(isTextNodeRenderable(undefined)).toBe(false)
93
+ })
94
+ })
95
+
96
+ describe("add Method", () => {
97
+ it("should add string child using add", () => {
98
+ const node = new TextNodeRenderable({})
99
+
100
+ const index = node.add("Hello")
101
+
102
+ expect(index).toBe(0)
103
+ expect(node.children).toEqual(["Hello"])
104
+ })
105
+
106
+ it("should add TextNode child using add", () => {
107
+ const parent = new TextNodeRenderable({})
108
+ const child = new TextNodeRenderable({})
109
+
110
+ const index = parent.add(child)
111
+
112
+ expect(index).toBe(0)
113
+ expect(parent.children).toEqual([child])
114
+ })
115
+
116
+ it("should add multiple children sequentially", () => {
117
+ const node = new TextNodeRenderable({})
118
+
119
+ node.add("First")
120
+ node.add("Second")
121
+ const childNode = new TextNodeRenderable({})
122
+ node.add(childNode)
123
+
124
+ expect(node.children).toEqual(["First", "Second", childNode])
125
+ })
126
+
127
+ it("should add child at specific index using add method", () => {
128
+ const node = new TextNodeRenderable({})
129
+ const child1 = new TextNodeRenderable({})
130
+ const child2 = new TextNodeRenderable({})
131
+
132
+ node.add("First")
133
+ node.add(child1, 0) // Insert at beginning
134
+ node.add(child2, 2) // Insert at end
135
+
136
+ expect(node.children).toEqual([child1, "First", child2])
137
+ })
138
+
139
+ it("should add string at specific index using add method", () => {
140
+ const node = new TextNodeRenderable({})
141
+ const child1 = new TextNodeRenderable({})
142
+
143
+ node.add("First")
144
+ node.add(child1, 0) // Insert at beginning
145
+ node.add("Middle", 1) // Insert in middle
146
+ node.add("Last") // Append at end
147
+
148
+ expect(node.children).toEqual([child1, "Middle", "First", "Last"])
149
+ })
150
+
151
+ it("should reject non-TextNode children in add method", () => {
152
+ const node = new TextNodeRenderable({})
153
+ const invalidChild = { id: "invalid" }
154
+
155
+ expect(() => {
156
+ node.add(invalidChild as any, 0)
157
+ }).toThrow("TextNodeRenderable only accepts strings, TextNodeRenderable instances, or StyledText instances")
158
+ })
159
+
160
+ it("should add StyledText child using add method", () => {
161
+ const node = new TextNodeRenderable({})
162
+ const styledText = new StyledText([
163
+ { __isChunk: true, text: "Hello", fg: RGBA.fromInts(255, 0, 0, 255), attributes: 1 },
164
+ { __isChunk: true, text: " World", fg: RGBA.fromInts(0, 255, 0, 255), attributes: 0 },
165
+ ])
166
+
167
+ const index = node.add(styledText)
168
+
169
+ expect(index).toBe(0)
170
+ expect(node.children).toHaveLength(2)
171
+ expect(node.children[0]).toBeInstanceOf(TextNodeRenderable)
172
+ expect(node.children[1]).toBeInstanceOf(TextNodeRenderable)
173
+
174
+ const firstChild = node.children[0] as TextNodeRenderable
175
+ const secondChild = node.children[1] as TextNodeRenderable
176
+
177
+ expect(firstChild.children).toEqual(["Hello"])
178
+ expect(firstChild.fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
179
+ expect(firstChild.attributes).toBe(1)
180
+
181
+ expect(secondChild.children).toEqual([" World"])
182
+ expect(secondChild.fg).toEqual(RGBA.fromInts(0, 255, 0, 255))
183
+ expect(secondChild.attributes).toBe(0)
184
+ })
185
+
186
+ it("should add StyledText child at specific index using add method", () => {
187
+ const node = new TextNodeRenderable({})
188
+ node.add("First")
189
+ node.add("Third")
190
+
191
+ const styledText = new StyledText([
192
+ { __isChunk: true, text: "Second", fg: RGBA.fromInts(255, 255, 0, 255), attributes: 2 },
193
+ ])
194
+
195
+ node.add(styledText, 1)
196
+
197
+ expect(node.children).toHaveLength(3)
198
+ expect(node.children[0]).toBe("First")
199
+ expect(node.children[1]).toBeInstanceOf(TextNodeRenderable)
200
+ expect(node.children[2]).toBe("Third")
201
+
202
+ const styledChild = node.children[1] as TextNodeRenderable
203
+ expect(styledChild.children).toEqual(["Second"])
204
+ expect(styledChild.fg).toEqual(RGBA.fromInts(255, 255, 0, 255))
205
+ expect(styledChild.attributes).toBe(2)
206
+ })
207
+ })
208
+
209
+ describe("insertBefore and remove Methods", () => {
210
+ it("should insert child before anchor node", () => {
211
+ const node = new TextNodeRenderable({})
212
+ const anchor = new TextNodeRenderable({})
213
+
214
+ node.add("First")
215
+ node.add(anchor)
216
+ node.add("Last")
217
+
218
+ node.insertBefore("Middle", anchor)
219
+
220
+ expect(node.children).toEqual(["First", "Middle", anchor, "Last"])
221
+ })
222
+
223
+ it("should throw error when anchor node not found in insertBefore", () => {
224
+ const node = new TextNodeRenderable({})
225
+ const anchor = new TextNodeRenderable({})
226
+
227
+ expect(() => {
228
+ node.insertBefore("Test", anchor)
229
+ }).toThrow("Anchor node not found in children")
230
+ })
231
+
232
+ it("should insert StyledText before anchor node", () => {
233
+ const node = new TextNodeRenderable({})
234
+ const anchor = new TextNodeRenderable({})
235
+ anchor.add("Anchor")
236
+
237
+ node.add("First")
238
+ node.add(anchor)
239
+ node.add("Last")
240
+
241
+ const styledText = new StyledText([
242
+ { __isChunk: true, text: "Middle", fg: RGBA.fromInts(128, 128, 128, 255), attributes: 4 },
243
+ ])
244
+
245
+ node.insertBefore(styledText, anchor)
246
+
247
+ expect(node.children).toHaveLength(4)
248
+ expect(node.children[0]).toBe("First")
249
+ expect(node.children[1]).toBeInstanceOf(TextNodeRenderable)
250
+ expect(node.children[2]).toBe(anchor)
251
+ expect(node.children[3]).toBe("Last")
252
+
253
+ const styledChild = node.children[1] as TextNodeRenderable
254
+ expect(styledChild.children).toEqual(["Middle"])
255
+ expect(styledChild.fg).toEqual(RGBA.fromInts(128, 128, 128, 255))
256
+ expect(styledChild.attributes).toBe(4)
257
+ })
258
+
259
+ it("should remove child from node", () => {
260
+ const node = new TextNodeRenderable({})
261
+ const child = new TextNodeRenderable({})
262
+
263
+ node.add("First")
264
+ node.add(child)
265
+ node.add("Last")
266
+
267
+ node.remove(child)
268
+
269
+ expect(node.children).toEqual(["First", "Last"])
270
+ expect(child.parent).toBeNull()
271
+ })
272
+
273
+ it("should remove duplicate-id children by first public match or exact object", () => {
274
+ const node = new TextNodeRenderable({})
275
+ const first = new TextNodeRenderable({ id: "duplicate" })
276
+ const second = new TextNodeRenderable({ id: "duplicate" })
277
+
278
+ node.add(first)
279
+ node.add(second)
280
+
281
+ expect(node.getRenderable("duplicate")).toBe(first)
282
+
283
+ node.remove(second)
284
+
285
+ expect(node.children).toHaveLength(1)
286
+ expect(node.children[0]).toBe(first)
287
+ expect(first.parent).toBe(node)
288
+ expect(second.parent).toBeNull()
289
+ expect(node.getRenderable("duplicate")).toBe(first)
290
+
291
+ const found = node.getRenderable("duplicate")
292
+ expect(found).toBe(first)
293
+ if (found) node.remove(found)
294
+
295
+ expect(node.children).toHaveLength(0)
296
+ expect(first.parent).toBeNull()
297
+ })
298
+
299
+ it("should warn in dev instead of throwing for a child not contained by the node", () => {
300
+ const node = new TextNodeRenderable({})
301
+ const child = new TextNodeRenderable({})
302
+
303
+ const warnSpy = spyOn(console, "warn").mockImplementation(() => {})
304
+ try {
305
+ expect(() => node.remove(child)).not.toThrow()
306
+ expect(warnSpy).toHaveBeenCalledTimes(1)
307
+ } finally {
308
+ warnSpy.mockRestore()
309
+ }
310
+ })
311
+
312
+ it("should not warn when removing an actual child", () => {
313
+ const node = new TextNodeRenderable({})
314
+ const child = new TextNodeRenderable({})
315
+ node.add(child)
316
+
317
+ const warnSpy = spyOn(console, "warn").mockImplementation(() => {})
318
+ try {
319
+ node.remove(child)
320
+ expect(warnSpy).not.toHaveBeenCalled()
321
+ } finally {
322
+ warnSpy.mockRestore()
323
+ }
324
+ })
325
+
326
+ it("should reject string ids at runtime", () => {
327
+ const node = new TextNodeRenderable({})
328
+ const child = new TextNodeRenderable({ id: "child" })
329
+ node.add(child)
330
+
331
+ expect(() => (node as any).remove("child")).toThrow("remove expects a TextNodeRenderable child object")
332
+ expect(node.children[0]).toBe(child)
333
+ })
334
+ })
335
+
336
+ describe("clear Method", () => {
337
+ it("should clear all children and change log", () => {
338
+ const node = new TextNodeRenderable({})
339
+
340
+ node.add("Test")
341
+ node.add(new TextNodeRenderable({}))
342
+
343
+ expect(node.children).toHaveLength(2)
344
+
345
+ node.clear()
346
+
347
+ expect(node.children).toEqual([])
348
+ })
349
+ })
350
+
351
+ describe("Style Inheritance and Merging", () => {
352
+ it("should merge styles with parent styles", () => {
353
+ const node = new TextNodeRenderable({
354
+ fg: RGBA.fromInts(255, 0, 0, 255),
355
+ attributes: 1,
356
+ })
357
+
358
+ const parentStyle = {
359
+ bg: RGBA.fromInts(0, 0, 255, 255),
360
+ attributes: 2,
361
+ }
362
+
363
+ const merged = node.mergeStyles(parentStyle)
364
+
365
+ expect(merged.fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
366
+ expect(merged.bg).toEqual(RGBA.fromInts(0, 0, 255, 255))
367
+ expect(merged.attributes).toBe(3)
368
+ })
369
+
370
+ it("should inherit undefined styles from parent", () => {
371
+ const node = new TextNodeRenderable({
372
+ fg: RGBA.fromInts(255, 0, 0, 255),
373
+ // bg and attributes undefined (attributes defaults to 0)
374
+ })
375
+
376
+ const parentStyle = {
377
+ bg: RGBA.fromInts(0, 0, 255, 255),
378
+ attributes: 2,
379
+ }
380
+
381
+ const merged = node.mergeStyles(parentStyle)
382
+
383
+ expect(merged.fg?.r).toBe(1)
384
+ expect(merged.fg?.g).toBe(0)
385
+ expect(merged.fg?.b).toBe(0)
386
+ expect(merged.bg?.r).toBe(0)
387
+ expect(merged.bg?.g).toBe(0)
388
+ expect(merged.bg?.b).toBe(1)
389
+ expect(merged.attributes).toBe(2)
390
+ })
391
+
392
+ it("should inherit nothing when parent has no styling", () => {
393
+ const node = new TextNodeRenderable({}) // No styles defined
394
+
395
+ const parentStyle = {
396
+ fg: undefined,
397
+ bg: undefined,
398
+ attributes: 0,
399
+ }
400
+
401
+ const merged = node.mergeStyles(parentStyle)
402
+
403
+ expect(merged.fg).toBeUndefined()
404
+ expect(merged.bg).toBeUndefined()
405
+ expect(merged.attributes).toBe(0)
406
+ })
407
+
408
+ it("should combine attributes using bitwise OR", () => {
409
+ // Test various attribute combinations
410
+ const testCases = [
411
+ { nodeAttrs: 0, parentAttrs: 0, expected: 0 }, // 0 | 0 = 0
412
+ { nodeAttrs: 1, parentAttrs: 0, expected: 1 }, // 1 | 0 = 1 (bold)
413
+ { nodeAttrs: 0, parentAttrs: 2, expected: 2 }, // 0 | 2 = 2 (italic)
414
+ { nodeAttrs: 1, parentAttrs: 2, expected: 3 }, // 1 | 2 = 3 (bold + italic)
415
+ { nodeAttrs: 3, parentAttrs: 4, expected: 7 }, // 3 | 4 = 7 (bold + italic + underline)
416
+ { nodeAttrs: 7, parentAttrs: 8, expected: 15 }, // 7 | 8 = 15 (all previous + strikethrough)
417
+ ]
418
+
419
+ testCases.forEach(({ nodeAttrs, parentAttrs, expected }) => {
420
+ const node = new TextNodeRenderable({ attributes: nodeAttrs })
421
+ const parentStyle = { fg: undefined, bg: undefined, attributes: parentAttrs }
422
+
423
+ const merged = node.mergeStyles(parentStyle)
424
+ expect(merged.attributes).toBe(expected)
425
+ })
426
+ })
427
+ })
428
+
429
+ describe("gatherWithInheritedStyle Method", () => {
430
+ it("should gather chunks with inherited styles", () => {
431
+ const node = new TextNodeRenderable({
432
+ fg: RGBA.fromInts(255, 0, 0, 255),
433
+ bg: RGBA.fromInts(0, 0, 255, 255),
434
+ attributes: 1,
435
+ })
436
+
437
+ node.add("Hello")
438
+ node.add(" ")
439
+ node.add("World")
440
+
441
+ const chunks = node.gatherWithInheritedStyle()
442
+
443
+ expect(chunks).toHaveLength(3)
444
+ chunks.forEach((chunk) => {
445
+ expect(chunk.fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
446
+ expect(chunk.bg).toEqual(RGBA.fromInts(0, 0, 255, 255))
447
+ expect(chunk.attributes).toBe(1)
448
+ })
449
+
450
+ expect(chunks[0].text).toBe("Hello")
451
+ expect(chunks[1].text).toBe(" ")
452
+ expect(chunks[2].text).toBe("World")
453
+ })
454
+
455
+ it("should recursively gather from child TextNodes", () => {
456
+ const parent = new TextNodeRenderable({
457
+ fg: RGBA.fromInts(255, 0, 0, 255),
458
+ })
459
+
460
+ const child = new TextNodeRenderable({
461
+ bg: RGBA.fromInts(0, 255, 0, 255),
462
+ })
463
+
464
+ child.add("Child")
465
+ parent.add("Parent")
466
+ parent.add(child)
467
+
468
+ const chunks = parent.gatherWithInheritedStyle()
469
+
470
+ expect(chunks).toHaveLength(2)
471
+ expect(chunks[0].text).toBe("Parent")
472
+ expect(chunks[0].fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
473
+ expect(chunks[0].bg).toBeUndefined()
474
+
475
+ expect(chunks[1].text).toBe("Child")
476
+ expect(chunks[1].fg).toEqual(RGBA.fromInts(255, 0, 0, 255)) // Inherited from parent
477
+ expect(chunks[1].bg).toEqual(RGBA.fromInts(0, 255, 0, 255)) // Own style
478
+ })
479
+
480
+ it("should inherit nothing when parent has no default styling", () => {
481
+ const parent = new TextNodeRenderable({}) // No styles
482
+
483
+ const child = new TextNodeRenderable({}) // No styles
484
+ child.add("Child")
485
+
486
+ parent.add("Parent")
487
+ parent.add(child)
488
+
489
+ const chunks = parent.gatherWithInheritedStyle()
490
+
491
+ expect(chunks).toHaveLength(2)
492
+ expect(chunks[0].text).toBe("Parent")
493
+ expect(chunks[0].fg).toBeUndefined()
494
+ expect(chunks[0].bg).toBeUndefined()
495
+ expect(chunks[0].attributes).toBe(0)
496
+
497
+ expect(chunks[1].text).toBe("Child")
498
+ expect(chunks[1].fg).toBeUndefined() // Nothing inherited
499
+ expect(chunks[1].bg).toBeUndefined() // Nothing inherited
500
+ expect(chunks[1].attributes).toBe(0) // Nothing inherited
501
+ })
502
+
503
+ it("should allow children to override parent styles independently", () => {
504
+ const parent = new TextNodeRenderable({
505
+ fg: RGBA.fromInts(255, 0, 0, 255),
506
+ bg: RGBA.fromInts(0, 0, 255, 255),
507
+ attributes: 1,
508
+ })
509
+
510
+ const childOverrideFg = new TextNodeRenderable({
511
+ fg: RGBA.fromInts(0, 255, 0, 255),
512
+ })
513
+ childOverrideFg.add("Green Text")
514
+
515
+ const childOverrideBg = new TextNodeRenderable({
516
+ bg: RGBA.fromInts(255, 255, 0, 255),
517
+ })
518
+ childOverrideBg.add("Yellow BG")
519
+
520
+ const childOverrideAttrs = new TextNodeRenderable({
521
+ attributes: 2,
522
+ })
523
+ childOverrideAttrs.add("Italic")
524
+
525
+ parent.add(childOverrideFg)
526
+ parent.add(childOverrideBg)
527
+ parent.add(childOverrideAttrs)
528
+
529
+ const chunks = parent.gatherWithInheritedStyle()
530
+
531
+ expect(chunks).toHaveLength(3)
532
+
533
+ // First child: overrides fg, inherits bg and attributes
534
+ expect(chunks[0].text).toBe("Green Text")
535
+ expect(chunks[0].fg).toEqual(RGBA.fromInts(0, 255, 0, 255)) // Overridden
536
+ expect(chunks[0].bg).toEqual(RGBA.fromInts(0, 0, 255, 255)) // Inherited
537
+ expect(chunks[0].attributes).toBe(1) // Inherited
538
+
539
+ // Second child: overrides bg, inherits fg and attributes
540
+ expect(chunks[1].text).toBe("Yellow BG")
541
+ expect(chunks[1].fg).toEqual(RGBA.fromInts(255, 0, 0, 255)) // Inherited
542
+ expect(chunks[1].bg).toEqual(RGBA.fromInts(255, 255, 0, 255)) // Overridden
543
+ expect(chunks[1].attributes).toBe(1) // Inherited
544
+
545
+ // Third child: overrides attributes (OR'd), inherits fg and bg
546
+ expect(chunks[2].text).toBe("Italic")
547
+ expect(chunks[2].fg).toEqual(RGBA.fromInts(255, 0, 0, 255)) // Inherited
548
+ expect(chunks[2].bg).toEqual(RGBA.fromInts(0, 0, 255, 255)) // Inherited
549
+ expect(chunks[2].attributes).toBe(3) // 1 | 2 = 3
550
+ })
551
+
552
+ it("should support multi-level inheritance (grandparent -> parent -> child)", () => {
553
+ const grandparent = new TextNodeRenderable({
554
+ fg: RGBA.fromInts(255, 0, 0, 255),
555
+ attributes: 1,
556
+ })
557
+
558
+ const parent = new TextNodeRenderable({
559
+ bg: RGBA.fromInts(0, 0, 255, 255),
560
+ })
561
+
562
+ const child = new TextNodeRenderable({
563
+ fg: RGBA.fromInts(0, 255, 0, 255),
564
+ attributes: 2,
565
+ })
566
+
567
+ child.add("Grandchild")
568
+ parent.add("Parent")
569
+ parent.add(child)
570
+ grandparent.add("Grandparent")
571
+ grandparent.add(parent)
572
+
573
+ const chunks = grandparent.gatherWithInheritedStyle()
574
+
575
+ expect(chunks).toHaveLength(3)
576
+
577
+ expect(chunks[0].text).toBe("Grandparent")
578
+ expect(chunks[0].fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
579
+ expect(chunks[0].bg).toBeUndefined()
580
+ expect(chunks[0].attributes).toBe(1)
581
+
582
+ expect(chunks[1].text).toBe("Parent")
583
+ expect(chunks[1].fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
584
+ expect(chunks[1].bg).toEqual(RGBA.fromInts(0, 0, 255, 255))
585
+ expect(chunks[1].attributes).toBe(1)
586
+
587
+ expect(chunks[2].text).toBe("Grandchild")
588
+ expect(chunks[2].fg).toEqual(RGBA.fromInts(0, 255, 0, 255))
589
+ expect(chunks[2].bg).toEqual(RGBA.fromInts(0, 0, 255, 255))
590
+ expect(chunks[2].attributes).toBe(3)
591
+ })
592
+
593
+ it("should support partial style overrides in children", () => {
594
+ const parent = new TextNodeRenderable({
595
+ fg: RGBA.fromInts(255, 0, 0, 255),
596
+ bg: RGBA.fromInts(0, 0, 255, 255),
597
+ attributes: 1,
598
+ })
599
+
600
+ const childPartial1 = new TextNodeRenderable({
601
+ fg: RGBA.fromInts(0, 255, 0, 255),
602
+ })
603
+ childPartial1.add("Green on Blue")
604
+
605
+ const childPartial2 = new TextNodeRenderable({
606
+ bg: RGBA.fromInts(255, 255, 0, 255),
607
+ })
608
+ childPartial2.add("Red on Yellow")
609
+
610
+ const childPartial3 = new TextNodeRenderable({
611
+ attributes: 2,
612
+ })
613
+ childPartial3.add("Red on Blue Bold+Italic")
614
+
615
+ const childPartial4 = new TextNodeRenderable({
616
+ fg: RGBA.fromInts(255, 255, 255, 255),
617
+ attributes: 4,
618
+ })
619
+ childPartial4.add("White on Blue Bold+Underline")
620
+
621
+ parent.add(childPartial1)
622
+ parent.add(childPartial2)
623
+ parent.add(childPartial3)
624
+ parent.add(childPartial4)
625
+
626
+ const chunks = parent.gatherWithInheritedStyle()
627
+
628
+ expect(chunks).toHaveLength(4)
629
+
630
+ // Child 1: only fg overridden
631
+ expect(chunks[0].text).toBe("Green on Blue")
632
+ expect(chunks[0].fg).toEqual(RGBA.fromInts(0, 255, 0, 255))
633
+ expect(chunks[0].bg).toEqual(RGBA.fromInts(0, 0, 255, 255))
634
+ expect(chunks[0].attributes).toBe(1)
635
+
636
+ // Child 2: only bg overridden
637
+ expect(chunks[1].text).toBe("Red on Yellow")
638
+ expect(chunks[1].fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
639
+ expect(chunks[1].bg).toEqual(RGBA.fromInts(255, 255, 0, 255))
640
+ expect(chunks[1].attributes).toBe(1)
641
+
642
+ // Child 3: only attributes overridden (OR'd)
643
+ expect(chunks[2].text).toBe("Red on Blue Bold+Italic")
644
+ expect(chunks[2].fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
645
+ expect(chunks[2].bg).toEqual(RGBA.fromInts(0, 0, 255, 255))
646
+ expect(chunks[2].attributes).toBe(3) // 1 | 2 = 3
647
+
648
+ // Child 4: fg and attributes overridden, bg inherited
649
+ expect(chunks[3].text).toBe("White on Blue Bold+Underline")
650
+ expect(chunks[3].fg).toEqual(RGBA.fromInts(255, 255, 255, 255))
651
+ expect(chunks[3].bg).toEqual(RGBA.fromInts(0, 0, 255, 255))
652
+ expect(chunks[3].attributes).toBe(5) // 1 | 4 = 5
653
+ })
654
+ })
655
+
656
+ describe("Static Factory Methods", () => {
657
+ it("should create TextNode from string using fromString", () => {
658
+ const node = TextNodeRenderable.fromString("Hello World", {
659
+ fg: "#ff0000",
660
+ attributes: 1,
661
+ })
662
+
663
+ expect(node.children).toEqual(["Hello World"])
664
+ expect(node.fg?.r).toBe(1)
665
+ expect(node.fg?.g).toBe(0)
666
+ expect(node.fg?.b).toBe(0)
667
+ expect(node.attributes).toBe(1)
668
+ })
669
+
670
+ it("should create TextNode from nodes using fromNodes", () => {
671
+ const child1 = new TextNodeRenderable({})
672
+ const child2 = new TextNodeRenderable({})
673
+
674
+ child1.add("First")
675
+ child2.add("Second")
676
+
677
+ const parent = TextNodeRenderable.fromNodes([child1, child2], {
678
+ fg: RGBA.fromInts(255, 255, 0, 255),
679
+ })
680
+
681
+ expect(parent.children).toEqual([child1, child2])
682
+ expect(parent.fg).toEqual(RGBA.fromInts(255, 255, 0, 255))
683
+ })
684
+ })
685
+
686
+ describe("Utility Methods", () => {
687
+ it("should convert to chunks using toChunks", () => {
688
+ const node = new TextNodeRenderable({
689
+ fg: "#00ff00",
690
+ })
691
+
692
+ node.add("Test")
693
+
694
+ const chunks = node.toChunks()
695
+
696
+ expect(chunks).toHaveLength(1)
697
+ expect(chunks[0].text).toBe("Test")
698
+ expect(chunks[0].fg).toBeInstanceOf(RGBA)
699
+ expect((chunks[0].fg as RGBA).r).toBe(0)
700
+ expect((chunks[0].fg as RGBA).g).toBe(1)
701
+ expect((chunks[0].fg as RGBA).b).toBe(0)
702
+ })
703
+
704
+ it("should get children using getChildren", () => {
705
+ const node = new TextNodeRenderable({})
706
+
707
+ node.add("String child")
708
+ const textNodeChild = new TextNodeRenderable({})
709
+ node.add(textNodeChild)
710
+
711
+ const children = node.getChildren()
712
+
713
+ expect(children).toHaveLength(1)
714
+ expect(children[0]).toBe(textNodeChild)
715
+ })
716
+
717
+ it("should get children count", () => {
718
+ const node = new TextNodeRenderable({})
719
+
720
+ expect(node.getChildrenCount()).toBe(0)
721
+
722
+ node.add("First")
723
+ expect(node.getChildrenCount()).toBe(1)
724
+
725
+ node.add(new TextNodeRenderable({}))
726
+ expect(node.getChildrenCount()).toBe(2)
727
+ })
728
+
729
+ it("should find renderable by id", () => {
730
+ const node = new TextNodeRenderable({})
731
+
732
+ const child1 = new TextNodeRenderable({ id: "child1" })
733
+ const child2 = new TextNodeRenderable({ id: "child2" })
734
+
735
+ node.add(child1)
736
+ node.add(child2)
737
+
738
+ expect(node.getRenderable("child1")).toBe(child1)
739
+ expect(node.getRenderable("child2")).toBe(child2)
740
+ expect(node.getRenderable("nonexistent")).toBeUndefined()
741
+ })
742
+ })
743
+
744
+ describe("StyledText Integration", () => {
745
+ it("should work with template literal styled text", () => {
746
+ const node = new TextNodeRenderable({})
747
+ const styled = t`Hello ${red("World")} with ${bold("bold")} text!`
748
+
749
+ node.add(styled)
750
+
751
+ expect(node.children).toHaveLength(5) // All parts become TextNodeRenderable instances
752
+ expect(node.children[0]).toBeInstanceOf(TextNodeRenderable)
753
+ expect(node.children[1]).toBeInstanceOf(TextNodeRenderable)
754
+ expect(node.children[2]).toBeInstanceOf(TextNodeRenderable)
755
+ expect(node.children[3]).toBeInstanceOf(TextNodeRenderable)
756
+ expect(node.children[4]).toBeInstanceOf(TextNodeRenderable)
757
+
758
+ // Check first chunk: "Hello " (no styling)
759
+ const helloChild = node.children[0] as TextNodeRenderable
760
+ expect(helloChild.children).toEqual(["Hello "])
761
+ expect(helloChild.fg).toBeUndefined()
762
+ expect(helloChild.attributes).toBe(0)
763
+
764
+ // Check second chunk: "World" (red styling)
765
+ const redChild = node.children[1] as TextNodeRenderable
766
+ expect(redChild.children).toEqual(["World"])
767
+ expect(redChild.fg?.r).toBe(1)
768
+ expect(redChild.fg?.g).toBe(0)
769
+ expect(redChild.fg?.b).toBe(0)
770
+ expect(redChild.attributes).toBe(0)
771
+
772
+ // Check third chunk: " with " (no styling)
773
+ const withChild = node.children[2] as TextNodeRenderable
774
+ expect(withChild.children).toEqual([" with "])
775
+ expect(withChild.fg).toBeUndefined()
776
+ expect(withChild.attributes).toBe(0)
777
+
778
+ // Check fourth chunk: "bold" (bold styling)
779
+ const boldChild = node.children[3] as TextNodeRenderable
780
+ expect(boldChild.children).toEqual(["bold"])
781
+ expect(boldChild.fg).toBeUndefined()
782
+ expect(boldChild.attributes).toBe(1) // bold attribute
783
+
784
+ // Check fifth chunk: " text!" (no styling)
785
+ const textChild = node.children[4] as TextNodeRenderable
786
+ expect(textChild.children).toEqual([" text!"])
787
+ expect(textChild.fg).toBeUndefined()
788
+ expect(textChild.attributes).toBe(0)
789
+ })
790
+
791
+ it("should preserve styles when converting StyledText to TextNodes", () => {
792
+ const node = new TextNodeRenderable({})
793
+ const styledText = new StyledText([
794
+ {
795
+ __isChunk: true,
796
+ text: "Red",
797
+ fg: RGBA.fromInts(255, 0, 0, 255),
798
+ bg: RGBA.fromInts(0, 0, 0, 255),
799
+ attributes: 1,
800
+ },
801
+ { __isChunk: true, text: "Blue", fg: RGBA.fromInts(0, 0, 255, 255), attributes: 2 },
802
+ { __isChunk: true, text: "Green", fg: RGBA.fromInts(0, 255, 0, 255), attributes: 0 },
803
+ ])
804
+
805
+ node.add(styledText)
806
+
807
+ expect(node.children).toHaveLength(3)
808
+
809
+ const redNode = node.children[0] as TextNodeRenderable
810
+ expect(redNode.children).toEqual(["Red"])
811
+ expect(redNode.fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
812
+ expect(redNode.bg).toEqual(RGBA.fromInts(0, 0, 0, 255))
813
+ expect(redNode.attributes).toBe(1)
814
+
815
+ const blueNode = node.children[1] as TextNodeRenderable
816
+ expect(blueNode.children).toEqual(["Blue"])
817
+ expect(blueNode.fg).toEqual(RGBA.fromInts(0, 0, 255, 255))
818
+ expect(blueNode.bg).toBeUndefined()
819
+ expect(blueNode.attributes).toBe(2)
820
+
821
+ const greenNode = node.children[2] as TextNodeRenderable
822
+ expect(greenNode.children).toEqual(["Green"])
823
+ expect(greenNode.fg).toEqual(RGBA.fromInts(0, 255, 0, 255))
824
+ expect(greenNode.bg).toBeUndefined()
825
+ expect(greenNode.attributes).toBe(0)
826
+ })
827
+
828
+ it("should handle empty StyledText", () => {
829
+ const node = new TextNodeRenderable({})
830
+ const emptyStyledText = new StyledText([])
831
+
832
+ // Add empty StyledText
833
+ const index = node.add(emptyStyledText)
834
+ expect(index).toBe(0)
835
+
836
+ // Should have no children since empty StyledText produces no TextNodes
837
+ expect(node.children).toHaveLength(0)
838
+
839
+ // Verify that gatherWithInheritedStyle returns empty array
840
+ const chunks = node.gatherWithInheritedStyle()
841
+ expect(chunks).toHaveLength(0)
842
+ })
843
+
844
+ it("should handle StyledText with empty text chunks", () => {
845
+ const node = new TextNodeRenderable({})
846
+ const styledTextWithEmptyChunks = new StyledText([
847
+ { __isChunk: true, text: "", fg: RGBA.fromInts(255, 0, 0, 255), attributes: 1 },
848
+ { __isChunk: true, text: "middle", fg: RGBA.fromInts(0, 255, 0, 255), attributes: 0 },
849
+ { __isChunk: true, text: "", fg: RGBA.fromInts(0, 0, 255, 255), attributes: 2 },
850
+ ])
851
+
852
+ node.add(styledTextWithEmptyChunks)
853
+
854
+ expect(node.children).toHaveLength(3)
855
+
856
+ // First chunk: empty text with red styling
857
+ const emptyRedNode = node.children[0] as TextNodeRenderable
858
+ expect(emptyRedNode.children).toEqual([""])
859
+ expect(emptyRedNode.fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
860
+ expect(emptyRedNode.attributes).toBe(1)
861
+
862
+ // Second chunk: "middle" with green styling
863
+ const middleNode = node.children[1] as TextNodeRenderable
864
+ expect(middleNode.children).toEqual(["middle"])
865
+ expect(middleNode.fg).toEqual(RGBA.fromInts(0, 255, 0, 255))
866
+ expect(middleNode.attributes).toBe(0)
867
+
868
+ // Third chunk: empty text with blue styling
869
+ const emptyBlueNode = node.children[2] as TextNodeRenderable
870
+ expect(emptyBlueNode.children).toEqual([""])
871
+ expect(emptyBlueNode.fg).toEqual(RGBA.fromInts(0, 0, 255, 255))
872
+ expect(emptyBlueNode.attributes).toBe(2)
873
+ })
874
+ })
875
+
876
+ describe("Link Inheritance", () => {
877
+ it("should inherit link from parent to child", () => {
878
+ const parent = new TextNodeRenderable({
879
+ link: { url: "https://opentui.com" },
880
+ })
881
+
882
+ const child = new TextNodeRenderable({})
883
+ child.add("Child text")
884
+
885
+ parent.add("Parent text")
886
+ parent.add(child)
887
+
888
+ const chunks = parent.gatherWithInheritedStyle()
889
+
890
+ expect(chunks).toHaveLength(2)
891
+ expect(chunks[0].text).toBe("Parent text")
892
+ expect(chunks[0].link?.url).toBe("https://opentui.com")
893
+
894
+ expect(chunks[1].text).toBe("Child text")
895
+ expect(chunks[1].link?.url).toBe("https://opentui.com")
896
+ })
897
+
898
+ it("should allow child to override parent link", () => {
899
+ const parent = new TextNodeRenderable({
900
+ link: { url: "https://parent.com" },
901
+ })
902
+
903
+ const child = new TextNodeRenderable({
904
+ link: { url: "https://child.com" },
905
+ })
906
+ child.add("Child text")
907
+
908
+ parent.add("Parent text")
909
+ parent.add(child)
910
+
911
+ const chunks = parent.gatherWithInheritedStyle()
912
+
913
+ expect(chunks).toHaveLength(2)
914
+ expect(chunks[0].link?.url).toBe("https://parent.com")
915
+ expect(chunks[1].link?.url).toBe("https://child.com")
916
+ })
917
+
918
+ it("should inherit link through multiple nesting levels", () => {
919
+ const grandparent = new TextNodeRenderable({
920
+ link: { url: "https://example.com" },
921
+ })
922
+
923
+ const parent = new TextNodeRenderable({})
924
+ const child = new TextNodeRenderable({})
925
+
926
+ child.add("Grandchild")
927
+ parent.add(child)
928
+ grandparent.add(parent)
929
+
930
+ const chunks = grandparent.gatherWithInheritedStyle()
931
+
932
+ expect(chunks).toHaveLength(1)
933
+ expect(chunks[0].text).toBe("Grandchild")
934
+ expect(chunks[0].link?.url).toBe("https://example.com")
935
+ })
936
+
937
+ it("should merge link with other styles", () => {
938
+ const parent = new TextNodeRenderable({
939
+ fg: RGBA.fromInts(255, 0, 0, 255),
940
+ attributes: 1,
941
+ link: { url: "https://opentui.com" },
942
+ })
943
+
944
+ const child = new TextNodeRenderable({
945
+ bg: RGBA.fromInts(0, 0, 255, 255),
946
+ attributes: 2,
947
+ })
948
+ child.add("Styled linked text")
949
+
950
+ parent.add(child)
951
+
952
+ const chunks = parent.gatherWithInheritedStyle()
953
+
954
+ expect(chunks).toHaveLength(1)
955
+ expect(chunks[0].text).toBe("Styled linked text")
956
+ expect(chunks[0].fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
957
+ expect(chunks[0].bg).toEqual(RGBA.fromInts(0, 0, 255, 255))
958
+ expect(chunks[0].attributes).toBe(3) // 1 | 2
959
+ expect(chunks[0].link?.url).toBe("https://opentui.com")
960
+ })
961
+
962
+ it("should handle undefined link in parent", () => {
963
+ const parent = new TextNodeRenderable({})
964
+
965
+ const child = new TextNodeRenderable({
966
+ link: { url: "https://child.com" },
967
+ })
968
+ child.add("Child with link")
969
+
970
+ parent.add("Parent without link")
971
+ parent.add(child)
972
+
973
+ const chunks = parent.gatherWithInheritedStyle()
974
+
975
+ expect(chunks).toHaveLength(2)
976
+ expect(chunks[0].link).toBeUndefined()
977
+ expect(chunks[1].link?.url).toBe("https://child.com")
978
+ })
979
+
980
+ it("should preserve link when merging styles", () => {
981
+ const node = new TextNodeRenderable({
982
+ link: { url: "https://example.com" },
983
+ attributes: 1,
984
+ })
985
+
986
+ const parentStyle = {
987
+ fg: RGBA.fromInts(255, 0, 0, 255),
988
+ bg: undefined,
989
+ attributes: 2,
990
+ }
991
+
992
+ const merged = node.mergeStyles(parentStyle)
993
+
994
+ expect(merged.link?.url).toBe("https://example.com")
995
+ expect(merged.fg).toEqual(RGBA.fromInts(255, 0, 0, 255))
996
+ expect(merged.attributes).toBe(3)
997
+ })
998
+
999
+ it("should inherit link when node has no link", () => {
1000
+ const node = new TextNodeRenderable({
1001
+ fg: RGBA.fromInts(0, 255, 0, 255),
1002
+ })
1003
+
1004
+ const parentStyle = {
1005
+ fg: undefined,
1006
+ bg: undefined,
1007
+ attributes: 0,
1008
+ link: { url: "https://inherited.com" },
1009
+ }
1010
+
1011
+ const merged = node.mergeStyles(parentStyle)
1012
+
1013
+ expect(merged.link?.url).toBe("https://inherited.com")
1014
+ expect(merged.fg).toEqual(RGBA.fromInts(0, 255, 0, 255))
1015
+ })
1016
+
1017
+ it("should handle complex link inheritance tree", () => {
1018
+ // Grandparent with link
1019
+ const grandparent = new TextNodeRenderable({
1020
+ link: { url: "https://grandparent.com" },
1021
+ fg: RGBA.fromInts(255, 0, 0, 255),
1022
+ })
1023
+
1024
+ // Parent inherits link, adds bg
1025
+ const parent = new TextNodeRenderable({
1026
+ bg: RGBA.fromInts(0, 0, 255, 255),
1027
+ })
1028
+
1029
+ // Child1 inherits link, overrides fg
1030
+ const child1 = new TextNodeRenderable({
1031
+ fg: RGBA.fromInts(0, 255, 0, 255),
1032
+ })
1033
+ child1.add("Child1")
1034
+
1035
+ // Child2 overrides link
1036
+ const child2 = new TextNodeRenderable({
1037
+ link: { url: "https://child2.com" },
1038
+ })
1039
+ child2.add("Child2")
1040
+
1041
+ // Child3 has no link set, should inherit from parent
1042
+ const child3 = new TextNodeRenderable({
1043
+ attributes: 1,
1044
+ })
1045
+ child3.add("Child3")
1046
+
1047
+ parent.add(child1)
1048
+ parent.add(child2)
1049
+ parent.add(child3)
1050
+ grandparent.add(parent)
1051
+
1052
+ const chunks = grandparent.gatherWithInheritedStyle()
1053
+
1054
+ expect(chunks).toHaveLength(3)
1055
+
1056
+ // Child1: inherits link from grandparent
1057
+ expect(chunks[0].text).toBe("Child1")
1058
+ expect(chunks[0].link?.url).toBe("https://grandparent.com")
1059
+ expect(chunks[0].fg).toEqual(RGBA.fromInts(0, 255, 0, 255))
1060
+
1061
+ // Child2: overrides link
1062
+ expect(chunks[1].text).toBe("Child2")
1063
+ expect(chunks[1].link?.url).toBe("https://child2.com")
1064
+
1065
+ // Child3: inherits link from grandparent
1066
+ expect(chunks[2].text).toBe("Child3")
1067
+ expect(chunks[2].link?.url).toBe("https://grandparent.com")
1068
+ })
1069
+ })
1070
+
1071
+ describe("Edge Cases and Error Handling", () => {
1072
+ it("should handle empty strings", () => {
1073
+ const node = new TextNodeRenderable({})
1074
+
1075
+ node.add("")
1076
+ node.add(" ")
1077
+
1078
+ const chunks = node.gatherWithInheritedStyle()
1079
+ expect(chunks).toHaveLength(2)
1080
+ expect(chunks[0].text).toBe("")
1081
+ expect(chunks[1].text).toBe(" ")
1082
+ })
1083
+
1084
+ it("should handle nested empty TextNodes", () => {
1085
+ const parent = new TextNodeRenderable({})
1086
+ const child = new TextNodeRenderable({})
1087
+
1088
+ parent.add(child)
1089
+
1090
+ const chunks = parent.gatherWithInheritedStyle()
1091
+ expect(chunks).toHaveLength(0)
1092
+ })
1093
+
1094
+ it("should handle multiple operations in sequence", () => {
1095
+ const node = new TextNodeRenderable({})
1096
+
1097
+ // Add multiple items
1098
+ for (let i = 0; i < 5; i++) {
1099
+ node.add(`Item ${i}`)
1100
+ }
1101
+
1102
+ expect(node.children).toHaveLength(5)
1103
+
1104
+ // Clear and verify
1105
+ node.clear()
1106
+ expect(node.children).toHaveLength(0)
1107
+ })
1108
+
1109
+ it("should efficiently calculate positions for large trees", () => {
1110
+ const root = new TextNodeRenderable({})
1111
+
1112
+ // Add many children to test position calculation efficiency
1113
+ for (let i = 0; i < 10; i++) {
1114
+ const child = new TextNodeRenderable({})
1115
+ for (let j = 0; j < 5; j++) {
1116
+ child.add(`Child ${i}-${j}`)
1117
+ }
1118
+ root.add(child)
1119
+ }
1120
+
1121
+ // Insert before the 5th child - should be efficient
1122
+ const fifthChild = root.children[4] as TextNodeRenderable
1123
+ const insertChild = new TextNodeRenderable({})
1124
+ insertChild.add("Inserted")
1125
+
1126
+ const startTime = performance.now()
1127
+ root.insertBefore(insertChild, fifthChild)
1128
+ const endTime = performance.now()
1129
+
1130
+ // Should complete quickly (less than 1ms for this small tree)
1131
+ expect(endTime - startTime).toBeLessThan(1)
1132
+
1133
+ // Verify correct position
1134
+ expect(root.children.indexOf(insertChild)).toBe(4)
1135
+ })
1136
+ })
1137
+ })