pptxElementReference.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. addPptxElementReference,
  4. buildPromptWithPptxElementReferences,
  5. createPptxElementReference,
  6. getPptxElementReferenceWorkspaceKey,
  7. isPptxElementReferencePayload,
  8. isPptxElementReferenceInWorkspaceScope,
  9. parsePromptPptxElementReferences,
  10. sha256Fingerprint,
  11. type PptxElementReference,
  12. } from "@/lib/pptxElementReference.js";
  13. function makeReference(
  14. overrides: Partial<PptxElementReference> = {},
  15. ): PptxElementReference {
  16. return {
  17. bounds: { x: 10, y: 20, width: 300, height: 80 },
  18. capturedAt: 1_700_000_000_000,
  19. id: "pptx-ref-1",
  20. nodeId: "7",
  21. nodeName: "Title 1",
  22. nodeType: "shape",
  23. slideIndex: 0,
  24. slidePart: "ppt/slides/slide1.xml",
  25. sourceFingerprint: `sha256:${"a".repeat(64)}`,
  26. sourcePath: "/workspace/deck.pptx",
  27. sourceTitle: "deck.pptx",
  28. text: "Quarterly review",
  29. textFingerprint: `sha256:${"b".repeat(64)}`,
  30. workspaceIdentity: "ssh:user@host:/workspace",
  31. workspacePath: "/workspace",
  32. remoteSessionId: "remote-1",
  33. zIndex: 2,
  34. ...overrides,
  35. };
  36. }
  37. describe("PPTX element reference", () => {
  38. it("划选子串和评论只作为上下文,完整元素文本生成引用 fingerprint", async () => {
  39. const elementText = "Quarterly review";
  40. const selectedText = "Quarterly";
  41. const reference = await createPptxElementReference({
  42. element: {
  43. ...makeReference(),
  44. text: elementText,
  45. },
  46. selectedText,
  47. comment: " Make it more concise ",
  48. workspacePath: "/workspace",
  49. sourcePath: "/workspace/deck.pptx",
  50. sourceTitle: "deck.pptx",
  51. sourceFingerprint: `sha256:${"a".repeat(64)}`,
  52. });
  53. expect(reference.text).toBe(elementText);
  54. expect(reference.selectedText).toBe(selectedText);
  55. expect(reference.comment).toBe("Make it more concise");
  56. expect(reference.textFingerprint).toBe(
  57. await sha256Fingerprint(elementText),
  58. );
  59. });
  60. it("空白评论不进入引用,也不改变元素指纹", async () => {
  61. const elementText = "Quarterly review";
  62. const reference = await createPptxElementReference({
  63. element: {
  64. ...makeReference(),
  65. text: elementText,
  66. },
  67. comment: " ",
  68. workspacePath: "/workspace",
  69. sourcePath: "/workspace/deck.pptx",
  70. sourceTitle: "deck.pptx",
  71. sourceFingerprint: `sha256:${"a".repeat(64)}`,
  72. });
  73. expect(reference).not.toHaveProperty("comment");
  74. expect(reference.textFingerprint).toBe(
  75. await sha256Fingerprint(elementText),
  76. );
  77. });
  78. it("校验稳定 source、slide、node 与 table-cell 坐标", () => {
  79. expect(isPptxElementReferencePayload(makeReference())).toBe(true);
  80. expect(
  81. isPptxElementReferencePayload(
  82. makeReference({ nodeType: "table-cell", rowIndex: 1, cellIndex: 2 }),
  83. ),
  84. ).toBe(true);
  85. expect(
  86. isPptxElementReferencePayload(
  87. makeReference({
  88. nodeType: "table-cell",
  89. rowIndex: undefined,
  90. cellIndex: 2,
  91. }),
  92. ),
  93. ).toBe(false);
  94. expect(
  95. isPptxElementReferencePayload(
  96. makeReference({ sourceFingerprint: "size:42" }),
  97. ),
  98. ).toBe(false);
  99. expect(
  100. isPptxElementReferencePayload({
  101. ...makeReference(),
  102. nodePath: 42,
  103. }),
  104. ).toBe(false);
  105. expect(
  106. isPptxElementReferencePayload({
  107. ...makeReference(),
  108. text: { unexpected: true },
  109. }),
  110. ).toBe(false);
  111. expect(
  112. isPptxElementReferencePayload({
  113. ...makeReference(),
  114. selectedText: { unexpected: true },
  115. }),
  116. ).toBe(false);
  117. expect(
  118. isPptxElementReferencePayload({
  119. ...makeReference(),
  120. comment: { unexpected: true },
  121. }),
  122. ).toBe(false);
  123. });
  124. it("按 source path、fingerprint、slide part、node id 和 cell 坐标精确去重", () => {
  125. const first = makeReference();
  126. const updated = makeReference({
  127. id: "new-id",
  128. text: "Updated preview",
  129. selectedText: "Updated",
  130. comment: "Use the revised wording",
  131. });
  132. const anotherCell = makeReference({
  133. id: "cell-2",
  134. nodeType: "table-cell",
  135. rowIndex: 0,
  136. cellIndex: 1,
  137. });
  138. expect(addPptxElementReference([first], updated)).toEqual([updated]);
  139. expect(addPptxElementReference([first], anotherCell)).toEqual([
  140. first,
  141. anotherCell,
  142. ]);
  143. expect(
  144. addPptxElementReference(
  145. [first],
  146. makeReference({
  147. id: "copied-deck",
  148. sourcePath: "/workspace/copy/deck.pptx",
  149. }),
  150. ),
  151. ).toHaveLength(2);
  152. });
  153. it("以代码评论式语义序列化多条 PPTX 评论,并可无损恢复", () => {
  154. const reference = makeReference({
  155. text: "Title with ``` marker",
  156. selectedText: "Title",
  157. comment: "Use sentence case",
  158. });
  159. const secondReference = makeReference({
  160. id: "pptx-ref-2",
  161. nodeId: "8",
  162. comment: "Make the subtitle shorter",
  163. });
  164. const prompt = buildPromptWithPptxElementReferences("请执行", [
  165. reference,
  166. secondReference,
  167. ]);
  168. const parsed = parsePromptPptxElementReferences(prompt);
  169. expect(prompt).toContain("# Presentation element comments:");
  170. expect(prompt).toContain(
  171. "Treat every non-empty `comment` as the user's instruction",
  172. );
  173. expect(prompt).toContain('"comment": "Use sentence case"');
  174. expect(prompt).toContain('"comment": "Make the subtitle shorter"');
  175. expect(parsed.visibleContent).toBe("请执行");
  176. expect(parsed.pptxElementReferences).toEqual([reference, secondReference]);
  177. });
  178. it("继续反解析历史 Presentation elements JSON 尾块", () => {
  179. const reference = makeReference();
  180. const legacyPrompt = `Please inspect\n\n# Presentation elements:\n\n\`\`\`json\n${JSON.stringify(
  181. [reference],
  182. null,
  183. 2,
  184. )}\n\`\`\``;
  185. expect(parsePromptPptxElementReferences(legacyPrompt)).toEqual({
  186. visibleContent: "Please inspect",
  187. pptxElementReferences: [reference],
  188. });
  189. });
  190. it("正文自带同名标题时只消费真实尾块,不截断可见正文", () => {
  191. // 回归:标题与 JSON 围栏之间若按通配匹配 directive,正文里的同名标题会先命中,
  192. // 并把它之后的正文一起吞进尾块。
  193. const visibleContent =
  194. "Please read this:\n\n# Presentation element comments:\n\nblah blah";
  195. const reference = makeReference({ comment: "Shorten it" });
  196. const prompt = buildPromptWithPptxElementReferences(visibleContent, [
  197. reference,
  198. ]);
  199. expect(parsePromptPptxElementReferences(prompt)).toEqual({
  200. visibleContent,
  201. pptxElementReferences: [reference],
  202. });
  203. });
  204. it("允许只有引用的 context-only prompt,并保持 workspace identity fallback", () => {
  205. const prompt = buildPromptWithPptxElementReferences("", [makeReference()]);
  206. expect(
  207. parsePromptPptxElementReferences(prompt).pptxElementReferences,
  208. ).toHaveLength(1);
  209. expect(
  210. getPptxElementReferenceWorkspaceKey("/workspace", " remote-id "),
  211. ).toBe("remote-id");
  212. expect(getPptxElementReferenceWorkspaceKey("/workspace", " ")).toBe(
  213. "/workspace",
  214. );
  215. expect(
  216. isPptxElementReferenceInWorkspaceScope(makeReference(), {
  217. workspacePath: "/different-mounted-path",
  218. workspaceIdentity: "ssh:user@host:/workspace",
  219. remoteSessionId: "remote-1",
  220. }),
  221. ).toBe(true);
  222. expect(
  223. isPptxElementReferenceInWorkspaceScope(makeReference(), {
  224. workspacePath: "/workspace",
  225. workspaceIdentity: "ssh:user@host:/workspace",
  226. remoteSessionId: "remote-2",
  227. }),
  228. ).toBe(false);
  229. });
  230. });