mentionSearch.test.ts 15 KB

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