@zyro-sec/tui 1.0.2 → 1.0.3

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 (573) hide show
  1. package/AGENTS.md +6 -6
  2. package/bunfig.toml +4 -4
  3. package/package.json +11 -11
  4. package/src/app.tsx +1632 -1632
  5. package/src/attention-sounds.bun.ts +8 -8
  6. package/src/attention-sounds.node.ts +16 -16
  7. package/src/attention.ts +189 -189
  8. package/src/audio.d.ts +9 -9
  9. package/src/audio.ts +49 -49
  10. package/src/clipboard.ts +68 -68
  11. package/src/component/background-image.tsx +26 -26
  12. package/src/component/command-palette.tsx +74 -74
  13. package/src/component/devtools-bar.tsx +611 -611
  14. package/src/component/dialog-agent.tsx +33 -33
  15. package/src/component/dialog-background.tsx +102 -102
  16. package/src/component/dialog-config.tsx +494 -494
  17. package/src/component/dialog-debug.tsx +90 -90
  18. package/src/component/dialog-error-details.tsx +130 -130
  19. package/src/component/dialog-experiments.tsx +101 -101
  20. package/src/component/dialog-go-upsell.tsx +448 -448
  21. package/src/component/dialog-highlights.tsx +88 -88
  22. package/src/component/dialog-image-preview.tsx +85 -85
  23. package/src/component/dialog-install-mcp.tsx +25 -3
  24. package/src/component/dialog-integration.tsx +1332 -1332
  25. package/src/component/dialog-mcp.tsx +184 -184
  26. package/src/component/dialog-model-free.tsx +79 -79
  27. package/src/component/dialog-model.tsx +238 -238
  28. package/src/component/dialog-open.tsx +509 -509
  29. package/src/component/dialog-pair.tsx +123 -123
  30. package/src/component/dialog-session-list.tsx +328 -328
  31. package/src/component/dialog-session-rename.tsx +37 -37
  32. package/src/component/dialog-shell-output.tsx +194 -194
  33. package/src/component/dialog-skill.tsx +93 -93
  34. package/src/component/dialog-speed.tsx +48 -48
  35. package/src/component/dialog-stash.tsx +90 -90
  36. package/src/component/dialog-status.tsx +50 -50
  37. package/src/component/dialog-theme-list.tsx +56 -56
  38. package/src/component/dialog-update.tsx +144 -144
  39. package/src/component/dialog-variant.tsx +43 -43
  40. package/src/component/dialog-workspace-file-changes.tsx +187 -187
  41. package/src/component/dialog-workspaces.tsx +413 -413
  42. package/src/component/dialog-worktree-name.tsx +40 -40
  43. package/src/component/error-component.tsx +258 -258
  44. package/src/component/fade-in-text.tsx +113 -113
  45. package/src/component/go-upsell-art.ts +338 -338
  46. package/src/component/logo.tsx +79 -79
  47. package/src/component/masked-text.ts +59 -59
  48. package/src/component/media-source.ts +43 -43
  49. package/src/component/migration-overlay.tsx +73 -73
  50. package/src/component/model-options.tsx +121 -121
  51. package/src/component/one-cell-spinner.tsx +75 -75
  52. package/src/component/panel-host.tsx +64 -64
  53. package/src/component/patch-diff.tsx +211 -211
  54. package/src/component/plugin-route-missing.tsx +21 -21
  55. package/src/component/prompt/autocomplete.tsx +1005 -1005
  56. package/src/component/prompt/draft-stash.ts +47 -47
  57. package/src/component/prompt/index.tsx +2080 -2080
  58. package/src/component/prompt/local-attachment.ts +188 -188
  59. package/src/component/prompt/metadata.tsx +193 -193
  60. package/src/component/prompt/move.tsx +181 -181
  61. package/src/component/reconnecting.tsx +40 -40
  62. package/src/component/retry-provider.tsx +202 -202
  63. package/src/component/session-frame.tsx +408 -408
  64. package/src/component/session-tabs-rail.tsx +94 -94
  65. package/src/component/session-tabs.tsx +1789 -1789
  66. package/src/component/shimmer-text.tsx +64 -64
  67. package/src/component/spinner-frames.ts +1 -1
  68. package/src/component/spinner-renderable.ts +126 -126
  69. package/src/component/spinner.tsx +50 -50
  70. package/src/component/startup-loading.tsx +63 -63
  71. package/src/component/tab-pulse.tsx +819 -819
  72. package/src/component/terminal-pane.tsx +377 -377
  73. package/src/component/theme-error-toast.tsx +20 -20
  74. package/src/component/title-shimmer.tsx +272 -272
  75. package/src/component/todo-item.tsx +27 -27
  76. package/src/component/usage.tsx +329 -329
  77. package/src/component/use-connected.tsx +14 -14
  78. package/src/config/index.tsx +438 -438
  79. package/src/config/keybind.ts +367 -367
  80. package/src/config/new-session-location.ts +16 -16
  81. package/src/config/v1/index.tsx +149 -149
  82. package/src/config/v1/keybind.ts +463 -463
  83. package/src/context/args.tsx +31 -31
  84. package/src/context/attention.tsx +23 -23
  85. package/src/context/client-mcp.tsx +119 -119
  86. package/src/context/client.tsx +76 -76
  87. package/src/context/clipboard.tsx +19 -19
  88. package/src/context/data.tsx +69 -69
  89. package/src/context/editor.ts +408 -408
  90. package/src/context/epilogue.tsx +14 -14
  91. package/src/context/event.ts +32 -32
  92. package/src/context/exit.tsx +8 -8
  93. package/src/context/helper.tsx +26 -26
  94. package/src/context/interactivity.tsx +20 -20
  95. package/src/context/keymap.tsx +515 -515
  96. package/src/context/local.tsx +723 -723
  97. package/src/context/location.tsx +80 -80
  98. package/src/context/log.tsx +24 -24
  99. package/src/context/panel.tsx +59 -59
  100. package/src/context/path-format.tsx +12 -12
  101. package/src/context/permission.tsx +18 -18
  102. package/src/context/program-status.ts +94 -94
  103. package/src/context/prompt.tsx +18 -18
  104. package/src/context/route.tsx +73 -73
  105. package/src/context/runtime.tsx +98 -98
  106. package/src/context/session-tabs-model.ts +263 -263
  107. package/src/context/session-tabs.tsx +508 -508
  108. package/src/context/session-terminals.tsx +136 -136
  109. package/src/context/storage.tsx +150 -150
  110. package/src/context/theme.tsx +447 -447
  111. package/src/context/thinking.ts +20 -20
  112. package/src/context/update-notification.tsx +158 -158
  113. package/src/devtools/index.ts +41 -41
  114. package/src/editor-zed-sqlite.bun.ts +1 -1
  115. package/src/editor-zed-sqlite.node.ts +21 -21
  116. package/src/editor-zed.ts +277 -277
  117. package/src/editor.ts +86 -86
  118. package/src/feature-plugins/home/footer.tsx +116 -116
  119. package/src/feature-plugins/prompt/btw.tsx +190 -190
  120. package/src/feature-plugins/prompt/footer.tsx +163 -163
  121. package/src/feature-plugins/prompt/goal.tsx +211 -211
  122. package/src/feature-plugins/sidebar/context.tsx +52 -52
  123. package/src/feature-plugins/sidebar/footer.tsx +126 -126
  124. package/src/feature-plugins/sidebar/limit.tsx +47 -47
  125. package/src/feature-plugins/sidebar/mcp.tsx +103 -103
  126. package/src/feature-plugins/sidebar/todo.tsx +58 -58
  127. package/src/feature-plugins/system/diff-viewer-file-menu.tsx +87 -87
  128. package/src/feature-plugins/system/diff-viewer-file-tree-utils.ts +174 -174
  129. package/src/feature-plugins/system/diff-viewer-file-tree.tsx +274 -274
  130. package/src/feature-plugins/system/diff-viewer-image.tsx +96 -96
  131. package/src/feature-plugins/system/diff-viewer.tsx +1380 -1380
  132. package/src/feature-plugins/system/notifications.ts +102 -102
  133. package/src/feature-plugins/system/plugins.tsx +356 -356
  134. package/src/feature-plugins/system/stats-data.ts +18 -18
  135. package/src/feature-plugins/system/stats.tsx +217 -217
  136. package/src/feature-plugins/system/storybook/footer.tsx +51 -51
  137. package/src/feature-plugins/system/storybook/highlights.tsx +71 -71
  138. package/src/feature-plugins/system/storybook/index.tsx +153 -153
  139. package/src/feature-plugins/system/storybook/link-clicks.tsx +125 -125
  140. package/src/feature-plugins/system/storybook/merman-layouts.tsx +200 -200
  141. package/src/feature-plugins/system/storybook/one-cell-spinner.fixtures.ts +144 -144
  142. package/src/feature-plugins/system/storybook/one-cell-spinner.tsx +293 -293
  143. package/src/feature-plugins/system/storybook/session-location-missing.tsx +80 -80
  144. package/src/feature-plugins/system/storybook/session-tabs.tsx +571 -571
  145. package/src/feature-plugins/system/storybook/subcell-spinner.fixtures.ts +115 -115
  146. package/src/feature-plugins/system/storybook/todo.tsx +166 -166
  147. package/src/index.tsx +2 -2
  148. package/src/logo.ts +21 -21
  149. package/src/mini/catalog.shared.ts +116 -116
  150. package/src/mini/demo.ts +1086 -1086
  151. package/src/mini/entry.body.ts +244 -244
  152. package/src/mini/footer.command.tsx +1205 -1205
  153. package/src/mini/footer.form.tsx +647 -647
  154. package/src/mini/footer.menu.tsx +313 -313
  155. package/src/mini/footer.permission.tsx +562 -562
  156. package/src/mini/footer.prompt.tsx +1533 -1533
  157. package/src/mini/footer.subagent.tsx +268 -268
  158. package/src/mini/footer.ts +1193 -1193
  159. package/src/mini/footer.view.tsx +1022 -1022
  160. package/src/mini/footer.width.ts +118 -118
  161. package/src/mini/form.shared.ts +241 -241
  162. package/src/mini/index.ts +7 -7
  163. package/src/mini/mono.ts +277 -277
  164. package/src/mini/permission.shared.ts +199 -199
  165. package/src/mini/prompt.editor.ts +74 -74
  166. package/src/mini/prompt.shared.ts +192 -192
  167. package/src/mini/runtime.boot.ts +98 -98
  168. package/src/mini/runtime.lifecycle.ts +393 -393
  169. package/src/mini/runtime.queue.ts +320 -320
  170. package/src/mini/runtime.ts +1117 -1117
  171. package/src/mini/scrollback.shared.ts +82 -82
  172. package/src/mini/scrollback.surface.ts +548 -548
  173. package/src/mini/scrollback.writer.tsx +380 -380
  174. package/src/mini/session-data.ts +13 -13
  175. package/src/mini/session.shared.ts +113 -113
  176. package/src/mini/splash.ts +307 -307
  177. package/src/mini/stream-v2.fragment.ts +83 -83
  178. package/src/mini/stream-v2.image.ts +38 -38
  179. package/src/mini/stream-v2.subagent.ts +1130 -1130
  180. package/src/mini/stream-v2.transport.ts +1916 -1916
  181. package/src/mini/stream.ts +152 -152
  182. package/src/mini/theme.ts +328 -328
  183. package/src/mini/tool.public.ts +3 -3
  184. package/src/mini/tool.ts +1394 -1394
  185. package/src/mini/turn-summary.ts +21 -21
  186. package/src/mini/types.ts +482 -482
  187. package/src/mini/variant.shared.ts +58 -58
  188. package/src/mini/verbosity.ts +84 -84
  189. package/src/model-preference.ts +206 -206
  190. package/src/parsers-config.ts +386 -386
  191. package/src/plugin/api.tsx +486 -486
  192. package/src/plugin/builtins.ts +35 -35
  193. package/src/plugin/context.tsx +757 -757
  194. package/src/plugin/discovery.ts +67 -67
  195. package/src/plugin/markdown.ts +18 -18
  196. package/src/plugin/render.tsx +116 -116
  197. package/src/plugin/runtime-plugin-support.bun.ts +11 -11
  198. package/src/plugin/runtime-plugin-support.node.ts +1 -1
  199. package/src/plugin/source.ts +1 -1
  200. package/src/plugin/structure.ts +159 -159
  201. package/src/plugin/watch.ts +113 -113
  202. package/src/prompt/attachment.ts +97 -97
  203. package/src/prompt/codec.ts +61 -61
  204. package/src/prompt/content.ts +22 -22
  205. package/src/prompt/directory-completion.ts +97 -97
  206. package/src/prompt/directory-recents.ts +36 -36
  207. package/src/prompt/display.ts +41 -41
  208. package/src/prompt/frecency.tsx +80 -80
  209. package/src/prompt/history.tsx +175 -175
  210. package/src/prompt/mention.ts +173 -173
  211. package/src/prompt/parse.ts +25 -25
  212. package/src/prompt/part.ts +35 -35
  213. package/src/prompt/restore.ts +39 -39
  214. package/src/prompt/stash.tsx +93 -93
  215. package/src/prompt/traits.ts +29 -29
  216. package/src/routes/home.tsx +180 -180
  217. package/src/routes/session/activity-summary.ts +101 -101
  218. package/src/routes/session/anchor-view.tsx +53 -53
  219. package/src/routes/session/anchors.ts +85 -85
  220. package/src/routes/session/composer/context.ts +24 -24
  221. package/src/routes/session/composer/index.tsx +143 -143
  222. package/src/routes/session/composer/scripts-tab.tsx +166 -166
  223. package/src/routes/session/composer/shell-tab.tsx +149 -149
  224. package/src/routes/session/composer/subagents-tab.tsx +264 -264
  225. package/src/routes/session/composer/tabs.ts +91 -91
  226. package/src/routes/session/composer/terminals-tab.tsx +114 -114
  227. package/src/routes/session/dialog-execute.tsx +276 -276
  228. package/src/routes/session/dialog-fork.tsx +87 -87
  229. package/src/routes/session/dialog-message.tsx +89 -89
  230. package/src/routes/session/dialog-timeline.tsx +43 -43
  231. package/src/routes/session/form.tsx +1177 -1177
  232. package/src/routes/session/group-view.tsx +407 -407
  233. package/src/routes/session/grouping/session.ts +184 -184
  234. package/src/routes/session/grouping/tree.ts +111 -111
  235. package/src/routes/session/history.ts +59 -59
  236. package/src/routes/session/index.tsx +3922 -3922
  237. package/src/routes/session/location-missing.tsx +81 -81
  238. package/src/routes/session/message-navigation.ts +48 -48
  239. package/src/routes/session/message-parts.tsx +322 -322
  240. package/src/routes/session/mount-budget.ts +38 -38
  241. package/src/routes/session/permission.tsx +622 -622
  242. package/src/routes/session/render-context.tsx +44 -44
  243. package/src/routes/session/rows.ts +493 -493
  244. package/src/routes/session/sidebar.tsx +80 -80
  245. package/src/routes/session/thinking-syntax.ts +8 -8
  246. package/src/runtime.tsx +1 -1
  247. package/src/simulation/semantics.ts +12 -12
  248. package/src/theme/assets/aura.json +69 -69
  249. package/src/theme/assets/ayu.json +80 -80
  250. package/src/theme/assets/carbonfox.json +248 -248
  251. package/src/theme/assets/catppuccin-frappe.json +230 -230
  252. package/src/theme/assets/catppuccin-macchiato.json +230 -230
  253. package/src/theme/assets/catppuccin.json +112 -112
  254. package/src/theme/assets/cobalt2.json +225 -225
  255. package/src/theme/assets/cursor.json +249 -249
  256. package/src/theme/assets/dracula.json +219 -219
  257. package/src/theme/assets/everforest.json +241 -241
  258. package/src/theme/assets/flexoki.json +237 -237
  259. package/src/theme/assets/github.json +233 -233
  260. package/src/theme/assets/gruvbox.json +242 -242
  261. package/src/theme/assets/kanagawa.json +77 -77
  262. package/src/theme/assets/lucent-orng.json +234 -234
  263. package/src/theme/assets/material.json +235 -235
  264. package/src/theme/assets/matrix.json +77 -77
  265. package/src/theme/assets/mercury.json +252 -252
  266. package/src/theme/assets/monokai.json +221 -221
  267. package/src/theme/assets/nightowl.json +221 -221
  268. package/src/theme/assets/nord.json +223 -223
  269. package/src/theme/assets/one-dark.json +84 -84
  270. package/src/theme/assets/opencode.json +245 -245
  271. package/src/theme/assets/orng.json +249 -249
  272. package/src/theme/assets/osaka-jade.json +93 -93
  273. package/src/theme/assets/palenight.json +222 -222
  274. package/src/theme/assets/rosepine.json +234 -234
  275. package/src/theme/assets/solarized.json +223 -223
  276. package/src/theme/assets/synthwave84.json +226 -226
  277. package/src/theme/assets/tokyonight.json +243 -243
  278. package/src/theme/assets/v2/opencode-classic.json +387 -387
  279. package/src/theme/assets/v2/opencode.json +395 -395
  280. package/src/theme/assets/vercel.json +245 -245
  281. package/src/theme/assets/vesper.json +218 -218
  282. package/src/theme/assets/zenburn.json +223 -223
  283. package/src/theme/color.ts +48 -48
  284. package/src/theme/component.ts +51 -51
  285. package/src/theme/discovery.ts +21 -21
  286. package/src/theme/index.ts +109 -109
  287. package/src/theme/resolve.ts +53 -53
  288. package/src/theme/system.ts +144 -144
  289. package/src/theme/v1.ts +70 -70
  290. package/src/ui/animation.ts +235 -235
  291. package/src/ui/border.ts +21 -21
  292. package/src/ui/dialog-alert.tsx +51 -51
  293. package/src/ui/dialog-confirm.tsx +62 -62
  294. package/src/ui/dialog-export-options.tsx +316 -316
  295. package/src/ui/dialog-export-result.tsx +84 -84
  296. package/src/ui/dialog-help.tsx +40 -40
  297. package/src/ui/dialog-prompt.tsx +180 -180
  298. package/src/ui/dialog-select.tsx +1006 -1006
  299. package/src/ui/dialog.tsx +560 -560
  300. package/src/ui/file-path.tsx +112 -112
  301. package/src/ui/layout.ts +26 -26
  302. package/src/ui/link.tsx +51 -51
  303. package/src/ui/one-cell-motion.ts +111 -111
  304. package/src/ui/pane-resize-handle.tsx +33 -33
  305. package/src/ui/pane-resize.ts +76 -76
  306. package/src/ui/select-controller.ts +45 -45
  307. package/src/ui/session-link.ts +92 -92
  308. package/src/ui/spinner.ts +368 -368
  309. package/src/ui/subcell.ts +37 -37
  310. package/src/ui/toast.tsx +222 -222
  311. package/src/ui/working-directory-actions.tsx +66 -66
  312. package/src/util/collapse-tool-output.ts +95 -95
  313. package/src/util/config-directories.ts +53 -53
  314. package/src/util/connected-provider.ts +15 -15
  315. package/src/util/delayed-presence.ts +34 -34
  316. package/src/util/diff.ts +84 -84
  317. package/src/util/error-details.ts +21 -21
  318. package/src/util/error.ts +141 -141
  319. package/src/util/filetype.ts +135 -135
  320. package/src/util/form.ts +146 -146
  321. package/src/util/highlights.ts +51 -51
  322. package/src/util/locale.ts +81 -81
  323. package/src/util/marquee.ts +51 -51
  324. package/src/util/media.ts +43 -43
  325. package/src/util/model.ts +33 -33
  326. package/src/util/path-format.ts +37 -37
  327. package/src/util/path.ts +12 -12
  328. package/src/util/permission.ts +188 -188
  329. package/src/util/persistence.ts +42 -42
  330. package/src/util/presentation.ts +42 -42
  331. package/src/util/project.ts +7 -7
  332. package/src/util/record.ts +3 -3
  333. package/src/util/renderer.ts +15 -15
  334. package/src/util/scroll.ts +29 -29
  335. package/src/util/selection.ts +103 -103
  336. package/src/util/session.ts +103 -103
  337. package/src/util/signal.ts +49 -49
  338. package/src/util/single-flight.ts +12 -12
  339. package/src/util/string-width.bun.ts +1 -1
  340. package/src/util/string-width.node.ts +26 -26
  341. package/src/util/string-width.ts +1 -1
  342. package/src/util/system.ts +23 -23
  343. package/src/util/todo.ts +43 -43
  344. package/src/util/tool-display.ts +70 -70
  345. package/src/util/usage-limits.ts +152 -152
  346. package/src/util/watch.ts +24 -24
  347. package/test/app-lifecycle.test.tsx +2962 -2962
  348. package/test/clear-session.test.tsx +67 -67
  349. package/test/cli/tui/__snapshots__/inline-tool-wrap-snapshot.test.tsx.snap +22 -22
  350. package/test/cli/tui/command-palette.test.tsx +86 -86
  351. package/test/cli/tui/composer-keymap.test.tsx +346 -346
  352. package/test/cli/tui/data.test.tsx +3669 -3669
  353. package/test/cli/tui/dialog-integration.test.tsx +588 -588
  354. package/test/cli/tui/dialog-mcp.test.tsx +197 -197
  355. package/test/cli/tui/dialog-open.test.tsx +1037 -1037
  356. package/test/cli/tui/dialog-prompt.test.tsx +253 -253
  357. package/test/cli/tui/dialog-select.test.tsx +740 -740
  358. package/test/cli/tui/dialog-session-list.test.tsx +130 -130
  359. package/test/cli/tui/diff-viewer-file-tree.test.tsx +256 -256
  360. package/test/cli/tui/diff-viewer-image.test.tsx +172 -172
  361. package/test/cli/tui/diff-viewer.test.tsx +2510 -2510
  362. package/test/cli/tui/form.test.tsx +950 -950
  363. package/test/cli/tui/group-view.test.tsx +352 -352
  364. package/test/cli/tui/inline-tool-wrap-snapshot.test.tsx +248 -248
  365. package/test/cli/tui/input-scope.test.tsx +282 -282
  366. package/test/cli/tui/keymap-arguments.test.tsx +45 -45
  367. package/test/cli/tui/plugins-dialog.test.tsx +217 -217
  368. package/test/cli/tui/prompt-interrupt-status.test.tsx +25 -25
  369. package/test/cli/tui/prompt-move.test.tsx +446 -446
  370. package/test/cli/tui/reasoning-body.test.tsx +80 -80
  371. package/test/cli/tui/theme-mode.test.tsx +179 -179
  372. package/test/cli/tui/toast.test.tsx +117 -117
  373. package/test/cli/tui/use-event.test.tsx +295 -295
  374. package/test/client-mcp.test.tsx +226 -226
  375. package/test/command-selection.test.tsx +123 -123
  376. package/test/compact-admission.test.tsx +251 -251
  377. package/test/completion-notices.test.tsx +197 -197
  378. package/test/component/animation-ticks.test.tsx +212 -212
  379. package/test/component/dialog-error-details.test.tsx +110 -110
  380. package/test/component/dialog-export-options.test.tsx +68 -68
  381. package/test/component/dialog-highlights.test.tsx +73 -73
  382. package/test/component/dialog-model.test.tsx +145 -145
  383. package/test/component/dialog-shell-output.test.tsx +255 -255
  384. package/test/component/dialog-speed.test.tsx +114 -114
  385. package/test/component/dialog-update.test.tsx +111 -111
  386. package/test/component/dialog-variant.test.tsx +24 -24
  387. package/test/component/go-upsell-art.test.tsx +110 -110
  388. package/test/component/retry-provider.test.tsx +288 -288
  389. package/test/component/session-tabs-marquee.test.tsx +100 -100
  390. package/test/component/session-tabs-mouse.test.tsx +216 -216
  391. package/test/component/session-tabs-rail.test.tsx +120 -120
  392. package/test/component/session-tabs-status.test.tsx +345 -345
  393. package/test/component/shimmer-text.test.tsx +37 -37
  394. package/test/component/spinner-renderable.test.tsx +133 -133
  395. package/test/component/tab-pulse.test.tsx +196 -196
  396. package/test/config-v2.test.tsx +335 -335
  397. package/test/config.test.tsx +184 -184
  398. package/test/context/local-selection.test.tsx +204 -204
  399. package/test/context/session-tabs.test.tsx +864 -864
  400. package/test/context/theme.test.tsx +183 -183
  401. package/test/copy-session-id.test.tsx +79 -79
  402. package/test/feature-plugins/goal.test.tsx +126 -126
  403. package/test/feature-plugins/prompt-footer.test.tsx +303 -303
  404. package/test/feature-plugins/sidebar-context.test.tsx +70 -70
  405. package/test/feature-plugins/sidebar-footer.test.tsx +139 -139
  406. package/test/fixture/animation-ticks.ts +19 -19
  407. package/test/fixture/app.ts +95 -95
  408. package/test/fixture/diff-image.ts +7 -7
  409. package/test/fixture/fixture.ts +27 -27
  410. package/test/fixture/frame.ts +25 -25
  411. package/test/fixture/local.tsx +158 -158
  412. package/test/fixture/plugin/client-reads/tui.tsx +9 -9
  413. package/test/fixture/plugin/dialog-throw/tui.tsx +11 -11
  414. package/test/fixture/plugin/native-resources/tui.tsx +26 -26
  415. package/test/fixture/plugin/setup-keymap/tui.tsx +72 -72
  416. package/test/fixture/plugin/slot-string/tui.tsx +10 -10
  417. package/test/fixture/tui-client.ts +186 -186
  418. package/test/fixture/tui-environment.tsx +47 -47
  419. package/test/fixture/tui-runtime.ts +21 -21
  420. package/test/interactivity.test.tsx +46 -46
  421. package/test/keymap-scope.test.tsx +271 -271
  422. package/test/keymap.test.tsx +244 -244
  423. package/test/markdown-streaming.test.tsx +49 -49
  424. package/test/mini/blocker.responsive.test.tsx +732 -732
  425. package/test/mini/fixture/catalog.ts +63 -63
  426. package/test/mini/fixture/footer-api.ts +69 -69
  427. package/test/mini/fixture/tool-part.ts +20 -20
  428. package/test/mini/footer-keymap.test.tsx +141 -141
  429. package/test/mini/footer.responsive.test.tsx +438 -438
  430. package/test/mini/footer.view.test.tsx +2627 -2627
  431. package/test/new-session-id.test.tsx +102 -102
  432. package/test/plugin-keymap.test.tsx +143 -143
  433. package/test/plugin-markdown.test.tsx +120 -120
  434. package/test/plugin-model.test.tsx +46 -46
  435. package/test/plugin-native-resources.test.tsx +11 -11
  436. package/test/plugin-select.test.tsx +391 -391
  437. package/test/plugin-slot-output.test.tsx +23 -23
  438. package/test/plugin-toast.test.tsx +62 -62
  439. package/test/preload.ts +17 -17
  440. package/test/prompt/history-composer.test.tsx +52 -52
  441. package/test/prompt/history-provider.test.tsx +108 -108
  442. package/test/prompt-cursor.test.tsx +27 -27
  443. package/test/prompt-offset.test.tsx +58 -58
  444. package/test/prompt-paste.test.tsx +127 -127
  445. package/test/prompt-shell-failure.test.tsx +76 -76
  446. package/test/routes/composer-tabs.test.tsx +97 -97
  447. package/test/runtime.test.tsx +37 -37
  448. package/test/session-activity-anchor.test.tsx +180 -180
  449. package/test/session-backfill-disclosure.test.tsx +89 -89
  450. package/test/session-dialog-resync.test.tsx +122 -122
  451. package/test/session-fork-pending.test.tsx +60 -60
  452. package/test/session-group-navigation.test.tsx +252 -252
  453. package/test/session-history-interactions.test.tsx +305 -305
  454. package/test/session-history-reveal.test.tsx +108 -108
  455. package/test/session-home.test.tsx +401 -401
  456. package/test/session-interrupt-undo.test.tsx +98 -98
  457. package/test/session-media-refs.test.tsx +360 -360
  458. package/test/session-mount-budget.test.tsx +137 -137
  459. package/test/session-panel.test.tsx +721 -721
  460. package/test/session-shell-output.test.tsx +114 -114
  461. package/test/session-shell-stopped.test.tsx +107 -107
  462. package/test/session-subagent-navigation.test.tsx +44 -44
  463. package/test/session-submit-scroll.test.tsx +85 -85
  464. package/test/session-terminal-availability.test.tsx +80 -80
  465. package/test/session-wheel-focus.test.tsx +176 -176
  466. package/test/stats.view.test.tsx +94 -94
  467. package/test/theme/v2/types.typecheck.ts +57 -57
  468. package/test/ui/dialog-layout.test.tsx +56 -56
  469. package/test/ui/link.test.tsx +60 -60
  470. package/test/ui/pane-resize-handle.test.tsx +134 -134
  471. package/test/ui/session-link.test.tsx +325 -325
  472. package/test/usage-limits.test.tsx +162 -162
  473. package/test/util/selection-copy-on-select.test.tsx +64 -64
  474. package/tsconfig.json +13 -13
  475. package/test/cli/cmd/tui/integration-options.test.ts +0 -98
  476. package/test/cli/cmd/tui/model-options.test.ts +0 -105
  477. package/test/cli/cmd/tui/notifications.test.ts +0 -356
  478. package/test/cli/tui/anchors.test.ts +0 -77
  479. package/test/cli/tui/collapse-tool-output.test.ts +0 -14
  480. package/test/cli/tui/group-boundary.test.ts +0 -63
  481. package/test/cli/tui/grouping-tree.test.ts +0 -82
  482. package/test/cli/tui/message-navigation.test.ts +0 -164
  483. package/test/cli/tui/mount-budget.test.ts +0 -74
  484. package/test/cli/tui/permission.test.ts +0 -7
  485. package/test/cli/tui/script-changes.test.ts +0 -41
  486. package/test/cli/tui/session-grouping.test.ts +0 -21
  487. package/test/cli/tui/session-history.test.ts +0 -124
  488. package/test/cli/tui/session-rows.test.ts +0 -616
  489. package/test/cli/tui/session-verbosity.test.ts +0 -307
  490. package/test/cli/tui/thinking.test.ts +0 -36
  491. package/test/clipboard.test.ts +0 -148
  492. package/test/component/terminal-pane-theme.test.ts +0 -68
  493. package/test/context/local.test.ts +0 -15
  494. package/test/context/program-status.test.ts +0 -73
  495. package/test/context/session-retention.test.ts +0 -234
  496. package/test/context/session-tabs-model.test.ts +0 -341
  497. package/test/context/storage.test.ts +0 -37
  498. package/test/editor.test.ts +0 -69
  499. package/test/feature-plugins/diff-viewer-file-tree-utils.test.ts +0 -234
  500. package/test/feature-plugins/home-footer.test.ts +0 -13
  501. package/test/highlights.test.ts +0 -43
  502. package/test/keybind.test.ts +0 -7
  503. package/test/mini/catalog.shared.test.ts +0 -76
  504. package/test/mini/entry.body.test.ts +0 -622
  505. package/test/mini/footer.menu.test.ts +0 -43
  506. package/test/mini/footer.test.ts +0 -446
  507. package/test/mini/footer.width.test.ts +0 -120
  508. package/test/mini/form.shared.test.ts +0 -66
  509. package/test/mini/permission.shared.test.ts +0 -195
  510. package/test/mini/prompt.editor.test.ts +0 -118
  511. package/test/mini/prompt.shared.test.ts +0 -104
  512. package/test/mini/runtime.boot.test.ts +0 -118
  513. package/test/mini/runtime.queue.test.ts +0 -443
  514. package/test/mini/runtime.test.ts +0 -591
  515. package/test/mini/scrollback.image.test.ts +0 -213
  516. package/test/mini/scrollback.surface.test.ts +0 -1078
  517. package/test/mini/session.shared.test.ts +0 -267
  518. package/test/mini/splash.test.ts +0 -196
  519. package/test/mini/stream-v2.transport.test.ts +0 -4705
  520. package/test/mini/stream.test.ts +0 -52
  521. package/test/mini/theme.test.ts +0 -438
  522. package/test/mini/tool.test.ts +0 -134
  523. package/test/mini/variant.shared.test.ts +0 -58
  524. package/test/mini/verbosity.test.ts +0 -53
  525. package/test/model-preference.test.ts +0 -70
  526. package/test/new-session-location.test.ts +0 -30
  527. package/test/panel.test.ts +0 -71
  528. package/test/plugin-discovery.test.ts +0 -18
  529. package/test/plugin-source.test.ts +0 -871
  530. package/test/prompt/attachment.test.ts +0 -138
  531. package/test/prompt/autocomplete.test.ts +0 -218
  532. package/test/prompt/codec.test.ts +0 -136
  533. package/test/prompt/display.test.ts +0 -72
  534. package/test/prompt/draft-stash.test.ts +0 -42
  535. package/test/prompt/history.test.ts +0 -94
  536. package/test/prompt/jsonl.test.ts +0 -24
  537. package/test/prompt/local-attachment.test.ts +0 -244
  538. package/test/prompt/mention.test.ts +0 -119
  539. package/test/prompt/parse.test.ts +0 -46
  540. package/test/prompt/part.test.ts +0 -40
  541. package/test/prompt/persistence.test.ts +0 -23
  542. package/test/prompt/traits.test.ts +0 -25
  543. package/test/simulation/semantics.test.ts +0 -23
  544. package/test/stats.test.ts +0 -33
  545. package/test/theme/v2/component.test.ts +0 -87
  546. package/test/theme/v2/resolve.test.ts +0 -424
  547. package/test/theme/v2/select.test.ts +0 -25
  548. package/test/theme/v2/v1-migrate.test.ts +0 -397
  549. package/test/theme.test.ts +0 -150
  550. package/test/ui/file-path.test.ts +0 -61
  551. package/test/ui/layout.test.ts +0 -31
  552. package/test/ui/pane-resize.test.ts +0 -258
  553. package/test/ui/select-controller.test.ts +0 -44
  554. package/test/util/added-patch.test.ts +0 -44
  555. package/test/util/connected-provider.test.ts +0 -59
  556. package/test/util/delayed-presence.test.ts +0 -114
  557. package/test/util/error-details.test.ts +0 -26
  558. package/test/util/error.test.ts +0 -36
  559. package/test/util/filetype.test.ts +0 -23
  560. package/test/util/form.test.ts +0 -110
  561. package/test/util/locale.test.ts +0 -32
  562. package/test/util/marquee.test.ts +0 -45
  563. package/test/util/media.test.ts +0 -54
  564. package/test/util/model.test.ts +0 -70
  565. package/test/util/path-format.test.ts +0 -15
  566. package/test/util/permission.test.ts +0 -23
  567. package/test/util/presentation.test.ts +0 -30
  568. package/test/util/renderer.test.ts +0 -42
  569. package/test/util/selection-keys.test.ts +0 -172
  570. package/test/util/selection.test.ts +0 -115
  571. package/test/util/session.test.ts +0 -70
  572. package/test/util/tool-display.test.ts +0 -65
  573. package/test/util/usage-limits.test.ts +0 -154
