tui-model-picker.test.ts 28 KB

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