thoughtLevelCycleControl.test.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import { createElement, type ReactElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { ThoughtLevelCycleControl } from "@/chat-input-toolbar/ThoughtLevelCycleControl.js";
  5. import { getNextThoughtLevelValue } from "@/chat-input-toolbar/thoughtLevelOptions.js";
  6. import { TooltipProvider } from "@/components/ui/tooltip.js";
  7. import type { ZCodeConfigOption } from "@zcode/shared";
  8. const zhIntl = {
  9. formatMessage({ id }: { id: string }) {
  10. return (
  11. {
  12. "chat.toolbar.thoughtLevel.tooltip": "思考级别",
  13. "chat.toolbar.thoughtLevel.placeholder": "选择思考档位",
  14. "chat.toolbar.thoughtLevel.value.max": "最高",
  15. }[id] ?? id
  16. );
  17. },
  18. };
  19. function renderWithTooltipProvider(element: ReactElement) {
  20. return renderToStaticMarkup(createElement(TooltipProvider, null, element));
  21. }
  22. describe("ThoughtLevelCycleControl", () => {
  23. it.each([
  24. ["high", "100%"],
  25. ["off", "0%"],
  26. ["ultra", "50%"],
  27. ["missing", "0%"],
  28. ])("关闭项在中间时 %s 的阶梯进度为 %s", (currentValue, progress) => {
  29. const html = renderWithTooltipProvider(
  30. createElement(ThoughtLevelCycleControl, {
  31. option: {
  32. id: "thought_level",
  33. category: "thought_level",
  34. type: "select",
  35. currentValue,
  36. options: [
  37. { value: "ultra", name: "Ultra" },
  38. { value: "off", name: "Off" },
  39. { value: "high", name: "High" },
  40. ],
  41. },
  42. intl: zhIntl,
  43. triggerRef: { current: null },
  44. onValueChange: vi.fn(),
  45. }),
  46. );
  47. expect(html).toContain(`height:${progress}`);
  48. });
  49. it.each([false, true])(
  50. "空值显示待选择而非第一档,保留非法值模式=%s",
  51. (showInvalidCurrentValue) => {
  52. const html = renderWithTooltipProvider(
  53. createElement(ThoughtLevelCycleControl, {
  54. option: {
  55. id: "thought_level",
  56. type: "select",
  57. currentValue: "",
  58. options: [{ value: "max", name: "Max" }],
  59. },
  60. intl: zhIntl,
  61. labelVisibilityClassName: "",
  62. showInvalidCurrentValue,
  63. triggerRef: { current: null },
  64. onValueChange: vi.fn(),
  65. }),
  66. );
  67. expect(html).toContain('aria-label="选择思考档位"');
  68. expect(html).toContain('role="combobox"');
  69. expect(html).not.toContain('data-thought-level-fixed="true"');
  70. expect(html).toContain("height:0%");
  71. },
  72. );
  73. it("单档位只读展示,不渲染可展开的选择器", () => {
  74. const option: ZCodeConfigOption = {
  75. id: "thought_level",
  76. category: "thought_level",
  77. type: "select",
  78. currentValue: "max",
  79. options: [{ value: "max", name: "Max" }],
  80. };
  81. const html = renderWithTooltipProvider(
  82. createElement(ThoughtLevelCycleControl, {
  83. option,
  84. intl: zhIntl,
  85. labelVisibilityClassName: "",
  86. triggerRef: { current: null },
  87. onValueChange: vi.fn(),
  88. }),
  89. );
  90. expect(html).toContain('data-thought-level-fixed="true"');
  91. expect(html).toContain("最高");
  92. expect(html).not.toContain('role="combobox"');
  93. });
  94. it("非法历史值遇到单档模型时仍渲染选择器以便修正", () => {
  95. const option: ZCodeConfigOption = {
  96. id: "thought_level",
  97. category: "thought_level",
  98. type: "select",
  99. currentValue: "medium",
  100. options: [{ value: "max", name: "Max" }],
  101. };
  102. const html = renderWithTooltipProvider(
  103. createElement(ThoughtLevelCycleControl, {
  104. option,
  105. intl: zhIntl,
  106. labelVisibilityClassName: "",
  107. showInvalidCurrentValue: true,
  108. triggerRef: { current: null },
  109. onValueChange: vi.fn(),
  110. }),
  111. );
  112. expect(html).not.toContain('data-thought-level-fixed="true"');
  113. expect(html).toContain('role="combobox"');
  114. expect(html).toContain("medium");
  115. expect(html).toContain("height:0%");
  116. });
  117. it("显示当前思考阶梯文案", () => {
  118. const option: ZCodeConfigOption = {
  119. id: "thought_level",
  120. category: "thought_level",
  121. type: "select",
  122. currentValue: "max",
  123. options: [
  124. { value: "off", name: "Off" },
  125. { value: "max", name: "Max" },
  126. ],
  127. };
  128. const html = renderWithTooltipProvider(
  129. createElement(ThoughtLevelCycleControl, {
  130. option,
  131. intl: zhIntl,
  132. labelVisibilityClassName: "",
  133. triggerRef: { current: null },
  134. onValueChange: vi.fn(),
  135. }),
  136. );
  137. expect(html).toContain("最高");
  138. });
  139. it("进度条仅在 sm 到 xl 的中间宽度显示", () => {
  140. const option: ZCodeConfigOption = {
  141. id: "thought_level",
  142. category: "thought_level",
  143. type: "select",
  144. currentValue: "max",
  145. options: [
  146. { value: "off", name: "Off" },
  147. { value: "max", name: "Max" },
  148. ],
  149. };
  150. const html = renderWithTooltipProvider(
  151. createElement(ThoughtLevelCycleControl, {
  152. option,
  153. intl: zhIntl,
  154. triggerRef: { current: null },
  155. onValueChange: vi.fn(),
  156. }),
  157. );
  158. expect(html).toContain("hidden @sm/composer:inline-flex @xl/composer:hidden");
  159. expect(html).not.toContain("@lg/composer:hidden");
  160. });
  161. it("允许 composer 先隐藏箭头并在超窄宽度收成正方形", () => {
  162. const option: ZCodeConfigOption = {
  163. id: "thought_level",
  164. category: "thought_level",
  165. type: "select",
  166. currentValue: "max",
  167. options: [
  168. { value: "off", name: "Off" },
  169. { value: "max", name: "Max" },
  170. ],
  171. };
  172. const html = renderWithTooltipProvider(
  173. createElement(ThoughtLevelCycleControl, {
  174. option,
  175. intl: zhIntl,
  176. indicatorClassName: "hidden @xl/composer:block",
  177. triggerClassName:
  178. "@max-sm/composer:size-7 @max-sm/composer:justify-center @max-sm/composer:p-0",
  179. triggerRef: { current: null },
  180. onValueChange: vi.fn(),
  181. }),
  182. );
  183. expect(html).toContain("lucide-chevron-down");
  184. expect(html).toContain("hidden @xl/composer:block");
  185. expect(html).toContain("gap-1 rounded-lg px-1.5 py-1.5");
  186. expect(html).toContain("@max-sm/composer:size-7");
  187. expect(html).toContain("@max-sm/composer:justify-center");
  188. expect(html).toContain("@max-sm/composer:p-0");
  189. });
  190. it("文字收起时仍保留当前阶梯文案用于 tooltip 和可访问信息", () => {
  191. const option: ZCodeConfigOption = {
  192. id: "thought_level",
  193. category: "thought_level",
  194. type: "select",
  195. currentValue: "max",
  196. options: [
  197. { value: "off", name: "Off" },
  198. { value: "max", name: "Max" },
  199. ],
  200. };
  201. const html = renderWithTooltipProvider(
  202. createElement(ThoughtLevelCycleControl, {
  203. option,
  204. intl: zhIntl,
  205. labelVisibilityClassName: "hidden",
  206. triggerRef: { current: null },
  207. onValueChange: vi.fn(),
  208. }),
  209. );
  210. expect(html).toContain('aria-label="最高"');
  211. expect(html).toContain('title="最高"');
  212. expect(html).toContain("最高");
  213. });
  214. it("保留 cycle 模式用于恢复旧版点击循环交互", () => {
  215. const option: ZCodeConfigOption = {
  216. id: "thought_level",
  217. category: "thought_level",
  218. type: "select",
  219. currentValue: "max",
  220. options: [
  221. { value: "off", name: "Off" },
  222. { value: "max", name: "Max" },
  223. ],
  224. };
  225. const html = renderWithTooltipProvider(
  226. createElement(ThoughtLevelCycleControl, {
  227. option,
  228. intl: zhIntl,
  229. interactionMode: "cycle",
  230. labelVisibilityClassName: "",
  231. triggerRef: { current: null },
  232. onValueChange: vi.fn(),
  233. }),
  234. );
  235. expect(html).toContain('type="button"');
  236. expect(html).toContain("最高");
  237. });
  238. it("Ctrl+T 快速切换按展示顺序选择下一个思考档位", () => {
  239. const option: ZCodeConfigOption = {
  240. id: "thought_level",
  241. category: "thought_level",
  242. type: "select",
  243. currentValue: "low",
  244. options: [
  245. { value: "high", name: "High" },
  246. { value: "off", name: "Off" },
  247. { value: "low", name: "Low" },
  248. ],
  249. };
  250. expect(getNextThoughtLevelValue(option)).toBe("high");
  251. });
  252. it("Ctrl+T 不循环唯一思考档位", () => {
  253. expect(
  254. getNextThoughtLevelValue({
  255. type: "select",
  256. currentValue: "max",
  257. options: [{ value: "max", name: "Max" }],
  258. }),
  259. ).toBeNull();
  260. });
  261. });