v4ConversationDraftSuggestedPrompts.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // @vitest-environment jsdom
  2. import { readFileSync } from "node:fs";
  3. import { resolve } from "node:path";
  4. import { createElement } from "react";
  5. import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
  6. import { afterEach, describe, expect, it, vi } from "vitest";
  7. vi.mock("@/i18n/IntlProvider.js", () => ({
  8. useZCodeIntl: () => ({
  9. locale: "zh-CN",
  10. }),
  11. }));
  12. import { ConversationDraftSuggestedPrompts } from "@/v4/ConversationDraftSuggestedPrompts.js";
  13. import type { DraftSuggestedPromptItem } from "@/v4/draftSuggestedPromptItems.js";
  14. const stylesSource = readFileSync(resolve(process.cwd(), "packages/ui/src/styles.css"), "utf8");
  15. const componentSource = readFileSync(
  16. resolve(process.cwd(), "packages/ui/src/v4/ConversationDraftSuggestedPrompts.tsx"),
  17. "utf8",
  18. );
  19. const items: DraftSuggestedPromptItem[] = [
  20. {
  21. id: "recent-commits",
  22. label: { cn: "检查 commit", en: "Check commits" },
  23. prompt: { cn: "检查当前工作区的 commit。", en: "Check workspace commits." },
  24. },
  25. {
  26. id: "create-pdf",
  27. iconName: "file-text",
  28. label: { cn: "制作 PDF", en: "Create PDF" },
  29. prompt: {
  30. cn: "根据当前工作区制作 PDF。",
  31. en: "Create a PDF from the workspace.",
  32. },
  33. plugin: {
  34. stableId: "document-skills@zcode-plugins-official",
  35. label: { cn: "文档技能", en: "Document skills" },
  36. },
  37. },
  38. ];
  39. afterEach(() => {
  40. cleanup();
  41. vi.restoreAllMocks();
  42. });
  43. describe("ConversationDraftSuggestedPrompts", () => {
  44. it("renders localized labels while preserving prompts for selection", () => {
  45. const view = render(createElement(ConversationDraftSuggestedPrompts, { items }));
  46. const rows = screen.getAllByRole("button");
  47. expect(rows).toHaveLength(items.length);
  48. for (const [index, item] of items.entries()) {
  49. expect(rows[index]?.textContent).toBe(item.label.cn);
  50. expect(rows[index]?.getAttribute("aria-label")).toBe(item.label.cn);
  51. expect(rows[index]?.getAttribute("title")).toBe(item.label.cn);
  52. expect(rows[index]?.textContent).not.toBe(item.prompt.cn);
  53. expect(rows[index]?.getAttribute("data-draft-suggested-prompt")).toBe(item.id);
  54. }
  55. expect(view.container.textContent).not.toContain(items[1]?.plugin?.label.cn);
  56. });
  57. it("reuses shared outline buttons with the 32px Figma button-group geometry", () => {
  58. render(createElement(ConversationDraftSuggestedPrompts, { items }));
  59. for (const row of screen.getAllByRole("button")) {
  60. expect(row.getAttribute("data-slot")).toBe("button");
  61. expect(row.getAttribute("data-variant")).toBe("outline");
  62. expect(row.getAttribute("data-size")).toBe("lg");
  63. expect(row.classList.contains("h-8")).toBe(true);
  64. expect(row.classList.contains("w-full")).toBe(false);
  65. expect(row.classList.contains("justify-start")).toBe(true);
  66. expect(row.classList.contains("gap-1.5")).toBe(true);
  67. expect(row.classList.contains("rounded-lg")).toBe(true);
  68. expect(row.classList.contains("rounded-md")).toBe(false);
  69. expect(row.classList.contains("px-3")).toBe(true);
  70. const iconSlot = row.querySelector("[data-draft-suggested-prompt-icon]");
  71. // 0980f9c7bd 样式微调整把图标从 size-5 缩到 size-4,断言需与组件保持同步
  72. expect(iconSlot?.classList.contains("size-4")).toBe(true);
  73. expect(iconSlot?.classList.contains("text-foreground")).toBe(true);
  74. expect(iconSlot?.classList.contains("opacity-70")).toBe(true);
  75. expect(iconSlot?.classList.contains("transition-opacity")).toBe(true);
  76. expect(iconSlot?.classList.contains("group-hover/button:opacity-100")).toBe(true);
  77. const icon = iconSlot?.querySelector("svg, img");
  78. expect(icon).not.toBeNull();
  79. expect(icon?.classList.contains("size-4")).toBe(true);
  80. }
  81. });
  82. it("keeps prompts in one centered horizontal group with narrow-screen scrolling", () => {
  83. const view = render(
  84. createElement(ConversationDraftSuggestedPrompts, {
  85. className: "mt-3",
  86. items,
  87. }),
  88. );
  89. const scroller = view.container.querySelector<HTMLElement>("[data-v4-draft-suggested-prompts]");
  90. const group = view.container.querySelector<HTMLElement>(
  91. "[data-v4-draft-suggested-prompts-group]",
  92. );
  93. expect(scroller).not.toBeNull();
  94. expect(scroller?.classList.contains("w-full")).toBe(true);
  95. expect(scroller?.classList.contains("overflow-x-auto")).toBe(true);
  96. expect(scroller?.classList.contains("mt-3")).toBe(true);
  97. expect(group?.classList.contains("flex")).toBe(true);
  98. expect(group?.classList.contains("w-max")).toBe(true);
  99. expect(group?.classList.contains("items-center")).toBe(true);
  100. expect(group?.classList.contains("gap-4")).toBe(true);
  101. expect(group?.classList.contains("mx-auto")).toBe(true);
  102. for (const prompt of view.container.querySelectorAll("[data-draft-suggested-prompt-text]")) {
  103. expect(prompt.classList.contains("min-w-0")).toBe(true);
  104. expect(prompt.classList.contains("truncate")).toBe(true);
  105. // 0980f9c7bd 样式微调整把提示词文本从 text-ui-caption 提到 text-ui-base
  106. expect(prompt.classList.contains("text-ui-base")).toBe(true);
  107. expect(prompt.classList.contains("text-foreground")).toBe(true);
  108. expect(prompt.classList.contains("opacity-70")).toBe(true);
  109. expect(prompt.classList.contains("transition-opacity")).toBe(true);
  110. expect(prompt.classList.contains("group-hover/button:opacity-100")).toBe(true);
  111. expect(prompt.classList.contains("text-foreground-subtlest")).toBe(false);
  112. }
  113. });
  114. it("reveals buttons in left-to-right DOM order and disables reduced motion", () => {
  115. render(createElement(ConversationDraftSuggestedPrompts, { items }));
  116. const rows = screen.getAllByRole("button");
  117. expect(rows[0]?.classList.contains("zcode-draft-prompt-waterfall")).toBe(true);
  118. expect(rows[1]?.classList.contains("zcode-draft-prompt-waterfall")).toBe(true);
  119. expect(rows[0]?.style.getPropertyValue("--zcode-draft-prompt-waterfall-delay")).toBe("0ms");
  120. expect(rows[1]?.style.getPropertyValue("--zcode-draft-prompt-waterfall-delay")).toBe("65ms");
  121. expect(stylesSource).toContain("@keyframes zcode-draft-prompt-waterfall");
  122. expect(stylesSource).toContain("clip-path: inset(0 0 100% 0)");
  123. expect(stylesSource).toContain("transform: translateY(-12px)");
  124. expect(stylesSource).toMatch(
  125. /@media \(prefers-reduced-motion: reduce\)[\s\S]*?\.zcode-draft-prompt-waterfall[\s\S]*?animation: none/u,
  126. );
  127. });
  128. it("keeps the missing-img placeholder icon on the button foreground", () => {
  129. render(createElement(ConversationDraftSuggestedPrompts, { items }));
  130. const placeholder = screen.getAllByRole("button")[0]?.querySelector("svg");
  131. expect(placeholder?.getAttribute("viewBox")).toBe("0 0 24 24");
  132. expect(placeholder?.classList.contains("text-foreground-subtle")).toBe(false);
  133. expect(
  134. placeholder
  135. ?.closest("[data-draft-suggested-prompt-icon]")
  136. ?.classList.contains("text-foreground"),
  137. ).toBe(true);
  138. });
  139. it("renders the img field as a Lucide icon name and inherits the slot color", async () => {
  140. const view = render(createElement(ConversationDraftSuggestedPrompts, { items }));
  141. const iconSlot = view.container.querySelectorAll("[data-draft-suggested-prompt-icon]")[1];
  142. await waitFor(() => {
  143. expect(iconSlot?.querySelector('[data-client-scene-lucide-icon="file-text"]')).not.toBeNull();
  144. });
  145. const icon = iconSlot?.querySelector("svg");
  146. expect(icon?.getAttribute("stroke")).toBe("currentColor");
  147. expect(icon?.classList.contains("text-foreground")).toBe(false);
  148. expect(iconSlot?.classList.contains("text-foreground")).toBe(true);
  149. expect(iconSlot?.querySelector("img")).toBeNull();
  150. });
  151. it("falls back without loading a URL when img is not a known Lucide name", () => {
  152. const view = render(
  153. createElement(ConversationDraftSuggestedPrompts, {
  154. items: [{ ...items[0]!, iconName: "https://cdn.example.com/icon.svg" }],
  155. }),
  156. );
  157. const iconSlot = view.container.querySelector("[data-draft-suggested-prompt-icon]");
  158. expect(iconSlot?.querySelector("img")).toBeNull();
  159. expect(iconSlot?.querySelector("svg.lucide-square-code")).not.toBeNull();
  160. expect(iconSlot?.querySelector("[data-client-scene-lucide-icon]")).toBeNull();
  161. });
  162. it("forwards the complete clicked recommendation config, and stays inert without a handler", () => {
  163. const onSelect = vi.fn();
  164. const view = render(createElement(ConversationDraftSuggestedPrompts, { items, onSelect }));
  165. fireEvent.click(screen.getAllByRole("button")[1]!);
  166. expect(onSelect).toHaveBeenCalledOnce();
  167. expect(onSelect).toHaveBeenCalledWith(items[1]);
  168. expect(items[0]?.plugin).toBeUndefined();
  169. expect(items[1]?.plugin?.stableId).toBe("document-skills@zcode-plugins-official");
  170. view.rerender(createElement(ConversationDraftSuggestedPrompts, { items }));
  171. expect(() => fireEvent.click(screen.getAllByRole("button")[0]!)).not.toThrow();
  172. });
  173. it("anchors the Plugin action Popover to the selected recommendation", async () => {
  174. const onAction = vi.fn();
  175. const onDismiss = vi.fn();
  176. const view = render(
  177. createElement(ConversationDraftSuggestedPrompts, {
  178. items,
  179. onSelect: vi.fn(),
  180. }),
  181. );
  182. const originalPluginButton = screen.getByRole("button", { name: "制作 PDF" });
  183. view.rerender(
  184. createElement(ConversationDraftSuggestedPrompts, {
  185. items,
  186. onSelect: vi.fn(),
  187. pluginActionPopover: {
  188. anchorItemId: "create-pdf",
  189. operationId: "install-document-skills",
  190. phase: "confirmation",
  191. message: "Enable Document Skills Plugin",
  192. actionLabel: "确认",
  193. onAction,
  194. onDismiss,
  195. },
  196. }),
  197. );
  198. const plainButton = screen.getByRole("button", { name: "检查 commit" });
  199. const pluginButton = screen.getByRole("button", { name: "制作 PDF" });
  200. expect(pluginButton).toBe(originalPluginButton);
  201. expect(plainButton.hasAttribute("data-draft-suggested-plugin-popover-anchor")).toBe(false);
  202. expect(pluginButton.getAttribute("data-draft-suggested-plugin-popover-anchor")).toBe("true");
  203. const popover = await waitFor(() => {
  204. const element = document.querySelector<HTMLElement>(
  205. '[data-draft-suggested-plugin-popover="confirmation"]',
  206. );
  207. expect(element).not.toBeNull();
  208. return element!;
  209. });
  210. expect(popover.getAttribute("data-anchor-item-id")).toBe("create-pdf");
  211. expect(popover.getAttribute("data-side")).toBe("bottom");
  212. expect(popover.getAttribute("data-draft-suggested-plugin-popover-offset")).toBe("9");
  213. expect(popover.classList.contains("w-60")).toBe(true);
  214. expect(popover.classList.contains("h-auto")).toBe(true);
  215. expect(popover.classList.contains("h-20")).toBe(false);
  216. expect(popover.classList.contains("h-11")).toBe(false);
  217. expect(popover.classList.contains("gap-3")).toBe(true);
  218. expect(popover.classList.contains("p-3")).toBe(true);
  219. expect(popover.classList.contains("shadow-md")).toBe(true);
  220. expect(popover.classList.contains("max-w-[calc(100vw-2rem)]")).toBe(true);
  221. const confirmationMessage = popover.querySelector(
  222. '[data-draft-suggested-plugin-popover-message="true"]',
  223. );
  224. expect(confirmationMessage?.textContent).toBe("Enable Document Skills Plugin");
  225. expect(confirmationMessage?.classList.contains("leading-4.5")).toBe(true);
  226. const confirmButton = screen.getByRole("button", { name: "确认" });
  227. expect(confirmButton.classList.contains("self-end")).toBe(true);
  228. fireEvent.click(confirmButton);
  229. expect(onAction).toHaveBeenCalledOnce();
  230. expect(onDismiss).not.toHaveBeenCalled();
  231. fireEvent.pointerDown(document.body);
  232. expect(onDismiss).toHaveBeenCalledOnce();
  233. view.rerender(
  234. createElement(ConversationDraftSuggestedPrompts, {
  235. items,
  236. onSelect: vi.fn(),
  237. pluginActionPopover: {
  238. anchorItemId: "create-pdf",
  239. operationId: "install-document-skills",
  240. phase: "success",
  241. message: "安装成功",
  242. },
  243. }),
  244. );
  245. const resultPopover = await waitFor(() => {
  246. const element = document.querySelector<HTMLElement>(
  247. '[data-draft-suggested-plugin-popover="success"]',
  248. );
  249. expect(element).not.toBeNull();
  250. return element!;
  251. });
  252. // 确认、进度和结果阶段必须复用同一个 PopoverContent;只复用 anchor 仍会让浮窗闪断重挂。
  253. expect(resultPopover).toBe(popover);
  254. expect(resultPopover.classList.contains("w-60")).toBe(true);
  255. expect(resultPopover.classList.contains("h-auto")).toBe(true);
  256. expect(resultPopover.classList.contains("h-20")).toBe(false);
  257. expect(resultPopover.classList.contains("h-11")).toBe(false);
  258. expect(resultPopover.classList.contains("gap-0")).toBe(true);
  259. const successContent = resultPopover.querySelector<HTMLElement>(
  260. '[data-draft-suggested-plugin-success-content="true"]',
  261. );
  262. const successIcon = resultPopover.querySelector<SVGElement>(
  263. '[data-draft-suggested-plugin-success-icon="true"]',
  264. );
  265. expect(successContent?.textContent).toBe("安装成功");
  266. expect(successIcon?.classList.contains("size-4")).toBe(true);
  267. expect(componentSource).toContain('<AnimatePresence initial={false} mode="popLayout">');
  268. expect(componentSource).toContain('key={state.phase === "success" ? "success" : "steady"}');
  269. expect(componentSource).toContain('initial={{ y: "0.75em", opacity: 0 }}');
  270. expect(componentSource).toContain('exit={{ y: "-0.75em", opacity: 0 }}');
  271. expect(componentSource).toContain("duration: 0.2");
  272. expect(componentSource).toContain("ease: [0.4, 0, 0.2, 1]");
  273. expect(componentSource).toContain("useReducedMotion");
  274. expect(stylesSource).not.toContain("zcode-draft-plugin-success-flip");
  275. expect(screen.queryByRole("button", { name: "确认" })).toBeNull();
  276. });
  277. it("renders nothing when the recommendation list is empty", () => {
  278. const view = render(createElement(ConversationDraftSuggestedPrompts, { items: [] }));
  279. expect(view.container.firstChild).toBeNull();
  280. });
  281. });