hostFileSearchCandidates.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, expect, it } from "vitest";
  2. import type { WorkspaceFileEntry } from "@zcode/shared";
  3. import { packWorkspaceFileEntries } from "@zcode/shared/workspaceFileEntriesCodec";
  4. import {
  5. filterWorkspaceFileSearchCandidates,
  6. mapWorkspaceFileEntriesToSearchCandidates,
  7. } from "@zcode/shared/workspaceFileSearch";
  8. import {
  9. buildHostFileSearchCandidates,
  10. searchHostFileCandidates,
  11. } from "../src/file/workspaceFileSearch.js";
  12. describe("Host 分批文件搜索", () => {
  13. it("跨批次保持原 fuzzy 排序和默认顺序,并让出 Host 事件循环", async () => {
  14. const entries: WorkspaceFileEntry[] = Array.from({ length: 9000 }, (_, index) => {
  15. const name = index === 3000 ? "tab\tnewline\n中文.ts" : `file-${index % 31}-${index}.ts`;
  16. const relativePath = `src/${index % 7}/${name}`;
  17. return {
  18. name,
  19. relativePath,
  20. path: `/repo/${relativePath}`,
  21. type: index % 8 ? "file" : "directory",
  22. };
  23. });
  24. const candidates = await buildHostFileSearchCandidates(
  25. packWorkspaceFileEntries(entries),
  26. "/repo",
  27. );
  28. expect(candidates).toEqual(mapWorkspaceFileEntriesToSearchCandidates(entries));
  29. for (const query of ["", "file", "f-2", "src/5", "中文", "no-match-999999"]) {
  30. for (const limit of [1, 10, 1000]) {
  31. expect(
  32. (await searchHostFileCandidates(candidates, query, limit)).map(
  33. (entry) => entry.relativePath,
  34. ),
  35. ).toEqual(
  36. filterWorkspaceFileSearchCandidates(candidates, query, { limit }).map(
  37. (entry) => entry.relativePath,
  38. ),
  39. );
  40. }
  41. }
  42. let yielded = false;
  43. setImmediate(() => {
  44. yielded = true;
  45. });
  46. await searchHostFileCandidates(candidates, "file", 1000);
  47. expect(yielded).toBe(true);
  48. });
  49. });