workspaceFileSearch.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { describe, expect, it } from "vitest";
  2. import type { WorkspaceFileEntry } from "@zcode/shared";
  3. import {
  4. filterWorkspaceFileSearchCandidates,
  5. mapWorkspaceFileEntriesToSearchCandidates,
  6. } from "@/workspace-file-search/workspaceFileSearch.js";
  7. function entry(relativePath: string, type: WorkspaceFileEntry["type"] = "file") {
  8. const name = relativePath.split("/").pop() ?? relativePath;
  9. return {
  10. name,
  11. path: `/workspace/${relativePath}`,
  12. relativePath,
  13. type,
  14. } satisfies WorkspaceFileEntry;
  15. }
  16. describe("workspace file search", () => {
  17. it("matches by file name, relative path and fuzzy class-name query", () => {
  18. const candidates = mapWorkspaceFileEntriesToSearchCandidates([
  19. entry("src/main/java/com/example/user/UserProfileService.java"),
  20. entry("src/main/java/com/example/user/UserProfileController.java"),
  21. entry("docs/profile-service.md"),
  22. ]);
  23. expect(
  24. filterWorkspaceFileSearchCandidates(candidates, "UserProfileService").map(
  25. (candidate) => candidate.relativePath,
  26. ),
  27. ).toEqual(["src/main/java/com/example/user/UserProfileService.java"]);
  28. expect(
  29. filterWorkspaceFileSearchCandidates(candidates, "com/example/user").map(
  30. (candidate) => candidate.relativePath,
  31. ),
  32. ).toEqual([
  33. "src/main/java/com/example/user/UserProfileService.java",
  34. "src/main/java/com/example/user/UserProfileController.java",
  35. ]);
  36. expect(
  37. filterWorkspaceFileSearchCandidates(candidates, "upsrv").map(
  38. (candidate) => candidate.relativePath,
  39. ),
  40. ).toEqual(["src/main/java/com/example/user/UserProfileService.java"]);
  41. });
  42. it("keeps file-before-directory empty-query priority compatible with @ files", () => {
  43. const candidates = mapWorkspaceFileEntriesToSearchCandidates([
  44. entry("src", "directory"),
  45. entry("README.md"),
  46. entry("docs", "directory"),
  47. entry("package.json"),
  48. ]);
  49. expect(
  50. filterWorkspaceFileSearchCandidates(candidates, "").map(
  51. (candidate) => candidate.relativePath,
  52. ),
  53. ).toEqual(["README.md", "package.json", "src", "docs"]);
  54. });
  55. it("keeps the best limited matches without sorting every candidate", () => {
  56. const candidates = mapWorkspaceFileEntriesToSearchCandidates([
  57. entry("docs/unrelated-0.md"),
  58. entry("src/target-service.ts"),
  59. entry("src/target-controller.ts"),
  60. entry("docs/unrelated-1.md"),
  61. ]);
  62. expect(
  63. filterWorkspaceFileSearchCandidates(candidates, "target", { limit: 2 }).map(
  64. (candidate) => candidate.relativePath,
  65. ),
  66. ).toEqual(["src/target-service.ts", "src/target-controller.ts"]);
  67. });
  68. it("evicts the worst scored matches when matches exceed the limit", () => {
  69. // 覆盖"命中数超过 limit 需要淘汰已插入元素":前缀匹配(低分)必须把
  70. // 先插入的子串匹配(高分)挤出 top-K,且结果保持 score 升序稳定。
  71. const candidates = mapWorkspaceFileEntriesToSearchCandidates([
  72. entry("docs/xtarget-old.md"),
  73. entry("docs/my-target-notes.md"),
  74. entry("src/target-best.ts"),
  75. entry("src/target-mid.ts"),
  76. entry("src/target.ts"),
  77. ]);
  78. expect(
  79. filterWorkspaceFileSearchCandidates(candidates, "target", { limit: 2 }).map(
  80. (candidate) => candidate.relativePath,
  81. ),
  82. ).toEqual(["src/target.ts", "src/target-mid.ts"]);
  83. });
  84. it("matches full sort ordering at scale without relying on insertion scan", () => {
  85. // 大规模候选等价性:top-K 维护必须与"全量排序后截断"的参考实现产出完全一致。
  86. // 用确定性伪随机生成大量含公共子串的候选,确保命中数远超 limit、充分覆盖淘汰路径。
  87. const entries: WorkspaceFileEntry[] = [];
  88. let seed = 0x2f6e2b1;
  89. const nextSeed = () => {
  90. seed = (Math.imul(seed, 48271) + 11) >>> 0;
  91. return seed;
  92. };
  93. for (let i = 0; i < 20000; i += 1) {
  94. const bucket = nextSeed() % 40;
  95. const stamp = nextSeed().toString(36);
  96. entries.push(
  97. entry(`pkg-${bucket}/mod-${bucket}/target-${stamp}-${i}.ts`, i % 500 === 0 ? "directory" : "file"),
  98. );
  99. }
  100. const candidates = mapWorkspaceFileEntriesToSearchCandidates(entries);
  101. const limit = 50;
  102. // 参考实现:与 compareScoredWorkspaceFileSearchCandidates 相同的
  103. // score 升序 → 原始 index 升序,全量排序后截断(旧语义)。
  104. const reference: Array<{ relativePath: string; score: number; index: number }> = [];
  105. for (const [index, candidate] of candidates.entries()) {
  106. const text = candidate.name.toLowerCase();
  107. const query = "target";
  108. let score: number;
  109. if (text.startsWith(query)) {
  110. score = text.length - query.length;
  111. } else {
  112. const at = text.indexOf(query);
  113. if (at === -1) continue;
  114. score = 100 + at;
  115. }
  116. reference.push({ relativePath: candidate.relativePath, score, index });
  117. }
  118. const referencePaths = reference
  119. .sort((left, right) => left.score - right.score || left.index - right.index)
  120. .slice(0, limit)
  121. .map((item) => item.relativePath);
  122. expect(
  123. filterWorkspaceFileSearchCandidates(candidates, "target", { limit }).map(
  124. (candidate) => candidate.relativePath,
  125. ),
  126. ).toEqual(referencePaths);
  127. });
  128. });