@google/gemini-cli 0.10.0-preview.1 → 0.11.0-nightly.20251020.a96f0659

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 (417) hide show
  1. package/README.md +9 -0
  2. package/dist/google-gemini-cli-0.11.0-nightly.20251015.203bad7c.tgz +0 -0
  3. package/dist/package.json +2 -2
  4. package/dist/src/commands/extensions/examples/mcp-server/example.d.ts +6 -0
  5. package/dist/src/commands/extensions/examples/mcp-server/example.js +46 -0
  6. package/dist/src/commands/extensions/examples/mcp-server/example.js.map +1 -0
  7. package/dist/src/commands/extensions/install.d.ts +1 -0
  8. package/dist/src/commands/extensions/install.js +15 -2
  9. package/dist/src/commands/extensions/install.js.map +1 -1
  10. package/dist/src/commands/extensions/list.js +3 -2
  11. package/dist/src/commands/extensions/list.js.map +1 -1
  12. package/dist/src/commands/extensions/update.js +2 -2
  13. package/dist/src/commands/extensions/update.js.map +1 -1
  14. package/dist/src/commands/mcp/add.test.d.ts +6 -0
  15. package/dist/src/commands/mcp/add.test.js +234 -0
  16. package/dist/src/commands/mcp/add.test.js.map +1 -0
  17. package/dist/src/commands/mcp/list.js +5 -3
  18. package/dist/src/commands/mcp/list.js.map +1 -1
  19. package/dist/src/commands/mcp/list.test.d.ts +6 -0
  20. package/dist/src/commands/mcp/list.test.js +117 -0
  21. package/dist/src/commands/mcp/list.test.js.map +1 -0
  22. package/dist/src/commands/mcp/remove.test.d.ts +6 -0
  23. package/dist/src/commands/mcp/remove.test.js +175 -0
  24. package/dist/src/commands/mcp/remove.test.js.map +1 -0
  25. package/dist/src/commands/mcp.test.d.ts +6 -0
  26. package/dist/src/commands/mcp.test.js +62 -0
  27. package/dist/src/commands/mcp.test.js.map +1 -0
  28. package/dist/src/config/auth.js +3 -1
  29. package/dist/src/config/auth.js.map +1 -1
  30. package/dist/src/config/auth.test.js +3 -1
  31. package/dist/src/config/auth.test.js.map +1 -1
  32. package/dist/src/config/config.d.ts +0 -11
  33. package/dist/src/config/config.integration.test.d.ts +6 -0
  34. package/dist/src/config/config.integration.test.js +351 -0
  35. package/dist/src/config/config.integration.test.js.map +1 -0
  36. package/dist/src/config/config.js +19 -74
  37. package/dist/src/config/config.js.map +1 -1
  38. package/dist/src/config/config.test.d.ts +6 -0
  39. package/dist/src/config/config.test.js +2001 -0
  40. package/dist/src/config/config.test.js.map +1 -0
  41. package/dist/src/config/extension.d.ts +1 -4
  42. package/dist/src/config/extension.js +64 -69
  43. package/dist/src/config/extension.js.map +1 -1
  44. package/dist/src/config/extension.test.d.ts +6 -0
  45. package/dist/src/config/extension.test.js +1176 -0
  46. package/dist/src/config/extension.test.js.map +1 -0
  47. package/dist/src/config/extensions/extensionEnablement.d.ts +1 -1
  48. package/dist/src/config/extensions/extensionEnablement.js +4 -3
  49. package/dist/src/config/extensions/extensionEnablement.js.map +1 -1
  50. package/dist/src/config/extensions/extensionEnablement.test.js +21 -18
  51. package/dist/src/config/extensions/extensionEnablement.test.js.map +1 -1
  52. package/dist/src/config/extensions/github.d.ts +15 -7
  53. package/dist/src/config/extensions/github.js +95 -21
  54. package/dist/src/config/extensions/github.js.map +1 -1
  55. package/dist/src/config/extensions/github.test.js +14 -12
  56. package/dist/src/config/extensions/github.test.js.map +1 -1
  57. package/dist/src/config/extensions/update.test.js +9 -9
  58. package/dist/src/config/extensions/update.test.js.map +1 -1
  59. package/dist/src/config/keyBindings.d.ts +2 -1
  60. package/dist/src/config/keyBindings.js +4 -2
  61. package/dist/src/config/keyBindings.js.map +1 -1
  62. package/dist/src/config/policy.js +13 -14
  63. package/dist/src/config/policy.js.map +1 -1
  64. package/dist/src/config/policy.test.js +3 -3
  65. package/dist/src/config/policy.test.js.map +1 -1
  66. package/dist/src/config/sandboxConfig.d.ts +0 -1
  67. package/dist/src/config/sandboxConfig.js +1 -3
  68. package/dist/src/config/sandboxConfig.js.map +1 -1
  69. package/dist/src/config/settings.test.d.ts +6 -0
  70. package/dist/src/config/settings.test.js +1937 -0
  71. package/dist/src/config/settings.test.js.map +1 -0
  72. package/dist/src/gemini.js +12 -10
  73. package/dist/src/gemini.js.map +1 -1
  74. package/dist/src/gemini.test.js +34 -11
  75. package/dist/src/gemini.test.js.map +1 -1
  76. package/dist/src/generated/git-commit.d.ts +2 -2
  77. package/dist/src/generated/git-commit.js +2 -2
  78. package/dist/src/generated/git-commit.js.map +1 -1
  79. package/dist/src/nonInteractiveCli.js +91 -3
  80. package/dist/src/nonInteractiveCli.js.map +1 -1
  81. package/dist/src/nonInteractiveCli.test.d.ts +6 -0
  82. package/dist/src/nonInteractiveCli.test.js +711 -0
  83. package/dist/src/nonInteractiveCli.test.js.map +1 -0
  84. package/dist/src/services/FileCommandLoader.test.d.ts +6 -0
  85. package/dist/src/services/FileCommandLoader.test.js +971 -0
  86. package/dist/src/services/FileCommandLoader.test.js.map +1 -0
  87. package/dist/src/services/prompt-processors/argumentProcessor.test.d.ts +6 -0
  88. package/dist/src/services/prompt-processors/argumentProcessor.test.js +40 -0
  89. package/dist/src/services/prompt-processors/argumentProcessor.test.js.map +1 -0
  90. package/dist/src/services/prompt-processors/shellProcessor.test.d.ts +6 -0
  91. package/dist/src/services/prompt-processors/shellProcessor.test.js +482 -0
  92. package/dist/src/services/prompt-processors/shellProcessor.test.js.map +1 -0
  93. package/dist/src/test-utils/render.d.ts +2 -1
  94. package/dist/src/test-utils/render.js +5 -2
  95. package/dist/src/test-utils/render.js.map +1 -1
  96. package/dist/src/ui/App.test.d.ts +6 -0
  97. package/dist/src/ui/App.test.js +110 -0
  98. package/dist/src/ui/App.test.js.map +1 -0
  99. package/dist/src/ui/AppContainer.js +39 -26
  100. package/dist/src/ui/AppContainer.js.map +1 -1
  101. package/dist/src/ui/AppContainer.test.js +35 -9
  102. package/dist/src/ui/AppContainer.test.js.map +1 -1
  103. package/dist/src/ui/auth/AuthDialog.d.ts +1 -1
  104. package/dist/src/ui/auth/AuthDialog.js +3 -1
  105. package/dist/src/ui/auth/AuthDialog.js.map +1 -1
  106. package/dist/src/ui/auth/useAuth.d.ts +1 -1
  107. package/dist/src/ui/auth/useAuth.js +3 -1
  108. package/dist/src/ui/auth/useAuth.js.map +1 -1
  109. package/dist/src/ui/commands/aboutCommand.js +1 -1
  110. package/dist/src/ui/commands/aboutCommand.test.d.ts +6 -0
  111. package/dist/src/ui/commands/aboutCommand.test.js +130 -0
  112. package/dist/src/ui/commands/aboutCommand.test.js.map +1 -0
  113. package/dist/src/ui/commands/authCommand.js +1 -1
  114. package/dist/src/ui/commands/authCommand.test.d.ts +6 -0
  115. package/dist/src/ui/commands/authCommand.test.js +30 -0
  116. package/dist/src/ui/commands/authCommand.test.js.map +1 -0
  117. package/dist/src/ui/commands/bugCommand.js +1 -1
  118. package/dist/src/ui/commands/bugCommand.test.d.ts +6 -0
  119. package/dist/src/ui/commands/bugCommand.test.js +105 -0
  120. package/dist/src/ui/commands/bugCommand.test.js.map +1 -0
  121. package/dist/src/ui/commands/chatCommand.js +1 -1
  122. package/dist/src/ui/commands/chatCommand.js.map +1 -1
  123. package/dist/src/ui/commands/chatCommand.test.d.ts +6 -0
  124. package/dist/src/ui/commands/chatCommand.test.js +555 -0
  125. package/dist/src/ui/commands/chatCommand.test.js.map +1 -0
  126. package/dist/src/ui/commands/clearCommand.js +1 -1
  127. package/dist/src/ui/commands/clearCommand.test.d.ts +6 -0
  128. package/dist/src/ui/commands/clearCommand.test.js +76 -0
  129. package/dist/src/ui/commands/clearCommand.test.js.map +1 -0
  130. package/dist/src/ui/commands/compressCommand.js +1 -1
  131. package/dist/src/ui/commands/compressCommand.js.map +1 -1
  132. package/dist/src/ui/commands/compressCommand.test.d.ts +6 -0
  133. package/dist/src/ui/commands/compressCommand.test.js +98 -0
  134. package/dist/src/ui/commands/compressCommand.test.js.map +1 -0
  135. package/dist/src/ui/commands/copyCommand.test.d.ts +6 -0
  136. package/dist/src/ui/commands/copyCommand.test.js +242 -0
  137. package/dist/src/ui/commands/copyCommand.test.js.map +1 -0
  138. package/dist/src/ui/commands/corgiCommand.js +1 -1
  139. package/dist/src/ui/commands/corgiCommand.js.map +1 -1
  140. package/dist/src/ui/commands/corgiCommand.test.d.ts +6 -0
  141. package/dist/src/ui/commands/corgiCommand.test.js +28 -0
  142. package/dist/src/ui/commands/corgiCommand.test.js.map +1 -0
  143. package/dist/src/ui/commands/directoryCommand.test.d.ts +6 -0
  144. package/dist/src/ui/commands/directoryCommand.test.js +145 -0
  145. package/dist/src/ui/commands/directoryCommand.test.js.map +1 -0
  146. package/dist/src/ui/commands/docsCommand.js +1 -1
  147. package/dist/src/ui/commands/docsCommand.test.d.ts +6 -0
  148. package/dist/src/ui/commands/docsCommand.test.js +72 -0
  149. package/dist/src/ui/commands/docsCommand.test.js.map +1 -0
  150. package/dist/src/ui/commands/editorCommand.js +1 -1
  151. package/dist/src/ui/commands/editorCommand.test.d.ts +6 -0
  152. package/dist/src/ui/commands/editorCommand.test.js +27 -0
  153. package/dist/src/ui/commands/editorCommand.test.js.map +1 -0
  154. package/dist/src/ui/commands/extensionsCommand.test.d.ts +6 -0
  155. package/dist/src/ui/commands/extensionsCommand.test.js +241 -0
  156. package/dist/src/ui/commands/extensionsCommand.test.js.map +1 -0
  157. package/dist/src/ui/commands/helpCommand.js +1 -1
  158. package/dist/src/ui/commands/helpCommand.test.d.ts +6 -0
  159. package/dist/src/ui/commands/helpCommand.test.js +42 -0
  160. package/dist/src/ui/commands/helpCommand.test.js.map +1 -0
  161. package/dist/src/ui/commands/ideCommand.js +6 -6
  162. package/dist/src/ui/commands/ideCommand.test.d.ts +6 -0
  163. package/dist/src/ui/commands/ideCommand.test.js +203 -0
  164. package/dist/src/ui/commands/ideCommand.test.js.map +1 -0
  165. package/dist/src/ui/commands/initCommand.js +1 -1
  166. package/dist/src/ui/commands/initCommand.js.map +1 -1
  167. package/dist/src/ui/commands/initCommand.test.d.ts +6 -0
  168. package/dist/src/ui/commands/initCommand.test.js +80 -0
  169. package/dist/src/ui/commands/initCommand.test.js.map +1 -0
  170. package/dist/src/ui/commands/mcpCommand.js +98 -88
  171. package/dist/src/ui/commands/mcpCommand.js.map +1 -1
  172. package/dist/src/ui/commands/mcpCommand.test.d.ts +6 -0
  173. package/dist/src/ui/commands/mcpCommand.test.js +148 -0
  174. package/dist/src/ui/commands/mcpCommand.test.js.map +1 -0
  175. package/dist/src/ui/commands/memoryCommand.js +5 -5
  176. package/dist/src/ui/commands/memoryCommand.js.map +1 -1
  177. package/dist/src/ui/commands/memoryCommand.test.d.ts +6 -0
  178. package/dist/src/ui/commands/memoryCommand.test.js +266 -0
  179. package/dist/src/ui/commands/memoryCommand.test.js.map +1 -0
  180. package/dist/src/ui/commands/privacyCommand.js +1 -1
  181. package/dist/src/ui/commands/privacyCommand.test.d.ts +6 -0
  182. package/dist/src/ui/commands/privacyCommand.test.js +32 -0
  183. package/dist/src/ui/commands/privacyCommand.test.js.map +1 -0
  184. package/dist/src/ui/commands/quitCommand.js +1 -1
  185. package/dist/src/ui/commands/quitCommand.test.d.ts +6 -0
  186. package/dist/src/ui/commands/quitCommand.test.js +50 -0
  187. package/dist/src/ui/commands/quitCommand.test.js.map +1 -0
  188. package/dist/src/ui/commands/restoreCommand.test.d.ts +6 -0
  189. package/dist/src/ui/commands/restoreCommand.test.js +190 -0
  190. package/dist/src/ui/commands/restoreCommand.test.js.map +1 -0
  191. package/dist/src/ui/commands/settingsCommand.test.d.ts +6 -0
  192. package/dist/src/ui/commands/settingsCommand.test.js +30 -0
  193. package/dist/src/ui/commands/settingsCommand.test.js.map +1 -0
  194. package/dist/src/ui/commands/setupGithubCommand.test.js +1 -2
  195. package/dist/src/ui/commands/setupGithubCommand.test.js.map +1 -1
  196. package/dist/src/ui/commands/statsCommand.js +3 -3
  197. package/dist/src/ui/commands/statsCommand.js.map +1 -1
  198. package/dist/src/ui/commands/statsCommand.test.d.ts +6 -0
  199. package/dist/src/ui/commands/statsCommand.test.js +53 -0
  200. package/dist/src/ui/commands/statsCommand.test.js.map +1 -0
  201. package/dist/src/ui/commands/terminalSetupCommand.test.d.ts +6 -0
  202. package/dist/src/ui/commands/terminalSetupCommand.test.js +66 -0
  203. package/dist/src/ui/commands/terminalSetupCommand.test.js.map +1 -0
  204. package/dist/src/ui/commands/themeCommand.js +1 -1
  205. package/dist/src/ui/commands/themeCommand.test.d.ts +6 -0
  206. package/dist/src/ui/commands/themeCommand.test.js +32 -0
  207. package/dist/src/ui/commands/themeCommand.test.js.map +1 -0
  208. package/dist/src/ui/commands/toolsCommand.js +1 -1
  209. package/dist/src/ui/commands/toolsCommand.test.d.ts +6 -0
  210. package/dist/src/ui/commands/toolsCommand.test.js +100 -0
  211. package/dist/src/ui/commands/toolsCommand.test.js.map +1 -0
  212. package/dist/src/ui/commands/vimCommand.js +1 -1
  213. package/dist/src/ui/components/Composer.js +5 -3
  214. package/dist/src/ui/components/Composer.js.map +1 -1
  215. package/dist/src/ui/components/Composer.test.js +16 -1
  216. package/dist/src/ui/components/Composer.test.js.map +1 -1
  217. package/dist/src/ui/components/ContextSummaryDisplay.d.ts +0 -1
  218. package/dist/src/ui/components/ContextSummaryDisplay.js +2 -12
  219. package/dist/src/ui/components/ContextSummaryDisplay.js.map +1 -1
  220. package/dist/src/ui/components/ContextSummaryDisplay.test.d.ts +6 -0
  221. package/dist/src/ui/components/ContextSummaryDisplay.test.js +66 -0
  222. package/dist/src/ui/components/ContextSummaryDisplay.test.js.map +1 -0
  223. package/dist/src/ui/components/DialogManager.js +1 -5
  224. package/dist/src/ui/components/DialogManager.js.map +1 -1
  225. package/dist/src/ui/components/EditorSettingsDialog.js +1 -1
  226. package/dist/src/ui/components/EditorSettingsDialog.js.map +1 -1
  227. package/dist/src/ui/components/FolderTrustDialog.test.js +7 -3
  228. package/dist/src/ui/components/FolderTrustDialog.test.js.map +1 -1
  229. package/dist/src/ui/components/Footer.js +1 -1
  230. package/dist/src/ui/components/Footer.js.map +1 -1
  231. package/dist/src/ui/components/Footer.test.d.ts +6 -0
  232. package/dist/src/ui/components/Footer.test.js +231 -0
  233. package/dist/src/ui/components/Footer.test.js.map +1 -0
  234. package/dist/src/ui/components/InputPrompt.d.ts +4 -0
  235. package/dist/src/ui/components/InputPrompt.js +53 -4
  236. package/dist/src/ui/components/InputPrompt.js.map +1 -1
  237. package/dist/src/ui/components/InputPrompt.test.d.ts +6 -0
  238. package/dist/src/ui/components/InputPrompt.test.js +1716 -0
  239. package/dist/src/ui/components/InputPrompt.test.js.map +1 -0
  240. package/dist/src/ui/components/ModelStatsDisplay.test.d.ts +6 -0
  241. package/dist/src/ui/components/ModelStatsDisplay.test.js +285 -0
  242. package/dist/src/ui/components/ModelStatsDisplay.test.js.map +1 -0
  243. package/dist/src/ui/components/PermissionsModifyTrustDialog.js +22 -18
  244. package/dist/src/ui/components/PermissionsModifyTrustDialog.js.map +1 -1
  245. package/dist/src/ui/components/PermissionsModifyTrustDialog.test.js +10 -2
  246. package/dist/src/ui/components/PermissionsModifyTrustDialog.test.js.map +1 -1
  247. package/dist/src/ui/components/QueuedMessageDisplay.js +3 -3
  248. package/dist/src/ui/components/QueuedMessageDisplay.js.map +1 -1
  249. package/dist/src/ui/components/QueuedMessageDisplay.test.js +4 -0
  250. package/dist/src/ui/components/QueuedMessageDisplay.test.js.map +1 -1
  251. package/dist/src/ui/components/RawMarkdownIndicator.d.ts +7 -0
  252. package/dist/src/ui/components/RawMarkdownIndicator.js +8 -0
  253. package/dist/src/ui/components/RawMarkdownIndicator.js.map +1 -0
  254. package/dist/src/ui/components/SessionSummaryDisplay.test.d.ts +6 -0
  255. package/dist/src/ui/components/SessionSummaryDisplay.test.js +74 -0
  256. package/dist/src/ui/components/SessionSummaryDisplay.test.js.map +1 -0
  257. package/dist/src/ui/components/SettingsDialog.js +8 -8
  258. package/dist/src/ui/components/SettingsDialog.js.map +1 -1
  259. package/dist/src/ui/components/SettingsDialog.test.js +188 -56
  260. package/dist/src/ui/components/SettingsDialog.test.js.map +1 -1
  261. package/dist/src/ui/components/StatsDisplay.test.d.ts +6 -0
  262. package/dist/src/ui/components/StatsDisplay.test.js +351 -0
  263. package/dist/src/ui/components/StatsDisplay.test.js.map +1 -0
  264. package/dist/src/ui/components/ThemeDialog.d.ts +4 -2
  265. package/dist/src/ui/components/ThemeDialog.js +3 -3
  266. package/dist/src/ui/components/ThemeDialog.js.map +1 -1
  267. package/dist/src/ui/components/ThemeDialog.test.js +13 -0
  268. package/dist/src/ui/components/ThemeDialog.test.js.map +1 -1
  269. package/dist/src/ui/components/ToolStatsDisplay.test.d.ts +6 -0
  270. package/dist/src/ui/components/ToolStatsDisplay.test.js +227 -0
  271. package/dist/src/ui/components/ToolStatsDisplay.test.js.map +1 -0
  272. package/dist/src/ui/components/messages/GeminiMessage.js +3 -1
  273. package/dist/src/ui/components/messages/GeminiMessage.js.map +1 -1
  274. package/dist/src/ui/components/messages/GeminiMessage.test.d.ts +6 -0
  275. package/dist/src/ui/components/messages/GeminiMessage.test.js +35 -0
  276. package/dist/src/ui/components/messages/GeminiMessage.test.js.map +1 -0
  277. package/dist/src/ui/components/messages/GeminiMessageContent.js +3 -1
  278. package/dist/src/ui/components/messages/GeminiMessageContent.js.map +1 -1
  279. package/dist/src/ui/components/messages/Todo.d.ts +7 -0
  280. package/dist/src/ui/components/messages/Todo.js +59 -0
  281. package/dist/src/ui/components/messages/Todo.js.map +1 -0
  282. package/dist/src/ui/components/messages/Todo.test.d.ts +6 -0
  283. package/dist/src/ui/components/messages/Todo.test.js +113 -0
  284. package/dist/src/ui/components/messages/Todo.test.js.map +1 -0
  285. package/dist/src/ui/components/messages/ToolGroupMessage.js +1 -1
  286. package/dist/src/ui/components/messages/ToolGroupMessage.js.map +1 -1
  287. package/dist/src/ui/components/messages/ToolMessage.js +8 -3
  288. package/dist/src/ui/components/messages/ToolMessage.js.map +1 -1
  289. package/dist/src/ui/components/messages/ToolMessage.test.js +2 -2
  290. package/dist/src/ui/components/messages/ToolMessage.test.js.map +1 -1
  291. package/dist/src/ui/components/messages/ToolMessageRawMarkdown.test.d.ts +6 -0
  292. package/dist/src/ui/components/messages/ToolMessageRawMarkdown.test.js +30 -0
  293. package/dist/src/ui/components/messages/ToolMessageRawMarkdown.test.js.map +1 -0
  294. package/dist/src/ui/components/messages/UserShellMessage.js +1 -1
  295. package/dist/src/ui/components/messages/UserShellMessage.js.map +1 -1
  296. package/dist/src/ui/components/shared/BaseSelectionList.test.js +1 -1
  297. package/dist/src/ui/components/shared/BaseSelectionList.test.js.map +1 -1
  298. package/dist/src/ui/components/shared/text-buffer.test.d.ts +6 -0
  299. package/dist/src/ui/components/shared/text-buffer.test.js +1554 -0
  300. package/dist/src/ui/components/shared/text-buffer.test.js.map +1 -0
  301. package/dist/src/ui/components/shared/vim-buffer-actions.test.d.ts +6 -0
  302. package/dist/src/ui/components/shared/vim-buffer-actions.test.js +951 -0
  303. package/dist/src/ui/components/shared/vim-buffer-actions.test.js.map +1 -0
  304. package/dist/src/ui/components/views/McpStatus.d.ts +0 -1
  305. package/dist/src/ui/components/views/McpStatus.js +2 -2
  306. package/dist/src/ui/components/views/McpStatus.js.map +1 -1
  307. package/dist/src/ui/components/views/McpStatus.test.js +0 -5
  308. package/dist/src/ui/components/views/McpStatus.test.js.map +1 -1
  309. package/dist/src/ui/components/views/ToolsList.test.js +4 -4
  310. package/dist/src/ui/components/views/ToolsList.test.js.map +1 -1
  311. package/dist/src/ui/contexts/KeypressContext.d.ts +1 -0
  312. package/dist/src/ui/contexts/KeypressContext.js +176 -50
  313. package/dist/src/ui/contexts/KeypressContext.js.map +1 -1
  314. package/dist/src/ui/contexts/KeypressContext.test.js +413 -14
  315. package/dist/src/ui/contexts/KeypressContext.test.js.map +1 -1
  316. package/dist/src/ui/contexts/SessionContext.test.d.ts +6 -0
  317. package/dist/src/ui/contexts/SessionContext.test.js +177 -0
  318. package/dist/src/ui/contexts/SessionContext.test.js.map +1 -0
  319. package/dist/src/ui/contexts/UIActionsContext.d.ts +5 -4
  320. package/dist/src/ui/contexts/UIActionsContext.js.map +1 -1
  321. package/dist/src/ui/contexts/UIStateContext.d.ts +3 -3
  322. package/dist/src/ui/contexts/UIStateContext.js.map +1 -1
  323. package/dist/src/ui/hooks/slashCommandProcessor.test.d.ts +6 -0
  324. package/dist/src/ui/hooks/slashCommandProcessor.test.js +779 -0
  325. package/dist/src/ui/hooks/slashCommandProcessor.test.js.map +1 -0
  326. package/dist/src/ui/hooks/useAtCompletion.js +2 -2
  327. package/dist/src/ui/hooks/useAtCompletion.js.map +1 -1
  328. package/dist/src/ui/hooks/useAtCompletion.test.d.ts +6 -0
  329. package/dist/src/ui/hooks/useAtCompletion.test.js +385 -0
  330. package/dist/src/ui/hooks/useAtCompletion.test.js.map +1 -0
  331. package/dist/src/ui/hooks/useAutoAcceptIndicator.test.js +0 -1
  332. package/dist/src/ui/hooks/useAutoAcceptIndicator.test.js.map +1 -1
  333. package/dist/src/ui/hooks/useCommandCompletion.d.ts +1 -1
  334. package/dist/src/ui/hooks/useCommandCompletion.js +5 -3
  335. package/dist/src/ui/hooks/useCommandCompletion.js.map +1 -1
  336. package/dist/src/ui/hooks/useCommandCompletion.test.d.ts +6 -0
  337. package/dist/src/ui/hooks/useCommandCompletion.test.js +375 -0
  338. package/dist/src/ui/hooks/useCommandCompletion.test.js.map +1 -0
  339. package/dist/src/ui/hooks/useConsoleMessages.test.d.ts +6 -0
  340. package/dist/src/ui/hooks/useConsoleMessages.test.js +110 -0
  341. package/dist/src/ui/hooks/useConsoleMessages.test.js.map +1 -0
  342. package/dist/src/ui/hooks/useExtensionUpdates.test.js +3 -3
  343. package/dist/src/ui/hooks/useExtensionUpdates.test.js.map +1 -1
  344. package/dist/src/ui/hooks/useFocus.test.d.ts +6 -0
  345. package/dist/src/ui/hooks/useFocus.test.js +115 -0
  346. package/dist/src/ui/hooks/useFocus.test.js.map +1 -0
  347. package/dist/src/ui/hooks/useFolderTrust.test.d.ts +6 -0
  348. package/dist/src/ui/hooks/useFolderTrust.test.js +164 -0
  349. package/dist/src/ui/hooks/useFolderTrust.test.js.map +1 -0
  350. package/dist/src/ui/hooks/useGeminiStream.js +33 -31
  351. package/dist/src/ui/hooks/useGeminiStream.js.map +1 -1
  352. package/dist/src/ui/hooks/useGeminiStream.test.d.ts +6 -0
  353. package/dist/src/ui/hooks/useGeminiStream.test.js +1936 -0
  354. package/dist/src/ui/hooks/useGeminiStream.test.js.map +1 -0
  355. package/dist/src/ui/hooks/useKeypress.test.d.ts +6 -0
  356. package/dist/src/ui/hooks/useKeypress.test.js +234 -0
  357. package/dist/src/ui/hooks/useKeypress.test.js.map +1 -0
  358. package/dist/src/ui/hooks/useLoadingIndicator.test.js +5 -0
  359. package/dist/src/ui/hooks/useLoadingIndicator.test.js.map +1 -1
  360. package/dist/src/ui/hooks/useMessageQueue.d.ts +1 -0
  361. package/dist/src/ui/hooks/useMessageQueue.js +14 -0
  362. package/dist/src/ui/hooks/useMessageQueue.js.map +1 -1
  363. package/dist/src/ui/hooks/useMessageQueue.test.js +121 -0
  364. package/dist/src/ui/hooks/useMessageQueue.test.js.map +1 -1
  365. package/dist/src/ui/hooks/usePhraseCycler.d.ts +1 -0
  366. package/dist/src/ui/hooks/usePhraseCycler.js +156 -5
  367. package/dist/src/ui/hooks/usePhraseCycler.js.map +1 -1
  368. package/dist/src/ui/hooks/usePhraseCycler.test.d.ts +6 -0
  369. package/dist/src/ui/hooks/usePhraseCycler.test.js +155 -0
  370. package/dist/src/ui/hooks/usePhraseCycler.test.js.map +1 -0
  371. package/dist/src/ui/hooks/useThemeCommand.d.ts +2 -1
  372. package/dist/src/ui/hooks/useThemeCommand.js +6 -0
  373. package/dist/src/ui/hooks/useThemeCommand.js.map +1 -1
  374. package/dist/src/ui/hooks/useToolScheduler.test.js +3 -0
  375. package/dist/src/ui/hooks/useToolScheduler.test.js.map +1 -1
  376. package/dist/src/ui/hooks/vim.test.d.ts +6 -0
  377. package/dist/src/ui/hooks/vim.test.js +1389 -0
  378. package/dist/src/ui/hooks/vim.test.js.map +1 -0
  379. package/dist/src/ui/keyMatchers.test.js +9 -3
  380. package/dist/src/ui/keyMatchers.test.js.map +1 -1
  381. package/dist/src/ui/themes/theme.test.d.ts +6 -0
  382. package/dist/src/ui/themes/theme.test.js +85 -0
  383. package/dist/src/ui/themes/theme.test.js.map +1 -0
  384. package/dist/src/ui/types.d.ts +0 -1
  385. package/dist/src/ui/types.js.map +1 -1
  386. package/dist/src/ui/utils/CodeColorizer.d.ts +1 -1
  387. package/dist/src/ui/utils/CodeColorizer.js +4 -2
  388. package/dist/src/ui/utils/CodeColorizer.js.map +1 -1
  389. package/dist/src/ui/utils/MarkdownDisplay.d.ts +1 -0
  390. package/dist/src/ui/utils/MarkdownDisplay.js +8 -1
  391. package/dist/src/ui/utils/MarkdownDisplay.js.map +1 -1
  392. package/dist/src/ui/utils/commandUtils.js +18 -2
  393. package/dist/src/ui/utils/commandUtils.js.map +1 -1
  394. package/dist/src/ui/utils/commandUtils.test.js +61 -6
  395. package/dist/src/ui/utils/commandUtils.test.js.map +1 -1
  396. package/dist/src/ui/utils/updateCheck.d.ts +2 -1
  397. package/dist/src/ui/utils/updateCheck.js +4 -1
  398. package/dist/src/ui/utils/updateCheck.js.map +1 -1
  399. package/dist/src/ui/utils/updateCheck.test.js +25 -10
  400. package/dist/src/ui/utils/updateCheck.test.js.map +1 -1
  401. package/dist/src/utils/errors.d.ts +1 -0
  402. package/dist/src/utils/errors.js +66 -5
  403. package/dist/src/utils/errors.js.map +1 -1
  404. package/dist/src/validateNonInterActiveAuth.test.d.ts +6 -0
  405. package/dist/src/validateNonInterActiveAuth.test.js +336 -0
  406. package/dist/src/validateNonInterActiveAuth.test.js.map +1 -0
  407. package/dist/src/zed-integration/zedIntegration.js +1 -2
  408. package/dist/src/zed-integration/zedIntegration.js.map +1 -1
  409. package/dist/tsconfig.tsbuildinfo +1 -1
  410. package/package.json +3 -3
  411. package/dist/google-gemini-cli-0.10.0-preview.0.tgz +0 -0
  412. package/dist/src/ui/components/WorkspaceMigrationDialog.d.ts +0 -11
  413. package/dist/src/ui/components/WorkspaceMigrationDialog.js +0 -44
  414. package/dist/src/ui/components/WorkspaceMigrationDialog.js.map +0 -1
  415. package/dist/src/ui/hooks/useWorkspaceMigration.d.ts +0 -13
  416. package/dist/src/ui/hooks/useWorkspaceMigration.js +0 -59
  417. package/dist/src/ui/hooks/useWorkspaceMigration.js.map +0 -1
