@neurocode-ai/tui 1.18.8

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 (241) hide show
  1. package/bunfig.toml +4 -0
  2. package/package.json +72 -0
  3. package/src/app.tsx +1134 -0
  4. package/src/attention.ts +260 -0
  5. package/src/audio.d.ts +9 -0
  6. package/src/audio.ts +53 -0
  7. package/src/clipboard.ts +124 -0
  8. package/src/component/bg-pulse-render.ts +436 -0
  9. package/src/component/bg-pulse.tsx +99 -0
  10. package/src/component/command-palette.tsx +79 -0
  11. package/src/component/dialog-agent.tsx +31 -0
  12. package/src/component/dialog-console-org.tsx +135 -0
  13. package/src/component/dialog-debug.tsx +90 -0
  14. package/src/component/dialog-mcp.tsx +85 -0
  15. package/src/component/dialog-model.tsx +197 -0
  16. package/src/component/dialog-move-session.tsx +353 -0
  17. package/src/component/dialog-provider.tsx +469 -0
  18. package/src/component/dialog-retry-action.tsx +160 -0
  19. package/src/component/dialog-session-delete-failed.tsx +99 -0
  20. package/src/component/dialog-session-list.tsx +364 -0
  21. package/src/component/dialog-session-rename.tsx +31 -0
  22. package/src/component/dialog-skill.tsx +70 -0
  23. package/src/component/dialog-stash.tsx +87 -0
  24. package/src/component/dialog-status.tsx +168 -0
  25. package/src/component/dialog-tag.tsx +47 -0
  26. package/src/component/dialog-theme-list.tsx +50 -0
  27. package/src/component/dialog-variant.tsx +39 -0
  28. package/src/component/dialog-workspace-create.tsx +308 -0
  29. package/src/component/dialog-workspace-file-changes.tsx +144 -0
  30. package/src/component/dialog-workspace-list.tsx +112 -0
  31. package/src/component/dialog-workspace-unavailable.tsx +69 -0
  32. package/src/component/error-component.tsx +240 -0
  33. package/src/component/logo.tsx +61 -0
  34. package/src/component/plugin-route-missing.tsx +14 -0
  35. package/src/component/prompt/autocomplete.tsx +781 -0
  36. package/src/component/prompt/cwd.ts +0 -0
  37. package/src/component/prompt/frecency.tsx +1 -0
  38. package/src/component/prompt/history.tsx +1 -0
  39. package/src/component/prompt/index.tsx +1713 -0
  40. package/src/component/prompt/local-attachment.ts +48 -0
  41. package/src/component/prompt/move.tsx +205 -0
  42. package/src/component/prompt/stash.tsx +1 -0
  43. package/src/component/prompt/workspace.tsx +137 -0
  44. package/src/component/register-spinner.ts +6 -0
  45. package/src/component/spinner.tsx +26 -0
  46. package/src/component/startup-loading.tsx +63 -0
  47. package/src/component/todo-item.tsx +32 -0
  48. package/src/component/use-connected.tsx +12 -0
  49. package/src/component/workspace-label.tsx +19 -0
  50. package/src/config/index.tsx +129 -0
  51. package/src/config/keybind.ts +471 -0
  52. package/src/context/args.tsx +16 -0
  53. package/src/context/clipboard.tsx +18 -0
  54. package/src/context/data.tsx +569 -0
  55. package/src/context/directory.ts +17 -0
  56. package/src/context/editor.ts +408 -0
  57. package/src/context/epilogue.tsx +6 -0
  58. package/src/context/event.ts +36 -0
  59. package/src/context/exit.tsx +8 -0
  60. package/src/context/helper.tsx +26 -0
  61. package/src/context/kv.tsx +66 -0
  62. package/src/context/local.tsx +542 -0
  63. package/src/context/location.tsx +14 -0
  64. package/src/context/path-format.tsx +24 -0
  65. package/src/context/permission.tsx +26 -0
  66. package/src/context/project.tsx +115 -0
  67. package/src/context/prompt.tsx +18 -0
  68. package/src/context/route.tsx +60 -0
  69. package/src/context/runtime.tsx +62 -0
  70. package/src/context/sdk.tsx +151 -0
  71. package/src/context/sync.tsx +666 -0
  72. package/src/context/theme.tsx +332 -0
  73. package/src/context/thinking.ts +67 -0
  74. package/src/editor-zed.ts +286 -0
  75. package/src/editor.ts +101 -0
  76. package/src/feature-plugins/builtins.ts +36 -0
  77. package/src/feature-plugins/home/footer.tsx +100 -0
  78. package/src/feature-plugins/home/tips-view.tsx +287 -0
  79. package/src/feature-plugins/home/tips.tsx +59 -0
  80. package/src/feature-plugins/sidebar/context.tsx +65 -0
  81. package/src/feature-plugins/sidebar/files.tsx +70 -0
  82. package/src/feature-plugins/sidebar/footer.tsx +98 -0
  83. package/src/feature-plugins/sidebar/lsp.tsx +65 -0
  84. package/src/feature-plugins/sidebar/mcp.tsx +97 -0
  85. package/src/feature-plugins/sidebar/todo.tsx +49 -0
  86. package/src/feature-plugins/system/diff-viewer-file-tree-utils.ts +232 -0
  87. package/src/feature-plugins/system/diff-viewer-file-tree.tsx +162 -0
  88. package/src/feature-plugins/system/diff-viewer-ui.tsx +103 -0
  89. package/src/feature-plugins/system/diff-viewer.tsx +1077 -0
  90. package/src/feature-plugins/system/notifications.ts +94 -0
  91. package/src/feature-plugins/system/plugins.tsx +269 -0
  92. package/src/feature-plugins/system/which-key.tsx +608 -0
  93. package/src/index.tsx +1 -0
  94. package/src/keymap.tsx +290 -0
  95. package/src/logo.ts +11 -0
  96. package/src/parsers-config.ts +386 -0
  97. package/src/plugin/adapters.tsx +355 -0
  98. package/src/plugin/api.ts +52 -0
  99. package/src/plugin/command-shim.ts +109 -0
  100. package/src/plugin/runtime.tsx +81 -0
  101. package/src/plugin/slots.tsx +65 -0
  102. package/src/prompt/display.ts +48 -0
  103. package/src/prompt/frecency.tsx +80 -0
  104. package/src/prompt/history.tsx +111 -0
  105. package/src/prompt/part.ts +29 -0
  106. package/src/prompt/stash.tsx +89 -0
  107. package/src/prompt/traits.ts +29 -0
  108. package/src/routes/home/session-destination.tsx +41 -0
  109. package/src/routes/home.tsx +95 -0
  110. package/src/routes/session/dialog-fork-from-timeline.tsx +76 -0
  111. package/src/routes/session/dialog-message.tsx +109 -0
  112. package/src/routes/session/dialog-subagent.tsx +26 -0
  113. package/src/routes/session/dialog-timeline.tsx +47 -0
  114. package/src/routes/session/footer.tsx +91 -0
  115. package/src/routes/session/index.tsx +2710 -0
  116. package/src/routes/session/permission.tsx +718 -0
  117. package/src/routes/session/question.tsx +514 -0
  118. package/src/routes/session/sidebar.tsx +103 -0
  119. package/src/routes/session/subagent-footer.tsx +132 -0
  120. package/src/runtime.tsx +9 -0
  121. package/src/terminal-win32.ts +130 -0
  122. package/src/theme/assets/aura.json +69 -0
  123. package/src/theme/assets/ayu.json +80 -0
  124. package/src/theme/assets/carbonfox.json +248 -0
  125. package/src/theme/assets/catppuccin-frappe.json +230 -0
  126. package/src/theme/assets/catppuccin-macchiato.json +230 -0
  127. package/src/theme/assets/catppuccin.json +112 -0
  128. package/src/theme/assets/cobalt2.json +225 -0
  129. package/src/theme/assets/cursor.json +249 -0
  130. package/src/theme/assets/dracula.json +219 -0
  131. package/src/theme/assets/everforest.json +241 -0
  132. package/src/theme/assets/flexoki.json +237 -0
  133. package/src/theme/assets/github.json +233 -0
  134. package/src/theme/assets/gruvbox.json +242 -0
  135. package/src/theme/assets/kanagawa.json +77 -0
  136. package/src/theme/assets/lucent-orng.json +234 -0
  137. package/src/theme/assets/material.json +235 -0
  138. package/src/theme/assets/matrix.json +77 -0
  139. package/src/theme/assets/mercury.json +252 -0
  140. package/src/theme/assets/monokai.json +221 -0
  141. package/src/theme/assets/nightowl.json +221 -0
  142. package/src/theme/assets/nord.json +223 -0
  143. package/src/theme/assets/one-dark.json +84 -0
  144. package/src/theme/assets/opencode.json +245 -0
  145. package/src/theme/assets/orng.json +249 -0
  146. package/src/theme/assets/osaka-jade.json +93 -0
  147. package/src/theme/assets/palenight.json +222 -0
  148. package/src/theme/assets/rosepine.json +234 -0
  149. package/src/theme/assets/solarized.json +223 -0
  150. package/src/theme/assets/synthwave84.json +226 -0
  151. package/src/theme/assets/tokyonight.json +243 -0
  152. package/src/theme/assets/vercel.json +245 -0
  153. package/src/theme/assets/vesper.json +218 -0
  154. package/src/theme/assets/zenburn.json +223 -0
  155. package/src/theme/index.ts +1089 -0
  156. package/src/ui/border.ts +21 -0
  157. package/src/ui/dialog-alert.tsx +66 -0
  158. package/src/ui/dialog-confirm.tsx +108 -0
  159. package/src/ui/dialog-export-options.tsx +217 -0
  160. package/src/ui/dialog-help.tsx +40 -0
  161. package/src/ui/dialog-prompt.tsx +126 -0
  162. package/src/ui/dialog-select.tsx +790 -0
  163. package/src/ui/dialog.tsx +231 -0
  164. package/src/ui/link.tsx +34 -0
  165. package/src/ui/spinner.ts +368 -0
  166. package/src/ui/toast.tsx +102 -0
  167. package/src/util/collapse-tool-output.ts +19 -0
  168. package/src/util/error.ts +182 -0
  169. package/src/util/filetype.ts +130 -0
  170. package/src/util/format.ts +20 -0
  171. package/src/util/layout.ts +25 -0
  172. package/src/util/locale.ts +86 -0
  173. package/src/util/model.ts +28 -0
  174. package/src/util/path.ts +12 -0
  175. package/src/util/persistence.ts +33 -0
  176. package/src/util/presentation.ts +38 -0
  177. package/src/util/provider-origin.ts +7 -0
  178. package/src/util/record.ts +3 -0
  179. package/src/util/renderer.ts +7 -0
  180. package/src/util/revert-diff.ts +18 -0
  181. package/src/util/scroll.ts +27 -0
  182. package/src/util/selection.ts +79 -0
  183. package/src/util/session.ts +3 -0
  184. package/src/util/signal.ts +51 -0
  185. package/src/util/system.ts +20 -0
  186. package/src/util/tool-display.ts +13 -0
  187. package/src/util/transcript.ts +112 -0
  188. package/sst-env.d.ts +10 -0
  189. package/test/app-lifecycle.test.tsx +128 -0
  190. package/test/cli/cmd/tui/dialog-workspace-create.test.ts +28 -0
  191. package/test/cli/cmd/tui/model-options.test.ts +32 -0
  192. package/test/cli/cmd/tui/notifications.test.ts +267 -0
  193. package/test/cli/cmd/tui/provider-options.test.ts +41 -0
  194. package/test/cli/cmd/tui/sync-fixture.tsx +70 -0
  195. package/test/cli/cmd/tui/sync-live-hydration.test.tsx +262 -0
  196. package/test/cli/cmd/tui/sync-undefined-messages.test.tsx +43 -0
  197. package/test/cli/cmd/tui/sync.test.tsx +65 -0
  198. package/test/cli/tui/__snapshots__/inline-tool-wrap-snapshot.test.tsx.snap +92 -0
  199. package/test/cli/tui/data.test.tsx +486 -0
  200. package/test/cli/tui/dialog-prompt.test.tsx +147 -0
  201. package/test/cli/tui/diff-viewer-file-tree.test.tsx +200 -0
  202. package/test/cli/tui/diff-viewer.test.tsx +268 -0
  203. package/test/cli/tui/inline-tool-wrap-snapshot.test.tsx +351 -0
  204. package/test/cli/tui/prompt-submit-race.test.ts +98 -0
  205. package/test/cli/tui/thinking.test.ts +36 -0
  206. package/test/cli/tui/use-event.test.tsx +148 -0
  207. package/test/clipboard.test.ts +19 -0
  208. package/test/component/dialog-session-list.test.ts +46 -0
  209. package/test/config.test.tsx +127 -0
  210. package/test/context/local.test.ts +22 -0
  211. package/test/editor.test.ts +32 -0
  212. package/test/feature-plugins/diff-viewer-file-tree-utils.test.ts +323 -0
  213. package/test/fixture/fixture.ts +13 -0
  214. package/test/fixture/tui-environment.tsx +32 -0
  215. package/test/fixture/tui-plugin.ts +36 -0
  216. package/test/fixture/tui-runtime.ts +12 -0
  217. package/test/fixture/tui-sdk.ts +109 -0
  218. package/test/index.test.tsx +6 -0
  219. package/test/keymap.test.tsx +141 -0
  220. package/test/plugin/runtime.test.ts +50 -0
  221. package/test/plugin/slots.test.tsx +38 -0
  222. package/test/prompt/display.test.ts +33 -0
  223. package/test/prompt/history.test.ts +39 -0
  224. package/test/prompt/jsonl.test.ts +24 -0
  225. package/test/prompt/local-attachment.test.ts +43 -0
  226. package/test/prompt/part.test.ts +53 -0
  227. package/test/prompt/persistence.test.ts +23 -0
  228. package/test/prompt/traits.test.ts +25 -0
  229. package/test/runtime.test.tsx +37 -0
  230. package/test/theme.test.ts +81 -0
  231. package/test/util/error.test.ts +49 -0
  232. package/test/util/filetype.test.ts +16 -0
  233. package/test/util/format.test.ts +59 -0
  234. package/test/util/model.test.ts +9 -0
  235. package/test/util/presentation.test.ts +8 -0
  236. package/test/util/renderer.test.ts +30 -0
  237. package/test/util/revert-diff.test.ts +35 -0
  238. package/test/util/session.test.ts +10 -0
  239. package/test/util/tool-display.test.ts +40 -0
  240. package/test/util/transcript.test.ts +421 -0
  241. package/tsconfig.json +10 -0
