tui-model-suggestions.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { completeModelCommand } from "../src/app-keyboard.js";
  4. import { palette } from "../src/app-model.js";
  5. import { modelOptionValue } from "../src/app-model-ref.js";
  6. import { ModelSuggestionPanel } from "../src/app-model-suggestion-panel.js";
  7. import {
  8. filterModelOptions,
  9. modelCommandQuery,
  10. selectedModelOption,
  11. visibleModelOptionWindow,
  12. } from "../src/app-input.js";
  13. import type { TuiModelOption } from "../src/types.js";
  14. // 模型选项改为协议 Model Option(ref + label),别名(main/lite)已移除;
  15. // 行尾元信息改由 disabledReason 与 current 组成。
  16. function modelOption(
  17. providerId: string,
  18. modelId: string,
  19. label: string,
  20. disabledReason?: string,
  21. ): TuiModelOption {
  22. return {
  23. ref: { providerId, modelId },
  24. label,
  25. properties: {
  26. inputFormat: {
  27. supportsText: true,
  28. supportsImage: false,
  29. supportsVideo: false,
  30. supportsAudio: false,
  31. supportsPdf: false,
  32. },
  33. outputFormat: { supportsText: true },
  34. },
  35. ...(disabledReason ? { disabledReason } : {}),
  36. };
  37. }
  38. const models: TuiModelOption[] = [
  39. modelOption("openai", "gpt-main", "gpt-main"),
  40. modelOption("openai", "gpt-lite", "gpt-lite", "no plan"),
  41. modelOption("anthropic", "claude-sonnet-4", "Sonnet"),
  42. ];
  43. test("model command popup filters model choices from the composer draft", () => {
  44. assert.equal(modelCommandQuery("/model"), "");
  45. assert.equal(modelCommandQuery("/model gpt"), "gpt");
  46. assert.equal(modelCommandQuery("/modelclaude"), "claude");
  47. assert.equal(modelCommandQuery("/mode"), undefined);
  48. assert.deepEqual(filterModelOptions("/model lite", models).map(modelOptionValue), [
  49. "openai/gpt-lite",
  50. ]);
  51. assert.deepEqual(filterModelOptions("/model claude", models).map(modelOptionValue), [
  52. "anthropic/claude-sonnet-4",
  53. ]);
  54. });
  55. test("model command popup selection completes and submits explicit model commands", () => {
  56. let draft = "";
  57. assert.equal(
  58. completeModelCommand(models, { selectedIndex: 1 }, (value) => {
  59. draft = value;
  60. }),
  61. true,
  62. );
  63. assert.equal(draft, "/model openai/gpt-lite");
  64. const selected = selectedModelOption(
  65. "/model gpt",
  66. { selectedIndex: 1 },
  67. filterModelOptions("/model gpt", models),
  68. );
  69. assert.equal(selected && modelOptionValue(selected), "openai/gpt-lite");
  70. });
  71. test("keeps selected model option inside the visible window", () => {
  72. const manyModels: TuiModelOption[] = Array.from({ length: 10 }, (_, index) => ({
  73. id: `provider/model-${index}`,
  74. }));
  75. assert.deepEqual(visibleModelOptionWindow(manyModels, 9, 8), {
  76. models: manyModels.slice(2, 10),
  77. selectedIndex: 7,
  78. startIndex: 2,
  79. });
  80. });
  81. test("renders model suggestions with model names first and muted right providers", () => {
  82. const panel = ModelSuggestionPanel({
  83. contentWidth: 34,
  84. currentModel: "openai/gpt-main",
  85. models,
  86. selectedIndex: 1,
  87. });
  88. assert.ok(panel);
  89. const lines = collectTextNodes(panel);
  90. assert.deepEqual(
  91. lines.map((line) => line.text),
  92. [
  93. " ",
  94. "gpt-main",
  95. " current",
  96. "openai",
  97. "> ",
  98. "gpt-lite",
  99. " no plan",
  100. "openai",
  101. " ",
  102. "Sonnet",
  103. "anthropic",
  104. ],
  105. );
  106. assert.equal(lines[5]?.style.fg, palette.accent);
  107. assert.equal(lines[7]?.style.fg, palette.muted);
  108. assert.equal(lines[7]?.style.flexShrink, 0);
  109. });
  110. function collectTextNodes(node: unknown): Array<{ style: Record<string, unknown>; text: string }> {
  111. const lines: ReturnType<typeof collectTextNodes> = [];
  112. const visit = (value: unknown) => {
  113. if (Array.isArray(value)) {
  114. for (const child of value) visit(child);
  115. return;
  116. }
  117. if (typeof value !== "object" || value === null || !("props" in value)) return;
  118. const element = value as {
  119. props?: {
  120. children?: unknown;
  121. style?: Record<string, unknown>;
  122. };
  123. type?: unknown;
  124. };
  125. if (typeof element.type === "function") {
  126. visit(element.type(element.props));
  127. return;
  128. }
  129. if (element.type === "text") {
  130. lines.push({
  131. style: element.props?.style ?? {},
  132. text: collectTextLines(element.props?.children).join(""),
  133. });
  134. }
  135. visit(element.props?.children);
  136. };
  137. visit(node);
  138. return lines;
  139. }
  140. function collectTextLines(node: unknown): string[] {
  141. const lines: string[] = [];
  142. const visit = (value: unknown) => {
  143. if (typeof value === "string") {
  144. lines.push(value);
  145. return;
  146. }
  147. if (Array.isArray(value)) {
  148. for (const child of value) visit(child);
  149. return;
  150. }
  151. if (typeof value === "object" && value !== null && "props" in value) {
  152. visit((value as { props?: { children?: unknown } }).props?.children);
  153. }
  154. };
  155. visit(node);
  156. return lines;
  157. }