v4ConversationDraftSuggestedPrompts.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. vi.mock("@/i18n/IntlProvider.js", () => ({
  6. useZCodeIntl: () => ({
  7. locale: "zh-CN",
  8. }),
  9. }));
  10. import { ConversationDraftSuggestedPrompts } from "@/v4/ConversationDraftSuggestedPrompts.js";
  11. import type { DraftSuggestedPromptItem } from "@/v4/draftSuggestedPromptItems.js";
  12. const items: DraftSuggestedPromptItem[] = [
  13. {
  14. id: "recent-commits",
  15. label: { cn: "检查 commit", en: "Check commits" },
  16. prompt: { cn: "检查当前工作区的 commit。", en: "Check workspace commits." },
  17. },
  18. {
  19. id: "create-pdf",
  20. iconName: "file-text",
  21. label: { cn: "制作 PDF", en: "Create PDF" },
  22. prompt: {
  23. cn: "根据当前工作区制作 PDF。",
  24. en: "Create a PDF from the workspace.",
  25. },
  26. plugin: {
  27. stableId: "document-skills@zcode-plugins-official",
  28. label: { cn: "文档技能", en: "Document skills" },
  29. },
  30. },
  31. ];
  32. afterEach(() => {
  33. cleanup();
  34. vi.restoreAllMocks();
  35. });
  36. describe("ConversationDraftSuggestedPrompts", () => {
  37. it("renders localized labels while preserving prompts for selection", () => {
  38. const view = render(createElement(ConversationDraftSuggestedPrompts, { items }));
  39. const rows = screen.getAllByRole("button");
  40. expect(rows).toHaveLength(items.length);
  41. for (const [index, item] of items.entries()) {
  42. expect(rows[index]?.textContent).toBe(item.label.cn);
  43. expect(rows[index]?.getAttribute("aria-label")).toBe(item.label.cn);
  44. expect(rows[index]?.getAttribute("title")).toBe(item.label.cn);
  45. expect(rows[index]?.textContent).not.toBe(item.prompt.cn);
  46. expect(rows[index]?.getAttribute("data-draft-suggested-prompt")).toBe(item.id);
  47. }
  48. expect(view.container.textContent).not.toContain(items[1]?.plugin?.label.cn);
  49. });
  50. it("assigns stagger delays in recommendation order", () => {
  51. render(createElement(ConversationDraftSuggestedPrompts, { items }));
  52. const rows = screen.getAllByRole("button");
  53. expect(rows[0]?.style.getPropertyValue("--zcode-draft-prompt-waterfall-delay")).toBe("0ms");
  54. expect(rows[1]?.style.getPropertyValue("--zcode-draft-prompt-waterfall-delay")).toBe("65ms");
  55. });
  56. it("loads the configured Lucide name without rendering an image URL", async () => {
  57. const view = render(createElement(ConversationDraftSuggestedPrompts, { items }));
  58. const iconSlot = view.container.querySelectorAll("[data-draft-suggested-prompt-icon]")[1];
  59. await waitFor(() => {
  60. expect(iconSlot?.querySelector('[data-client-scene-lucide-icon="file-text"]')).not.toBeNull();
  61. });
  62. expect(iconSlot?.querySelector("img")).toBeNull();
  63. });
  64. it("falls back without loading a URL when img is not a known Lucide name", () => {
  65. const view = render(
  66. createElement(ConversationDraftSuggestedPrompts, {
  67. items: [{ ...items[0]!, iconName: "https://cdn.example.com/icon.svg" }],
  68. }),
  69. );
  70. const iconSlot = view.container.querySelector("[data-draft-suggested-prompt-icon]");
  71. expect(iconSlot?.querySelector("img")).toBeNull();
  72. expect(iconSlot?.querySelector("svg.lucide-square-code")).not.toBeNull();
  73. expect(iconSlot?.querySelector("[data-client-scene-lucide-icon]")).toBeNull();
  74. });
  75. it("forwards the complete clicked recommendation config, and stays inert without a handler", () => {
  76. const onSelect = vi.fn();
  77. const view = render(createElement(ConversationDraftSuggestedPrompts, { items, onSelect }));
  78. fireEvent.click(screen.getAllByRole("button")[1]!);
  79. expect(onSelect).toHaveBeenCalledOnce();
  80. expect(onSelect).toHaveBeenCalledWith(items[1]);
  81. expect(items[0]?.plugin).toBeUndefined();
  82. expect(items[1]?.plugin?.stableId).toBe("document-skills@zcode-plugins-official");
  83. view.rerender(createElement(ConversationDraftSuggestedPrompts, { items }));
  84. expect(() => fireEvent.click(screen.getAllByRole("button")[0]!)).not.toThrow();
  85. });
  86. it("anchors the Plugin action Popover to the selected recommendation", async () => {
  87. const onAction = vi.fn();
  88. const onDismiss = vi.fn();
  89. const view = render(
  90. createElement(ConversationDraftSuggestedPrompts, {
  91. items,
  92. onSelect: vi.fn(),
  93. }),
  94. );
  95. const originalPluginButton = screen.getByRole("button", { name: "制作 PDF" });
  96. view.rerender(
  97. createElement(ConversationDraftSuggestedPrompts, {
  98. items,
  99. onSelect: vi.fn(),
  100. pluginActionPopover: {
  101. anchorItemId: "create-pdf",
  102. operationId: "install-document-skills",
  103. phase: "confirmation",
  104. message: "Enable Document Skills Plugin",
  105. actionLabel: "确认",
  106. onAction,
  107. onDismiss,
  108. },
  109. }),
  110. );
  111. const plainButton = screen.getByRole("button", { name: "检查 commit" });
  112. const pluginButton = screen.getByRole("button", { name: "制作 PDF" });
  113. expect(pluginButton).toBe(originalPluginButton);
  114. expect(plainButton.hasAttribute("data-draft-suggested-plugin-popover-anchor")).toBe(false);
  115. expect(pluginButton.getAttribute("data-draft-suggested-plugin-popover-anchor")).toBe("true");
  116. const popover = await waitFor(() => {
  117. const element = document.querySelector<HTMLElement>(
  118. '[data-draft-suggested-plugin-popover="confirmation"]',
  119. );
  120. expect(element).not.toBeNull();
  121. return element!;
  122. });
  123. expect(popover.getAttribute("data-anchor-item-id")).toBe("create-pdf");
  124. expect(popover.getAttribute("data-side")).toBe("bottom");
  125. expect(popover.getAttribute("data-draft-suggested-plugin-popover-offset")).toBe("9");
  126. expect(popover.classList.contains("gap-3")).toBe(true);
  127. const confirmationMessage = popover.querySelector(
  128. '[data-draft-suggested-plugin-popover-message="true"]',
  129. );
  130. expect(confirmationMessage?.textContent).toBe("Enable Document Skills Plugin");
  131. const confirmButton = screen.getByRole("button", { name: "确认" });
  132. fireEvent.click(confirmButton);
  133. expect(onAction).toHaveBeenCalledOnce();
  134. expect(onDismiss).not.toHaveBeenCalled();
  135. fireEvent.pointerDown(document.body);
  136. expect(onDismiss).toHaveBeenCalledOnce();
  137. view.rerender(
  138. createElement(ConversationDraftSuggestedPrompts, {
  139. items,
  140. onSelect: vi.fn(),
  141. pluginActionPopover: {
  142. anchorItemId: "create-pdf",
  143. operationId: "install-document-skills",
  144. phase: "success",
  145. message: "安装成功",
  146. },
  147. }),
  148. );
  149. const resultPopover = await waitFor(() => {
  150. const element = document.querySelector<HTMLElement>(
  151. '[data-draft-suggested-plugin-popover="success"]',
  152. );
  153. expect(element).not.toBeNull();
  154. return element!;
  155. });
  156. // 确认、进度和结果阶段必须复用同一个 PopoverContent;只复用 anchor 仍会让浮窗闪断重挂。
  157. expect(resultPopover).toBe(popover);
  158. expect(resultPopover.classList.contains("gap-0")).toBe(true);
  159. const successContent = resultPopover.querySelector<HTMLElement>(
  160. '[data-draft-suggested-plugin-success-content="true"]',
  161. );
  162. const successIcon = resultPopover.querySelector<SVGElement>(
  163. '[data-draft-suggested-plugin-success-icon="true"]',
  164. );
  165. expect(successContent?.textContent).toBe("安装成功");
  166. expect(successIcon).not.toBeNull();
  167. expect(screen.queryByRole("button", { name: "确认" })).toBeNull();
  168. });
  169. it("renders nothing when the recommendation list is empty", () => {
  170. const view = render(createElement(ConversationDraftSuggestedPrompts, { items: [] }));
  171. expect(view.container.firstChild).toBeNull();
  172. });
  173. });