@@ -0,0 +1,46 @@
1
+ import { describe, expect, test } from "bun:test"
2
+ import { createDialogSessionListQuery, loadDialogSessionList } from "../../src/component/dialog-session-list"
3
+
4
+ describe("dialog session list", () => {
5
+ test("requests root sessions for the default browse list", () => {
6
+ expect(createDialogSessionListQuery({ filter: { path: "packages/tui" } })).toEqual({
7
+ roots: true,
8
+ limit: 100,
9
+ path: "packages/tui",
10
+ })
11
+ })
12
+
13
+ test("requests root sessions for search results", () => {
14
+ expect(createDialogSessionListQuery({ search: " deploy ", filter: { scope: "project" } })).toEqual({
15
+ roots: true,
16
+ limit: 30,
17
+ search: "deploy",
18
+ scope: "project",
19
+ })
20
+ })
21
+
22
+ test("keeps the cache usable while the root request is pending", async () => {
23
+ let resolve!: (result: { data: string[] }) => void
24
+ const pending = loadDialogSessionList<string>({
25
+ filter: {},
26
+ list: () => new Promise((done) => (resolve = done)),
27
+ })
28
+
29
+ expect(await Promise.race([pending, Promise.resolve("pending")])).toBe("pending")
30
+ resolve({ data: ["root"] })
31
+ expect(await pending).toEqual(["root"])
32
+ })
33
+
34
+ test("falls back when the root request returns an error response", async () => {
35
+ expect(await loadDialogSessionList({ filter: {}, list: async () => ({}) })).toBeUndefined()
36
+ })
37
+
38
+ test("falls back when the root request rejects", async () => {
39
+ expect(
40
+ await loadDialogSessionList({
41
+ filter: {},
42
+ list: () => Promise.reject(new Error("offline")),
43
+ }),
44
+ ).toBeUndefined()
45
+ })
46
+ })
@@ -0,0 +1,127 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+ import { testRender } from "@opentui/solid"
3
+ import { expect, test } from "bun:test"
4
+ import { Schema } from "effect"
5
+ import {
6
+ AttentionSoundName,
7
+ Info,
8
+ LeaderTimeoutDefault,
9
+ PluginSpec,
10
+ resolve,
11
+ TuiConfigProvider,
12
+ type Info as TuiConfigInfo,
13
+ useTuiConfig,
14
+ } from "../src/config"
15
+
16
+ const decodeInfo = Schema.decodeUnknownSync(Info)
17
+ const decodePlugin = Schema.decodeUnknownSync(PluginSpec)
18
+
19
+ test("defines package-owned plugin specs and attention sound names", () => {
20
+ expect(decodePlugin("example-plugin")).toBe("example-plugin")
21
+ expect(decodePlugin(["example-plugin", { enabled: true }])).toEqual(["example-plugin", { enabled: true }])
22
+ expect(() => decodePlugin(["example-plugin"])).toThrow()
23
+ expect(AttentionSoundName.literals).toEqual(["default", "question", "permission", "error", "done", "subagent_done"])
24
+ })
25
+
26
+ test("validates config constraints", () => {
27
+ expect(
28
+ decodeInfo({
29
+ leader_timeout: 250,
30
+ attention: { volume: 1, sounds: { done: "done.wav" } },
31
+ prompt: { max_height: 10, max_width: "auto" },
32
+ scroll_speed: 0.001,
33
+ diff_style: "stacked",
34
+ plugin: ["example-plugin"],
35
+ }),
36
+ ).toMatchObject({ leader_timeout: 250, attention: { volume: 1 }, diff_style: "stacked" })
37
+ expect(() => decodeInfo({ leader_timeout: 0 })).toThrow()
38
+ expect(() => decodeInfo({ attention: { volume: 1.1 } })).toThrow()
39
+ expect(() => decodeInfo({ prompt: { max_width: 0 } })).toThrow()
40
+ expect(() => decodeInfo({ scroll_speed: 0 })).toThrow()
41
+ expect(decodeInfo({ attention: { sounds: { unknown: "sound.wav" } } })).toEqual({ attention: { sounds: {} } })
42
+ })
43
+
44
+ test("resolves host-neutral defaults", () => {
45
+ const config = resolve({}, { terminalSuspend: true })
46
+
47
+ expect(config.attention).toEqual({
48
+ enabled: false,
49
+ notifications: true,
50
+ sound: true,
51
+ volume: 0.4,
52
+ sound_pack: "opencode.default",
53
+ sounds: {},
54
+ })
55
+ expect(config.leader_timeout).toBe(LeaderTimeoutDefault)
56
+ expect(config.mouse).toBe(true)
57
+ expect(config.keybinds.has("terminal.suspend")).toBe(true)
58
+ expect(config.keybinds.has("session.list")).toBe(true)
59
+ })
60
+
61
+ test("resolves overrides without mutating input", () => {
62
+ const input: TuiConfigInfo = {
63
+ theme: "custom",
64
+ mouse: false,
65
+ leader_timeout: 750,
66
+ attention: {
67
+ enabled: true,
68
+ notifications: false,
69
+ sound: false,
70
+ volume: 0.8,
71
+ sound_pack: "custom.pack",
72
+ sounds: { question: "/sounds/question.wav" },
73
+ },
74
+ keybinds: { session_list: "ctrl+l" },
75
+ }
76
+ const config = resolve(input, { terminalSuspend: true })
77
+
78
+ expect(config).toMatchObject({ theme: "custom", mouse: false, leader_timeout: 750, attention: input.attention })
79
+ expect(config.keybinds.get("session.list")).toHaveLength(1)
80
+ expect(input.keybinds).toEqual({ session_list: "ctrl+l" })
81
+ })
82
+
83
+ test("resolves a session move keybind", () => {
84
+ const config = resolve({ keybinds: { session_move: "ctrl+o" } }, { terminalSuspend: true })
85
+
86
+ expect(config.keybinds.get("session.move")).toMatchObject([{ key: "ctrl+o" }])
87
+ })
88
+
89
+ test("disables suspend and assigns ctrl+z to undo when unsupported", () => {
90
+ const config = resolve({}, { terminalSuspend: false })
91
+
92
+ expect(config.keybinds.has("terminal.suspend")).toBe(false)
93
+ expect(config.keybinds.get("input.undo")).toMatchObject([{ key: "ctrl+z,ctrl+-,super+z" }])
94
+ })
95
+
96
+ test("preserves an explicit undo binding when suspend is unsupported", () => {
97
+ const config = resolve({ keybinds: { input_undo: "ctrl+u", terminal_suspend: "ctrl+s" } }, { terminalSuspend: false })
98
+
99
+ expect(config.keybinds.has("terminal.suspend")).toBe(false)
100
+ expect(config.keybinds.get("input.undo")).toHaveLength(1)
101
+ expect(config.keybinds.get("input.undo")).toMatchObject([{ key: "ctrl+u" }])
102
+ })
103
+
104
+ test("provides resolved config through Solid context", async () => {
105
+ const config = resolve({ theme: "custom" }, { terminalSuspend: true })
106
+
107
+ function Consumer() {
108
+ const value = useTuiConfig()
109
+ return <text>{`${value.theme} ${value.mouse} ${value.leader_timeout}`}</text>
110
+ }
111
+
112
+ const app = await testRender(() => (
113
+ <TuiConfigProvider config={config}>
114
+ <Consumer />
115
+ </TuiConfigProvider>
116
+ ))
117
+ try {
118
+ await app.renderOnce()
119
+ expect(app.captureCharFrame()).toContain(`custom true ${LeaderTimeoutDefault}`)
120
+ } finally {
121
+ app.renderer.destroy()
122
+ }
123
+ })
124
+
125
+ test("requires the config provider", () => {
126
+ expect(() => useTuiConfig()).toThrow("TuiConfigProvider is missing")
127
+ })
@@ -0,0 +1,22 @@
1
+ import { expect, test } from "bun:test"
2
+ import { parseModel, recentModels } from "../../src/context/local"
3
+
4
+ test("parses model IDs containing slashes", () => {
5
+ expect(parseModel("provider/family/model")).toEqual({
6
+ providerID: "provider",
7
+ modelID: "family/model",
8
+ })
9
+ })
10
+
11
+ test("moves a model to the front, deduplicates, and limits recents", () => {
12
+ const recent = Array.from({ length: 12 }, (_, index) => ({
13
+ providerID: "provider",
14
+ modelID: `model-${index}`,
15
+ }))
16
+
17
+ expect(recentModels({ providerID: "provider", modelID: "model-5" }, recent)).toEqual([
18
+ { providerID: "provider", modelID: "model-5" },
19
+ ...recent.slice(0, 5),
20
+ ...recent.slice(6, 10),
21
+ ])
22
+ })
@@ -0,0 +1,32 @@
1
+ import { afterEach, expect, test } from "bun:test"
2
+ import { normalizePromptContent, openEditor } from "../src/editor"
3
+
4
+ const editor = process.env.EDITOR
5
+ const visual = process.env.VISUAL
6
+
7
+ afterEach(() => {
8
+ process.env.EDITOR = editor
9
+ process.env.VISUAL = visual
10
+ })
11
+
12
+ test("rejects when the external editor cannot start", async () => {
13
+ delete process.env.VISUAL
14
+ process.env.EDITOR = "opencode-editor-that-does-not-exist"
15
+ const renderer = {
16
+ suspend() {},
17
+ resume() {},
18
+ requestRender() {},
19
+ currentRenderBuffer: { clear() {} },
20
+ }
21
+
22
+ await expect(openEditor({ value: "original", renderer: renderer as never })).rejects.toThrow()
23
+ })
24
+
25
+ test("normalizes a single trailing editor newline for one-line prompts", () => {
26
+ expect(normalizePromptContent("hello\n")).toBe("hello")
27
+ expect(normalizePromptContent("hello\r\n")).toBe("hello")
28
+ })
29
+
30
+ test("preserves multiline prompts that end with a newline", () => {
31
+ expect(normalizePromptContent("hello\nworld\n")).toBe("hello\nworld\n")
32
+ })
@@ -0,0 +1,323 @@
1
+ import { describe, expect, test } from "bun:test"
2
+ import {
3
+ allExpandedFileTreeDirectories,
4
+ buildFileTree,
5
+ fileTreeFileSelection,
6
+ flattenFileTree,
7
+ moveFileTreeSelection,
8
+ moveFileTreeSelectionToFirstChild,
9
+ moveFileTreeSelectionToFile,
10
+ moveFileTreeSelectionToParent,
11
+ movePatchFileIndex,
12
+ orderedPatchFileIndexes,
13
+ setFileTreeDirectoryExpanded,
14
+ showDiffViewerFileTree,
15
+ singlePatchFileIndex,
16
+ toggleFileTreeDirectory,
17
+ } from "../../src/feature-plugins/system/diff-viewer-file-tree-utils"
18
+
19
+ describe("diff viewer file tree utilities", () => {
20
+ test("builds a nested tree with deduplicated directories and file indexes", () => {
21
+ const tree = buildFileTree([
22
+ { file: "src/config/tui.ts" },
23
+ { file: "src/config/keybind.ts" },
24
+ { file: "src/session/index.ts" },
25
+ ])
26
+
27
+ expect(tree.nodes.filter((node) => node.kind === "directory" && node.name === "src")).toHaveLength(1)
28
+ expect(tree.nodes.filter((node) => node.kind === "directory" && node.name === "config")).toHaveLength(1)
29
+ expect(tree.nodes.filter((node) => node.kind === "directory" && node.name === "session")).toHaveLength(1)
30
+ expect(
31
+ tree.nodes
32
+ .filter((node) => node.kind === "file")
33
+ .map((node) => ({ name: node.name, fileIndex: node.fileIndex, depth: node.depth })),
34
+ ).toEqual([
35
+ { name: "tui.ts", fileIndex: 0, depth: 2 },
36
+ { name: "keybind.ts", fileIndex: 1, depth: 2 },
37
+ { name: "index.ts", fileIndex: 2, depth: 2 },
38
+ ])
39
+ })
40
+
41
+ test("sorts directories before files and alphabetically within each group", () => {
42
+ const rows = flattenFileTree(
43
+ buildFileTree([
44
+ { file: "z-file.ts" },
45
+ { file: "b/file.ts" },
46
+ { file: "a/zeta.ts" },
47
+ { file: "b/alpha.ts" },
48
+ { file: "a/alpha.ts" },
49
+ ]),
50
+ )
51
+
52
+ expect(rows.map((row) => `${" ".repeat(row.depth)}${row.kind}:${row.name}`)).toEqual([
53
+ "directory:a",
54
+ " file:alpha.ts",
55
+ " file:zeta.ts",
56
+ "directory:b",
57
+ " file:alpha.ts",
58
+ " file:file.ts",
59
+ "file:z-file.ts",
60
+ ])
61
+ })
62
+
63
+ test("sorts root-level files without creating directories", () => {
64
+ const tree = buildFileTree([{ file: "zeta.ts" }, { file: "alpha.ts" }, { file: "beta.ts" }])
65
+
66
+ expect(tree.nodes.every((node) => node.kind === "file")).toBe(true)
67
+ expect(flattenFileTree(tree).map((row) => row.name)).toEqual(["alpha.ts", "beta.ts", "zeta.ts"])
68
+ })
69
+
70
+ test("collapses unary directory chains while flattening", () => {
71
+ const rows = flattenFileTree(
72
+ buildFileTree([{ file: "packages/opencode/src/cli/app.ts" }, { file: "packages/opencode/src/server/server.ts" }]),
73
+ )
74
+
75
+ expect(rows.map((row) => `${" ".repeat(row.depth)}${row.kind}:${row.name}`)).toEqual([
76
+ "directory:packages/opencode/src",
77
+ " directory:cli",
78
+ " file:app.ts",
79
+ " directory:server",
80
+ " file:server.ts",
81
+ ])
82
+ })
83
+
84
+ test("does not collapse a directory into a file row", () => {
85
+ const rows = flattenFileTree(buildFileTree([{ file: "packages/opencode/src/app.ts" }]))
86
+
87
+ expect(rows.map((row) => `${" ".repeat(row.depth)}${row.kind}:${row.name}`)).toEqual([
88
+ "directory:packages/opencode/src",
89
+ " file:app.ts",
90
+ ])
91
+ })
92
+
93
+ test("stops collapsing at branches", () => {
94
+ const rows = flattenFileTree(
95
+ buildFileTree([
96
+ { file: "packages/opencode/src/cli/app.ts" },
97
+ { file: "packages/opencode/src/server/server.ts" },
98
+ { file: "packages/readme.md" },
99
+ ]),
100
+ )
101
+
102
+ expect(rows.map((row) => `${" ".repeat(row.depth)}${row.kind}:${row.name}`)).toEqual([
103
+ "directory:packages",
104
+ " directory:opencode/src",
105
+ " directory:cli",
106
+ " file:app.ts",
107
+ " directory:server",
108
+ " file:server.ts",
109
+ " file:readme.md",
110
+ ])
111
+ })
112
+
113
+ test("keeps same directory names under different parents separate", () => {
114
+ const rows = flattenFileTree(
115
+ buildFileTree([{ file: "components/button.ts" }, { file: "docs/components/usage.md" }]),
116
+ )
117
+
118
+ expect(rows.map((row) => `${" ".repeat(row.depth)}${row.kind}:${row.name}`)).toEqual([
119
+ "directory:components",
120
+ " file:button.ts",
121
+ "directory:docs/components",
122
+ " file:usage.md",
123
+ ])
124
+ })
125
+
126
+ test("flattens all-expanded rows depth-first with depths and file references", () => {
127
+ const rows = flattenFileTree(
128
+ buildFileTree([{ file: "src/config/tui.ts" }, { file: "src/config/keybind.ts" }, { file: "README.md" }]),
129
+ )
130
+
131
+ expect(rows.map((row) => ({ name: row.name, kind: row.kind, depth: row.depth, fileIndex: row.fileIndex }))).toEqual(
132
+ [
133
+ { name: "src/config", kind: "directory", depth: 0, fileIndex: undefined },
134
+ { name: "keybind.ts", kind: "file", depth: 1, fileIndex: 1 },
135
+ { name: "tui.ts", kind: "file", depth: 1, fileIndex: 0 },
136
+ { name: "README.md", kind: "file", depth: 0, fileIndex: 2 },
137
+ ],
138
+ )
139
+ })
140
+
141
+ test("collapses expanded unary children under the first visible directory id", () => {
142
+ const tree = buildFileTree([
143
+ { file: "packages/opencode/src/cli/app.ts" },
144
+ { file: "packages/opencode/src/server/server.ts" },
145
+ ])
146
+ const packages = tree.nodes.find((node) => node.kind === "directory" && node.name === "packages")!
147
+
148
+ expect(flattenFileTree(tree, new Set()).map((row) => row.name)).toEqual(["packages/opencode/src"])
149
+ expect(flattenFileTree(tree, new Set([packages.id])).map((row) => row.name)).toEqual([
150
+ "packages/opencode/src",
151
+ "cli",
152
+ "server",
153
+ ])
154
+ })
155
+
156
+ test("flattens only expanded directory descendants when expansion is provided", () => {
157
+ const tree = buildFileTree([{ file: "src/config/tui.ts" }, { file: "src/session/index.ts" }, { file: "README.md" }])
158
+ const src = tree.nodes.find((node) => node.kind === "directory" && node.name === "src")!
159
+ const config = tree.nodes.find((node) => node.kind === "directory" && node.name === "config")!
160
+
161
+ expect(flattenFileTree(tree, new Set()).map((row) => row.name)).toEqual(["src", "README.md"])
162
+ expect(flattenFileTree(tree, new Set([src.id])).map((row) => row.name)).toEqual([
163
+ "src",
164
+ "config",
165
+ "session",
166
+ "README.md",
167
+ ])
168
+ expect(flattenFileTree(tree, new Set([src.id, config.id])).map((row) => row.name)).toEqual([
169
+ "src",
170
+ "config",
171
+ "tui.ts",
172
+ "session",
173
+ "README.md",
174
+ ])
175
+ })
176
+
177
+ test("moves selection across visible rows and clamps to bounds", () => {
178
+ const rows = flattenFileTree(buildFileTree([{ file: "src/config/tui.ts" }, { file: "README.md" }]))
179
+
180
+ expect(moveFileTreeSelection(rows, undefined, 1)).toBe(rows[0]!.id)
181
+ expect(moveFileTreeSelection(rows, rows[0]!.id, 1)).toBe(rows[1]!.id)
182
+ expect(moveFileTreeSelection(rows, rows[1]!.id, 99)).toBe(rows[rows.length - 1]!.id)
183
+ expect(moveFileTreeSelection(rows, rows[1]!.id, -99)).toBe(rows[0]!.id)
184
+ expect(moveFileTreeSelection([], undefined, 1)).toBeUndefined()
185
+ })
186
+
187
+ test("moves directory selection to first visible child", () => {
188
+ const rows = flattenFileTree(buildFileTree([{ file: "src/config/tui.ts" }, { file: "src/session/index.ts" }]))
189
+ const src = rows.find((row) => row.kind === "directory" && row.name === "src")!
190
+ const config = rows.find((row) => row.kind === "directory" && row.name === "config")!
191
+ const tui = rows.find((row) => row.name === "tui.ts")!
192
+
193
+ expect(moveFileTreeSelectionToFirstChild(rows, src.id)).toBe(config.id)
194
+ expect(moveFileTreeSelectionToFirstChild(rows, tui.id)).toBe(tui.id)
195
+ expect(moveFileTreeSelectionToFirstChild(rows, undefined)).toBeUndefined()
196
+ })
197
+
198
+ test("moves collapsed chain selection to first visible child", () => {
199
+ const rows = flattenFileTree(
200
+ buildFileTree([{ file: "packages/opencode/src/cli/app.ts" }, { file: "packages/opencode/src/server/server.ts" }]),
201
+ )
202
+ const packages = rows.find((row) => row.kind === "directory" && row.name === "packages/opencode/src")!
203
+ const cli = rows.find((row) => row.kind === "directory" && row.name === "cli")!
204
+
205
+ expect(moveFileTreeSelectionToFirstChild(rows, packages.id)).toBe(cli.id)
206
+ })
207
+
208
+ test("moves file and collapsed directory selection to visible parent", () => {
209
+ const rows = flattenFileTree(
210
+ buildFileTree([{ file: "packages/opencode/src/cli/app.ts" }, { file: "packages/opencode/src/server/server.ts" }]),
211
+ )
212
+ const root = rows.find((row) => row.kind === "directory" && row.name === "packages/opencode/src")!
213
+ const cli = rows.find((row) => row.kind === "directory" && row.name === "cli")!
214
+ const app = rows.find((row) => row.name === "app.ts")!
215
+
216
+ expect(moveFileTreeSelectionToParent(rows, app.id)).toBe(cli.id)
217
+ expect(moveFileTreeSelectionToParent(rows, cli.id)).toBe(root.id)
218
+ expect(moveFileTreeSelectionToParent(rows, root.id)).toBe(root.id)
219
+ expect(moveFileTreeSelectionToParent(rows, undefined)).toBeUndefined()
220
+ })
221
+
222
+ test("moves file selection relative to the highlighted row", () => {
223
+ const rows = flattenFileTree(
224
+ buildFileTree([{ file: "src/config/tui.ts" }, { file: "src/session/index.ts" }, { file: "README.md" }]),
225
+ )
226
+ const config = rows.find((row) => row.kind === "directory" && row.name === "config")!
227
+ const session = rows.find((row) => row.kind === "directory" && row.name === "session")!
228
+ const tui = rows.find((row) => row.name === "tui.ts")!
229
+ const index = rows.find((row) => row.name === "index.ts")!
230
+ const readme = rows.find((row) => row.name === "README.md")!
231
+
232
+ expect(moveFileTreeSelectionToFile(rows, undefined, 1)).toBe(tui.id)
233
+ expect(moveFileTreeSelectionToFile(rows, undefined, -1)).toBe(readme.id)
234
+ expect(moveFileTreeSelectionToFile(rows, config.id, 1)).toBe(tui.id)
235
+ expect(moveFileTreeSelectionToFile(rows, session.id, -1)).toBe(tui.id)
236
+ expect(moveFileTreeSelectionToFile(rows, tui.id, 1)).toBe(index.id)
237
+ expect(moveFileTreeSelectionToFile(rows, index.id, -1)).toBe(tui.id)
238
+ expect(moveFileTreeSelectionToFile(rows, readme.id, 1)).toBe(readme.id)
239
+ })
240
+
241
+ test("selects a file tree node and expands its parents for a patch file", () => {
242
+ const tree = buildFileTree([{ file: "src/config/tui.ts" }, { file: "src/session/index.ts" }, { file: "README.md" }])
243
+ const selection = fileTreeFileSelection(tree, 1)
244
+
245
+ expect(selection?.highlightedNode).toBe(
246
+ tree.nodes.find((node) => node.kind === "file" && node.name === "index.ts")?.id,
247
+ )
248
+ expect([...selection!.expandedNodes].map((id) => tree.nodes[id]!.name)).toEqual(["session", "src"])
249
+ expect(fileTreeFileSelection(tree, 99)).toBeUndefined()
250
+ })
251
+
252
+ test("prefers the selected file when choosing the single patch file", () => {
253
+ expect(singlePatchFileIndex(2, 1, 0, 3)).toBe(2)
254
+ expect(singlePatchFileIndex(undefined, 1, 0, 3)).toBe(1)
255
+ expect(singlePatchFileIndex(undefined, undefined, 0, 3)).toBe(0)
256
+ expect(singlePatchFileIndex(undefined, undefined, undefined, 3)).toBe(3)
257
+ })
258
+
259
+ test("orders patches by the flattened file tree order", () => {
260
+ const rows = flattenFileTree(
261
+ buildFileTree([
262
+ { file: "src/dir-8/juniper-4.ts" },
263
+ { file: "src/dir-8/harbor-94.ts" },
264
+ { file: "src/dir-8/cedar-16.ts" },
265
+ ]),
266
+ )
267
+
268
+ expect(orderedPatchFileIndexes(rows)).toEqual([2, 1, 0])
269
+ })
270
+
271
+ test("shows the diff viewer file tree only when enabled and files exist", () => {
272
+ expect(showDiffViewerFileTree(true, 1)).toBe(true)
273
+ expect(showDiffViewerFileTree(true, 0)).toBe(false)
274
+ expect(showDiffViewerFileTree(false, 1)).toBe(false)
275
+ expect(showDiffViewerFileTree(false, 0)).toBe(false)
276
+ })
277
+
278
+ test("moves patch selection through the ordered patch file indexes", () => {
279
+ const fileIndexes = [2, 1, 0]
280
+
281
+ expect(movePatchFileIndex(fileIndexes, undefined, 1)).toBe(2)
282
+ expect(movePatchFileIndex(fileIndexes, undefined, -1)).toBe(2)
283
+ expect(movePatchFileIndex(fileIndexes, 2, 1)).toBe(1)
284
+ expect(movePatchFileIndex(fileIndexes, 1, -1)).toBe(2)
285
+ expect(movePatchFileIndex(fileIndexes, 0, 1)).toBe(0)
286
+ expect(movePatchFileIndex(fileIndexes, 99, 1)).toBe(2)
287
+ expect(movePatchFileIndex(fileIndexes, 99, -1)).toBe(2)
288
+ expect(movePatchFileIndex([], undefined, 1)).toBeUndefined()
289
+ })
290
+
291
+ test("toggles only selected directory expansion", () => {
292
+ const tree = buildFileTree([{ file: "src/config/tui.ts" }, { file: "README.md" }])
293
+ const src = tree.nodes.find((node) => node.kind === "directory" && node.name === "src")!
294
+ const readme = tree.nodes.find((node) => node.kind === "file" && node.name === "README.md")!
295
+ const expanded = allExpandedFileTreeDirectories(tree)
296
+
297
+ const collapsed = toggleFileTreeDirectory(tree, expanded, src.id)
298
+ expect(collapsed.has(src.id)).toBe(false)
299
+ expect(flattenFileTree(tree, collapsed).map((row) => row.name)).toEqual(["src/config", "README.md"])
300
+
301
+ const reopened = toggleFileTreeDirectory(tree, collapsed, src.id)
302
+ expect(reopened.has(src.id)).toBe(true)
303
+
304
+ expect(toggleFileTreeDirectory(tree, reopened, readme.id)).toBe(reopened)
305
+ expect(toggleFileTreeDirectory(tree, reopened, undefined)).toBe(reopened)
306
+ })
307
+
308
+ test("sets only selected directory expansion", () => {
309
+ const tree = buildFileTree([{ file: "src/config/tui.ts" }, { file: "README.md" }])
310
+ const src = tree.nodes.find((node) => node.kind === "directory" && node.name === "src")!
311
+ const readme = tree.nodes.find((node) => node.kind === "file" && node.name === "README.md")!
312
+ const expanded = allExpandedFileTreeDirectories(tree)
313
+
314
+ const collapsed = setFileTreeDirectoryExpanded(tree, expanded, src.id, false)
315
+ expect(collapsed.has(src.id)).toBe(false)
316
+
317
+ const reopened = setFileTreeDirectoryExpanded(tree, collapsed, src.id, true)
318
+ expect(reopened.has(src.id)).toBe(true)
319
+
320
+ expect(setFileTreeDirectoryExpanded(tree, reopened, readme.id, false)).toBe(reopened)
321
+ expect(setFileTreeDirectoryExpanded(tree, reopened, undefined, false)).toBe(reopened)
322
+ })
323
+ })
@@ -0,0 +1,13 @@
1
+ import { mkdtemp, realpath, rm } from "node:fs/promises"
2
+ import path from "node:path"
3
+ import os from "node:os"
4
+
5
+ export async function tmpdir() {
6
+ const directory = await realpath(await mkdtemp(path.join(os.tmpdir(), "opencode-tui-test-")))
7
+ return {
8
+ path: directory,
9
+ async [Symbol.asyncDispose]() {
10
+ await rm(directory, { recursive: true, force: true })
11
+ },
12
+ }
13
+ }
@@ -0,0 +1,32 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+ import {
3
+ TuiPathsProvider,
4
+ TuiStartupProvider,
5
+ TuiTerminalEnvironmentProvider,
6
+ type TuiPaths,
7
+ } from "../../src/context/runtime"
8
+ import type { ParentProps } from "solid-js"
9
+
10
+ export function TestTuiContexts(
11
+ props: ParentProps<{
12
+ cwd?: string
13
+ directory?: string
14
+ paths?: Partial<TuiPaths>
15
+ }>,
16
+ ) {
17
+ return (
18
+ <TuiPathsProvider
19
+ value={{
20
+ cwd: props.cwd ?? props.directory ?? "/tmp/opencode/packages/tui",
21
+ home: "/tmp/opencode/home",
22
+ state: "/tmp/opencode/state",
23
+ worktree: "/tmp/opencode",
24
+ ...props.paths,
25
+ }}
26
+ >
27
+ <TuiTerminalEnvironmentProvider value={{ platform: "linux" }}>
28
+ <TuiStartupProvider value={{ skipInitialLoading: false }}>{props.children}</TuiStartupProvider>
29
+ </TuiTerminalEnvironmentProvider>
30
+ </TuiPathsProvider>
31
+ )
32
+ }
@@ -0,0 +1,36 @@
1
+ import type { TuiPluginApi } from "@neurocode-ai/plugin/tui"
2
+ import { RGBA } from "@opentui/core"
3
+ import { createTuiResolvedConfig } from "./tui-runtime"
4
+
5
+ type Opts = {
6
+ client?: TuiPluginApi["client"]
7
+ keymap?: TuiPluginApi["keymap"]
8
+ attention?: Partial<TuiPluginApi["attention"]>
9
+ event?: TuiPluginApi["event"]
10
+ state?: { session?: Partial<TuiPluginApi["state"]["session"]> }
11
+ }
12
+
13
+ export function createTuiPluginApi(opts: Opts = {}) {
14
+ const values = new Map<string, unknown>()
15
+ const color = RGBA.fromInts(200, 200, 200)
16
+ const dialog = { clear() {}, replace() {}, setSize() {}, size: "medium" as const, depth: 0, open: false }
17
+ return {
18
+ attention: { notify: async () => ({ ok: false, notification: false, sound: false }), ...opts.attention },
19
+ client: opts.client,
20
+ event: opts.event,
21
+ keymap: opts.keymap,
22
+ kv: {
23
+ get(name: string, fallback?: unknown) {
24
+ return values.has(name) ? values.get(name) : fallback
25
+ },
26
+ set(name: string, value: unknown) {
27
+ values.set(name, value)
28
+ },
29
+ ready: true,
30
+ },
31
+ state: { session: { get: () => undefined, ...opts.state?.session } },
32
+ theme: { current: new Proxy({}, { get: () => color }) },
33
+ tuiConfig: createTuiResolvedConfig(),
34
+ ui: { dialog },
35
+ } as unknown as TuiPluginApi
36
+ }
@@ -0,0 +1,12 @@
1
+ import { resolve, type Info, type Resolved } from "../../src/config"
2
+ import { TuiKeybind } from "../../src/config/keybind"
3
+
4
+ type ResolvedInput = Omit<Info, "attention" | "keybinds" | "leader_timeout"> & {
5
+ attention?: Partial<Resolved["attention"]>
6
+ keybinds?: Partial<TuiKeybind.Keybinds>
7
+ leader_timeout?: number
8
+ }
9
+
10
+ export function createTuiResolvedConfig(input: ResolvedInput = {}) {
11
+ return resolve(input, { terminalSuspend: process.platform !== "win32" })
12
+ }