tui-selection-panel.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import type { KeyEvent } from "@mbears/opentui-core";
  4. import { getZCodeCopy } from "@zcode/i18n";
  5. import { filterSelectionItems, handleSelectionKey } from "../src/app-input.js";
  6. import type { SelectionState } from "../src/app-model.js";
  7. import { palette } from "../src/app-model.js";
  8. import { visibleSelectionItemWindow } from "../src/app-selection-keyboard.js";
  9. import { SelectionPanel } from "../src/app-selection-panel.js";
  10. const EN_COPY = getZCodeCopy("en-US").tui;
  11. test("renders non-filterable selections without a filter prompt", () => {
  12. const panel = SelectionPanel({
  13. contentWidth: 72,
  14. copy: EN_COPY,
  15. selection: {
  16. ...baseSelection,
  17. filter: "zai",
  18. filterable: false,
  19. selectedIndex: 0,
  20. },
  21. });
  22. const textNodes = collectTextNodes(panel);
  23. assert.equal(textNodes.some((node) => node.text.includes("filter:")), false);
  24. assert.ok(textNodes.some((node) => node.text === baseSelection.help));
  25. assert.ok(textNodes.some((node) => node.text === "Z.AI Coding Plan"));
  26. const detailNode = textNodes.find((node) => node.text.includes("Open browser login"));
  27. assert.equal(detailNode?.style.fg, palette.muted);
  28. assert.equal(detailNode?.style.flexShrink, 0);
  29. });
  30. test("non-filterable selections ignore typed filter keys", () => {
  31. let selection: SelectionState = {
  32. ...baseSelection,
  33. filter: "missing",
  34. filterable: false,
  35. selectedIndex: 0,
  36. };
  37. const statuses: string[] = [];
  38. const submitted: string[] = [];
  39. assert.equal(filterSelectionItems(selection).length, baseSelection.items.length);
  40. handleSelectionKey(
  41. key("x"),
  42. selection,
  43. (next) => {
  44. selection = typeof next === "function" ? next(selection) : next ?? selection;
  45. },
  46. (status) => statuses.push(status),
  47. async (value) => {
  48. submitted.push(value);
  49. },
  50. );
  51. assert.equal(selection.filter, "missing");
  52. assert.deepEqual(statuses, []);
  53. assert.deepEqual(submitted, []);
  54. });
  55. test("selection panel follows the active row beyond the first page", () => {
  56. const items = Array.from({ length: 7 }, (_, index) => ({
  57. command: `/resume sess_${index + 1}`,
  58. id: `sess_${index + 1}`,
  59. primary: `Session ${index + 1}`,
  60. }));
  61. const panel = SelectionPanel({
  62. contentWidth: 72,
  63. copy: EN_COPY,
  64. selection: {
  65. ...baseSelection,
  66. filter: "",
  67. items,
  68. prompt: "Choose a session to resume.",
  69. selectedIndex: 5,
  70. title: "Resume Session",
  71. },
  72. });
  73. assert.deepEqual(visibleSelectionItemWindow(items, 5, 5), {
  74. items: items.slice(1, 6),
  75. selectedIndex: 4,
  76. startIndex: 1,
  77. });
  78. const texts = collectTextNodes(panel).map((node) => node.text);
  79. assert.equal(texts.includes("Session 1"), false);
  80. assert.ok(texts.includes("Session 2"));
  81. assert.ok(texts.includes("Session 6"));
  82. assert.equal(texts[texts.indexOf("> ") + 1], "Session 6");
  83. });
  84. test("pending selections replace options and keep escape as cancel", () => {
  85. const selection: SelectionState = {
  86. ...baseSelection,
  87. filter: "",
  88. pending: {
  89. cancelStatus: "Login cancelled. Choose a setup method.",
  90. command: "/login zai-coding-plan",
  91. help: "Esc cancels and returns to setup choices.",
  92. itemId: "zai-coding-plan",
  93. primary: "Waiting for Z.AI authorization",
  94. secondary: "Complete sign-in in your browser.",
  95. status: "Waiting for browser authorization...",
  96. },
  97. selectedIndex: 0,
  98. };
  99. const panel = SelectionPanel({ contentWidth: 72, copy: EN_COPY, selection });
  100. const textNodes = collectTextNodes(panel);
  101. assert.ok(textNodes.some((node) => node.text === "Waiting for Z.AI authorization"));
  102. assert.equal(textNodes.some((node) => node.text === "Z.AI Coding Plan"), false);
  103. });
  104. test("pending selection escape aborts and restores setup choices", () => {
  105. let cancelled = false;
  106. let selection: SelectionState = {
  107. ...baseSelection,
  108. filter: "",
  109. pending: {
  110. cancelStatus: "Login cancelled. Choose a setup method.",
  111. command: "/login zai-coding-plan",
  112. help: "Esc cancels and returns to setup choices.",
  113. itemId: "zai-coding-plan",
  114. primary: "Waiting for Z.AI authorization",
  115. status: "Waiting for browser authorization...",
  116. },
  117. selectedIndex: 0,
  118. };
  119. const statuses: string[] = [];
  120. handleSelectionKey(
  121. key("escape"),
  122. selection,
  123. (next) => {
  124. selection = typeof next === "function" ? next(selection) : next ?? selection;
  125. },
  126. (status) => statuses.push(status),
  127. async () => undefined,
  128. () => {
  129. cancelled = true;
  130. },
  131. );
  132. assert.equal(cancelled, true);
  133. assert.equal(selection.pending, undefined);
  134. assert.deepEqual(statuses, ["Login cancelled. Choose a setup method."]);
  135. });
  136. test("pending row submission preserves the selection panel while it waits", () => {
  137. let selection: SelectionState = {
  138. ...baseSelection,
  139. filter: "",
  140. selectedIndex: 0,
  141. };
  142. const statuses: string[] = [];
  143. const submissions: Array<{ options: unknown; value: string }> = [];
  144. handleSelectionKey(
  145. key("return"),
  146. selection,
  147. (next) => {
  148. selection = typeof next === "function" ? next(selection) : next ?? selection;
  149. },
  150. (status) => statuses.push(status),
  151. async (value, options) => {
  152. submissions.push({ options, value });
  153. },
  154. );
  155. assert.equal(selection.pending?.itemId, "zai-coding-plan");
  156. assert.equal(statuses.at(-1), "Waiting for browser authorization...");
  157. assert.deepEqual(submissions, [
  158. {
  159. options: {
  160. abortStatus: "Login cancelled. Choose a setup method.",
  161. preserveSelection: true,
  162. },
  163. value: "/login zai-coding-plan",
  164. },
  165. ]);
  166. });
  167. test("input selections replace options with an inline hidden input", () => {
  168. const selection: SelectionState = {
  169. ...baseSelection,
  170. filter: "",
  171. input: {
  172. cancelStatus: "API key entry cancelled. Choose a setup method.",
  173. clearStatus: "API key input cleared.",
  174. command: "/login zai-coding-plan-api-key",
  175. emptyStatus: "API key is required.",
  176. help: "Enter saves the key. Esc returns to setup choices.",
  177. itemId: "zai-coding-plan-api-key",
  178. mask: true,
  179. placeholder: "Paste API key",
  180. primary: "Enter Z.AI Coding Plan API Key",
  181. secondary: "Paste the key here. It is hidden while typing.",
  182. status: "Enter the API key, then press Enter.",
  183. submitStatus: "Saving API key...",
  184. value: "manual-key",
  185. },
  186. selectedIndex: 2,
  187. };
  188. const panel = SelectionPanel({ contentWidth: 72, copy: EN_COPY, selection });
  189. const textNodes = collectTextNodes(panel);
  190. assert.ok(textNodes.some((node) => node.text === "Enter Z.AI Coding Plan API Key"));
  191. assert.ok(textNodes.some((node) => node.text === "> **********"));
  192. assert.equal(textNodes.some((node) => node.text === "BigModel Coding Plan"), false);
  193. });
  194. test("input row submission stays in the setup panel before saving the key", () => {
  195. let selection: SelectionState | undefined = {
  196. ...baseSelection,
  197. filter: "",
  198. selectedIndex: 2,
  199. };
  200. const statuses: string[] = [];
  201. const submissions: string[] = [];
  202. assert.ok(selection);
  203. handleSelectionKey(
  204. key("return"),
  205. selection,
  206. (next) => {
  207. selection = typeof next === "function" ? next(selection) : next;
  208. },
  209. (status) => statuses.push(status),
  210. async (value) => {
  211. submissions.push(value);
  212. },
  213. );
  214. assert.equal(selection?.input?.itemId, "zai-coding-plan-api-key");
  215. assert.equal(statuses.at(-1), "Enter the API key, then press Enter.");
  216. assert.deepEqual(submissions, []);
  217. assert.ok(selection);
  218. handleSelectionKey(
  219. key("", { sequence: "manual-key" }),
  220. selection,
  221. (next) => {
  222. selection = typeof next === "function" ? next(selection) : next;
  223. },
  224. (status) => statuses.push(status),
  225. async (value) => {
  226. submissions.push(value);
  227. },
  228. );
  229. assert.equal(selection?.input?.value, "manual-key");
  230. assert.ok(selection);
  231. handleSelectionKey(
  232. key("return"),
  233. selection,
  234. (next) => {
  235. selection = typeof next === "function" ? next(selection) : next;
  236. },
  237. (status) => statuses.push(status),
  238. async (value) => {
  239. submissions.push(value);
  240. },
  241. );
  242. assert.equal(selection, undefined);
  243. assert.deepEqual(submissions, ["/login zai-coding-plan-api-key manual-key"]);
  244. });
  245. test("input selection escape restores setup choices", () => {
  246. let selection: SelectionState | undefined = {
  247. ...baseSelection,
  248. filter: "",
  249. input: {
  250. cancelStatus: "API key entry cancelled. Choose a setup method.",
  251. clearStatus: "API key input cleared.",
  252. command: "/login zai-coding-plan-api-key",
  253. emptyStatus: "API key is required.",
  254. itemId: "zai-coding-plan-api-key",
  255. mask: true,
  256. placeholder: "Paste API key",
  257. primary: "Enter Z.AI Coding Plan API Key",
  258. status: "Enter the API key, then press Enter.",
  259. value: "manual-key",
  260. },
  261. selectedIndex: 2,
  262. };
  263. const statuses: string[] = [];
  264. assert.ok(selection);
  265. handleSelectionKey(
  266. key("escape"),
  267. selection,
  268. (next) => {
  269. selection = typeof next === "function" ? next(selection) : next;
  270. },
  271. (status) => statuses.push(status),
  272. async () => undefined,
  273. );
  274. assert.equal(selection?.input, undefined);
  275. assert.deepEqual(statuses, ["API key entry cancelled. Choose a setup method."]);
  276. });
  277. const baseSelection: SelectionState = {
  278. emptyMessage: "No login options are available.",
  279. filter: "",
  280. help: "Use Up/Down to choose, Enter to select.",
  281. items: [
  282. {
  283. command: "/login zai-coding-plan",
  284. id: "zai-coding-plan",
  285. pending: {
  286. cancelStatus: "Login cancelled. Choose a setup method.",
  287. help: "Esc cancels and returns to setup choices.",
  288. primary: "Waiting for Z.AI authorization",
  289. secondary: "Complete sign-in in your browser.",
  290. status: "Waiting for browser authorization...",
  291. },
  292. primary: "Z.AI Coding Plan",
  293. secondary: "Open browser login and create a Coding Plan API key.",
  294. },
  295. {
  296. command: "/login bigmodel-coding-plan",
  297. id: "bigmodel-coding-plan",
  298. primary: "BigModel Coding Plan",
  299. secondary: "Open browser login with a localhost callback.",
  300. },
  301. {
  302. command: "/login zai-coding-plan-api-key",
  303. id: "zai-coding-plan-api-key",
  304. input: {
  305. cancelStatus: "API key entry cancelled. Choose a setup method.",
  306. clearStatus: "API key input cleared.",
  307. emptyStatus: "API key is required.",
  308. help: "Enter saves the key. Esc returns to setup choices.",
  309. mask: true,
  310. placeholder: "Paste API key",
  311. primary: "Enter Z.AI Coding Plan API Key",
  312. secondary: "Paste the key here. It is hidden while typing.",
  313. status: "Enter the API key, then press Enter.",
  314. submitStatus: "Saving API key...",
  315. },
  316. primary: "Z.AI Coding Plan API Key",
  317. secondary: "Paste a Coding Plan API key manually.",
  318. },
  319. ],
  320. prompt: "Choose a login or API key setup method.",
  321. selectedIndex: 0,
  322. title: "Set Up Coding Plan",
  323. };
  324. function key(name: string, overrides: Partial<KeyEvent> = {}): KeyEvent {
  325. return {
  326. ctrl: false,
  327. meta: false,
  328. name,
  329. sequence: name,
  330. shift: false,
  331. ...overrides,
  332. } as KeyEvent;
  333. }
  334. function collectTextNodes(node: unknown): Array<{ style: Record<string, unknown>; text: string }> {
  335. const lines: ReturnType<typeof collectTextNodes> = [];
  336. const visit = (value: unknown) => {
  337. if (Array.isArray(value)) {
  338. for (const child of value) visit(child);
  339. return;
  340. }
  341. if (typeof value !== "object" || value === null || !("props" in value)) return;
  342. const element = value as {
  343. props?: {
  344. children?: unknown;
  345. style?: Record<string, unknown>;
  346. };
  347. type?: unknown;
  348. };
  349. if (typeof element.type === "function") {
  350. visit(element.type(element.props));
  351. return;
  352. }
  353. if (element.type === "text") {
  354. const text = collectTextLines(element.props?.children).join("");
  355. lines.push({ style: element.props?.style ?? {}, text });
  356. }
  357. visit(element.props?.children);
  358. };
  359. visit(node);
  360. return lines;
  361. }
  362. function collectTextLines(node: unknown): string[] {
  363. const lines: string[] = [];
  364. const visit = (value: unknown) => {
  365. if (typeof value === "string") {
  366. lines.push(value);
  367. return;
  368. }
  369. if (Array.isArray(value)) {
  370. for (const child of value) visit(child);
  371. return;
  372. }
  373. if (typeof value === "object" && value !== null && "props" in value) {
  374. const element = value as { props?: { children?: unknown } };
  375. visit(element.props?.children);
  376. }
  377. };
  378. visit(node);
  379. return lines;
  380. }