zcodeFileCitation.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. extractZCodeFileCitations,
  4. projectZCodeFileCitations,
  5. resolveZCodeFileCitationPreviewKind,
  6. } from "@/lib/zcodeFileCitation.js";
  7. describe("zcode-file-citation parser", () => {
  8. it("只保留 path、purpose 和 artifact_kind,并容忍其它定位参数", () => {
  9. const content = [
  10. '前文 ::zcode-file-citation{path="docs/年度 报告.docx" purpose="output" artifact_kind="document" page_number=4 future_locator="ignored"} 后文',
  11. String.raw`::zcode-file-citation{path='C:\Users\张三\演示 文稿.pptx', artifact_kind=presentation, slide_number=3}`,
  12. ':zcode-file-citation{path="sheets/年度 总结.xlsx" purpose="source" artifact_kind="workbook" sheet_name="总览"}',
  13. ':::zcode-file-citation{path="reports/论文.pdf" purpose="source"}',
  14. ].join("\n");
  15. expect(extractZCodeFileCitations(content)).toEqual([
  16. {
  17. artifactKind: "document",
  18. end: content.indexOf("} 后文") + 1,
  19. path: "docs/年度 报告.docx",
  20. purpose: "output",
  21. raw: '::zcode-file-citation{path="docs/年度 报告.docx" purpose="output" artifact_kind="document" page_number=4 future_locator="ignored"}',
  22. start: 3,
  23. },
  24. {
  25. artifactKind: "presentation",
  26. end: content.indexOf("\n:zcode-file-citation"),
  27. path: String.raw`C:\Users\张三\演示 文稿.pptx`,
  28. purpose: undefined,
  29. raw: String.raw`::zcode-file-citation{path='C:\Users\张三\演示 文稿.pptx', artifact_kind=presentation, slide_number=3}`,
  30. start: content.indexOf("\n::zcode-file-citation") + 1,
  31. },
  32. {
  33. artifactKind: "workbook",
  34. end: content.indexOf("\n:::zcode-file-citation"),
  35. path: "sheets/年度 总结.xlsx",
  36. purpose: "source",
  37. raw: ':zcode-file-citation{path="sheets/年度 总结.xlsx" purpose="source" artifact_kind="workbook" sheet_name="总览"}',
  38. start: content.indexOf("\n:zcode-file-citation") + 1,
  39. },
  40. {
  41. end: content.length,
  42. path: "reports/论文.pdf",
  43. purpose: "source",
  44. raw: ':::zcode-file-citation{path="reports/论文.pdf" purpose="source"}',
  45. start: content.indexOf("\n:::zcode-file-citation") + 1,
  46. },
  47. ]);
  48. });
  49. it("支持 citation 参数使用中文智能单双引号", () => {
  50. const content = [
  51. '::zcode-file-citation{path=“docs/年度 报告.docx” purpose=‘output’ artifact_kind="document"}',
  52. "::zcode-file-citation{path='docs/普通 报告.pdf' purpose=\"output\"}",
  53. ].join("\n");
  54. expect(
  55. extractZCodeFileCitations(content).map(({ path, purpose, artifactKind }) => ({
  56. path,
  57. purpose,
  58. artifactKind,
  59. })),
  60. ).toEqual([
  61. {
  62. path: "docs/年度 报告.docx",
  63. purpose: "output",
  64. artifactKind: "document",
  65. },
  66. {
  67. path: "docs/普通 报告.pdf",
  68. purpose: "output",
  69. artifactKind: undefined,
  70. },
  71. ]);
  72. });
  73. it("忽略未闭合、参数格式错误和缺少 path 的指令", () => {
  74. expect(
  75. extractZCodeFileCitations(
  76. [
  77. '::zcode-file-citation{purpose="output"}',
  78. '::zcode-file-citation{path "missing equals.pdf"}',
  79. '::zcode-file-citation{path="unclosed.pdf"',
  80. ':zcode-file-citation{path "single-colon-missing-equals.pdf"}',
  81. '::::zcode-file-citation{path="too-many-colons.pdf"}',
  82. ].join("\n"),
  83. ),
  84. ).toEqual([]);
  85. });
  86. it("错配智能引号时 fail closed", () => {
  87. expect(extractZCodeFileCitations("::zcode-file-citation{path=“docs/report.pdf'}")).toEqual([]);
  88. });
  89. it("streaming 隐藏最后一行未闭合 citation,但不吞掉异常输出后的正文", () => {
  90. const incomplete = projectZCodeFileCitations(
  91. '正文\n::zcode-file-citation{path="reports/result.pdf"',
  92. { streaming: true },
  93. );
  94. expect(incomplete.visibleText).toBe("正文\n");
  95. const inlineIncomplete = projectZCodeFileCitations(
  96. '请注意 ::zcode-file-citation{path="reports/result.pdf"',
  97. { streaming: true },
  98. );
  99. expect(inlineIncomplete.visibleText).toBe("请注意 ");
  100. const malformedWithFollowingText = projectZCodeFileCitations(
  101. '正文\n::zcode-file-citation{path="reports/result.pdf"\n后续说明仍然可见',
  102. { streaming: true },
  103. );
  104. expect(malformedWithFollowingText.visibleText).toContain("后续说明仍然可见");
  105. expect(malformedWithFollowingText.visibleText).toContain("::zcode-file-citation");
  106. const inlineMalformed = projectZCodeFileCitations(
  107. '请注意 ::zcode-file-citation{path="reports/result.pdf" 后续说明',
  108. { streaming: true },
  109. );
  110. expect(inlineMalformed.visibleText).toContain("请注意");
  111. expect(inlineMalformed.visibleText).toContain("后续说明");
  112. const legacyIncomplete = projectZCodeFileCitations(
  113. '正文\n:zcode-file-citation{path="reports/legacy.pdf"',
  114. { streaming: true },
  115. );
  116. expect(legacyIncomplete.visibleText).toBe("正文\n");
  117. const legacyMalformed = projectZCodeFileCitations(
  118. '正文\n:zcode-file-citation{path="reports/legacy.pdf"\n后续说明仍然可见',
  119. { streaming: true },
  120. );
  121. expect(legacyMalformed.visibleText).toContain("后续说明仍然可见");
  122. expect(legacyMalformed.visibleText).toContain(":zcode-file-citation");
  123. const tripleIncomplete = projectZCodeFileCitations(
  124. '正文\n:::zcode-file-citation{path="reports/triple.pdf"',
  125. { streaming: true },
  126. );
  127. expect(tripleIncomplete.visibleText).toBe("正文\n");
  128. });
  129. it("streaming 在 citation 名称尚未闭合前暂存协议前缀", () => {
  130. expect(projectZCodeFileCitations("正文\n::", { streaming: true }).visibleText).toBe("正文\n");
  131. expect(
  132. projectZCodeFileCitations("正文\n::zcode-file-cit", { streaming: true }).visibleText,
  133. ).toBe("正文\n");
  134. expect(projectZCodeFileCitations("正文\n:", { streaming: true }).visibleText).toBe("正文\n:");
  135. expect(
  136. projectZCodeFileCitations("正文\n:zcode-file-cit", { streaming: true }).visibleText,
  137. ).toBe("正文\n");
  138. expect(
  139. projectZCodeFileCitations("正文\n:::zcode-file-cit", { streaming: true }).visibleText,
  140. ).toBe("正文\n");
  141. expect(
  142. projectZCodeFileCitations("正文\n::zcode-file-citationX", { streaming: true }).visibleText,
  143. ).toContain("::zcode-file-citationX");
  144. expect(
  145. projectZCodeFileCitations("正文\n:zcode-file-citationX", { streaming: true }).visibleText,
  146. ).toContain(":zcode-file-citationX");
  147. });
  148. it("智能引号未闭合时仍隐藏流式 citation 尾部", () => {
  149. const result = projectZCodeFileCitations("正文\n::zcode-file-citation{path=“docs/report.pdf”", {
  150. streaming: true,
  151. });
  152. expect(result.visibleText).toBe("正文\n");
  153. });
  154. it("artifact_kind 优先映射且必须与后缀兼容,未设置时按后缀推断", () => {
  155. expect(
  156. resolveZCodeFileCitationPreviewKind({
  157. artifactKind: "document",
  158. path: "report.docx",
  159. }),
  160. ).toBe("docx");
  161. expect(
  162. resolveZCodeFileCitationPreviewKind({
  163. artifactKind: "presentation",
  164. path: "deck.pptx",
  165. }),
  166. ).toBe("pptx");
  167. expect(
  168. resolveZCodeFileCitationPreviewKind({
  169. artifactKind: "workbook",
  170. path: "book.xlsx",
  171. }),
  172. ).toBe("xlsx");
  173. expect(resolveZCodeFileCitationPreviewKind({ path: "paper.PDF" })).toBe("pdf");
  174. expect(resolveZCodeFileCitationPreviewKind({ path: "README.md" })).toBeNull();
  175. expect(
  176. resolveZCodeFileCitationPreviewKind({
  177. artifactKind: "document",
  178. path: "paper.pdf",
  179. }),
  180. ).toBeNull();
  181. expect(
  182. resolveZCodeFileCitationPreviewKind({
  183. artifactKind: "unknown",
  184. path: "paper.pdf",
  185. }),
  186. ).toBeNull();
  187. expect(
  188. resolveZCodeFileCitationPreviewKind({
  189. artifactKind: "",
  190. path: "paper.pdf",
  191. }),
  192. ).toBeNull();
  193. });
  194. });