tui-model-picker.test.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. import { stripAnsi, visibleWidth } from "../../src/tui/rendering/text.js";
  2. import { describe, expect, it, vi } from "vitest";
  3. import { TuiModelPicker } from "../../src/tui/features/model/picker.js";
  4. describe("TuiModelPicker", () => {
  5. it("selects each declared Kimi K3 effort without inventing a medium level", () => {
  6. const onSelect = vi.fn();
  7. const model = {
  8. providerId: "custom_provider:moonshotai",
  9. modelId: "kimi-k3",
  10. selected: true,
  11. effortOptions: ["low", "high", "max"],
  12. };
  13. const picker = new TuiModelPicker([model], onSelect, vi.fn());
  14. expect(stripAnsi(picker.render(80).join("\n"))).toContain("[ high ]");
  15. for (const [key, effort] of [
  16. ["\u001b[D", "low"],
  17. ["\u001b[C", "high"],
  18. ["\u001b[C", "max"],
  19. ] as const) {
  20. picker.handleInput(key);
  21. expect(stripAnsi(picker.render(80).join("\n"))).toContain(`[ ${effort} ]`);
  22. picker.handleInput("\r");
  23. expect(onSelect).toHaveBeenLastCalledWith(model, effort);
  24. }
  25. });
  26. it("switches M3 context independently of thinking and restores the saved choice", () => {
  27. const model = {
  28. providerId: "minimax",
  29. modelId: "MiniMax-M3",
  30. selected: true,
  31. contextLimit: 512_000,
  32. contextWindowOptions: [512_000, 1_000_000],
  33. contextWindowOptionHints: { "1000000": "higher_usage" as const },
  34. thinkingConfig: { mode: "switchable", defaultValue: "true" },
  35. };
  36. const onSelect = vi.fn();
  37. const picker = new TuiModelPicker([model], onSelect, vi.fn());
  38. expect(stripAnsi(picker.render(100).join("\n"))).toContain("[ 512K ]");
  39. picker.handleInput("\t");
  40. picker.handleInput("\x1b[C");
  41. const rendered = stripAnsi(picker.render(100).join("\n"));
  42. expect(rendered).toContain("[ 1M ]");
  43. expect(rendered).toContain("Higher usage with this context window.");
  44. expect(rendered).toContain("[ Off ]");
  45. expect(onSelect).not.toHaveBeenCalled();
  46. expect(model.contextLimit).toBe(512_000);
  47. picker.handleInput("\r");
  48. expect(onSelect).toHaveBeenCalledWith(
  49. expect.objectContaining({ contextLimit: 1_000_000, variant: "" }),
  50. );
  51. const reopened = new TuiModelPicker(
  52. [{ ...model, contextLimit: 1_000_000 }],
  53. onSelect,
  54. vi.fn(),
  55. );
  56. expect(stripAnsi(reopened.render(100).join("\n"))).toContain("[ 1M ]");
  57. reopened.handleInput("\x1b[Z");
  58. expect(stripAnsi(reopened.render(100).join("\n"))).toContain("[ 512K ]");
  59. });
  60. it("keeps context drafts while filtering, preserves effort and discards cancelled changes", () => {
  61. const model = {
  62. providerId: "minimax",
  63. modelId: "MiniMax-M3",
  64. contextLimit: 512_000,
  65. contextWindowOptions: [512_000, 1_000_000],
  66. effortOptions: ["low", "high"],
  67. };
  68. const onSelect = vi.fn();
  69. const onCancel = vi.fn();
  70. const picker = new TuiModelPicker([model], onSelect, onCancel);
  71. picker.handleInput("M3");
  72. picker.handleInput("\t");
  73. expect(stripAnsi(picker.render(100).join("\n"))).toContain("Search: M3");
  74. picker.handleInput("\x7f");
  75. picker.handleInput("\x7f");
  76. picker.handleInput("\x1b[C");
  77. picker.handleInput("\r");
  78. expect(onSelect).toHaveBeenCalledWith(
  79. expect.objectContaining({ contextLimit: 1_000_000 }),
  80. "high",
  81. );
  82. const cancelled = new TuiModelPicker([model], onSelect, onCancel);
  83. cancelled.handleInput("\t");
  84. cancelled.handleInput("\x1b");
  85. expect(onCancel).toHaveBeenCalledOnce();
  86. expect(onSelect).toHaveBeenCalledOnce();
  87. expect(model.contextLimit).toBe(512_000);
  88. });
  89. it("does not invent context choices and keeps the control usable in short viewports", () => {
  90. const onSelect = vi.fn();
  91. const fixed = {
  92. providerId: "work",
  93. modelId: "fixed",
  94. contextLimit: 200_000,
  95. };
  96. const picker = new TuiModelPicker([fixed], onSelect, vi.fn());
  97. picker.handleInput("\t");
  98. picker.handleInput("\r");
  99. expect(onSelect).toHaveBeenCalledWith(fixed);
  100. expect(stripAnsi(picker.render(100).join("\n"))).not.toContain(
  101. "tab context",
  102. );
  103. const switchable = new TuiModelPicker(
  104. [{ ...fixed, contextWindowOptions: [200_000, 1_000_000] }],
  105. vi.fn(),
  106. vi.fn(),
  107. );
  108. switchable.handleInput("\t");
  109. for (const height of [6, 8, 12]) {
  110. const lines = switchable.renderViewport(60, height);
  111. expect(lines.length).toBeLessThanOrEqual(height);
  112. expect(lines.every((line) => visibleWidth(line) <= 60)).toBe(true);
  113. expect(stripAnsi(lines.join("\n"))).toContain(
  114. height === 6 ? "1M ctx" : "[ 1M ]",
  115. );
  116. }
  117. });
  118. it("keeps model focus ahead of helper text in a short viewport", () => {
  119. const picker = new TuiModelPicker(
  120. [
  121. {
  122. providerId: "minimax",
  123. modelId: "MiniMax-M3",
  124. displayName: "MiniMax-M3",
  125. },
  126. ],
  127. vi.fn(),
  128. vi.fn(),
  129. { unavailableHint: "Sign in to use this model" },
  130. );
  131. for (const height of [6, 8]) {
  132. const visible = stripAnsi(picker.renderViewport(40, height).join("\n"));
  133. expect(visible).toContain("›");
  134. expect(visible).toContain("MiniMax-M3");
  135. expect(visible.toLowerCase()).toContain("esc");
  136. }
  137. });
  138. it("keeps third-party provider onboarding available when the model roster is empty", () => {
  139. const onAddProvider = vi.fn();
  140. const picker = new TuiModelPicker([], vi.fn(), vi.fn(), { onAddProvider });
  141. expect(stripAnsi(picker.render(90).join("\n"))).toContain(
  142. "Add 3rd-party provider",
  143. );
  144. expect(stripAnsi(picker.render(90).join("\n"))).toContain(
  145. "No matching models",
  146. );
  147. picker.handleInput("\r");
  148. expect(onAddProvider).toHaveBeenCalledOnce();
  149. });
  150. it("groups models by provider and filters the grouped list while typing", () => {
  151. const onSelect = vi.fn();
  152. const picker = new TuiModelPicker(
  153. [
  154. {
  155. providerId: "minimax",
  156. providerName: "MiniMax",
  157. modelId: "MiniMax-M3",
  158. displayName: "MiniMax-M3",
  159. },
  160. {
  161. providerId: "minimax",
  162. providerName: "MiniMax",
  163. modelId: "MiniMax-M2.7",
  164. displayName: "MiniMax-M2.7",
  165. },
  166. {
  167. providerId: "deepseek",
  168. providerName: "DeepSeek",
  169. modelId: "deepseek-v4-pro",
  170. displayName: "deepseek-v4-pro",
  171. },
  172. ],
  173. onSelect,
  174. vi.fn(),
  175. );
  176. const initial = stripAnsi(picker.render(90).join("\n"));
  177. expect(initial).toContain("MiniMax · 2");
  178. expect(initial).toContain("DeepSeek · 1");
  179. picker.handleInput("deepseek");
  180. const filtered = stripAnsi(picker.render(90).join("\n"));
  181. expect(filtered).toContain("Search: deepseek");
  182. expect(filtered).toContain("DeepSeek · 1");
  183. expect(filtered).toContain("deepseek-v4-pro");
  184. expect(filtered).not.toContain("MiniMax-M3");
  185. picker.handleInput("\r");
  186. expect(onSelect).toHaveBeenCalledWith(
  187. expect.objectContaining({
  188. providerId: "deepseek",
  189. modelId: "deepseek-v4-pro",
  190. }),
  191. );
  192. });
  193. it("confirms before deleting the focused custom provider and keeps plain d for search", async () => {
  194. const onDeleteProvider = vi.fn(async () => undefined);
  195. const onSelect = vi.fn();
  196. const picker = new TuiModelPicker(
  197. [
  198. {
  199. providerId: "custom_provider:openai",
  200. providerName: "OpenAI",
  201. providerSource: "custom_provider",
  202. providerKind: "custom",
  203. modelId: "gpt-4o",
  204. displayName: "GPT-4o",
  205. },
  206. {
  207. providerId: "custom_provider:openai",
  208. providerName: "OpenAI",
  209. providerSource: "custom_provider",
  210. providerKind: "custom",
  211. modelId: "gpt-4o-mini",
  212. displayName: "GPT-4o mini",
  213. },
  214. ],
  215. onSelect,
  216. vi.fn(),
  217. { onDeleteProvider, requestRender: vi.fn() },
  218. );
  219. expect(stripAnsi(picker.render(90).join("\n"))).toContain(
  220. "ctrl+d delete provider",
  221. );
  222. picker.handleInput("d");
  223. expect(stripAnsi(picker.render(90).join("\n"))).toContain("Search: d");
  224. expect(onDeleteProvider).not.toHaveBeenCalled();
  225. picker.handleInput("\x7f");
  226. picker.handleInput("\x04");
  227. const confirmation = stripAnsi(picker.render(90).join("\n"));
  228. expect(confirmation).toContain("Delete provider?");
  229. expect(confirmation).toContain("OpenAI");
  230. expect(confirmation).toContain("all 2 configured models");
  231. expect(onDeleteProvider).not.toHaveBeenCalled();
  232. picker.handleInput("\x1b");
  233. expect(stripAnsi(picker.render(90).join("\n"))).toContain("GPT-4o");
  234. picker.handleInput("\x04");
  235. picker.handleInput("\r");
  236. await vi.waitFor(() =>
  237. expect(onDeleteProvider).toHaveBeenCalledWith("custom_provider:openai"),
  238. );
  239. expect(onSelect).not.toHaveBeenCalled();
  240. });
  241. it("does not offer provider deletion for built-in models", () => {
  242. const onDeleteProvider = vi.fn(async () => undefined);
  243. const picker = new TuiModelPicker(
  244. [
  245. {
  246. providerId: "minimax",
  247. providerName: "MiniMax",
  248. providerSource: "provider",
  249. providerKind: "minimax-managed",
  250. modelId: "MiniMax-M3",
  251. displayName: "MiniMax-M3",
  252. },
  253. ],
  254. vi.fn(),
  255. vi.fn(),
  256. { onDeleteProvider, requestRender: vi.fn() },
  257. );
  258. expect(stripAnsi(picker.render(90).join("\n"))).not.toContain(
  259. "delete provider",
  260. );
  261. picker.handleInput("\x04");
  262. expect(stripAnsi(picker.render(90).join("\n"))).not.toContain(
  263. "Delete provider?",
  264. );
  265. expect(onDeleteProvider).not.toHaveBeenCalled();
  266. });
  267. it("keeps the provider and confirmation available when deletion fails", async () => {
  268. const picker = new TuiModelPicker(
  269. [
  270. {
  271. providerId: "custom_provider:openai",
  272. providerName: "OpenAI",
  273. providerSource: "custom_provider",
  274. providerKind: "custom",
  275. modelId: "gpt-4o",
  276. displayName: "GPT-4o",
  277. },
  278. ],
  279. vi.fn(),
  280. vi.fn(),
  281. {
  282. onDeleteProvider: vi.fn(async () => {
  283. throw new Error("storage unavailable");
  284. }),
  285. requestRender: vi.fn(),
  286. },
  287. );
  288. picker.handleInput("\x04");
  289. picker.handleInput("\r");
  290. await vi.waitFor(() =>
  291. expect(stripAnsi(picker.render(180).join("\n"))).toContain(
  292. "Couldn't delete provider",
  293. ),
  294. );
  295. expect(stripAnsi(picker.render(180).join("\n"))).toContain(
  296. "The provider and its models are unchanged.",
  297. );
  298. picker.handleInput("\x1b");
  299. expect(stripAnsi(picker.render(90).join("\n"))).toContain("GPT-4o");
  300. });
  301. it("searches Provider display names and restores the grouped list after an empty result", () => {
  302. const picker = new TuiModelPicker(
  303. [
  304. {
  305. providerId: "gateway-east",
  306. providerName: "Team Gateway",
  307. modelId: "model-a",
  308. displayName: "Model A",
  309. },
  310. {
  311. providerId: "gateway-west",
  312. providerName: "Other Gateway",
  313. modelId: "model-b",
  314. displayName: "Model B",
  315. },
  316. ],
  317. vi.fn(),
  318. vi.fn(),
  319. );
  320. picker.handleInput("team");
  321. let rendered = stripAnsi(picker.render(90).join("\n"));
  322. expect(rendered).toContain("Team Gateway · 1");
  323. expect(rendered).toContain("Model A");
  324. expect(rendered).not.toContain("Model B");
  325. picker.handleInput("x");
  326. rendered = stripAnsi(picker.render(90).join("\n"));
  327. expect(rendered).toContain("No matching models");
  328. expect(rendered).not.toContain("Model A");
  329. picker.handleInput("\x7f");
  330. rendered = stripAnsi(picker.render(90).join("\n"));
  331. expect(rendered).toContain("Team Gateway · 1");
  332. expect(rendered).toContain("Model A");
  333. });
  334. it("restores the Runtime-selected model after clearing a filter that hid it", () => {
  335. const onSelect = vi.fn();
  336. const picker = new TuiModelPicker(
  337. [
  338. {
  339. providerId: "minimax",
  340. modelId: "MiniMax-M3",
  341. displayName: "MiniMax-M3",
  342. selected: true,
  343. },
  344. {
  345. providerId: "deepseek",
  346. modelId: "deepseek-v4-pro",
  347. displayName: "deepseek-v4-pro",
  348. },
  349. ],
  350. onSelect,
  351. vi.fn(),
  352. );
  353. picker.handleInput("deepseek");
  354. for (let index = 0; index < "deepseek".length; index += 1)
  355. picker.handleInput("\x7f");
  356. picker.handleInput("\r");
  357. expect(onSelect).toHaveBeenCalledWith(
  358. expect.objectContaining({ providerId: "minimax", modelId: "MiniMax-M3" }),
  359. );
  360. });
  361. it("keeps Think effort unchanged while left and right move the search cursor", () => {
  362. const onSelect = vi.fn();
  363. const picker = new TuiModelPicker(
  364. [
  365. {
  366. providerId: "provider",
  367. modelId: "model-a",
  368. displayName: "Model A",
  369. selected: true,
  370. effortOptions: ["low", "medium", "high"],
  371. },
  372. ],
  373. onSelect,
  374. vi.fn(),
  375. {},
  376. "low",
  377. );
  378. picker.handleInput("model");
  379. expect(stripAnsi(picker.render(90).join("\n"))).toContain("[ low ]");
  380. picker.handleInput("\u001b[C");
  381. expect(stripAnsi(picker.render(90).join("\n"))).toContain("[ low ]");
  382. picker.handleInput("\r");
  383. expect(onSelect).toHaveBeenCalledWith(
  384. expect.objectContaining({ modelId: "model-a" }),
  385. "low",
  386. );
  387. });
  388. it("renders Desktop-owned models and returns the selected model", () => {
  389. const onSelect = vi.fn();
  390. const picker = new TuiModelPicker(
  391. [
  392. {
  393. providerId: "minimax",
  394. modelId: "MiniMax-M2.7",
  395. displayName: "MiniMax M2.7",
  396. selected: true,
  397. contextLimit: 204800,
  398. },
  399. {
  400. providerId: "provider-b",
  401. modelId: "model-b",
  402. displayName: "Model B",
  403. },
  404. ],
  405. onSelect,
  406. vi.fn(),
  407. );
  408. expect(picker.render(90).join("\n")).toContain("Models");
  409. expect(picker.render(90).join("\n")).toContain("MiniMax M2.7");
  410. expect(picker.render(90).join("\n")).toContain("Available models");
  411. picker.handleInput("\r");
  412. expect(onSelect).toHaveBeenCalledWith(
  413. expect.objectContaining({
  414. providerId: "minimax",
  415. modelId: "MiniMax-M2.7",
  416. }),
  417. );
  418. });
  419. it("focuses the Runtime-selected model when reopening the picker", () => {
  420. const onSelect = vi.fn();
  421. const picker = new TuiModelPicker(
  422. [
  423. {
  424. providerId: "minimax",
  425. modelId: "MiniMax-M3",
  426. displayName: "MiniMax-M3",
  427. selected: false,
  428. },
  429. {
  430. providerId: "custom_provider:innerTest",
  431. modelId: "MiniMax-M3",
  432. displayName: "m3.05",
  433. selected: true,
  434. },
  435. ],
  436. onSelect,
  437. vi.fn(),
  438. );
  439. picker.handleInput("\r");
  440. expect(onSelect).toHaveBeenCalledWith(
  441. expect.objectContaining({
  442. providerId: "custom_provider:innerTest",
  443. displayName: "m3.05",
  444. }),
  445. );
  446. });
  447. it.each(["\u001B", "\x03"])(
  448. "supports a prefiltered model list and Pi cancel input %j without inventing fallback models",
  449. (input) => {
  450. const onCancel = vi.fn();
  451. const picker = new TuiModelPicker([], vi.fn(), onCancel);
  452. expect(picker.render(70).join("\n")).toContain("No matching models");
  453. picker.handleInput(input);
  454. expect(onCancel).toHaveBeenCalledOnce();
  455. },
  456. );
  457. it("keeps managed models visible but rejects them when login is required", () => {
  458. const onSelect = vi.fn();
  459. const onUnavailable = vi.fn();
  460. const picker = new TuiModelPicker(
  461. [
  462. {
  463. providerId: "minimax",
  464. modelId: "MiniMax-M2.7",
  465. displayName: "MiniMax M2.7",
  466. providerKind: "minimax-managed",
  467. },
  468. ],
  469. onSelect,
  470. vi.fn(),
  471. {
  472. isUnavailable: () => true,
  473. onUnavailable,
  474. },
  475. );
  476. expect(picker.render(90).join("\n")).toContain("Login required");
  477. picker.handleInput("\r");
  478. expect(onUnavailable).toHaveBeenCalledOnce();
  479. expect(onSelect).not.toHaveBeenCalled();
  480. });
  481. it("keeps switchable Thinking as a draft until Enter applies the model selection", () => {
  482. // Regression caught: treating Thinking as a model-name suffix or persisting arrow keys immediately.
  483. const onSelect = vi.fn();
  484. const picker = new TuiModelPicker(
  485. [
  486. {
  487. providerId: "minimax",
  488. modelId: "MiniMax-M3",
  489. displayName: "MiniMax-M3",
  490. selected: true,
  491. variant: "thinking",
  492. thinkingConfig: { mode: "switchable", defaultValue: "true" },
  493. },
  494. ],
  495. onSelect,
  496. vi.fn(),
  497. );
  498. expect(stripAnsi(picker.render(80).join("\n"))).toContain("Thinking");
  499. expect(stripAnsi(picker.render(80).join("\n"))).toContain("[ On ]");
  500. picker.handleInput("\u001b[D");
  501. expect(onSelect).not.toHaveBeenCalled();
  502. expect(stripAnsi(picker.render(80).join("\n"))).toContain("[ Off ]");
  503. picker.handleInput("\r");
  504. expect(onSelect).toHaveBeenCalledWith(
  505. expect.objectContaining({
  506. providerId: "minimax",
  507. modelId: "MiniMax-M3",
  508. variant: "",
  509. }),
  510. );
  511. });
  512. it("uses each highlighted model Thinking state without leaking the previous draft", () => {
  513. // Regression caught: carrying one model's unsaved Thinking choice into the next highlighted model.
  514. const onSelect = vi.fn();
  515. const picker = new TuiModelPicker(
  516. [
  517. {
  518. providerId: "minimax",
  519. modelId: "MiniMax-M3",
  520. displayName: "MiniMax-M3",
  521. variant: "thinking",
  522. thinkingConfig: { mode: "switchable", defaultValue: "true" },
  523. },
  524. {
  525. providerId: "provider-b",
  526. modelId: "model-b",
  527. displayName: "Model B",
  528. variant: "",
  529. thinkingConfig: { mode: "switchable", defaultValue: "false" },
  530. },
  531. ],
  532. onSelect,
  533. vi.fn(),
  534. );
  535. picker.handleInput("\u001b[D");
  536. picker.handleInput("\u001b[B");
  537. expect(stripAnsi(picker.render(80).join("\n"))).toContain("[ Off ]");
  538. picker.handleInput("\u001b[D");
  539. picker.handleInput("\u001b[A");
  540. expect(stripAnsi(picker.render(80).join("\n"))).toContain("[ Off ]");
  541. });
  542. it("keeps a stable panel height when highlighted models expose different controls", () => {
  543. const picker = new TuiModelPicker(
  544. [
  545. {
  546. providerId: "minimax",
  547. modelId: "MiniMax-M3",
  548. displayName: "MiniMax-M3",
  549. selected: true,
  550. variant: "thinking",
  551. thinkingConfig: { mode: "switchable", defaultValue: "true" },
  552. },
  553. {
  554. providerId: "minimax",
  555. modelId: "MiniMax-M2.7-highspeed",
  556. displayName: "MiniMax-M2.7-highspeed",
  557. thinkingConfig: { mode: "hidden" },
  558. },
  559. ],
  560. vi.fn(),
  561. vi.fn(),
  562. );
  563. const thinkingRows = picker.render(90);
  564. expect(stripAnsi(thinkingRows.join("\n"))).toContain("Thinking");
  565. picker.handleInput("\u001b[B");
  566. const plainRows = picker.render(90);
  567. expect(stripAnsi(plainRows.join("\n"))).not.toContain("Thinking");
  568. expect(plainRows).toHaveLength(thinkingRows.length);
  569. });
  570. it("renders forced Thinking as read only and hides undisclosed Thinking configuration", () => {
  571. // Regression caught: exposing a switch for forced or hidden provider policy.
  572. const onSelect = vi.fn();
  573. const forced = new TuiModelPicker(
  574. [
  575. {
  576. providerId: "minimax",
  577. modelId: "MiniMax-M3",
  578. displayName: "MiniMax-M3",
  579. variant: "thinking",
  580. thinkingConfig: { mode: "forced_on" },
  581. },
  582. ],
  583. onSelect,
  584. vi.fn(),
  585. );
  586. const hidden = new TuiModelPicker(
  587. [
  588. {
  589. providerId: "provider-b",
  590. modelId: "model-b",
  591. displayName: "Model B",
  592. thinkingConfig: { mode: "hidden" },
  593. },
  594. ],
  595. vi.fn(),
  596. vi.fn(),
  597. );
  598. expect(stripAnsi(forced.render(80).join("\n"))).toContain("Thinking On");
  599. expect(stripAnsi(forced.render(80).join("\n"))).not.toContain("←/→");
  600. forced.handleInput("\u001b[D");
  601. forced.handleInput("\r");
  602. expect(onSelect).toHaveBeenCalledWith(
  603. expect.objectContaining({ variant: "thinking" }),
  604. );
  605. expect(stripAnsi(hidden.render(80).join("\n"))).not.toContain("Thinking");
  606. });
  607. it("keeps catalog, empty, and footer states inside a narrow interaction card", () => {
  608. const picker = new TuiModelPicker(
  609. [
  610. {
  611. providerId: "provider-with-a-long-name",
  612. modelId: "model-with-a-deliberately-long-name",
  613. displayName: "A deliberately long Desktop model display name",
  614. },
  615. ],
  616. vi.fn(),
  617. vi.fn(),
  618. );
  619. const empty = new TuiModelPicker([], vi.fn(), vi.fn());
  620. expect(picker.render(24).every((line) => visibleWidth(line) <= 24)).toBe(
  621. true,
  622. );
  623. expect(empty.render(24).every((line) => visibleWidth(line) <= 24)).toBe(
  624. true,
  625. );
  626. });
  627. it("keeps provider deletion confirmation inside a narrow interaction card", () => {
  628. const picker = new TuiModelPicker(
  629. [
  630. {
  631. providerId: "custom_provider:provider-with-a-long-name",
  632. providerName: "Provider with a deliberately long display name",
  633. providerSource: "custom_provider",
  634. providerKind: "custom",
  635. modelId: "model-a",
  636. displayName: "Model A",
  637. },
  638. ],
  639. vi.fn(),
  640. vi.fn(),
  641. {
  642. onDeleteProvider: vi.fn(async () => undefined),
  643. requestRender: vi.fn(),
  644. },
  645. );
  646. picker.handleInput("\x04");
  647. expect(picker.render(24).every((line) => visibleWidth(line) <= 24)).toBe(
  648. true,
  649. );
  650. });
  651. it("keeps think effort as a draft until Enter applies it", () => {
  652. const onSelect = vi.fn();
  653. const picker = new TuiModelPicker(
  654. [
  655. {
  656. providerId: "custom_provider:byok",
  657. modelId: "byok-large-5",
  658. displayName: "byok-large-5",
  659. selected: true,
  660. effortOptions: ["low", "medium", "high", "xhigh", "max"],
  661. },
  662. ],
  663. onSelect,
  664. vi.fn(),
  665. );
  666. // Middle level mirrors what Runtime applies without an explicit choice.
  667. expect(stripAnsi(picker.render(80).join("\n"))).toContain("Think effort");
  668. expect(stripAnsi(picker.render(80).join("\n"))).toContain("[ high ]");
  669. picker.handleInput("\u001b[C");
  670. expect(onSelect).not.toHaveBeenCalled();
  671. expect(stripAnsi(picker.render(80).join("\n"))).toContain("[ xhigh ]");
  672. picker.handleInput("\r");
  673. expect(onSelect).toHaveBeenCalledWith(
  674. expect.objectContaining({ modelId: "byok-large-5" }),
  675. "xhigh",
  676. );
  677. });
  678. it("starts the selected row from the Session effort and clamps at the ends", () => {
  679. const onSelect = vi.fn();
  680. const picker = new TuiModelPicker(
  681. [
  682. {
  683. providerId: "custom_provider:byok",
  684. modelId: "byok-large-5",
  685. displayName: "byok-large-5",
  686. selected: true,
  687. effortOptions: ["low", "medium", "high"],
  688. },
  689. ],
  690. onSelect,
  691. vi.fn(),
  692. {},
  693. "low",
  694. );
  695. expect(stripAnsi(picker.render(80).join("\n"))).toContain("[ low ]");
  696. picker.handleInput("\u001b[D");
  697. expect(stripAnsi(picker.render(80).join("\n"))).toContain("[ low ]");
  698. picker.handleInput("\r");
  699. expect(onSelect).toHaveBeenCalledWith(
  700. expect.objectContaining({ modelId: "byok-large-5" }),
  701. "low",
  702. );
  703. });
  704. it("uses effort as the only control when legacy metadata also exposes Thinking", () => {
  705. const onSelect = vi.fn();
  706. const picker = new TuiModelPicker(
  707. [
  708. {
  709. providerId: "custom_provider:byok",
  710. modelId: "byok-large-5",
  711. displayName: "byok-large-5",
  712. selected: true,
  713. variant: "",
  714. thinkingConfig: { mode: "switchable", defaultValue: "false" },
  715. effortOptions: ["low", "medium", "high"],
  716. },
  717. ],
  718. onSelect,
  719. vi.fn(),
  720. );
  721. const rendered = stripAnsi(picker.render(90).join("\n"));
  722. expect(rendered).toContain("←/→ effort");
  723. expect(rendered).not.toContain("t thinking");
  724. expect(rendered).not.toContain("Thinking (");
  725. expect(rendered).not.toContain("[ Off ]");
  726. picker.handleInput("\u001b[D");
  727. expect(stripAnsi(picker.render(90).join("\n"))).toContain("[ low ]");
  728. picker.handleInput("\r");
  729. expect(onSelect).toHaveBeenCalledWith(
  730. expect.objectContaining({ modelId: "byok-large-5", variant: "" }),
  731. "low",
  732. );
  733. });
  734. it("applies the configured effort when a thinking-on model is confirmed without touching effort", () => {
  735. const onSelect = vi.fn();
  736. const picker = new TuiModelPicker(
  737. [
  738. {
  739. providerId: "minimax",
  740. modelId: "deep-reasoner-1",
  741. displayName: "deep-reasoner-1",
  742. variant: "thinking",
  743. thinkingConfig: { mode: "switchable", defaultValue: "true" },
  744. effortOptions: ["xhigh"],
  745. },
  746. ],
  747. onSelect,
  748. vi.fn(),
  749. );
  750. picker.handleInput("\r");
  751. // Thinking is already on, so the Run must carry the configured level. Sending
  752. // the model without an effort lets the provider fall back to its own default.
  753. expect(onSelect).toHaveBeenCalledWith(
  754. expect.objectContaining({ modelId: "deep-reasoner-1" }),
  755. "xhigh",
  756. );
  757. });
  758. it("applies the configured effort when a forced-on model is confirmed", () => {
  759. const onSelect = vi.fn();
  760. const picker = new TuiModelPicker(
  761. [
  762. {
  763. providerId: "minimax",
  764. modelId: "deep-reasoner-1",
  765. displayName: "deep-reasoner-1",
  766. variant: "thinking",
  767. thinkingConfig: { mode: "forced_on" },
  768. effortOptions: ["xhigh"],
  769. },
  770. ],
  771. onSelect,
  772. vi.fn(),
  773. );
  774. picker.handleInput("\r");
  775. expect(onSelect).toHaveBeenCalledWith(
  776. expect.objectContaining({ modelId: "deep-reasoner-1" }),
  777. "xhigh",
  778. );
  779. });
  780. it("keeps an explicit legacy variant when Enter is pressed without choosing effort", () => {
  781. const onSelect = vi.fn();
  782. const picker = new TuiModelPicker(
  783. [
  784. {
  785. providerId: "custom_provider:byok",
  786. modelId: "byok-large-5",
  787. displayName: "byok-large-5",
  788. selected: true,
  789. variant: "",
  790. thinkingConfig: { mode: "switchable", defaultValue: "false" },
  791. effortOptions: ["low", "medium", "high"],
  792. },
  793. ],
  794. onSelect,
  795. vi.fn(),
  796. );
  797. const rendered = stripAnsi(picker.render(90).join("\n"));
  798. expect(rendered).not.toMatch(/\[\s*(?:low|medium|high)\s*\]/u);
  799. picker.handleInput("\r");
  800. expect(onSelect).toHaveBeenCalledWith(
  801. expect.objectContaining({ modelId: "byok-large-5", variant: "" }),
  802. );
  803. });
  804. it("shows a fixed catalog effort without offering editable levels", () => {
  805. const onSelect = vi.fn();
  806. const model = {
  807. providerId: "minimax",
  808. modelId: "fixed",
  809. defaultEffort: "max",
  810. thinkingConfig: { mode: "forced_on" },
  811. };
  812. const picker = new TuiModelPicker([model], onSelect, vi.fn());
  813. const rendered = stripAnsi(picker.render(100).join("\n"));
  814. expect(rendered).toContain("Effort max (fixed)");
  815. expect(rendered).not.toContain("Thinking On");
  816. expect(rendered).not.toContain("←/→ effort");
  817. picker.handleInput("\r");
  818. expect(onSelect).toHaveBeenCalledWith(model, "max");
  819. });
  820. it("keeps the thinking switch usable alongside a fixed default effort", () => {
  821. const onSelect = vi.fn();
  822. const model = {
  823. providerId: "minimax",
  824. modelId: "fixed-switchable",
  825. defaultEffort: "max",
  826. thinkingConfig: { mode: "switchable", defaultValue: "true" },
  827. };
  828. const picker = new TuiModelPicker([model], onSelect, vi.fn());
  829. expect(stripAnsi(picker.render(100).join("\n"))).toContain(
  830. "Effort max (fixed)",
  831. );
  832. picker.handleInput("\x1b[C");
  833. expect(stripAnsi(picker.render(100).join("\n"))).not.toContain(
  834. "Effort max",
  835. );
  836. picker.handleInput("\r");
  837. expect(onSelect).toHaveBeenCalledWith({ ...model, variant: "" });
  838. const offModel = { ...model, variant: "" };
  839. const enable = new TuiModelPicker([offModel], onSelect, vi.fn());
  840. enable.handleInput("\x1b[C");
  841. expect(stripAnsi(enable.render(100).join("\n"))).toContain(
  842. "Effort max (fixed)",
  843. );
  844. enable.handleInput("\r");
  845. expect(onSelect).toHaveBeenLastCalledWith(
  846. { ...model, variant: "thinking" },
  847. "max",
  848. );
  849. });
  850. it("leaves models without effort levels untouched", () => {
  851. const onSelect = vi.fn();
  852. const picker = new TuiModelPicker(
  853. [
  854. {
  855. providerId: "minimax",
  856. modelId: "MiniMax-M3",
  857. displayName: "MiniMax-M3",
  858. selected: true,
  859. variant: "thinking",
  860. thinkingConfig: { mode: "switchable", defaultValue: "true" },
  861. },
  862. ],
  863. onSelect,
  864. vi.fn(),
  865. {},
  866. "high",
  867. );
  868. const rendered = stripAnsi(picker.render(80).join("\n"));
  869. expect(rendered).not.toContain("Think effort");
  870. expect(rendered).toContain("←/→ thinking");
  871. picker.handleInput("\u001b[D");
  872. picker.handleInput("\r");
  873. expect(onSelect).toHaveBeenCalledWith(
  874. expect.objectContaining({ variant: "" }),
  875. );
  876. });
  877. });