thoughtLevelCycleControl.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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("文字收起时仍保留当前阶梯文案用于 tooltip 和可访问信息", () => {
  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: "hidden",
  133. triggerRef: { current: null },
  134. onValueChange: vi.fn(),
  135. }),
  136. );
  137. expect(html).toContain('aria-label="最高"');
  138. expect(html).toContain('title="最高"');
  139. expect(html).toContain("最高");
  140. });
  141. it("保留 cycle 模式用于恢复旧版点击循环交互", () => {
  142. const option: ZCodeConfigOption = {
  143. id: "thought_level",
  144. category: "thought_level",
  145. type: "select",
  146. currentValue: "max",
  147. options: [
  148. { value: "off", name: "Off" },
  149. { value: "max", name: "Max" },
  150. ],
  151. };
  152. const html = renderWithTooltipProvider(
  153. createElement(ThoughtLevelCycleControl, {
  154. option,
  155. intl: zhIntl,
  156. interactionMode: "cycle",
  157. labelVisibilityClassName: "",
  158. triggerRef: { current: null },
  159. onValueChange: vi.fn(),
  160. }),
  161. );
  162. expect(html).toContain('type="button"');
  163. expect(html).toContain("最高");
  164. });
  165. it("Ctrl+T 快速切换按展示顺序选择下一个思考档位", () => {
  166. const option: ZCodeConfigOption = {
  167. id: "thought_level",
  168. category: "thought_level",
  169. type: "select",
  170. currentValue: "low",
  171. options: [
  172. { value: "high", name: "High" },
  173. { value: "off", name: "Off" },
  174. { value: "low", name: "Low" },
  175. ],
  176. };
  177. expect(getNextThoughtLevelValue(option)).toBe("high");
  178. });
  179. it("Ctrl+T 不循环唯一思考档位", () => {
  180. expect(
  181. getNextThoughtLevelValue({
  182. type: "select",
  183. currentValue: "max",
  184. options: [{ value: "max", name: "Max" }],
  185. }),
  186. ).toBeNull();
  187. });
  188. });