pluginStoreTryPrefillEditable.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // @vitest-environment jsdom
  2. import { $getPromptMarkdown } from "@/mentions/promptSerialization.js";
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. $getRoot,
  6. $createTextNode,
  7. $createParagraphNode,
  8. $getSelection,
  9. $isRangeSelection,
  10. $isTextNode,
  11. createEditor,
  12. } from "lexical";
  13. import {
  14. insertEditorMention,
  15. prependEditorMentionIfMissing,
  16. replaceEditorWithMention,
  17. } from "@/LexicalChatInput.js";
  18. import {
  19. $createPromptMentionNode,
  20. $isPromptMentionNode,
  21. PromptMentionNode,
  22. } from "@/mentions/nodes/PromptMentionNode.js";
  23. const TRY_NOW_MENTION = {
  24. id: "plugin:commit-commands@zcode-official",
  25. category: "plugins" as const,
  26. label: "commit-commands",
  27. value: "commit-commands@zcode-official",
  28. markdown: "[@commit-commands](plugin://commit-commands@zcode-official)",
  29. data: { pluginId: "commit-commands@zcode-official" },
  30. };
  31. const SECOND_PLUGIN_MENTION = {
  32. id: "plugin:pdf@zcode-plugins-official",
  33. category: "plugins" as const,
  34. label: "pdf",
  35. value: "pdf@zcode-plugins-official",
  36. markdown: "[@pdf](plugin://pdf@zcode-plugins-official)",
  37. data: { pluginId: "pdf@zcode-plugins-official" },
  38. };
  39. async function prefill(trailingText?: string) {
  40. const editor = createEditor({ nodes: [PromptMentionNode] });
  41. editor.setRootElement(document.createElement("div"));
  42. await new Promise<void>((resolve) => {
  43. editor.registerUpdateListener(() => resolve());
  44. replaceEditorWithMention(editor, TRY_NOW_MENTION, trailingText);
  45. });
  46. return editor;
  47. }
  48. describe("Plugin 商店试用预填后的输入框可继续编辑", () => {
  49. // 生产路径传的是显式 ""(ConversationComposer 把 request.text 切掉 markdown 前缀后为空串),
  50. // 不是缺省参数,所以这里必须用 "" 而不是 undefined,否则默认参数单独就能让测试变绿。
  51. it("空提示词试用也会在 mention token 后留下文本节点并把光标放在 token 之外", async () => {
  52. const editor = await prefill("");
  53. editor.getEditorState().read(() => {
  54. const children = $getRoot().getFirstChild()?.getChildren?.() ?? [];
  55. expect(children.map((node) => node.getType())).toEqual(["prompt-mention", "text"]);
  56. const selection = $getSelection();
  57. if (!$isRangeSelection(selection)) {
  58. throw new Error("预填后没有 range selection");
  59. }
  60. const anchorNode = selection.anchor.getNode();
  61. // 关键不变量:光标必须落在 mention 之后的普通文本节点里。
  62. // 停在 token 内部时,Lexical insertText 会整节点替换,用户按一下空格引用就消失。
  63. expect($isPromptMentionNode(anchorNode)).toBe(false);
  64. expect($isTextNode(anchorNode)).toBe(true);
  65. expect(anchorNode.getKey()).toBe(children[1]?.getKey());
  66. });
  67. });
  68. it("带示例提示词试用保持原有草稿正文", async () => {
  69. const editor = await prefill(" 帮我提交当前改动");
  70. expect(editor.getEditorState().read(() => $getPromptMarkdown())).toBe(
  71. `${TRY_NOW_MENTION.markdown} 帮我提交当前改动`,
  72. );
  73. });
  74. });
  75. describe("前置 Plugin mention 保留现有 Lexical 节点", () => {
  76. it("连续选择不同 Plugin 时保留旧 chip、文件 chip 与多段正文", () => {
  77. const editor = createEditor({ nodes: [PromptMentionNode] });
  78. editor.update(
  79. () => {
  80. $getRoot().append(
  81. $createParagraphNode().append(
  82. $createPromptMentionNode(TRY_NOW_MENTION),
  83. $createTextNode(" 正文"),
  84. ),
  85. $createParagraphNode().append(
  86. $createPromptMentionNode({
  87. id: "file:src/demo.ts",
  88. category: "files",
  89. label: "demo.ts",
  90. value: "src/demo.ts",
  91. markdown: "[demo.ts](src/demo.ts)",
  92. data: { path: "src/demo.ts" },
  93. }),
  94. $createTextNode(" "),
  95. $createPromptMentionNode({
  96. id: "prefill-skill:review",
  97. category: "skills",
  98. label: "review",
  99. value: "review",
  100. markdown: "$review",
  101. }),
  102. $createTextNode(" 第二段"),
  103. ),
  104. );
  105. },
  106. { discrete: true },
  107. );
  108. expect(prependEditorMentionIfMissing(editor, SECOND_PLUGIN_MENTION)).toBe(true);
  109. expect(prependEditorMentionIfMissing(editor, TRY_NOW_MENTION)).toBe(false);
  110. editor.getEditorState().read(() => {
  111. const paragraphs = $getRoot().getChildren();
  112. expect(paragraphs).toHaveLength(2);
  113. const mentionNodes = $getRoot()
  114. .getAllTextNodes()
  115. .filter($isPromptMentionNode);
  116. expect(mentionNodes.map((node) => node.getMention().id)).toEqual([
  117. SECOND_PLUGIN_MENTION.id,
  118. TRY_NOW_MENTION.id,
  119. "file:src/demo.ts",
  120. "prefill-skill:review",
  121. ]);
  122. expect($getPromptMarkdown()).toBe(
  123. `${SECOND_PLUGIN_MENTION.markdown} ${TRY_NOW_MENTION.markdown} 正文\n\n` +
  124. "[demo.ts](src/demo.ts) $review 第二段",
  125. );
  126. });
  127. });
  128. it("序列化恢复后仍保留两个 Plugin mention 节点", () => {
  129. const editor = createEditor({ nodes: [PromptMentionNode] });
  130. editor.update(
  131. () => {
  132. $getRoot().append($createParagraphNode().append($createPromptMentionNode(TRY_NOW_MENTION)));
  133. },
  134. { discrete: true },
  135. );
  136. prependEditorMentionIfMissing(editor, SECOND_PLUGIN_MENTION);
  137. const restored = createEditor({ nodes: [PromptMentionNode] });
  138. restored.setEditorState(restored.parseEditorState(JSON.stringify(editor.getEditorState())));
  139. restored.getEditorState().read(() => {
  140. const mentions = $getRoot().getAllTextNodes().filter($isPromptMentionNode);
  141. expect(mentions.map((node) => node.getMention().id)).toEqual([
  142. SECOND_PLUGIN_MENTION.id,
  143. TRY_NOW_MENTION.id,
  144. ]);
  145. });
  146. });
  147. });
  148. describe("菜单保存的 Lexical 选区", () => {
  149. it("草稿节点被替换后在新草稿末尾插入,不恢复失效 key", async () => {
  150. const editor = createEditor({
  151. nodes: [PromptMentionNode],
  152. onError: (error) => {
  153. throw error;
  154. },
  155. });
  156. editor.update(
  157. () => {
  158. const text = $createTextNode("old draft");
  159. $getRoot().append($createParagraphNode().append(text));
  160. text.select(3, 3);
  161. },
  162. { discrete: true },
  163. );
  164. const saved = editor.getEditorState();
  165. editor.update(
  166. () => {
  167. $getRoot()
  168. .clear()
  169. .append($createParagraphNode().append($createTextNode("replacement ")));
  170. },
  171. { discrete: true },
  172. );
  173. await new Promise<void>((resolve) => {
  174. const unregister = editor.registerUpdateListener(() => {
  175. unregister();
  176. resolve();
  177. });
  178. insertEditorMention(editor, TRY_NOW_MENTION, saved);
  179. });
  180. expect(editor.getEditorState().read(() => $getPromptMarkdown())).toBe(
  181. "replacement " + TRY_NOW_MENTION.markdown + " ",
  182. );
  183. });
  184. });