conversationSelectionReference.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import {
  3. appendConversationSelectionReference,
  4. buildPromptWithConversationSelections,
  5. clearConversationSelectionReferenceScope,
  6. dispatchConversationSelectionAdd,
  7. getConversationSelectionReferenceScope,
  8. parsePromptConversationSelections,
  9. type ConversationSelectionReference,
  10. } from "../src/lib/conversationSelectionReference.js";
  11. import {
  12. buildPromptWithWebElementContexts,
  13. parsePromptWebElementContexts,
  14. type WebElementContextComposerAttachment,
  15. } from "../src/lib/webElementContext.js";
  16. function reference(
  17. id: string,
  18. sourceRowId = 1,
  19. text = "selected text",
  20. ): ConversationSelectionReference {
  21. return {
  22. contentType: "assistant",
  23. id,
  24. sourceRowId,
  25. sourceSessionId: "parent-1",
  26. text,
  27. };
  28. }
  29. describe("conversation selection references", () => {
  30. afterEach(() => vi.unstubAllGlobals());
  31. it("PD03 同目录项目的新建草稿引用不串用", () => {
  32. vi.stubGlobal("window", new EventTarget());
  33. const item = reference("project-p");
  34. dispatchConversationSelectionAdd({
  35. targetSessionId: null,
  36. workspaceKey: "/same-project-root",
  37. draftScopeKey: "project-p",
  38. reference: item,
  39. });
  40. expect(getConversationSelectionReferenceScope(null, "/same-project-root", "project-p")).toEqual(
  41. [item],
  42. );
  43. expect(getConversationSelectionReferenceScope(null, "/same-project-root", "project-q")).toEqual(
  44. [],
  45. );
  46. });
  47. it("在 composer 挂载前先写入 session scope,首次打开不会丢引用", () => {
  48. vi.stubGlobal("window", new EventTarget());
  49. const nextReference = reference("ref-before-mount");
  50. expect(
  51. dispatchConversationSelectionAdd({
  52. targetSessionId: "child-before-mount",
  53. workspaceKey: "/workspace",
  54. reference: nextReference,
  55. }),
  56. ).toMatchObject({ ok: true, duplicate: false });
  57. expect(getConversationSelectionReferenceScope("child-before-mount", "/workspace")).toEqual([
  58. nextReference,
  59. ]);
  60. clearConversationSelectionReferenceScope("child-before-mount", "/workspace");
  61. });
  62. it("按来源与原文去重,同文不同 row 保留", () => {
  63. const first = appendConversationSelectionReference([], reference("ref-1"));
  64. expect(first).toMatchObject({ ok: true, duplicate: false });
  65. if (!first.ok) return;
  66. const duplicate = appendConversationSelectionReference(first.references, reference("ref-2"));
  67. expect(duplicate).toMatchObject({ ok: true, duplicate: true });
  68. const otherRow = appendConversationSelectionReference(first.references, reference("ref-3", 2));
  69. expect(otherRow).toMatchObject({ ok: true, duplicate: false });
  70. if (otherRow.ok) expect(otherRow.references).toHaveLength(2);
  71. });
  72. it("超限原子拒绝,不截断或修改已有引用", () => {
  73. const existing = [reference("ref-existing", 1, "a".repeat(8_000))];
  74. const rejected = appendConversationSelectionReference(
  75. existing,
  76. reference("ref-next", 2, "b".repeat(8_001)),
  77. );
  78. expect(rejected).toEqual({ ok: false, reason: "single" });
  79. expect(existing).toHaveLength(1);
  80. expect(existing[0]?.text).toHaveLength(8_000);
  81. });
  82. it("新发送只序列化 userselect text,并恢复可见正文与 chips", () => {
  83. const references = [reference("ref-1"), reference("ref-2", 2, "second")];
  84. const prompt = buildPromptWithConversationSelections("Explain this", references);
  85. expect(prompt).toBe(
  86. [
  87. "Explain this",
  88. "",
  89. "# userselect:",
  90. "```userselect",
  91. JSON.stringify([{ text: "selected text" }, { text: "second" }]),
  92. "```",
  93. ].join("\n"),
  94. );
  95. expect(parsePromptConversationSelections(prompt)).toEqual({
  96. visibleContent: "Explain this",
  97. references: [{ text: "selected text" }, { text: "second" }],
  98. });
  99. });
  100. it("文件路径随选段往返,普通对话与无路径 Markdown 不发送内部来源", () => {
  101. const file = {
  102. id: "md",
  103. contentType: "markdown" as const,
  104. sourceKey: "identity",
  105. sourceTitle: "note",
  106. path: "C:\\docs\\a.md",
  107. text: "excerpt",
  108. };
  109. const prompt = buildPromptWithConversationSelections("Explain", [
  110. file,
  111. reference("msg"),
  112. { ...file, path: undefined },
  113. ]);
  114. expect(parsePromptConversationSelections(prompt)).toEqual({
  115. visibleContent: "Explain",
  116. references: [
  117. { path: file.path, text: "excerpt" },
  118. { text: "selected text" },
  119. { text: "excerpt" },
  120. ],
  121. });
  122. expect(prompt).not.toContain("identity");
  123. expect(
  124. buildPromptWithConversationSelections(
  125. "Explain",
  126. parsePromptConversationSelections(prompt).references,
  127. ),
  128. ).toBe(prompt);
  129. });
  130. it.each([42, "", " ", null])("非法文件路径不吞掉原文:%s", (path) => {
  131. const prompt =
  132. "# userselect:\n```userselect\n" + JSON.stringify([{ path, text: "excerpt" }]) + "\n```";
  133. expect(parsePromptConversationSelections(prompt)).toEqual({
  134. visibleContent: prompt,
  135. references: [],
  136. });
  137. });
  138. it("继续只读解析旧 Conversation selections 完整结构块", () => {
  139. const references = [reference("legacy-ref")];
  140. const legacyPrompt = [
  141. "Explain legacy",
  142. "",
  143. "# Conversation selections:",
  144. "```zcode-conversation-selections",
  145. JSON.stringify(references),
  146. "```",
  147. ].join("\n");
  148. expect(parsePromptConversationSelections(legacyPrompt)).toEqual({
  149. visibleContent: "Explain legacy",
  150. references,
  151. });
  152. });
  153. it("与 Web element block 共存且按发送端顺序逐层解析", () => {
  154. const webContext: WebElementContextComposerAttachment = {
  155. id: "web-1",
  156. pageTitle: "Example",
  157. pageUrl: "https://example.com",
  158. selector: "#submit",
  159. tagName: "button",
  160. text: "Submit",
  161. workspacePath: "/workspace",
  162. };
  163. const selectionReferences = [reference("ref-1")];
  164. const prompt = buildPromptWithWebElementContexts(
  165. buildPromptWithConversationSelections("Explain both", selectionReferences),
  166. [webContext],
  167. );
  168. const webParsed = parsePromptWebElementContexts(prompt, {
  169. workspacePath: "/workspace",
  170. });
  171. const selectionParsed = parsePromptConversationSelections(webParsed.visibleContent);
  172. expect(webParsed.webElementContexts).toHaveLength(1);
  173. expect(selectionParsed).toEqual({
  174. visibleContent: "Explain both",
  175. references: [{ text: "selected text" }],
  176. });
  177. });
  178. });