tui-app.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import React, { act } from "react";
  4. import { testRender } from "@mbears/opentui-react/test-utils";
  5. import { getZCodeCopy } from "@zcode/i18n";
  6. import { TuiApp } from "../src/app.js";
  7. import { LoginRequiredPanel } from "../src/app-components.js";
  8. import { ZCODE_LOGO_LINES } from "../src/app-empty-transcript.js";
  9. import { InputPane } from "../src/app-input-pane.js";
  10. import { InputActiveStatus } from "../src/app-input-status.js";
  11. import { QueuedInputPanel } from "../src/app-queued-inputs.js";
  12. import { SelectionPanel } from "../src/app-selection-panel.js";
  13. import { ComposerInputArea } from "../src/app-view.js";
  14. import type { TuiOptions } from "../src/types.js";
  15. test("starts without a runtime readiness system message", async () => {
  16. let view: Awaited<ReturnType<typeof testRender>> | undefined;
  17. const options: TuiOptions = {
  18. noColor: true,
  19. stderr: process.stderr,
  20. stdin: process.stdin,
  21. stdout: process.stdout,
  22. submitPrompt: async () => ({ response: "done" }),
  23. };
  24. await act(async () => {
  25. view = await testRender(
  26. React.createElement(TuiApp, {
  27. copySelection: async () => ({ kind: "empty" }),
  28. hasCopyableSelection: () => false,
  29. onExit: () => undefined,
  30. options,
  31. }),
  32. {
  33. height: 24,
  34. kittyKeyboard: true,
  35. otherModifiersMode: true,
  36. width: 100,
  37. },
  38. );
  39. await view.renderOnce();
  40. });
  41. try {
  42. assert.ok(view);
  43. const frame = view.captureCharFrame();
  44. const rows = frame.split("\n");
  45. const firstLogoRow = rows.findIndex((row) => row.includes(ZCODE_LOGO_LINES[0]));
  46. const lastLogoRow = rows.findIndex((row) => row.includes(ZCODE_LOGO_LINES.at(-1) ?? ""));
  47. const composerTopRow = rows.findIndex((row) => row.includes("╭─ Build"));
  48. const logoCenterRow = Math.floor((firstLogoRow + lastLogoRow) / 2);
  49. const transcriptCenterRow = Math.floor((composerTopRow - 1) / 2);
  50. assert.equal(frame.includes("OpenTUI Node runtime loaded."), false);
  51. assert.notEqual(firstLogoRow, -1);
  52. assert.notEqual(lastLogoRow, -1);
  53. assert.notEqual(composerTopRow, -1);
  54. assert.ok(Math.abs(logoCenterRow - transcriptCenterRow) <= 1);
  55. } finally {
  56. await act(async () => {
  57. view?.renderer.destroy();
  58. });
  59. }
  60. });
  61. test("places the login notice before the prompt input in the composer area", () => {
  62. const children = reactElementChildren(
  63. ComposerInputArea({
  64. busy: false,
  65. contentWidth: 80,
  66. contextUsage: {},
  67. copy: getZCodeCopy("en-US").tui,
  68. draft: "",
  69. editorRef: { current: null },
  70. inputCursorToEndVersion: 0,
  71. loginRequired: true,
  72. mode: "build",
  73. model: "default-deepseek/deepseek-v4-pro",
  74. queuedInputs: [],
  75. setDraftValue: () => undefined,
  76. slashCommands: [],
  77. submitValue: () => undefined,
  78. thoughtLevel: "max",
  79. }),
  80. );
  81. assert.equal(children[0]?.type, LoginRequiredPanel);
  82. assert.equal(children.at(-2)?.type, InputPane);
  83. assert.equal(children.at(-2)?.props?.model, "default-deepseek/deepseek-v4-pro");
  84. assert.equal(children.at(-2)?.props?.thoughtLevel, "max");
  85. assert.equal(children.at(-1)?.type, InputActiveStatus);
  86. assert.equal(children.at(-1)?.props?.active, false);
  87. });
  88. test("places composer selections above the prompt input", () => {
  89. const children = reactElementChildren(
  90. ComposerInputArea({
  91. busy: false,
  92. contentWidth: 80,
  93. contextUsage: {},
  94. copy: getZCodeCopy("en-US").tui,
  95. draft: "",
  96. editorRef: { current: null },
  97. inputCursorToEndVersion: 0,
  98. loginRequired: false,
  99. mode: "build",
  100. model: "default-deepseek/deepseek-v4-pro",
  101. queuedInputs: [],
  102. selection: {
  103. emptyMessage: "No saved sessions found.",
  104. filter: "",
  105. items: [
  106. {
  107. command: "/resume sess_one",
  108. id: "sess_one",
  109. primary: "Session one",
  110. },
  111. ],
  112. placement: "composer",
  113. prompt: "Choose a session to resume.",
  114. selectedIndex: 0,
  115. title: "Resume Session",
  116. },
  117. setDraftValue: () => undefined,
  118. slashCommands: [],
  119. submitValue: () => undefined,
  120. thoughtLevel: "max",
  121. }),
  122. );
  123. assert.equal(children.find((child) => child.type === SelectionPanel)?.type, SelectionPanel);
  124. assert.equal(children.at(-2)?.type, InputPane);
  125. assert.equal(children.at(-1)?.type, InputActiveStatus);
  126. });
  127. test("passes the busy active status outside the prompt input", () => {
  128. const children = reactElementChildren(
  129. ComposerInputArea({
  130. busy: true,
  131. contentWidth: 80,
  132. contextUsage: { contextUsed: 11_100, contextWindow: 185_000 },
  133. copy: getZCodeCopy("en-US").tui,
  134. draft: "",
  135. editorRef: { current: null },
  136. inputCursorToEndVersion: 0,
  137. loginRequired: false,
  138. mode: "build",
  139. model: "default-deepseek/deepseek-v4-pro",
  140. queuedInputs: [],
  141. setDraftValue: () => undefined,
  142. slashCommands: [],
  143. submitValue: () => undefined,
  144. thoughtLevel: "max",
  145. }),
  146. );
  147. assert.equal(children.at(-2)?.type, InputPane);
  148. assert.equal(children.at(-2)?.props?.busy, true);
  149. assert.equal(children.at(-2)?.props?.model, "default-deepseek/deepseek-v4-pro");
  150. assert.equal(children.at(-2)?.props?.thoughtLevel, "max");
  151. assert.equal(children.at(-1)?.type, InputActiveStatus);
  152. assert.equal(children.at(-1)?.props?.active, true);
  153. assert.deepEqual(children.at(-1)?.props?.contextUsage, {
  154. contextUsed: 11_100,
  155. contextWindow: 185_000,
  156. });
  157. });
  158. test("places queued input panel immediately above the prompt input", () => {
  159. const children = reactElementChildren(
  160. ComposerInputArea({
  161. busy: true,
  162. contentWidth: 80,
  163. contextUsage: {},
  164. copy: getZCodeCopy("en-US").tui,
  165. draft: "",
  166. editorRef: { current: null },
  167. inputCursorToEndVersion: 0,
  168. loginRequired: false,
  169. mode: "build",
  170. model: "default-deepseek/deepseek-v4-pro",
  171. queuedInputs: [{ id: "pending-1", text: "hello queue" }],
  172. setDraftValue: () => undefined,
  173. slashCommands: [],
  174. submitValue: () => undefined,
  175. thoughtLevel: "max",
  176. }),
  177. );
  178. assert.equal(children.at(-3)?.type, QueuedInputPanel);
  179. assert.deepEqual(children.at(-3)?.props?.inputs, [{ id: "pending-1", text: "hello queue" }]);
  180. assert.equal(children.at(-2)?.type, InputPane);
  181. assert.equal(children.at(-1)?.type, InputActiveStatus);
  182. });
  183. test("Shift+Tab switches mode through the injected mode setter", async () => {
  184. let view: Awaited<ReturnType<typeof testRender>> | undefined;
  185. const requestedModes: string[] = [];
  186. const options: TuiOptions = {
  187. initialMode: "build",
  188. noColor: true,
  189. stderr: process.stderr,
  190. stdin: process.stdin,
  191. stdout: process.stdout,
  192. setMode: async (mode) => {
  193. requestedModes.push(mode);
  194. return { mode };
  195. },
  196. submitPrompt: async () => ({ response: "done" }),
  197. };
  198. await act(async () => {
  199. view = await testRender(
  200. React.createElement(TuiApp, {
  201. copySelection: async () => ({ kind: "empty" }),
  202. hasCopyableSelection: () => false,
  203. onExit: () => undefined,
  204. options,
  205. }),
  206. {
  207. height: 24,
  208. kittyKeyboard: true,
  209. otherModifiersMode: true,
  210. width: 100,
  211. },
  212. );
  213. await view.renderOnce();
  214. });
  215. try {
  216. await act(async () => {
  217. assert.ok(view);
  218. await view.mockInput.typeText("keep this draft");
  219. await view.renderOnce();
  220. });
  221. await act(async () => {
  222. assert.ok(view);
  223. view.mockInput.pressTab({ shift: true });
  224. await Promise.resolve();
  225. });
  226. await act(async () => {
  227. await view.renderOnce();
  228. });
  229. assert.deepEqual(requestedModes, ["guarded"]);
  230. const frame = view.captureCharFrame();
  231. assert.equal(frame.includes("Input - Guarded"), false);
  232. assert.equal(frame.includes("Guarded"), true);
  233. assert.equal(frame.includes("keep this draft"), true);
  234. } finally {
  235. await act(async () => {
  236. view?.renderer.destroy();
  237. });
  238. }
  239. });
  240. test("Shift+Tab can cycle guarded mode to yolo mode", async () => {
  241. let view: Awaited<ReturnType<typeof testRender>> | undefined;
  242. const requestedModes: string[] = [];
  243. const options: TuiOptions = {
  244. initialMode: "guarded",
  245. noColor: true,
  246. stderr: process.stderr,
  247. stdin: process.stdin,
  248. stdout: process.stdout,
  249. setMode: async (mode) => {
  250. requestedModes.push(mode);
  251. return { mode };
  252. },
  253. submitPrompt: async () => ({ response: "done" }),
  254. };
  255. await act(async () => {
  256. view = await testRender(
  257. React.createElement(TuiApp, {
  258. copySelection: async () => ({ kind: "empty" }),
  259. hasCopyableSelection: () => false,
  260. onExit: () => undefined,
  261. options,
  262. }),
  263. {
  264. height: 24,
  265. kittyKeyboard: true,
  266. otherModifiersMode: true,
  267. width: 100,
  268. },
  269. );
  270. await view.renderOnce();
  271. });
  272. try {
  273. await act(async () => {
  274. assert.ok(view);
  275. view.mockInput.pressTab({ shift: true });
  276. await Promise.resolve();
  277. await view.renderOnce();
  278. });
  279. assert.deepEqual(requestedModes, ["yolo"]);
  280. } finally {
  281. await act(async () => {
  282. view?.renderer.destroy();
  283. });
  284. }
  285. });
  286. test("recalls input history while model output is active", async () => {
  287. let view: Awaited<ReturnType<typeof testRender>> | undefined;
  288. const pendingTurn = deferred<TuiOptions["initialResult"]>();
  289. let recalled = 0;
  290. const options: TuiOptions = {
  291. noColor: true,
  292. recallPreviousInput: async () => {
  293. recalled += 1;
  294. return { text: "/help" };
  295. },
  296. stderr: process.stderr,
  297. stdin: process.stdin,
  298. stdout: process.stdout,
  299. submitPrompt: async () => (await pendingTurn.promise) ?? { response: "done" },
  300. };
  301. await act(async () => {
  302. view = await testRender(
  303. React.createElement(TuiApp, {
  304. copySelection: async () => ({ kind: "empty" }),
  305. hasCopyableSelection: () => false,
  306. onExit: () => undefined,
  307. options,
  308. }),
  309. {
  310. height: 24,
  311. kittyKeyboard: true,
  312. otherModifiersMode: true,
  313. width: 100,
  314. },
  315. );
  316. await view.renderOnce();
  317. });
  318. try {
  319. await act(async () => {
  320. assert.ok(view);
  321. await view.mockInput.typeText("first prompt");
  322. await Promise.resolve();
  323. await view.renderOnce();
  324. });
  325. await act(async () => {
  326. assert.ok(view);
  327. view.mockInput.pressEnter();
  328. await Promise.resolve();
  329. await view.renderOnce();
  330. });
  331. await act(async () => {
  332. assert.ok(view);
  333. view.mockInput.pressArrow("up");
  334. await Promise.resolve();
  335. await view.renderOnce();
  336. });
  337. // Bugfix: the shared OpenTUI test renderer buffer is noisy while package tests
  338. // run concurrently; the regression contract here is that busy Up reaches history.
  339. assert.equal(recalled, 1);
  340. } finally {
  341. pendingTurn.resolve({ response: "done" });
  342. await act(async () => {
  343. await Promise.resolve();
  344. await view?.renderOnce();
  345. view?.renderer.destroy();
  346. });
  347. }
  348. });
  349. test("mode composer popup submits the highlighted explicit mode command", async () => {
  350. let view: Awaited<ReturnType<typeof testRender>> | undefined;
  351. const submitted: string[] = [];
  352. const options: TuiOptions = {
  353. initialMode: "build",
  354. noColor: true,
  355. stderr: process.stderr,
  356. stdin: process.stdin,
  357. stdout: process.stdout,
  358. submitPrompt: async (input) => {
  359. submitted.push(typeof input === "string" ? input : input.text);
  360. return { mode: "yolo", response: "done" };
  361. },
  362. };
  363. await act(async () => {
  364. view = await testRender(
  365. React.createElement(TuiApp, {
  366. copySelection: async () => ({ kind: "empty" }),
  367. hasCopyableSelection: () => false,
  368. onExit: () => undefined,
  369. options,
  370. }),
  371. {
  372. height: 24,
  373. kittyKeyboard: true,
  374. otherModifiersMode: true,
  375. width: 100,
  376. },
  377. );
  378. await view.renderOnce();
  379. });
  380. try {
  381. await act(async () => {
  382. assert.ok(view);
  383. await view.mockInput.typeText("/mode");
  384. await view.renderOnce();
  385. });
  386. await act(async () => {
  387. assert.ok(view);
  388. view.mockInput.pressArrow("down");
  389. await Promise.resolve();
  390. await view.renderOnce();
  391. });
  392. await act(async () => {
  393. assert.ok(view);
  394. view.mockInput.pressArrow("down");
  395. await Promise.resolve();
  396. await view.renderOnce();
  397. });
  398. await act(async () => {
  399. assert.ok(view);
  400. view.mockInput.pressArrow("down");
  401. await Promise.resolve();
  402. await view.renderOnce();
  403. });
  404. await act(async () => {
  405. assert.ok(view);
  406. view.mockInput.pressEnter();
  407. await Promise.resolve();
  408. await view.renderOnce();
  409. });
  410. assert.deepEqual(submitted, ["/mode yolo"]);
  411. } finally {
  412. await act(async () => {
  413. view?.renderer.destroy();
  414. });
  415. }
  416. });
  417. test("mode composer popup can submit guarded mode command", async () => {
  418. let view: Awaited<ReturnType<typeof testRender>> | undefined;
  419. const submitted: string[] = [];
  420. const options: TuiOptions = {
  421. initialMode: "build",
  422. noColor: true,
  423. stderr: process.stderr,
  424. stdin: process.stdin,
  425. stdout: process.stdout,
  426. submitPrompt: async (input) => {
  427. submitted.push(typeof input === "string" ? input : input.text);
  428. return { mode: "edit", response: "done" };
  429. },
  430. };
  431. await act(async () => {
  432. view = await testRender(
  433. React.createElement(TuiApp, {
  434. copySelection: async () => ({ kind: "empty" }),
  435. hasCopyableSelection: () => false,
  436. onExit: () => undefined,
  437. options,
  438. }),
  439. {
  440. height: 24,
  441. kittyKeyboard: true,
  442. otherModifiersMode: true,
  443. width: 100,
  444. },
  445. );
  446. await view.renderOnce();
  447. });
  448. try {
  449. await act(async () => {
  450. assert.ok(view);
  451. await view.mockInput.typeText("/mode");
  452. await view.renderOnce();
  453. });
  454. await act(async () => {
  455. assert.ok(view);
  456. view.mockInput.pressArrow("down");
  457. await Promise.resolve();
  458. await view.renderOnce();
  459. });
  460. await act(async () => {
  461. assert.ok(view);
  462. view.mockInput.pressArrow("down");
  463. await Promise.resolve();
  464. await view.renderOnce();
  465. });
  466. await act(async () => {
  467. assert.ok(view);
  468. view.mockInput.pressEnter();
  469. await Promise.resolve();
  470. await view.renderOnce();
  471. });
  472. assert.deepEqual(submitted, ["/mode guarded"]);
  473. } finally {
  474. await act(async () => {
  475. view?.renderer.destroy();
  476. });
  477. }
  478. });
  479. function reactElementChildren(
  480. node: unknown,
  481. ): Array<{ props?: Record<string, unknown>; type?: unknown }> {
  482. if (typeof node !== "object" || node === null || !("props" in node)) return [];
  483. const element = node as { props?: { children?: unknown } };
  484. const children = element.props?.children;
  485. if (children === undefined) return [];
  486. return (Array.isArray(children) ? children : [children]).filter(
  487. (child): child is { type?: unknown } => typeof child === "object" && child !== null,
  488. );
  489. }
  490. function deferred<T>(): {
  491. promise: Promise<T>;
  492. resolve: (value: T) => void;
  493. } {
  494. let resolve!: (value: T) => void;
  495. const promise = new Promise<T>((innerResolve) => {
  496. resolve = innerResolve;
  497. });
  498. return { promise, resolve };
  499. }