reasoningLayout.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import type { IBroadcastService, IServiceAccessor } from "@zcode/services";
  2. import type { IPlatformService } from "@zcode/shared";
  3. import { PlatformProvider } from "@/hooks/usePlatform.js";
  4. import { ServiceProvider } from "@/hooks/useServices.js";
  5. import { createElement } from "react";
  6. import type { ComponentProps } from "react";
  7. import { renderToStaticMarkup } from "react-dom/server";
  8. import { describe, expect, it } from "vitest";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. import { StoreProvider } from "@/store/StoreProvider.js";
  11. import {
  12. getReasoningBottomDistance,
  13. getReasoningSummaryMaskStyle,
  14. isReasoningScrollAtBottom,
  15. Reasoning,
  16. ReasoningContent,
  17. ReasoningTrigger,
  18. isReasoningSummaryOverflowing,
  19. resolveReasoningStreamingSummary,
  20. shouldAutoCollapseReasoning,
  21. } from "../src/components/ai-elements/reasoning.js";
  22. const mockServices = {} as IServiceAccessor;
  23. const mockPlatform = {
  24. reportTelemetryEvent: async () => {},
  25. } as unknown as IPlatformService;
  26. const mockBroadcastService: IBroadcastService = {
  27. send: async () => {},
  28. onMessage: () => ({ dispose: () => {} }),
  29. };
  30. const localStorageState = new Map<string, string>();
  31. Object.defineProperty(globalThis, "localStorage", {
  32. configurable: true,
  33. value: {
  34. clear: () => localStorageState.clear(),
  35. getItem: (key: string) => localStorageState.get(key) ?? null,
  36. removeItem: (key: string) => {
  37. localStorageState.delete(key);
  38. },
  39. setItem: (key: string, value: string) => {
  40. localStorageState.set(key, value);
  41. },
  42. },
  43. });
  44. Object.defineProperty(globalThis, "document", {
  45. configurable: true,
  46. value: {
  47. documentElement: {
  48. classList: {
  49. contains: () => false,
  50. toggle: () => {},
  51. },
  52. },
  53. },
  54. });
  55. function renderReasoningContent(
  56. content: string,
  57. reasoningProps: Partial<ComponentProps<typeof Reasoning>> = {},
  58. contentProps: Partial<ComponentProps<typeof ReasoningContent>> = {},
  59. locale: "zh-CN" | "en-US" = "zh-CN",
  60. ) {
  61. return renderToStaticMarkup(
  62. createElement(
  63. ZCodeIntlProvider,
  64. { initialLocale: locale },
  65. createElement(
  66. ServiceProvider,
  67. { services: mockServices },
  68. createElement(
  69. PlatformProvider,
  70. { platform: mockPlatform },
  71. createElement(
  72. StoreProvider,
  73. { broadcastService: mockBroadcastService },
  74. createElement(
  75. Reasoning,
  76. { className: "w-full", defaultOpen: true, ...reasoningProps },
  77. createElement(ReasoningTrigger, { streamingText: content }),
  78. createElement(ReasoningContent, contentProps, content),
  79. ),
  80. ),
  81. ),
  82. ),
  83. ),
  84. );
  85. }
  86. describe("Reasoning layout", () => {
  87. it("renders expanded thought content as plain text instead of Markdown", () => {
  88. const html = renderReasoningContent("**阶段一**\n\n# 计划\n`命令`");
  89. expect(html).toContain("**阶段一**");
  90. expect(html).toContain("# 计划");
  91. expect(html).toContain("`命令`");
  92. expect(html).not.toContain("<strong>");
  93. expect(html).not.toContain("<h1");
  94. expect(html).not.toContain("<code>");
  95. });
  96. it("auto collapses only for a new output boundary without user interaction", () => {
  97. expect(
  98. shouldAutoCollapseReasoning({
  99. autoCollapseKey: "content:1",
  100. previousAutoCollapseKey: null,
  101. userInteracted: false,
  102. }),
  103. ).toBe(true);
  104. expect(
  105. shouldAutoCollapseReasoning({
  106. autoCollapseKey: "content:1",
  107. previousAutoCollapseKey: "content:1",
  108. userInteracted: false,
  109. }),
  110. ).toBe(false);
  111. expect(
  112. shouldAutoCollapseReasoning({
  113. autoCollapseKey: "content:1",
  114. previousAutoCollapseKey: null,
  115. userInteracted: true,
  116. }),
  117. ).toBe(false);
  118. expect(
  119. shouldAutoCollapseReasoning({
  120. autoCollapseKey: null,
  121. previousAutoCollapseKey: null,
  122. userInteracted: false,
  123. }),
  124. ).toBe(false);
  125. });
  126. it("removes the left guide and indentation for nested reasoning content", () => {
  127. const html = renderReasoningContent("CUA 子思考。", {}, { variant: "nested" });
  128. expect(html).toContain('data-reasoning-content-variant="nested"');
  129. expect(html).not.toContain("border-l");
  130. expect(html).not.toContain("ml-2");
  131. expect(html).not.toContain("pl-3.5");
  132. });
  133. it("renders only the latest reasoning line without converting it to a break", () => {
  134. const html = renderReasoningContent("正在分析输入。\n继续检查实现。", {
  135. defaultOpen: false,
  136. isStreaming: true,
  137. });
  138. expect(html).toContain("正在思考");
  139. expect(html).not.toContain("思考过程");
  140. expect(html).toContain("animated-gradient-text");
  141. expect(html).not.toContain("正在分析输入。");
  142. expect(html).toContain("继续检查实现。");
  143. expect(html).not.toContain("<br");
  144. expect(html).toContain('data-state="closed"');
  145. expect(html).toContain('data-reasoning-label="true"');
  146. expect(html).toContain('data-reasoning-streaming-text="true"');
  147. expect(html).toContain('data-reasoning-streaming-roll="true"');
  148. });
  149. it("derives a stable rolling key per non-empty reasoning line", () => {
  150. expect(resolveReasoningStreamingSummary("第一行正在生成")).toEqual({
  151. key: "0",
  152. text: "第一行正在生成",
  153. });
  154. expect(resolveReasoningStreamingSummary("第一行完成\n\n第二行正在生成")).toEqual({
  155. key: "2",
  156. text: "第二行正在生成",
  157. });
  158. expect(resolveReasoningStreamingSummary(" \n\t ")).toBeNull();
  159. });
  160. it("shows elapsed duration instead of the inline summary while streaming is expanded", () => {
  161. const html = renderReasoningContent("正在分析输入。", {
  162. defaultOpen: true,
  163. duration: 7,
  164. isStreaming: true,
  165. });
  166. expect(html).toContain("思考");
  167. expect(html).not.toContain("思考过程");
  168. expect(html).toContain("持续了 7 秒");
  169. expect(html).toContain('data-state="open"');
  170. expect(html).not.toContain("animated-gradient-text");
  171. expect(html).not.toContain('data-reasoning-streaming-line="true"');
  172. });
  173. it("renders the English duration directly without a leading preposition", () => {
  174. const html = renderReasoningContent(
  175. "Checking the implementation.",
  176. { defaultOpen: false, duration: 2, isStreaming: false },
  177. {},
  178. "en-US",
  179. );
  180. expect(html).toContain("Thought");
  181. expect(html).toContain("2 seconds");
  182. expect(html).not.toContain("for 2 seconds");
  183. });
  184. it("adds a two-sided fade mask only while the streaming summary overflows", () => {
  185. expect(isReasoningSummaryOverflowing({ clientWidth: 240, scrollWidth: 240 })).toBe(false);
  186. expect(isReasoningSummaryOverflowing({ clientWidth: 240, scrollWidth: 420 })).toBe(true);
  187. expect(getReasoningSummaryMaskStyle(false)).toBeUndefined();
  188. expect(getReasoningSummaryMaskStyle(true)).toMatchObject({
  189. WebkitMaskImage:
  190. "linear-gradient(to right, transparent 0, black 16px, black calc(100% - 16px), transparent 100%)",
  191. maskImage:
  192. "linear-gradient(to right, transparent 0, black 16px, black calc(100% - 16px), transparent 100%)",
  193. });
  194. });
  195. it("keeps completed reasoning closed when defaultOpen is false", () => {
  196. const html = renderReasoningContent("已经完成的思考。", {
  197. defaultOpen: false,
  198. isStreaming: false,
  199. });
  200. expect(html).toContain("思考");
  201. expect(html).not.toContain("思考过程");
  202. expect(html).toContain("持续了");
  203. expect(html).not.toContain("animated-gradient-text");
  204. expect(html).not.toContain("已经完成的思考。");
  205. expect(html).toContain('data-state="closed"');
  206. });
  207. it("treats only the bottom lock zone as auto-followable", () => {
  208. expect(
  209. getReasoningBottomDistance({
  210. clientHeight: 100,
  211. scrollHeight: 260,
  212. scrollTop: 120,
  213. }),
  214. ).toBe(40);
  215. expect(
  216. isReasoningScrollAtBottom({
  217. clientHeight: 100,
  218. scrollHeight: 260,
  219. scrollTop: 120,
  220. }),
  221. ).toBe(false);
  222. expect(
  223. isReasoningScrollAtBottom({
  224. clientHeight: 100,
  225. scrollHeight: 260,
  226. scrollTop: 159,
  227. }),
  228. ).toBe(true);
  229. expect(
  230. isReasoningScrollAtBottom({
  231. clientHeight: 100,
  232. scrollHeight: 260,
  233. scrollTop: 160,
  234. }),
  235. ).toBe(true);
  236. });
  237. });