footer.view.test.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /** @jsxImportSource @opentui/solid */
  2. import { expect, test } from "bun:test"
  3. import { testRender, useRenderer } from "@opentui/solid"
  4. import { createSignal } from "solid-js"
  5. import { createDefaultOpenTuiKeymap } from "@opentui/keymap/opentui"
  6. import type { QuestionRequest } from "@opencode-ai/sdk/v2"
  7. import { OpencodeKeymapProvider, registerOpencodeKeymap } from "@/cli/cmd/tui/keymap"
  8. import {
  9. RUN_COMMAND_PANEL_ROWS,
  10. RUN_SUBAGENT_PANEL_ROWS,
  11. RunCommandMenuBody,
  12. RunModelSelectBody,
  13. RunQueuedPromptSelectBody,
  14. RunSubagentSelectBody,
  15. RunVariantSelectBody,
  16. } from "@/cli/cmd/run/footer.command"
  17. import { RunFooterView } from "@/cli/cmd/run/footer.view"
  18. import { RunEntryContent } from "@/cli/cmd/run/scrollback.writer"
  19. import { RUN_THEME_FALLBACK } from "@/cli/cmd/run/theme"
  20. import type {
  21. FooterState,
  22. FooterSubagentState,
  23. FooterSubagentTab,
  24. FooterView,
  25. RunCommand,
  26. RunInput,
  27. RunPrompt,
  28. RunProvider,
  29. RunTuiConfig,
  30. StreamCommit,
  31. } from "@/cli/cmd/run/types"
  32. import { RunQuestionBody } from "@/cli/cmd/run/footer.question"
  33. import { RejectField } from "@/cli/cmd/run/footer.permission"
  34. import { createTuiResolvedConfig } from "../../fixture/tui-runtime"
  35. const tuiConfig = createTuiResolvedConfig()
  36. function command(input: { name: string; description: string; source?: "command" | "mcp" | "skill" }) {
  37. return {
  38. name: input.name,
  39. description: input.description,
  40. source: input.source,
  41. template: "",
  42. hints: [],
  43. } satisfies RunCommand
  44. }
  45. function model(input: {
  46. id: string
  47. name: string
  48. status?: "active" | "deprecated"
  49. cost?: number
  50. variants?: Record<string, Record<string, never>>
  51. }) {
  52. return {
  53. id: input.id,
  54. providerID: "opencode",
  55. api: {
  56. id: "opencode",
  57. url: "https://opencode.ai",
  58. npm: "@ai-sdk/openai-compatible",
  59. },
  60. name: input.name,
  61. capabilities: {
  62. temperature: true,
  63. reasoning: true,
  64. attachment: true,
  65. toolcall: true,
  66. input: {
  67. text: true,
  68. audio: false,
  69. image: true,
  70. video: false,
  71. pdf: true,
  72. },
  73. output: {
  74. text: true,
  75. audio: false,
  76. image: false,
  77. video: false,
  78. pdf: false,
  79. },
  80. interleaved: false,
  81. },
  82. cost: {
  83. input: input.cost ?? 1,
  84. output: 1,
  85. cache: {
  86. read: 0,
  87. write: 0,
  88. },
  89. },
  90. limit: {
  91. context: 128000,
  92. output: 8192,
  93. },
  94. status: input.status ?? "active",
  95. options: {},
  96. headers: {},
  97. release_date: "2026-01-01",
  98. variants: input.variants,
  99. } satisfies RunProvider["models"][string]
  100. }
  101. function provider() {
  102. return {
  103. id: "opencode",
  104. name: "opencode",
  105. source: "api",
  106. env: [],
  107. options: {},
  108. models: {
  109. "gpt-5": model({ id: "gpt-5", name: "GPT-5", variants: { high: {}, minimal: {} } }),
  110. "gpt-free": model({ id: "gpt-free", name: "GPT Free", cost: 0 }),
  111. old: model({ id: "old", name: "Old Model", status: "deprecated" }),
  112. },
  113. } satisfies RunProvider
  114. }
  115. function subagent(input: {
  116. sessionID: string
  117. label: string
  118. description: string
  119. status?: FooterSubagentTab["status"]
  120. }) {
  121. return {
  122. sessionID: input.sessionID,
  123. partID: `part-${input.sessionID}`,
  124. callID: `call-${input.sessionID}`,
  125. label: input.label,
  126. description: input.description,
  127. status: input.status ?? "running",
  128. lastUpdatedAt: 1,
  129. } satisfies FooterSubagentTab
  130. }
  131. function footerState(input: Partial<FooterState> = {}) {
  132. return createSignal<FooterState>({
  133. phase: "idle",
  134. status: "",
  135. queue: 0,
  136. model: "gpt-5",
  137. duration: "",
  138. usage: "",
  139. first: false,
  140. interrupt: 0,
  141. exit: 0,
  142. ...input,
  143. })[0]
  144. }
  145. async function renderFooter(
  146. input: {
  147. tuiConfig?: RunTuiConfig
  148. commands?: RunCommand[]
  149. onCycle?: () => void
  150. onSubmit?: (prompt: RunPrompt) => boolean
  151. } = {},
  152. ) {
  153. const [view] = createSignal<FooterView>({ type: "prompt" })
  154. const [subagents] = createSignal<FooterSubagentState>({ tabs: [], details: {}, permissions: [], questions: [] })
  155. const state = footerState()
  156. const config = input.tuiConfig ?? tuiConfig
  157. let offKeymap: (() => void) | undefined
  158. function Harness() {
  159. const renderer = useRenderer()
  160. const keymap = createDefaultOpenTuiKeymap(renderer)
  161. offKeymap = registerOpencodeKeymap(keymap, renderer, config)
  162. return (
  163. <OpencodeKeymapProvider keymap={keymap}>
  164. <RunFooterView
  165. directory="/tmp"
  166. findFiles={async () => []}
  167. agents={() => []}
  168. resources={() => []}
  169. commands={() => input.commands ?? []}
  170. providers={() => undefined}
  171. currentModel={() => undefined}
  172. variants={() => []}
  173. currentVariant={() => undefined}
  174. state={state}
  175. view={view}
  176. subagent={subagents}
  177. theme={RUN_THEME_FALLBACK}
  178. tuiConfig={config}
  179. agent="opencode"
  180. onSubmit={input.onSubmit ?? (() => true)}
  181. onPermissionReply={() => {}}
  182. onQuestionReply={() => {}}
  183. onQuestionReject={() => {}}
  184. onCycle={input.onCycle ?? (() => {})}
  185. onInterrupt={() => false}
  186. onInputClear={() => {}}
  187. onExit={() => {}}
  188. onModelSelect={() => {}}
  189. onVariantSelect={() => {}}
  190. onRows={() => {}}
  191. onLayout={() => {}}
  192. onStatus={() => {}}
  193. onQueuedRemove={async () => true}
  194. />
  195. </OpencodeKeymapProvider>
  196. )
  197. }
  198. const app = await testRender(
  199. () => (
  200. <box width={100} height={8}>
  201. <Harness />
  202. </box>
  203. ),
  204. { width: 100, height: 8, kittyKeyboard: true },
  205. )
  206. return {
  207. ...app,
  208. cleanup() {
  209. app.renderer.currentFocusedRenderable?.blur()
  210. app.renderer.currentFocusedEditor?.blur()
  211. offKeymap?.()
  212. offKeymap = undefined
  213. app.renderer.destroy()
  214. },
  215. }
  216. }
  217. test("run entry content updates when live commit text changes", async () => {
  218. const [commit, setCommit] = createSignal<StreamCommit>({
  219. kind: "tool",
  220. text: "I",
  221. phase: "progress",
  222. source: "tool",
  223. messageID: "msg-1",
  224. partID: "part-1",
  225. tool: "bash",
  226. })
  227. const app = await testRender(
  228. () => (
  229. <box width={80} height={4}>
  230. <RunEntryContent commit={commit()} theme={RUN_THEME_FALLBACK} width={80} />
  231. </box>
  232. ),
  233. {
  234. width: 80,
  235. height: 4,
  236. },
  237. )
  238. try {
  239. await app.renderOnce()
  240. expect(app.captureCharFrame()).toContain("I")
  241. setCommit({
  242. kind: "tool",
  243. text: "I need to inspect the codebase",
  244. phase: "progress",
  245. source: "tool",
  246. messageID: "msg-1",
  247. partID: "part-1",
  248. tool: "bash",
  249. })
  250. await app.renderOnce()
  251. expect(app.captureCharFrame()).toContain("I need to inspect the codebase")
  252. } finally {
  253. app.renderer.destroy()
  254. }
  255. })
  256. test("direct command panel renders grouped command palette", async () => {
  257. const [commands] = createSignal<RunCommand[] | undefined>([
  258. command({ name: "review", description: "Review code" }),
  259. command({ name: "deploy", description: "Deploy prompt", source: "mcp" }),
  260. command({ name: "internal", description: "Skill command", source: "skill" }),
  261. ])
  262. const [subagents] = createSignal([])
  263. const [variants] = createSignal(["high", "minimal"])
  264. const app = await testRender(
  265. () => (
  266. <box width={100} height={RUN_COMMAND_PANEL_ROWS}>
  267. <RunCommandMenuBody
  268. theme={() => RUN_THEME_FALLBACK.footer}
  269. commands={commands}
  270. subagents={subagents}
  271. queued={() => []}
  272. variants={variants}
  273. variantCycle="ctrl+t"
  274. onClose={() => {}}
  275. onModel={() => {}}
  276. onSubagent={() => {}}
  277. onQueued={() => {}}
  278. onVariant={() => {}}
  279. onVariantCycle={() => {}}
  280. onCommand={() => {}}
  281. onNew={() => {}}
  282. onExit={() => {}}
  283. />
  284. </box>
  285. ),
  286. {
  287. width: 100,
  288. height: RUN_COMMAND_PANEL_ROWS,
  289. },
  290. )
  291. try {
  292. await app.renderOnce()
  293. const frame = app.captureCharFrame()
  294. expect(frame).toContain("Commands")
  295. expect(frame).toContain("Search")
  296. expect(frame).toContain("Suggested")
  297. expect(frame).toContain("Switch model")
  298. expect(frame).toContain("Variant cycle")
  299. expect(frame).toContain("ctrl+t")
  300. expect(frame).toContain("Switch model variant")
  301. expect(frame).toContain("Session")
  302. expect(frame).toContain("New session")
  303. expect(frame).toContain("/new")
  304. expect(frame).toContain("Project Commands")
  305. expect(frame).toContain("review")
  306. expect(frame).toContain("/review")
  307. expect(frame).not.toContain("/internal")
  308. expect(frame).not.toContain("Choose model for future turns")
  309. expect(frame).not.toContain("Cycle reasoning effort for future turns")
  310. expect(frame).not.toContain("Review code")
  311. expect(frame).not.toContain("Commands 8")
  312. } finally {
  313. app.renderer.destroy()
  314. }
  315. })
  316. test("direct command panel shows subagent entry when available", async () => {
  317. const [commands] = createSignal<RunCommand[] | undefined>([])
  318. const [subagents] = createSignal([subagent({ sessionID: "s-1", label: "Explore", description: "Inspect auth flow" })])
  319. const [variants] = createSignal<string[]>([])
  320. const app = await testRender(
  321. () => (
  322. <box width={100} height={RUN_COMMAND_PANEL_ROWS}>
  323. <RunCommandMenuBody
  324. theme={() => RUN_THEME_FALLBACK.footer}
  325. commands={commands}
  326. subagents={subagents}
  327. queued={() => []}
  328. variants={variants}
  329. variantCycle="ctrl+t"
  330. onClose={() => {}}
  331. onModel={() => {}}
  332. onSubagent={() => {}}
  333. onQueued={() => {}}
  334. onVariant={() => {}}
  335. onVariantCycle={() => {}}
  336. onCommand={() => {}}
  337. onNew={() => {}}
  338. onExit={() => {}}
  339. />
  340. </box>
  341. ),
  342. {
  343. width: 100,
  344. height: RUN_COMMAND_PANEL_ROWS,
  345. },
  346. )
  347. try {
  348. await app.renderOnce()
  349. const frame = app.captureCharFrame()
  350. expect(frame).toContain("View subagents")
  351. expect(frame).toContain("1 active")
  352. } finally {
  353. app.renderer.destroy()
  354. }
  355. })
  356. test("direct subagent panel renders active subagents", async () => {
  357. const [tabs] = createSignal([
  358. subagent({ sessionID: "s-1", label: "Explore", description: "Inspect auth flow" }),
  359. subagent({ sessionID: "s-2", label: "General", description: "Write migration plan", status: "completed" }),
  360. ])
  361. const [current] = createSignal<string | undefined>("s-1")
  362. let rows = 0
  363. const app = await testRender(
  364. () => (
  365. <box width={100} height={RUN_SUBAGENT_PANEL_ROWS}>
  366. <RunSubagentSelectBody
  367. theme={() => RUN_THEME_FALLBACK.footer}
  368. tabs={tabs}
  369. current={current}
  370. onClose={() => {}}
  371. onSelect={() => {}}
  372. onRows={(value) => {
  373. rows = value
  374. }}
  375. />
  376. </box>
  377. ),
  378. {
  379. width: 100,
  380. height: RUN_SUBAGENT_PANEL_ROWS,
  381. },
  382. )
  383. try {
  384. await app.renderOnce()
  385. const frame = app.captureCharFrame()
  386. expect(frame).toContain("Select subagent")
  387. expect(frame).toContain("Inspect auth flow")
  388. expect(frame).toContain("Write migration plan")
  389. expect(frame).toContain("done")
  390. expect(rows).toBe(8)
  391. } finally {
  392. app.renderer.destroy()
  393. }
  394. })
  395. test("direct queued prompt panel renders pending prompt actions", async () => {
  396. const [prompts] = createSignal([
  397. { messageID: "m-1", partID: "p-1", prompt: { text: "fix the auth test", parts: [] } },
  398. ])
  399. const app = await testRender(
  400. () => (
  401. <box width={100} height={RUN_SUBAGENT_PANEL_ROWS}>
  402. <RunQueuedPromptSelectBody
  403. theme={() => RUN_THEME_FALLBACK.footer}
  404. prompts={prompts}
  405. onClose={() => {}}
  406. onEdit={() => {}}
  407. onDelete={() => {}}
  408. />
  409. </box>
  410. ),
  411. { width: 100, height: RUN_SUBAGENT_PANEL_ROWS },
  412. )
  413. try {
  414. await app.renderOnce()
  415. expect(app.captureCharFrame()).toContain("Queued prompts")
  416. expect(app.captureCharFrame()).toContain("fix the auth test")
  417. expect(app.captureCharFrame()).toContain("queued")
  418. } finally {
  419. app.renderer.destroy()
  420. }
  421. })
  422. // OpenTUI currently segfaults when the full footer view suite creates several
  423. // keymap-backed test renderers in one process. Re-enable after the runtime fix.
  424. test.skip("direct footer opens command panel through keymap binding", async () => {
  425. const app = await renderFooter()
  426. try {
  427. await app.renderOnce()
  428. app.mockInput.pressKey("p", { ctrl: true })
  429. await app.renderOnce()
  430. expect(app.captureCharFrame()).toContain("Commands")
  431. } finally {
  432. app.cleanup()
  433. }
  434. })
  435. test.skip("direct footer dispatches leader variant binding only when leader is registered", async () => {
  436. const calls: string[] = []
  437. const app = await renderFooter({
  438. tuiConfig: createTuiResolvedConfig({ keybinds: { leader: "ctrl+x", variant_cycle: "<leader>t" } }),
  439. onCycle: () => calls.push("cycle"),
  440. })
  441. try {
  442. await app.renderOnce()
  443. app.mockInput.pressKey("t")
  444. expect(calls).toEqual([])
  445. app.mockInput.pressKey("x", { ctrl: true })
  446. app.mockInput.pressKey("t")
  447. expect(calls).toEqual(["cycle"])
  448. } finally {
  449. app.cleanup()
  450. }
  451. })
  452. test("direct footer keeps leader variant binding inactive when leader is disabled", async () => {
  453. const calls: string[] = []
  454. const app = await renderFooter({
  455. tuiConfig: createTuiResolvedConfig({ keybinds: { leader: "none", variant_cycle: "<leader>t" } }),
  456. onCycle: () => calls.push("cycle"),
  457. })
  458. try {
  459. await app.renderOnce()
  460. app.mockInput.pressKey("t")
  461. app.mockInput.pressKey("x", { ctrl: true })
  462. app.mockInput.pressKey("t")
  463. expect(calls).toEqual([])
  464. } finally {
  465. app.cleanup()
  466. }
  467. })
  468. test("direct footer submits slash autocomplete selections without dispatching shell completions", async () => {
  469. const submits: RunPrompt[] = []
  470. const app = await renderFooter({
  471. commands: [command({ name: "review", description: "Review code" })],
  472. onSubmit(prompt) {
  473. submits.push(prompt)
  474. return true
  475. },
  476. })
  477. try {
  478. await app.renderOnce()
  479. "/rev".split("").forEach((key) => app.mockInput.pressKey(key))
  480. await app.renderOnce()
  481. app.mockInput.pressEnter()
  482. await app.renderOnce()
  483. "/rev".split("").forEach((key) => app.mockInput.pressKey(key))
  484. await app.renderOnce()
  485. app.mockInput.pressKey("TAB")
  486. await app.renderOnce()
  487. "/re branch".split("").forEach((key) => app.mockInput.pressKey(key))
  488. Array.from({ length: 7 }).forEach(() => app.mockInput.pressKey("ARROW_LEFT"))
  489. app.mockInput.pressKey("v")
  490. await app.renderOnce()
  491. app.mockInput.pressEnter()
  492. await app.renderOnce()
  493. "/nx".split("").forEach((key) => app.mockInput.pressKey(key))
  494. app.mockInput.pressKey("ARROW_LEFT")
  495. app.mockInput.pressKey("e")
  496. await app.renderOnce()
  497. app.mockInput.pressEnter()
  498. await app.renderOnce()
  499. "/n scratch".split("").forEach((key) => app.mockInput.pressKey(key))
  500. Array.from({ length: 8 }).forEach(() => app.mockInput.pressKey("ARROW_LEFT"))
  501. app.mockInput.pressKey("e")
  502. await app.renderOnce()
  503. app.mockInput.pressEnter()
  504. await app.renderOnce()
  505. app.mockInput.pressKey("!")
  506. "/rev".split("").forEach((key) => app.mockInput.pressKey(key))
  507. await app.renderOnce()
  508. app.mockInput.pressEnter()
  509. await app.renderOnce()
  510. expect(submits).toEqual([
  511. { text: "/review ", parts: [], command: { name: "review", arguments: "" } },
  512. { text: "/review ", parts: [], command: { name: "review", arguments: "" } },
  513. { text: "/review branch", parts: [], command: { name: "review", arguments: "branch" } },
  514. { text: "/new ", parts: [] },
  515. { text: "/new ", parts: [] },
  516. ])
  517. expect(app.captureCharFrame()).toContain("/review")
  518. } finally {
  519. app.cleanup()
  520. }
  521. })
  522. test("direct footer shows editable prompts and additional queued work while running", async () => {
  523. const [state] = createSignal<FooterState>({
  524. phase: "running",
  525. status: "",
  526. queue: 3,
  527. model: "gpt-5",
  528. duration: "",
  529. usage: "",
  530. first: false,
  531. interrupt: 0,
  532. exit: 0,
  533. })
  534. const [view] = createSignal<FooterView>({ type: "prompt" })
  535. const [subagents] = createSignal<FooterSubagentState>({
  536. tabs: [subagent({ sessionID: "s-1", label: "Explore", description: "Inspect auth flow" })],
  537. details: {},
  538. permissions: [],
  539. questions: [],
  540. })
  541. let offKeymap: (() => void) | undefined
  542. function Harness() {
  543. const renderer = useRenderer()
  544. const keymap = createDefaultOpenTuiKeymap(renderer)
  545. offKeymap = registerOpencodeKeymap(keymap, renderer, tuiConfig)
  546. return (
  547. <OpencodeKeymapProvider keymap={keymap}>
  548. <RunFooterView
  549. directory="/tmp"
  550. findFiles={async () => []}
  551. agents={() => []}
  552. resources={() => []}
  553. commands={() => []}
  554. providers={() => undefined}
  555. currentModel={() => undefined}
  556. variants={() => []}
  557. currentVariant={() => undefined}
  558. state={state}
  559. view={view}
  560. subagent={subagents}
  561. queuedPrompts={() => [
  562. { messageID: "m-queued", partID: "p-queued", prompt: { text: "follow up", parts: [] } },
  563. ]}
  564. theme={RUN_THEME_FALLBACK}
  565. tuiConfig={tuiConfig}
  566. agent="opencode"
  567. onSubmit={() => true}
  568. onPermissionReply={() => {}}
  569. onQuestionReply={() => {}}
  570. onQuestionReject={() => {}}
  571. onCycle={() => {}}
  572. onInterrupt={() => false}
  573. onInputClear={() => {}}
  574. onExit={() => {}}
  575. onModelSelect={() => {}}
  576. onVariantSelect={() => {}}
  577. onRows={() => {}}
  578. onLayout={() => {}}
  579. onStatus={() => {}}
  580. onQueuedRemove={async () => true}
  581. />
  582. </OpencodeKeymapProvider>
  583. )
  584. }
  585. const app = await testRender(
  586. () => (
  587. <box width={160} height={8}>
  588. <Harness />
  589. </box>
  590. ),
  591. {
  592. width: 160,
  593. height: 8,
  594. },
  595. )
  596. try {
  597. await app.renderOnce()
  598. expect(app.captureCharFrame()).toContain("interrupt • 1 agent ctrl+x down • 1 queued ctrl+x q")
  599. expect(app.captureCharFrame()).toContain("2 queued")
  600. expect(app.captureCharFrame()).not.toContain("to view")
  601. expect(app.captureCharFrame()).not.toContain("edit/remove")
  602. } finally {
  603. app.renderer.currentFocusedRenderable?.blur()
  604. app.renderer.currentFocusedEditor?.blur()
  605. offKeymap?.()
  606. app.renderer.destroy()
  607. }
  608. })
  609. test("direct question body separates single-select checkmark from label", async () => {
  610. const request = {
  611. id: "question-1",
  612. sessionID: "session-1",
  613. questions: [
  614. {
  615. question: "Which categorical concept is often described as a universal way to combine two objects?",
  616. header: "Universal Product",
  617. options: [
  618. { label: "Product", description: "A product comes with projections." },
  619. { label: "Equalizer", description: "An equalizer selects morphisms where arrows agree." },
  620. ],
  621. },
  622. ],
  623. } satisfies QuestionRequest
  624. const replies: unknown[] = []
  625. const app = await testRender(
  626. () => (
  627. <box width={100} height={12}>
  628. <RunQuestionBody
  629. request={request}
  630. theme={RUN_THEME_FALLBACK.footer}
  631. onReply={(input) => {
  632. replies.push(input)
  633. }}
  634. onReject={() => {}}
  635. />
  636. </box>
  637. ),
  638. {
  639. width: 100,
  640. height: 12,
  641. },
  642. )
  643. try {
  644. app.mockInput.pressEnter()
  645. await app.renderOnce()
  646. expect(replies).toHaveLength(1)
  647. expect(app.captureCharFrame()).toContain("Product ✓")
  648. } finally {
  649. app.renderer.destroy()
  650. }
  651. })
  652. // OpenTUI currently segfaults while tearing down this textarea-backed keymap renderer.
  653. // Re-enable after the runtime fix.
  654. test.skip("direct custom answer submits through keymap return binding", async () => {
  655. const question = {
  656. id: "question-1",
  657. sessionID: "session-1",
  658. questions: [
  659. {
  660. question: "Which answer should I use?",
  661. header: "Answer",
  662. options: [{ label: "Provided", description: "Use the listed answer." }],
  663. custom: true,
  664. },
  665. ],
  666. } satisfies QuestionRequest
  667. const questions: unknown[] = []
  668. let off: (() => void) | undefined
  669. function Harness() {
  670. const renderer = useRenderer()
  671. const keymap = createDefaultOpenTuiKeymap(renderer)
  672. off = registerOpencodeKeymap(keymap, renderer, tuiConfig)
  673. return (
  674. <OpencodeKeymapProvider keymap={keymap}>
  675. <RunQuestionBody
  676. request={question}
  677. theme={RUN_THEME_FALLBACK.footer}
  678. onReply={(input) => {
  679. questions.push(input)
  680. }}
  681. onReject={() => {}}
  682. />
  683. </OpencodeKeymapProvider>
  684. )
  685. }
  686. const app = await testRender(
  687. () => (
  688. <box width={100} height={18}>
  689. <Harness />
  690. </box>
  691. ),
  692. { width: 100, height: 18, kittyKeyboard: true },
  693. )
  694. try {
  695. await app.renderOnce()
  696. app.mockInput.pressKey("2")
  697. await app.renderOnce()
  698. "typed".split("").forEach((key) => app.mockInput.pressKey(key))
  699. await app.renderOnce()
  700. app.mockInput.pressEnter()
  701. await app.renderOnce()
  702. expect(questions).toEqual([{ requestID: "question-1", answers: [["typed"]] }])
  703. } finally {
  704. app.renderer.currentFocusedRenderable?.blur()
  705. app.renderer.currentFocusedEditor?.blur()
  706. off?.()
  707. app.renderer.destroy()
  708. }
  709. })
  710. test("direct permission rejection submits through keymap return binding", async () => {
  711. let text = ""
  712. const submits: string[] = []
  713. let off: (() => void) | undefined
  714. function Harness() {
  715. const renderer = useRenderer()
  716. const keymap = createDefaultOpenTuiKeymap(renderer)
  717. off = registerOpencodeKeymap(keymap, renderer, tuiConfig)
  718. return (
  719. <OpencodeKeymapProvider keymap={keymap}>
  720. <RejectField
  721. theme={RUN_THEME_FALLBACK.footer}
  722. text=""
  723. disabled={false}
  724. onChange={(input) => {
  725. text = input
  726. }}
  727. onConfirm={() => {
  728. submits.push(text)
  729. }}
  730. onCancel={() => {}}
  731. />
  732. </OpencodeKeymapProvider>
  733. )
  734. }
  735. const app = await testRender(
  736. () => (
  737. <box width={100} height={18}>
  738. <Harness />
  739. </box>
  740. ),
  741. { width: 100, height: 18, kittyKeyboard: true },
  742. )
  743. try {
  744. await app.renderOnce()
  745. "retry".split("").forEach((key) => app.mockInput.pressKey(key))
  746. await app.renderOnce()
  747. expect(app.captureCharFrame()).toContain("retry")
  748. app.mockInput.pressEnter()
  749. await app.renderOnce()
  750. expect(submits).toEqual(["retry"])
  751. } finally {
  752. app.renderer.currentFocusedRenderable?.blur()
  753. app.renderer.currentFocusedEditor?.blur()
  754. off?.()
  755. app.renderer.destroy()
  756. }
  757. })
  758. test("direct model panel renders current model selector", async () => {
  759. const [providers] = createSignal<RunProvider[] | undefined>([provider()])
  760. const [current] = createSignal<RunInput["model"]>({ providerID: "opencode", modelID: "gpt-5" })
  761. const app = await testRender(
  762. () => (
  763. <box width={100} height={RUN_COMMAND_PANEL_ROWS}>
  764. <RunModelSelectBody
  765. theme={() => RUN_THEME_FALLBACK.footer}
  766. providers={providers}
  767. current={current}
  768. onClose={() => {}}
  769. onSelect={() => {}}
  770. />
  771. </box>
  772. ),
  773. {
  774. width: 100,
  775. height: RUN_COMMAND_PANEL_ROWS,
  776. },
  777. )
  778. try {
  779. await app.renderOnce()
  780. const frame = app.captureCharFrame()
  781. expect(frame).toContain("Select model")
  782. expect(frame).toContain("Search")
  783. expect(frame).toContain("opencode")
  784. expect(frame).toContain("GPT-5")
  785. expect(frame).toContain("current")
  786. expect(frame).toContain("GPT Free")
  787. expect(frame).toContain("Free")
  788. expect(frame).not.toContain("Old Model")
  789. } finally {
  790. app.renderer.destroy()
  791. }
  792. })
  793. test("direct variant panel renders current variant selector", async () => {
  794. const [variants] = createSignal(["high", "minimal"])
  795. const [current] = createSignal<string | undefined>("high")
  796. const app = await testRender(
  797. () => (
  798. <box width={100} height={RUN_COMMAND_PANEL_ROWS}>
  799. <RunVariantSelectBody
  800. theme={() => RUN_THEME_FALLBACK.footer}
  801. variants={variants}
  802. current={current}
  803. onClose={() => {}}
  804. onSelect={() => {}}
  805. />
  806. </box>
  807. ),
  808. {
  809. width: 100,
  810. height: RUN_COMMAND_PANEL_ROWS,
  811. },
  812. )
  813. try {
  814. await app.renderOnce()
  815. const frame = app.captureCharFrame()
  816. expect(frame).toContain("Select variant")
  817. expect(frame).toContain("Default")
  818. expect(frame).toContain("high")
  819. expect(frame).toContain("minimal")
  820. expect(frame).toContain("current")
  821. } finally {
  822. app.renderer.destroy()
  823. }
  824. })