promptMentionNode.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // @vitest-environment jsdom
  2. import { describe, expect, it, vi } from "vitest";
  3. import { $getPromptMarkdown } from "@/mentions/promptSerialization.js";
  4. import { $createParagraphNode, $getRoot, createEditor } from "lexical";
  5. import { normalizePromptMentionDisplayLabel } from "../src/mentions/promptMentionLabel.js";
  6. import {
  7. $createPromptMentionNode,
  8. GOAL_COMMAND_MENTION_ICON_NODE,
  9. GOAL_COMMAND_MENTION_LUCIDE_ICON_NAME,
  10. WORKFLOW_COMMAND_MENTION_ICON_NODE,
  11. WORKFLOW_COMMAND_MENTION_LUCIDE_ICON_NAME,
  12. PLUGIN_MENTION_ICON_NODE,
  13. PLUGIN_MENTION_LUCIDE_ICON_NAME,
  14. PromptMentionNode,
  15. SUBAGENT_MENTION_ICON_NODE,
  16. SUBAGENT_MENTION_LUCIDE_ICON_NAME,
  17. } from "../src/mentions/nodes/PromptMentionNode.js";
  18. describe("PromptMentionNode", () => {
  19. async function renderPluginMention(icon: string) {
  20. const editor = createEditor({ nodes: [PromptMentionNode] });
  21. const rootElement = document.createElement("div");
  22. editor.setRootElement(rootElement);
  23. await new Promise<void>((resolve) => {
  24. editor.update(
  25. () => {
  26. $getRoot().append(
  27. $createParagraphNode().append(
  28. $createPromptMentionNode({
  29. id: "plugin:demo@mkt",
  30. category: "plugins",
  31. label: "Demo",
  32. value: "demo@mkt",
  33. markdown: "[@Demo](plugin://demo@mkt)",
  34. data: { pluginId: "demo@mkt", icon },
  35. }),
  36. ),
  37. );
  38. },
  39. { onUpdate: resolve },
  40. );
  41. });
  42. return { editor, rootElement };
  43. }
  44. it("keeps native selection offsets aligned with the visible label", async () => {
  45. const { editor, rootElement } = await renderPluginMention("https://cdn.example.com/demo.png");
  46. expect(editor.getEditorState().read(() => $getRoot().getTextContent())).toBe("Demo");
  47. expect(rootElement.querySelector("[data-mention-id]")?.firstChild?.nodeType).toBe(
  48. Node.TEXT_NODE,
  49. );
  50. });
  51. it("renders a trusted original Plugin icon and keeps canonical text content", async () => {
  52. const { editor, rootElement } = await renderPluginMention("https://cdn.example.com/demo.png");
  53. expect(
  54. rootElement
  55. .querySelector<HTMLElement>("[data-mention-id]")
  56. ?.style.getPropertyValue("--mention-image"),
  57. ).toContain("https://cdn.example.com/demo.png");
  58. expect(rootElement.querySelector("img,svg")).toBeNull();
  59. expect(rootElement.textContent).toBe("Demo");
  60. expect(editor.getEditorState().read(() => $getPromptMarkdown())).toBe(
  61. "[@Demo](plugin://demo@mkt)",
  62. );
  63. });
  64. it("rejects an untrusted Plugin icon URL and falls back to Cable", async () => {
  65. const { rootElement } = await renderPluginMention("http://cdn.example.com/demo.png");
  66. expect(rootElement.querySelector("img")).toBeNull();
  67. expect(
  68. rootElement
  69. .querySelector<HTMLElement>("[data-mention-id]")
  70. ?.style.getPropertyValue("--mention-mask"),
  71. ).toContain("data:image/svg+xml");
  72. expect(rootElement.textContent).toBe("Demo");
  73. });
  74. it("keeps the text node and native selection when an icon fails asynchronously", async () => {
  75. const images: HTMLImageElement[] = [];
  76. const NativeImage = Image;
  77. vi.stubGlobal(
  78. "Image",
  79. class extends NativeImage {
  80. constructor() {
  81. super();
  82. images.push(this);
  83. }
  84. },
  85. );
  86. try {
  87. const { rootElement } = await renderPluginMention("https://cdn.example.com/fail.png");
  88. const chip = rootElement.querySelector<HTMLElement>("[data-mention-id]")!;
  89. const text = chip.firstChild;
  90. document.body.append(rootElement);
  91. const selection = window.getSelection()!;
  92. selection.selectAllChildren(chip);
  93. images[0]?.dispatchEvent(new Event("error"));
  94. expect(selection.toString()).toBe("Demo");
  95. expect(selection.isCollapsed).toBe(false);
  96. rootElement.remove();
  97. expect(chip.firstChild).toBe(text);
  98. expect(chip.textContent).toBe("Demo");
  99. expect(chip.style.getPropertyValue("--mention-mask")).toContain("data:image/svg+xml");
  100. expect(chip.style.getPropertyValue("--mention-image")).toBe("");
  101. } finally {
  102. vi.unstubAllGlobals();
  103. }
  104. });
  105. it("uses the lucide Cable icon node for Plugin fallbacks", () => {
  106. expect(PLUGIN_MENTION_LUCIDE_ICON_NAME).toBe("Cable");
  107. expect(PLUGIN_MENTION_ICON_NODE).toEqual([
  108. ["path", { d: "M17 19a1 1 0 0 1-1-1v-2a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2v2a1 1 0 0 1-1 1z" }],
  109. ["path", { d: "M17 21v-2" }],
  110. ["path", { d: "M19 14V6.5a1 1 0 0 0-7 0v11a1 1 0 0 1-7 0V10" }],
  111. ["path", { d: "M21 21v-2" }],
  112. ["path", { d: "M3 5V3" }],
  113. ["path", { d: "M4 10a2 2 0 0 1-2-2V6a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2a2 2 0 0 1-2 2z" }],
  114. ["path", { d: "M7 5V3" }],
  115. ]);
  116. });
  117. it("uses the lucide Goal icon node for the goal command mention icon", () => {
  118. expect(GOAL_COMMAND_MENTION_LUCIDE_ICON_NAME).toBe("Goal");
  119. expect(GOAL_COMMAND_MENTION_ICON_NODE).toEqual([
  120. ["path", { d: "M12 13V2l8 4-8 4" }],
  121. ["path", { d: "M20.561 10.222a9 9 0 1 1-12.55-5.29" }],
  122. ["path", { d: "M8.002 9.997a5 5 0 1 0 8.9 2.02" }],
  123. ]);
  124. });
  125. it("uses the lucide Workflow icon node for the workflow command mention icon", () => {
  126. expect(WORKFLOW_COMMAND_MENTION_LUCIDE_ICON_NAME).toBe("Workflow");
  127. expect(WORKFLOW_COMMAND_MENTION_ICON_NODE).toEqual([
  128. ["rect", { width: "8", height: "8", x: "3", y: "3", rx: "2" }],
  129. ["path", { d: "M7 11v4a2 2 0 0 0 2 2h4" }],
  130. ["rect", { width: "8", height: "8", x: "13", y: "13", rx: "2" }],
  131. ]);
  132. });
  133. it("uses the lucide Bot icon node for subagent mentions", () => {
  134. expect(SUBAGENT_MENTION_LUCIDE_ICON_NAME).toBe("Bot");
  135. expect(SUBAGENT_MENTION_ICON_NODE).toEqual([
  136. ["path", { d: "M12 8V4H8" }],
  137. ["rect", { width: "16", height: "12", x: "4", y: "8", rx: "2" }],
  138. ["path", { d: "M2 14h2" }],
  139. ["path", { d: "M20 14h2" }],
  140. ["path", { d: "M15 13v2" }],
  141. ["path", { d: "M9 13v2" }],
  142. ]);
  143. });
  144. it("normalizes markdown labels before rendering mention tags", () => {
  145. expect(
  146. normalizePromptMentionDisplayLabel(
  147. "skills",
  148. "[$find-skills](/Users/dev/.claude/skills/find-skills/SKILL.md)",
  149. "find-skills",
  150. ),
  151. ).toBe("find-skills");
  152. expect(
  153. normalizePromptMentionDisplayLabel(
  154. "sessions",
  155. "[#Review mailbox flow](#sess_visible_target)",
  156. "sess_visible_target",
  157. ),
  158. ).toBe("Review mailbox flow");
  159. expect(normalizePromptMentionDisplayLabel("skills", "code-review", "code-review")).toBe(
  160. "code-review",
  161. );
  162. });
  163. });