conversationSelectionReference.test.ts 6.0 KB

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