reasoningLayout.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. scrollReasoningSummaryToEnd,
  21. shouldAutoCollapseReasoning,
  22. } from "../src/components/ai-elements/reasoning.js";
  23. const mockServices = {} as IServiceAccessor;
  24. const mockPlatform = {
  25. reportTelemetryEvent: async () => {},
  26. } as unknown as IPlatformService;
  27. const mockBroadcastService: IBroadcastService = {
  28. send: async () => {},
  29. onMessage: () => ({ dispose: () => {} }),
  30. };
  31. const localStorageState = new Map<string, string>();
  32. Object.defineProperty(globalThis, "localStorage", {
  33. configurable: true,
  34. value: {
  35. clear: () => localStorageState.clear(),
  36. getItem: (key: string) => localStorageState.get(key) ?? null,
  37. removeItem: (key: string) => {
  38. localStorageState.delete(key);
  39. },
  40. setItem: (key: string, value: string) => {
  41. localStorageState.set(key, value);
  42. },
  43. },
  44. });
  45. Object.defineProperty(globalThis, "document", {
  46. configurable: true,
  47. value: {
  48. documentElement: {
  49. classList: {
  50. contains: () => false,
  51. toggle: () => {},
  52. },
  53. },
  54. },
  55. });
  56. function renderReasoningContent(
  57. content: string,
  58. reasoningProps: Partial<ComponentProps<typeof Reasoning>> = {},
  59. contentProps: Partial<ComponentProps<typeof ReasoningContent>> = {},
  60. locale: "zh-CN" | "en-US" = "zh-CN",
  61. ) {
  62. return renderToStaticMarkup(
  63. createElement(
  64. ZCodeIntlProvider,
  65. { initialLocale: locale },
  66. createElement(
  67. ServiceProvider,
  68. { services: mockServices },
  69. createElement(
  70. PlatformProvider,
  71. { platform: mockPlatform },
  72. createElement(
  73. StoreProvider,
  74. { broadcastService: mockBroadcastService },
  75. createElement(
  76. Reasoning,
  77. { className: "w-full", defaultOpen: true, ...reasoningProps },
  78. createElement(ReasoningTrigger, { streamingText: content }),
  79. createElement(ReasoningContent, contentProps, content),
  80. ),
  81. ),
  82. ),
  83. ),
  84. ),
  85. );
  86. }
  87. describe("Reasoning layout", () => {
  88. it("keeps collapsible spacing inside the animated content", () => {
  89. const html = renderReasoningContent("先分析,再回答。");
  90. expect(html).toContain('data-testid="chat-reasoning-trigger"');
  91. expect(html).toMatch(
  92. /data-testid="chat-reasoning-trigger"[^>]*class="[^"]*\binline-flex\b[^"]*\bmax-w-full\b/,
  93. );
  94. expect(html).not.toMatch(
  95. /data-testid="chat-reasoning-trigger"[^>]*class="[^"]*(?:^|\s)w-full(?:\s|")/,
  96. );
  97. expect(html).toContain('class="not-prose flex flex-col w-full"');
  98. expect(html).not.toContain('class="not-prose flex flex-col gap-2');
  99. expect(html).toContain('class="pt-3"');
  100. expect(html).not.toMatch(/data-slot="collapsible-content"[^>]*class="[^"]*\bpt-1\b[^"]*"/);
  101. });
  102. it("renders expanded thought content as plain text instead of Markdown", () => {
  103. const html = renderReasoningContent("**阶段一**\n\n# 计划\n`命令`");
  104. expect(html).toContain("**阶段一**");
  105. expect(html).toContain("# 计划");
  106. expect(html).toContain("`命令`");
  107. expect(html).not.toContain("<strong>");
  108. expect(html).not.toContain("<h1");
  109. expect(html).not.toContain("<code>");
  110. });
  111. it("auto collapses only for a new output boundary without user interaction", () => {
  112. expect(
  113. shouldAutoCollapseReasoning({
  114. autoCollapseKey: "content:1",
  115. previousAutoCollapseKey: null,
  116. userInteracted: false,
  117. }),
  118. ).toBe(true);
  119. expect(
  120. shouldAutoCollapseReasoning({
  121. autoCollapseKey: "content:1",
  122. previousAutoCollapseKey: "content:1",
  123. userInteracted: false,
  124. }),
  125. ).toBe(false);
  126. expect(
  127. shouldAutoCollapseReasoning({
  128. autoCollapseKey: "content:1",
  129. previousAutoCollapseKey: null,
  130. userInteracted: true,
  131. }),
  132. ).toBe(false);
  133. expect(
  134. shouldAutoCollapseReasoning({
  135. autoCollapseKey: null,
  136. previousAutoCollapseKey: null,
  137. userInteracted: false,
  138. }),
  139. ).toBe(false);
  140. });
  141. it("marks the expanded reasoning scroll container for conditional masks", () => {
  142. const html = renderReasoningContent("先分析,再回答。");
  143. expect(html).toContain('data-reasoning-scroll-mask="none"');
  144. expect(html).toContain("max-h-60");
  145. expect(html).toContain("overflow-auto");
  146. });
  147. it("removes the left guide and indentation for nested reasoning content", () => {
  148. const html = renderReasoningContent("CUA 子思考。", {}, { variant: "nested" });
  149. expect(html).toContain('data-reasoning-content-variant="nested"');
  150. expect(html).not.toContain("border-l");
  151. expect(html).not.toContain("ml-2");
  152. expect(html).not.toContain("pl-3.5");
  153. });
  154. it("renders only the latest reasoning line without converting it to a break", () => {
  155. const html = renderReasoningContent("正在分析输入。\n继续检查实现。", {
  156. defaultOpen: false,
  157. isStreaming: true,
  158. });
  159. expect(html).toContain("正在思考");
  160. expect(html).not.toContain("思考过程");
  161. expect(html).toContain("animated-gradient-text");
  162. expect(html).not.toContain("正在分析输入。");
  163. expect(html).toContain("继续检查实现。");
  164. expect(html).not.toContain("<br");
  165. expect(html).toContain('data-state="closed"');
  166. expect(html).toContain('data-reasoning-label="true"');
  167. expect(html).toMatch(
  168. /class="[^"]*\bshrink-0\b[^"]*\bwhitespace-nowrap\b"[^>]*data-reasoning-label="true"/,
  169. );
  170. expect(html).toMatch(
  171. /class="[^"]*\bmin-w-0\b[^"]*\bflex-1\b[^"]*\boverflow-hidden\b[^"]*\bwhitespace-nowrap\b[^>]*data-reasoning-streaming-line="true"/,
  172. );
  173. expect(html).not.toContain("ml-auto");
  174. expect(html).not.toContain("text-right");
  175. expect(html).not.toContain("text-ellipsis");
  176. expect(html).toContain('data-reasoning-streaming-text="true"');
  177. expect(html).toContain('data-reasoning-streaming-roll="true"');
  178. expect(html).toMatch(
  179. /class="[^"]*\btext-foreground-subtle\b[^>]*data-reasoning-streaming-mask=/,
  180. );
  181. });
  182. it("derives a stable rolling key per non-empty reasoning line", () => {
  183. expect(resolveReasoningStreamingSummary("第一行正在生成")).toEqual({
  184. key: "0",
  185. text: "第一行正在生成",
  186. });
  187. expect(resolveReasoningStreamingSummary("第一行完成\n\n第二行正在生成")).toEqual({
  188. key: "2",
  189. text: "第二行正在生成",
  190. });
  191. expect(resolveReasoningStreamingSummary(" \n\t ")).toBeNull();
  192. });
  193. it("shows elapsed duration instead of the inline summary while streaming is expanded", () => {
  194. const html = renderReasoningContent("正在分析输入。", {
  195. defaultOpen: true,
  196. duration: 7,
  197. isStreaming: true,
  198. });
  199. expect(html).toContain("思考");
  200. expect(html).not.toContain("思考过程");
  201. expect(html).toContain("持续了 7 秒");
  202. expect(html).toContain('data-state="open"');
  203. expect(html).not.toContain("animated-gradient-text");
  204. expect(html).not.toContain('data-reasoning-streaming-line="true"');
  205. });
  206. it("renders the English duration directly without a leading preposition", () => {
  207. const html = renderReasoningContent(
  208. "Checking the implementation.",
  209. { defaultOpen: false, duration: 2, isStreaming: false },
  210. {},
  211. "en-US",
  212. );
  213. expect(html).toContain("Thought");
  214. expect(html).toContain("2 seconds");
  215. expect(html).not.toContain("for 2 seconds");
  216. });
  217. it("keeps the latest streaming tail visible after the summary fills its viewport", () => {
  218. const viewport = { scrollLeft: 0, scrollWidth: 320 };
  219. scrollReasoningSummaryToEnd(viewport);
  220. expect(viewport.scrollLeft).toBe(320);
  221. });
  222. it("adds a two-sided fade mask only while the streaming summary overflows", () => {
  223. expect(
  224. isReasoningSummaryOverflowing({ clientWidth: 240, scrollWidth: 240 }),
  225. ).toBe(false);
  226. expect(
  227. isReasoningSummaryOverflowing({ clientWidth: 240, scrollWidth: 420 }),
  228. ).toBe(true);
  229. expect(getReasoningSummaryMaskStyle(false)).toBeUndefined();
  230. expect(getReasoningSummaryMaskStyle(true)).toMatchObject({
  231. WebkitMaskImage:
  232. "linear-gradient(to right, transparent 0, black 16px, black calc(100% - 16px), transparent 100%)",
  233. maskImage:
  234. "linear-gradient(to right, transparent 0, black 16px, black calc(100% - 16px), transparent 100%)",
  235. });
  236. });
  237. it("keeps completed reasoning collapsed by default", () => {
  238. const html = renderReasoningContent("已经完成的思考。", {
  239. defaultOpen: false,
  240. isStreaming: false,
  241. });
  242. expect(html).toContain("思考");
  243. expect(html).not.toContain("思考过程");
  244. expect(html).toMatch(
  245. /class="inline-flex items-center gap-2"><span class="font-medium[^>]*">思考<\/span><span class="font-normal[^>]*">·<\/span><span class="font-normal/,
  246. );
  247. expect(html).toMatch(
  248. /class="[^"]*\bfont-medium\b[^"]*">思考<\/span>/,
  249. );
  250. expect(html).not.toMatch(
  251. /class="[^"]*\bfont-semibold\b[^"]*">思考<\/span>/,
  252. );
  253. expect(html).toContain("持续了");
  254. expect(html).not.toContain("animated-gradient-text");
  255. expect(html).not.toContain("已经完成的思考。");
  256. expect(html).toContain('data-state="closed"');
  257. });
  258. it("treats only the bottom lock zone as auto-followable", () => {
  259. expect(
  260. getReasoningBottomDistance({
  261. clientHeight: 100,
  262. scrollHeight: 260,
  263. scrollTop: 120,
  264. }),
  265. ).toBe(40);
  266. expect(
  267. isReasoningScrollAtBottom({
  268. clientHeight: 100,
  269. scrollHeight: 260,
  270. scrollTop: 120,
  271. }),
  272. ).toBe(false);
  273. expect(
  274. isReasoningScrollAtBottom({
  275. clientHeight: 100,
  276. scrollHeight: 260,
  277. scrollTop: 159,
  278. }),
  279. ).toBe(true);
  280. expect(
  281. isReasoningScrollAtBottom({
  282. clientHeight: 100,
  283. scrollHeight: 260,
  284. scrollTop: 160,
  285. }),
  286. ).toBe(true);
  287. });
  288. });