@@ -1,2510 +1,2510 @@
1
- /** @jsxImportSource @opentui/solid */
2
- import { afterAll, expect, test } from "bun:test"
3
- import { once } from "node:events"
4
- import { readdir } from "node:fs/promises"
5
- import path from "node:path"
6
- import {
7
- BoxRenderable,
8
- CliRenderEvents,
9
- DiffRenderable,
10
- ImageRenderable,
11
- InputRenderable,
12
- MouseButton,
13
- type Renderable,
14
- ScrollBoxRenderable,
15
- } from "@opentui/core"
16
- import { testRender } from "@opentui/solid"
17
- import type {
18
- Context,
19
- Destination,
20
- KeymapCommand,
21
- KeymapLayer,
22
- Page,
23
- SlotClaim,
24
- Route,
25
- } from "@zyro-sec/plugin/tui/context"
26
- import { ThemeProvider, useThemes } from "../../../src/context/theme"
27
- import { emptyThemeSource, tmpdir } from "../../fixture/fixture"
28
- import { StorageProvider, useStorage } from "../../../src/context/storage"
29
- import { TuiAppProvider } from "../../../src/context/runtime"
30
- import { ConfigProvider, type Info } from "../../../src/config"
31
- import type { TuiKeybind } from "../../../src/config/keybind"
32
- import { Keymap } from "../../../src/context/keymap"
33
- import diffViewerPlugin from "../../../src/feature-plugins/system/diff-viewer"
34
- import { createTuiResolvedConfig } from "../../fixture/tui-runtime"
35
- import { TestTuiContexts } from "../../fixture/tui-environment"
36
- import { createApi, createEventStream, createFetch, json } from "../../fixture/tui-client"
37
- import { DialogProvider, useDialog } from "../../../src/ui/dialog"
38
- import { createDialogApi } from "../../../src/plugin/api"
39
- import { ToastProvider } from "../../../src/ui/toast"
40
- import { createSignal, Show } from "solid-js"
41
- import { diffImageFixture } from "../../fixture/diff-image"
42
-
43
- const temporary = await tmpdir()
44
- afterAll(() => temporary[Symbol.asyncDispose]())
45
-
46
- test("closing the diff viewer returns to the route it opened from", async () => {
47
- const viewer = await renderDiffViewer([])
48
- try {
49
- expect(viewer.current()).toEqual({
50
- type: "plugin",
51
- id: "opencode.diffs",
52
- name: "diff",
53
- data: { sessionID: "session-1", returnRoute: startRoute },
54
- })
55
- const route = viewer.current()
56
- expect(route.type === "plugin" ? route.data?.returnRoute : undefined).not.toBe(startRoute)
57
- expect(viewer.vcsDiffInput()).toEqual({
58
- location: { directory: "/repo/session" },
59
- mode: "branch",
60
- base: "refs/heads/v2",
61
- context: "12",
62
- })
63
-
64
- expect(viewer.commands.has("diff.close")).toBe(true)
65
- viewer.commands.get("diff.close")!.run()
66
- expect(viewer.current()).toEqual(startRoute)
67
- } finally {
68
- viewer.app.renderer.destroy()
69
- }
70
- })
71
-
72
- test.each([80, 160])("standalone full-screen diff does not expose panel-only toggling at %i columns", async (width) => {
73
- const viewer = await renderDiffViewer([], {
74
- width,
75
- initialRoute: {
76
- type: "plugin",
77
- id: "opencode.diffs",
78
- name: "diff",
79
- data: { sessionID: "session-1", returnRoute: startRoute, split: true },
80
- },
81
- })
82
- try {
83
- const command = viewer.commands.get("diff.toggle_fullscreen")
84
- expect(typeof command?.enabled === "function" ? command.enabled() : command?.enabled).toBe(false)
85
- const route = viewer.current()
86
- viewer.app.mockInput.pressKey("f")
87
- await viewer.app.flush()
88
- expect(viewer.current()).toEqual(route)
89
- } finally {
90
- viewer.app.renderer.destroy()
91
- }
92
- })
93
-
94
- test("ctrl+c closes the diff viewer without exiting the application", async () => {
95
- const viewer = await renderDiffViewer([])
96
-
97
- try {
98
- viewer.app.mockInput.pressKey("c", { ctrl: true })
99
- await viewer.app.waitFor(() => viewer.current().type !== "plugin")
100
- expect(viewer.current()).toEqual(startRoute)
101
- } finally {
102
- viewer.app.renderer.destroy()
103
- }
104
- })
105
-
106
- test("shows an error instead of an empty diff when loading fails", async () => {
107
- const viewer = await renderDiffViewer([], { fail: true })
108
- try {
109
- await viewer.app.waitForFrame((frame) => frame.includes("Could not load diff"))
110
- expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
111
- } finally {
112
- viewer.app.renderer.destroy()
113
- }
114
- })
115
-
116
- test("uses the active location when opened outside a session", async () => {
117
- const viewer = await renderDiffViewer([], { initialRoute: { type: "home" } })
118
- try {
119
- expect(viewer.vcsDiffInput()).toEqual({
120
- location: { directory: "/repo/default" },
121
- mode: "branch",
122
- base: "refs/heads/v2",
123
- context: "12",
124
- })
125
- } finally {
126
- viewer.app.renderer.destroy()
127
- }
128
- })
129
-
130
- test("base lookup is lazy until the viewer opens", async () => {
131
- const viewer = await renderDiffViewer([], { open: false })
132
- try {
133
- expect(viewer.baseRequests).toHaveLength(0)
134
- expect(viewer.diffRequests).toHaveLength(0)
135
- viewer.commands.get("diff.open")!.run()
136
- await viewer.app.waitForFrame((frame) => frame.includes("No changes to show"))
137
- expect(viewer.baseRequests).toHaveLength(1)
138
- expect(viewer.baseRequests[0].searchParams.get("location[directory]")).toBe("/repo/session")
139
- expect(viewer.diffRequests).toHaveLength(1)
140
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: "branch", base: "refs/heads/v2" })
141
- } finally {
142
- viewer.app.renderer.destroy()
143
- }
144
- })
145
-
146
- test.each([50, 160])("the source chooser selects all three scopes directly at %i columns", async (width) => {
147
- const files = {
148
- branch: ["committed.txt", "staged.txt", "unstaged.txt", "untracked.txt"],
149
- committed: ["committed.txt"],
150
- working: ["staged.txt", "unstaged.txt", "untracked.txt"],
151
- }
152
- const viewer = await renderDiffViewer([], {
153
- width,
154
- height: 30,
155
- diffResponse: async (url) => {
156
- const mode = url.searchParams.get("mode")
157
- if (mode !== "branch" && mode !== "committed" && mode !== "working") throw new Error("Unexpected diff mode")
158
- return json({
159
- location: session.location,
160
- data: files[mode].map((file) => ({
161
- file,
162
- status: "modified",
163
- additions: 1,
164
- deletions: 0,
165
- })),
166
- })
167
- },
168
- })
169
- try {
170
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: "branch" })
171
- for (const choice of [
172
- { mode: "committed", index: 1, label: "Committed", absent: "untracked.txt" },
173
- { mode: "working", index: 2, label: "Uncommitted", absent: "committed.txt" },
174
- { mode: "branch", index: 0, label: "All", absent: "never-present.txt" },
175
- ] as const) {
176
- await chooseSource(viewer, choice.index)
177
- await viewer.app.waitForFrame((frame) => frame.includes(choice.label) && !frame.includes("Loading diff"))
178
- const frame = viewer.app.captureCharFrame()
179
- expect(frame).toContain(files[choice.mode][0])
180
- expect(frame).not.toContain(choice.absent)
181
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: choice.mode })
182
- expect(viewer.diffRequests.at(-1)!.searchParams.get("base")).toBe(
183
- choice.mode === "working" ? null : "refs/heads/v2",
184
- )
185
- }
186
- expect(viewer.baseRequests).toHaveLength(1)
187
- expect(viewer.diffRequests).toHaveLength(4)
188
- expect(viewer.writes).toHaveLength(0)
189
- expect(viewer.settings().diffs?.source).toBeUndefined()
190
- } finally {
191
- viewer.app.renderer.destroy()
192
- }
193
- })
194
-
195
- test.each(["branch", "committed", "working"] as const)(
196
- "reopening remembers the last source choice instead of the initial %s setting",
197
- async (source) => {
198
- const viewer = await renderDiffViewer(hunkDiff, { source, height: 30 })
199
- try {
200
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: source })
201
- expect(viewer.baseRequests).toHaveLength(source === "working" ? 0 : 1)
202
- await chooseSource(viewer, source === "working" ? 1 : 2)
203
- await viewer.app.waitForFrame(
204
- (frame) => frame.includes(source === "working" ? "Committed" : "Uncommitted") && frame.includes("const first"),
205
- )
206
- expect(viewer.writes).toHaveLength(0)
207
- expect(viewer.settings().diffs?.source).toBe(source)
208
- viewer.commands.get("diff.close")!.run()
209
- await viewer.app.flush()
210
- viewer.commands.get("diff.open")!.run()
211
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
212
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: source === "working" ? "committed" : "working" })
213
- expect(viewer.baseRequests).toHaveLength(source === "working" ? 2 : 1)
214
- expect(viewer.diffRequests).toHaveLength(3)
215
- } finally {
216
- viewer.app.renderer.destroy()
217
- }
218
- },
219
- )
220
-
221
- test("explicit route source overrides the configured default", async () => {
222
- const viewer = await renderDiffViewer(hunkDiff, {
223
- source: "working",
224
- initialRoute: { type: "plugin", id: "opencode.diffs", name: "diff", data: { mode: "committed" } },
225
- })
226
- try {
227
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: "committed", base: "refs/heads/v2" })
228
- expect(viewer.settings().diffs?.source).toBe("working")
229
- } finally {
230
- viewer.app.renderer.destroy()
231
- }
232
- })
233
-
234
- test("the turn source diffs the session's last turn without resolving a base", async () => {
235
- const viewer = await renderDiffViewer(hunkDiff, { height: 30, kittyKeyboard: true })
236
- try {
237
- await chooseSource(viewer, 3)
238
- await viewer.app.waitForFrame((frame) => frame.includes("Last turn") && frame.includes("const first"))
239
- expect(viewer.app.captureCharFrame()).not.toContain("Base not reported")
240
- expect(viewer.turnDiffRequests).toHaveLength(1)
241
- expect(viewer.turnDiffRequests[0].searchParams.get("context")).toBe("12")
242
- expect(viewer.turnDiffRequests[0].searchParams.has("from")).toBe(false)
243
- expect(viewer.diffRequests).toHaveLength(1)
244
- expect(viewer.baseRequests).toHaveLength(1)
245
- await chooseSource(viewer, 2)
246
- await viewer.app.waitForFrame((frame) => frame.includes("Uncommitted · vs HEAD") && frame.includes("const first"))
247
- expect(viewer.vcsDiffInput()).toEqual({ location: session.location, mode: "working", context: "12" })
248
- } finally {
249
- viewer.app.renderer.destroy()
250
- }
251
- })
252
-
253
- test("the turn source refreshes only when the session finishes a turn", async () => {
254
- const viewer = await renderDiffViewer(hunkDiff, { source: "turn" })
255
- try {
256
- expect(viewer.turnDiffRequests).toHaveLength(1)
257
- viewer.setSessionStatus("running")
258
- await viewer.app.flush()
259
- expect(viewer.turnDiffRequests).toHaveLength(1)
260
- viewer.setSessionStatus("idle")
261
- await viewer.app.waitFor(() => viewer.turnDiffRequests.length === 2)
262
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
263
- expect(viewer.diffRequests).toHaveLength(0)
264
- } finally {
265
- viewer.app.renderer.destroy()
266
- }
267
- })
268
-
269
- test("an empty or failing turn diff uses the viewer's empty and error states", async () => {
270
- const empty = await renderDiffViewer([], { source: "turn" })
271
- try {
272
- await empty.app.waitForFrame((frame) => frame.includes("No changes to show"))
273
- expect(empty.baseRequests).toHaveLength(0)
274
- expect(empty.diffRequests).toHaveLength(0)
275
- expect(empty.turnDiffRequests).toHaveLength(1)
276
- } finally {
277
- empty.app.renderer.destroy()
278
- }
279
- const failing = await renderDiffViewer([], { source: "turn", fail: true })
280
- try {
281
- await failing.app.waitForFrame((frame) => frame.includes("Could not load diff. Reopen the diff viewer"))
282
- expect(failing.app.captureCharFrame()).toContain("Last turn · Diff unavailable")
283
- expect(failing.app.captureCharFrame()).not.toContain("Choose a base branch")
284
- } finally {
285
- failing.app.renderer.destroy()
286
- }
287
- })
288
-
289
- test("the turn source is unavailable outside a session and falls back to the branch scope", async () => {
290
- const viewer = await renderDiffViewer(hunkDiff, { source: "turn", height: 30, initialRoute: { type: "home" } })
291
- try {
292
- expect(viewer.vcsDiffInput()).toEqual({
293
- location: { directory: "/repo/default" },
294
- mode: "branch",
295
- base: "refs/heads/v2",
296
- context: "12",
297
- })
298
- expect(viewer.turnDiffRequests).toHaveLength(0)
299
- viewer.app.mockInput.pressKey("d")
300
- await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
301
- expect(viewer.app.captureCharFrame()).not.toContain("Last turn")
302
- expect(viewer.app.captureCharFrame()).toMatch(/Base\s+v2/)
303
- } finally {
304
- viewer.app.renderer.destroy()
305
- }
306
- })
307
-
308
- test.each([50, 80, 160])(
309
- "keeps scope, base, and review count on one row with a selectable base at %i columns",
310
- async (width) => {
311
- const viewer = await renderDiffViewer(hunkDiff, {
312
- width,
313
- height: 30,
314
- kittyKeyboard: true,
315
- base: {
316
- name: "release",
317
- ref: "refs/remotes/origin/release",
318
- source: "reflog",
319
- },
320
- })
321
- try {
322
- const header = viewer.app.renderer.root.findDescendantById("diff-source-header")!
323
- const source = viewer.app.renderer.root.findDescendantById("diff-source-switch")!
324
- const count = viewer.app.renderer.root.findDescendantById("diff-review-count")!
325
- // Full screen keeps the header on its top row, compact or not.
326
- expect(header.height).toBe(1)
327
- expect(source.y).toBe(header.y)
328
- expect(count.y).toBe(header.y)
329
- expect(viewer.app.captureCharFrame().split("\n")[header.y]).toMatch(/All · vs release\s+0\/1/)
330
- expect(viewer.app.captureCharFrame()).not.toContain("reviewed")
331
- expect(viewer.vcsDiffInput()).toMatchObject({ base: "refs/remotes/origin/release" })
332
- await viewer.app.mockMouse.click(source.x, source.y)
333
- await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
334
- expect(viewer.app.captureCharFrame()).toMatch(/Base\s+release/)
335
- const rows = viewer.app.captureCharFrame().split("\n")
336
- const first = rows.findIndex((row) => row.includes("Branch + local changes"))
337
- expect(rows[first]).toMatch(/All\s+Branch \+ local changes/)
338
- expect(rows[first + 1]).toMatch(/Committed\s+Branch commits only/)
339
- expect(rows[first + 2]).toMatch(/Uncommitted\s+Local changes only/)
340
- expect(rows[first + 3]).toMatch(/Last turn\s+Latest session turn/)
341
- expect(rows[first + 4]).toMatch(/Base\s+release/)
342
- expect(rows[first + 1].indexOf("Branch commits only")).toBe(rows[first].indexOf("Branch + local changes"))
343
- expect(rows[first + 2].indexOf("Local changes only")).toBe(rows[first].indexOf("Branch + local changes"))
344
- expect(rows[first + 3].indexOf("Latest session turn")).toBe(rows[first].indexOf("Branch + local changes"))
345
- viewer.app.mockInput.pressEscape()
346
- await viewer.app.waitForFrame((frame) => !frame.includes("Diff source"))
347
- viewer.commands.get("diff.mark_reviewed")!.run()
348
- await viewer.app.flush()
349
- expect(viewer.app.captureCharFrame().split("\n")[header.y]).toMatch(/All · vs release\s+1\/1/)
350
- await chooseSource(viewer, 2)
351
- await viewer.app.waitForFrame((frame) => frame.includes("Uncommitted · vs HEAD"))
352
- viewer.app.mockInput.pressKey("d")
353
- await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
354
- expect(viewer.app.captureCharFrame()).toMatch(/Base\s+release/)
355
- expect(viewer.app.captureCharFrame()).not.toMatch(/Base\s+HEAD/)
356
- expect(viewer.baseRequests).toHaveLength(1)
357
- } finally {
358
- viewer.app.renderer.destroy()
359
- }
360
- },
361
- )
362
-
363
- test.each([50, 80, 100, 160])(
364
- "long comparison bases cannot hide the source or review count at %i columns",
365
- async (width) => {
366
- const viewer = await renderDiffViewer(hunkDiff, {
367
- width,
368
- source: "committed",
369
- base: { ...baseFixture, name: "release/very-long-comparison-branch-name-that-does-not-fit-in-the-header" },
370
- })
371
- try {
372
- const header = viewer.app.renderer.root.findDescendantById("diff-source-header")!
373
- const count = viewer.app.renderer.root.findDescendantById("diff-review-count")!
374
- const y = header.y + header.height - 1
375
- const row = viewer.app.captureCharFrame().split("\n")[y]
376
- expect(header.height).toBe(1)
377
- expect(row).toContain("Committed · vs")
378
- expect(row).toContain("0/1")
379
- expect(row).not.toContain("release/very-long-comparison-branch-name-that-does-not-fit-in-the-header")
380
- expect(count.y).toBe(y)
381
- expect(count.x + count.width).toBeLessThanOrEqual(header.x + header.width)
382
- const hint = viewer.app.renderer.root.findDescendantById("diff-help-shortcut")!
383
- const scroll = findScrollBox(viewer.app.renderer.root)!
384
- if (width < 88) {
385
- // The compact layout moves help from the header row into its footer.
386
- expect(hint.y).toBe(viewer.app.renderer.root.findDescendantById("diff-footer")!.y)
387
- expect(hint.y).toBeGreaterThanOrEqual(scroll.viewport.y + scroll.viewport.height)
388
- expect(scroll.viewport.y).toBeGreaterThan(y)
389
- }
390
- } finally {
391
- viewer.app.renderer.destroy()
392
- }
393
- },
394
- )
395
-
396
- test("opening the source chooser from initial Uncommitted does not resolve a branch base", async () => {
397
- const viewer = await renderDiffViewer(hunkDiff, { source: "working", height: 30 })
398
- try {
399
- viewer.app.mockInput.pressKey("d")
400
- await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
401
- expect(viewer.app.captureCharFrame()).toContain("Uncommitted · vs HEAD")
402
- expect(viewer.app.captureCharFrame()).toMatch(/Base\s+Choose…/)
403
- expect(viewer.baseRequests).toHaveLength(0)
404
- expect(viewer.diffRequests).toHaveLength(1)
405
- } finally {
406
- viewer.app.renderer.destroy()
407
- }
408
- })
409
-
410
- test.each(["branch", "committed", "working"] as const)(
411
- "choosing a base remembers it and refreshes only the affected %s comparison",
412
- async (source) => {
413
- const viewer = await renderDiffViewer(hunkDiff, { source, height: 30, kittyKeyboard: true })
414
- try {
415
- viewer.commands.get("diff.mark_reviewed")!.run()
416
- await viewer.app.flush()
417
- expect(viewer.app.captureCharFrame()).toContain("1/1")
418
- await chooseSource(viewer, 4)
419
- await viewer.app.waitForFrame((frame) => frame.includes("Base branch") && frame.includes("origin/release"))
420
- expect(viewer.app.captureCharFrame()).toMatch(/●\s+v2/)
421
- expect(viewer.branchesRequests[0].searchParams.get("location[directory]")).toBe("/repo/session")
422
- expect(viewer.branchesRequests[0].searchParams.get("limit")).toBe("100")
423
- // The picker can paint before its deferred input focus.
424
- if (!viewer.app.renderer.currentFocusedEditor) await once(viewer.app.renderer, CliRenderEvents.FOCUSED_EDITOR)
425
- expect(viewer.app.renderer.currentFocusedEditor).toBeInstanceOf(InputRenderable)
426
- await viewer.app.mockInput.typeText("origin/release")
427
- await Bun.sleep(160)
428
- await viewer.app.waitFor(() => viewer.branchesRequests.at(-1)?.searchParams.get("search") === "origin/release")
429
- await viewer.app.waitForFrame((frame) => frame.includes("origin/release") && !frame.includes("Loading branches"))
430
- viewer.app.mockInput.pressEnter()
431
- await viewer.app.waitForFrame((frame) => !frame.includes("Base branch") && frame.includes("src/file.txt"))
432
- expect(viewer.mutationRequests).toHaveLength(0)
433
- expect(viewer.vcsDiffInput()).toEqual({
434
- location: session.location,
435
- mode: source,
436
- context: "12",
437
- ...(source === "working" ? {} : { base: "origin/release" }),
438
- })
439
- expect(viewer.diffRequests).toHaveLength(source === "working" ? 1 : 2)
440
- expect(viewer.app.captureCharFrame()).toContain(source === "working" ? "1/1" : "0/1")
441
- expect(viewer.writes).toHaveLength(0)
442
- if (source !== "working") viewer.commands.get("diff.mark_reviewed")!.run()
443
- await viewer.app.flush()
444
- expect(viewer.app.captureCharFrame()).toContain("1/1")
445
- viewer.commands.get("diff.close")!.run()
446
- await viewer.app.flush()
447
- viewer.commands.get("diff.open")!.run()
448
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
449
- expect(viewer.app.captureCharFrame()).toContain("0/1")
450
- expect(viewer.diffRequests).toHaveLength(source === "working" ? 2 : 3)
451
- if (source !== "working") expect(viewer.vcsDiffInput()).toMatchObject({ base: "origin/release" })
452
- await chooseSource(viewer, 4)
453
- await viewer.app.waitForFrame((frame) => /●\s+origin\/release/.test(frame))
454
- expect(viewer.baseRequests).toHaveLength(1)
455
- } finally {
456
- viewer.app.renderer.destroy()
457
- }
458
- },
459
- )
460
-
461
- test.each(["branch", "committed"] as const)("an ambiguous base never requests a guessed %s diff", async (source) => {
462
- const viewer = await renderDiffViewer([], {
463
- source,
464
- height: 30,
465
- kittyKeyboard: true,
466
- baseResponse: async () => json({ message: "choose a base" }, { status: 503 }),
467
- diffResponse: async (url) =>
468
- url.searchParams.get("base")
469
- ? json({ location: session.location, data: hunkDiff })
470
- : json({ message: "choose a base" }, { status: 503 }),
471
- })
472
- try {
473
- expect(viewer.app.captureCharFrame()).toContain("Choose a base branch")
474
- expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
475
- expect(viewer.diffRequests).toHaveLength(0)
476
- await chooseSource(viewer, 4)
477
- await viewer.app.waitForFrame((frame) => frame.includes("Base branch") && frame.includes("origin/release"))
478
- viewer.app.mockInput.pressKey("HOME")
479
- viewer.app.mockInput.pressArrow("down")
480
- viewer.app.mockInput.pressEnter()
481
- await viewer.app.waitForFrame((frame) => frame.includes("vs release") && frame.includes("const first"))
482
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: source, base: "release" })
483
- expect(viewer.settings().diffs?.source).toBe(source)
484
- expect(viewer.baseRequests).toHaveLength(1)
485
- } finally {
486
- viewer.app.renderer.destroy()
487
- }
488
- })
489
-
490
- test("base and scope choices survive reopening but not a new TUI instance", async () => {
491
- const viewer = await renderDiffViewer(hunkDiff, { source: "working", height: 30, kittyKeyboard: true })
492
- try {
493
- await chooseSource(viewer, 4)
494
- await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
495
- viewer.app.mockInput.pressArrow("down")
496
- viewer.app.mockInput.pressEnter()
497
- await viewer.app.waitForFrame((frame) => !frame.includes("Base branch"))
498
- await chooseSource(viewer, 1)
499
- await viewer.app.waitForFrame((frame) => frame.includes("Committed · vs release") && frame.includes("const first"))
500
- viewer.commands.get("diff.close")!.run()
501
- await viewer.app.flush()
502
- viewer.commands.get("diff.open")!.run()
503
- await viewer.app.waitForFrame((frame) => frame.includes("Committed · vs release") && frame.includes("const first"))
504
- expect(viewer.writes).toHaveLength(0)
505
- expect(viewer.mutationRequests).toHaveLength(0)
506
- expect(await readdir(path.join(viewer.state, "test", "tui"))).toEqual([])
507
- } finally {
508
- viewer.app.renderer.destroy()
509
- }
510
-
511
- const restarted = await renderDiffViewer(hunkDiff, { state: viewer.state, source: "working", height: 30 })
512
- try {
513
- expect(restarted.vcsDiffInput()).toMatchObject({ mode: "working" })
514
- await chooseSource(restarted, 1)
515
- await restarted.app.waitForFrame((frame) => frame.includes("Committed · vs v2") && frame.includes("const first"))
516
- expect(restarted.vcsDiffInput()).toMatchObject({ base: "refs/heads/v2" })
517
- expect(restarted.writes).toHaveLength(0)
518
- } finally {
519
- restarted.app.renderer.destroy()
520
- }
521
- })
522
-
523
- test("base choices are isolated by branch within the same location", async () => {
524
- const viewer = await renderDiffViewer(hunkDiff, { height: 30, kittyKeyboard: true })
525
- try {
526
- await chooseSource(viewer, 4)
527
- await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
528
- viewer.app.mockInput.pressArrow("down")
529
- viewer.app.mockInput.pressEnter()
530
- await viewer.app.waitForFrame((frame) => frame.includes("All · vs release") && frame.includes("const first"))
531
- viewer.setBranch("other-feature")
532
- await viewer.app.waitForFrame((frame) => frame.includes("All · vs v2") && frame.includes("const first"))
533
- expect(viewer.vcsDiffInput()).toMatchObject({ base: "refs/heads/v2" })
534
- viewer.setBranch("feature")
535
- await viewer.app.waitForFrame((frame) => frame.includes("All · vs release") && frame.includes("const first"))
536
- expect(viewer.vcsDiffInput()).toMatchObject({ base: "release" })
537
- expect(viewer.baseRequests).toHaveLength(2)
538
- expect(viewer.mutationRequests).toHaveLength(0)
539
- } finally {
540
- viewer.app.renderer.destroy()
541
- }
542
- })
543
-
544
- test("an invalid comparison reports an error and allows another base choice", async () => {
545
- const viewer = await renderDiffViewer(hunkDiff, {
546
- height: 30,
547
- kittyKeyboard: true,
548
- diffResponse: async (url) =>
549
- url.searchParams.get("base") === "release"
550
- ? json({ message: "no common history" }, { status: 503 })
551
- : json({ location: session.location, data: hunkDiff }),
552
- })
553
- try {
554
- await chooseSource(viewer, 4)
555
- await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
556
- viewer.app.mockInput.pressArrow("down")
557
- viewer.app.mockInput.pressEnter()
558
- await viewer.app.waitForFrame((frame) => frame.includes("Base or diff unavailable"))
559
- expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
560
- await chooseSource(viewer, 4)
561
- await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
562
- viewer.app.mockInput.pressKey("HOME")
563
- viewer.app.mockInput.pressEnter()
564
- await viewer.app.waitForFrame((frame) => frame.includes("All · vs v2") && frame.includes("const first"))
565
- expect(viewer.diffRequests).toHaveLength(3)
566
- expect(viewer.vcsDiffInput()).toMatchObject({ base: "v2" })
567
- expect(viewer.mutationRequests).toHaveLength(0)
568
- } finally {
569
- viewer.app.renderer.destroy()
570
- }
571
- })
572
-
573
- test("base search failures are visible without changing the diff", async () => {
574
- const viewer = await renderDiffViewer(hunkDiff, {
575
- height: 30,
576
- branchesResponse: async () => json({ message: "branches unavailable" }, { status: 503 }),
577
- })
578
- try {
579
- await chooseSource(viewer, 4)
580
- await viewer.app.waitForFrame((frame) => frame.includes("Could not load branches"))
581
- expect(viewer.app.captureCharFrame()).toContain("All · vs v2")
582
- expect(viewer.mutationRequests).toHaveLength(0)
583
- expect(viewer.diffRequests).toHaveLength(1)
584
- } finally {
585
- viewer.app.renderer.destroy()
586
- }
587
- })
588
-
589
- test("a late base lookup cannot overwrite an in-memory base choice", async () => {
590
- const pending = Promise.withResolvers<Response>()
591
- const viewer = await renderDiffViewer(hunkDiff, {
592
- height: 30,
593
- pending: true,
594
- kittyKeyboard: true,
595
- baseResponse: () => pending.promise,
596
- })
597
- try {
598
- await chooseSource(viewer, 4)
599
- await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
600
- viewer.app.mockInput.pressArrow("down")
601
- viewer.app.mockInput.pressEnter()
602
- await viewer.app.waitForFrame((frame) => frame.includes("All · vs release") && frame.includes("const first"))
603
- pending.resolve(json({ location: session.location, data: baseFixture }))
604
- await viewer.app.flush()
605
- expect(viewer.app.captureCharFrame()).toContain("All · vs release")
606
- expect(viewer.diffRequests).toHaveLength(1)
607
- expect(viewer.vcsDiffInput()).toMatchObject({ base: "release" })
608
- expect(viewer.baseRequests).toHaveLength(1)
609
- await chooseSource(viewer, 1)
610
- await viewer.app.waitForFrame((frame) => frame.includes("Committed · vs release"))
611
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: "committed", base: "release" })
612
- viewer.app.mockInput.pressKey("d")
613
- await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
614
- expect(viewer.app.captureCharFrame()).toMatch(/Base\s+release/)
615
- } finally {
616
- pending.resolve(json({ location: session.location, data: baseFixture }))
617
- viewer.app.renderer.destroy()
618
- }
619
- })
620
-
621
- test("dismissing the base picker leaves the comparison unchanged", async () => {
622
- const viewer = await renderDiffViewer(hunkDiff, {
623
- height: 30,
624
- kittyKeyboard: true,
625
- })
626
- try {
627
- await chooseSource(viewer, 4)
628
- await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
629
- viewer.app.mockInput.pressArrow("down")
630
- viewer.app.mockInput.pressEscape()
631
- await viewer.app.waitForFrame((frame) => !frame.includes("Base branch"))
632
- viewer.app.mockInput.pressKey("d")
633
- await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
634
- expect(viewer.app.captureCharFrame()).toContain("Diff source")
635
- expect(viewer.app.captureCharFrame()).toContain("All · vs v2")
636
- expect(viewer.diffRequests).toHaveLength(1)
637
- } finally {
638
- viewer.app.renderer.destroy()
639
- }
640
- })
641
-
642
- test("the base picker remembers its captured location without refreshing a moved session", async () => {
643
- const viewer = await renderDiffViewer(hunkDiff, { height: 30, kittyKeyboard: true })
644
- try {
645
- await chooseSource(viewer, 4)
646
- await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
647
- viewer.setSessionLocation({ directory: "/repo/moved" })
648
- await viewer.app.flush()
649
- viewer.app.mockInput.pressArrow("down")
650
- viewer.app.mockInput.pressEnter()
651
- await viewer.app.waitForFrame((frame) => !frame.includes("Base branch"))
652
- expect(viewer.mutationRequests).toHaveLength(0)
653
- expect(viewer.vcsDiffInput()).toMatchObject({ location: { directory: "/repo/moved" }, base: "refs/heads/v2" })
654
- expect(viewer.diffRequests).toHaveLength(2)
655
- viewer.setSessionLocation(session.location)
656
- await viewer.app.waitForFrame((frame) => frame.includes("All · vs release") && frame.includes("const first"))
657
- expect(viewer.vcsDiffInput()).toMatchObject({ location: session.location, base: "release" })
658
- } finally {
659
- viewer.app.renderer.destroy()
660
- }
661
- })
662
-
663
- test("tree, single-file, and layout preferences do not refetch base or diff", async () => {
664
- const viewer = await renderDiffViewer(hunkDiff, { width: 160 })
665
- try {
666
- for (const command of ["diff.toggle_file_tree", "diff.single_patch", "diff.toggle_view"]) {
667
- viewer.commands.get(command)!.run()
668
- await viewer.app.flush()
669
- }
670
- expect(viewer.writes).toHaveLength(3)
671
- expect(viewer.settings().diffs).toMatchObject({ tree: false, single: true, view: "unified" })
672
- expect(viewer.baseRequests).toHaveLength(1)
673
- expect(viewer.diffRequests).toHaveLength(1)
674
- } finally {
675
- viewer.app.renderer.destroy()
676
- }
677
- })
678
-
679
- test("the remapped source chooser preserves its current selection marker", async () => {
680
- const viewer = await renderDiffViewer(hunkDiff, { height: 30, keybinds: { "diff.switch_source": "x" } })
681
- try {
682
- viewer.app.mockInput.pressKey("d")
683
- await viewer.app.flush()
684
- expect(viewer.app.captureCharFrame()).not.toContain("Diff source")
685
- await chooseSource(viewer, 1, "x")
686
- await viewer.app.waitForFrame((frame) => frame.includes("Committed") && frame.includes("const first"))
687
- viewer.app.mockInput.pressKey("x")
688
- await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
689
- expect(viewer.app.captureCharFrame()).toMatch(/●\s+Committed/)
690
- expect(viewer.app.captureCharFrame()).toContain("Branch commits only")
691
- expect(viewer.app.captureCharFrame()).toContain("Local changes only")
692
- expect(viewer.app.captureCharFrame()).toMatch(/Base\s+v2/)
693
- expect(viewer.writes).toHaveLength(0)
694
- } finally {
695
- viewer.app.renderer.destroy()
696
- }
697
- })
698
-
699
- test("pending and failed scopes never render the previous scope's files", async () => {
700
- const pending = Promise.withResolvers<Response>()
701
- const viewer = await renderDiffViewer([], {
702
- height: 30,
703
- diffResponse: async (url) =>
704
- url.searchParams.get("mode") === "committed"
705
- ? pending.promise
706
- : json({ location: session.location, data: hunkDiff }),
707
- })
708
- try {
709
- expect(viewer.app.captureCharFrame()).toContain("const first")
710
- await chooseSource(viewer, 1)
711
- await viewer.app.waitForFrame((frame) => frame.includes("Loading diff"))
712
- expect(viewer.app.captureCharFrame()).toContain("Committed")
713
- expect(viewer.app.captureCharFrame()).not.toContain("src/file.txt")
714
- expect(findDiffs(viewer.app.renderer.root)).toHaveLength(0)
715
- pending.resolve(json({ message: "comparison failed" }, { status: 503 }))
716
- await viewer.app.waitForFrame((frame) => frame.includes("Could not load diff"))
717
- expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
718
- expect(viewer.app.captureCharFrame()).not.toContain("src/file.txt")
719
- await chooseSource(viewer, 2)
720
- await viewer.app.waitForFrame((frame) => frame.includes("Uncommitted") && frame.includes("const first"))
721
- expect(viewer.baseRequests).toHaveLength(1)
722
- } finally {
723
- pending.resolve(json({ location: session.location, data: [] }))
724
- viewer.app.renderer.destroy()
725
- }
726
- })
727
-
728
- test("late diff responses cannot replace the selected scope", async () => {
729
- const pending = Promise.withResolvers<Response>()
730
- const viewer = await renderDiffViewer([], {
731
- height: 30,
732
- diffResponse: async (url) =>
733
- url.searchParams.get("mode") === "committed"
734
- ? pending.promise
735
- : json({ location: session.location, data: [{ ...hunkDiff[0], file: "uncommitted.txt" }] }),
736
- })
737
- try {
738
- await chooseSource(viewer, 1)
739
- await viewer.app.waitForFrame((frame) => frame.includes("Loading diff"))
740
- await chooseSource(viewer, 2)
741
- await viewer.app.waitForFrame((frame) => frame.includes("uncommitted.txt"))
742
- pending.resolve(json({ location: session.location, data: [{ ...hunkDiff[0], file: "committed.txt" }] }))
743
- await viewer.app.flush()
744
- expect(viewer.app.captureCharFrame()).toContain("Uncommitted")
745
- expect(viewer.app.captureCharFrame()).toContain("uncommitted.txt")
746
- expect(viewer.app.captureCharFrame()).not.toMatch(/\scommitted\.txt/)
747
- expect(viewer.baseRequests).toHaveLength(1)
748
- } finally {
749
- pending.resolve(json({ location: session.location, data: [] }))
750
- viewer.app.renderer.destroy()
751
- }
752
- })
753
-
754
- test("switching to uncommitted does not wait for or display a late base result", async () => {
755
- const pending = Promise.withResolvers<Response>()
756
- const viewer = await renderDiffViewer(hunkDiff, { height: 30, pending: true, baseResponse: () => pending.promise })
757
- try {
758
- expect(viewer.app.captureCharFrame()).toContain("Loading diff")
759
- expect(viewer.diffRequests).toHaveLength(0)
760
- await chooseSource(viewer, 2)
761
- await viewer.app.waitForFrame((frame) => frame.includes("Uncommitted") && frame.includes("const first"))
762
- pending.resolve(json({ location: session.location, data: baseFixture }))
763
- await viewer.app.flush()
764
- expect(viewer.app.captureCharFrame()).toContain("vs HEAD")
765
- expect(viewer.app.captureCharFrame()).not.toContain("vs v2")
766
- expect(viewer.diffRequests).toHaveLength(1)
767
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: "working" })
768
- } finally {
769
- pending.resolve(json({ location: session.location, data: baseFixture }))
770
- viewer.app.renderer.destroy()
771
- }
772
- })
773
-
774
- test.each([false, true])(
775
- "missing base metadata preserves provider branch review (has branch changes: %s)",
776
- async (changed) => {
777
- const viewer = await renderDiffViewer([], {
778
- base: null,
779
- height: 30,
780
- diffResponse: async (url) =>
781
- json({
782
- location: session.location,
783
- data:
784
- url.searchParams.get("mode") === "working"
785
- ? [{ ...hunkDiff[0], file: "dirty.txt" }]
786
- : changed
787
- ? [{ ...hunkDiff[0], file: "branch.txt" }]
788
- : [],
789
- }),
790
- })
791
- try {
792
- expect(viewer.app.captureCharFrame()).toContain("All")
793
- expect(viewer.app.captureCharFrame()).toContain("Base not reported")
794
- expect(viewer.app.captureCharFrame()).toContain(changed ? "branch.txt" : "No changes to show")
795
- expect(viewer.app.captureCharFrame()).not.toContain("dirty.txt")
796
- expect(viewer.app.captureCharFrame()).not.toContain("showing uncommitted")
797
- expect(viewer.vcsDiffInput()).toEqual({ location: session.location, mode: "branch", context: "12" })
798
- await chooseSource(viewer, 1)
799
- await viewer.app.waitForFrame((frame) => frame.includes("Committed comparison unavailable"))
800
- expect(viewer.app.captureCharFrame()).toContain("Base not reported")
801
- expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
802
- expect(viewer.app.captureCharFrame()).not.toContain("branch.txt")
803
- expect(viewer.diffRequests).toHaveLength(1)
804
- await chooseSource(viewer, 2)
805
- await viewer.app.waitForFrame((frame) => frame.includes("dirty.txt"))
806
- expect(viewer.app.captureCharFrame()).not.toContain("Base not reported")
807
- expect(viewer.vcsDiffInput()).toEqual({ location: session.location, mode: "working", context: "12" })
808
- expect(viewer.diffRequests).toHaveLength(2)
809
- expect(viewer.baseRequests).toHaveLength(1)
810
- } finally {
811
- viewer.app.renderer.destroy()
812
- }
813
- },
814
- )
815
-
816
- test("unresolved branch comparisons report errors without switching to working", async () => {
817
- const viewer = await renderDiffViewer([], {
818
- base: null,
819
- height: 30,
820
- diffResponse: async (url) =>
821
- url.searchParams.get("mode") === "branch"
822
- ? json({ message: "comparison base unavailable" }, { status: 503 })
823
- : json({ location: session.location, data: hunkDiff }),
824
- })
825
- try {
826
- expect(viewer.app.captureCharFrame()).toContain("Could not load diff")
827
- expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
828
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: "branch" })
829
- expect(viewer.diffRequests).toHaveLength(1)
830
- await chooseSource(viewer, 2)
831
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
832
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: "working" })
833
- expect(viewer.baseRequests).toHaveLength(1)
834
- } finally {
835
- viewer.app.renderer.destroy()
836
- }
837
- })
838
-
839
- test("base lookup failures are errors rather than missing-base fallbacks", async () => {
840
- const viewer = await renderDiffViewer([], {
841
- height: 30,
842
- baseResponse: async () => json({ message: "base failed" }, { status: 503 }),
843
- })
844
- try {
845
- expect(viewer.app.captureCharFrame()).toContain("Could not load diff")
846
- expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
847
- expect(viewer.app.captureCharFrame()).not.toContain("showing uncommitted")
848
- expect(viewer.diffRequests).toHaveLength(0)
849
- await chooseSource(viewer, 2)
850
- await viewer.app.waitForFrame((frame) => frame.includes("No changes to show"))
851
- expect(viewer.vcsDiffInput()).toMatchObject({ mode: "working" })
852
- } finally {
853
- viewer.app.renderer.destroy()
854
- }
855
- })
856
-
857
- test.each([undefined, "Binary files a/assets/preview.png and b/assets/preview.png differ"])(
858
- "committed images never read the working-tree image (patch: %s)",
859
- async (patch) => {
860
- const viewer = await renderDiffViewer(
861
- [{ file: "assets/preview.png", status: "modified", additions: 0, deletions: 0, patch }],
862
- { source: "committed" },
863
- )
864
- try {
865
- expect(viewer.app.captureCharFrame()).toContain("Committed image preview unavailable")
866
- expect(viewer.imageReadInput()).toBeUndefined()
867
- expect(viewer.app.captureCharFrame()).not.toContain("Working tree preview")
868
- } finally {
869
- viewer.app.renderer.destroy()
870
- }
871
- },
872
- )
873
-
874
- async function chooseSource(viewer: Awaited<ReturnType<typeof renderDiffViewer>>, index: number, key = "d") {
875
- viewer.app.mockInput.pressKey(key)
876
- await viewer.app.waitForFrame((frame) => /Diff source\s+esc/.test(frame))
877
- viewer.app.mockInput.pressKey("HOME")
878
- await viewer.app.flush()
879
- for (let i = 0; i < index; i++) {
880
- viewer.app.mockInput.pressArrow("down")
881
- await viewer.app.flush()
882
- }
883
- viewer.app.mockInput.pressEnter()
884
- await viewer.app.waitForFrame((frame) => !/Diff source\s+esc/.test(frame))
885
- }
886
-
887
- test("brackets navigate diff hunks", async () => {
888
- const viewer = await renderDiffViewer(hunkDiff, { height: 12 })
889
- try {
890
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
891
- await viewer.app.waitFor(() => Boolean(findScrollBox(viewer.app.renderer.root)))
892
- await viewer.app.flush()
893
- expect(findDiffs(viewer.app.renderer.root)).toHaveLength(3)
894
- const scroll = findScrollBox(viewer.app.renderer.root)!
895
- const initial = scroll.scrollTop
896
-
897
- viewer.app.mockInput.pressKey("]")
898
- await viewer.app.renderOnce()
899
- const first = scroll.scrollTop
900
- expect(first).toBeGreaterThan(initial)
901
- expect(findDiffs(viewer.app.renderer.root)[1].y).toBeGreaterThanOrEqual(scroll.viewport.y + 2)
902
-
903
- viewer.app.mockInput.pressKey("]")
904
- await viewer.app.renderOnce()
905
- const second = scroll.scrollTop
906
- expect(second).toBeGreaterThan(first)
907
- expect(findDiffs(viewer.app.renderer.root)[2].y).toBeGreaterThanOrEqual(scroll.viewport.y + 2)
908
-
909
- viewer.app.mockInput.pressKey("[")
910
- await viewer.app.renderOnce()
911
- expect(scroll.scrollTop).toBe(first)
912
-
913
- viewer.app.mockInput.pressKey("]")
914
- await viewer.app.renderOnce()
915
- expect(scroll.scrollTop).toBe(second)
916
-
917
- scroll.scrollTo(initial)
918
- viewer.app.mockInput.pressKey("]")
919
- await viewer.app.renderOnce()
920
- expect(scroll.scrollTop).toBe(first)
921
- } finally {
922
- viewer.app.renderer.destroy()
923
- }
924
- })
925
-
926
- test("disabled diff keybinds have no component fallbacks", async () => {
927
- const viewer = await renderDiffViewer(hunkDiff, {
928
- height: 12,
929
- keybinds: disabledDiffKeybinds,
930
- })
931
- try {
932
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
933
- await viewer.app.waitFor(() => Boolean(findScrollBox(viewer.app.renderer.root)))
934
- await viewer.app.flush()
935
- const scroll = findScrollBox(viewer.app.renderer.root)!
936
- const initial = scroll.scrollTop
937
-
938
- Object.keys(disabledDiffKeybinds).forEach((command) => expect(viewer.shortcut(command)).toBe(""))
939
-
940
- viewer.app.mockInput.pressKey("j")
941
- await viewer.app.renderOnce()
942
-
943
- expect(scroll.scrollTop).toBe(initial)
944
- } finally {
945
- viewer.app.renderer.destroy()
946
- }
947
- })
948
-
949
- test.each(["added", "deleted"] as const)("%s files use full width even in split view", async (status) => {
950
- const viewer = await renderDiffViewer(
951
- [
952
- {
953
- file: "src/new.txt",
954
- status,
955
- additions: status === "added" ? 1 : 0,
956
- deletions: status === "deleted" ? 1 : 0,
957
- patch:
958
- status === "added"
959
- ? "--- /dev/null\n+++ b/src/new.txt\n@@ -0,0 +1 @@\n+full width content\n"
960
- : "--- a/src/new.txt\n+++ /dev/null\n@@ -1 +0,0 @@\n-full width content\n",
961
- },
962
- ],
963
- { width: 160 },
964
- )
965
- try {
966
- await viewer.app.waitForFrame((frame) => frame.includes("full width content"))
967
- const diffs = findDiffs(viewer.app.renderer.root)
968
- expect(diffs).toHaveLength(1)
969
- expect(diffs[0].view).toBe("unified")
970
- expect(viewer.app.captureCharFrame()).not.toMatch(/[├└┌┐┬┴─]/)
971
- } finally {
972
- viewer.app.renderer.destroy()
973
- }
974
- })
975
-
976
- test.each([80, 160])("adapts navigation and modified patches at %i columns", async (width) => {
977
- const viewer = await renderDiffViewer(hunkDiff, { width })
978
- try {
979
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
980
- expect(findDiffs(viewer.app.renderer.root)[0].view).toBe(width === 80 ? "unified" : "split")
981
- expect(viewer.app.captureCharFrame()).toContain("0/1")
982
- } finally {
983
- viewer.app.renderer.destroy()
984
- }
985
- })
986
-
987
- test("the first file title sits below full-height top padding with a blank row below", async () => {
988
- const viewer = await renderDiffViewer(hunkDiff)
989
- try {
990
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
991
- const lines = viewer.app.captureCharFrame().split("\n")
992
- const title = lines.findIndex((line) => line.includes("src/file.txt"))
993
- expect(title).toBeGreaterThan(0)
994
- const padding = viewer.app.captureSpans().lines[title - 1].spans.find((span) => span.width > 2)!
995
- expect(lines[title - 1].trim()).toBe("")
996
- expect(padding.bg).toEqual(
997
- viewer.app.captureSpans().lines[title].spans.find((span) => span.text.includes("src/"))!.bg,
998
- )
999
- expect(lines[title + 1].trim()).toBe("")
1000
- expect(lines[title + 2]).toContain("const first")
1001
- expect(lines[title]).toMatch(/\+3\s+-3/)
1002
- } finally {
1003
- viewer.app.renderer.destroy()
1004
- }
1005
- })
1006
-
1007
- test.each([100, 160])("shared pane edges align headings and stay fixed at %i columns", async (width) => {
1008
- const viewer = await renderDiffViewer(manyDiffs.slice(0, 6), { width, height: 40 })
1009
- try {
1010
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1011
- await viewer.app.flush()
1012
- const lines = viewer.app.captureCharFrame().split("\n")
1013
- const title = lines.findIndex((line) => line.includes("file00.txt"))
1014
- expect(lines.findIndex((line) => line.includes("All"))).toBe(title)
1015
- const frame = viewer.app.captureSpans()
1016
- const caps = frame.lines[title - 1].spans.filter((span) => span.width > 2)
1017
- expect(caps).toHaveLength(2)
1018
- caps.forEach((span) => expect(span.text.trim()).toBe(""))
1019
- expect(caps[0].bg).toEqual(frame.lines[title].spans.find((span) => span.text.includes("All"))!.bg)
1020
- expect(caps[1].bg).toEqual(frame.lines[title].spans.find((span) => span.text.includes("file00.txt"))!.bg)
1021
- expect(title).toBe(1)
1022
- expect(viewer.app.captureCharFrame()).not.toContain("Diff working tree")
1023
- expect(lines[title + 1].slice(lines[title].indexOf("file00.txt")).trim()).toBe("")
1024
- expect(lines[title + 2]).toContain("const first")
1025
-
1026
- const scroll = findScrollBox(viewer.app.renderer.root)!
1027
- scroll.scrollTo(5)
1028
- await viewer.app.flush()
1029
- expect(viewer.app.captureCharFrame().split("\n")[title - 1]).toBe(lines[title - 1])
1030
- expect(viewer.app.captureCharFrame().split("\n")[title]).toContain("file00.txt")
1031
-
1032
- viewer.commands.get("diff.next_file")!.run()
1033
- await viewer.app.flush()
1034
- expect(viewer.app.captureCharFrame().split("\n")[title - 1]).toBe(lines[title - 1])
1035
- expect(viewer.app.captureCharFrame().split("\n")[title]).toContain("file01.txt")
1036
- viewer.commands.get("diff.single_patch")!.run()
1037
- await viewer.app.flush()
1038
- expect(viewer.app.captureCharFrame().split("\n")[title]).toContain("file01.txt")
1039
-
1040
- viewer.app.resize(80, 30)
1041
- await viewer.app.flush()
1042
- expect(viewer.app.renderer.root.findDescendantById("diff-tree-top-edge")).toBeUndefined()
1043
- expect(viewer.app.renderer.root.findDescendantById("diff-patch-top-edge")).toBeDefined()
1044
- // The compact full screen header is one row, directly above the shared pane edge.
1045
- expect(viewer.app.captureCharFrame().split("\n")[title + 1]).toContain("file01.txt")
1046
- } finally {
1047
- viewer.app.renderer.destroy()
1048
- }
1049
- })
1050
-
1051
- test.each(["dark", "light"] as const)("the pane edge matches the visible reviewed card in %s mode", async (mode) => {
1052
- const viewer = await renderDiffViewer(
1053
- Array.from({ length: 4 }, (_, index) => ({ ...hunkDiff[0], file: `src/file${index}.txt` })),
1054
- { width: 160, height: 18, mode },
1055
- )
1056
- try {
1057
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1058
- await viewer.app.flush()
1059
- const scroll = findScrollBox(viewer.app.renderer.root)!
1060
- const expectEdge = (file: string, collapsed = false) => {
1061
- const frame = viewer.app.captureSpans()
1062
- // Full-screen cards are inset two columns from each pane edge.
1063
- const card = scroll.viewport.width - 4
1064
- const edge = frame.lines[scroll.viewport.y - 1].spans.find((span) => span.width === card)!
1065
- const page = frame.lines[scroll.viewport.y - 1].spans.at(-1)!.bg
1066
- const title = frame.lines[scroll.viewport.y].spans.find((span) => span.text.includes(file))!
1067
- expect(title, viewer.app.captureCharFrame()).toBeDefined()
1068
- expect(edge.text.trim()).toBe("")
1069
- expect(edge.bg).toEqual(title.bg)
1070
- expect(edge.bg).not.toEqual(page)
1071
- expect(frame.lines[0].spans[0].bg).toEqual(frame.lines[1].spans.find((span) => span.text.includes("All"))!.bg)
1072
- const bottom = frame.lines[scroll.viewport.y + 1].spans.find(
1073
- (span) => span.text.includes("▀") && span.width === card,
1074
- )
1075
- if (!collapsed) {
1076
- expect(bottom).toBeUndefined()
1077
- return
1078
- }
1079
- expect(bottom?.text.trim()).toBe("▀".repeat(card))
1080
- expect(bottom?.fg).toEqual(edge.bg)
1081
- expect(bottom?.bg).toEqual(page)
1082
- }
1083
- expectEdge("file0.txt")
1084
- viewer.app.mockInput.pressKey("m")
1085
- await viewer.app.flush()
1086
- expectEdge("file0.txt", true)
1087
- viewer.app.mockInput.pressKey("n")
1088
- await viewer.app.flush()
1089
- expectEdge("file1.txt")
1090
- viewer.app.mockInput.pressKey("m")
1091
- await viewer.app.flush()
1092
- expectEdge("file1.txt", true)
1093
- viewer.app.mockInput.pressKey("n")
1094
- await viewer.app.flush()
1095
- expectEdge("file2.txt")
1096
-
1097
- // Scrolling back must follow the visible card, not the selected unreviewed file.
1098
- scroll.scrollTo(0)
1099
- await viewer.app.flush()
1100
- expectEdge("file0.txt", true)
1101
- const second = viewer.app.renderer.root.findDescendantById("diff-file-header-1")!
1102
- const frame = viewer.app.captureSpans()
1103
- // The gap's card segment sits between the two inset segments.
1104
- expect(frame.lines[second.y - 1].spans.find((span) => span.text.includes("▄") && span.width > 2)!.fg).toEqual(
1105
- frame.lines[second.y].spans.find((span) => span.text.includes("file1.txt"))!.bg,
1106
- )
1107
-
1108
- viewer.commands.get("diff.single_patch")!.run()
1109
- await viewer.app.flush()
1110
- expectEdge("file0.txt", true)
1111
- viewer.app.mockInput.pressKey("m")
1112
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1113
- await viewer.app.flush()
1114
- expectEdge("file0.txt")
1115
- } finally {
1116
- viewer.app.renderer.destroy()
1117
- }
1118
- })
1119
-
1120
- test("the sidebar holds a compact help hint and the patch pane reaches the screen bottom", async () => {
1121
- const viewer = await renderDiffViewer(hunkDiff, { width: 160, height: 40, kittyKeyboard: true })
1122
- try {
1123
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1124
- await viewer.app.flush()
1125
- const scroll = findScrollBox(viewer.app.renderer.root)!
1126
- const hint = viewer.app.renderer.root.findDescendantById("diff-help-shortcut")!
1127
- expect(scroll.viewport.y + scroll.viewport.height).toBe(40)
1128
- expect(hint.x).toBe(2)
1129
- expect(hint.y).toBe(37)
1130
- expect(
1131
- findScrollBox(viewer.app.renderer.root, false)!.parent!.y +
1132
- findScrollBox(viewer.app.renderer.root, false)!.parent!.height,
1133
- ).toBe(40)
1134
- expect(viewer.app.captureCharFrame()).not.toContain("n/p files")
1135
- expect(viewer.app.captureCharFrame()).not.toContain("tab focus")
1136
- expect(viewer.app.captureCharFrame()).toContain("? help")
1137
- expect(viewer.app.captureCharFrame()).not.toContain("next file")
1138
- expect(viewer.app.captureCharFrame()).not.toContain("next hunk")
1139
- expect(viewer.app.captureCharFrame()).not.toContain("half page")
1140
- viewer.app.mockInput.pressTab()
1141
- await viewer.app.renderOnce()
1142
- expect(viewer.app.captureCharFrame()).toContain("? help")
1143
-
1144
- viewer.app.mockInput.pressKey("?")
1145
- await viewer.app.waitForFrame((frame) => frame.includes("Diff shortcuts"))
1146
- expect(viewer.app.captureCharFrame()).toContain("Next file")
1147
- expect(viewer.app.captureCharFrame()).toContain("Half page")
1148
- expect(viewer.app.captureCharFrame()).toContain("Split / unified")
1149
- expect(viewer.app.captureCharFrame()).toContain("alt+↓")
1150
- expect(viewer.app.captureCharFrame()).toContain("Review + collapse / reopen")
1151
- expect(viewer.app.captureCharFrame()).toContain("right-click")
1152
- expect(viewer.app.captureCharFrame()).not.toContain("Focus files")
1153
- expect(viewer.app.captureCharFrame()).not.toContain("Description")
1154
- viewer.app.mockInput.pressEscape()
1155
- await viewer.app.waitForFrame((frame) => !frame.includes("Diff shortcuts"))
1156
- expect(viewer.current()).toEqual(expect.objectContaining({ type: "plugin", name: "diff" }))
1157
- } finally {
1158
- viewer.app.renderer.destroy()
1159
- }
1160
- })
1161
-
1162
- test("compact gutter help leaves the full patch viewport available when the sidebar is hidden", async () => {
1163
- const viewer = await renderDiffViewer(hunkDiff, { width: 160, height: 24, kittyKeyboard: true })
1164
- try {
1165
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1166
- viewer.app.mockInput.pressKey("b")
1167
- await viewer.app.flush()
1168
- const scroll = findScrollBox(viewer.app.renderer.root)!
1169
- const hint = viewer.app.renderer.root.findDescendantById("diff-help-shortcut")!
1170
- expect(hint.y).toBe(0)
1171
- expect(hint.x).toBe(159)
1172
- expect(scroll.viewport.y).toBe(2)
1173
- expect(scroll.viewport.y + scroll.viewport.height).toBe(24)
1174
- await viewer.app.mockMouse.click(hint.x, hint.y)
1175
- await viewer.app.waitForFrame((frame) => frame.includes("Diff shortcuts"))
1176
- expect(viewer.app.renderer.getSelection()).toBeNull()
1177
- viewer.app.mockInput.pressEscape()
1178
- await viewer.app.waitForFrame((frame) => !frame.includes("Diff shortcuts"))
1179
-
1180
- viewer.app.mockInput.pressKey("b")
1181
- await viewer.app.flush()
1182
- expect(viewer.app.renderer.root.findDescendantById("diff-help-shortcut")!.y).toBe(21)
1183
- // Too narrow for the tree, full screen switches to the compact layout and its footer holds the help.
1184
- viewer.app.resize(80, 24)
1185
- await viewer.app.flush()
1186
- const footer = viewer.app.renderer.root.findDescendantById("diff-footer")!
1187
- expect(viewer.app.renderer.root.findDescendantById("diff-files")).toBeUndefined()
1188
- expect(viewer.app.renderer.root.findDescendantById("diff-help-shortcut")!.y).toBe(footer.y)
1189
- expect(scroll.viewport.y + scroll.viewport.height).toBe(footer.y)
1190
- expect(viewer.app.captureCharFrame()).not.toContain("? help")
1191
- // No padding: the header and footer sit on the first and last rows.
1192
- expect(viewer.app.captureCharFrame().split("\n")[0]).toContain("All")
1193
- expect(footer.y).toBe(23)
1194
- expect(viewer.app.captureCharFrame().split("\n")[footer.y]).toMatch(/^ escape close\s+\? see all $/)
1195
- viewer.app.mockInput.pressKey("d")
1196
- await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
1197
- viewer.app.mockInput.pressEscape()
1198
- await viewer.app.waitForFrame((frame) => !frame.includes("Diff source"))
1199
- } finally {
1200
- viewer.app.renderer.destroy()
1201
- }
1202
- })
1203
-
1204
- test("full screen keeps its tree down to 88 columns and uses the compact layout below", async () => {
1205
- const viewer = await renderDiffViewer(manyDiffs.slice(0, 3), { width: 88, height: 24, kittyKeyboard: true })
1206
- try {
1207
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1208
- viewer.app.mockInput.pressKey("m")
1209
- await viewer.app.flush()
1210
- const scroll = findScrollBox(viewer.app.renderer.root)!
1211
- expect(viewer.app.renderer.root.findDescendantById("diff-files")).toBeDefined()
1212
- expect(viewer.app.renderer.root.findDescendantById("diff-footer")).toBeUndefined()
1213
- // Full cards are inset two columns from the patch pane.
1214
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")!.width).toBe(scroll.viewport.width - 4)
1215
-
1216
- viewer.app.resize(87, 24)
1217
- await viewer.app.flush()
1218
- expect(findScrollBox(viewer.app.renderer.root)).toBe(scroll)
1219
- expect(viewer.app.renderer.root.findDescendantById("diff-files")).toBeUndefined()
1220
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")!.width).toBe(87)
1221
- const second = viewer.app.renderer.root.findDescendantById("diff-file-header-1")
1222
- if (!(second instanceof BoxRenderable)) throw new Error("Missing second file title")
1223
- // The gap above a flush card takes the card's color across the full width, without an inset notch.
1224
- expect(
1225
- viewer.app
1226
- .captureSpans()
1227
- .lines[second.y - 1].spans.flatMap((span) => Array.from({ length: span.width }, () => span.fg)),
1228
- ).toEqual(Array.from({ length: 87 }, () => second.backgroundColor))
1229
- expect(viewer.app.renderer.root.findDescendantById("diff-review-count")!.y).toBe(0)
1230
- const footer = viewer.app.renderer.root.findDescendantById("diff-footer")!
1231
- expect(viewer.app.captureCharFrame().split("\n")[footer.y]).toMatch(/^ escape close\s+\? see all $/)
1232
- expect(viewer.app.captureCharFrame()).toContain("1/3")
1233
- viewer.app.mockInput.pressKey("b")
1234
- await viewer.app.flush()
1235
- expect(viewer.app.renderer.root.findDescendantById("diff-files")).toBeUndefined()
1236
-
1237
- viewer.app.resize(88, 24)
1238
- await viewer.app.flush()
1239
- expect(viewer.app.renderer.root.findDescendantById("diff-files")).toBeDefined()
1240
- expect(viewer.app.renderer.root.findDescendantById("diff-footer")).toBeUndefined()
1241
- expect(viewer.app.captureCharFrame()).toContain("1/3")
1242
- } finally {
1243
- viewer.app.renderer.destroy()
1244
- }
1245
- })
1246
-
1247
- test("compact help scrolls at a narrow size and reflects customized bindings", async () => {
1248
- const viewer = await renderDiffViewer(hunkDiff, {
1249
- width: 50,
1250
- height: 20,
1251
- kittyKeyboard: true,
1252
- keybinds: { "diff.next_file": "ctrl+n", "diff.previous_file": "ctrl+p", "diff.close": "x" },
1253
- })
1254
- try {
1255
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1256
- viewer.app.mockInput.pressKey("?")
1257
- await viewer.app.waitForFrame((frame) => frame.includes("Diff shortcuts"))
1258
- const scroll = viewer.app.renderer.root.findDescendantById("diff-help-scroll")
1259
- if (!(scroll instanceof ScrollBoxRenderable)) throw new Error("Missing help scrollbox")
1260
- expect(scroll.width).toBeLessThanOrEqual(46)
1261
- expect(viewer.app.captureCharFrame()).toMatch(/ctrl\+n\s+Next file/)
1262
- expect(viewer.app.captureCharFrame()).not.toContain("alt+↓")
1263
- scroll.scrollTo(scroll.scrollHeight)
1264
- await viewer.app.flush()
1265
- // The compact layout has no file tree to toggle.
1266
- expect(viewer.app.captureCharFrame()).not.toContain("Show / hide file tree")
1267
- expect(viewer.app.captureCharFrame()).not.toContain("Focus files")
1268
- expect(viewer.app.captureCharFrame()).toMatch(/x\s+Close diff viewer/)
1269
- viewer.app.mockInput.pressEscape()
1270
- await viewer.app.waitForFrame((frame) => !frame.includes("Diff shortcuts"))
1271
- expect(viewer.current()).toEqual(expect.objectContaining({ type: "plugin", name: "diff" }))
1272
- } finally {
1273
- viewer.app.renderer.destroy()
1274
- }
1275
- })
1276
-
1277
- test.each([
1278
- { key: "?", shift: false },
1279
- { key: "?", shift: true },
1280
- { key: "/", shift: true },
1281
- ])("the viewer opens help from $key with shift=$shift", async (input) => {
1282
- const viewer = await renderDiffViewer(hunkDiff, { width: 160, height: 40, kittyKeyboard: true })
1283
- try {
1284
- await viewer.app.waitForFrame((frame) => frame.includes("const first") && frame.includes("? help"))
1285
- viewer.app.mockInput.pressKey(input.key, { shift: input.shift })
1286
- await viewer.app.waitForFrame((frame) => frame.includes("Diff shortcuts"))
1287
- expect(viewer.app.captureCharFrame()).toContain("Next file")
1288
- viewer.app.mockInput.pressEscape()
1289
- await viewer.app.waitForFrame((frame) => !frame.includes("Diff shortcuts"))
1290
- expect(viewer.app.captureCharFrame()).toContain("const first")
1291
- } finally {
1292
- viewer.app.renderer.destroy()
1293
- }
1294
- })
1295
-
1296
- test.each([80, 160])("file titles stick to the viewport and hand off while scrolling at %i columns", async (width) => {
1297
- const viewer = await renderDiffViewer(
1298
- Array.from({ length: 4 }, (_, index) => ({ ...hunkDiff[0], file: `src/file${index}.txt` })),
1299
- { width, height: 18 },
1300
- )
1301
- try {
1302
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1303
- await viewer.app.flush()
1304
- const scroll = findScrollBox(viewer.app.renderer.root)!
1305
- const first = viewer.app.renderer.root.findDescendantById("diff-file-header-0")
1306
- const second = viewer.app.renderer.root.findDescendantById("diff-file-header-1")
1307
- if (!(first instanceof BoxRenderable) || !(second instanceof BoxRenderable)) throw new Error("Missing file titles")
1308
- const next = second.y - scroll.content.y
1309
-
1310
- scroll.scrollTo(5)
1311
- await viewer.app.flush()
1312
- expect(first.y).toBe(scroll.viewport.y)
1313
- expect(viewer.app.captureCharFrame().split("src/file0.txt")).toHaveLength(2)
1314
-
1315
- viewer.app.resize(width === 160 ? 80 : 160, 22)
1316
- await viewer.app.flush()
1317
- expect(first.y).toBe(scroll.viewport.y)
1318
- viewer.app.resize(width, 18)
1319
- await viewer.app.flush()
1320
- expect(first.y).toBe(scroll.viewport.y)
1321
-
1322
- if (width === 160) {
1323
- viewer.app.mockInput.pressTab()
1324
- await viewer.app.renderOnce()
1325
- expect(first.y).toBe(scroll.viewport.y)
1326
- expect(viewer.app.captureCharFrame()).toContain("src/file0.txt")
1327
- viewer.app.mockInput.pressTab()
1328
- }
1329
-
1330
- scroll.scrollTo(next + 3)
1331
- await viewer.app.flush()
1332
- expect(second.y).toBe(scroll.viewport.y)
1333
- expect(first.y + first.height).toBeLessThanOrEqual(scroll.viewport.y)
1334
- expect(viewer.app.captureCharFrame().split("src/file1.txt")).toHaveLength(2)
1335
- expect(viewer.app.captureCharFrame()).not.toContain("src/file0.txt")
1336
-
1337
- const background = second.backgroundColor
1338
- viewer.commands.get("diff.mark_reviewed")!.run()
1339
- await viewer.app.flush()
1340
- expect(second.backgroundColor).not.toEqual(background)
1341
- expect(second.y).toBe(scroll.viewport.y)
1342
-
1343
- scroll.scrollTo(0)
1344
- await viewer.app.flush()
1345
- expect(first.translateY).toBe(0)
1346
- expect(first.y).toBe(scroll.viewport.y)
1347
-
1348
- viewer.commands.get("diff.single_patch")!.run()
1349
- await viewer.app.flush()
1350
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-1")).toBeUndefined()
1351
- const single = viewer.app.renderer.root.findDescendantById("diff-file-header-0")
1352
- if (!(single instanceof BoxRenderable)) throw new Error("Missing single file title")
1353
- scroll.scrollTo(5)
1354
- await viewer.app.flush()
1355
- expect(single.y).toBe(scroll.viewport.y)
1356
- } finally {
1357
- viewer.app.renderer.destroy()
1358
- }
1359
- })
1360
-
1361
- test("split diff filler keeps the full card background", async () => {
1362
- const viewer = await renderDiffViewer(
1363
- [
1364
- {
1365
- file: ".gitignore",
1366
- status: "modified",
1367
- additions: 1,
1368
- deletions: 0,
1369
- patch: "--- a/.gitignore\n+++ b/.gitignore\n@@ -1 +1,2 @@\n node_modules/\n+artifacts/\n",
1370
- },
1371
- ],
1372
- { width: 160 },
1373
- )
1374
- try {
1375
- await viewer.app.waitForFrame((frame) => frame.includes("artifacts/"))
1376
- const diff = findDiffs(viewer.app.renderer.root)[0]
1377
- const frame = viewer.app.captureSpans()
1378
- const backgrounds = (row: number) =>
1379
- frame.lines[row].spans.flatMap((span) => Array.from({ length: span.width }, () => span.bg))
1380
- expect(backgrounds(diff.y + 1).slice(diff.x, diff.x + Math.floor(diff.width / 2))).toEqual(
1381
- Array.from({ length: Math.floor(diff.width / 2) }, () => backgrounds(diff.y - 1)[diff.x]),
1382
- )
1383
- } finally {
1384
- viewer.app.renderer.destroy()
1385
- }
1386
- })
1387
-
1388
- test.each(["patch", "image", "fallback"] as const)(
1389
- "reviewing collapses a %s body and unmarking reopens it",
1390
- async (kind) => {
1391
- const file =
1392
- kind === "patch"
1393
- ? hunkDiff[0]
1394
- : {
1395
- file: kind === "image" ? "assets/preview.png" : "bun.lock",
1396
- status: "modified",
1397
- additions: 0,
1398
- deletions: 0,
1399
- }
1400
- const content = kind === "patch" ? "const first" : kind === "image" ? "96 x 48" : "No patch available"
1401
- const viewer = await renderDiffViewer([file], { width: 80, height: kind === "patch" ? 16 : 24 })
1402
- try {
1403
- await viewer.app.waitForFrame((frame) => frame.includes(content))
1404
- const scroll = findScrollBox(viewer.app.renderer.root)!
1405
- if (kind === "patch") {
1406
- scroll.scrollTo(4)
1407
- await viewer.app.flush()
1408
- expect(scroll.scrollTop).toBeGreaterThan(0)
1409
- }
1410
- viewer.app.mockInput.pressKey("m")
1411
- await viewer.app.flush()
1412
- const header = viewer.app.renderer.root.findDescendantById("diff-file-header-0")
1413
- if (!(header instanceof BoxRenderable)) throw new Error("Missing reviewed file title")
1414
- expect(header.parent?.height).toBe(header.height)
1415
- expect(header.height).toBe(2)
1416
- // Compact cards are flush with the pane edges.
1417
- expect(viewer.app.captureCharFrame().split("\n")[header.y + 1].trim()).toBe("▀".repeat(header.width))
1418
- expect(header.y).toBe(scroll.viewport.y)
1419
- expect(viewer.app.captureCharFrame()).toContain(file.file)
1420
- expect(viewer.app.captureCharFrame()).toContain("✓")
1421
- expect(viewer.app.captureCharFrame()).not.toContain(content)
1422
- expect(findDiffs(viewer.app.renderer.root)).toHaveLength(0)
1423
-
1424
- viewer.app.mockInput.pressKey("m")
1425
- await viewer.app.waitForFrame((frame) => frame.includes(content))
1426
- await viewer.app.flush()
1427
- expect(header.parent!.height).toBeGreaterThan(header.height)
1428
- expect(viewer.app.captureCharFrame().split("\n")[header.y + 1].trim()).toBe("")
1429
- expect(viewer.app.captureCharFrame()).not.toContain("✓")
1430
- if (kind === "patch") {
1431
- viewer.app.mockInput.pressKey("]")
1432
- await viewer.app.flush()
1433
- expect(scroll.scrollTop).toBeGreaterThan(0)
1434
- }
1435
- } finally {
1436
- viewer.app.renderer.destroy()
1437
- }
1438
- },
1439
- )
1440
-
1441
- test("reviewing after scrolling targets the visible file rather than the initial file", async () => {
1442
- const viewer = await renderDiffViewer(
1443
- [
1444
- { ...hunkDiff[0], file: "src/file0.txt" },
1445
- { ...hunkDiff[0], file: "src/file1.txt" },
1446
- ],
1447
- { width: 160, height: 16 },
1448
- )
1449
- try {
1450
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1451
- await viewer.app.flush()
1452
- const scroll = findScrollBox(viewer.app.renderer.root)!
1453
- scroll.scrollTo(scroll.scrollTop + findDiffs(viewer.app.renderer.root)[3].y - scroll.viewport.y)
1454
- await viewer.app.renderOnce()
1455
- viewer.commands.get("diff.mark_reviewed")!.run()
1456
- await viewer.app.waitForFrame((frame) => /file1\.txt\s+✓/.test(frame))
1457
- expect(viewer.app.captureCharFrame()).not.toMatch(/file0\.txt\s+✓/)
1458
- } finally {
1459
- viewer.app.renderer.destroy()
1460
- }
1461
- })
1462
-
1463
- test.each([80, 160])(
1464
- "single-file review advances, but reopening and the last file stay put at %i columns",
1465
- async (width) => {
1466
- const viewer = await renderDiffViewer(
1467
- Array.from({ length: 3 }, (_, index) => ({ ...hunkDiff[0], file: `src/file${index}.txt` })),
1468
- { width, height: 24, kittyKeyboard: true },
1469
- )
1470
- try {
1471
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1472
- viewer.app.mockInput.pressKey("s")
1473
- await viewer.app.flush()
1474
- if (width === 160) viewer.app.mockInput.pressTab()
1475
- viewer.app.mockInput.pressKey("m")
1476
- await viewer.app.flush()
1477
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")).toBeUndefined()
1478
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-1")).toBeDefined()
1479
- expect(viewer.app.captureCharFrame()).toContain("const first")
1480
-
1481
- viewer.app.mockInput.pressKey("p")
1482
- await viewer.app.flush()
1483
- expect(findDiffs(viewer.app.renderer.root)).toHaveLength(0)
1484
- viewer.app.mockInput.pressKey("m")
1485
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1486
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")).toBeDefined()
1487
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-1")).toBeUndefined()
1488
-
1489
- viewer.app.mockInput.pressKey("n")
1490
- await viewer.app.flush()
1491
- viewer.app.mockInput.pressKey("m")
1492
- await viewer.app.flush()
1493
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-2")).toBeDefined()
1494
- viewer.app.mockInput.pressKey("m")
1495
- await viewer.app.flush()
1496
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-2")).toBeDefined()
1497
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")).toBeUndefined()
1498
- expect(findDiffs(viewer.app.renderer.root)).toHaveLength(0)
1499
- viewer.app.mockInput.pressKey("m")
1500
- await viewer.app.flush()
1501
- expect(findDiffs(viewer.app.renderer.root)).toHaveLength(0)
1502
-
1503
- viewer.app.mockInput.pressKey("?")
1504
- await viewer.app.waitForFrame((frame) => frame.includes("Review + next / reopen"))
1505
- } finally {
1506
- viewer.app.renderer.destroy()
1507
- }
1508
- },
1509
- )
1510
-
1511
- test.each([false, true])("Alt+arrows navigate files rather than session tabs (after Tab: %s)", async (afterTab) => {
1512
- let tabChanges = 0
1513
- const viewer = await renderDiffViewer(manyDiffs, {
1514
- width: 160,
1515
- height: 24,
1516
- kittyKeyboard: true,
1517
- onSessionTab: () => tabChanges++,
1518
- })
1519
- try {
1520
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1521
- await viewer.app.flush()
1522
- const scroll = findScrollBox(viewer.app.renderer.root)!
1523
- if (afterTab) viewer.app.mockInput.pressTab()
1524
- viewer.app.mockInput.pressKey("down", { meta: true })
1525
- await viewer.app.flush()
1526
- expect(viewer.app.captureCharFrame().split("\n")[scroll.viewport.y]).toContain("file01.txt")
1527
- viewer.app.mockInput.pressKey("up", { meta: true })
1528
- await viewer.app.flush()
1529
- expect(scroll.scrollTop).toBe(0)
1530
- expect(tabChanges).toBe(0)
1531
- } finally {
1532
- viewer.app.renderer.destroy()
1533
- }
1534
- })
1535
-
1536
- test.each([
1537
- { target: "tree", mode: "dark" },
1538
- { target: "heading", mode: "dark" },
1539
- { target: "tree", mode: "light" },
1540
- { target: "heading", mode: "light" },
1541
- ] as const)("the $target context menu completes the clicked file without selecting it in $mode mode", async (input) => {
1542
- const viewer = await renderDiffViewer(manyDiffs.slice(0, 3), {
1543
- width: 160,
1544
- height: 40,
1545
- kittyKeyboard: true,
1546
- mode: input.mode,
1547
- })
1548
- try {
1549
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1550
- await viewer.app.flush()
1551
- const scroll = findScrollBox(viewer.app.renderer.root)!
1552
- const node = () =>
1553
- viewer.app.renderer.root.findDescendantById(input.target === "tree" ? "diff-file-row-1" : "diff-file-header-1")!
1554
- await viewer.app.mockMouse.click(node().x + 4, node().y, MouseButton.RIGHT)
1555
- await viewer.app.waitForFrame((frame) => frame.includes("Mark complete"))
1556
- expect(scroll.scrollTop).toBe(0)
1557
- expect(viewer.app.captureCharFrame()).toContain("0/3")
1558
- viewer.app.mockInput.pressKey("n")
1559
- viewer.app.mockInput.pressKey("m")
1560
- await viewer.app.flush()
1561
- expect(scroll.scrollTop).toBe(0)
1562
- expect(viewer.app.captureCharFrame()).toContain("0/3")
1563
- const menu = viewer.app.renderer.root.findDescendantById("diff-file-menu")!
1564
- expect(
1565
- viewer.app.captureSpans().lines[menu.y].spans.find((span) => span.text.includes("Mark complete"))!.bg,
1566
- ).not.toEqual(viewer.app.captureSpans().lines[1].spans.find((span) => span.text.includes("All"))!.bg)
1567
- await viewer.app.mockMouse.click(menu.x + 1, menu.y)
1568
- await viewer.app.waitForFrame((frame) => frame.includes("1/3"))
1569
- expect(viewer.app.captureCharFrame()).toMatch(/file01\.txt\s+✓/)
1570
- expect(viewer.app.captureCharFrame()).not.toMatch(/file00\.txt\s+✓/)
1571
- expect(viewer.app.renderer.root.findDescendantById("diff-file-menu")).toBeUndefined()
1572
- expect(findDiffs(viewer.app.renderer.root)).toHaveLength(6)
1573
- expect(scroll.scrollTop).toBe(0)
1574
-
1575
- await viewer.app.mockMouse.click(node().x + 4, node().y, MouseButton.RIGHT)
1576
- await viewer.app.waitForFrame((frame) => frame.includes("Mark incomplete"))
1577
- viewer.app.mockInput.pressEnter()
1578
- await viewer.app.waitForFrame((frame) => frame.includes("0/3"))
1579
- expect(findDiffs(viewer.app.renderer.root)).toHaveLength(9)
1580
- expect(scroll.scrollTop).toBe(0)
1581
- } finally {
1582
- viewer.app.renderer.destroy()
1583
- }
1584
- })
1585
-
1586
- test.each(["escape", "outside"] as const)(
1587
- "a clamped file menu dismisses with %s without closing the viewer",
1588
- async (dismiss) => {
1589
- const viewer = await renderDiffViewer(hunkDiff, { width: 80, height: 16, kittyKeyboard: true })
1590
- try {
1591
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1592
- await viewer.app.flush()
1593
- const header = viewer.app.renderer.root.findDescendantById("diff-file-header-0")!
1594
- await viewer.app.mockMouse.click(header.x + header.width - 1, header.y, MouseButton.RIGHT)
1595
- await viewer.app.waitForFrame((frame) => frame.includes("Mark complete"))
1596
- const menu = viewer.app.renderer.root.findDescendantById("diff-file-menu")!
1597
- expect(menu.x).toBeGreaterThanOrEqual(0)
1598
- expect(menu.x + menu.width).toBeLessThanOrEqual(80)
1599
- expect(menu.y + menu.height).toBeLessThanOrEqual(16)
1600
- if (dismiss === "escape") viewer.app.mockInput.pressEscape()
1601
- if (dismiss === "outside") await viewer.app.mockMouse.click(1, 1)
1602
- await viewer.app.waitForFrame((frame) => !frame.includes("Mark complete"))
1603
- expect(viewer.current()).toEqual(expect.objectContaining({ type: "plugin", name: "diff" }))
1604
- expect(findDiffs(viewer.app.renderer.root)).toHaveLength(3)
1605
- viewer.app.mockInput.pressKey("j")
1606
- await viewer.app.flush()
1607
- expect(findScrollBox(viewer.app.renderer.root)!.scrollTop).toBe(1)
1608
- } finally {
1609
- viewer.app.renderer.destroy()
1610
- }
1611
- },
1612
- )
1613
-
1614
- test("single-file menu actions advance only when completing the current file", async () => {
1615
- const viewer = await renderDiffViewer(manyDiffs.slice(0, 3), { width: 160, height: 24, kittyKeyboard: true })
1616
- try {
1617
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1618
- viewer.app.mockInput.pressKey("s")
1619
- await viewer.app.flush()
1620
- const other = viewer.app.renderer.root.findDescendantById("diff-file-row-2")!
1621
- await viewer.app.mockMouse.click(other.x + 4, other.y, MouseButton.RIGHT)
1622
- await viewer.app.waitForFrame((frame) => frame.includes("Mark complete"))
1623
- viewer.app.mockInput.pressEnter()
1624
- await viewer.app.waitForFrame((frame) => frame.includes("1/3"))
1625
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")).toBeDefined()
1626
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-2")).toBeUndefined()
1627
- expect(viewer.app.captureCharFrame()).toMatch(/file02\.txt\s+✓/)
1628
- const current = viewer.app.renderer.root.findDescendantById("diff-file-header-0")!
1629
- await viewer.app.mockMouse.click(current.x + 4, current.y, MouseButton.RIGHT)
1630
- await viewer.app.waitForFrame((frame) => frame.includes("Mark complete"))
1631
- viewer.app.mockInput.pressEnter()
1632
- await viewer.app.waitForFrame((frame) => frame.includes("2/3") && frame.includes("const first"))
1633
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")).toBeUndefined()
1634
- expect(viewer.app.renderer.root.findDescendantById("diff-file-header-1")).toBeDefined()
1635
- expect(viewer.app.captureCharFrame()).toContain("const first")
1636
- } finally {
1637
- viewer.app.renderer.destroy()
1638
- }
1639
- })
1640
-
1641
- test("image previews use the diff session's filesystem location", async () => {
1642
- const viewer = await renderDiffViewer([
1643
- { file: "assets/mock image.png", status: "modified", additions: 0, deletions: 0 },
1644
- ])
1645
- try {
1646
- await viewer.app.waitFor(() => viewer.imageReadInput() !== undefined)
1647
- expect(viewer.imageReadInput()).toEqual({ path: "assets/mock image.png", location: { directory: "/repo/session" } })
1648
- await viewer.app.waitForFrame((frame) => frame.includes("96 x 48"))
1649
- const lines = viewer.app.captureCharFrame().split("\n")
1650
- const row = lines.findIndex((line) => line.includes("Working tree preview"))
1651
- const top = lines[lines.findIndex((line) => line.includes("assets/mock image.png")) - 1]
1652
- const header = viewer.app.renderer.root.findDescendantById("diff-file-header-0")!
1653
- const background = viewer.app.captureSpans().lines[row].spans.find((span) => span.text.includes("Working tree"))!.bg
1654
- const edges = viewer.app
1655
- .captureSpans()
1656
- .lines[row + 2].spans.flatMap((span) => Array.from({ length: span.width }, () => span.bg))
1657
- expect(top.trim()).toBe("")
1658
- expect(edges[header.x]).toEqual(background)
1659
- expect(edges[header.x + header.width - 1]).toEqual(background)
1660
- } finally {
1661
- viewer.app.renderer.destroy()
1662
- }
1663
- })
1664
-
1665
- test("single-file images load once per file and cancel reads when their preview is removed", async () => {
1666
- const reads: { file: string; signal: AbortSignal }[] = []
1667
- const viewer = await renderDiffViewer(
1668
- ["a.png", "b.png"].map((file) => ({ file, status: "modified", additions: 0, deletions: 0 })),
1669
- {
1670
- single: true,
1671
- readImage: async (file, signal) => {
1672
- reads.push({ file, signal })
1673
- return diffImageFixture
1674
- },
1675
- },
1676
- )
1677
- try {
1678
- await viewer.app.waitForFrame((frame) => frame.includes("96 x 48"))
1679
- expect(reads.map((read) => read.file)).toEqual(["a.png"])
1680
- viewer.app.mockInput.pressKey("n")
1681
- await viewer.app.waitForFrame((frame) => frame.includes("b.png") && frame.includes("96 x 48"))
1682
- await viewer.app.flush()
1683
- expect(reads.map((read) => read.file)).toEqual(["a.png", "b.png"])
1684
- expect(reads[0].signal.aborted).toBe(true)
1685
- viewer.app.mockInput.pressKey("j")
1686
- await viewer.app.flush()
1687
- expect(reads.map((read) => read.file)).toEqual(["a.png", "b.png"])
1688
- viewer.app.mockInput.pressKey("m")
1689
- await viewer.app.flush()
1690
- expect(reads[1].signal.aborted).toBe(true)
1691
- } finally {
1692
- viewer.app.renderer.destroy()
1693
- }
1694
- })
1695
-
1696
- test("reviewing a mouse-scrolled past selection keeps the visible file in place", async () => {
1697
- const viewer = await renderDiffViewer(manyDiffs.slice(0, 5), { width: 160, height: 24, kittyKeyboard: true })
1698
- try {
1699
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1700
- viewer.app.mockInput.pressKey("n")
1701
- await viewer.app.flush()
1702
- const scroll = findScrollBox(viewer.app.renderer.root)!
1703
- const header = viewer.app.renderer.root.findDescendantById("diff-file-header-2")!
1704
- scroll.scrollTo(scroll.scrollTop + header.y - scroll.viewport.y + 2)
1705
- await viewer.app.flush()
1706
- await viewer.app.mockMouse.scroll(scroll.x + 5, scroll.viewport.y + 5, "down")
1707
- await viewer.app.flush()
1708
- const before = scroll.scrollTop
1709
- const row = viewer.app.renderer.root.findDescendantById("diff-file-row-1")!
1710
- await viewer.app.mockMouse.click(row.x + 4, row.y, MouseButton.RIGHT)
1711
- await viewer.app.waitForFrame((frame) => frame.includes("Mark complete"))
1712
- viewer.app.mockInput.pressEnter()
1713
- await viewer.app.flush()
1714
- expect(viewer.app.captureCharFrame().split("\n")[scroll.viewport.y]).toContain("file02.txt")
1715
- expect(scroll.scrollTop).toBeLessThan(before)
1716
- viewer.app.mockInput.pressKey("m")
1717
- await viewer.app.waitForFrame((frame) => /file02\.txt\s+✓/.test(frame))
1718
- } finally {
1719
- viewer.app.renderer.destroy()
1720
- }
1721
- })
1722
-
1723
- test.each(["patch", "image"] as const)(
1724
- "completing and reopening an earlier %s preserves the reading position",
1725
- async (kind) => {
1726
- const pending = Promise.withResolvers<Uint8Array>()
1727
- let reads = 0
1728
- const viewer = await renderDiffViewer(
1729
- [
1730
- ...(kind === "image" ? [{ file: "a.png", status: "modified", additions: 0, deletions: 0 }] : []),
1731
- ...manyDiffs.slice(0, 4),
1732
- ],
1733
- {
1734
- width: 160,
1735
- height: 24,
1736
- kittyKeyboard: true,
1737
- readImage: () => (++reads === 1 ? Promise.resolve(diffImageFixture) : pending.promise),
1738
- },
1739
- )
1740
- try {
1741
- await viewer.app.waitForFrame((frame) => frame.includes(kind === "image" ? "96 x 48" : "const first"))
1742
- viewer.app.mockInput.pressKey("n")
1743
- await viewer.app.flush()
1744
- const scroll = findScrollBox(viewer.app.renderer.root)!
1745
- scroll.scrollBy(4)
1746
- await viewer.app.flush()
1747
- const patchFrame = () =>
1748
- viewer.app
1749
- .captureCharFrame()
1750
- .split("\n")
1751
- .map((line) => line.slice(scroll.x))
1752
- .join("\n")
1753
- const before = patchFrame()
1754
- const row = viewer.app.renderer.root.findDescendantById("diff-file-row-0")!
1755
- for (const label of ["Mark complete", "Mark incomplete"]) {
1756
- await viewer.app.mockMouse.click(row.x + 4, row.y, MouseButton.RIGHT)
1757
- await viewer.app.waitForFrame((frame) => frame.includes(label))
1758
- viewer.app.mockInput.pressEnter()
1759
- await viewer.app.flush()
1760
- expect(patchFrame()).toBe(before)
1761
- }
1762
- if (kind === "patch") return
1763
- pending.resolve(diffImageFixture)
1764
- await viewer.app.waitFor(() => {
1765
- const image = viewer.app.renderer.root.findDescendantById("diff-image-a.png")
1766
- return image instanceof ImageRenderable && image.image?.width === 96
1767
- })
1768
- await viewer.app.flush()
1769
- expect(patchFrame()).toBe(before)
1770
- } finally {
1771
- pending.resolve(diffImageFixture)
1772
- viewer.app.renderer.destroy()
1773
- }
1774
- },
1775
- )
1776
-
1777
- test.each([
1778
- { file: "bun.lock", status: "modified" as const, message: "No patch available for this file." },
1779
- { file: "assets/old.png", status: "deleted" as const, message: "Deleted image." },
1780
- ])("pads $file fallback messages inside the card without reading images", async (input) => {
1781
- const viewer = await renderDiffViewer([{ file: input.file, status: input.status, additions: 0, deletions: 0 }])
1782
- try {
1783
- await viewer.app.waitForFrame((frame) => frame.includes(input.message))
1784
- expect(viewer.imageReadInput()).toBeUndefined()
1785
- const header = viewer.app.renderer.root.findDescendantById("diff-file-header-0")
1786
- if (!(header instanceof BoxRenderable)) throw new Error("Missing file title")
1787
- const lines = viewer.app.captureCharFrame().split("\n")
1788
- const title = lines.findIndex((line) => line.includes(input.file))
1789
- const row = lines.findIndex((line) => line.includes(input.message))
1790
- expect(lines[row].indexOf(input.message)).toBe(lines[title].indexOf(input.file))
1791
- expect(lines[row + 1].trim()).toBe("")
1792
- const padding = viewer.app
1793
- .captureSpans()
1794
- .lines[row + 1].spans.flatMap((span) => Array.from({ length: span.width }, () => span.bg))
1795
- expect(padding[header.x]).toEqual(header.backgroundColor)
1796
- expect(padding[header.x + header.width - 1]).toEqual(header.backgroundColor)
1797
- } finally {
1798
- viewer.app.renderer.destroy()
1799
- }
1800
- })
1801
-
1802
- test("Ctrl+D and Ctrl+U scroll the patch viewport by half a page, without closing", async () => {
1803
- const viewer = await renderDiffViewer(manyDiffs, { width: 160, height: 24 })
1804
- try {
1805
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1806
- await viewer.app.flush()
1807
- const scroll = findScrollBox(viewer.app.renderer.root)!
1808
- viewer.app.mockInput.pressKey("d", { ctrl: true })
1809
- await viewer.app.renderOnce()
1810
- expect(viewer.current().type).toBe("plugin")
1811
- expect(scroll.scrollTop).toBe(Math.floor(scroll.viewport.height / 2))
1812
- viewer.app.mockInput.pressKey("u", { ctrl: true })
1813
- await viewer.app.renderOnce()
1814
- expect(scroll.scrollTop).toBe(0)
1815
- viewer.app.mockInput.pressKey("f", { ctrl: true })
1816
- await viewer.app.renderOnce()
1817
- expect(scroll.scrollTop).toBe(scroll.viewport.height)
1818
- viewer.app.mockInput.pressKey("b", { ctrl: true })
1819
- await viewer.app.renderOnce()
1820
- expect(scroll.scrollTop).toBe(0)
1821
- } finally {
1822
- viewer.app.renderer.destroy()
1823
- }
1824
- })
1825
-
1826
- test.each([20, 32])("Tab and file clicks never redirect scrolling keys into the tree at %i rows", async (height) => {
1827
- const viewer = await renderDiffViewer(manyDiffs, { width: 160, height, keybinds: { "diff.switch_focus": "tab" } })
1828
- try {
1829
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1830
- await viewer.app.flush()
1831
- const scroll = findScrollBox(viewer.app.renderer.root)!
1832
- const tree = findScrollBox(viewer.app.renderer.root, false)!
1833
- const row = viewer.app.renderer.root.findDescendantById("diff-file-row-0")!
1834
- expect(viewer.commands.has("diff.switch_focus")).toBe(false)
1835
- await viewer.app.mockMouse.click(row.x + 4, row.y)
1836
- viewer.app.mockInput.pressTab()
1837
- viewer.app.mockInput.pressKey("j")
1838
- await viewer.app.renderOnce()
1839
- expect(scroll.scrollTop).toBe(1)
1840
- expect(tree.scrollTop).toBe(0)
1841
- viewer.app.mockInput.pressKey("k")
1842
- await viewer.app.renderOnce()
1843
- expect(scroll.scrollTop).toBe(0)
1844
- viewer.app.mockInput.pressArrow("down")
1845
- await viewer.app.renderOnce()
1846
- expect(scroll.scrollTop).toBe(1)
1847
- viewer.app.mockInput.pressArrow("up")
1848
- await viewer.app.renderOnce()
1849
- expect(scroll.scrollTop).toBe(0)
1850
- viewer.app.mockInput.pressKey("d", { ctrl: true })
1851
- await viewer.app.renderOnce()
1852
- const half = Math.floor(scroll.viewport.height / 2)
1853
- expect(viewer.current().type).toBe("plugin")
1854
- expect(scroll.scrollTop).toBe(half)
1855
- expect(tree.scrollTop).toBe(0)
1856
- viewer.app.mockInput.pressKey("u", { ctrl: true })
1857
- await viewer.app.renderOnce()
1858
- expect(scroll.scrollTop).toBe(0)
1859
- viewer.app.mockInput.pressKey("f", { ctrl: true })
1860
- await viewer.app.renderOnce()
1861
- expect(scroll.scrollTop).toBe(scroll.viewport.height)
1862
- expect(tree.scrollTop).toBe(0)
1863
- } finally {
1864
- viewer.app.renderer.destroy()
1865
- }
1866
- })
1867
-
1868
- test("folders are mouse controlled and n/p reveal files inside collapsed folders", async () => {
1869
- const viewer = await renderDiffViewer(
1870
- [
1871
- { ...hunkDiff[0], file: "src/a/first.txt" },
1872
- { ...hunkDiff[0], file: "src/b/second.txt" },
1873
- { ...hunkDiff[0], file: "test/third.txt" },
1874
- ],
1875
- { width: 160, height: 28 },
1876
- )
1877
- try {
1878
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1879
- await viewer.app.flush()
1880
- const treeFrame = () =>
1881
- viewer.app
1882
- .captureCharFrame()
1883
- .split("\n")
1884
- .map((line) => line.slice(0, 40))
1885
- .join("\n")
1886
- const folder = viewer.app.renderer.root.findDescendantById("diff-folder-row-0")!
1887
- await viewer.app.mockMouse.click(folder.x + 1, folder.y, MouseButton.RIGHT)
1888
- expect(viewer.app.renderer.root.findDescendantById("diff-file-menu")).toBeUndefined()
1889
- expect(treeFrame()).toContain("first.txt")
1890
- await viewer.app.mockMouse.click(folder.x + 1, folder.y)
1891
- await viewer.app.renderOnce()
1892
- expect(treeFrame()).toContain("▸ src")
1893
- expect(treeFrame()).not.toContain("first.txt")
1894
- viewer.app.mockInput.pressTab()
1895
- viewer.app.mockInput.pressKey("l")
1896
- viewer.app.mockInput.pressKey("}")
1897
- viewer.app.mockInput.pressKey("h")
1898
- await viewer.app.renderOnce()
1899
- expect(treeFrame()).toContain("▸ src")
1900
- viewer.app.mockInput.pressKey("n")
1901
- await viewer.app.flush()
1902
- expect(treeFrame()).toContain("▾ src")
1903
- expect(treeFrame()).toContain("second.txt")
1904
- const scroll = findScrollBox(viewer.app.renderer.root)!
1905
- expect(viewer.app.captureCharFrame().split("\n")[scroll.viewport.y]).toContain("src/b/second.txt")
1906
- viewer.app.mockInput.pressKey("p")
1907
- await viewer.app.flush()
1908
- expect(scroll.scrollTop).toBe(0)
1909
- } finally {
1910
- viewer.app.renderer.destroy()
1911
- }
1912
- })
1913
-
1914
- test("n/p jump between files and gg/G always navigate the diff", async () => {
1915
- const viewer = await renderDiffViewer(manyDiffs, { width: 160, height: 24 })
1916
- try {
1917
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1918
- await viewer.app.flush()
1919
- const scroll = findScrollBox(viewer.app.renderer.root)!
1920
- const row = viewer.app.renderer.root.findDescendantById("diff-file-row-0")!
1921
- viewer.app.mockInput.pressKey("n")
1922
- await viewer.app.flush()
1923
- expect(viewer.app.renderer.root.findDescendantById("diff-file-row-0")).toBe(row)
1924
- expect(scroll.scrollTop).toBeGreaterThan(0)
1925
- expect(viewer.app.captureCharFrame()).toContain("file01.txt")
1926
- viewer.app.mockInput.pressKey("p")
1927
- await viewer.app.flush()
1928
- expect(viewer.app.renderer.root.findDescendantById("diff-file-row-0")).toBe(row)
1929
- expect(scroll.scrollTop).toBe(0)
1930
- viewer.app.mockInput.pressKey("G")
1931
- await viewer.app.renderOnce()
1932
- expect(scroll.scrollTop).toBe(scroll.scrollHeight - scroll.viewport.height)
1933
- viewer.app.mockInput.pressKey("g")
1934
- viewer.app.mockInput.pressKey("g")
1935
- await viewer.app.renderOnce()
1936
- expect(scroll.scrollTop).toBe(0)
1937
- viewer.app.mockInput.pressTab()
1938
- viewer.app.mockInput.pressKey("G")
1939
- await viewer.app.renderOnce()
1940
- expect(scroll.scrollTop).toBe(scroll.scrollHeight - scroll.viewport.height)
1941
- expect(findScrollBox(viewer.app.renderer.root, false)!.scrollTop).toBe(0)
1942
- viewer.app.mockInput.pressKey("g")
1943
- viewer.app.mockInput.pressKey("g")
1944
- viewer.app.mockInput.pressKey("m")
1945
- await viewer.app.waitForFrame((frame) => /file00\.txt\s+✓/.test(frame))
1946
- } finally {
1947
- viewer.app.renderer.destroy()
1948
- }
1949
- })
1950
-
1951
- async function renderDiffViewer(
1952
- vcsDiff: unknown[],
1953
- options: {
1954
- width?: number
1955
- height?: number
1956
- initialRoute?: Route
1957
- fail?: boolean
1958
- mode?: "dark" | "light"
1959
- single?: boolean
1960
- readImage?: (file: string, signal: AbortSignal) => Promise<Uint8Array>
1961
- onSessionTab?: () => void
1962
- keybinds?: TuiKeybind.KeybindOverrides
1963
- kittyKeyboard?: boolean
1964
- source?: "branch" | "committed" | "working" | "turn"
1965
- base?: typeof baseFixture | null
1966
- open?: boolean
1967
- pending?: boolean
1968
- baseResponse?: () => Promise<Response>
1969
- diffResponse?: (url: URL) => Promise<Response>
1970
- branchesResponse?: (url: URL) => Promise<Response>
1971
- state?: string
1972
- } = {},
1973
- ) {
1974
- const commands = new Map<string, KeymapCommand>()
1975
- const [current, setCurrent] = createSignal<Route>(options.initialRoute ?? startRoute)
1976
- const [sessionLocation, setSessionLocation] = createSignal(session.location)
1977
- const [branch, setBranch] = createSignal("feature")
1978
- const [sessionStatus, setSessionStatus] = createSignal<"idle" | "running">("idle")
1979
- const state = options.state ?? path.join(temporary.path, crypto.randomUUID())
1980
- let renderDiff: Page["render"] | undefined
1981
- let renderCommands: SlotClaim<"app">["render"] | undefined
1982
- let vcsDiffInput: unknown
1983
- let imageReadInput: unknown
1984
- let shortcut: (command: string) => string | undefined = () => undefined
1985
- const stored: { info: Info } = {
1986
- info: { keybinds: options.keybinds, diffs: { source: options.source, single: options.single } },
1987
- }
1988
- const writes: Info[] = []
1989
- const baseRequests: URL[] = []
1990
- const diffRequests: URL[] = []
1991
- const turnDiffRequests: URL[] = []
1992
- const branchesRequests: URL[] = []
1993
- const mutationRequests: URL[] = []
1994
- const config = createTuiResolvedConfig(stored.info)
1995
- const transport = createFetch(async (url, request) => {
1996
- if (request.method !== "GET") mutationRequests.push(url)
1997
- if (url.pathname.startsWith("/api/fs/read/")) {
1998
- const file = decodeURIComponent(url.pathname.slice("/api/fs/read/".length))
1999
- imageReadInput = {
2000
- path: file,
2001
- location: { directory: url.searchParams.get("location[directory]") },
2002
- }
2003
- return new Response(
2004
- options.readImage ? (await options.readImage(file, request.signal)).slice() : diffImageFixture,
2005
- )
2006
- }
2007
- if (url.pathname === "/api/vcs/base") {
2008
- baseRequests.push(url)
2009
- return options.baseResponse
2010
- ? options.baseResponse()
2011
- : json({ location: session.location, data: options.base === undefined ? baseFixture : options.base })
2012
- }
2013
- if (url.pathname === "/api/vcs/branch") {
2014
- branchesRequests.push(url)
2015
- return options.branchesResponse
2016
- ? options.branchesResponse(url)
2017
- : json({
2018
- location: session.location,
2019
- data: ["v2", "release", "origin/release"].filter((name) =>
2020
- name.includes(url.searchParams.get("search") ?? ""),
2021
- ),
2022
- })
2023
- }
2024
- if (url.pathname === "/api/session/session-1/diff") {
2025
- turnDiffRequests.push(url)
2026
- if (options.fail) return json({ message: "boom" }, { status: 500 })
2027
- return json({ data: vcsDiff })
2028
- }
2029
- if (url.pathname !== "/api/vcs/diff") return
2030
- diffRequests.push(url)
2031
- vcsDiffInput = {
2032
- location: { directory: url.searchParams.get("location[directory]") },
2033
- mode: url.searchParams.get("mode"),
2034
- context: url.searchParams.get("context"),
2035
- ...(url.searchParams.has("base") ? { base: url.searchParams.get("base") } : {}),
2036
- }
2037
- if (options.diffResponse) return options.diffResponse(url)
2038
- if (options.fail) return json({ message: "boom" }, { status: 500 })
2039
- return json({
2040
- location: { directory: "/repo/session", project: { id: "project-1", directory: "/repo/session" } },
2041
- data: vcsDiff,
2042
- })
2043
- }, createEventStream())
2044
- function Harness() {
2045
- let theme: ReturnType<ReturnType<typeof useThemes>["currentTokens"]>
2046
- function Content() {
2047
- const dialog = useDialog()
2048
- const keymap = Keymap.use()
2049
- const shortcuts = Keymap.useShortcuts()
2050
- if (options.onSessionTab) {
2051
- Keymap.createLayer(() => ({
2052
- mode: "global",
2053
- commands: ["session.tab.next", "session.tab.previous"].map((id) => ({
2054
- id,
2055
- title: id,
2056
- run: () => options.onSessionTab?.(),
2057
- })),
2058
- }))
2059
- }
2060
- shortcut = shortcuts.get
2061
- theme = useThemes().currentTokens()
2062
- const context = {
2063
- options: {},
2064
- storage: useStorage(),
2065
- client: createApi(transport.fetch),
2066
- data: {
2067
- session: { get: () => ({ ...session, location: sessionLocation() }), status: sessionStatus },
2068
- location: {
2069
- default: () => ({ directory: "/repo/default" }),
2070
- vcs: { info: () => ({ branch: { current: branch() } }) },
2071
- },
2072
- },
2073
- get theme() {
2074
- return theme
2075
- },
2076
- keymap: {
2077
- layer(input: () => KeymapLayer) {
2078
- input().commands?.forEach((command) => {
2079
- if (command.id) commands.set(command.id, command)
2080
- })
2081
- Keymap.createLayer(input)
2082
- },
2083
- dispatch: keymap.dispatch,
2084
- shortcuts: shortcuts.list,
2085
- mode: keymap.mode,
2086
- },
2087
- ui: {
2088
- dialog: createDialogApi(dialog, (render) => render()),
2089
- panel: { open: () => false, close: () => {}, current: () => undefined },
2090
- router: {
2091
- register(page: Page) {
2092
- if (page.name === "diff") renderDiff = page.render
2093
- return () => {}
2094
- },
2095
- navigate(destination: Destination) {
2096
- setCurrent(
2097
- destination.type === "plugin" && !("id" in destination)
2098
- ? { ...destination, id: "opencode.diffs" }
2099
- : destination,
2100
- )
2101
- },
2102
- current,
2103
- },
2104
- slot(claim: SlotClaim<"app">) {
2105
- renderCommands = claim.render
2106
- return () => {}
2107
- },
2108
- },
2109
- } as unknown as Context
2110
-
2111
- void diffViewerPlugin.setup(context)
2112
- const commandView = renderCommands?.({})
2113
- return (
2114
- <>
2115
- {commandView}
2116
- <Show when={current().type === "plugin" ? current() : undefined} keyed>
2117
- {(route) => renderDiff?.({ data: route.type === "plugin" ? route.data : undefined })}
2118
- </Show>
2119
- </>
2120
- )
2121
- }
2122
-
2123
- return (
2124
- <TestTuiContexts paths={{ state }}>
2125
- <TuiAppProvider value={{ name: "test", version: "test", channel: "test" }}>
2126
- <StorageProvider>
2127
- <ConfigProvider
2128
- config={config}
2129
- service={{
2130
- get: async () => stored.info,
2131
- update: async (update) => {
2132
- const next = structuredClone(stored.info)
2133
- update(next)
2134
- stored.info = next
2135
- writes.push(next)
2136
- return next
2137
- },
2138
- }}
2139
- >
2140
- <Keymap.Provider>
2141
- <ToastProvider>
2142
- <ThemeProvider mode={options.mode ?? "dark"} source={emptyThemeSource}>
2143
- <DialogProvider>
2144
- <Content />
2145
- </DialogProvider>
2146
- </ThemeProvider>
2147
- </ToastProvider>
2148
- </Keymap.Provider>
2149
- </ConfigProvider>
2150
- </StorageProvider>
2151
- </TuiAppProvider>
2152
- </TestTuiContexts>
2153
- )
2154
- }
2155
-
2156
- const app = await testRender(() => <Harness />, {
2157
- width: options.width ?? 80,
2158
- height: options.height ?? 20,
2159
- kittyKeyboard: options.kittyKeyboard,
2160
- })
2161
- await app.waitFor(() => commands.has("diff.open"))
2162
- if (options.open !== false && current().type !== "plugin") commands.get("diff.open")!.run()
2163
- if (options.open !== false) await app.waitFor(() => commands.has("diff.close"))
2164
- await app.flush()
2165
- if (!options.pending) await app.waitForFrame((frame) => !frame.includes("Loading diff"))
2166
- return {
2167
- app,
2168
- commands,
2169
- current,
2170
- shortcut: (command: string) => shortcut(command),
2171
- vcsDiffInput: () => vcsDiffInput,
2172
- imageReadInput: () => imageReadInput,
2173
- baseRequests,
2174
- diffRequests,
2175
- turnDiffRequests,
2176
- branchesRequests,
2177
- mutationRequests,
2178
- setSessionLocation,
2179
- setSessionStatus,
2180
- setBranch,
2181
- state,
2182
- writes,
2183
- settings: () => stored.info,
2184
- }
2185
- }
2186
-
2187
- const startRoute: Route = { type: "session", sessionID: "session-1" }
2188
-
2189
- const baseFixture = { name: "v2", ref: "refs/heads/v2", source: "default" }
2190
-
2191
- const disabledDiffKeybinds = {
2192
- "diff.down": "none",
2193
- "diff.up": "none",
2194
- "diff.page.down": "none",
2195
- "diff.page.up": "none",
2196
- "diff.half_page.down": "none",
2197
- "diff.half_page.up": "none",
2198
- "diff.mark_reviewed": "none",
2199
- } satisfies TuiKeybind.KeybindOverrides
2200
-
2201
- const hunkDiff = [
2202
- {
2203
- file: "src/file.txt",
2204
- additions: 3,
2205
- deletions: 3,
2206
- status: "modified",
2207
- patch: `--- a/src/file.txt
2208
- +++ b/src/file.txt
2209
- @@ -1,3 +1,3 @@
2210
- const first = true
2211
- -const oldFirst = true
2212
- +const newFirst = true
2213
- const afterFirst = true
2214
- @@ -20,3 +20,3 @@
2215
- const second = true
2216
- -const oldSecond = true
2217
- +const newSecond = true
2218
- const afterSecond = true
2219
- @@ -40,3 +40,3 @@
2220
- const third = true
2221
- -const oldThird = true
2222
- +const newThird = true
2223
- const afterThird = true`,
2224
- },
2225
- ]
2226
-
2227
- const manyDiffs = Array.from({ length: 40 }, (_, index) => ({
2228
- ...hunkDiff[0],
2229
- file: `file${String(index).padStart(2, "0")}.txt`,
2230
- }))
2231
-
2232
- test.each([80, 160])("virtualizes a large added file at %i columns without losing its end", async (width) => {
2233
- const lines = [
2234
- "+{",
2235
- ...Array.from(
2236
- { length: 7500 },
2237
- (_, index) =>
2238
- `+ "row-${String(index).padStart(4, "0")}": "${"value".repeat(index === 777 ? 2000 : index % 7 === 0 ? 24 : 1)}"${index === 7499 ? "" : ","}`,
2239
- ),
2240
- "+}",
2241
- ]
2242
- const viewer = await renderDiffViewer(
2243
- [
2244
- {
2245
- file: "snapshot.json",
2246
- status: "added",
2247
- additions: lines.length,
2248
- deletions: 0,
2249
- patch: `diff --git a/snapshot.json b/snapshot.json\nnew file mode 100644\n--- /dev/null\n+++ b/snapshot.json\n@@ -0,0 +1,${lines.length} @@\n${lines.join("\n")}`,
2250
- },
2251
- ],
2252
- { width, height: 24 },
2253
- )
2254
- try {
2255
- expect(viewer.app.captureCharFrame()).toContain("row-0000")
2256
- expect(
2257
- findDiffs(viewer.app.renderer.root).reduce((total, node) => total + node.diff.split("\n").length, 0),
2258
- ).toBeLessThan(2000)
2259
- viewer.commands.get("diff.last")!.run()
2260
- await viewer.app.flush()
2261
- if (!viewer.app.captureCharFrame().includes("row-7499")) {
2262
- await viewer.app.waitForFrame((frame) => frame.includes("row-7499"))
2263
- }
2264
- expect(viewer.app.captureCharFrame()).toContain("row-7499")
2265
- expect(
2266
- findDiffs(viewer.app.renderer.root).reduce((total, node) => total + node.diff.split("\n").length, 0),
2267
- ).toBeLessThan(2000)
2268
- viewer.commands.get("diff.first")!.run()
2269
- await viewer.app.flush()
2270
- expect(viewer.app.captureCharFrame()).toContain("row-0000")
2271
- viewer.app.resize(width === 80 ? 160 : 80, 20)
2272
- await viewer.app.flush()
2273
- viewer.commands.get("diff.last")!.run()
2274
- await viewer.app.flush()
2275
- expect(viewer.app.captureCharFrame()).toContain("row-7499")
2276
- } finally {
2277
- viewer.app.renderer.destroy()
2278
- }
2279
- })
2280
-
2281
- test("keeps the line-number gutter the same width across virtual chunks", async () => {
2282
- const additions = Array.from({ length: 10500 }, (_, index) => `+line-${String(index + 1).padStart(5, "0")}`)
2283
- const viewer = await renderDiffViewer(
2284
- [
2285
- {
2286
- file: "wide.txt",
2287
- status: "added",
2288
- additions: additions.length,
2289
- deletions: 0,
2290
- patch: `--- /dev/null\n+++ b/wide.txt\n@@ -0,0 +1,${additions.length} @@\n${additions.join("\n")}`,
2291
- },
2292
- ],
2293
- { width: 120, height: 24 },
2294
- )
2295
- const column = (text: string) =>
2296
- viewer.app
2297
- .captureCharFrame()
2298
- .split("\n")
2299
- .find((line) => line.includes(text))
2300
- ?.indexOf(text)
2301
- try {
2302
- await viewer.app.flush()
2303
- const top = column("line-00001")
2304
- viewer.commands.get("diff.last")!.run()
2305
- await viewer.app.flush()
2306
- if (!viewer.app.captureCharFrame().includes("line-10500")) {
2307
- await viewer.app.waitForFrame((frame) => frame.includes("line-10500"))
2308
- }
2309
- expect(top).toBeDefined()
2310
- expect(column("line-10500")).toBe(top)
2311
- } finally {
2312
- viewer.app.renderer.destroy()
2313
- }
2314
- })
2315
-
2316
- test("highlights virtual chunks with whole-file syntax context", async () => {
2317
- const additions = Array.from({ length: 3200 }, (_, index) => {
2318
- if (index === 370) return "+/*"
2319
- if (index === 399) return "+*/"
2320
- if (index > 370 && index < 399) return `+ comment ${index}`
2321
- return `+const value${index} = ${index}`
2322
- })
2323
- const viewer = await renderDiffViewer(
2324
- [
2325
- {
2326
- file: "big.ts",
2327
- status: "added",
2328
- additions: additions.length,
2329
- deletions: 0,
2330
- patch: `--- /dev/null\n+++ b/big.ts\n@@ -0,0 +1,${additions.length} @@\n${additions.join("\n")}`,
2331
- },
2332
- ],
2333
- { width: 120, height: 40 },
2334
- )
2335
- const color = (text: string) =>
2336
- viewer.app
2337
- .captureSpans()
2338
- .lines.flatMap((line) => line.spans)
2339
- .find((span) => span.text.includes(text))?.fg
2340
- try {
2341
- findScrollBox(viewer.app.renderer.root)!.scrollTo(360)
2342
- await viewer.app.flush()
2343
- // The comment starts in the first chunk and ends in the second; wait until it is highlighted.
2344
- for (let attempt = 0; attempt < 100 && `${color("comment 375")}` === `${color("value365")}`; attempt++) {
2345
- await Bun.sleep(20)
2346
- await viewer.app.flush()
2347
- }
2348
- expect(`${color("comment 375")}`).not.toBe(`${color("value365")}`)
2349
- for (let attempt = 0; attempt < 100 && `${color("comment 390")}` !== `${color("comment 375")}`; attempt++) {
2350
- await Bun.sleep(20)
2351
- await viewer.app.flush()
2352
- }
2353
- expect(`${color("comment 390")}`).toBe(`${color("comment 375")}`)
2354
- } finally {
2355
- viewer.app.renderer.destroy()
2356
- }
2357
- })
2358
-
2359
- test("does not virtualize added files at or below the size threshold", async () => {
2360
- const additions = Array.from({ length: 3000 }, (_, index) => `+small line ${index}`)
2361
- const viewer = await renderDiffViewer(
2362
- [
2363
- {
2364
- file: "small.txt",
2365
- status: "added",
2366
- additions: additions.length,
2367
- deletions: 0,
2368
- patch: `--- /dev/null\n+++ b/small.txt\n@@ -0,0 +1,${additions.length} @@\n${additions.join("\n")}`,
2369
- },
2370
- ],
2371
- { width: 120, height: 24 },
2372
- )
2373
- try {
2374
- expect(findDiffs(viewer.app.renderer.root)).toHaveLength(1)
2375
- } finally {
2376
- viewer.app.renderer.destroy()
2377
- }
2378
- })
2379
-
2380
- test("file navigation and review still work after a virtualized patch", async () => {
2381
- const additions = Array.from({ length: 6600 }, (_, index) => `+added line ${index}`)
2382
- const viewer = await renderDiffViewer(
2383
- [
2384
- {
2385
- file: "a-large.txt",
2386
- status: "added",
2387
- additions: additions.length,
2388
- deletions: 0,
2389
- patch: `--- /dev/null\n+++ b/a-large.txt\n@@ -0,0 +1,${additions.length} @@\n${additions.join("\n")}`,
2390
- },
2391
- { ...hunkDiff[0], file: "b-small.txt" },
2392
- ],
2393
- { width: 160, height: 24 },
2394
- )
2395
- try {
2396
- const scroll = findScrollBox(viewer.app.renderer.root)!
2397
- scroll.scrollTo(2700)
2398
- await viewer.app.flush()
2399
- viewer.commands.get("diff.previous_hunk")!.run()
2400
- await viewer.app.flush()
2401
- expect(viewer.app.captureCharFrame()).toContain("added line 0")
2402
- viewer.commands.get("diff.next_hunk")!.run()
2403
- await viewer.app.flush()
2404
- expect(viewer.app.captureCharFrame()).toContain("b-small.txt")
2405
- viewer.commands.get("diff.next_file")!.run()
2406
- await viewer.app.flush()
2407
- expect(viewer.app.captureCharFrame()).toContain("b-small.txt")
2408
- expect(viewer.app.captureCharFrame()).toContain("const first")
2409
- viewer.commands.get("diff.previous_file")!.run()
2410
- await viewer.app.flush()
2411
- expect(viewer.app.captureCharFrame()).toContain("a-large.txt")
2412
- viewer.commands.get("diff.mark_reviewed")!.run()
2413
- await viewer.app.flush()
2414
- expect(viewer.app.captureCharFrame()).not.toContain("added line 0")
2415
- viewer.commands.get("diff.mark_reviewed")!.run()
2416
- await viewer.app.flush()
2417
- expect(viewer.app.captureCharFrame()).toContain("added line 0")
2418
- } finally {
2419
- viewer.app.renderer.destroy()
2420
- }
2421
- })
2422
-
2423
- function findScrollBox(root: Renderable, patches = true): ScrollBoxRenderable | undefined {
2424
- const node = root.findDescendantById(patches ? "diff-patches" : "diff-files")
2425
- return node instanceof ScrollBoxRenderable ? node : undefined
2426
- }
2427
-
2428
- function findDiffs(root: Renderable): DiffRenderable[] {
2429
- return root instanceof DiffRenderable ? [root] : root.getChildren().flatMap(findDiffs)
2430
- }
2431
-
2432
- const session = {
2433
- id: "session-1",
2434
- projectID: "project-1",
2435
- location: { directory: "/repo/session" },
2436
- title: "Session",
2437
- cost: { currency: "USD", amount: 0 },
2438
- tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
2439
- time: {
2440
- created: 0,
2441
- updated: 0,
2442
- },
2443
- }
2444
-
2445
- test.each([100, 160])("the sidebar source picker switches VCS sources at %i columns", async (width) => {
2446
- const viewer = await renderDiffViewer(hunkDiff, {
2447
- width,
2448
- kittyKeyboard: true,
2449
- initialRoute: {
2450
- type: "plugin",
2451
- id: "opencode.diffs",
2452
- name: "diff",
2453
- data: { mode: "branch", sessionID: "session-1", returnRoute: startRoute },
2454
- },
2455
- })
2456
- try {
2457
- expect(viewer.current()).toEqual({
2458
- type: "plugin",
2459
- id: "opencode.diffs",
2460
- name: "diff",
2461
- data: { mode: "branch", sessionID: "session-1", returnRoute: startRoute },
2462
- })
2463
- expect(viewer.vcsDiffInput()).toEqual({
2464
- location: { directory: "/repo/session" },
2465
- mode: "branch",
2466
- base: "refs/heads/v2",
2467
- context: "12",
2468
- })
2469
- await viewer.app.waitForFrame((frame) => frame.includes("const first"))
2470
- await viewer.app.flush()
2471
- const source = () => viewer.app.renderer.root.findDescendantById("diff-source-switch")!
2472
- expect(source().y).toBe(1)
2473
- expect(viewer.app.captureCharFrame().split("\n")[1]).toContain("All")
2474
- await viewer.app.mockMouse.click(source().x, source().y, MouseButton.RIGHT)
2475
- await viewer.app.flush()
2476
- expect(viewer.app.captureCharFrame()).not.toContain("Diff source")
2477
- await viewer.app.mockMouse.click(source().x, source().y)
2478
- await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
2479
- expect(viewer.app.renderer.getSelection()).toBeNull()
2480
- viewer.app.mockInput.pressKey("HOME")
2481
- viewer.app.mockInput.pressArrow("down")
2482
- viewer.app.mockInput.pressArrow("down")
2483
- await viewer.app.flush()
2484
- viewer.app.mockInput.pressEnter()
2485
- await viewer.app.waitForFrame(
2486
- (frame) => !frame.includes("Diff source") && frame.includes("Uncommitted") && frame.includes("const first"),
2487
- )
2488
- expect(viewer.vcsDiffInput()).toEqual({ location: { directory: "/repo/session" }, mode: "working", context: "12" })
2489
- // The header resized with the new label; render once more so the click hits its new position.
2490
- await viewer.app.flush()
2491
- await viewer.app.mockMouse.click(source().x, source().y)
2492
- await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
2493
- viewer.app.mockInput.pressKey("HOME")
2494
- await viewer.app.flush()
2495
- viewer.app.mockInput.pressEnter()
2496
- await viewer.app.waitForFrame(
2497
- (frame) => !frame.includes("Diff source") && frame.includes("All") && frame.includes("const first"),
2498
- )
2499
- expect(viewer.vcsDiffInput()).toEqual({
2500
- location: { directory: "/repo/session" },
2501
- mode: "branch",
2502
- context: "12",
2503
- base: "refs/heads/v2",
2504
- })
2505
- expect(viewer.baseRequests).toHaveLength(1)
2506
- expect(viewer.writes).toHaveLength(0)
2507
- } finally {
2508
- viewer.app.renderer.destroy()
2509
- }
2510
- })
1
+ /** @jsxImportSource @opentui/solid */
2
+ import { afterAll, expect, test } from "bun:test"
3
+ import { once } from "node:events"
4
+ import { readdir } from "node:fs/promises"
5
+ import path from "node:path"
6
+ import {
7
+ BoxRenderable,
8
+ CliRenderEvents,
9
+ DiffRenderable,
10
+ ImageRenderable,
11
+ InputRenderable,
12
+ MouseButton,
13
+ type Renderable,
14
+ ScrollBoxRenderable,
15
+ } from "@opentui/core"
16
+ import { testRender } from "@opentui/solid"
17
+ import type {
18
+ Context,
19
+ Destination,
20
+ KeymapCommand,
21
+ KeymapLayer,
22
+ Page,
23
+ SlotClaim,
24
+ Route,
25
+ } from "@zyro-sec/plugin/tui/context"
26
+ import { ThemeProvider, useThemes } from "../../../src/context/theme"
27
+ import { emptyThemeSource, tmpdir } from "../../fixture/fixture"
28
+ import { StorageProvider, useStorage } from "../../../src/context/storage"
29
+ import { TuiAppProvider } from "../../../src/context/runtime"
30
+ import { ConfigProvider, type Info } from "../../../src/config"
31
+ import type { TuiKeybind } from "../../../src/config/keybind"
32
+ import { Keymap } from "../../../src/context/keymap"
33
+ import diffViewerPlugin from "../../../src/feature-plugins/system/diff-viewer"
34
+ import { createTuiResolvedConfig } from "../../fixture/tui-runtime"
35
+ import { TestTuiContexts } from "../../fixture/tui-environment"
36
+ import { createApi, createEventStream, createFetch, json } from "../../fixture/tui-client"
37
+ import { DialogProvider, useDialog } from "../../../src/ui/dialog"
38
+ import { createDialogApi } from "../../../src/plugin/api"
39
+ import { ToastProvider } from "../../../src/ui/toast"
40
+ import { createSignal, Show } from "solid-js"
41
+ import { diffImageFixture } from "../../fixture/diff-image"
42
+
43
+ const temporary = await tmpdir()
44
+ afterAll(() => temporary[Symbol.asyncDispose]())
45
+
46
+ test("closing the diff viewer returns to the route it opened from", async () => {
47
+ const viewer = await renderDiffViewer([])
48
+ try {
49
+ expect(viewer.current()).toEqual({
50
+ type: "plugin",
51
+ id: "opencode.diffs",
52
+ name: "diff",
53
+ data: { sessionID: "session-1", returnRoute: startRoute },
54
+ })
55
+ const route = viewer.current()
56
+ expect(route.type === "plugin" ? route.data?.returnRoute : undefined).not.toBe(startRoute)
57
+ expect(viewer.vcsDiffInput()).toEqual({
58
+ location: { directory: "/repo/session" },
59
+ mode: "branch",
60
+ base: "refs/heads/v2",
61
+ context: "12",
62
+ })
63
+
64
+ expect(viewer.commands.has("diff.close")).toBe(true)
65
+ viewer.commands.get("diff.close")!.run()
66
+ expect(viewer.current()).toEqual(startRoute)
67
+ } finally {
68
+ viewer.app.renderer.destroy()
69
+ }
70
+ })
71
+
72
+ test.each([80, 160])("standalone full-screen diff does not expose panel-only toggling at %i columns", async (width) => {
73
+ const viewer = await renderDiffViewer([], {
74
+ width,
75
+ initialRoute: {
76
+ type: "plugin",
77
+ id: "opencode.diffs",
78
+ name: "diff",
79
+ data: { sessionID: "session-1", returnRoute: startRoute, split: true },
80
+ },
81
+ })
82
+ try {
83
+ const command = viewer.commands.get("diff.toggle_fullscreen")
84
+ expect(typeof command?.enabled === "function" ? command.enabled() : command?.enabled).toBe(false)
85
+ const route = viewer.current()
86
+ viewer.app.mockInput.pressKey("f")
87
+ await viewer.app.flush()
88
+ expect(viewer.current()).toEqual(route)
89
+ } finally {
90
+ viewer.app.renderer.destroy()
91
+ }
92
+ })
93
+
94
+ test("ctrl+c closes the diff viewer without exiting the application", async () => {
95
+ const viewer = await renderDiffViewer([])
96
+
97
+ try {
98
+ viewer.app.mockInput.pressKey("c", { ctrl: true })
99
+ await viewer.app.waitFor(() => viewer.current().type !== "plugin")
100
+ expect(viewer.current()).toEqual(startRoute)
101
+ } finally {
102
+ viewer.app.renderer.destroy()
103
+ }
104
+ })
105
+
106
+ test("shows an error instead of an empty diff when loading fails", async () => {
107
+ const viewer = await renderDiffViewer([], { fail: true })
108
+ try {
109
+ await viewer.app.waitForFrame((frame) => frame.includes("Could not load diff"))
110
+ expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
111
+ } finally {
112
+ viewer.app.renderer.destroy()
113
+ }
114
+ })
115
+
116
+ test("uses the active location when opened outside a session", async () => {
117
+ const viewer = await renderDiffViewer([], { initialRoute: { type: "home" } })
118
+ try {
119
+ expect(viewer.vcsDiffInput()).toEqual({
120
+ location: { directory: "/repo/default" },
121
+ mode: "branch",
122
+ base: "refs/heads/v2",
123
+ context: "12",
124
+ })
125
+ } finally {
126
+ viewer.app.renderer.destroy()
127
+ }
128
+ })
129
+
130
+ test("base lookup is lazy until the viewer opens", async () => {
131
+ const viewer = await renderDiffViewer([], { open: false })
132
+ try {
133
+ expect(viewer.baseRequests).toHaveLength(0)
134
+ expect(viewer.diffRequests).toHaveLength(0)
135
+ viewer.commands.get("diff.open")!.run()
136
+ await viewer.app.waitForFrame((frame) => frame.includes("No changes to show"))
137
+ expect(viewer.baseRequests).toHaveLength(1)
138
+ expect(viewer.baseRequests[0].searchParams.get("location[directory]")).toBe("/repo/session")
139
+ expect(viewer.diffRequests).toHaveLength(1)
140
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: "branch", base: "refs/heads/v2" })
141
+ } finally {
142
+ viewer.app.renderer.destroy()
143
+ }
144
+ })
145
+
146
+ test.each([50, 160])("the source chooser selects all three scopes directly at %i columns", async (width) => {
147
+ const files = {
148
+ branch: ["committed.txt", "staged.txt", "unstaged.txt", "untracked.txt"],
149
+ committed: ["committed.txt"],
150
+ working: ["staged.txt", "unstaged.txt", "untracked.txt"],
151
+ }
152
+ const viewer = await renderDiffViewer([], {
153
+ width,
154
+ height: 30,
155
+ diffResponse: async (url) => {
156
+ const mode = url.searchParams.get("mode")
157
+ if (mode !== "branch" && mode !== "committed" && mode !== "working") throw new Error("Unexpected diff mode")
158
+ return json({
159
+ location: session.location,
160
+ data: files[mode].map((file) => ({
161
+ file,
162
+ status: "modified",
163
+ additions: 1,
164
+ deletions: 0,
165
+ })),
166
+ })
167
+ },
168
+ })
169
+ try {
170
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: "branch" })
171
+ for (const choice of [
172
+ { mode: "committed", index: 1, label: "Committed", absent: "untracked.txt" },
173
+ { mode: "working", index: 2, label: "Uncommitted", absent: "committed.txt" },
174
+ { mode: "branch", index: 0, label: "All", absent: "never-present.txt" },
175
+ ] as const) {
176
+ await chooseSource(viewer, choice.index)
177
+ await viewer.app.waitForFrame((frame) => frame.includes(choice.label) && !frame.includes("Loading diff"))
178
+ const frame = viewer.app.captureCharFrame()
179
+ expect(frame).toContain(files[choice.mode][0])
180
+ expect(frame).not.toContain(choice.absent)
181
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: choice.mode })
182
+ expect(viewer.diffRequests.at(-1)!.searchParams.get("base")).toBe(
183
+ choice.mode === "working" ? null : "refs/heads/v2",
184
+ )
185
+ }
186
+ expect(viewer.baseRequests).toHaveLength(1)
187
+ expect(viewer.diffRequests).toHaveLength(4)
188
+ expect(viewer.writes).toHaveLength(0)
189
+ expect(viewer.settings().diffs?.source).toBeUndefined()
190
+ } finally {
191
+ viewer.app.renderer.destroy()
192
+ }
193
+ })
194
+
195
+ test.each(["branch", "committed", "working"] as const)(
196
+ "reopening remembers the last source choice instead of the initial %s setting",
197
+ async (source) => {
198
+ const viewer = await renderDiffViewer(hunkDiff, { source, height: 30 })
199
+ try {
200
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: source })
201
+ expect(viewer.baseRequests).toHaveLength(source === "working" ? 0 : 1)
202
+ await chooseSource(viewer, source === "working" ? 1 : 2)
203
+ await viewer.app.waitForFrame(
204
+ (frame) => frame.includes(source === "working" ? "Committed" : "Uncommitted") && frame.includes("const first"),
205
+ )
206
+ expect(viewer.writes).toHaveLength(0)
207
+ expect(viewer.settings().diffs?.source).toBe(source)
208
+ viewer.commands.get("diff.close")!.run()
209
+ await viewer.app.flush()
210
+ viewer.commands.get("diff.open")!.run()
211
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
212
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: source === "working" ? "committed" : "working" })
213
+ expect(viewer.baseRequests).toHaveLength(source === "working" ? 2 : 1)
214
+ expect(viewer.diffRequests).toHaveLength(3)
215
+ } finally {
216
+ viewer.app.renderer.destroy()
217
+ }
218
+ },
219
+ )
220
+
221
+ test("explicit route source overrides the configured default", async () => {
222
+ const viewer = await renderDiffViewer(hunkDiff, {
223
+ source: "working",
224
+ initialRoute: { type: "plugin", id: "opencode.diffs", name: "diff", data: { mode: "committed" } },
225
+ })
226
+ try {
227
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: "committed", base: "refs/heads/v2" })
228
+ expect(viewer.settings().diffs?.source).toBe("working")
229
+ } finally {
230
+ viewer.app.renderer.destroy()
231
+ }
232
+ })
233
+
234
+ test("the turn source diffs the session's last turn without resolving a base", async () => {
235
+ const viewer = await renderDiffViewer(hunkDiff, { height: 30, kittyKeyboard: true })
236
+ try {
237
+ await chooseSource(viewer, 3)
238
+ await viewer.app.waitForFrame((frame) => frame.includes("Last turn") && frame.includes("const first"))
239
+ expect(viewer.app.captureCharFrame()).not.toContain("Base not reported")
240
+ expect(viewer.turnDiffRequests).toHaveLength(1)
241
+ expect(viewer.turnDiffRequests[0].searchParams.get("context")).toBe("12")
242
+ expect(viewer.turnDiffRequests[0].searchParams.has("from")).toBe(false)
243
+ expect(viewer.diffRequests).toHaveLength(1)
244
+ expect(viewer.baseRequests).toHaveLength(1)
245
+ await chooseSource(viewer, 2)
246
+ await viewer.app.waitForFrame((frame) => frame.includes("Uncommitted · vs HEAD") && frame.includes("const first"))
247
+ expect(viewer.vcsDiffInput()).toEqual({ location: session.location, mode: "working", context: "12" })
248
+ } finally {
249
+ viewer.app.renderer.destroy()
250
+ }
251
+ })
252
+
253
+ test("the turn source refreshes only when the session finishes a turn", async () => {
254
+ const viewer = await renderDiffViewer(hunkDiff, { source: "turn" })
255
+ try {
256
+ expect(viewer.turnDiffRequests).toHaveLength(1)
257
+ viewer.setSessionStatus("running")
258
+ await viewer.app.flush()
259
+ expect(viewer.turnDiffRequests).toHaveLength(1)
260
+ viewer.setSessionStatus("idle")
261
+ await viewer.app.waitFor(() => viewer.turnDiffRequests.length === 2)
262
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
263
+ expect(viewer.diffRequests).toHaveLength(0)
264
+ } finally {
265
+ viewer.app.renderer.destroy()
266
+ }
267
+ })
268
+
269
+ test("an empty or failing turn diff uses the viewer's empty and error states", async () => {
270
+ const empty = await renderDiffViewer([], { source: "turn" })
271
+ try {
272
+ await empty.app.waitForFrame((frame) => frame.includes("No changes to show"))
273
+ expect(empty.baseRequests).toHaveLength(0)
274
+ expect(empty.diffRequests).toHaveLength(0)
275
+ expect(empty.turnDiffRequests).toHaveLength(1)
276
+ } finally {
277
+ empty.app.renderer.destroy()
278
+ }
279
+ const failing = await renderDiffViewer([], { source: "turn", fail: true })
280
+ try {
281
+ await failing.app.waitForFrame((frame) => frame.includes("Could not load diff. Reopen the diff viewer"))
282
+ expect(failing.app.captureCharFrame()).toContain("Last turn · Diff unavailable")
283
+ expect(failing.app.captureCharFrame()).not.toContain("Choose a base branch")
284
+ } finally {
285
+ failing.app.renderer.destroy()
286
+ }
287
+ })
288
+
289
+ test("the turn source is unavailable outside a session and falls back to the branch scope", async () => {
290
+ const viewer = await renderDiffViewer(hunkDiff, { source: "turn", height: 30, initialRoute: { type: "home" } })
291
+ try {
292
+ expect(viewer.vcsDiffInput()).toEqual({
293
+ location: { directory: "/repo/default" },
294
+ mode: "branch",
295
+ base: "refs/heads/v2",
296
+ context: "12",
297
+ })
298
+ expect(viewer.turnDiffRequests).toHaveLength(0)
299
+ viewer.app.mockInput.pressKey("d")
300
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
301
+ expect(viewer.app.captureCharFrame()).not.toContain("Last turn")
302
+ expect(viewer.app.captureCharFrame()).toMatch(/Base\s+v2/)
303
+ } finally {
304
+ viewer.app.renderer.destroy()
305
+ }
306
+ })
307
+
308
+ test.each([50, 80, 160])(
309
+ "keeps scope, base, and review count on one row with a selectable base at %i columns",
310
+ async (width) => {
311
+ const viewer = await renderDiffViewer(hunkDiff, {
312
+ width,
313
+ height: 30,
314
+ kittyKeyboard: true,
315
+ base: {
316
+ name: "release",
317
+ ref: "refs/remotes/origin/release",
318
+ source: "reflog",
319
+ },
320
+ })
321
+ try {
322
+ const header = viewer.app.renderer.root.findDescendantById("diff-source-header")!
323
+ const source = viewer.app.renderer.root.findDescendantById("diff-source-switch")!
324
+ const count = viewer.app.renderer.root.findDescendantById("diff-review-count")!
325
+ // Full screen keeps the header on its top row, compact or not.
326
+ expect(header.height).toBe(1)
327
+ expect(source.y).toBe(header.y)
328
+ expect(count.y).toBe(header.y)
329
+ expect(viewer.app.captureCharFrame().split("\n")[header.y]).toMatch(/All · vs release\s+0\/1/)
330
+ expect(viewer.app.captureCharFrame()).not.toContain("reviewed")
331
+ expect(viewer.vcsDiffInput()).toMatchObject({ base: "refs/remotes/origin/release" })
332
+ await viewer.app.mockMouse.click(source.x, source.y)
333
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
334
+ expect(viewer.app.captureCharFrame()).toMatch(/Base\s+release/)
335
+ const rows = viewer.app.captureCharFrame().split("\n")
336
+ const first = rows.findIndex((row) => row.includes("Branch + local changes"))
337
+ expect(rows[first]).toMatch(/All\s+Branch \+ local changes/)
338
+ expect(rows[first + 1]).toMatch(/Committed\s+Branch commits only/)
339
+ expect(rows[first + 2]).toMatch(/Uncommitted\s+Local changes only/)
340
+ expect(rows[first + 3]).toMatch(/Last turn\s+Latest session turn/)
341
+ expect(rows[first + 4]).toMatch(/Base\s+release/)
342
+ expect(rows[first + 1].indexOf("Branch commits only")).toBe(rows[first].indexOf("Branch + local changes"))
343
+ expect(rows[first + 2].indexOf("Local changes only")).toBe(rows[first].indexOf("Branch + local changes"))
344
+ expect(rows[first + 3].indexOf("Latest session turn")).toBe(rows[first].indexOf("Branch + local changes"))
345
+ viewer.app.mockInput.pressEscape()
346
+ await viewer.app.waitForFrame((frame) => !frame.includes("Diff source"))
347
+ viewer.commands.get("diff.mark_reviewed")!.run()
348
+ await viewer.app.flush()
349
+ expect(viewer.app.captureCharFrame().split("\n")[header.y]).toMatch(/All · vs release\s+1\/1/)
350
+ await chooseSource(viewer, 2)
351
+ await viewer.app.waitForFrame((frame) => frame.includes("Uncommitted · vs HEAD"))
352
+ viewer.app.mockInput.pressKey("d")
353
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
354
+ expect(viewer.app.captureCharFrame()).toMatch(/Base\s+release/)
355
+ expect(viewer.app.captureCharFrame()).not.toMatch(/Base\s+HEAD/)
356
+ expect(viewer.baseRequests).toHaveLength(1)
357
+ } finally {
358
+ viewer.app.renderer.destroy()
359
+ }
360
+ },
361
+ )
362
+
363
+ test.each([50, 80, 100, 160])(
364
+ "long comparison bases cannot hide the source or review count at %i columns",
365
+ async (width) => {
366
+ const viewer = await renderDiffViewer(hunkDiff, {
367
+ width,
368
+ source: "committed",
369
+ base: { ...baseFixture, name: "release/very-long-comparison-branch-name-that-does-not-fit-in-the-header" },
370
+ })
371
+ try {
372
+ const header = viewer.app.renderer.root.findDescendantById("diff-source-header")!
373
+ const count = viewer.app.renderer.root.findDescendantById("diff-review-count")!
374
+ const y = header.y + header.height - 1
375
+ const row = viewer.app.captureCharFrame().split("\n")[y]
376
+ expect(header.height).toBe(1)
377
+ expect(row).toContain("Committed · vs")
378
+ expect(row).toContain("0/1")
379
+ expect(row).not.toContain("release/very-long-comparison-branch-name-that-does-not-fit-in-the-header")
380
+ expect(count.y).toBe(y)
381
+ expect(count.x + count.width).toBeLessThanOrEqual(header.x + header.width)
382
+ const hint = viewer.app.renderer.root.findDescendantById("diff-help-shortcut")!
383
+ const scroll = findScrollBox(viewer.app.renderer.root)!
384
+ if (width < 88) {
385
+ // The compact layout moves help from the header row into its footer.
386
+ expect(hint.y).toBe(viewer.app.renderer.root.findDescendantById("diff-footer")!.y)
387
+ expect(hint.y).toBeGreaterThanOrEqual(scroll.viewport.y + scroll.viewport.height)
388
+ expect(scroll.viewport.y).toBeGreaterThan(y)
389
+ }
390
+ } finally {
391
+ viewer.app.renderer.destroy()
392
+ }
393
+ },
394
+ )
395
+
396
+ test("opening the source chooser from initial Uncommitted does not resolve a branch base", async () => {
397
+ const viewer = await renderDiffViewer(hunkDiff, { source: "working", height: 30 })
398
+ try {
399
+ viewer.app.mockInput.pressKey("d")
400
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
401
+ expect(viewer.app.captureCharFrame()).toContain("Uncommitted · vs HEAD")
402
+ expect(viewer.app.captureCharFrame()).toMatch(/Base\s+Choose…/)
403
+ expect(viewer.baseRequests).toHaveLength(0)
404
+ expect(viewer.diffRequests).toHaveLength(1)
405
+ } finally {
406
+ viewer.app.renderer.destroy()
407
+ }
408
+ })
409
+
410
+ test.each(["branch", "committed", "working"] as const)(
411
+ "choosing a base remembers it and refreshes only the affected %s comparison",
412
+ async (source) => {
413
+ const viewer = await renderDiffViewer(hunkDiff, { source, height: 30, kittyKeyboard: true })
414
+ try {
415
+ viewer.commands.get("diff.mark_reviewed")!.run()
416
+ await viewer.app.flush()
417
+ expect(viewer.app.captureCharFrame()).toContain("1/1")
418
+ await chooseSource(viewer, 4)
419
+ await viewer.app.waitForFrame((frame) => frame.includes("Base branch") && frame.includes("origin/release"))
420
+ expect(viewer.app.captureCharFrame()).toMatch(/●\s+v2/)
421
+ expect(viewer.branchesRequests[0].searchParams.get("location[directory]")).toBe("/repo/session")
422
+ expect(viewer.branchesRequests[0].searchParams.get("limit")).toBe("100")
423
+ // The picker can paint before its deferred input focus.
424
+ if (!viewer.app.renderer.currentFocusedEditor) await once(viewer.app.renderer, CliRenderEvents.FOCUSED_EDITOR)
425
+ expect(viewer.app.renderer.currentFocusedEditor).toBeInstanceOf(InputRenderable)
426
+ await viewer.app.mockInput.typeText("origin/release")
427
+ await Bun.sleep(160)
428
+ await viewer.app.waitFor(() => viewer.branchesRequests.at(-1)?.searchParams.get("search") === "origin/release")
429
+ await viewer.app.waitForFrame((frame) => frame.includes("origin/release") && !frame.includes("Loading branches"))
430
+ viewer.app.mockInput.pressEnter()
431
+ await viewer.app.waitForFrame((frame) => !frame.includes("Base branch") && frame.includes("src/file.txt"))
432
+ expect(viewer.mutationRequests).toHaveLength(0)
433
+ expect(viewer.vcsDiffInput()).toEqual({
434
+ location: session.location,
435
+ mode: source,
436
+ context: "12",
437
+ ...(source === "working" ? {} : { base: "origin/release" }),
438
+ })
439
+ expect(viewer.diffRequests).toHaveLength(source === "working" ? 1 : 2)
440
+ expect(viewer.app.captureCharFrame()).toContain(source === "working" ? "1/1" : "0/1")
441
+ expect(viewer.writes).toHaveLength(0)
442
+ if (source !== "working") viewer.commands.get("diff.mark_reviewed")!.run()
443
+ await viewer.app.flush()
444
+ expect(viewer.app.captureCharFrame()).toContain("1/1")
445
+ viewer.commands.get("diff.close")!.run()
446
+ await viewer.app.flush()
447
+ viewer.commands.get("diff.open")!.run()
448
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
449
+ expect(viewer.app.captureCharFrame()).toContain("0/1")
450
+ expect(viewer.diffRequests).toHaveLength(source === "working" ? 2 : 3)
451
+ if (source !== "working") expect(viewer.vcsDiffInput()).toMatchObject({ base: "origin/release" })
452
+ await chooseSource(viewer, 4)
453
+ await viewer.app.waitForFrame((frame) => /●\s+origin\/release/.test(frame))
454
+ expect(viewer.baseRequests).toHaveLength(1)
455
+ } finally {
456
+ viewer.app.renderer.destroy()
457
+ }
458
+ },
459
+ )
460
+
461
+ test.each(["branch", "committed"] as const)("an ambiguous base never requests a guessed %s diff", async (source) => {
462
+ const viewer = await renderDiffViewer([], {
463
+ source,
464
+ height: 30,
465
+ kittyKeyboard: true,
466
+ baseResponse: async () => json({ message: "choose a base" }, { status: 503 }),
467
+ diffResponse: async (url) =>
468
+ url.searchParams.get("base")
469
+ ? json({ location: session.location, data: hunkDiff })
470
+ : json({ message: "choose a base" }, { status: 503 }),
471
+ })
472
+ try {
473
+ expect(viewer.app.captureCharFrame()).toContain("Choose a base branch")
474
+ expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
475
+ expect(viewer.diffRequests).toHaveLength(0)
476
+ await chooseSource(viewer, 4)
477
+ await viewer.app.waitForFrame((frame) => frame.includes("Base branch") && frame.includes("origin/release"))
478
+ viewer.app.mockInput.pressKey("HOME")
479
+ viewer.app.mockInput.pressArrow("down")
480
+ viewer.app.mockInput.pressEnter()
481
+ await viewer.app.waitForFrame((frame) => frame.includes("vs release") && frame.includes("const first"))
482
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: source, base: "release" })
483
+ expect(viewer.settings().diffs?.source).toBe(source)
484
+ expect(viewer.baseRequests).toHaveLength(1)
485
+ } finally {
486
+ viewer.app.renderer.destroy()
487
+ }
488
+ })
489
+
490
+ test("base and scope choices survive reopening but not a new TUI instance", async () => {
491
+ const viewer = await renderDiffViewer(hunkDiff, { source: "working", height: 30, kittyKeyboard: true })
492
+ try {
493
+ await chooseSource(viewer, 4)
494
+ await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
495
+ viewer.app.mockInput.pressArrow("down")
496
+ viewer.app.mockInput.pressEnter()
497
+ await viewer.app.waitForFrame((frame) => !frame.includes("Base branch"))
498
+ await chooseSource(viewer, 1)
499
+ await viewer.app.waitForFrame((frame) => frame.includes("Committed · vs release") && frame.includes("const first"))
500
+ viewer.commands.get("diff.close")!.run()
501
+ await viewer.app.flush()
502
+ viewer.commands.get("diff.open")!.run()
503
+ await viewer.app.waitForFrame((frame) => frame.includes("Committed · vs release") && frame.includes("const first"))
504
+ expect(viewer.writes).toHaveLength(0)
505
+ expect(viewer.mutationRequests).toHaveLength(0)
506
+ expect(await readdir(path.join(viewer.state, "test", "tui"))).toEqual([])
507
+ } finally {
508
+ viewer.app.renderer.destroy()
509
+ }
510
+
511
+ const restarted = await renderDiffViewer(hunkDiff, { state: viewer.state, source: "working", height: 30 })
512
+ try {
513
+ expect(restarted.vcsDiffInput()).toMatchObject({ mode: "working" })
514
+ await chooseSource(restarted, 1)
515
+ await restarted.app.waitForFrame((frame) => frame.includes("Committed · vs v2") && frame.includes("const first"))
516
+ expect(restarted.vcsDiffInput()).toMatchObject({ base: "refs/heads/v2" })
517
+ expect(restarted.writes).toHaveLength(0)
518
+ } finally {
519
+ restarted.app.renderer.destroy()
520
+ }
521
+ })
522
+
523
+ test("base choices are isolated by branch within the same location", async () => {
524
+ const viewer = await renderDiffViewer(hunkDiff, { height: 30, kittyKeyboard: true })
525
+ try {
526
+ await chooseSource(viewer, 4)
527
+ await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
528
+ viewer.app.mockInput.pressArrow("down")
529
+ viewer.app.mockInput.pressEnter()
530
+ await viewer.app.waitForFrame((frame) => frame.includes("All · vs release") && frame.includes("const first"))
531
+ viewer.setBranch("other-feature")
532
+ await viewer.app.waitForFrame((frame) => frame.includes("All · vs v2") && frame.includes("const first"))
533
+ expect(viewer.vcsDiffInput()).toMatchObject({ base: "refs/heads/v2" })
534
+ viewer.setBranch("feature")
535
+ await viewer.app.waitForFrame((frame) => frame.includes("All · vs release") && frame.includes("const first"))
536
+ expect(viewer.vcsDiffInput()).toMatchObject({ base: "release" })
537
+ expect(viewer.baseRequests).toHaveLength(2)
538
+ expect(viewer.mutationRequests).toHaveLength(0)
539
+ } finally {
540
+ viewer.app.renderer.destroy()
541
+ }
542
+ })
543
+
544
+ test("an invalid comparison reports an error and allows another base choice", async () => {
545
+ const viewer = await renderDiffViewer(hunkDiff, {
546
+ height: 30,
547
+ kittyKeyboard: true,
548
+ diffResponse: async (url) =>
549
+ url.searchParams.get("base") === "release"
550
+ ? json({ message: "no common history" }, { status: 503 })
551
+ : json({ location: session.location, data: hunkDiff }),
552
+ })
553
+ try {
554
+ await chooseSource(viewer, 4)
555
+ await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
556
+ viewer.app.mockInput.pressArrow("down")
557
+ viewer.app.mockInput.pressEnter()
558
+ await viewer.app.waitForFrame((frame) => frame.includes("Base or diff unavailable"))
559
+ expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
560
+ await chooseSource(viewer, 4)
561
+ await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
562
+ viewer.app.mockInput.pressKey("HOME")
563
+ viewer.app.mockInput.pressEnter()
564
+ await viewer.app.waitForFrame((frame) => frame.includes("All · vs v2") && frame.includes("const first"))
565
+ expect(viewer.diffRequests).toHaveLength(3)
566
+ expect(viewer.vcsDiffInput()).toMatchObject({ base: "v2" })
567
+ expect(viewer.mutationRequests).toHaveLength(0)
568
+ } finally {
569
+ viewer.app.renderer.destroy()
570
+ }
571
+ })
572
+
573
+ test("base search failures are visible without changing the diff", async () => {
574
+ const viewer = await renderDiffViewer(hunkDiff, {
575
+ height: 30,
576
+ branchesResponse: async () => json({ message: "branches unavailable" }, { status: 503 }),
577
+ })
578
+ try {
579
+ await chooseSource(viewer, 4)
580
+ await viewer.app.waitForFrame((frame) => frame.includes("Could not load branches"))
581
+ expect(viewer.app.captureCharFrame()).toContain("All · vs v2")
582
+ expect(viewer.mutationRequests).toHaveLength(0)
583
+ expect(viewer.diffRequests).toHaveLength(1)
584
+ } finally {
585
+ viewer.app.renderer.destroy()
586
+ }
587
+ })
588
+
589
+ test("a late base lookup cannot overwrite an in-memory base choice", async () => {
590
+ const pending = Promise.withResolvers<Response>()
591
+ const viewer = await renderDiffViewer(hunkDiff, {
592
+ height: 30,
593
+ pending: true,
594
+ kittyKeyboard: true,
595
+ baseResponse: () => pending.promise,
596
+ })
597
+ try {
598
+ await chooseSource(viewer, 4)
599
+ await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
600
+ viewer.app.mockInput.pressArrow("down")
601
+ viewer.app.mockInput.pressEnter()
602
+ await viewer.app.waitForFrame((frame) => frame.includes("All · vs release") && frame.includes("const first"))
603
+ pending.resolve(json({ location: session.location, data: baseFixture }))
604
+ await viewer.app.flush()
605
+ expect(viewer.app.captureCharFrame()).toContain("All · vs release")
606
+ expect(viewer.diffRequests).toHaveLength(1)
607
+ expect(viewer.vcsDiffInput()).toMatchObject({ base: "release" })
608
+ expect(viewer.baseRequests).toHaveLength(1)
609
+ await chooseSource(viewer, 1)
610
+ await viewer.app.waitForFrame((frame) => frame.includes("Committed · vs release"))
611
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: "committed", base: "release" })
612
+ viewer.app.mockInput.pressKey("d")
613
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
614
+ expect(viewer.app.captureCharFrame()).toMatch(/Base\s+release/)
615
+ } finally {
616
+ pending.resolve(json({ location: session.location, data: baseFixture }))
617
+ viewer.app.renderer.destroy()
618
+ }
619
+ })
620
+
621
+ test("dismissing the base picker leaves the comparison unchanged", async () => {
622
+ const viewer = await renderDiffViewer(hunkDiff, {
623
+ height: 30,
624
+ kittyKeyboard: true,
625
+ })
626
+ try {
627
+ await chooseSource(viewer, 4)
628
+ await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
629
+ viewer.app.mockInput.pressArrow("down")
630
+ viewer.app.mockInput.pressEscape()
631
+ await viewer.app.waitForFrame((frame) => !frame.includes("Base branch"))
632
+ viewer.app.mockInput.pressKey("d")
633
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
634
+ expect(viewer.app.captureCharFrame()).toContain("Diff source")
635
+ expect(viewer.app.captureCharFrame()).toContain("All · vs v2")
636
+ expect(viewer.diffRequests).toHaveLength(1)
637
+ } finally {
638
+ viewer.app.renderer.destroy()
639
+ }
640
+ })
641
+
642
+ test("the base picker remembers its captured location without refreshing a moved session", async () => {
643
+ const viewer = await renderDiffViewer(hunkDiff, { height: 30, kittyKeyboard: true })
644
+ try {
645
+ await chooseSource(viewer, 4)
646
+ await viewer.app.waitForFrame((frame) => frame.includes("origin/release"))
647
+ viewer.setSessionLocation({ directory: "/repo/moved" })
648
+ await viewer.app.flush()
649
+ viewer.app.mockInput.pressArrow("down")
650
+ viewer.app.mockInput.pressEnter()
651
+ await viewer.app.waitForFrame((frame) => !frame.includes("Base branch"))
652
+ expect(viewer.mutationRequests).toHaveLength(0)
653
+ expect(viewer.vcsDiffInput()).toMatchObject({ location: { directory: "/repo/moved" }, base: "refs/heads/v2" })
654
+ expect(viewer.diffRequests).toHaveLength(2)
655
+ viewer.setSessionLocation(session.location)
656
+ await viewer.app.waitForFrame((frame) => frame.includes("All · vs release") && frame.includes("const first"))
657
+ expect(viewer.vcsDiffInput()).toMatchObject({ location: session.location, base: "release" })
658
+ } finally {
659
+ viewer.app.renderer.destroy()
660
+ }
661
+ })
662
+
663
+ test("tree, single-file, and layout preferences do not refetch base or diff", async () => {
664
+ const viewer = await renderDiffViewer(hunkDiff, { width: 160 })
665
+ try {
666
+ for (const command of ["diff.toggle_file_tree", "diff.single_patch", "diff.toggle_view"]) {
667
+ viewer.commands.get(command)!.run()
668
+ await viewer.app.flush()
669
+ }
670
+ expect(viewer.writes).toHaveLength(3)
671
+ expect(viewer.settings().diffs).toMatchObject({ tree: false, single: true, view: "unified" })
672
+ expect(viewer.baseRequests).toHaveLength(1)
673
+ expect(viewer.diffRequests).toHaveLength(1)
674
+ } finally {
675
+ viewer.app.renderer.destroy()
676
+ }
677
+ })
678
+
679
+ test("the remapped source chooser preserves its current selection marker", async () => {
680
+ const viewer = await renderDiffViewer(hunkDiff, { height: 30, keybinds: { "diff.switch_source": "x" } })
681
+ try {
682
+ viewer.app.mockInput.pressKey("d")
683
+ await viewer.app.flush()
684
+ expect(viewer.app.captureCharFrame()).not.toContain("Diff source")
685
+ await chooseSource(viewer, 1, "x")
686
+ await viewer.app.waitForFrame((frame) => frame.includes("Committed") && frame.includes("const first"))
687
+ viewer.app.mockInput.pressKey("x")
688
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
689
+ expect(viewer.app.captureCharFrame()).toMatch(/●\s+Committed/)
690
+ expect(viewer.app.captureCharFrame()).toContain("Branch commits only")
691
+ expect(viewer.app.captureCharFrame()).toContain("Local changes only")
692
+ expect(viewer.app.captureCharFrame()).toMatch(/Base\s+v2/)
693
+ expect(viewer.writes).toHaveLength(0)
694
+ } finally {
695
+ viewer.app.renderer.destroy()
696
+ }
697
+ })
698
+
699
+ test("pending and failed scopes never render the previous scope's files", async () => {
700
+ const pending = Promise.withResolvers<Response>()
701
+ const viewer = await renderDiffViewer([], {
702
+ height: 30,
703
+ diffResponse: async (url) =>
704
+ url.searchParams.get("mode") === "committed"
705
+ ? pending.promise
706
+ : json({ location: session.location, data: hunkDiff }),
707
+ })
708
+ try {
709
+ expect(viewer.app.captureCharFrame()).toContain("const first")
710
+ await chooseSource(viewer, 1)
711
+ await viewer.app.waitForFrame((frame) => frame.includes("Loading diff"))
712
+ expect(viewer.app.captureCharFrame()).toContain("Committed")
713
+ expect(viewer.app.captureCharFrame()).not.toContain("src/file.txt")
714
+ expect(findDiffs(viewer.app.renderer.root)).toHaveLength(0)
715
+ pending.resolve(json({ message: "comparison failed" }, { status: 503 }))
716
+ await viewer.app.waitForFrame((frame) => frame.includes("Could not load diff"))
717
+ expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
718
+ expect(viewer.app.captureCharFrame()).not.toContain("src/file.txt")
719
+ await chooseSource(viewer, 2)
720
+ await viewer.app.waitForFrame((frame) => frame.includes("Uncommitted") && frame.includes("const first"))
721
+ expect(viewer.baseRequests).toHaveLength(1)
722
+ } finally {
723
+ pending.resolve(json({ location: session.location, data: [] }))
724
+ viewer.app.renderer.destroy()
725
+ }
726
+ })
727
+
728
+ test("late diff responses cannot replace the selected scope", async () => {
729
+ const pending = Promise.withResolvers<Response>()
730
+ const viewer = await renderDiffViewer([], {
731
+ height: 30,
732
+ diffResponse: async (url) =>
733
+ url.searchParams.get("mode") === "committed"
734
+ ? pending.promise
735
+ : json({ location: session.location, data: [{ ...hunkDiff[0], file: "uncommitted.txt" }] }),
736
+ })
737
+ try {
738
+ await chooseSource(viewer, 1)
739
+ await viewer.app.waitForFrame((frame) => frame.includes("Loading diff"))
740
+ await chooseSource(viewer, 2)
741
+ await viewer.app.waitForFrame((frame) => frame.includes("uncommitted.txt"))
742
+ pending.resolve(json({ location: session.location, data: [{ ...hunkDiff[0], file: "committed.txt" }] }))
743
+ await viewer.app.flush()
744
+ expect(viewer.app.captureCharFrame()).toContain("Uncommitted")
745
+ expect(viewer.app.captureCharFrame()).toContain("uncommitted.txt")
746
+ expect(viewer.app.captureCharFrame()).not.toMatch(/\scommitted\.txt/)
747
+ expect(viewer.baseRequests).toHaveLength(1)
748
+ } finally {
749
+ pending.resolve(json({ location: session.location, data: [] }))
750
+ viewer.app.renderer.destroy()
751
+ }
752
+ })
753
+
754
+ test("switching to uncommitted does not wait for or display a late base result", async () => {
755
+ const pending = Promise.withResolvers<Response>()
756
+ const viewer = await renderDiffViewer(hunkDiff, { height: 30, pending: true, baseResponse: () => pending.promise })
757
+ try {
758
+ expect(viewer.app.captureCharFrame()).toContain("Loading diff")
759
+ expect(viewer.diffRequests).toHaveLength(0)
760
+ await chooseSource(viewer, 2)
761
+ await viewer.app.waitForFrame((frame) => frame.includes("Uncommitted") && frame.includes("const first"))
762
+ pending.resolve(json({ location: session.location, data: baseFixture }))
763
+ await viewer.app.flush()
764
+ expect(viewer.app.captureCharFrame()).toContain("vs HEAD")
765
+ expect(viewer.app.captureCharFrame()).not.toContain("vs v2")
766
+ expect(viewer.diffRequests).toHaveLength(1)
767
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: "working" })
768
+ } finally {
769
+ pending.resolve(json({ location: session.location, data: baseFixture }))
770
+ viewer.app.renderer.destroy()
771
+ }
772
+ })
773
+
774
+ test.each([false, true])(
775
+ "missing base metadata preserves provider branch review (has branch changes: %s)",
776
+ async (changed) => {
777
+ const viewer = await renderDiffViewer([], {
778
+ base: null,
779
+ height: 30,
780
+ diffResponse: async (url) =>
781
+ json({
782
+ location: session.location,
783
+ data:
784
+ url.searchParams.get("mode") === "working"
785
+ ? [{ ...hunkDiff[0], file: "dirty.txt" }]
786
+ : changed
787
+ ? [{ ...hunkDiff[0], file: "branch.txt" }]
788
+ : [],
789
+ }),
790
+ })
791
+ try {
792
+ expect(viewer.app.captureCharFrame()).toContain("All")
793
+ expect(viewer.app.captureCharFrame()).toContain("Base not reported")
794
+ expect(viewer.app.captureCharFrame()).toContain(changed ? "branch.txt" : "No changes to show")
795
+ expect(viewer.app.captureCharFrame()).not.toContain("dirty.txt")
796
+ expect(viewer.app.captureCharFrame()).not.toContain("showing uncommitted")
797
+ expect(viewer.vcsDiffInput()).toEqual({ location: session.location, mode: "branch", context: "12" })
798
+ await chooseSource(viewer, 1)
799
+ await viewer.app.waitForFrame((frame) => frame.includes("Committed comparison unavailable"))
800
+ expect(viewer.app.captureCharFrame()).toContain("Base not reported")
801
+ expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
802
+ expect(viewer.app.captureCharFrame()).not.toContain("branch.txt")
803
+ expect(viewer.diffRequests).toHaveLength(1)
804
+ await chooseSource(viewer, 2)
805
+ await viewer.app.waitForFrame((frame) => frame.includes("dirty.txt"))
806
+ expect(viewer.app.captureCharFrame()).not.toContain("Base not reported")
807
+ expect(viewer.vcsDiffInput()).toEqual({ location: session.location, mode: "working", context: "12" })
808
+ expect(viewer.diffRequests).toHaveLength(2)
809
+ expect(viewer.baseRequests).toHaveLength(1)
810
+ } finally {
811
+ viewer.app.renderer.destroy()
812
+ }
813
+ },
814
+ )
815
+
816
+ test("unresolved branch comparisons report errors without switching to working", async () => {
817
+ const viewer = await renderDiffViewer([], {
818
+ base: null,
819
+ height: 30,
820
+ diffResponse: async (url) =>
821
+ url.searchParams.get("mode") === "branch"
822
+ ? json({ message: "comparison base unavailable" }, { status: 503 })
823
+ : json({ location: session.location, data: hunkDiff }),
824
+ })
825
+ try {
826
+ expect(viewer.app.captureCharFrame()).toContain("Could not load diff")
827
+ expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
828
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: "branch" })
829
+ expect(viewer.diffRequests).toHaveLength(1)
830
+ await chooseSource(viewer, 2)
831
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
832
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: "working" })
833
+ expect(viewer.baseRequests).toHaveLength(1)
834
+ } finally {
835
+ viewer.app.renderer.destroy()
836
+ }
837
+ })
838
+
839
+ test("base lookup failures are errors rather than missing-base fallbacks", async () => {
840
+ const viewer = await renderDiffViewer([], {
841
+ height: 30,
842
+ baseResponse: async () => json({ message: "base failed" }, { status: 503 }),
843
+ })
844
+ try {
845
+ expect(viewer.app.captureCharFrame()).toContain("Could not load diff")
846
+ expect(viewer.app.captureCharFrame()).not.toContain("No changes to show")
847
+ expect(viewer.app.captureCharFrame()).not.toContain("showing uncommitted")
848
+ expect(viewer.diffRequests).toHaveLength(0)
849
+ await chooseSource(viewer, 2)
850
+ await viewer.app.waitForFrame((frame) => frame.includes("No changes to show"))
851
+ expect(viewer.vcsDiffInput()).toMatchObject({ mode: "working" })
852
+ } finally {
853
+ viewer.app.renderer.destroy()
854
+ }
855
+ })
856
+
857
+ test.each([undefined, "Binary files a/assets/preview.png and b/assets/preview.png differ"])(
858
+ "committed images never read the working-tree image (patch: %s)",
859
+ async (patch) => {
860
+ const viewer = await renderDiffViewer(
861
+ [{ file: "assets/preview.png", status: "modified", additions: 0, deletions: 0, patch }],
862
+ { source: "committed" },
863
+ )
864
+ try {
865
+ expect(viewer.app.captureCharFrame()).toContain("Committed image preview unavailable")
866
+ expect(viewer.imageReadInput()).toBeUndefined()
867
+ expect(viewer.app.captureCharFrame()).not.toContain("Working tree preview")
868
+ } finally {
869
+ viewer.app.renderer.destroy()
870
+ }
871
+ },
872
+ )
873
+
874
+ async function chooseSource(viewer: Awaited<ReturnType<typeof renderDiffViewer>>, index: number, key = "d") {
875
+ viewer.app.mockInput.pressKey(key)
876
+ await viewer.app.waitForFrame((frame) => /Diff source\s+esc/.test(frame))
877
+ viewer.app.mockInput.pressKey("HOME")
878
+ await viewer.app.flush()
879
+ for (let i = 0; i < index; i++) {
880
+ viewer.app.mockInput.pressArrow("down")
881
+ await viewer.app.flush()
882
+ }
883
+ viewer.app.mockInput.pressEnter()
884
+ await viewer.app.waitForFrame((frame) => !/Diff source\s+esc/.test(frame))
885
+ }
886
+
887
+ test("brackets navigate diff hunks", async () => {
888
+ const viewer = await renderDiffViewer(hunkDiff, { height: 12 })
889
+ try {
890
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
891
+ await viewer.app.waitFor(() => Boolean(findScrollBox(viewer.app.renderer.root)))
892
+ await viewer.app.flush()
893
+ expect(findDiffs(viewer.app.renderer.root)).toHaveLength(3)
894
+ const scroll = findScrollBox(viewer.app.renderer.root)!
895
+ const initial = scroll.scrollTop
896
+
897
+ viewer.app.mockInput.pressKey("]")
898
+ await viewer.app.renderOnce()
899
+ const first = scroll.scrollTop
900
+ expect(first).toBeGreaterThan(initial)
901
+ expect(findDiffs(viewer.app.renderer.root)[1].y).toBeGreaterThanOrEqual(scroll.viewport.y + 2)
902
+
903
+ viewer.app.mockInput.pressKey("]")
904
+ await viewer.app.renderOnce()
905
+ const second = scroll.scrollTop
906
+ expect(second).toBeGreaterThan(first)
907
+ expect(findDiffs(viewer.app.renderer.root)[2].y).toBeGreaterThanOrEqual(scroll.viewport.y + 2)
908
+
909
+ viewer.app.mockInput.pressKey("[")
910
+ await viewer.app.renderOnce()
911
+ expect(scroll.scrollTop).toBe(first)
912
+
913
+ viewer.app.mockInput.pressKey("]")
914
+ await viewer.app.renderOnce()
915
+ expect(scroll.scrollTop).toBe(second)
916
+
917
+ scroll.scrollTo(initial)
918
+ viewer.app.mockInput.pressKey("]")
919
+ await viewer.app.renderOnce()
920
+ expect(scroll.scrollTop).toBe(first)
921
+ } finally {
922
+ viewer.app.renderer.destroy()
923
+ }
924
+ })
925
+
926
+ test("disabled diff keybinds have no component fallbacks", async () => {
927
+ const viewer = await renderDiffViewer(hunkDiff, {
928
+ height: 12,
929
+ keybinds: disabledDiffKeybinds,
930
+ })
931
+ try {
932
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
933
+ await viewer.app.waitFor(() => Boolean(findScrollBox(viewer.app.renderer.root)))
934
+ await viewer.app.flush()
935
+ const scroll = findScrollBox(viewer.app.renderer.root)!
936
+ const initial = scroll.scrollTop
937
+
938
+ Object.keys(disabledDiffKeybinds).forEach((command) => expect(viewer.shortcut(command)).toBe(""))
939
+
940
+ viewer.app.mockInput.pressKey("j")
941
+ await viewer.app.renderOnce()
942
+
943
+ expect(scroll.scrollTop).toBe(initial)
944
+ } finally {
945
+ viewer.app.renderer.destroy()
946
+ }
947
+ })
948
+
949
+ test.each(["added", "deleted"] as const)("%s files use full width even in split view", async (status) => {
950
+ const viewer = await renderDiffViewer(
951
+ [
952
+ {
953
+ file: "src/new.txt",
954
+ status,
955
+ additions: status === "added" ? 1 : 0,
956
+ deletions: status === "deleted" ? 1 : 0,
957
+ patch:
958
+ status === "added"
959
+ ? "--- /dev/null\n+++ b/src/new.txt\n@@ -0,0 +1 @@\n+full width content\n"
960
+ : "--- a/src/new.txt\n+++ /dev/null\n@@ -1 +0,0 @@\n-full width content\n",
961
+ },
962
+ ],
963
+ { width: 160 },
964
+ )
965
+ try {
966
+ await viewer.app.waitForFrame((frame) => frame.includes("full width content"))
967
+ const diffs = findDiffs(viewer.app.renderer.root)
968
+ expect(diffs).toHaveLength(1)
969
+ expect(diffs[0].view).toBe("unified")
970
+ expect(viewer.app.captureCharFrame()).not.toMatch(/[├└┌┐┬┴─]/)
971
+ } finally {
972
+ viewer.app.renderer.destroy()
973
+ }
974
+ })
975
+
976
+ test.each([80, 160])("adapts navigation and modified patches at %i columns", async (width) => {
977
+ const viewer = await renderDiffViewer(hunkDiff, { width })
978
+ try {
979
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
980
+ expect(findDiffs(viewer.app.renderer.root)[0].view).toBe(width === 80 ? "unified" : "split")
981
+ expect(viewer.app.captureCharFrame()).toContain("0/1")
982
+ } finally {
983
+ viewer.app.renderer.destroy()
984
+ }
985
+ })
986
+
987
+ test("the first file title sits below full-height top padding with a blank row below", async () => {
988
+ const viewer = await renderDiffViewer(hunkDiff)
989
+ try {
990
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
991
+ const lines = viewer.app.captureCharFrame().split("\n")
992
+ const title = lines.findIndex((line) => line.includes("src/file.txt"))
993
+ expect(title).toBeGreaterThan(0)
994
+ const padding = viewer.app.captureSpans().lines[title - 1].spans.find((span) => span.width > 2)!
995
+ expect(lines[title - 1].trim()).toBe("")
996
+ expect(padding.bg).toEqual(
997
+ viewer.app.captureSpans().lines[title].spans.find((span) => span.text.includes("src/"))!.bg,
998
+ )
999
+ expect(lines[title + 1].trim()).toBe("")
1000
+ expect(lines[title + 2]).toContain("const first")
1001
+ expect(lines[title]).toMatch(/\+3\s+-3/)
1002
+ } finally {
1003
+ viewer.app.renderer.destroy()
1004
+ }
1005
+ })
1006
+
1007
+ test.each([100, 160])("shared pane edges align headings and stay fixed at %i columns", async (width) => {
1008
+ const viewer = await renderDiffViewer(manyDiffs.slice(0, 6), { width, height: 40 })
1009
+ try {
1010
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1011
+ await viewer.app.flush()
1012
+ const lines = viewer.app.captureCharFrame().split("\n")
1013
+ const title = lines.findIndex((line) => line.includes("file00.txt"))
1014
+ expect(lines.findIndex((line) => line.includes("All"))).toBe(title)
1015
+ const frame = viewer.app.captureSpans()
1016
+ const caps = frame.lines[title - 1].spans.filter((span) => span.width > 2)
1017
+ expect(caps).toHaveLength(2)
1018
+ caps.forEach((span) => expect(span.text.trim()).toBe(""))
1019
+ expect(caps[0].bg).toEqual(frame.lines[title].spans.find((span) => span.text.includes("All"))!.bg)
1020
+ expect(caps[1].bg).toEqual(frame.lines[title].spans.find((span) => span.text.includes("file00.txt"))!.bg)
1021
+ expect(title).toBe(1)
1022
+ expect(viewer.app.captureCharFrame()).not.toContain("Diff working tree")
1023
+ expect(lines[title + 1].slice(lines[title].indexOf("file00.txt")).trim()).toBe("")
1024
+ expect(lines[title + 2]).toContain("const first")
1025
+
1026
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1027
+ scroll.scrollTo(5)
1028
+ await viewer.app.flush()
1029
+ expect(viewer.app.captureCharFrame().split("\n")[title - 1]).toBe(lines[title - 1])
1030
+ expect(viewer.app.captureCharFrame().split("\n")[title]).toContain("file00.txt")
1031
+
1032
+ viewer.commands.get("diff.next_file")!.run()
1033
+ await viewer.app.flush()
1034
+ expect(viewer.app.captureCharFrame().split("\n")[title - 1]).toBe(lines[title - 1])
1035
+ expect(viewer.app.captureCharFrame().split("\n")[title]).toContain("file01.txt")
1036
+ viewer.commands.get("diff.single_patch")!.run()
1037
+ await viewer.app.flush()
1038
+ expect(viewer.app.captureCharFrame().split("\n")[title]).toContain("file01.txt")
1039
+
1040
+ viewer.app.resize(80, 30)
1041
+ await viewer.app.flush()
1042
+ expect(viewer.app.renderer.root.findDescendantById("diff-tree-top-edge")).toBeUndefined()
1043
+ expect(viewer.app.renderer.root.findDescendantById("diff-patch-top-edge")).toBeDefined()
1044
+ // The compact full screen header is one row, directly above the shared pane edge.
1045
+ expect(viewer.app.captureCharFrame().split("\n")[title + 1]).toContain("file01.txt")
1046
+ } finally {
1047
+ viewer.app.renderer.destroy()
1048
+ }
1049
+ })
1050
+
1051
+ test.each(["dark", "light"] as const)("the pane edge matches the visible reviewed card in %s mode", async (mode) => {
1052
+ const viewer = await renderDiffViewer(
1053
+ Array.from({ length: 4 }, (_, index) => ({ ...hunkDiff[0], file: `src/file${index}.txt` })),
1054
+ { width: 160, height: 18, mode },
1055
+ )
1056
+ try {
1057
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1058
+ await viewer.app.flush()
1059
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1060
+ const expectEdge = (file: string, collapsed = false) => {
1061
+ const frame = viewer.app.captureSpans()
1062
+ // Full-screen cards are inset two columns from each pane edge.
1063
+ const card = scroll.viewport.width - 4
1064
+ const edge = frame.lines[scroll.viewport.y - 1].spans.find((span) => span.width === card)!
1065
+ const page = frame.lines[scroll.viewport.y - 1].spans.at(-1)!.bg
1066
+ const title = frame.lines[scroll.viewport.y].spans.find((span) => span.text.includes(file))!
1067
+ expect(title, viewer.app.captureCharFrame()).toBeDefined()
1068
+ expect(edge.text.trim()).toBe("")
1069
+ expect(edge.bg).toEqual(title.bg)
1070
+ expect(edge.bg).not.toEqual(page)
1071
+ expect(frame.lines[0].spans[0].bg).toEqual(frame.lines[1].spans.find((span) => span.text.includes("All"))!.bg)
1072
+ const bottom = frame.lines[scroll.viewport.y + 1].spans.find(
1073
+ (span) => span.text.includes("▀") && span.width === card,
1074
+ )
1075
+ if (!collapsed) {
1076
+ expect(bottom).toBeUndefined()
1077
+ return
1078
+ }
1079
+ expect(bottom?.text.trim()).toBe("▀".repeat(card))
1080
+ expect(bottom?.fg).toEqual(edge.bg)
1081
+ expect(bottom?.bg).toEqual(page)
1082
+ }
1083
+ expectEdge("file0.txt")
1084
+ viewer.app.mockInput.pressKey("m")
1085
+ await viewer.app.flush()
1086
+ expectEdge("file0.txt", true)
1087
+ viewer.app.mockInput.pressKey("n")
1088
+ await viewer.app.flush()
1089
+ expectEdge("file1.txt")
1090
+ viewer.app.mockInput.pressKey("m")
1091
+ await viewer.app.flush()
1092
+ expectEdge("file1.txt", true)
1093
+ viewer.app.mockInput.pressKey("n")
1094
+ await viewer.app.flush()
1095
+ expectEdge("file2.txt")
1096
+
1097
+ // Scrolling back must follow the visible card, not the selected unreviewed file.
1098
+ scroll.scrollTo(0)
1099
+ await viewer.app.flush()
1100
+ expectEdge("file0.txt", true)
1101
+ const second = viewer.app.renderer.root.findDescendantById("diff-file-header-1")!
1102
+ const frame = viewer.app.captureSpans()
1103
+ // The gap's card segment sits between the two inset segments.
1104
+ expect(frame.lines[second.y - 1].spans.find((span) => span.text.includes("▄") && span.width > 2)!.fg).toEqual(
1105
+ frame.lines[second.y].spans.find((span) => span.text.includes("file1.txt"))!.bg,
1106
+ )
1107
+
1108
+ viewer.commands.get("diff.single_patch")!.run()
1109
+ await viewer.app.flush()
1110
+ expectEdge("file0.txt", true)
1111
+ viewer.app.mockInput.pressKey("m")
1112
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1113
+ await viewer.app.flush()
1114
+ expectEdge("file0.txt")
1115
+ } finally {
1116
+ viewer.app.renderer.destroy()
1117
+ }
1118
+ })
1119
+
1120
+ test("the sidebar holds a compact help hint and the patch pane reaches the screen bottom", async () => {
1121
+ const viewer = await renderDiffViewer(hunkDiff, { width: 160, height: 40, kittyKeyboard: true })
1122
+ try {
1123
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1124
+ await viewer.app.flush()
1125
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1126
+ const hint = viewer.app.renderer.root.findDescendantById("diff-help-shortcut")!
1127
+ expect(scroll.viewport.y + scroll.viewport.height).toBe(40)
1128
+ expect(hint.x).toBe(2)
1129
+ expect(hint.y).toBe(37)
1130
+ expect(
1131
+ findScrollBox(viewer.app.renderer.root, false)!.parent!.y +
1132
+ findScrollBox(viewer.app.renderer.root, false)!.parent!.height,
1133
+ ).toBe(40)
1134
+ expect(viewer.app.captureCharFrame()).not.toContain("n/p files")
1135
+ expect(viewer.app.captureCharFrame()).not.toContain("tab focus")
1136
+ expect(viewer.app.captureCharFrame()).toContain("? help")
1137
+ expect(viewer.app.captureCharFrame()).not.toContain("next file")
1138
+ expect(viewer.app.captureCharFrame()).not.toContain("next hunk")
1139
+ expect(viewer.app.captureCharFrame()).not.toContain("half page")
1140
+ viewer.app.mockInput.pressTab()
1141
+ await viewer.app.renderOnce()
1142
+ expect(viewer.app.captureCharFrame()).toContain("? help")
1143
+
1144
+ viewer.app.mockInput.pressKey("?")
1145
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff shortcuts"))
1146
+ expect(viewer.app.captureCharFrame()).toContain("Next file")
1147
+ expect(viewer.app.captureCharFrame()).toContain("Half page")
1148
+ expect(viewer.app.captureCharFrame()).toContain("Split / unified")
1149
+ expect(viewer.app.captureCharFrame()).toContain("alt+↓")
1150
+ expect(viewer.app.captureCharFrame()).toContain("Review + collapse / reopen")
1151
+ expect(viewer.app.captureCharFrame()).toContain("right-click")
1152
+ expect(viewer.app.captureCharFrame()).not.toContain("Focus files")
1153
+ expect(viewer.app.captureCharFrame()).not.toContain("Description")
1154
+ viewer.app.mockInput.pressEscape()
1155
+ await viewer.app.waitForFrame((frame) => !frame.includes("Diff shortcuts"))
1156
+ expect(viewer.current()).toEqual(expect.objectContaining({ type: "plugin", name: "diff" }))
1157
+ } finally {
1158
+ viewer.app.renderer.destroy()
1159
+ }
1160
+ })
1161
+
1162
+ test("compact gutter help leaves the full patch viewport available when the sidebar is hidden", async () => {
1163
+ const viewer = await renderDiffViewer(hunkDiff, { width: 160, height: 24, kittyKeyboard: true })
1164
+ try {
1165
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1166
+ viewer.app.mockInput.pressKey("b")
1167
+ await viewer.app.flush()
1168
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1169
+ const hint = viewer.app.renderer.root.findDescendantById("diff-help-shortcut")!
1170
+ expect(hint.y).toBe(0)
1171
+ expect(hint.x).toBe(159)
1172
+ expect(scroll.viewport.y).toBe(2)
1173
+ expect(scroll.viewport.y + scroll.viewport.height).toBe(24)
1174
+ await viewer.app.mockMouse.click(hint.x, hint.y)
1175
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff shortcuts"))
1176
+ expect(viewer.app.renderer.getSelection()).toBeNull()
1177
+ viewer.app.mockInput.pressEscape()
1178
+ await viewer.app.waitForFrame((frame) => !frame.includes("Diff shortcuts"))
1179
+
1180
+ viewer.app.mockInput.pressKey("b")
1181
+ await viewer.app.flush()
1182
+ expect(viewer.app.renderer.root.findDescendantById("diff-help-shortcut")!.y).toBe(21)
1183
+ // Too narrow for the tree, full screen switches to the compact layout and its footer holds the help.
1184
+ viewer.app.resize(80, 24)
1185
+ await viewer.app.flush()
1186
+ const footer = viewer.app.renderer.root.findDescendantById("diff-footer")!
1187
+ expect(viewer.app.renderer.root.findDescendantById("diff-files")).toBeUndefined()
1188
+ expect(viewer.app.renderer.root.findDescendantById("diff-help-shortcut")!.y).toBe(footer.y)
1189
+ expect(scroll.viewport.y + scroll.viewport.height).toBe(footer.y)
1190
+ expect(viewer.app.captureCharFrame()).not.toContain("? help")
1191
+ // No padding: the header and footer sit on the first and last rows.
1192
+ expect(viewer.app.captureCharFrame().split("\n")[0]).toContain("All")
1193
+ expect(footer.y).toBe(23)
1194
+ expect(viewer.app.captureCharFrame().split("\n")[footer.y]).toMatch(/^ escape close\s+\? see all $/)
1195
+ viewer.app.mockInput.pressKey("d")
1196
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
1197
+ viewer.app.mockInput.pressEscape()
1198
+ await viewer.app.waitForFrame((frame) => !frame.includes("Diff source"))
1199
+ } finally {
1200
+ viewer.app.renderer.destroy()
1201
+ }
1202
+ })
1203
+
1204
+ test("full screen keeps its tree down to 88 columns and uses the compact layout below", async () => {
1205
+ const viewer = await renderDiffViewer(manyDiffs.slice(0, 3), { width: 88, height: 24, kittyKeyboard: true })
1206
+ try {
1207
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1208
+ viewer.app.mockInput.pressKey("m")
1209
+ await viewer.app.flush()
1210
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1211
+ expect(viewer.app.renderer.root.findDescendantById("diff-files")).toBeDefined()
1212
+ expect(viewer.app.renderer.root.findDescendantById("diff-footer")).toBeUndefined()
1213
+ // Full cards are inset two columns from the patch pane.
1214
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")!.width).toBe(scroll.viewport.width - 4)
1215
+
1216
+ viewer.app.resize(87, 24)
1217
+ await viewer.app.flush()
1218
+ expect(findScrollBox(viewer.app.renderer.root)).toBe(scroll)
1219
+ expect(viewer.app.renderer.root.findDescendantById("diff-files")).toBeUndefined()
1220
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")!.width).toBe(87)
1221
+ const second = viewer.app.renderer.root.findDescendantById("diff-file-header-1")
1222
+ if (!(second instanceof BoxRenderable)) throw new Error("Missing second file title")
1223
+ // The gap above a flush card takes the card's color across the full width, without an inset notch.
1224
+ expect(
1225
+ viewer.app
1226
+ .captureSpans()
1227
+ .lines[second.y - 1].spans.flatMap((span) => Array.from({ length: span.width }, () => span.fg)),
1228
+ ).toEqual(Array.from({ length: 87 }, () => second.backgroundColor))
1229
+ expect(viewer.app.renderer.root.findDescendantById("diff-review-count")!.y).toBe(0)
1230
+ const footer = viewer.app.renderer.root.findDescendantById("diff-footer")!
1231
+ expect(viewer.app.captureCharFrame().split("\n")[footer.y]).toMatch(/^ escape close\s+\? see all $/)
1232
+ expect(viewer.app.captureCharFrame()).toContain("1/3")
1233
+ viewer.app.mockInput.pressKey("b")
1234
+ await viewer.app.flush()
1235
+ expect(viewer.app.renderer.root.findDescendantById("diff-files")).toBeUndefined()
1236
+
1237
+ viewer.app.resize(88, 24)
1238
+ await viewer.app.flush()
1239
+ expect(viewer.app.renderer.root.findDescendantById("diff-files")).toBeDefined()
1240
+ expect(viewer.app.renderer.root.findDescendantById("diff-footer")).toBeUndefined()
1241
+ expect(viewer.app.captureCharFrame()).toContain("1/3")
1242
+ } finally {
1243
+ viewer.app.renderer.destroy()
1244
+ }
1245
+ })
1246
+
1247
+ test("compact help scrolls at a narrow size and reflects customized bindings", async () => {
1248
+ const viewer = await renderDiffViewer(hunkDiff, {
1249
+ width: 50,
1250
+ height: 20,
1251
+ kittyKeyboard: true,
1252
+ keybinds: { "diff.next_file": "ctrl+n", "diff.previous_file": "ctrl+p", "diff.close": "x" },
1253
+ })
1254
+ try {
1255
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1256
+ viewer.app.mockInput.pressKey("?")
1257
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff shortcuts"))
1258
+ const scroll = viewer.app.renderer.root.findDescendantById("diff-help-scroll")
1259
+ if (!(scroll instanceof ScrollBoxRenderable)) throw new Error("Missing help scrollbox")
1260
+ expect(scroll.width).toBeLessThanOrEqual(46)
1261
+ expect(viewer.app.captureCharFrame()).toMatch(/ctrl\+n\s+Next file/)
1262
+ expect(viewer.app.captureCharFrame()).not.toContain("alt+↓")
1263
+ scroll.scrollTo(scroll.scrollHeight)
1264
+ await viewer.app.flush()
1265
+ // The compact layout has no file tree to toggle.
1266
+ expect(viewer.app.captureCharFrame()).not.toContain("Show / hide file tree")
1267
+ expect(viewer.app.captureCharFrame()).not.toContain("Focus files")
1268
+ expect(viewer.app.captureCharFrame()).toMatch(/x\s+Close diff viewer/)
1269
+ viewer.app.mockInput.pressEscape()
1270
+ await viewer.app.waitForFrame((frame) => !frame.includes("Diff shortcuts"))
1271
+ expect(viewer.current()).toEqual(expect.objectContaining({ type: "plugin", name: "diff" }))
1272
+ } finally {
1273
+ viewer.app.renderer.destroy()
1274
+ }
1275
+ })
1276
+
1277
+ test.each([
1278
+ { key: "?", shift: false },
1279
+ { key: "?", shift: true },
1280
+ { key: "/", shift: true },
1281
+ ])("the viewer opens help from $key with shift=$shift", async (input) => {
1282
+ const viewer = await renderDiffViewer(hunkDiff, { width: 160, height: 40, kittyKeyboard: true })
1283
+ try {
1284
+ await viewer.app.waitForFrame((frame) => frame.includes("const first") && frame.includes("? help"))
1285
+ viewer.app.mockInput.pressKey(input.key, { shift: input.shift })
1286
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff shortcuts"))
1287
+ expect(viewer.app.captureCharFrame()).toContain("Next file")
1288
+ viewer.app.mockInput.pressEscape()
1289
+ await viewer.app.waitForFrame((frame) => !frame.includes("Diff shortcuts"))
1290
+ expect(viewer.app.captureCharFrame()).toContain("const first")
1291
+ } finally {
1292
+ viewer.app.renderer.destroy()
1293
+ }
1294
+ })
1295
+
1296
+ test.each([80, 160])("file titles stick to the viewport and hand off while scrolling at %i columns", async (width) => {
1297
+ const viewer = await renderDiffViewer(
1298
+ Array.from({ length: 4 }, (_, index) => ({ ...hunkDiff[0], file: `src/file${index}.txt` })),
1299
+ { width, height: 18 },
1300
+ )
1301
+ try {
1302
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1303
+ await viewer.app.flush()
1304
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1305
+ const first = viewer.app.renderer.root.findDescendantById("diff-file-header-0")
1306
+ const second = viewer.app.renderer.root.findDescendantById("diff-file-header-1")
1307
+ if (!(first instanceof BoxRenderable) || !(second instanceof BoxRenderable)) throw new Error("Missing file titles")
1308
+ const next = second.y - scroll.content.y
1309
+
1310
+ scroll.scrollTo(5)
1311
+ await viewer.app.flush()
1312
+ expect(first.y).toBe(scroll.viewport.y)
1313
+ expect(viewer.app.captureCharFrame().split("src/file0.txt")).toHaveLength(2)
1314
+
1315
+ viewer.app.resize(width === 160 ? 80 : 160, 22)
1316
+ await viewer.app.flush()
1317
+ expect(first.y).toBe(scroll.viewport.y)
1318
+ viewer.app.resize(width, 18)
1319
+ await viewer.app.flush()
1320
+ expect(first.y).toBe(scroll.viewport.y)
1321
+
1322
+ if (width === 160) {
1323
+ viewer.app.mockInput.pressTab()
1324
+ await viewer.app.renderOnce()
1325
+ expect(first.y).toBe(scroll.viewport.y)
1326
+ expect(viewer.app.captureCharFrame()).toContain("src/file0.txt")
1327
+ viewer.app.mockInput.pressTab()
1328
+ }
1329
+
1330
+ scroll.scrollTo(next + 3)
1331
+ await viewer.app.flush()
1332
+ expect(second.y).toBe(scroll.viewport.y)
1333
+ expect(first.y + first.height).toBeLessThanOrEqual(scroll.viewport.y)
1334
+ expect(viewer.app.captureCharFrame().split("src/file1.txt")).toHaveLength(2)
1335
+ expect(viewer.app.captureCharFrame()).not.toContain("src/file0.txt")
1336
+
1337
+ const background = second.backgroundColor
1338
+ viewer.commands.get("diff.mark_reviewed")!.run()
1339
+ await viewer.app.flush()
1340
+ expect(second.backgroundColor).not.toEqual(background)
1341
+ expect(second.y).toBe(scroll.viewport.y)
1342
+
1343
+ scroll.scrollTo(0)
1344
+ await viewer.app.flush()
1345
+ expect(first.translateY).toBe(0)
1346
+ expect(first.y).toBe(scroll.viewport.y)
1347
+
1348
+ viewer.commands.get("diff.single_patch")!.run()
1349
+ await viewer.app.flush()
1350
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-1")).toBeUndefined()
1351
+ const single = viewer.app.renderer.root.findDescendantById("diff-file-header-0")
1352
+ if (!(single instanceof BoxRenderable)) throw new Error("Missing single file title")
1353
+ scroll.scrollTo(5)
1354
+ await viewer.app.flush()
1355
+ expect(single.y).toBe(scroll.viewport.y)
1356
+ } finally {
1357
+ viewer.app.renderer.destroy()
1358
+ }
1359
+ })
1360
+
1361
+ test("split diff filler keeps the full card background", async () => {
1362
+ const viewer = await renderDiffViewer(
1363
+ [
1364
+ {
1365
+ file: ".gitignore",
1366
+ status: "modified",
1367
+ additions: 1,
1368
+ deletions: 0,
1369
+ patch: "--- a/.gitignore\n+++ b/.gitignore\n@@ -1 +1,2 @@\n node_modules/\n+artifacts/\n",
1370
+ },
1371
+ ],
1372
+ { width: 160 },
1373
+ )
1374
+ try {
1375
+ await viewer.app.waitForFrame((frame) => frame.includes("artifacts/"))
1376
+ const diff = findDiffs(viewer.app.renderer.root)[0]
1377
+ const frame = viewer.app.captureSpans()
1378
+ const backgrounds = (row: number) =>
1379
+ frame.lines[row].spans.flatMap((span) => Array.from({ length: span.width }, () => span.bg))
1380
+ expect(backgrounds(diff.y + 1).slice(diff.x, diff.x + Math.floor(diff.width / 2))).toEqual(
1381
+ Array.from({ length: Math.floor(diff.width / 2) }, () => backgrounds(diff.y - 1)[diff.x]),
1382
+ )
1383
+ } finally {
1384
+ viewer.app.renderer.destroy()
1385
+ }
1386
+ })
1387
+
1388
+ test.each(["patch", "image", "fallback"] as const)(
1389
+ "reviewing collapses a %s body and unmarking reopens it",
1390
+ async (kind) => {
1391
+ const file =
1392
+ kind === "patch"
1393
+ ? hunkDiff[0]
1394
+ : {
1395
+ file: kind === "image" ? "assets/preview.png" : "bun.lock",
1396
+ status: "modified",
1397
+ additions: 0,
1398
+ deletions: 0,
1399
+ }
1400
+ const content = kind === "patch" ? "const first" : kind === "image" ? "96 x 48" : "No patch available"
1401
+ const viewer = await renderDiffViewer([file], { width: 80, height: kind === "patch" ? 16 : 24 })
1402
+ try {
1403
+ await viewer.app.waitForFrame((frame) => frame.includes(content))
1404
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1405
+ if (kind === "patch") {
1406
+ scroll.scrollTo(4)
1407
+ await viewer.app.flush()
1408
+ expect(scroll.scrollTop).toBeGreaterThan(0)
1409
+ }
1410
+ viewer.app.mockInput.pressKey("m")
1411
+ await viewer.app.flush()
1412
+ const header = viewer.app.renderer.root.findDescendantById("diff-file-header-0")
1413
+ if (!(header instanceof BoxRenderable)) throw new Error("Missing reviewed file title")
1414
+ expect(header.parent?.height).toBe(header.height)
1415
+ expect(header.height).toBe(2)
1416
+ // Compact cards are flush with the pane edges.
1417
+ expect(viewer.app.captureCharFrame().split("\n")[header.y + 1].trim()).toBe("▀".repeat(header.width))
1418
+ expect(header.y).toBe(scroll.viewport.y)
1419
+ expect(viewer.app.captureCharFrame()).toContain(file.file)
1420
+ expect(viewer.app.captureCharFrame()).toContain("✓")
1421
+ expect(viewer.app.captureCharFrame()).not.toContain(content)
1422
+ expect(findDiffs(viewer.app.renderer.root)).toHaveLength(0)
1423
+
1424
+ viewer.app.mockInput.pressKey("m")
1425
+ await viewer.app.waitForFrame((frame) => frame.includes(content))
1426
+ await viewer.app.flush()
1427
+ expect(header.parent!.height).toBeGreaterThan(header.height)
1428
+ expect(viewer.app.captureCharFrame().split("\n")[header.y + 1].trim()).toBe("")
1429
+ expect(viewer.app.captureCharFrame()).not.toContain("✓")
1430
+ if (kind === "patch") {
1431
+ viewer.app.mockInput.pressKey("]")
1432
+ await viewer.app.flush()
1433
+ expect(scroll.scrollTop).toBeGreaterThan(0)
1434
+ }
1435
+ } finally {
1436
+ viewer.app.renderer.destroy()
1437
+ }
1438
+ },
1439
+ )
1440
+
1441
+ test("reviewing after scrolling targets the visible file rather than the initial file", async () => {
1442
+ const viewer = await renderDiffViewer(
1443
+ [
1444
+ { ...hunkDiff[0], file: "src/file0.txt" },
1445
+ { ...hunkDiff[0], file: "src/file1.txt" },
1446
+ ],
1447
+ { width: 160, height: 16 },
1448
+ )
1449
+ try {
1450
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1451
+ await viewer.app.flush()
1452
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1453
+ scroll.scrollTo(scroll.scrollTop + findDiffs(viewer.app.renderer.root)[3].y - scroll.viewport.y)
1454
+ await viewer.app.renderOnce()
1455
+ viewer.commands.get("diff.mark_reviewed")!.run()
1456
+ await viewer.app.waitForFrame((frame) => /file1\.txt\s+✓/.test(frame))
1457
+ expect(viewer.app.captureCharFrame()).not.toMatch(/file0\.txt\s+✓/)
1458
+ } finally {
1459
+ viewer.app.renderer.destroy()
1460
+ }
1461
+ })
1462
+
1463
+ test.each([80, 160])(
1464
+ "single-file review advances, but reopening and the last file stay put at %i columns",
1465
+ async (width) => {
1466
+ const viewer = await renderDiffViewer(
1467
+ Array.from({ length: 3 }, (_, index) => ({ ...hunkDiff[0], file: `src/file${index}.txt` })),
1468
+ { width, height: 24, kittyKeyboard: true },
1469
+ )
1470
+ try {
1471
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1472
+ viewer.app.mockInput.pressKey("s")
1473
+ await viewer.app.flush()
1474
+ if (width === 160) viewer.app.mockInput.pressTab()
1475
+ viewer.app.mockInput.pressKey("m")
1476
+ await viewer.app.flush()
1477
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")).toBeUndefined()
1478
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-1")).toBeDefined()
1479
+ expect(viewer.app.captureCharFrame()).toContain("const first")
1480
+
1481
+ viewer.app.mockInput.pressKey("p")
1482
+ await viewer.app.flush()
1483
+ expect(findDiffs(viewer.app.renderer.root)).toHaveLength(0)
1484
+ viewer.app.mockInput.pressKey("m")
1485
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1486
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")).toBeDefined()
1487
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-1")).toBeUndefined()
1488
+
1489
+ viewer.app.mockInput.pressKey("n")
1490
+ await viewer.app.flush()
1491
+ viewer.app.mockInput.pressKey("m")
1492
+ await viewer.app.flush()
1493
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-2")).toBeDefined()
1494
+ viewer.app.mockInput.pressKey("m")
1495
+ await viewer.app.flush()
1496
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-2")).toBeDefined()
1497
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")).toBeUndefined()
1498
+ expect(findDiffs(viewer.app.renderer.root)).toHaveLength(0)
1499
+ viewer.app.mockInput.pressKey("m")
1500
+ await viewer.app.flush()
1501
+ expect(findDiffs(viewer.app.renderer.root)).toHaveLength(0)
1502
+
1503
+ viewer.app.mockInput.pressKey("?")
1504
+ await viewer.app.waitForFrame((frame) => frame.includes("Review + next / reopen"))
1505
+ } finally {
1506
+ viewer.app.renderer.destroy()
1507
+ }
1508
+ },
1509
+ )
1510
+
1511
+ test.each([false, true])("Alt+arrows navigate files rather than session tabs (after Tab: %s)", async (afterTab) => {
1512
+ let tabChanges = 0
1513
+ const viewer = await renderDiffViewer(manyDiffs, {
1514
+ width: 160,
1515
+ height: 24,
1516
+ kittyKeyboard: true,
1517
+ onSessionTab: () => tabChanges++,
1518
+ })
1519
+ try {
1520
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1521
+ await viewer.app.flush()
1522
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1523
+ if (afterTab) viewer.app.mockInput.pressTab()
1524
+ viewer.app.mockInput.pressKey("down", { meta: true })
1525
+ await viewer.app.flush()
1526
+ expect(viewer.app.captureCharFrame().split("\n")[scroll.viewport.y]).toContain("file01.txt")
1527
+ viewer.app.mockInput.pressKey("up", { meta: true })
1528
+ await viewer.app.flush()
1529
+ expect(scroll.scrollTop).toBe(0)
1530
+ expect(tabChanges).toBe(0)
1531
+ } finally {
1532
+ viewer.app.renderer.destroy()
1533
+ }
1534
+ })
1535
+
1536
+ test.each([
1537
+ { target: "tree", mode: "dark" },
1538
+ { target: "heading", mode: "dark" },
1539
+ { target: "tree", mode: "light" },
1540
+ { target: "heading", mode: "light" },
1541
+ ] as const)("the $target context menu completes the clicked file without selecting it in $mode mode", async (input) => {
1542
+ const viewer = await renderDiffViewer(manyDiffs.slice(0, 3), {
1543
+ width: 160,
1544
+ height: 40,
1545
+ kittyKeyboard: true,
1546
+ mode: input.mode,
1547
+ })
1548
+ try {
1549
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1550
+ await viewer.app.flush()
1551
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1552
+ const node = () =>
1553
+ viewer.app.renderer.root.findDescendantById(input.target === "tree" ? "diff-file-row-1" : "diff-file-header-1")!
1554
+ await viewer.app.mockMouse.click(node().x + 4, node().y, MouseButton.RIGHT)
1555
+ await viewer.app.waitForFrame((frame) => frame.includes("Mark complete"))
1556
+ expect(scroll.scrollTop).toBe(0)
1557
+ expect(viewer.app.captureCharFrame()).toContain("0/3")
1558
+ viewer.app.mockInput.pressKey("n")
1559
+ viewer.app.mockInput.pressKey("m")
1560
+ await viewer.app.flush()
1561
+ expect(scroll.scrollTop).toBe(0)
1562
+ expect(viewer.app.captureCharFrame()).toContain("0/3")
1563
+ const menu = viewer.app.renderer.root.findDescendantById("diff-file-menu")!
1564
+ expect(
1565
+ viewer.app.captureSpans().lines[menu.y].spans.find((span) => span.text.includes("Mark complete"))!.bg,
1566
+ ).not.toEqual(viewer.app.captureSpans().lines[1].spans.find((span) => span.text.includes("All"))!.bg)
1567
+ await viewer.app.mockMouse.click(menu.x + 1, menu.y)
1568
+ await viewer.app.waitForFrame((frame) => frame.includes("1/3"))
1569
+ expect(viewer.app.captureCharFrame()).toMatch(/file01\.txt\s+✓/)
1570
+ expect(viewer.app.captureCharFrame()).not.toMatch(/file00\.txt\s+✓/)
1571
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-menu")).toBeUndefined()
1572
+ expect(findDiffs(viewer.app.renderer.root)).toHaveLength(6)
1573
+ expect(scroll.scrollTop).toBe(0)
1574
+
1575
+ await viewer.app.mockMouse.click(node().x + 4, node().y, MouseButton.RIGHT)
1576
+ await viewer.app.waitForFrame((frame) => frame.includes("Mark incomplete"))
1577
+ viewer.app.mockInput.pressEnter()
1578
+ await viewer.app.waitForFrame((frame) => frame.includes("0/3"))
1579
+ expect(findDiffs(viewer.app.renderer.root)).toHaveLength(9)
1580
+ expect(scroll.scrollTop).toBe(0)
1581
+ } finally {
1582
+ viewer.app.renderer.destroy()
1583
+ }
1584
+ })
1585
+
1586
+ test.each(["escape", "outside"] as const)(
1587
+ "a clamped file menu dismisses with %s without closing the viewer",
1588
+ async (dismiss) => {
1589
+ const viewer = await renderDiffViewer(hunkDiff, { width: 80, height: 16, kittyKeyboard: true })
1590
+ try {
1591
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1592
+ await viewer.app.flush()
1593
+ const header = viewer.app.renderer.root.findDescendantById("diff-file-header-0")!
1594
+ await viewer.app.mockMouse.click(header.x + header.width - 1, header.y, MouseButton.RIGHT)
1595
+ await viewer.app.waitForFrame((frame) => frame.includes("Mark complete"))
1596
+ const menu = viewer.app.renderer.root.findDescendantById("diff-file-menu")!
1597
+ expect(menu.x).toBeGreaterThanOrEqual(0)
1598
+ expect(menu.x + menu.width).toBeLessThanOrEqual(80)
1599
+ expect(menu.y + menu.height).toBeLessThanOrEqual(16)
1600
+ if (dismiss === "escape") viewer.app.mockInput.pressEscape()
1601
+ if (dismiss === "outside") await viewer.app.mockMouse.click(1, 1)
1602
+ await viewer.app.waitForFrame((frame) => !frame.includes("Mark complete"))
1603
+ expect(viewer.current()).toEqual(expect.objectContaining({ type: "plugin", name: "diff" }))
1604
+ expect(findDiffs(viewer.app.renderer.root)).toHaveLength(3)
1605
+ viewer.app.mockInput.pressKey("j")
1606
+ await viewer.app.flush()
1607
+ expect(findScrollBox(viewer.app.renderer.root)!.scrollTop).toBe(1)
1608
+ } finally {
1609
+ viewer.app.renderer.destroy()
1610
+ }
1611
+ },
1612
+ )
1613
+
1614
+ test("single-file menu actions advance only when completing the current file", async () => {
1615
+ const viewer = await renderDiffViewer(manyDiffs.slice(0, 3), { width: 160, height: 24, kittyKeyboard: true })
1616
+ try {
1617
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1618
+ viewer.app.mockInput.pressKey("s")
1619
+ await viewer.app.flush()
1620
+ const other = viewer.app.renderer.root.findDescendantById("diff-file-row-2")!
1621
+ await viewer.app.mockMouse.click(other.x + 4, other.y, MouseButton.RIGHT)
1622
+ await viewer.app.waitForFrame((frame) => frame.includes("Mark complete"))
1623
+ viewer.app.mockInput.pressEnter()
1624
+ await viewer.app.waitForFrame((frame) => frame.includes("1/3"))
1625
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")).toBeDefined()
1626
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-2")).toBeUndefined()
1627
+ expect(viewer.app.captureCharFrame()).toMatch(/file02\.txt\s+✓/)
1628
+ const current = viewer.app.renderer.root.findDescendantById("diff-file-header-0")!
1629
+ await viewer.app.mockMouse.click(current.x + 4, current.y, MouseButton.RIGHT)
1630
+ await viewer.app.waitForFrame((frame) => frame.includes("Mark complete"))
1631
+ viewer.app.mockInput.pressEnter()
1632
+ await viewer.app.waitForFrame((frame) => frame.includes("2/3") && frame.includes("const first"))
1633
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-0")).toBeUndefined()
1634
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-header-1")).toBeDefined()
1635
+ expect(viewer.app.captureCharFrame()).toContain("const first")
1636
+ } finally {
1637
+ viewer.app.renderer.destroy()
1638
+ }
1639
+ })
1640
+
1641
+ test("image previews use the diff session's filesystem location", async () => {
1642
+ const viewer = await renderDiffViewer([
1643
+ { file: "assets/mock image.png", status: "modified", additions: 0, deletions: 0 },
1644
+ ])
1645
+ try {
1646
+ await viewer.app.waitFor(() => viewer.imageReadInput() !== undefined)
1647
+ expect(viewer.imageReadInput()).toEqual({ path: "assets/mock image.png", location: { directory: "/repo/session" } })
1648
+ await viewer.app.waitForFrame((frame) => frame.includes("96 x 48"))
1649
+ const lines = viewer.app.captureCharFrame().split("\n")
1650
+ const row = lines.findIndex((line) => line.includes("Working tree preview"))
1651
+ const top = lines[lines.findIndex((line) => line.includes("assets/mock image.png")) - 1]
1652
+ const header = viewer.app.renderer.root.findDescendantById("diff-file-header-0")!
1653
+ const background = viewer.app.captureSpans().lines[row].spans.find((span) => span.text.includes("Working tree"))!.bg
1654
+ const edges = viewer.app
1655
+ .captureSpans()
1656
+ .lines[row + 2].spans.flatMap((span) => Array.from({ length: span.width }, () => span.bg))
1657
+ expect(top.trim()).toBe("")
1658
+ expect(edges[header.x]).toEqual(background)
1659
+ expect(edges[header.x + header.width - 1]).toEqual(background)
1660
+ } finally {
1661
+ viewer.app.renderer.destroy()
1662
+ }
1663
+ })
1664
+
1665
+ test("single-file images load once per file and cancel reads when their preview is removed", async () => {
1666
+ const reads: { file: string; signal: AbortSignal }[] = []
1667
+ const viewer = await renderDiffViewer(
1668
+ ["a.png", "b.png"].map((file) => ({ file, status: "modified", additions: 0, deletions: 0 })),
1669
+ {
1670
+ single: true,
1671
+ readImage: async (file, signal) => {
1672
+ reads.push({ file, signal })
1673
+ return diffImageFixture
1674
+ },
1675
+ },
1676
+ )
1677
+ try {
1678
+ await viewer.app.waitForFrame((frame) => frame.includes("96 x 48"))
1679
+ expect(reads.map((read) => read.file)).toEqual(["a.png"])
1680
+ viewer.app.mockInput.pressKey("n")
1681
+ await viewer.app.waitForFrame((frame) => frame.includes("b.png") && frame.includes("96 x 48"))
1682
+ await viewer.app.flush()
1683
+ expect(reads.map((read) => read.file)).toEqual(["a.png", "b.png"])
1684
+ expect(reads[0].signal.aborted).toBe(true)
1685
+ viewer.app.mockInput.pressKey("j")
1686
+ await viewer.app.flush()
1687
+ expect(reads.map((read) => read.file)).toEqual(["a.png", "b.png"])
1688
+ viewer.app.mockInput.pressKey("m")
1689
+ await viewer.app.flush()
1690
+ expect(reads[1].signal.aborted).toBe(true)
1691
+ } finally {
1692
+ viewer.app.renderer.destroy()
1693
+ }
1694
+ })
1695
+
1696
+ test("reviewing a mouse-scrolled past selection keeps the visible file in place", async () => {
1697
+ const viewer = await renderDiffViewer(manyDiffs.slice(0, 5), { width: 160, height: 24, kittyKeyboard: true })
1698
+ try {
1699
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1700
+ viewer.app.mockInput.pressKey("n")
1701
+ await viewer.app.flush()
1702
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1703
+ const header = viewer.app.renderer.root.findDescendantById("diff-file-header-2")!
1704
+ scroll.scrollTo(scroll.scrollTop + header.y - scroll.viewport.y + 2)
1705
+ await viewer.app.flush()
1706
+ await viewer.app.mockMouse.scroll(scroll.x + 5, scroll.viewport.y + 5, "down")
1707
+ await viewer.app.flush()
1708
+ const before = scroll.scrollTop
1709
+ const row = viewer.app.renderer.root.findDescendantById("diff-file-row-1")!
1710
+ await viewer.app.mockMouse.click(row.x + 4, row.y, MouseButton.RIGHT)
1711
+ await viewer.app.waitForFrame((frame) => frame.includes("Mark complete"))
1712
+ viewer.app.mockInput.pressEnter()
1713
+ await viewer.app.flush()
1714
+ expect(viewer.app.captureCharFrame().split("\n")[scroll.viewport.y]).toContain("file02.txt")
1715
+ expect(scroll.scrollTop).toBeLessThan(before)
1716
+ viewer.app.mockInput.pressKey("m")
1717
+ await viewer.app.waitForFrame((frame) => /file02\.txt\s+✓/.test(frame))
1718
+ } finally {
1719
+ viewer.app.renderer.destroy()
1720
+ }
1721
+ })
1722
+
1723
+ test.each(["patch", "image"] as const)(
1724
+ "completing and reopening an earlier %s preserves the reading position",
1725
+ async (kind) => {
1726
+ const pending = Promise.withResolvers<Uint8Array>()
1727
+ let reads = 0
1728
+ const viewer = await renderDiffViewer(
1729
+ [
1730
+ ...(kind === "image" ? [{ file: "a.png", status: "modified", additions: 0, deletions: 0 }] : []),
1731
+ ...manyDiffs.slice(0, 4),
1732
+ ],
1733
+ {
1734
+ width: 160,
1735
+ height: 24,
1736
+ kittyKeyboard: true,
1737
+ readImage: () => (++reads === 1 ? Promise.resolve(diffImageFixture) : pending.promise),
1738
+ },
1739
+ )
1740
+ try {
1741
+ await viewer.app.waitForFrame((frame) => frame.includes(kind === "image" ? "96 x 48" : "const first"))
1742
+ viewer.app.mockInput.pressKey("n")
1743
+ await viewer.app.flush()
1744
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1745
+ scroll.scrollBy(4)
1746
+ await viewer.app.flush()
1747
+ const patchFrame = () =>
1748
+ viewer.app
1749
+ .captureCharFrame()
1750
+ .split("\n")
1751
+ .map((line) => line.slice(scroll.x))
1752
+ .join("\n")
1753
+ const before = patchFrame()
1754
+ const row = viewer.app.renderer.root.findDescendantById("diff-file-row-0")!
1755
+ for (const label of ["Mark complete", "Mark incomplete"]) {
1756
+ await viewer.app.mockMouse.click(row.x + 4, row.y, MouseButton.RIGHT)
1757
+ await viewer.app.waitForFrame((frame) => frame.includes(label))
1758
+ viewer.app.mockInput.pressEnter()
1759
+ await viewer.app.flush()
1760
+ expect(patchFrame()).toBe(before)
1761
+ }
1762
+ if (kind === "patch") return
1763
+ pending.resolve(diffImageFixture)
1764
+ await viewer.app.waitFor(() => {
1765
+ const image = viewer.app.renderer.root.findDescendantById("diff-image-a.png")
1766
+ return image instanceof ImageRenderable && image.image?.width === 96
1767
+ })
1768
+ await viewer.app.flush()
1769
+ expect(patchFrame()).toBe(before)
1770
+ } finally {
1771
+ pending.resolve(diffImageFixture)
1772
+ viewer.app.renderer.destroy()
1773
+ }
1774
+ },
1775
+ )
1776
+
1777
+ test.each([
1778
+ { file: "bun.lock", status: "modified" as const, message: "No patch available for this file." },
1779
+ { file: "assets/old.png", status: "deleted" as const, message: "Deleted image." },
1780
+ ])("pads $file fallback messages inside the card without reading images", async (input) => {
1781
+ const viewer = await renderDiffViewer([{ file: input.file, status: input.status, additions: 0, deletions: 0 }])
1782
+ try {
1783
+ await viewer.app.waitForFrame((frame) => frame.includes(input.message))
1784
+ expect(viewer.imageReadInput()).toBeUndefined()
1785
+ const header = viewer.app.renderer.root.findDescendantById("diff-file-header-0")
1786
+ if (!(header instanceof BoxRenderable)) throw new Error("Missing file title")
1787
+ const lines = viewer.app.captureCharFrame().split("\n")
1788
+ const title = lines.findIndex((line) => line.includes(input.file))
1789
+ const row = lines.findIndex((line) => line.includes(input.message))
1790
+ expect(lines[row].indexOf(input.message)).toBe(lines[title].indexOf(input.file))
1791
+ expect(lines[row + 1].trim()).toBe("")
1792
+ const padding = viewer.app
1793
+ .captureSpans()
1794
+ .lines[row + 1].spans.flatMap((span) => Array.from({ length: span.width }, () => span.bg))
1795
+ expect(padding[header.x]).toEqual(header.backgroundColor)
1796
+ expect(padding[header.x + header.width - 1]).toEqual(header.backgroundColor)
1797
+ } finally {
1798
+ viewer.app.renderer.destroy()
1799
+ }
1800
+ })
1801
+
1802
+ test("Ctrl+D and Ctrl+U scroll the patch viewport by half a page, without closing", async () => {
1803
+ const viewer = await renderDiffViewer(manyDiffs, { width: 160, height: 24 })
1804
+ try {
1805
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1806
+ await viewer.app.flush()
1807
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1808
+ viewer.app.mockInput.pressKey("d", { ctrl: true })
1809
+ await viewer.app.renderOnce()
1810
+ expect(viewer.current().type).toBe("plugin")
1811
+ expect(scroll.scrollTop).toBe(Math.floor(scroll.viewport.height / 2))
1812
+ viewer.app.mockInput.pressKey("u", { ctrl: true })
1813
+ await viewer.app.renderOnce()
1814
+ expect(scroll.scrollTop).toBe(0)
1815
+ viewer.app.mockInput.pressKey("f", { ctrl: true })
1816
+ await viewer.app.renderOnce()
1817
+ expect(scroll.scrollTop).toBe(scroll.viewport.height)
1818
+ viewer.app.mockInput.pressKey("b", { ctrl: true })
1819
+ await viewer.app.renderOnce()
1820
+ expect(scroll.scrollTop).toBe(0)
1821
+ } finally {
1822
+ viewer.app.renderer.destroy()
1823
+ }
1824
+ })
1825
+
1826
+ test.each([20, 32])("Tab and file clicks never redirect scrolling keys into the tree at %i rows", async (height) => {
1827
+ const viewer = await renderDiffViewer(manyDiffs, { width: 160, height, keybinds: { "diff.switch_focus": "tab" } })
1828
+ try {
1829
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1830
+ await viewer.app.flush()
1831
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1832
+ const tree = findScrollBox(viewer.app.renderer.root, false)!
1833
+ const row = viewer.app.renderer.root.findDescendantById("diff-file-row-0")!
1834
+ expect(viewer.commands.has("diff.switch_focus")).toBe(false)
1835
+ await viewer.app.mockMouse.click(row.x + 4, row.y)
1836
+ viewer.app.mockInput.pressTab()
1837
+ viewer.app.mockInput.pressKey("j")
1838
+ await viewer.app.renderOnce()
1839
+ expect(scroll.scrollTop).toBe(1)
1840
+ expect(tree.scrollTop).toBe(0)
1841
+ viewer.app.mockInput.pressKey("k")
1842
+ await viewer.app.renderOnce()
1843
+ expect(scroll.scrollTop).toBe(0)
1844
+ viewer.app.mockInput.pressArrow("down")
1845
+ await viewer.app.renderOnce()
1846
+ expect(scroll.scrollTop).toBe(1)
1847
+ viewer.app.mockInput.pressArrow("up")
1848
+ await viewer.app.renderOnce()
1849
+ expect(scroll.scrollTop).toBe(0)
1850
+ viewer.app.mockInput.pressKey("d", { ctrl: true })
1851
+ await viewer.app.renderOnce()
1852
+ const half = Math.floor(scroll.viewport.height / 2)
1853
+ expect(viewer.current().type).toBe("plugin")
1854
+ expect(scroll.scrollTop).toBe(half)
1855
+ expect(tree.scrollTop).toBe(0)
1856
+ viewer.app.mockInput.pressKey("u", { ctrl: true })
1857
+ await viewer.app.renderOnce()
1858
+ expect(scroll.scrollTop).toBe(0)
1859
+ viewer.app.mockInput.pressKey("f", { ctrl: true })
1860
+ await viewer.app.renderOnce()
1861
+ expect(scroll.scrollTop).toBe(scroll.viewport.height)
1862
+ expect(tree.scrollTop).toBe(0)
1863
+ } finally {
1864
+ viewer.app.renderer.destroy()
1865
+ }
1866
+ })
1867
+
1868
+ test("folders are mouse controlled and n/p reveal files inside collapsed folders", async () => {
1869
+ const viewer = await renderDiffViewer(
1870
+ [
1871
+ { ...hunkDiff[0], file: "src/a/first.txt" },
1872
+ { ...hunkDiff[0], file: "src/b/second.txt" },
1873
+ { ...hunkDiff[0], file: "test/third.txt" },
1874
+ ],
1875
+ { width: 160, height: 28 },
1876
+ )
1877
+ try {
1878
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1879
+ await viewer.app.flush()
1880
+ const treeFrame = () =>
1881
+ viewer.app
1882
+ .captureCharFrame()
1883
+ .split("\n")
1884
+ .map((line) => line.slice(0, 40))
1885
+ .join("\n")
1886
+ const folder = viewer.app.renderer.root.findDescendantById("diff-folder-row-0")!
1887
+ await viewer.app.mockMouse.click(folder.x + 1, folder.y, MouseButton.RIGHT)
1888
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-menu")).toBeUndefined()
1889
+ expect(treeFrame()).toContain("first.txt")
1890
+ await viewer.app.mockMouse.click(folder.x + 1, folder.y)
1891
+ await viewer.app.renderOnce()
1892
+ expect(treeFrame()).toContain("▸ src")
1893
+ expect(treeFrame()).not.toContain("first.txt")
1894
+ viewer.app.mockInput.pressTab()
1895
+ viewer.app.mockInput.pressKey("l")
1896
+ viewer.app.mockInput.pressKey("}")
1897
+ viewer.app.mockInput.pressKey("h")
1898
+ await viewer.app.renderOnce()
1899
+ expect(treeFrame()).toContain("▸ src")
1900
+ viewer.app.mockInput.pressKey("n")
1901
+ await viewer.app.flush()
1902
+ expect(treeFrame()).toContain("▾ src")
1903
+ expect(treeFrame()).toContain("second.txt")
1904
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1905
+ expect(viewer.app.captureCharFrame().split("\n")[scroll.viewport.y]).toContain("src/b/second.txt")
1906
+ viewer.app.mockInput.pressKey("p")
1907
+ await viewer.app.flush()
1908
+ expect(scroll.scrollTop).toBe(0)
1909
+ } finally {
1910
+ viewer.app.renderer.destroy()
1911
+ }
1912
+ })
1913
+
1914
+ test("n/p jump between files and gg/G always navigate the diff", async () => {
1915
+ const viewer = await renderDiffViewer(manyDiffs, { width: 160, height: 24 })
1916
+ try {
1917
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
1918
+ await viewer.app.flush()
1919
+ const scroll = findScrollBox(viewer.app.renderer.root)!
1920
+ const row = viewer.app.renderer.root.findDescendantById("diff-file-row-0")!
1921
+ viewer.app.mockInput.pressKey("n")
1922
+ await viewer.app.flush()
1923
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-row-0")).toBe(row)
1924
+ expect(scroll.scrollTop).toBeGreaterThan(0)
1925
+ expect(viewer.app.captureCharFrame()).toContain("file01.txt")
1926
+ viewer.app.mockInput.pressKey("p")
1927
+ await viewer.app.flush()
1928
+ expect(viewer.app.renderer.root.findDescendantById("diff-file-row-0")).toBe(row)
1929
+ expect(scroll.scrollTop).toBe(0)
1930
+ viewer.app.mockInput.pressKey("G")
1931
+ await viewer.app.renderOnce()
1932
+ expect(scroll.scrollTop).toBe(scroll.scrollHeight - scroll.viewport.height)
1933
+ viewer.app.mockInput.pressKey("g")
1934
+ viewer.app.mockInput.pressKey("g")
1935
+ await viewer.app.renderOnce()
1936
+ expect(scroll.scrollTop).toBe(0)
1937
+ viewer.app.mockInput.pressTab()
1938
+ viewer.app.mockInput.pressKey("G")
1939
+ await viewer.app.renderOnce()
1940
+ expect(scroll.scrollTop).toBe(scroll.scrollHeight - scroll.viewport.height)
1941
+ expect(findScrollBox(viewer.app.renderer.root, false)!.scrollTop).toBe(0)
1942
+ viewer.app.mockInput.pressKey("g")
1943
+ viewer.app.mockInput.pressKey("g")
1944
+ viewer.app.mockInput.pressKey("m")
1945
+ await viewer.app.waitForFrame((frame) => /file00\.txt\s+✓/.test(frame))
1946
+ } finally {
1947
+ viewer.app.renderer.destroy()
1948
+ }
1949
+ })
1950
+
1951
+ async function renderDiffViewer(
1952
+ vcsDiff: unknown[],
1953
+ options: {
1954
+ width?: number
1955
+ height?: number
1956
+ initialRoute?: Route
1957
+ fail?: boolean
1958
+ mode?: "dark" | "light"
1959
+ single?: boolean
1960
+ readImage?: (file: string, signal: AbortSignal) => Promise<Uint8Array>
1961
+ onSessionTab?: () => void
1962
+ keybinds?: TuiKeybind.KeybindOverrides
1963
+ kittyKeyboard?: boolean
1964
+ source?: "branch" | "committed" | "working" | "turn"
1965
+ base?: typeof baseFixture | null
1966
+ open?: boolean
1967
+ pending?: boolean
1968
+ baseResponse?: () => Promise<Response>
1969
+ diffResponse?: (url: URL) => Promise<Response>
1970
+ branchesResponse?: (url: URL) => Promise<Response>
1971
+ state?: string
1972
+ } = {},
1973
+ ) {
1974
+ const commands = new Map<string, KeymapCommand>()
1975
+ const [current, setCurrent] = createSignal<Route>(options.initialRoute ?? startRoute)
1976
+ const [sessionLocation, setSessionLocation] = createSignal(session.location)
1977
+ const [branch, setBranch] = createSignal("feature")
1978
+ const [sessionStatus, setSessionStatus] = createSignal<"idle" | "running">("idle")
1979
+ const state = options.state ?? path.join(temporary.path, crypto.randomUUID())
1980
+ let renderDiff: Page["render"] | undefined
1981
+ let renderCommands: SlotClaim<"app">["render"] | undefined
1982
+ let vcsDiffInput: unknown
1983
+ let imageReadInput: unknown
1984
+ let shortcut: (command: string) => string | undefined = () => undefined
1985
+ const stored: { info: Info } = {
1986
+ info: { keybinds: options.keybinds, diffs: { source: options.source, single: options.single } },
1987
+ }
1988
+ const writes: Info[] = []
1989
+ const baseRequests: URL[] = []
1990
+ const diffRequests: URL[] = []
1991
+ const turnDiffRequests: URL[] = []
1992
+ const branchesRequests: URL[] = []
1993
+ const mutationRequests: URL[] = []
1994
+ const config = createTuiResolvedConfig(stored.info)
1995
+ const transport = createFetch(async (url, request) => {
1996
+ if (request.method !== "GET") mutationRequests.push(url)
1997
+ if (url.pathname.startsWith("/api/fs/read/")) {
1998
+ const file = decodeURIComponent(url.pathname.slice("/api/fs/read/".length))
1999
+ imageReadInput = {
2000
+ path: file,
2001
+ location: { directory: url.searchParams.get("location[directory]") },
2002
+ }
2003
+ return new Response(
2004
+ options.readImage ? (await options.readImage(file, request.signal)).slice() : diffImageFixture,
2005
+ )
2006
+ }
2007
+ if (url.pathname === "/api/vcs/base") {
2008
+ baseRequests.push(url)
2009
+ return options.baseResponse
2010
+ ? options.baseResponse()
2011
+ : json({ location: session.location, data: options.base === undefined ? baseFixture : options.base })
2012
+ }
2013
+ if (url.pathname === "/api/vcs/branch") {
2014
+ branchesRequests.push(url)
2015
+ return options.branchesResponse
2016
+ ? options.branchesResponse(url)
2017
+ : json({
2018
+ location: session.location,
2019
+ data: ["v2", "release", "origin/release"].filter((name) =>
2020
+ name.includes(url.searchParams.get("search") ?? ""),
2021
+ ),
2022
+ })
2023
+ }
2024
+ if (url.pathname === "/api/session/session-1/diff") {
2025
+ turnDiffRequests.push(url)
2026
+ if (options.fail) return json({ message: "boom" }, { status: 500 })
2027
+ return json({ data: vcsDiff })
2028
+ }
2029
+ if (url.pathname !== "/api/vcs/diff") return
2030
+ diffRequests.push(url)
2031
+ vcsDiffInput = {
2032
+ location: { directory: url.searchParams.get("location[directory]") },
2033
+ mode: url.searchParams.get("mode"),
2034
+ context: url.searchParams.get("context"),
2035
+ ...(url.searchParams.has("base") ? { base: url.searchParams.get("base") } : {}),
2036
+ }
2037
+ if (options.diffResponse) return options.diffResponse(url)
2038
+ if (options.fail) return json({ message: "boom" }, { status: 500 })
2039
+ return json({
2040
+ location: { directory: "/repo/session", project: { id: "project-1", directory: "/repo/session" } },
2041
+ data: vcsDiff,
2042
+ })
2043
+ }, createEventStream())
2044
+ function Harness() {
2045
+ let theme: ReturnType<ReturnType<typeof useThemes>["currentTokens"]>
2046
+ function Content() {
2047
+ const dialog = useDialog()
2048
+ const keymap = Keymap.use()
2049
+ const shortcuts = Keymap.useShortcuts()
2050
+ if (options.onSessionTab) {
2051
+ Keymap.createLayer(() => ({
2052
+ mode: "global",
2053
+ commands: ["session.tab.next", "session.tab.previous"].map((id) => ({
2054
+ id,
2055
+ title: id,
2056
+ run: () => options.onSessionTab?.(),
2057
+ })),
2058
+ }))
2059
+ }
2060
+ shortcut = shortcuts.get
2061
+ theme = useThemes().currentTokens()
2062
+ const context = {
2063
+ options: {},
2064
+ storage: useStorage(),
2065
+ client: createApi(transport.fetch),
2066
+ data: {
2067
+ session: { get: () => ({ ...session, location: sessionLocation() }), status: sessionStatus },
2068
+ location: {
2069
+ default: () => ({ directory: "/repo/default" }),
2070
+ vcs: { info: () => ({ branch: { current: branch() } }) },
2071
+ },
2072
+ },
2073
+ get theme() {
2074
+ return theme
2075
+ },
2076
+ keymap: {
2077
+ layer(input: () => KeymapLayer) {
2078
+ input().commands?.forEach((command) => {
2079
+ if (command.id) commands.set(command.id, command)
2080
+ })
2081
+ Keymap.createLayer(input)
2082
+ },
2083
+ dispatch: keymap.dispatch,
2084
+ shortcuts: shortcuts.list,
2085
+ mode: keymap.mode,
2086
+ },
2087
+ ui: {
2088
+ dialog: createDialogApi(dialog, (render) => render()),
2089
+ panel: { open: () => false, close: () => {}, current: () => undefined },
2090
+ router: {
2091
+ register(page: Page) {
2092
+ if (page.name === "diff") renderDiff = page.render
2093
+ return () => {}
2094
+ },
2095
+ navigate(destination: Destination) {
2096
+ setCurrent(
2097
+ destination.type === "plugin" && !("id" in destination)
2098
+ ? { ...destination, id: "opencode.diffs" }
2099
+ : destination,
2100
+ )
2101
+ },
2102
+ current,
2103
+ },
2104
+ slot(claim: SlotClaim<"app">) {
2105
+ renderCommands = claim.render
2106
+ return () => {}
2107
+ },
2108
+ },
2109
+ } as unknown as Context
2110
+
2111
+ void diffViewerPlugin.setup(context)
2112
+ const commandView = renderCommands?.({})
2113
+ return (
2114
+ <>
2115
+ {commandView}
2116
+ <Show when={current().type === "plugin" ? current() : undefined} keyed>
2117
+ {(route) => renderDiff?.({ data: route.type === "plugin" ? route.data : undefined })}
2118
+ </Show>
2119
+ </>
2120
+ )
2121
+ }
2122
+
2123
+ return (
2124
+ <TestTuiContexts paths={{ state }}>
2125
+ <TuiAppProvider value={{ name: "test", version: "test", channel: "test" }}>
2126
+ <StorageProvider>
2127
+ <ConfigProvider
2128
+ config={config}
2129
+ service={{
2130
+ get: async () => stored.info,
2131
+ update: async (update) => {
2132
+ const next = structuredClone(stored.info)
2133
+ update(next)
2134
+ stored.info = next
2135
+ writes.push(next)
2136
+ return next
2137
+ },
2138
+ }}
2139
+ >
2140
+ <Keymap.Provider>
2141
+ <ToastProvider>
2142
+ <ThemeProvider mode={options.mode ?? "dark"} source={emptyThemeSource}>
2143
+ <DialogProvider>
2144
+ <Content />
2145
+ </DialogProvider>
2146
+ </ThemeProvider>
2147
+ </ToastProvider>
2148
+ </Keymap.Provider>
2149
+ </ConfigProvider>
2150
+ </StorageProvider>
2151
+ </TuiAppProvider>
2152
+ </TestTuiContexts>
2153
+ )
2154
+ }
2155
+
2156
+ const app = await testRender(() => <Harness />, {
2157
+ width: options.width ?? 80,
2158
+ height: options.height ?? 20,
2159
+ kittyKeyboard: options.kittyKeyboard,
2160
+ })
2161
+ await app.waitFor(() => commands.has("diff.open"))
2162
+ if (options.open !== false && current().type !== "plugin") commands.get("diff.open")!.run()
2163
+ if (options.open !== false) await app.waitFor(() => commands.has("diff.close"))
2164
+ await app.flush()
2165
+ if (!options.pending) await app.waitForFrame((frame) => !frame.includes("Loading diff"))
2166
+ return {
2167
+ app,
2168
+ commands,
2169
+ current,
2170
+ shortcut: (command: string) => shortcut(command),
2171
+ vcsDiffInput: () => vcsDiffInput,
2172
+ imageReadInput: () => imageReadInput,
2173
+ baseRequests,
2174
+ diffRequests,
2175
+ turnDiffRequests,
2176
+ branchesRequests,
2177
+ mutationRequests,
2178
+ setSessionLocation,
2179
+ setSessionStatus,
2180
+ setBranch,
2181
+ state,
2182
+ writes,
2183
+ settings: () => stored.info,
2184
+ }
2185
+ }
2186
+
2187
+ const startRoute: Route = { type: "session", sessionID: "session-1" }
2188
+
2189
+ const baseFixture = { name: "v2", ref: "refs/heads/v2", source: "default" }
2190
+
2191
+ const disabledDiffKeybinds = {
2192
+ "diff.down": "none",
2193
+ "diff.up": "none",
2194
+ "diff.page.down": "none",
2195
+ "diff.page.up": "none",
2196
+ "diff.half_page.down": "none",
2197
+ "diff.half_page.up": "none",
2198
+ "diff.mark_reviewed": "none",
2199
+ } satisfies TuiKeybind.KeybindOverrides
2200
+
2201
+ const hunkDiff = [
2202
+ {
2203
+ file: "src/file.txt",
2204
+ additions: 3,
2205
+ deletions: 3,
2206
+ status: "modified",
2207
+ patch: `--- a/src/file.txt
2208
+ +++ b/src/file.txt
2209
+ @@ -1,3 +1,3 @@
2210
+ const first = true
2211
+ -const oldFirst = true
2212
+ +const newFirst = true
2213
+ const afterFirst = true
2214
+ @@ -20,3 +20,3 @@
2215
+ const second = true
2216
+ -const oldSecond = true
2217
+ +const newSecond = true
2218
+ const afterSecond = true
2219
+ @@ -40,3 +40,3 @@
2220
+ const third = true
2221
+ -const oldThird = true
2222
+ +const newThird = true
2223
+ const afterThird = true`,
2224
+ },
2225
+ ]
2226
+
2227
+ const manyDiffs = Array.from({ length: 40 }, (_, index) => ({
2228
+ ...hunkDiff[0],
2229
+ file: `file${String(index).padStart(2, "0")}.txt`,
2230
+ }))
2231
+
2232
+ test.each([80, 160])("virtualizes a large added file at %i columns without losing its end", async (width) => {
2233
+ const lines = [
2234
+ "+{",
2235
+ ...Array.from(
2236
+ { length: 7500 },
2237
+ (_, index) =>
2238
+ `+ "row-${String(index).padStart(4, "0")}": "${"value".repeat(index === 777 ? 2000 : index % 7 === 0 ? 24 : 1)}"${index === 7499 ? "" : ","}`,
2239
+ ),
2240
+ "+}",
2241
+ ]
2242
+ const viewer = await renderDiffViewer(
2243
+ [
2244
+ {
2245
+ file: "snapshot.json",
2246
+ status: "added",
2247
+ additions: lines.length,
2248
+ deletions: 0,
2249
+ patch: `diff --git a/snapshot.json b/snapshot.json\nnew file mode 100644\n--- /dev/null\n+++ b/snapshot.json\n@@ -0,0 +1,${lines.length} @@\n${lines.join("\n")}`,
2250
+ },
2251
+ ],
2252
+ { width, height: 24 },
2253
+ )
2254
+ try {
2255
+ expect(viewer.app.captureCharFrame()).toContain("row-0000")
2256
+ expect(
2257
+ findDiffs(viewer.app.renderer.root).reduce((total, node) => total + node.diff.split("\n").length, 0),
2258
+ ).toBeLessThan(2000)
2259
+ viewer.commands.get("diff.last")!.run()
2260
+ await viewer.app.flush()
2261
+ if (!viewer.app.captureCharFrame().includes("row-7499")) {
2262
+ await viewer.app.waitForFrame((frame) => frame.includes("row-7499"))
2263
+ }
2264
+ expect(viewer.app.captureCharFrame()).toContain("row-7499")
2265
+ expect(
2266
+ findDiffs(viewer.app.renderer.root).reduce((total, node) => total + node.diff.split("\n").length, 0),
2267
+ ).toBeLessThan(2000)
2268
+ viewer.commands.get("diff.first")!.run()
2269
+ await viewer.app.flush()
2270
+ expect(viewer.app.captureCharFrame()).toContain("row-0000")
2271
+ viewer.app.resize(width === 80 ? 160 : 80, 20)
2272
+ await viewer.app.flush()
2273
+ viewer.commands.get("diff.last")!.run()
2274
+ await viewer.app.flush()
2275
+ expect(viewer.app.captureCharFrame()).toContain("row-7499")
2276
+ } finally {
2277
+ viewer.app.renderer.destroy()
2278
+ }
2279
+ })
2280
+
2281
+ test("keeps the line-number gutter the same width across virtual chunks", async () => {
2282
+ const additions = Array.from({ length: 10500 }, (_, index) => `+line-${String(index + 1).padStart(5, "0")}`)
2283
+ const viewer = await renderDiffViewer(
2284
+ [
2285
+ {
2286
+ file: "wide.txt",
2287
+ status: "added",
2288
+ additions: additions.length,
2289
+ deletions: 0,
2290
+ patch: `--- /dev/null\n+++ b/wide.txt\n@@ -0,0 +1,${additions.length} @@\n${additions.join("\n")}`,
2291
+ },
2292
+ ],
2293
+ { width: 120, height: 24 },
2294
+ )
2295
+ const column = (text: string) =>
2296
+ viewer.app
2297
+ .captureCharFrame()
2298
+ .split("\n")
2299
+ .find((line) => line.includes(text))
2300
+ ?.indexOf(text)
2301
+ try {
2302
+ await viewer.app.flush()
2303
+ const top = column("line-00001")
2304
+ viewer.commands.get("diff.last")!.run()
2305
+ await viewer.app.flush()
2306
+ if (!viewer.app.captureCharFrame().includes("line-10500")) {
2307
+ await viewer.app.waitForFrame((frame) => frame.includes("line-10500"))
2308
+ }
2309
+ expect(top).toBeDefined()
2310
+ expect(column("line-10500")).toBe(top)
2311
+ } finally {
2312
+ viewer.app.renderer.destroy()
2313
+ }
2314
+ })
2315
+
2316
+ test("highlights virtual chunks with whole-file syntax context", async () => {
2317
+ const additions = Array.from({ length: 3200 }, (_, index) => {
2318
+ if (index === 370) return "+/*"
2319
+ if (index === 399) return "+*/"
2320
+ if (index > 370 && index < 399) return `+ comment ${index}`
2321
+ return `+const value${index} = ${index}`
2322
+ })
2323
+ const viewer = await renderDiffViewer(
2324
+ [
2325
+ {
2326
+ file: "big.ts",
2327
+ status: "added",
2328
+ additions: additions.length,
2329
+ deletions: 0,
2330
+ patch: `--- /dev/null\n+++ b/big.ts\n@@ -0,0 +1,${additions.length} @@\n${additions.join("\n")}`,
2331
+ },
2332
+ ],
2333
+ { width: 120, height: 40 },
2334
+ )
2335
+ const color = (text: string) =>
2336
+ viewer.app
2337
+ .captureSpans()
2338
+ .lines.flatMap((line) => line.spans)
2339
+ .find((span) => span.text.includes(text))?.fg
2340
+ try {
2341
+ findScrollBox(viewer.app.renderer.root)!.scrollTo(360)
2342
+ await viewer.app.flush()
2343
+ // The comment starts in the first chunk and ends in the second; wait until it is highlighted.
2344
+ for (let attempt = 0; attempt < 100 && `${color("comment 375")}` === `${color("value365")}`; attempt++) {
2345
+ await Bun.sleep(20)
2346
+ await viewer.app.flush()
2347
+ }
2348
+ expect(`${color("comment 375")}`).not.toBe(`${color("value365")}`)
2349
+ for (let attempt = 0; attempt < 100 && `${color("comment 390")}` !== `${color("comment 375")}`; attempt++) {
2350
+ await Bun.sleep(20)
2351
+ await viewer.app.flush()
2352
+ }
2353
+ expect(`${color("comment 390")}`).toBe(`${color("comment 375")}`)
2354
+ } finally {
2355
+ viewer.app.renderer.destroy()
2356
+ }
2357
+ })
2358
+
2359
+ test("does not virtualize added files at or below the size threshold", async () => {
2360
+ const additions = Array.from({ length: 3000 }, (_, index) => `+small line ${index}`)
2361
+ const viewer = await renderDiffViewer(
2362
+ [
2363
+ {
2364
+ file: "small.txt",
2365
+ status: "added",
2366
+ additions: additions.length,
2367
+ deletions: 0,
2368
+ patch: `--- /dev/null\n+++ b/small.txt\n@@ -0,0 +1,${additions.length} @@\n${additions.join("\n")}`,
2369
+ },
2370
+ ],
2371
+ { width: 120, height: 24 },
2372
+ )
2373
+ try {
2374
+ expect(findDiffs(viewer.app.renderer.root)).toHaveLength(1)
2375
+ } finally {
2376
+ viewer.app.renderer.destroy()
2377
+ }
2378
+ })
2379
+
2380
+ test("file navigation and review still work after a virtualized patch", async () => {
2381
+ const additions = Array.from({ length: 6600 }, (_, index) => `+added line ${index}`)
2382
+ const viewer = await renderDiffViewer(
2383
+ [
2384
+ {
2385
+ file: "a-large.txt",
2386
+ status: "added",
2387
+ additions: additions.length,
2388
+ deletions: 0,
2389
+ patch: `--- /dev/null\n+++ b/a-large.txt\n@@ -0,0 +1,${additions.length} @@\n${additions.join("\n")}`,
2390
+ },
2391
+ { ...hunkDiff[0], file: "b-small.txt" },
2392
+ ],
2393
+ { width: 160, height: 24 },
2394
+ )
2395
+ try {
2396
+ const scroll = findScrollBox(viewer.app.renderer.root)!
2397
+ scroll.scrollTo(2700)
2398
+ await viewer.app.flush()
2399
+ viewer.commands.get("diff.previous_hunk")!.run()
2400
+ await viewer.app.flush()
2401
+ expect(viewer.app.captureCharFrame()).toContain("added line 0")
2402
+ viewer.commands.get("diff.next_hunk")!.run()
2403
+ await viewer.app.flush()
2404
+ expect(viewer.app.captureCharFrame()).toContain("b-small.txt")
2405
+ viewer.commands.get("diff.next_file")!.run()
2406
+ await viewer.app.flush()
2407
+ expect(viewer.app.captureCharFrame()).toContain("b-small.txt")
2408
+ expect(viewer.app.captureCharFrame()).toContain("const first")
2409
+ viewer.commands.get("diff.previous_file")!.run()
2410
+ await viewer.app.flush()
2411
+ expect(viewer.app.captureCharFrame()).toContain("a-large.txt")
2412
+ viewer.commands.get("diff.mark_reviewed")!.run()
2413
+ await viewer.app.flush()
2414
+ expect(viewer.app.captureCharFrame()).not.toContain("added line 0")
2415
+ viewer.commands.get("diff.mark_reviewed")!.run()
2416
+ await viewer.app.flush()
2417
+ expect(viewer.app.captureCharFrame()).toContain("added line 0")
2418
+ } finally {
2419
+ viewer.app.renderer.destroy()
2420
+ }
2421
+ })
2422
+
2423
+ function findScrollBox(root: Renderable, patches = true): ScrollBoxRenderable | undefined {
2424
+ const node = root.findDescendantById(patches ? "diff-patches" : "diff-files")
2425
+ return node instanceof ScrollBoxRenderable ? node : undefined
2426
+ }
2427
+
2428
+ function findDiffs(root: Renderable): DiffRenderable[] {
2429
+ return root instanceof DiffRenderable ? [root] : root.getChildren().flatMap(findDiffs)
2430
+ }
2431
+
2432
+ const session = {
2433
+ id: "session-1",
2434
+ projectID: "project-1",
2435
+ location: { directory: "/repo/session" },
2436
+ title: "Session",
2437
+ cost: { currency: "USD", amount: 0 },
2438
+ tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
2439
+ time: {
2440
+ created: 0,
2441
+ updated: 0,
2442
+ },
2443
+ }
2444
+
2445
+ test.each([100, 160])("the sidebar source picker switches VCS sources at %i columns", async (width) => {
2446
+ const viewer = await renderDiffViewer(hunkDiff, {
2447
+ width,
2448
+ kittyKeyboard: true,
2449
+ initialRoute: {
2450
+ type: "plugin",
2451
+ id: "opencode.diffs",
2452
+ name: "diff",
2453
+ data: { mode: "branch", sessionID: "session-1", returnRoute: startRoute },
2454
+ },
2455
+ })
2456
+ try {
2457
+ expect(viewer.current()).toEqual({
2458
+ type: "plugin",
2459
+ id: "opencode.diffs",
2460
+ name: "diff",
2461
+ data: { mode: "branch", sessionID: "session-1", returnRoute: startRoute },
2462
+ })
2463
+ expect(viewer.vcsDiffInput()).toEqual({
2464
+ location: { directory: "/repo/session" },
2465
+ mode: "branch",
2466
+ base: "refs/heads/v2",
2467
+ context: "12",
2468
+ })
2469
+ await viewer.app.waitForFrame((frame) => frame.includes("const first"))
2470
+ await viewer.app.flush()
2471
+ const source = () => viewer.app.renderer.root.findDescendantById("diff-source-switch")!
2472
+ expect(source().y).toBe(1)
2473
+ expect(viewer.app.captureCharFrame().split("\n")[1]).toContain("All")
2474
+ await viewer.app.mockMouse.click(source().x, source().y, MouseButton.RIGHT)
2475
+ await viewer.app.flush()
2476
+ expect(viewer.app.captureCharFrame()).not.toContain("Diff source")
2477
+ await viewer.app.mockMouse.click(source().x, source().y)
2478
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
2479
+ expect(viewer.app.renderer.getSelection()).toBeNull()
2480
+ viewer.app.mockInput.pressKey("HOME")
2481
+ viewer.app.mockInput.pressArrow("down")
2482
+ viewer.app.mockInput.pressArrow("down")
2483
+ await viewer.app.flush()
2484
+ viewer.app.mockInput.pressEnter()
2485
+ await viewer.app.waitForFrame(
2486
+ (frame) => !frame.includes("Diff source") && frame.includes("Uncommitted") && frame.includes("const first"),
2487
+ )
2488
+ expect(viewer.vcsDiffInput()).toEqual({ location: { directory: "/repo/session" }, mode: "working", context: "12" })
2489
+ // The header resized with the new label; render once more so the click hits its new position.
2490
+ await viewer.app.flush()
2491
+ await viewer.app.mockMouse.click(source().x, source().y)
2492
+ await viewer.app.waitForFrame((frame) => frame.includes("Diff source"))
2493
+ viewer.app.mockInput.pressKey("HOME")
2494
+ await viewer.app.flush()
2495
+ viewer.app.mockInput.pressEnter()
2496
+ await viewer.app.waitForFrame(
2497
+ (frame) => !frame.includes("Diff source") && frame.includes("All") && frame.includes("const first"),
2498
+ )
2499
+ expect(viewer.vcsDiffInput()).toEqual({
2500
+ location: { directory: "/repo/session" },
2501
+ mode: "branch",
2502
+ context: "12",
2503
+ base: "refs/heads/v2",
2504
+ })
2505
+ expect(viewer.baseRequests).toHaveLength(1)
2506
+ expect(viewer.writes).toHaveLength(0)
2507
+ } finally {
2508
+ viewer.app.renderer.destroy()
2509
+ }
2510
+ })