@@ -0,0 +1,1554 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { describe, it, expect, beforeEach } from 'vitest';
7
+ import stripAnsi from 'strip-ansi';
8
+ import { renderHook, act } from '@testing-library/react';
9
+ import { useTextBuffer, offsetToLogicalPos, logicalPosToOffset, textBufferReducer, findWordEndInLine, findNextWordStartInLine, isWordCharStrict, } from './text-buffer.js';
10
+ import { cpLen } from '../../utils/textUtils.js';
11
+ const defaultVisualLayout = {
12
+ visualLines: [''],
13
+ logicalToVisualMap: [[[0, 0]]],
14
+ visualToLogicalMap: [[0, 0]],
15
+ };
16
+ const initialState = {
17
+ lines: [''],
18
+ cursorRow: 0,
19
+ cursorCol: 0,
20
+ preferredCol: null,
21
+ undoStack: [],
22
+ redoStack: [],
23
+ clipboard: null,
24
+ selectionAnchor: null,
25
+ viewportWidth: 80,
26
+ viewportHeight: 24,
27
+ visualLayout: defaultVisualLayout,
28
+ };
29
+ describe('textBufferReducer', () => {
30
+ it('should return the initial state if state is undefined', () => {
31
+ const action = { type: 'unknown_action' };
32
+ const state = textBufferReducer(initialState, action);
33
+ expect(state).toHaveOnlyValidCharacters();
34
+ expect(state).toEqual(initialState);
35
+ });
36
+ describe('set_text action', () => {
37
+ it('should set new text and move cursor to the end', () => {
38
+ const action = {
39
+ type: 'set_text',
40
+ payload: 'hello\nworld',
41
+ };
42
+ const state = textBufferReducer(initialState, action);
43
+ expect(state).toHaveOnlyValidCharacters();
44
+ expect(state.lines).toEqual(['hello', 'world']);
45
+ expect(state.cursorRow).toBe(1);
46
+ expect(state.cursorCol).toBe(5);
47
+ expect(state.undoStack.length).toBe(1);
48
+ });
49
+ it('should not create an undo snapshot if pushToUndo is false', () => {
50
+ const action = {
51
+ type: 'set_text',
52
+ payload: 'no undo',
53
+ pushToUndo: false,
54
+ };
55
+ const state = textBufferReducer(initialState, action);
56
+ expect(state).toHaveOnlyValidCharacters();
57
+ expect(state.lines).toEqual(['no undo']);
58
+ expect(state.undoStack.length).toBe(0);
59
+ });
60
+ });
61
+ describe('insert action', () => {
62
+ it('should insert a character', () => {
63
+ const action = { type: 'insert', payload: 'a' };
64
+ const state = textBufferReducer(initialState, action);
65
+ expect(state).toHaveOnlyValidCharacters();
66
+ expect(state.lines).toEqual(['a']);
67
+ expect(state.cursorCol).toBe(1);
68
+ });
69
+ it('should insert a newline', () => {
70
+ const stateWithText = { ...initialState, lines: ['hello'] };
71
+ const action = { type: 'insert', payload: '\n' };
72
+ const state = textBufferReducer(stateWithText, action);
73
+ expect(state).toHaveOnlyValidCharacters();
74
+ expect(state.lines).toEqual(['', 'hello']);
75
+ expect(state.cursorRow).toBe(1);
76
+ expect(state.cursorCol).toBe(0);
77
+ });
78
+ });
79
+ describe('backspace action', () => {
80
+ it('should remove a character', () => {
81
+ const stateWithText = {
82
+ ...initialState,
83
+ lines: ['a'],
84
+ cursorRow: 0,
85
+ cursorCol: 1,
86
+ };
87
+ const action = { type: 'backspace' };
88
+ const state = textBufferReducer(stateWithText, action);
89
+ expect(state).toHaveOnlyValidCharacters();
90
+ expect(state.lines).toEqual(['']);
91
+ expect(state.cursorCol).toBe(0);
92
+ });
93
+ it('should join lines if at the beginning of a line', () => {
94
+ const stateWithText = {
95
+ ...initialState,
96
+ lines: ['hello', 'world'],
97
+ cursorRow: 1,
98
+ cursorCol: 0,
99
+ };
100
+ const action = { type: 'backspace' };
101
+ const state = textBufferReducer(stateWithText, action);
102
+ expect(state).toHaveOnlyValidCharacters();
103
+ expect(state.lines).toEqual(['helloworld']);
104
+ expect(state.cursorRow).toBe(0);
105
+ expect(state.cursorCol).toBe(5);
106
+ });
107
+ });
108
+ describe('undo/redo actions', () => {
109
+ it('should undo and redo a change', () => {
110
+ // 1. Insert text
111
+ const insertAction = {
112
+ type: 'insert',
113
+ payload: 'test',
114
+ };
115
+ const stateAfterInsert = textBufferReducer(initialState, insertAction);
116
+ expect(stateAfterInsert).toHaveOnlyValidCharacters();
117
+ expect(stateAfterInsert.lines).toEqual(['test']);
118
+ expect(stateAfterInsert.undoStack.length).toBe(1);
119
+ // 2. Undo
120
+ const undoAction = { type: 'undo' };
121
+ const stateAfterUndo = textBufferReducer(stateAfterInsert, undoAction);
122
+ expect(stateAfterUndo).toHaveOnlyValidCharacters();
123
+ expect(stateAfterUndo.lines).toEqual(['']);
124
+ expect(stateAfterUndo.undoStack.length).toBe(0);
125
+ expect(stateAfterUndo.redoStack.length).toBe(1);
126
+ // 3. Redo
127
+ const redoAction = { type: 'redo' };
128
+ const stateAfterRedo = textBufferReducer(stateAfterUndo, redoAction);
129
+ expect(stateAfterRedo).toHaveOnlyValidCharacters();
130
+ expect(stateAfterRedo.lines).toEqual(['test']);
131
+ expect(stateAfterRedo.undoStack.length).toBe(1);
132
+ expect(stateAfterRedo.redoStack.length).toBe(0);
133
+ });
134
+ });
135
+ describe('create_undo_snapshot action', () => {
136
+ it('should create a snapshot without changing state', () => {
137
+ const stateWithText = {
138
+ ...initialState,
139
+ lines: ['hello'],
140
+ cursorRow: 0,
141
+ cursorCol: 5,
142
+ };
143
+ const action = { type: 'create_undo_snapshot' };
144
+ const state = textBufferReducer(stateWithText, action);
145
+ expect(state).toHaveOnlyValidCharacters();
146
+ expect(state.lines).toEqual(['hello']);
147
+ expect(state.cursorRow).toBe(0);
148
+ expect(state.cursorCol).toBe(5);
149
+ expect(state.undoStack.length).toBe(1);
150
+ expect(state.undoStack[0].lines).toEqual(['hello']);
151
+ expect(state.undoStack[0].cursorRow).toBe(0);
152
+ expect(state.undoStack[0].cursorCol).toBe(5);
153
+ });
154
+ });
155
+ describe('delete_word_left action', () => {
156
+ it('should delete a simple word', () => {
157
+ const stateWithText = {
158
+ ...initialState,
159
+ lines: ['hello world'],
160
+ cursorRow: 0,
161
+ cursorCol: 11,
162
+ };
163
+ const action = { type: 'delete_word_left' };
164
+ const state = textBufferReducer(stateWithText, action);
165
+ expect(state.lines).toEqual(['hello ']);
166
+ expect(state.cursorCol).toBe(6);
167
+ });
168
+ it('should delete a path segment', () => {
169
+ const stateWithText = {
170
+ ...initialState,
171
+ lines: ['path/to/file'],
172
+ cursorRow: 0,
173
+ cursorCol: 12,
174
+ };
175
+ const action = { type: 'delete_word_left' };
176
+ const state = textBufferReducer(stateWithText, action);
177
+ expect(state.lines).toEqual(['path/to/']);
178
+ expect(state.cursorCol).toBe(8);
179
+ });
180
+ it('should delete variable_name parts', () => {
181
+ const stateWithText = {
182
+ ...initialState,
183
+ lines: ['variable_name'],
184
+ cursorRow: 0,
185
+ cursorCol: 13,
186
+ };
187
+ const action = { type: 'delete_word_left' };
188
+ const state = textBufferReducer(stateWithText, action);
189
+ expect(state.lines).toEqual(['variable_']);
190
+ expect(state.cursorCol).toBe(9);
191
+ });
192
+ it('should act like backspace at the beginning of a line', () => {
193
+ const stateWithText = {
194
+ ...initialState,
195
+ lines: ['hello', 'world'],
196
+ cursorRow: 1,
197
+ cursorCol: 0,
198
+ };
199
+ const action = { type: 'delete_word_left' };
200
+ const state = textBufferReducer(stateWithText, action);
201
+ expect(state.lines).toEqual(['helloworld']);
202
+ expect(state.cursorRow).toBe(0);
203
+ expect(state.cursorCol).toBe(5);
204
+ });
205
+ });
206
+ describe('delete_word_right action', () => {
207
+ it('should delete a simple word', () => {
208
+ const stateWithText = {
209
+ ...initialState,
210
+ lines: ['hello world'],
211
+ cursorRow: 0,
212
+ cursorCol: 0,
213
+ };
214
+ const action = { type: 'delete_word_right' };
215
+ const state = textBufferReducer(stateWithText, action);
216
+ expect(state.lines).toEqual(['world']);
217
+ expect(state.cursorCol).toBe(0);
218
+ });
219
+ it('should delete a path segment', () => {
220
+ const stateWithText = {
221
+ ...initialState,
222
+ lines: ['path/to/file'],
223
+ cursorRow: 0,
224
+ cursorCol: 0,
225
+ };
226
+ const action = { type: 'delete_word_right' };
227
+ let state = textBufferReducer(stateWithText, action);
228
+ expect(state.lines).toEqual(['/to/file']);
229
+ state = textBufferReducer(state, action);
230
+ expect(state.lines).toEqual(['to/file']);
231
+ });
232
+ it('should delete variable_name parts', () => {
233
+ const stateWithText = {
234
+ ...initialState,
235
+ lines: ['variable_name'],
236
+ cursorRow: 0,
237
+ cursorCol: 0,
238
+ };
239
+ const action = { type: 'delete_word_right' };
240
+ const state = textBufferReducer(stateWithText, action);
241
+ expect(state.lines).toEqual(['_name']);
242
+ expect(state.cursorCol).toBe(0);
243
+ });
244
+ it('should act like delete at the end of a line', () => {
245
+ const stateWithText = {
246
+ ...initialState,
247
+ lines: ['hello', 'world'],
248
+ cursorRow: 0,
249
+ cursorCol: 5,
250
+ };
251
+ const action = { type: 'delete_word_right' };
252
+ const state = textBufferReducer(stateWithText, action);
253
+ expect(state.lines).toEqual(['helloworld']);
254
+ expect(state.cursorRow).toBe(0);
255
+ expect(state.cursorCol).toBe(5);
256
+ });
257
+ });
258
+ });
259
+ // Helper to get the state from the hook
260
+ const getBufferState = (result) => {
261
+ expect(result.current).toHaveOnlyValidCharacters();
262
+ return {
263
+ text: result.current.text,
264
+ lines: [...result.current.lines], // Clone for safety
265
+ cursor: [...result.current.cursor],
266
+ allVisualLines: [...result.current.allVisualLines],
267
+ viewportVisualLines: [...result.current.viewportVisualLines],
268
+ visualCursor: [...result.current.visualCursor],
269
+ visualScrollRow: result.current.visualScrollRow,
270
+ preferredCol: result.current.preferredCol,
271
+ };
272
+ };
273
+ describe('useTextBuffer', () => {
274
+ let viewport;
275
+ beforeEach(() => {
276
+ viewport = { width: 10, height: 3 }; // Default viewport for tests
277
+ });
278
+ describe('Initialization', () => {
279
+ it('should initialize with empty text and cursor at (0,0) by default', () => {
280
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
281
+ const state = getBufferState(result);
282
+ expect(state.text).toBe('');
283
+ expect(state.lines).toEqual(['']);
284
+ expect(state.cursor).toEqual([0, 0]);
285
+ expect(state.allVisualLines).toEqual(['']);
286
+ expect(state.viewportVisualLines).toEqual(['']);
287
+ expect(state.visualCursor).toEqual([0, 0]);
288
+ expect(state.visualScrollRow).toBe(0);
289
+ });
290
+ it('should initialize with provided initialText', () => {
291
+ const { result } = renderHook(() => useTextBuffer({
292
+ initialText: 'hello',
293
+ viewport,
294
+ isValidPath: () => false,
295
+ }));
296
+ const state = getBufferState(result);
297
+ expect(state.text).toBe('hello');
298
+ expect(state.lines).toEqual(['hello']);
299
+ expect(state.cursor).toEqual([0, 0]); // Default cursor if offset not given
300
+ expect(state.allVisualLines).toEqual(['hello']);
301
+ expect(state.viewportVisualLines).toEqual(['hello']);
302
+ expect(state.visualCursor).toEqual([0, 0]);
303
+ });
304
+ it('should initialize with initialText and initialCursorOffset', () => {
305
+ const { result } = renderHook(() => useTextBuffer({
306
+ initialText: 'hello\nworld',
307
+ initialCursorOffset: 7, // Should be at 'o' in 'world'
308
+ viewport,
309
+ isValidPath: () => false,
310
+ }));
311
+ const state = getBufferState(result);
312
+ expect(state.text).toBe('hello\nworld');
313
+ expect(state.lines).toEqual(['hello', 'world']);
314
+ expect(state.cursor).toEqual([1, 1]); // Logical cursor at 'o' in "world"
315
+ expect(state.allVisualLines).toEqual(['hello', 'world']);
316
+ expect(state.viewportVisualLines).toEqual(['hello', 'world']);
317
+ expect(state.visualCursor[0]).toBe(1); // On the second visual line
318
+ expect(state.visualCursor[1]).toBe(1); // At 'o' in "world"
319
+ });
320
+ it('should wrap visual lines', () => {
321
+ const { result } = renderHook(() => useTextBuffer({
322
+ initialText: 'The quick brown fox jumps over the lazy dog.',
323
+ initialCursorOffset: 2, // After '好'
324
+ viewport: { width: 15, height: 4 },
325
+ isValidPath: () => false,
326
+ }));
327
+ const state = getBufferState(result);
328
+ expect(state.allVisualLines).toEqual([
329
+ 'The quick',
330
+ 'brown fox',
331
+ 'jumps over the',
332
+ 'lazy dog.',
333
+ ]);
334
+ });
335
+ it('should wrap visual lines with multiple spaces', () => {
336
+ const { result } = renderHook(() => useTextBuffer({
337
+ initialText: 'The quick brown fox jumps over the lazy dog.',
338
+ viewport: { width: 15, height: 4 },
339
+ isValidPath: () => false,
340
+ }));
341
+ const state = getBufferState(result);
342
+ // Including multiple spaces at the end of the lines like this is
343
+ // consistent with Google docs behavior and makes it intuitive to edit
344
+ // the spaces as needed.
345
+ expect(state.allVisualLines).toEqual([
346
+ 'The quick ',
347
+ 'brown fox ',
348
+ 'jumps over the',
349
+ 'lazy dog.',
350
+ ]);
351
+ });
352
+ it('should wrap visual lines even without spaces', () => {
353
+ const { result } = renderHook(() => useTextBuffer({
354
+ initialText: '123456789012345ABCDEFG', // 4 chars, 12 bytes
355
+ viewport: { width: 15, height: 2 },
356
+ isValidPath: () => false,
357
+ }));
358
+ const state = getBufferState(result);
359
+ // Including multiple spaces at the end of the lines like this is
360
+ // consistent with Google docs behavior and makes it intuitive to edit
361
+ // the spaces as needed.
362
+ expect(state.allVisualLines).toEqual(['123456789012345', 'ABCDEFG']);
363
+ });
364
+ it('should initialize with multi-byte unicode characters and correct cursor offset', () => {
365
+ const { result } = renderHook(() => useTextBuffer({
366
+ initialText: '你好世界', // 4 chars, 12 bytes
367
+ initialCursorOffset: 2, // After '好'
368
+ viewport: { width: 5, height: 2 },
369
+ isValidPath: () => false,
370
+ }));
371
+ const state = getBufferState(result);
372
+ expect(state.text).toBe('你好世界');
373
+ expect(state.lines).toEqual(['你好世界']);
374
+ expect(state.cursor).toEqual([0, 2]);
375
+ // Visual: "你好" (width 4), "世"界" (width 4) with viewport width 5
376
+ expect(state.allVisualLines).toEqual(['你好', '世界']);
377
+ expect(state.visualCursor).toEqual([1, 0]);
378
+ });
379
+ });
380
+ describe('Basic Editing', () => {
381
+ it('insert: should insert a character and update cursor', () => {
382
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
383
+ act(() => result.current.insert('a'));
384
+ let state = getBufferState(result);
385
+ expect(state.text).toBe('a');
386
+ expect(state.cursor).toEqual([0, 1]);
387
+ expect(state.visualCursor).toEqual([0, 1]);
388
+ act(() => result.current.insert('b'));
389
+ state = getBufferState(result);
390
+ expect(state.text).toBe('ab');
391
+ expect(state.cursor).toEqual([0, 2]);
392
+ expect(state.visualCursor).toEqual([0, 2]);
393
+ });
394
+ it('insert: should insert text in the middle of a line', () => {
395
+ const { result } = renderHook(() => useTextBuffer({
396
+ initialText: 'abc',
397
+ viewport,
398
+ isValidPath: () => false,
399
+ }));
400
+ act(() => result.current.move('right'));
401
+ act(() => result.current.insert('-NEW-'));
402
+ const state = getBufferState(result);
403
+ expect(state.text).toBe('a-NEW-bc');
404
+ expect(state.cursor).toEqual([0, 6]);
405
+ });
406
+ it('newline: should create a new line and move cursor', () => {
407
+ const { result } = renderHook(() => useTextBuffer({
408
+ initialText: 'ab',
409
+ viewport,
410
+ isValidPath: () => false,
411
+ }));
412
+ act(() => result.current.move('end')); // cursor at [0,2]
413
+ act(() => result.current.newline());
414
+ const state = getBufferState(result);
415
+ expect(state.text).toBe('ab\n');
416
+ expect(state.lines).toEqual(['ab', '']);
417
+ expect(state.cursor).toEqual([1, 0]);
418
+ expect(state.allVisualLines).toEqual(['ab', '']);
419
+ expect(state.viewportVisualLines).toEqual(['ab', '']); // viewport height 3
420
+ expect(state.visualCursor).toEqual([1, 0]); // On the new visual line
421
+ });
422
+ it('backspace: should delete char to the left or merge lines', () => {
423
+ const { result } = renderHook(() => useTextBuffer({
424
+ initialText: 'a\nb',
425
+ viewport,
426
+ isValidPath: () => false,
427
+ }));
428
+ act(() => {
429
+ result.current.move('down');
430
+ });
431
+ act(() => {
432
+ result.current.move('end'); // cursor to [1,1] (end of 'b')
433
+ });
434
+ act(() => result.current.backspace()); // delete 'b'
435
+ let state = getBufferState(result);
436
+ expect(state.text).toBe('a\n');
437
+ expect(state.cursor).toEqual([1, 0]);
438
+ act(() => result.current.backspace()); // merge lines
439
+ state = getBufferState(result);
440
+ expect(state.text).toBe('a');
441
+ expect(state.cursor).toEqual([0, 1]); // cursor after 'a'
442
+ expect(state.allVisualLines).toEqual(['a']);
443
+ expect(state.viewportVisualLines).toEqual(['a']);
444
+ expect(state.visualCursor).toEqual([0, 1]);
445
+ });
446
+ it('del: should delete char to the right or merge lines', () => {
447
+ const { result } = renderHook(() => useTextBuffer({
448
+ initialText: 'a\nb',
449
+ viewport,
450
+ isValidPath: () => false,
451
+ }));
452
+ // cursor at [0,0]
453
+ act(() => result.current.del()); // delete 'a'
454
+ let state = getBufferState(result);
455
+ expect(state.text).toBe('\nb');
456
+ expect(state.cursor).toEqual([0, 0]);
457
+ act(() => result.current.del()); // merge lines (deletes newline)
458
+ state = getBufferState(result);
459
+ expect(state.text).toBe('b');
460
+ expect(state.cursor).toEqual([0, 0]);
461
+ expect(state.allVisualLines).toEqual(['b']);
462
+ expect(state.viewportVisualLines).toEqual(['b']);
463
+ expect(state.visualCursor).toEqual([0, 0]);
464
+ });
465
+ });
466
+ describe('Drag and Drop File Paths', () => {
467
+ it('should prepend @ to a valid file path on insert', () => {
468
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => true }));
469
+ const filePath = '/path/to/a/valid/file.txt';
470
+ act(() => result.current.insert(filePath, { paste: true }));
471
+ expect(getBufferState(result).text).toBe(`@${filePath} `);
472
+ });
473
+ it('should not prepend @ to an invalid file path on insert', () => {
474
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
475
+ const notAPath = 'this is just some long text';
476
+ act(() => result.current.insert(notAPath, { paste: true }));
477
+ expect(getBufferState(result).text).toBe(notAPath);
478
+ });
479
+ it('should handle quoted paths', () => {
480
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => true }));
481
+ const filePath = "'/path/to/a/valid/file.txt'";
482
+ act(() => result.current.insert(filePath, { paste: true }));
483
+ expect(getBufferState(result).text).toBe(`@/path/to/a/valid/file.txt `);
484
+ });
485
+ it('should not prepend @ to short text that is not a path', () => {
486
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => true }));
487
+ const shortText = 'ab';
488
+ act(() => result.current.insert(shortText, { paste: true }));
489
+ expect(getBufferState(result).text).toBe(shortText);
490
+ });
491
+ });
492
+ describe('Shell Mode Behavior', () => {
493
+ it('should not prepend @ to valid file paths when shellModeActive is true', () => {
494
+ const { result } = renderHook(() => useTextBuffer({
495
+ viewport,
496
+ isValidPath: () => true,
497
+ shellModeActive: true,
498
+ }));
499
+ const filePath = '/path/to/a/valid/file.txt';
500
+ act(() => result.current.insert(filePath, { paste: true }));
501
+ expect(getBufferState(result).text).toBe(filePath); // No @ prefix
502
+ });
503
+ it('should not prepend @ to quoted paths when shellModeActive is true', () => {
504
+ const { result } = renderHook(() => useTextBuffer({
505
+ viewport,
506
+ isValidPath: () => true,
507
+ shellModeActive: true,
508
+ }));
509
+ const quotedFilePath = "'/path/to/a/valid/file.txt'";
510
+ act(() => result.current.insert(quotedFilePath, { paste: true }));
511
+ expect(getBufferState(result).text).toBe(quotedFilePath); // No @ prefix, keeps quotes
512
+ });
513
+ it('should behave normally with invalid paths when shellModeActive is true', () => {
514
+ const { result } = renderHook(() => useTextBuffer({
515
+ viewport,
516
+ isValidPath: () => false,
517
+ shellModeActive: true,
518
+ }));
519
+ const notAPath = 'this is just some text';
520
+ act(() => result.current.insert(notAPath, { paste: true }));
521
+ expect(getBufferState(result).text).toBe(notAPath);
522
+ });
523
+ it('should behave normally with short text when shellModeActive is true', () => {
524
+ const { result } = renderHook(() => useTextBuffer({
525
+ viewport,
526
+ isValidPath: () => true,
527
+ shellModeActive: true,
528
+ }));
529
+ const shortText = 'ls';
530
+ act(() => result.current.insert(shortText, { paste: true }));
531
+ expect(getBufferState(result).text).toBe(shortText); // No @ prefix for short text
532
+ });
533
+ });
534
+ describe('Cursor Movement', () => {
535
+ it('move: left/right should work within and across visual lines (due to wrapping)', () => {
536
+ // Text: "long line1next line2" (20 chars)
537
+ // Viewport width 5. Word wrapping should produce:
538
+ // "long " (5)
539
+ // "line1" (5)
540
+ // "next " (5)
541
+ // "line2" (5)
542
+ const { result } = renderHook(() => useTextBuffer({
543
+ initialText: 'long line1next line2', // Corrected: was 'long line1next line2'
544
+ viewport: { width: 5, height: 4 },
545
+ isValidPath: () => false,
546
+ }));
547
+ // Initial cursor [0,0] logical, visual [0,0] ("l" of "long ")
548
+ act(() => result.current.move('right')); // visual [0,1] ("o")
549
+ expect(getBufferState(result).visualCursor).toEqual([0, 1]);
550
+ act(() => result.current.move('right')); // visual [0,2] ("n")
551
+ act(() => result.current.move('right')); // visual [0,3] ("g")
552
+ act(() => result.current.move('right')); // visual [0,4] (" ")
553
+ expect(getBufferState(result).visualCursor).toEqual([0, 4]);
554
+ act(() => result.current.move('right')); // visual [1,0] ("l" of "line1")
555
+ expect(getBufferState(result).visualCursor).toEqual([1, 0]);
556
+ expect(getBufferState(result).cursor).toEqual([0, 5]); // logical cursor
557
+ act(() => result.current.move('left')); // visual [0,4] (" " of "long ")
558
+ expect(getBufferState(result).visualCursor).toEqual([0, 4]);
559
+ expect(getBufferState(result).cursor).toEqual([0, 4]); // logical cursor
560
+ });
561
+ it('move: up/down should preserve preferred visual column', () => {
562
+ const text = 'abcde\nxy\n12345';
563
+ const { result } = renderHook(() => useTextBuffer({
564
+ initialText: text,
565
+ viewport,
566
+ isValidPath: () => false,
567
+ }));
568
+ expect(result.current.allVisualLines).toEqual(['abcde', 'xy', '12345']);
569
+ // Place cursor at the end of "abcde" -> logical [0,5]
570
+ act(() => {
571
+ result.current.move('home'); // to [0,0]
572
+ });
573
+ for (let i = 0; i < 5; i++) {
574
+ act(() => {
575
+ result.current.move('right'); // to [0,5]
576
+ });
577
+ }
578
+ expect(getBufferState(result).cursor).toEqual([0, 5]);
579
+ expect(getBufferState(result).visualCursor).toEqual([0, 5]);
580
+ // Set preferredCol by moving up then down to the same spot, then test.
581
+ act(() => {
582
+ result.current.move('down'); // to xy, logical [1,2], visual [1,2], preferredCol should be 5
583
+ });
584
+ let state = getBufferState(result);
585
+ expect(state.cursor).toEqual([1, 2]); // Logical cursor at end of 'xy'
586
+ expect(state.visualCursor).toEqual([1, 2]); // Visual cursor at end of 'xy'
587
+ expect(state.preferredCol).toBe(5);
588
+ act(() => result.current.move('down')); // to '12345', preferredCol=5.
589
+ state = getBufferState(result);
590
+ expect(state.cursor).toEqual([2, 5]); // Logical cursor at end of '12345'
591
+ expect(state.visualCursor).toEqual([2, 5]); // Visual cursor at end of '12345'
592
+ expect(state.preferredCol).toBe(5); // Preferred col is maintained
593
+ act(() => result.current.move('left')); // preferredCol should reset
594
+ state = getBufferState(result);
595
+ expect(state.preferredCol).toBe(null);
596
+ });
597
+ it('move: home/end should go to visual line start/end', () => {
598
+ const initialText = 'line one\nsecond line';
599
+ const { result } = renderHook(() => useTextBuffer({
600
+ initialText,
601
+ viewport: { width: 5, height: 5 },
602
+ isValidPath: () => false,
603
+ }));
604
+ expect(result.current.allVisualLines).toEqual([
605
+ 'line',
606
+ 'one',
607
+ 'secon',
608
+ 'd',
609
+ 'line',
610
+ ]);
611
+ // Initial cursor [0,0] (start of "line")
612
+ act(() => result.current.move('down')); // visual cursor from [0,0] to [1,0] ("o" of "one")
613
+ act(() => result.current.move('right')); // visual cursor to [1,1] ("n" of "one")
614
+ expect(getBufferState(result).visualCursor).toEqual([1, 1]);
615
+ act(() => result.current.move('home')); // visual cursor to [1,0] (start of "one")
616
+ expect(getBufferState(result).visualCursor).toEqual([1, 0]);
617
+ act(() => result.current.move('end')); // visual cursor to [1,3] (end of "one")
618
+ expect(getBufferState(result).visualCursor).toEqual([1, 3]); // "one" is 3 chars
619
+ });
620
+ });
621
+ describe('Visual Layout & Viewport', () => {
622
+ it('should wrap long lines correctly into visualLines', () => {
623
+ const { result } = renderHook(() => useTextBuffer({
624
+ initialText: 'This is a very long line of text.', // 33 chars
625
+ viewport: { width: 10, height: 5 },
626
+ isValidPath: () => false,
627
+ }));
628
+ const state = getBufferState(result);
629
+ // Expected visual lines with word wrapping (viewport width 10):
630
+ // "This is a"
631
+ // "very long"
632
+ // "line of"
633
+ // "text."
634
+ expect(state.allVisualLines.length).toBe(4);
635
+ expect(state.allVisualLines[0]).toBe('This is a');
636
+ expect(state.allVisualLines[1]).toBe('very long');
637
+ expect(state.allVisualLines[2]).toBe('line of');
638
+ expect(state.allVisualLines[3]).toBe('text.');
639
+ });
640
+ it('should update visualScrollRow when visualCursor moves out of viewport', () => {
641
+ const { result } = renderHook(() => useTextBuffer({
642
+ initialText: 'l1\nl2\nl3\nl4\nl5',
643
+ viewport: { width: 5, height: 3 }, // Can show 3 visual lines
644
+ isValidPath: () => false,
645
+ }));
646
+ // Initial: l1, l2, l3 visible. visualScrollRow = 0. visualCursor = [0,0]
647
+ expect(getBufferState(result).visualScrollRow).toBe(0);
648
+ expect(getBufferState(result).allVisualLines).toEqual([
649
+ 'l1',
650
+ 'l2',
651
+ 'l3',
652
+ 'l4',
653
+ 'l5',
654
+ ]);
655
+ expect(getBufferState(result).viewportVisualLines).toEqual([
656
+ 'l1',
657
+ 'l2',
658
+ 'l3',
659
+ ]);
660
+ act(() => result.current.move('down')); // vc=[1,0]
661
+ act(() => result.current.move('down')); // vc=[2,0] (l3)
662
+ expect(getBufferState(result).visualScrollRow).toBe(0);
663
+ act(() => result.current.move('down')); // vc=[3,0] (l4) - scroll should happen
664
+ // Now: l2, l3, l4 visible. visualScrollRow = 1.
665
+ let state = getBufferState(result);
666
+ expect(state.visualScrollRow).toBe(1);
667
+ expect(state.allVisualLines).toEqual(['l1', 'l2', 'l3', 'l4', 'l5']);
668
+ expect(state.viewportVisualLines).toEqual(['l2', 'l3', 'l4']);
669
+ expect(state.visualCursor).toEqual([3, 0]);
670
+ act(() => result.current.move('up')); // vc=[2,0] (l3)
671
+ act(() => result.current.move('up')); // vc=[1,0] (l2)
672
+ expect(getBufferState(result).visualScrollRow).toBe(1);
673
+ act(() => result.current.move('up')); // vc=[0,0] (l1) - scroll up
674
+ // Now: l1, l2, l3 visible. visualScrollRow = 0
675
+ state = getBufferState(result); // Assign to the existing `state` variable
676
+ expect(state.visualScrollRow).toBe(0);
677
+ expect(state.allVisualLines).toEqual(['l1', 'l2', 'l3', 'l4', 'l5']);
678
+ expect(state.viewportVisualLines).toEqual(['l1', 'l2', 'l3']);
679
+ expect(state.visualCursor).toEqual([0, 0]);
680
+ });
681
+ });
682
+ describe('Undo/Redo', () => {
683
+ it('should undo and redo an insert operation', () => {
684
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
685
+ act(() => result.current.insert('a'));
686
+ expect(getBufferState(result).text).toBe('a');
687
+ act(() => result.current.undo());
688
+ expect(getBufferState(result).text).toBe('');
689
+ expect(getBufferState(result).cursor).toEqual([0, 0]);
690
+ act(() => result.current.redo());
691
+ expect(getBufferState(result).text).toBe('a');
692
+ expect(getBufferState(result).cursor).toEqual([0, 1]);
693
+ });
694
+ it('should undo and redo a newline operation', () => {
695
+ const { result } = renderHook(() => useTextBuffer({
696
+ initialText: 'test',
697
+ viewport,
698
+ isValidPath: () => false,
699
+ }));
700
+ act(() => result.current.move('end'));
701
+ act(() => result.current.newline());
702
+ expect(getBufferState(result).text).toBe('test\n');
703
+ act(() => result.current.undo());
704
+ expect(getBufferState(result).text).toBe('test');
705
+ expect(getBufferState(result).cursor).toEqual([0, 4]);
706
+ act(() => result.current.redo());
707
+ expect(getBufferState(result).text).toBe('test\n');
708
+ expect(getBufferState(result).cursor).toEqual([1, 0]);
709
+ });
710
+ });
711
+ describe('Unicode Handling', () => {
712
+ it('insert: should correctly handle multi-byte unicode characters', () => {
713
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
714
+ act(() => result.current.insert('你好'));
715
+ const state = getBufferState(result);
716
+ expect(state.text).toBe('你好');
717
+ expect(state.cursor).toEqual([0, 2]); // Cursor is 2 (char count)
718
+ expect(state.visualCursor).toEqual([0, 2]);
719
+ });
720
+ it('backspace: should correctly delete multi-byte unicode characters', () => {
721
+ const { result } = renderHook(() => useTextBuffer({
722
+ initialText: '你好',
723
+ viewport,
724
+ isValidPath: () => false,
725
+ }));
726
+ act(() => result.current.move('end')); // cursor at [0,2]
727
+ act(() => result.current.backspace()); // delete '好'
728
+ let state = getBufferState(result);
729
+ expect(state.text).toBe('你');
730
+ expect(state.cursor).toEqual([0, 1]);
731
+ act(() => result.current.backspace()); // delete '你'
732
+ state = getBufferState(result);
733
+ expect(state.text).toBe('');
734
+ expect(state.cursor).toEqual([0, 0]);
735
+ });
736
+ it('move: left/right should treat multi-byte chars as single units for visual cursor', () => {
737
+ const { result } = renderHook(() => useTextBuffer({
738
+ initialText: '🐶🐱',
739
+ viewport: { width: 5, height: 1 },
740
+ isValidPath: () => false,
741
+ }));
742
+ // Initial: visualCursor [0,0]
743
+ act(() => result.current.move('right')); // visualCursor [0,1] (after 🐶)
744
+ let state = getBufferState(result);
745
+ expect(state.cursor).toEqual([0, 1]);
746
+ expect(state.visualCursor).toEqual([0, 1]);
747
+ act(() => result.current.move('right')); // visualCursor [0,2] (after 🐱)
748
+ state = getBufferState(result);
749
+ expect(state.cursor).toEqual([0, 2]);
750
+ expect(state.visualCursor).toEqual([0, 2]);
751
+ act(() => result.current.move('left')); // visualCursor [0,1] (before 🐱 / after 🐶)
752
+ state = getBufferState(result);
753
+ expect(state.cursor).toEqual([0, 1]);
754
+ expect(state.visualCursor).toEqual([0, 1]);
755
+ });
756
+ });
757
+ describe('handleInput', () => {
758
+ it('should insert printable characters', () => {
759
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
760
+ act(() => result.current.handleInput({
761
+ name: 'h',
762
+ ctrl: false,
763
+ meta: false,
764
+ shift: false,
765
+ paste: false,
766
+ sequence: 'h',
767
+ }));
768
+ act(() => result.current.handleInput({
769
+ name: 'i',
770
+ ctrl: false,
771
+ meta: false,
772
+ shift: false,
773
+ paste: false,
774
+ sequence: 'i',
775
+ }));
776
+ expect(getBufferState(result).text).toBe('hi');
777
+ });
778
+ it('should handle "Enter" key as newline', () => {
779
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
780
+ act(() => result.current.handleInput({
781
+ name: 'return',
782
+ ctrl: false,
783
+ meta: false,
784
+ shift: false,
785
+ paste: false,
786
+ sequence: '\r',
787
+ }));
788
+ expect(getBufferState(result).lines).toEqual(['', '']);
789
+ });
790
+ it('should handle "Backspace" key', () => {
791
+ const { result } = renderHook(() => useTextBuffer({
792
+ initialText: 'a',
793
+ viewport,
794
+ isValidPath: () => false,
795
+ }));
796
+ act(() => result.current.move('end'));
797
+ act(() => result.current.handleInput({
798
+ name: 'backspace',
799
+ ctrl: false,
800
+ meta: false,
801
+ shift: false,
802
+ paste: false,
803
+ sequence: '\x7f',
804
+ }));
805
+ expect(getBufferState(result).text).toBe('');
806
+ });
807
+ it('should handle multiple delete characters in one input', () => {
808
+ const { result } = renderHook(() => useTextBuffer({
809
+ initialText: 'abcde',
810
+ viewport,
811
+ isValidPath: () => false,
812
+ }));
813
+ act(() => result.current.move('end')); // cursor at the end
814
+ expect(getBufferState(result).cursor).toEqual([0, 5]);
815
+ act(() => {
816
+ result.current.handleInput({
817
+ name: 'backspace',
818
+ ctrl: false,
819
+ meta: false,
820
+ shift: false,
821
+ paste: false,
822
+ sequence: '\x7f',
823
+ });
824
+ result.current.handleInput({
825
+ name: 'backspace',
826
+ ctrl: false,
827
+ meta: false,
828
+ shift: false,
829
+ paste: false,
830
+ sequence: '\x7f',
831
+ });
832
+ result.current.handleInput({
833
+ name: 'backspace',
834
+ ctrl: false,
835
+ meta: false,
836
+ shift: false,
837
+ paste: false,
838
+ sequence: '\x7f',
839
+ });
840
+ });
841
+ expect(getBufferState(result).text).toBe('ab');
842
+ expect(getBufferState(result).cursor).toEqual([0, 2]);
843
+ });
844
+ it('should handle inserts that contain delete characters', () => {
845
+ const { result } = renderHook(() => useTextBuffer({
846
+ initialText: 'abcde',
847
+ viewport,
848
+ isValidPath: () => false,
849
+ }));
850
+ act(() => result.current.move('end')); // cursor at the end
851
+ expect(getBufferState(result).cursor).toEqual([0, 5]);
852
+ act(() => {
853
+ result.current.insert('\x7f\x7f\x7f');
854
+ });
855
+ expect(getBufferState(result).text).toBe('ab');
856
+ expect(getBufferState(result).cursor).toEqual([0, 2]);
857
+ });
858
+ it('should handle inserts with a mix of regular and delete characters', () => {
859
+ const { result } = renderHook(() => useTextBuffer({
860
+ initialText: 'abcde',
861
+ viewport,
862
+ isValidPath: () => false,
863
+ }));
864
+ act(() => result.current.move('end')); // cursor at the end
865
+ expect(getBufferState(result).cursor).toEqual([0, 5]);
866
+ act(() => {
867
+ result.current.insert('\x7fI\x7f\x7fNEW');
868
+ });
869
+ expect(getBufferState(result).text).toBe('abcNEW');
870
+ expect(getBufferState(result).cursor).toEqual([0, 6]);
871
+ });
872
+ it('should handle arrow keys for movement', () => {
873
+ const { result } = renderHook(() => useTextBuffer({
874
+ initialText: 'ab',
875
+ viewport,
876
+ isValidPath: () => false,
877
+ }));
878
+ act(() => result.current.move('end')); // cursor [0,2]
879
+ act(() => result.current.handleInput({
880
+ name: 'left',
881
+ ctrl: false,
882
+ meta: false,
883
+ shift: false,
884
+ paste: false,
885
+ sequence: '\x1b[D',
886
+ })); // cursor [0,1]
887
+ expect(getBufferState(result).cursor).toEqual([0, 1]);
888
+ act(() => result.current.handleInput({
889
+ name: 'right',
890
+ ctrl: false,
891
+ meta: false,
892
+ shift: false,
893
+ paste: false,
894
+ sequence: '\x1b[C',
895
+ })); // cursor [0,2]
896
+ expect(getBufferState(result).cursor).toEqual([0, 2]);
897
+ });
898
+ it('should strip ANSI escape codes when pasting text', () => {
899
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
900
+ const textWithAnsi = '\x1B[31mHello\x1B[0m \x1B[32mWorld\x1B[0m';
901
+ // Simulate pasting by calling handleInput with a string longer than 1 char
902
+ act(() => result.current.handleInput({
903
+ name: '',
904
+ ctrl: false,
905
+ meta: false,
906
+ shift: false,
907
+ paste: false,
908
+ sequence: textWithAnsi,
909
+ }));
910
+ expect(getBufferState(result).text).toBe('Hello World');
911
+ });
912
+ it('should handle VSCode terminal Shift+Enter as newline', () => {
913
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
914
+ act(() => result.current.handleInput({
915
+ name: 'return',
916
+ ctrl: false,
917
+ meta: false,
918
+ shift: true,
919
+ paste: false,
920
+ sequence: '\r',
921
+ })); // Simulates Shift+Enter in VSCode terminal
922
+ expect(getBufferState(result).lines).toEqual(['', '']);
923
+ });
924
+ it('should correctly handle repeated pasting of long text', () => {
925
+ const longText = `not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
926
+
927
+ Why do we use it?
928
+ It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
929
+
930
+ Where does it come from?
931
+ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lore
932
+ `;
933
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
934
+ // Simulate pasting the long text multiple times
935
+ act(() => {
936
+ result.current.insert(longText, { paste: true });
937
+ result.current.insert(longText, { paste: true });
938
+ result.current.insert(longText, { paste: true });
939
+ });
940
+ const state = getBufferState(result);
941
+ // Check that the text is the result of three concatenations.
942
+ expect(state.lines).toStrictEqual((longText + longText + longText).split('\n'));
943
+ const expectedCursorPos = offsetToLogicalPos(state.text, state.text.length);
944
+ expect(state.cursor).toEqual(expectedCursorPos);
945
+ });
946
+ });
947
+ // More tests would be needed for:
948
+ // - setText, replaceRange
949
+ // - deleteWordLeft, deleteWordRight
950
+ // - More complex undo/redo scenarios
951
+ // - Selection and clipboard (copy/paste) - might need clipboard API mocks or internal state check
952
+ // - openInExternalEditor (heavy mocking of fs, child_process, os)
953
+ // - All edge cases for visual scrolling and wrapping with different viewport sizes and text content.
954
+ describe('replaceRange', () => {
955
+ it('should replace a single-line range with single-line text', () => {
956
+ const { result } = renderHook(() => useTextBuffer({
957
+ initialText: '@pac',
958
+ viewport,
959
+ isValidPath: () => false,
960
+ }));
961
+ act(() => result.current.replaceRange(0, 1, 0, 4, 'packages'));
962
+ const state = getBufferState(result);
963
+ expect(state.text).toBe('@packages');
964
+ expect(state.cursor).toEqual([0, 9]); // cursor after 'typescript'
965
+ });
966
+ it('should replace a multi-line range with single-line text', () => {
967
+ const { result } = renderHook(() => useTextBuffer({
968
+ initialText: 'hello\nworld\nagain',
969
+ viewport,
970
+ isValidPath: () => false,
971
+ }));
972
+ act(() => result.current.replaceRange(0, 2, 1, 3, ' new ')); // replace 'llo\nwor' with ' new '
973
+ const state = getBufferState(result);
974
+ expect(state.text).toBe('he new ld\nagain');
975
+ expect(state.cursor).toEqual([0, 7]); // cursor after ' new '
976
+ });
977
+ it('should delete a range when replacing with an empty string', () => {
978
+ const { result } = renderHook(() => useTextBuffer({
979
+ initialText: 'hello world',
980
+ viewport,
981
+ isValidPath: () => false,
982
+ }));
983
+ act(() => result.current.replaceRange(0, 5, 0, 11, '')); // delete ' world'
984
+ const state = getBufferState(result);
985
+ expect(state.text).toBe('hello');
986
+ expect(state.cursor).toEqual([0, 5]);
987
+ });
988
+ it('should handle replacing at the beginning of the text', () => {
989
+ const { result } = renderHook(() => useTextBuffer({
990
+ initialText: 'world',
991
+ viewport,
992
+ isValidPath: () => false,
993
+ }));
994
+ act(() => result.current.replaceRange(0, 0, 0, 0, 'hello '));
995
+ const state = getBufferState(result);
996
+ expect(state.text).toBe('hello world');
997
+ expect(state.cursor).toEqual([0, 6]);
998
+ });
999
+ it('should handle replacing at the end of the text', () => {
1000
+ const { result } = renderHook(() => useTextBuffer({
1001
+ initialText: 'hello',
1002
+ viewport,
1003
+ isValidPath: () => false,
1004
+ }));
1005
+ act(() => result.current.replaceRange(0, 5, 0, 5, ' world'));
1006
+ const state = getBufferState(result);
1007
+ expect(state.text).toBe('hello world');
1008
+ expect(state.cursor).toEqual([0, 11]);
1009
+ });
1010
+ it('should handle replacing the entire buffer content', () => {
1011
+ const { result } = renderHook(() => useTextBuffer({
1012
+ initialText: 'old text',
1013
+ viewport,
1014
+ isValidPath: () => false,
1015
+ }));
1016
+ act(() => result.current.replaceRange(0, 0, 0, 8, 'new text'));
1017
+ const state = getBufferState(result);
1018
+ expect(state.text).toBe('new text');
1019
+ expect(state.cursor).toEqual([0, 8]);
1020
+ });
1021
+ it('should correctly replace with unicode characters', () => {
1022
+ const { result } = renderHook(() => useTextBuffer({
1023
+ initialText: 'hello *** world',
1024
+ viewport,
1025
+ isValidPath: () => false,
1026
+ }));
1027
+ act(() => result.current.replaceRange(0, 6, 0, 9, '你好'));
1028
+ const state = getBufferState(result);
1029
+ expect(state.text).toBe('hello 你好 world');
1030
+ expect(state.cursor).toEqual([0, 8]); // after '你好'
1031
+ });
1032
+ it('should handle invalid range by returning false and not changing text', () => {
1033
+ const { result } = renderHook(() => useTextBuffer({
1034
+ initialText: 'test',
1035
+ viewport,
1036
+ isValidPath: () => false,
1037
+ }));
1038
+ act(() => {
1039
+ result.current.replaceRange(0, 5, 0, 3, 'fail'); // startCol > endCol in same line
1040
+ });
1041
+ expect(getBufferState(result).text).toBe('test');
1042
+ act(() => {
1043
+ result.current.replaceRange(1, 0, 0, 0, 'fail'); // startRow > endRow
1044
+ });
1045
+ expect(getBufferState(result).text).toBe('test');
1046
+ });
1047
+ it('replaceRange: multiple lines with a single character', () => {
1048
+ const { result } = renderHook(() => useTextBuffer({
1049
+ initialText: 'first\nsecond\nthird',
1050
+ viewport,
1051
+ isValidPath: () => false,
1052
+ }));
1053
+ act(() => result.current.replaceRange(0, 2, 2, 3, 'X')); // Replace 'rst\nsecond\nthi'
1054
+ const state = getBufferState(result);
1055
+ expect(state.text).toBe('fiXrd');
1056
+ expect(state.cursor).toEqual([0, 3]); // After 'X'
1057
+ });
1058
+ it('should replace a single-line range with multi-line text', () => {
1059
+ const { result } = renderHook(() => useTextBuffer({
1060
+ initialText: 'one two three',
1061
+ viewport,
1062
+ isValidPath: () => false,
1063
+ }));
1064
+ // Replace "two" with "new\nline"
1065
+ act(() => result.current.replaceRange(0, 4, 0, 7, 'new\nline'));
1066
+ const state = getBufferState(result);
1067
+ expect(state.lines).toEqual(['one new', 'line three']);
1068
+ expect(state.text).toBe('one new\nline three');
1069
+ expect(state.cursor).toEqual([1, 4]); // cursor after 'line'
1070
+ });
1071
+ });
1072
+ describe('Input Sanitization', () => {
1073
+ it('should strip ANSI escape codes from input', () => {
1074
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
1075
+ const textWithAnsi = '\x1B[31mHello\x1B[0m \x1B[32mWorld\x1B[0m';
1076
+ act(() => result.current.handleInput({
1077
+ name: '',
1078
+ ctrl: false,
1079
+ meta: false,
1080
+ shift: false,
1081
+ paste: false,
1082
+ sequence: textWithAnsi,
1083
+ }));
1084
+ expect(getBufferState(result).text).toBe('Hello World');
1085
+ });
1086
+ it('should strip control characters from input', () => {
1087
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
1088
+ const textWithControlChars = 'H\x07e\x08l\x0Bl\x0Co'; // BELL, BACKSPACE, VT, FF
1089
+ act(() => result.current.handleInput({
1090
+ name: '',
1091
+ ctrl: false,
1092
+ meta: false,
1093
+ shift: false,
1094
+ paste: false,
1095
+ sequence: textWithControlChars,
1096
+ }));
1097
+ expect(getBufferState(result).text).toBe('Hello');
1098
+ });
1099
+ it('should strip mixed ANSI and control characters from input', () => {
1100
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
1101
+ const textWithMixed = '\u001B[4mH\u001B[0mello';
1102
+ act(() => result.current.handleInput({
1103
+ name: '',
1104
+ ctrl: false,
1105
+ meta: false,
1106
+ shift: false,
1107
+ paste: false,
1108
+ sequence: textWithMixed,
1109
+ }));
1110
+ expect(getBufferState(result).text).toBe('Hello');
1111
+ });
1112
+ it('should not strip standard characters or newlines', () => {
1113
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
1114
+ const validText = 'Hello World\nThis is a test.';
1115
+ act(() => result.current.handleInput({
1116
+ name: '',
1117
+ ctrl: false,
1118
+ meta: false,
1119
+ shift: false,
1120
+ paste: false,
1121
+ sequence: validText,
1122
+ }));
1123
+ expect(getBufferState(result).text).toBe(validText);
1124
+ });
1125
+ it('should sanitize pasted text via handleInput', () => {
1126
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
1127
+ const pastedText = '\u001B[4mPasted\u001B[4m Text';
1128
+ act(() => result.current.handleInput({
1129
+ name: '',
1130
+ ctrl: false,
1131
+ meta: false,
1132
+ shift: false,
1133
+ paste: false,
1134
+ sequence: pastedText,
1135
+ }));
1136
+ expect(getBufferState(result).text).toBe('Pasted Text');
1137
+ });
1138
+ it('should sanitize large text (>5000 chars) and strip unsafe characters', () => {
1139
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
1140
+ const unsafeChars = '\x07\x08\x0B\x0C';
1141
+ const largeTextWithUnsafe = 'safe text'.repeat(600) + unsafeChars + 'more safe text';
1142
+ expect(largeTextWithUnsafe.length).toBeGreaterThan(5000);
1143
+ act(() => result.current.handleInput({
1144
+ name: '',
1145
+ ctrl: false,
1146
+ meta: false,
1147
+ shift: false,
1148
+ paste: false,
1149
+ sequence: largeTextWithUnsafe,
1150
+ }));
1151
+ const resultText = getBufferState(result).text;
1152
+ expect(resultText).not.toContain('\x07');
1153
+ expect(resultText).not.toContain('\x08');
1154
+ expect(resultText).not.toContain('\x0B');
1155
+ expect(resultText).not.toContain('\x0C');
1156
+ expect(resultText).toContain('safe text');
1157
+ expect(resultText).toContain('more safe text');
1158
+ });
1159
+ it('should sanitize large ANSI text (>5000 chars) and strip escape codes', () => {
1160
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
1161
+ const largeTextWithAnsi = '\x1B[31m' +
1162
+ 'red text'.repeat(800) +
1163
+ '\x1B[0m' +
1164
+ '\x1B[32m' +
1165
+ 'green text'.repeat(200) +
1166
+ '\x1B[0m';
1167
+ expect(largeTextWithAnsi.length).toBeGreaterThan(5000);
1168
+ act(() => result.current.handleInput({
1169
+ name: '',
1170
+ ctrl: false,
1171
+ meta: false,
1172
+ shift: false,
1173
+ paste: false,
1174
+ sequence: largeTextWithAnsi,
1175
+ }));
1176
+ const resultText = getBufferState(result).text;
1177
+ expect(resultText).not.toContain('\x1B[31m');
1178
+ expect(resultText).not.toContain('\x1B[32m');
1179
+ expect(resultText).not.toContain('\x1B[0m');
1180
+ expect(resultText).toContain('red text');
1181
+ expect(resultText).toContain('green text');
1182
+ });
1183
+ it('should not strip popular emojis', () => {
1184
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath: () => false }));
1185
+ const emojis = '🐍🐳🦀🦄';
1186
+ act(() => result.current.handleInput({
1187
+ name: '',
1188
+ ctrl: false,
1189
+ meta: false,
1190
+ shift: false,
1191
+ paste: false,
1192
+ sequence: emojis,
1193
+ }));
1194
+ expect(getBufferState(result).text).toBe(emojis);
1195
+ });
1196
+ });
1197
+ describe('stripAnsi', () => {
1198
+ it('should correctly strip ANSI escape codes', () => {
1199
+ const textWithAnsi = '\x1B[31mHello\x1B[0m World';
1200
+ expect(stripAnsi(textWithAnsi)).toBe('Hello World');
1201
+ });
1202
+ it('should handle multiple ANSI codes', () => {
1203
+ const textWithMultipleAnsi = '\x1B[1m\x1B[34mBold Blue\x1B[0m Text';
1204
+ expect(stripAnsi(textWithMultipleAnsi)).toBe('Bold Blue Text');
1205
+ });
1206
+ it('should not modify text without ANSI codes', () => {
1207
+ const plainText = 'Plain text';
1208
+ expect(stripAnsi(plainText)).toBe('Plain text');
1209
+ });
1210
+ it('should handle empty string', () => {
1211
+ expect(stripAnsi('')).toBe('');
1212
+ });
1213
+ });
1214
+ describe('Memoization', () => {
1215
+ it('should keep action references stable across re-renders', () => {
1216
+ // We pass a stable `isValidPath` so that callbacks that depend on it
1217
+ // are not recreated on every render.
1218
+ const isValidPath = () => false;
1219
+ const { result, rerender } = renderHook(() => useTextBuffer({ viewport, isValidPath }));
1220
+ const initialInsert = result.current.insert;
1221
+ const initialBackspace = result.current.backspace;
1222
+ const initialMove = result.current.move;
1223
+ const initialHandleInput = result.current.handleInput;
1224
+ rerender();
1225
+ expect(result.current.insert).toBe(initialInsert);
1226
+ expect(result.current.backspace).toBe(initialBackspace);
1227
+ expect(result.current.move).toBe(initialMove);
1228
+ expect(result.current.handleInput).toBe(initialHandleInput);
1229
+ });
1230
+ it('should have memoized actions that operate on the latest state', () => {
1231
+ const isValidPath = () => false;
1232
+ const { result } = renderHook(() => useTextBuffer({ viewport, isValidPath }));
1233
+ // Store a reference to the memoized insert function.
1234
+ const memoizedInsert = result.current.insert;
1235
+ // Update the buffer state.
1236
+ act(() => {
1237
+ result.current.insert('hello');
1238
+ });
1239
+ expect(getBufferState(result).text).toBe('hello');
1240
+ // Now, call the original memoized function reference.
1241
+ act(() => {
1242
+ memoizedInsert(' world');
1243
+ });
1244
+ // It should have operated on the updated state.
1245
+ expect(getBufferState(result).text).toBe('hello world');
1246
+ });
1247
+ });
1248
+ });
1249
+ describe('offsetToLogicalPos', () => {
1250
+ it('should return [0,0] for offset 0', () => {
1251
+ expect(offsetToLogicalPos('any text', 0)).toEqual([0, 0]);
1252
+ });
1253
+ it('should handle single line text', () => {
1254
+ const text = 'hello';
1255
+ expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]); // Start
1256
+ expect(offsetToLogicalPos(text, 2)).toEqual([0, 2]); // Middle 'l'
1257
+ expect(offsetToLogicalPos(text, 5)).toEqual([0, 5]); // End
1258
+ expect(offsetToLogicalPos(text, 10)).toEqual([0, 5]); // Beyond end
1259
+ });
1260
+ it('should handle multi-line text', () => {
1261
+ const text = 'hello\nworld\n123';
1262
+ // "hello" (5) + \n (1) + "world" (5) + \n (1) + "123" (3)
1263
+ // h e l l o \n w o r l d \n 1 2 3
1264
+ // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
1265
+ // Line 0: "hello" (length 5)
1266
+ expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]); // Start of 'hello'
1267
+ expect(offsetToLogicalPos(text, 3)).toEqual([0, 3]); // 'l' in 'hello'
1268
+ expect(offsetToLogicalPos(text, 5)).toEqual([0, 5]); // End of 'hello' (before \n)
1269
+ // Line 1: "world" (length 5)
1270
+ expect(offsetToLogicalPos(text, 6)).toEqual([1, 0]); // Start of 'world' (after \n)
1271
+ expect(offsetToLogicalPos(text, 8)).toEqual([1, 2]); // 'r' in 'world'
1272
+ expect(offsetToLogicalPos(text, 11)).toEqual([1, 5]); // End of 'world' (before \n)
1273
+ // Line 2: "123" (length 3)
1274
+ expect(offsetToLogicalPos(text, 12)).toEqual([2, 0]); // Start of '123' (after \n)
1275
+ expect(offsetToLogicalPos(text, 13)).toEqual([2, 1]); // '2' in '123'
1276
+ expect(offsetToLogicalPos(text, 15)).toEqual([2, 3]); // End of '123'
1277
+ expect(offsetToLogicalPos(text, 20)).toEqual([2, 3]); // Beyond end of text
1278
+ });
1279
+ it('should handle empty lines', () => {
1280
+ const text = 'a\n\nc'; // "a" (1) + \n (1) + "" (0) + \n (1) + "c" (1)
1281
+ expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]); // 'a'
1282
+ expect(offsetToLogicalPos(text, 1)).toEqual([0, 1]); // End of 'a'
1283
+ expect(offsetToLogicalPos(text, 2)).toEqual([1, 0]); // Start of empty line
1284
+ expect(offsetToLogicalPos(text, 3)).toEqual([2, 0]); // Start of 'c'
1285
+ expect(offsetToLogicalPos(text, 4)).toEqual([2, 1]); // End of 'c'
1286
+ });
1287
+ it('should handle text ending with a newline', () => {
1288
+ const text = 'hello\n'; // "hello" (5) + \n (1)
1289
+ expect(offsetToLogicalPos(text, 5)).toEqual([0, 5]); // End of 'hello'
1290
+ expect(offsetToLogicalPos(text, 6)).toEqual([1, 0]); // Position on the new empty line after
1291
+ expect(offsetToLogicalPos(text, 7)).toEqual([1, 0]); // Still on the new empty line
1292
+ });
1293
+ it('should handle text starting with a newline', () => {
1294
+ const text = '\nhello'; // "" (0) + \n (1) + "hello" (5)
1295
+ expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]); // Start of first empty line
1296
+ expect(offsetToLogicalPos(text, 1)).toEqual([1, 0]); // Start of 'hello'
1297
+ expect(offsetToLogicalPos(text, 3)).toEqual([1, 2]); // 'l' in 'hello'
1298
+ });
1299
+ it('should handle empty string input', () => {
1300
+ expect(offsetToLogicalPos('', 0)).toEqual([0, 0]);
1301
+ expect(offsetToLogicalPos('', 5)).toEqual([0, 0]);
1302
+ });
1303
+ it('should handle multi-byte unicode characters correctly', () => {
1304
+ const text = '你好\n世界'; // "你好" (2 chars) + \n (1) + "世界" (2 chars)
1305
+ // Total "code points" for offset calculation: 2 + 1 + 2 = 5
1306
+ expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]); // Start of '你好'
1307
+ expect(offsetToLogicalPos(text, 1)).toEqual([0, 1]); // After '你', before '好'
1308
+ expect(offsetToLogicalPos(text, 2)).toEqual([0, 2]); // End of '你好'
1309
+ expect(offsetToLogicalPos(text, 3)).toEqual([1, 0]); // Start of '世界'
1310
+ expect(offsetToLogicalPos(text, 4)).toEqual([1, 1]); // After '世', before '界'
1311
+ expect(offsetToLogicalPos(text, 5)).toEqual([1, 2]); // End of '世界'
1312
+ expect(offsetToLogicalPos(text, 6)).toEqual([1, 2]); // Beyond end
1313
+ });
1314
+ it('should handle offset exactly at newline character', () => {
1315
+ const text = 'abc\ndef';
1316
+ // a b c \n d e f
1317
+ // 0 1 2 3 4 5 6
1318
+ expect(offsetToLogicalPos(text, 3)).toEqual([0, 3]); // End of 'abc'
1319
+ // The next character is the newline, so an offset of 4 means the start of the next line.
1320
+ expect(offsetToLogicalPos(text, 4)).toEqual([1, 0]); // Start of 'def'
1321
+ });
1322
+ it('should handle offset in the middle of a multi-byte character (should place at start of that char)', () => {
1323
+ // This scenario is tricky as "offset" is usually character-based.
1324
+ // Assuming cpLen and related logic handles this by treating multi-byte as one unit.
1325
+ // The current implementation of offsetToLogicalPos uses cpLen, so it should be code-point aware.
1326
+ const text = '🐶🐱'; // 2 code points
1327
+ expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]);
1328
+ expect(offsetToLogicalPos(text, 1)).toEqual([0, 1]); // After 🐶
1329
+ expect(offsetToLogicalPos(text, 2)).toEqual([0, 2]); // After 🐱
1330
+ });
1331
+ });
1332
+ describe('logicalPosToOffset', () => {
1333
+ it('should convert row/col position to offset correctly', () => {
1334
+ const lines = ['hello', 'world', '123'];
1335
+ // Line 0: "hello" (5 chars)
1336
+ expect(logicalPosToOffset(lines, 0, 0)).toBe(0); // Start of 'hello'
1337
+ expect(logicalPosToOffset(lines, 0, 3)).toBe(3); // 'l' in 'hello'
1338
+ expect(logicalPosToOffset(lines, 0, 5)).toBe(5); // End of 'hello'
1339
+ // Line 1: "world" (5 chars), offset starts at 6 (5 + 1 for newline)
1340
+ expect(logicalPosToOffset(lines, 1, 0)).toBe(6); // Start of 'world'
1341
+ expect(logicalPosToOffset(lines, 1, 2)).toBe(8); // 'r' in 'world'
1342
+ expect(logicalPosToOffset(lines, 1, 5)).toBe(11); // End of 'world'
1343
+ // Line 2: "123" (3 chars), offset starts at 12 (5 + 1 + 5 + 1)
1344
+ expect(logicalPosToOffset(lines, 2, 0)).toBe(12); // Start of '123'
1345
+ expect(logicalPosToOffset(lines, 2, 1)).toBe(13); // '2' in '123'
1346
+ expect(logicalPosToOffset(lines, 2, 3)).toBe(15); // End of '123'
1347
+ });
1348
+ it('should handle empty lines', () => {
1349
+ const lines = ['a', '', 'c'];
1350
+ expect(logicalPosToOffset(lines, 0, 0)).toBe(0); // 'a'
1351
+ expect(logicalPosToOffset(lines, 0, 1)).toBe(1); // End of 'a'
1352
+ expect(logicalPosToOffset(lines, 1, 0)).toBe(2); // Empty line
1353
+ expect(logicalPosToOffset(lines, 2, 0)).toBe(3); // 'c'
1354
+ expect(logicalPosToOffset(lines, 2, 1)).toBe(4); // End of 'c'
1355
+ });
1356
+ it('should handle single empty line', () => {
1357
+ const lines = [''];
1358
+ expect(logicalPosToOffset(lines, 0, 0)).toBe(0);
1359
+ });
1360
+ it('should be inverse of offsetToLogicalPos', () => {
1361
+ const lines = ['hello', 'world', '123'];
1362
+ const text = lines.join('\n');
1363
+ // Test round-trip conversion
1364
+ for (let offset = 0; offset <= text.length; offset++) {
1365
+ const [row, col] = offsetToLogicalPos(text, offset);
1366
+ const convertedOffset = logicalPosToOffset(lines, row, col);
1367
+ expect(convertedOffset).toBe(offset);
1368
+ }
1369
+ });
1370
+ it('should handle out-of-bounds positions', () => {
1371
+ const lines = ['hello'];
1372
+ // Beyond end of line
1373
+ expect(logicalPosToOffset(lines, 0, 10)).toBe(5); // Clamps to end of line
1374
+ // Beyond array bounds - should clamp to the last line
1375
+ expect(logicalPosToOffset(lines, 5, 0)).toBe(0); // Clamps to start of last line (row 0)
1376
+ expect(logicalPosToOffset(lines, 5, 10)).toBe(5); // Clamps to end of last line
1377
+ });
1378
+ });
1379
+ // Helper to create state for reducer tests
1380
+ const createTestState = (lines, cursorRow, cursorCol, viewportWidth = 80) => {
1381
+ const text = lines.join('\n');
1382
+ let state = textBufferReducer(initialState, {
1383
+ type: 'set_text',
1384
+ payload: text,
1385
+ });
1386
+ state = textBufferReducer(state, {
1387
+ type: 'set_cursor',
1388
+ payload: { cursorRow, cursorCol, preferredCol: null },
1389
+ });
1390
+ state = textBufferReducer(state, {
1391
+ type: 'set_viewport',
1392
+ payload: { width: viewportWidth, height: 24 },
1393
+ });
1394
+ return state;
1395
+ };
1396
+ describe('textBufferReducer vim operations', () => {
1397
+ describe('vim_delete_line', () => {
1398
+ it('should delete a single line including newline in multi-line text', () => {
1399
+ const state = createTestState(['line1', 'line2', 'line3'], 1, 2);
1400
+ const action = {
1401
+ type: 'vim_delete_line',
1402
+ payload: { count: 1 },
1403
+ };
1404
+ const result = textBufferReducer(state, action);
1405
+ expect(result).toHaveOnlyValidCharacters();
1406
+ // After deleting line2, we should have line1 and line3, with cursor on line3 (now at index 1)
1407
+ expect(result.lines).toEqual(['line1', 'line3']);
1408
+ expect(result.cursorRow).toBe(1);
1409
+ expect(result.cursorCol).toBe(0);
1410
+ });
1411
+ it('should delete multiple lines when count > 1', () => {
1412
+ const state = createTestState(['line1', 'line2', 'line3', 'line4'], 1, 0);
1413
+ const action = {
1414
+ type: 'vim_delete_line',
1415
+ payload: { count: 2 },
1416
+ };
1417
+ const result = textBufferReducer(state, action);
1418
+ expect(result).toHaveOnlyValidCharacters();
1419
+ // Should delete line2 and line3, leaving line1 and line4
1420
+ expect(result.lines).toEqual(['line1', 'line4']);
1421
+ expect(result.cursorRow).toBe(1);
1422
+ expect(result.cursorCol).toBe(0);
1423
+ });
1424
+ it('should clear single line content when only one line exists', () => {
1425
+ const state = createTestState(['only line'], 0, 5);
1426
+ const action = {
1427
+ type: 'vim_delete_line',
1428
+ payload: { count: 1 },
1429
+ };
1430
+ const result = textBufferReducer(state, action);
1431
+ expect(result).toHaveOnlyValidCharacters();
1432
+ // Should clear the line content but keep the line
1433
+ expect(result.lines).toEqual(['']);
1434
+ expect(result.cursorRow).toBe(0);
1435
+ expect(result.cursorCol).toBe(0);
1436
+ });
1437
+ it('should handle deleting the last line properly', () => {
1438
+ const state = createTestState(['line1', 'line2'], 1, 0);
1439
+ const action = {
1440
+ type: 'vim_delete_line',
1441
+ payload: { count: 1 },
1442
+ };
1443
+ const result = textBufferReducer(state, action);
1444
+ expect(result).toHaveOnlyValidCharacters();
1445
+ // Should delete the last line completely, not leave empty line
1446
+ expect(result.lines).toEqual(['line1']);
1447
+ expect(result.cursorRow).toBe(0);
1448
+ expect(result.cursorCol).toBe(0);
1449
+ });
1450
+ it('should handle deleting all lines and maintain valid state for subsequent paste', () => {
1451
+ const state = createTestState(['line1', 'line2', 'line3', 'line4'], 0, 0);
1452
+ // Delete all 4 lines with 4dd
1453
+ const deleteAction = {
1454
+ type: 'vim_delete_line',
1455
+ payload: { count: 4 },
1456
+ };
1457
+ const afterDelete = textBufferReducer(state, deleteAction);
1458
+ expect(afterDelete).toHaveOnlyValidCharacters();
1459
+ // After deleting all lines, should have one empty line
1460
+ expect(afterDelete.lines).toEqual(['']);
1461
+ expect(afterDelete.cursorRow).toBe(0);
1462
+ expect(afterDelete.cursorCol).toBe(0);
1463
+ // Now paste multiline content - this should work correctly
1464
+ const pasteAction = {
1465
+ type: 'insert',
1466
+ payload: 'new1\nnew2\nnew3\nnew4',
1467
+ };
1468
+ const afterPaste = textBufferReducer(afterDelete, pasteAction);
1469
+ expect(afterPaste).toHaveOnlyValidCharacters();
1470
+ // All lines including the first one should be present
1471
+ expect(afterPaste.lines).toEqual(['new1', 'new2', 'new3', 'new4']);
1472
+ expect(afterPaste.cursorRow).toBe(3);
1473
+ expect(afterPaste.cursorCol).toBe(4);
1474
+ });
1475
+ });
1476
+ });
1477
+ describe('Unicode helper functions', () => {
1478
+ describe('findWordEndInLine with Unicode', () => {
1479
+ it('should handle combining characters', () => {
1480
+ // café with combining accent
1481
+ const cafeWithCombining = 'cafe\u0301';
1482
+ const result = findWordEndInLine(cafeWithCombining + ' test', 0);
1483
+ expect(result).toBe(3); // End of 'café' at base character 'e', not combining accent
1484
+ });
1485
+ it('should handle precomposed characters with diacritics', () => {
1486
+ // café with precomposed é (U+00E9)
1487
+ const cafePrecomposed = 'café';
1488
+ const result = findWordEndInLine(cafePrecomposed + ' test', 0);
1489
+ expect(result).toBe(3); // End of 'café' at precomposed character 'é'
1490
+ });
1491
+ it('should return null when no word end found', () => {
1492
+ const result = findWordEndInLine(' ', 0);
1493
+ expect(result).toBeNull(); // No word end found in whitespace-only string string
1494
+ });
1495
+ });
1496
+ describe('findNextWordStartInLine with Unicode', () => {
1497
+ it('should handle right-to-left text', () => {
1498
+ const result = findNextWordStartInLine('hello مرحبا world', 0);
1499
+ expect(result).toBe(6); // Start of Arabic word
1500
+ });
1501
+ it('should handle Chinese characters', () => {
1502
+ const result = findNextWordStartInLine('hello 你好 world', 0);
1503
+ expect(result).toBe(6); // Start of Chinese word
1504
+ });
1505
+ it('should return null at end of line', () => {
1506
+ const result = findNextWordStartInLine('hello', 10);
1507
+ expect(result).toBeNull();
1508
+ });
1509
+ it('should handle combining characters', () => {
1510
+ // café with combining accent + next word
1511
+ const textWithCombining = 'cafe\u0301 test';
1512
+ const result = findNextWordStartInLine(textWithCombining, 0);
1513
+ expect(result).toBe(6); // Start of 'test' after 'café ' (combining char makes string longer)
1514
+ });
1515
+ it('should handle precomposed characters with diacritics', () => {
1516
+ // café with precomposed é + next word
1517
+ const textPrecomposed = 'café test';
1518
+ const result = findNextWordStartInLine(textPrecomposed, 0);
1519
+ expect(result).toBe(5); // Start of 'test' after 'café '
1520
+ });
1521
+ });
1522
+ describe('isWordCharStrict with Unicode', () => {
1523
+ it('should return true for ASCII word characters', () => {
1524
+ expect(isWordCharStrict('a')).toBe(true);
1525
+ expect(isWordCharStrict('Z')).toBe(true);
1526
+ expect(isWordCharStrict('0')).toBe(true);
1527
+ expect(isWordCharStrict('_')).toBe(true);
1528
+ });
1529
+ it('should return false for punctuation', () => {
1530
+ expect(isWordCharStrict('.')).toBe(false);
1531
+ expect(isWordCharStrict(',')).toBe(false);
1532
+ expect(isWordCharStrict('!')).toBe(false);
1533
+ });
1534
+ it('should return true for non-Latin scripts', () => {
1535
+ expect(isWordCharStrict('你')).toBe(true); // Chinese character
1536
+ expect(isWordCharStrict('م')).toBe(true); // Arabic character
1537
+ });
1538
+ it('should return false for whitespace', () => {
1539
+ expect(isWordCharStrict(' ')).toBe(false);
1540
+ expect(isWordCharStrict('\t')).toBe(false);
1541
+ });
1542
+ });
1543
+ describe('cpLen with Unicode', () => {
1544
+ it('should handle combining characters', () => {
1545
+ expect(cpLen('é')).toBe(1); // Precomposed
1546
+ expect(cpLen('e\u0301')).toBe(2); // e + combining acute
1547
+ });
1548
+ it('should handle Chinese and Arabic text', () => {
1549
+ expect(cpLen('hello 你好 world')).toBe(14); // 5 + 1 + 2 + 1 + 5 = 14
1550
+ expect(cpLen('hello مرحبا world')).toBe(17);
1551
+ });
1552
+ });
1553
+ });
1554
+ //# sourceMappingURL=text-buffer.test.js.map