mentionSearch.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildFileMentionMarkdown,
  4. buildMentionMarkdown,
  5. buildSkillMentionMarkdown,
  6. formatSkillMentionDisplayLabel,
  7. parseMentionMarkdown,
  8. renderMentionMarkdownAsPlainText,
  9. } from "../src/mentions/mentionMarkdown.js";
  10. import { collectSessionMentionItems } from "../src/mentions/providers/sessionsMentionProvider.js";
  11. import {
  12. buildVisibleMentionGroups,
  13. filterMentionItems,
  14. filterMentionItemsWithOptions,
  15. getMentionGroupLimitForQuery,
  16. hasMentionQuery,
  17. MENTION_DEFAULT_GROUP_PREVIEW_LIMIT,
  18. MENTION_FILES_ONLY_DEFAULT_PREVIEW_LIMIT,
  19. } from "../src/mentions/mentionSearch.js";
  20. import type { MentionItem } from "../src/mentions/mentionTypes.js";
  21. const items: MentionItem[] = [
  22. {
  23. id: "file:package.json",
  24. category: "files",
  25. label: "package.json",
  26. description: "package.json",
  27. value: "package.json",
  28. markdown: buildFileMentionMarkdown("package.json", "package.json"),
  29. keywords: ["/repo/package.json"],
  30. },
  31. {
  32. id: "file:packages/ui/src/LexicalChatInput.tsx",
  33. category: "files",
  34. label: "LexicalChatInput.tsx",
  35. description: "packages/ui/src/LexicalChatInput.tsx",
  36. value: "packages/ui/src/LexicalChatInput.tsx",
  37. markdown: buildFileMentionMarkdown(
  38. "packages/ui/src/LexicalChatInput.tsx",
  39. "LexicalChatInput.tsx",
  40. ),
  41. keywords: ["/repo/packages/ui/src/LexicalChatInput.tsx"],
  42. },
  43. ];
  44. describe("buildFileMentionMarkdown", () => {
  45. it("serializes the file mention to markdown link syntax", () => {
  46. expect(
  47. buildFileMentionMarkdown("packages/ui/src/LexicalChatInput.tsx", "LexicalChatInput.tsx"),
  48. ).toBe("[LexicalChatInput.tsx](./packages/ui/src/LexicalChatInput.tsx)");
  49. });
  50. it("flattens serialized file mentions to plain text labels", () => {
  51. expect(
  52. renderMentionMarkdownAsPlainText(
  53. "请查看 [src/pages/\\[demo\\].html](./src/pages/\\[demo\\].html)",
  54. ),
  55. ).toBe("请查看 src/pages/[demo].html");
  56. expect(
  57. renderMentionMarkdownAsPlainText(
  58. "请查看旧格式 [@src/pages/\\[demo\\].html](<./src/pages/\\[demo\\].html>)",
  59. ),
  60. ).toBe("请查看旧格式 src/pages/[demo].html");
  61. expect(renderMentionMarkdownAsPlainText("**保持原样**")).toBe("**保持原样**");
  62. });
  63. });
  64. describe("buildSkillMentionMarkdown", () => {
  65. it("serializes skills mention to markdown link syntax when path exists", () => {
  66. expect(buildSkillMentionMarkdown("code-review", ".codex/skills/code-review/SKILL.md")).toBe(
  67. "[$code-review](./.codex/skills/code-review/SKILL.md)",
  68. );
  69. });
  70. it("falls back to inline syntax when path is missing", () => {
  71. expect(buildSkillMentionMarkdown("code-review")).toBe("$code-review");
  72. });
  73. it("buildMentionMarkdown supports skills category", () => {
  74. expect(
  75. buildMentionMarkdown({
  76. category: "skills",
  77. label: "Code Review",
  78. value: "code-review",
  79. data: {
  80. path: ".codex/skills/code-review/SKILL.md",
  81. },
  82. }),
  83. ).toBe("[$code-review](./.codex/skills/code-review/SKILL.md)");
  84. });
  85. it("buildMentionMarkdown supports sessions category", () => {
  86. expect(
  87. buildMentionMarkdown({
  88. category: "sessions",
  89. label: "Chat",
  90. value: "sess_123",
  91. data: {},
  92. }),
  93. ).toBe("[#Chat](#sess_123)");
  94. });
  95. });
  96. describe("parseMentionMarkdown", () => {
  97. it("parses skill links and keeps old inline skill syntax compatible", () => {
  98. expect(
  99. parseMentionMarkdown(
  100. "运行 [$agent-browser](./.agents/skills/agent-browser/SKILL.md) 然后 $code-review",
  101. ),
  102. ).toEqual([
  103. { type: "text", text: "运行 " },
  104. { type: "skill", label: "agent-browser" },
  105. { type: "text", text: " 然后 " },
  106. { type: "skill", label: "code-review" },
  107. ]);
  108. });
  109. it("parses inline session mentions", () => {
  110. expect(parseMentionMarkdown("发给 #archived 和 #sess_123 继续聊")).toEqual([
  111. { type: "text", text: "发给 #archived 和 " },
  112. { type: "session", label: "sess_123" },
  113. { type: "text", text: " 继续聊" },
  114. ]);
  115. expect(parseMentionMarkdown("发给 [#Review mailbox flow](#sess_123) 继续聊")).toEqual([
  116. { type: "text", text: "发给 " },
  117. { type: "session", label: "Review mailbox flow" },
  118. { type: "text", text: " 继续聊" },
  119. ]);
  120. });
  121. it("parses inline subagent mentions", () => {
  122. expect(parseMentionMarkdown("交给 @reviewer 继续")).toEqual([
  123. { type: "text", text: "交给 " },
  124. { type: "subagent", label: "reviewer" },
  125. { type: "text", text: " 继续" },
  126. ]);
  127. });
  128. it("parses directory links as directory mentions", () => {
  129. expect(
  130. parseMentionMarkdown("查看 [@src/components](<./src/components/>) 和 [src/app](./src/app/)"),
  131. ).toEqual([
  132. { type: "text", text: "查看 " },
  133. { type: "directory", label: "src/components" },
  134. { type: "text", text: " 和 " },
  135. { type: "directory", label: "src/app" },
  136. ]);
  137. });
  138. it("parses file links without keeping the legacy @ prefix", () => {
  139. expect(
  140. parseMentionMarkdown("查看 [@src/demo.ts](<./src/demo.ts>) 和 [src/app.ts](./src/app.ts)"),
  141. ).toEqual([
  142. { type: "text", text: "查看 " },
  143. { type: "file", label: "src/demo.ts" },
  144. { type: "text", text: " 和 " },
  145. { type: "file", label: "src/app.ts" },
  146. ]);
  147. });
  148. });
  149. describe("collectSessionMentionItems", () => {
  150. it("merges all known task sources and dedupes by ZCode Agent session id", () => {
  151. const workspacePath = "/local/workspace";
  152. const items = collectSessionMentionItems(
  153. [
  154. {
  155. taskId: "sess_local_old",
  156. traceId: "trace-local-old",
  157. title: "Local old",
  158. workspacePath,
  159. createdAt: 1,
  160. updatedAt: 10,
  161. mode: "default",
  162. provider: "glm",
  163. },
  164. {
  165. taskId: "sess_remote_new",
  166. traceId: "trace-remote-new",
  167. title: "Remote new",
  168. workspacePath: "/remote/workspace",
  169. workspaceIdentity: "ssh://box/remote/workspace",
  170. createdAt: 2,
  171. updatedAt: 100,
  172. mode: "default",
  173. provider: "glm",
  174. },
  175. {
  176. taskId: "sess_remote_new",
  177. traceId: "trace-remote-new-stale",
  178. title: "Remote stale duplicate",
  179. workspacePath: "/remote/workspace",
  180. workspaceIdentity: "ssh://box/remote/workspace",
  181. createdAt: 2,
  182. updatedAt: 90,
  183. mode: "default",
  184. provider: "glm",
  185. },
  186. ],
  187. "glm",
  188. );
  189. expect(items.map((item) => item.value)).toEqual(["sess_remote_new", "sess_local_old"]);
  190. expect(items[0]?.label).toBe("Remote new");
  191. });
  192. it("does not expose a leading session id from title text as the visible label", () => {
  193. const items = collectSessionMentionItems(
  194. [
  195. {
  196. taskId: "sess_visible_target",
  197. traceId: "trace-session-title",
  198. title: "#sess_visible_target continue this chat",
  199. workspacePath: "/workspace/current",
  200. createdAt: 1,
  201. updatedAt: 10,
  202. mode: "default",
  203. provider: "glm",
  204. },
  205. ],
  206. "glm",
  207. );
  208. expect(items[0]?.label).toBe("continue this chat");
  209. expect(items[0]?.markdown).toBe("[#continue this chat](#sess_visible_target)");
  210. });
  211. it("keeps app-created sessions, hides imported native history, and prioritizes the current workspace", () => {
  212. const items = collectSessionMentionItems(
  213. [
  214. {
  215. taskId: "sess_other_app",
  216. traceId: "trace-other-app",
  217. title: "Other app",
  218. workspacePath: "/workspace/other",
  219. createdAt: 1,
  220. updatedAt: 300,
  221. mode: "default",
  222. provider: "glm",
  223. },
  224. {
  225. taskId: "sess_current_app",
  226. traceId: "trace-current-app",
  227. title: "Current app",
  228. workspacePath: "/workspace/current",
  229. createdAt: 2,
  230. updatedAt: 100,
  231. mode: "default",
  232. provider: "glm",
  233. },
  234. {
  235. taskId: "sess_imported_native",
  236. traceId: "trace-imported-native",
  237. title: "Imported native",
  238. workspacePath: "/workspace/current",
  239. createdAt: 3,
  240. updatedAt: 400,
  241. mode: "default",
  242. provider: "claude",
  243. migrationSource: "claudeCode",
  244. },
  245. ],
  246. "glm",
  247. {
  248. workspacePath: "/workspace/current",
  249. },
  250. );
  251. expect(items.map((item) => item.value)).toEqual(["sess_current_app", "sess_other_app"]);
  252. expect(items.map((item) => item.description)).toEqual(["current", "other"]);
  253. });
  254. });
  255. describe("formatSkillMentionDisplayLabel", () => {
  256. it("将 kebab-case slug 格式化为标题样式展示", () => {
  257. expect(formatSkillMentionDisplayLabel("code-review")).toBe("Code Review");
  258. });
  259. it("已是带空格的短语时不改写", () => {
  260. expect(formatSkillMentionDisplayLabel("Code Review")).toBe("Code Review");
  261. });
  262. });
  263. describe("filterMentionItems", () => {
  264. it("keeps original order when query is empty", () => {
  265. expect(filterMentionItems(items, "").map((item) => item.id)).toEqual([
  266. "file:package.json",
  267. "file:packages/ui/src/LexicalChatInput.tsx",
  268. ]);
  269. });
  270. it("prioritizes files before directories in the default empty-query preview", () => {
  271. const mixedItems: MentionItem[] = [
  272. {
  273. id: "file:src",
  274. category: "files",
  275. label: "src",
  276. description: "src",
  277. value: "src",
  278. markdown: buildFileMentionMarkdown("src", "src", "directory"),
  279. data: { kind: "directory" },
  280. },
  281. {
  282. id: "file:package.json",
  283. category: "files",
  284. label: "package.json",
  285. description: "package.json",
  286. value: "package.json",
  287. markdown: buildFileMentionMarkdown("package.json", "package.json", "file"),
  288. data: { kind: "file" },
  289. },
  290. {
  291. id: "file:docs",
  292. category: "files",
  293. label: "docs",
  294. description: "docs",
  295. value: "docs",
  296. markdown: buildFileMentionMarkdown("docs", "docs", "directory"),
  297. data: { kind: "directory" },
  298. },
  299. ];
  300. expect(filterMentionItems(mixedItems, "").map((item) => item.id)).toEqual([
  301. "file:package.json",
  302. "file:src",
  303. "file:docs",
  304. ]);
  305. });
  306. it("matches by file name and relative path", () => {
  307. expect(filterMentionItems(items, "lexical").map((item) => item.id)).toEqual([
  308. "file:packages/ui/src/LexicalChatInput.tsx",
  309. ]);
  310. expect(filterMentionItems(items, "packages/ui").map((item) => item.id)).toEqual([
  311. "file:packages/ui/src/LexicalChatInput.tsx",
  312. ]);
  313. });
  314. it("can require a non-empty query before exposing any results", () => {
  315. expect(
  316. filterMentionItemsWithOptions(items, "", {
  317. requireQuery: true,
  318. }),
  319. ).toEqual([]);
  320. });
  321. it("can cap the number of returned results", () => {
  322. expect(
  323. filterMentionItemsWithOptions(items, "", {
  324. limit: 1,
  325. }).map((item) => item.id),
  326. ).toEqual(["file:package.json"]);
  327. });
  328. it("uses the small default group preview limit before the user types a query", () => {
  329. expect(MENTION_DEFAULT_GROUP_PREVIEW_LIMIT).toBe(3);
  330. expect(MENTION_FILES_ONLY_DEFAULT_PREVIEW_LIMIT).toBe(10);
  331. expect(getMentionGroupLimitForQuery("")).toBe(3);
  332. expect(getMentionGroupLimitForQuery(" ")).toBe(3);
  333. expect(getMentionGroupLimitForQuery("", MENTION_FILES_ONLY_DEFAULT_PREVIEW_LIMIT)).toBe(10);
  334. expect(getMentionGroupLimitForQuery("pkg")).toBeUndefined();
  335. });
  336. });
  337. // 插件 @ 引用中文搜索(feat/plugin_ref_i18n):中文命中走本地化显示名 keywords 通道;
  338. // CJK query 只保留前缀/子串两级,逐字符子序列仅对非 CJK query 生效。
  339. describe("filterMentionItems CJK queries", () => {
  340. const pluginItems: MentionItem[] = [
  341. {
  342. id: "plugin:browser-use@zcode-plugins-official",
  343. category: "plugins",
  344. label: "浏览器操作",
  345. description: "zcode-plugins-official · 2 skills · 1 MCP",
  346. value: "browser-use@zcode-plugins-official",
  347. markdown: "[@browser-use](plugin://browser-use@zcode-plugins-official)",
  348. keywords: [
  349. "browser-use",
  350. "browser-use@zcode-plugins-official",
  351. "zcode-plugins-official",
  352. "浏览器操作",
  353. ],
  354. },
  355. {
  356. id: "plugin:zcode-cua@zcode-plugins-official",
  357. category: "plugins",
  358. label: "电脑控制",
  359. description: "zcode-plugins-official · 1 skills · 1 MCP",
  360. value: "zcode-cua@zcode-plugins-official",
  361. markdown: "[@zcode-cua](plugin://zcode-cua@zcode-plugins-official)",
  362. keywords: [
  363. "zcode-cua",
  364. "zcode-cua@zcode-plugins-official",
  365. "zcode-plugins-official",
  366. "电脑控制",
  367. ],
  368. },
  369. ];
  370. it("matches Chinese display names by prefix and substring", () => {
  371. expect(filterMentionItems(pluginItems, "浏览").map((item) => item.id)).toEqual([
  372. "plugin:browser-use@zcode-plugins-official",
  373. ]);
  374. expect(filterMentionItems(pluginItems, "览器").map((item) => item.id)).toEqual([
  375. "plugin:browser-use@zcode-plugins-official",
  376. ]);
  377. expect(filterMentionItems(pluginItems, "控制").map((item) => item.id)).toEqual([
  378. "plugin:zcode-cua@zcode-plugins-official",
  379. ]);
  380. });
  381. it("does not fall back to per-character subsequence matching for CJK queries", () => {
  382. // 「浏器」是「浏览器操作」的非连续子序列;CJK query 不应命中。
  383. expect(filterMentionItems(pluginItems, "浏器")).toEqual([]);
  384. });
  385. it("keeps subsequence matching for ASCII queries", () => {
  386. expect(filterMentionItems(pluginItems, "bru").map((item) => item.id)).toEqual([
  387. "plugin:browser-use@zcode-plugins-official",
  388. ]);
  389. });
  390. });
  391. describe("hasMentionQuery", () => {
  392. it("only enables search when the query contains non-whitespace characters", () => {
  393. expect(hasMentionQuery("")).toBe(false);
  394. expect(hasMentionQuery(" ")).toBe(false);
  395. expect(hasMentionQuery("pkg")).toBe(true);
  396. });
  397. });
  398. describe("buildVisibleMentionGroups", () => {
  399. it("only keeps groups that have results, loading state, or errors", () => {
  400. expect(
  401. buildVisibleMentionGroups([
  402. {
  403. id: "empty",
  404. title: "Empty",
  405. items: [],
  406. loading: false,
  407. errorText: null,
  408. emptyText: "no items",
  409. },
  410. {
  411. id: "files",
  412. title: "Files",
  413. items,
  414. loading: false,
  415. errorText: null,
  416. emptyText: "no files",
  417. },
  418. ]).map((group) => group.id),
  419. ).toEqual(["files"]);
  420. });
  421. });
  422. describe("filterMentionItemsWithOptions – no hard cap", () => {
  423. const manyItems: MentionItem[] = Array.from({ length: 500 }, (_, i) => ({
  424. id: `file:src/file${i}.ts`,
  425. category: "files" as const,
  426. label: `file${i}.ts`,
  427. description: `src/file${i}.ts`,
  428. value: `src/file${i}.ts`,
  429. markdown: `[src/file${i}.ts](src/file${i}.ts)`,
  430. keywords: [],
  431. }));
  432. it("returns all matching items when no explicit limit is set", () => {
  433. const result = filterMentionItemsWithOptions(manyItems, "file");
  434. expect(result.length).toBe(500);
  435. });
  436. it("respects an explicit limit when provided", () => {
  437. const result = filterMentionItemsWithOptions(manyItems, "file", { limit: 10 });
  438. expect(result.length).toBe(10);
  439. });
  440. });