assistantCodeComment.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildAssistantCodeCommentCards,
  4. extractAssistantCodeComments,
  5. projectAssistantCodeComments,
  6. } from "@/lib/assistantCodeComment.js";
  7. describe("assistant code-comment parser", () => {
  8. it("解析必填字段、范围、优先级、Unicode 和跨平台路径", () => {
  9. const content = [
  10. '::code-comment{title="[P1] 空指针风险" body="user 为空时仍访问 user.id" file="src/用户 服务.ts" start=42 end=45 priority=1 ignored="yes"}',
  11. String.raw`::code-comment{title='Windows path' body='保留反斜杠' file='C:\repo\src\main.ts' start=9}`,
  12. ].join("\n");
  13. expect(extractAssistantCodeComments(content)).toMatchObject([
  14. {
  15. title: "[P1] 空指针风险",
  16. body: "user 为空时仍访问 user.id",
  17. file: "src/用户 服务.ts",
  18. startLine: 42,
  19. endLine: 45,
  20. priority: 1,
  21. },
  22. {
  23. title: "Windows path",
  24. body: "保留反斜杠",
  25. file: String.raw`C:\repo\src\main.ts`,
  26. startLine: 9,
  27. endLine: 9,
  28. },
  29. ]);
  30. });
  31. it("默认不为 code-comment 放宽智能引号语法", () => {
  32. expect(
  33. extractAssistantCodeComments("::code-comment{title=“标题” body=“正文” file=“src/main.ts”}"),
  34. ).toMatchObject([
  35. {
  36. title: "“标题”",
  37. body: "“正文”",
  38. file: "“src/main.ts”",
  39. },
  40. ]);
  41. });
  42. it("允许 body 中包含 Markdown 反引号和 directive 样例文本,不产生嵌套伪卡片", () => {
  43. const content =
  44. '::code-comment{title="Guard" body="Check `user.id`; do not emit ::code-comment{title=\\"fake\\"}" file="src/user.ts"}';
  45. expect(extractAssistantCodeComments(content)).toMatchObject([
  46. {
  47. title: "Guard",
  48. body: 'Check `user.id`; do not emit ::code-comment{title="fake"}',
  49. file: "src/user.ts",
  50. },
  51. ]);
  52. });
  53. it("忽略 fenced/inline code 中的示例,并保留语法错误或缺少必填字段的原文", () => {
  54. const valid = '::code-comment{title="Real" body="Actionable" file="src/real.ts" priority=2}';
  55. const invalid = '::code-comment{title="Missing body" file="src/no.ts"}';
  56. const content = [
  57. "```text",
  58. '::code-comment{title="Example" body="Only docs" file="src/example.ts"}',
  59. "```",
  60. '`::code-comment{title="Inline" body="Only docs" file="src/inline.ts"}`',
  61. invalid,
  62. valid,
  63. ].join("\n");
  64. const projection = projectAssistantCodeComments(content, { streaming: false });
  65. expect(projection.comments).toHaveLength(1);
  66. expect(projection.comments[0]?.title).toBe("Real");
  67. expect(projection.visibleText).toContain("Example");
  68. expect(projection.visibleText).toContain("Inline");
  69. expect(projection.visibleText).toContain(invalid);
  70. expect(projection.visibleText).not.toContain(valid);
  71. });
  72. it("独占行删除整行、内联指令替换为空格,streaming 未闭合尾部不闪现", () => {
  73. const standalone = '::code-comment{title="One" body="Standalone" file="src/a.ts"}';
  74. const inline = '::code-comment{title="Two" body="Inline" file="src/b.ts" start=3}';
  75. const complete = projectAssistantCodeComments(`before\n${standalone}\nafter ${inline} text`, {
  76. streaming: false,
  77. });
  78. expect(complete.visibleText).toBe("before\nafter text");
  79. expect(complete.comments).toHaveLength(2);
  80. expect(
  81. projectAssistantCodeComments('正文\n::code-comment{title="partial" body="still streaming', {
  82. streaming: true,
  83. }).visibleText,
  84. ).toBe("正文\n");
  85. expect(
  86. projectAssistantCodeComments('正文\n::code-comment{title="partial" body="terminal', {
  87. streaming: false,
  88. }).visibleText,
  89. ).toContain("::code-comment");
  90. });
  91. it("流式协议名称尚未到左大括号前也不泄露", () => {
  92. const prefixes = [
  93. "::",
  94. "::code",
  95. "::code-comment",
  96. '::code-comment{title="partial" body="still streaming',
  97. ":::zcode-file-cit",
  98. ];
  99. for (const prefix of prefixes) {
  100. expect(projectAssistantCodeComments(`正文\n${prefix}`, { streaming: true }).visibleText).toBe(
  101. "正文\n",
  102. );
  103. }
  104. });
  105. it("协议前缀分叉或位于代码块时不误吞正文", () => {
  106. expect(
  107. projectAssistantCodeComments("正文\n::code-review", { streaming: true }).visibleText,
  108. ).toContain("::code-review");
  109. expect(
  110. projectAssistantCodeComments("```text\n::code-comment\n```", { streaming: true }).visibleText,
  111. ).toContain("::code-comment");
  112. expect(
  113. projectAssistantCodeComments(
  114. ':code-comment{title="Legacy" body="must stay strict" file="src/a.ts"}',
  115. { streaming: false },
  116. ).comments,
  117. ).toEqual([]);
  118. });
  119. it("构建 workspace 内卡片并拒绝相对越界和 workspace 外绝对路径", () => {
  120. const cards = buildAssistantCodeCommentCards(
  121. [
  122. '::code-comment{title="中文" body="正文" file="src/中文 路径.ts" start=2 end=4 priority=0}',
  123. '::code-comment{title="escape" body="bad" file="foo/../../private.ts"}',
  124. '::code-comment{title="outside" body="bad" file="/private/outside.ts"}',
  125. ].join("\n"),
  126. "/workspace",
  127. );
  128. expect(cards).toMatchObject([
  129. {
  130. title: "中文",
  131. body: "正文",
  132. path: "/workspace/src/中文 路径.ts",
  133. displayPath: "src/中文 路径.ts",
  134. startLine: 2,
  135. endLine: 4,
  136. priority: 0,
  137. },
  138. ]);
  139. });
  140. it("Windows 盘符和 UNC workspace 内路径使用大小写不敏感包含判断", () => {
  141. expect(
  142. buildAssistantCodeCommentCards(
  143. String.raw`::code-comment{title="Win" body="ok" file="c:\Repo\SRC\main.ts"}`,
  144. String.raw`C:\repo`,
  145. )[0]?.displayPath,
  146. ).toBe("SRC/main.ts");
  147. expect(
  148. buildAssistantCodeCommentCards(
  149. String.raw`::code-comment{title="UNC" body="ok" file="\\\\server\share\repo\src\main.ts"}`,
  150. String.raw`\\server\share\repo`,
  151. )[0]?.displayPath,
  152. ).toBe("src/main.ts");
  153. });
  154. it("Home-relative code comment 在 Home 位于 workspace 时复用统一解析器", () => {
  155. const cards = buildAssistantCodeCommentCards(
  156. '::code-comment{title="Home" body="ok" file="~/src/main.ts"}',
  157. "/workspace",
  158. 50,
  159. { homePath: "/workspace" },
  160. );
  161. expect(cards).toMatchObject([
  162. {
  163. path: "/workspace/src/main.ts",
  164. displayPath: "src/main.ts",
  165. },
  166. ]);
  167. });
  168. it("根目录 workspace 仍能生成绝对文件的相对展示路径", () => {
  169. const cards = buildAssistantCodeCommentCards(
  170. '::code-comment{title="Root file" body="检查根目录文件" file="/src/main.ts"}',
  171. "/",
  172. );
  173. expect(cards).toMatchObject([
  174. {
  175. path: "/src/main.ts",
  176. displayPath: "src/main.ts",
  177. },
  178. ]);
  179. });
  180. it("单轮最多生成 50 张卡片且保持 directive 正序", () => {
  181. const content = Array.from(
  182. { length: 55 },
  183. (_, index) =>
  184. `::code-comment{title="Comment ${index + 1}" body="Body" file="src/file-${index + 1}.ts"}`,
  185. ).join("\n");
  186. const cards = buildAssistantCodeCommentCards(content, "/workspace");
  187. expect(cards).toHaveLength(50);
  188. expect(cards[0]?.title).toBe("Comment 1");
  189. expect(cards.at(-1)?.title).toBe("Comment 50");
  190. });
  191. });