workspaceFileSearch.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
  2. import * as fsPromises from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import { createFileService } from "../src/file/fileService.js";
  7. import { unpackWorkspaceFileEntries } from "@zcode/shared/workspaceFileEntriesCodec";
  8. import {
  9. filterWorkspaceFileSearchCandidates,
  10. mapWorkspaceFileEntriesToSearchCandidates,
  11. } from "@zcode/shared/workspaceFileSearch";
  12. vi.mock("node:fs/promises", async (importOriginal) => {
  13. const actual = await importOriginal<typeof fsPromises>();
  14. return { ...actual, readdir: vi.fn(actual.readdir) };
  15. });
  16. const roots: string[] = [];
  17. async function workspace() {
  18. const rootPath = await mkdtemp(join(tmpdir(), "mention-host-search-"));
  19. roots.push(rootPath);
  20. await writeFile(join(rootPath, ".zcodeignore"), "node_modules/\n");
  21. await mkdir(join(rootPath, "src"));
  22. await Promise.all(
  23. ["Main.ts", "main.test.ts", "README.md", "中文.txt"].map((name) =>
  24. writeFile(join(rootPath, "src", name), "fixture"),
  25. ),
  26. );
  27. return rootPath;
  28. }
  29. afterEach(async () => {
  30. vi.restoreAllMocks();
  31. await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
  32. });
  33. describe("Host 文件候选查询", () => {
  34. it("显式刷新完成后旧扫描不能覆盖新缓存", async () => {
  35. const rootPath = await workspace();
  36. const service = createFileService();
  37. const { readdir } = await vi.importActual<typeof fsPromises>("node:fs/promises");
  38. let release!: () => void;
  39. let entered!: () => void;
  40. const paused = new Promise<void>((resolve) => {
  41. release = resolve;
  42. });
  43. const started = new Promise<void>((resolve) => {
  44. entered = resolve;
  45. });
  46. vi.mocked(fsPromises.readdir).mockImplementationOnce(async (...args) => {
  47. const entries = await readdir(...args);
  48. entered();
  49. await paused;
  50. return entries;
  51. });
  52. const old = service.searchWorkspaceFiles({ rootPath, query: "race-987654" });
  53. await started;
  54. try {
  55. await writeFile(join(rootPath, "race-987654.ts"), "new");
  56. expect(
  57. await service.searchWorkspaceFiles({ rootPath, query: "race-987654", refresh: true }),
  58. ).toHaveLength(1);
  59. } finally {
  60. release();
  61. }
  62. expect(await old).toEqual([]);
  63. expect(await service.searchWorkspaceFiles({ rootPath, query: "race-987654" })).toHaveLength(1);
  64. });
  65. it("缓存按 LRU 有界,过期索引重新扫描", async () => {
  66. const rootPath = await workspace();
  67. const service = createFileService();
  68. for (const identity of ["a", "b", "c", "d", "e"]) {
  69. await service.searchWorkspaceFiles({ rootPath, workspaceIdentity: identity, query: "" });
  70. }
  71. await writeFile(join(rootPath, "lru-987654.ts"), "new");
  72. expect(
  73. await service.searchWorkspaceFiles({ rootPath, workspaceIdentity: "a", query: "lru-987654" }),
  74. ).toHaveLength(1);
  75. expect(
  76. await service.searchWorkspaceFiles({ rootPath, workspaceIdentity: "e", query: "lru-987654" }),
  77. ).toEqual([]);
  78. vi.spyOn(Date, "now").mockReturnValue(Date.now() + 60_001);
  79. expect(
  80. await service.searchWorkspaceFiles({ rootPath, workspaceIdentity: "e", query: "lru-987654" }),
  81. ).toHaveLength(1);
  82. });
  83. it("调用方过大的 limit 仍最多返回 1000 条", async () => {
  84. const rootPath = await workspace();
  85. await Promise.all(
  86. Array.from({ length: 1005 }, (_, index) => writeFile(join(rootPath, `cap-${index}.ts`), "")),
  87. );
  88. expect(
  89. await createFileService().searchWorkspaceFiles({ rootPath, query: "", limit: 100_000 }),
  90. ).toHaveLength(1000);
  91. });
  92. it("复用原索引规则和 fuzzy 排序,只返回限定候选", async () => {
  93. const rootPath = await workspace();
  94. const service = createFileService();
  95. const length = await service.listWorkspaceFilesLength({ rootPath });
  96. const packed = await service.listWorkspaceFilesRange({ rootPath, offset: 0, length });
  97. const candidates = mapWorkspaceFileEntriesToSearchCandidates(
  98. unpackWorkspaceFileEntries(packed, rootPath),
  99. );
  100. for (const query of ["", "main", "MAIN", "src/", "中文", "not-present"]) {
  101. const actual = await service.searchWorkspaceFiles({ rootPath, query, limit: 2 });
  102. expect(actual.map((entry) => entry.relativePath)).toEqual(
  103. filterWorkspaceFileSearchCandidates(candidates, query, { limit: 2 }).map(
  104. (entry) => entry.relativePath,
  105. ),
  106. );
  107. expect(actual.length).toBeLessThanOrEqual(2);
  108. expect(actual.every((entry) => Object.keys(entry).length === 4)).toBe(true);
  109. }
  110. });
  111. it("普通查询复用索引,显式补扫可以发现刚创建的文件", async () => {
  112. const rootPath = await workspace();
  113. const service = createFileService();
  114. expect(await service.searchWorkspaceFiles({ rootPath, query: "new-987654321" })).toEqual([]);
  115. await writeFile(join(rootPath, "new-987654321.ts"), "new");
  116. expect(await service.searchWorkspaceFiles({ rootPath, query: "new-987654321" })).toEqual([]);
  117. expect(
  118. await service.searchWorkspaceFiles({ rootPath, query: "new-987654321", refresh: true }),
  119. ).toEqual([
  120. {
  121. name: "new-987654321.ts",
  122. relativePath: "new-987654321.ts",
  123. path: join(rootPath, "new-987654321.ts"),
  124. type: "file",
  125. },
  126. ]);
  127. });
  128. it("不同 identity / service 的同路径查询不复用旧索引", async () => {
  129. const rootPath = await workspace();
  130. const service = createFileService();
  131. await service.searchWorkspaceFiles({ rootPath, workspaceIdentity: "host-a", query: "" });
  132. await writeFile(join(rootPath, "new-identity.ts"), "new");
  133. expect(
  134. await service.searchWorkspaceFiles({
  135. rootPath,
  136. workspaceIdentity: "host-a",
  137. query: "new-identity",
  138. }),
  139. ).toEqual([]);
  140. expect(
  141. await service.searchWorkspaceFiles({
  142. rootPath,
  143. workspaceIdentity: "host-b",
  144. query: "new-identity",
  145. }),
  146. ).toHaveLength(1);
  147. expect(
  148. await createFileService().searchWorkspaceFiles({
  149. rootPath,
  150. workspaceIdentity: "host-a",
  151. query: "new-identity",
  152. }),
  153. ).toHaveLength(1);
  154. });
  155. it("修改忽略规则后下一次查询立即生效", async () => {
  156. const rootPath = await workspace();
  157. const service = createFileService();
  158. expect(await service.searchWorkspaceFiles({ rootPath, query: "Main.ts" })).toHaveLength(2);
  159. await service.writeWorkspaceFileSearchIgnore({ rootPath, content: "src/\n" });
  160. expect(await service.searchWorkspaceFiles({ rootPath, query: "Main.ts" })).toEqual([]);
  161. });
  162. it("零上限返回空,非法上限被拒绝,缺失目录保持空列表语义", async () => {
  163. const rootPath = await workspace();
  164. const service = createFileService();
  165. expect(await service.searchWorkspaceFiles({ rootPath, query: "", limit: 0 })).toEqual([]);
  166. await expect(
  167. service.searchWorkspaceFiles({ rootPath, query: "", limit: Number.NaN }),
  168. ).rejects.toThrow();
  169. expect(
  170. await service.searchWorkspaceFiles({ rootPath: join(rootPath, "missing"), query: "" }),
  171. ).toEqual([]);
  172. });
  173. });