3302-find-path-glob.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  5. import { createFindToolDefinition } from "../../../src/core/tools/find.ts";
  6. /**
  7. * Regression test for https://github.com/earendil-works/pi-mono/issues/3302
  8. *
  9. * The `find` tool advertises glob patterns like `src/**\/*.spec.ts`, but the
  10. * default fd-backed implementation used `fd --glob <pattern>` without
  11. * `--full-path`, which makes fd match only against the basename. Any pattern
  12. * containing a `/` therefore silently returned no matches.
  13. *
  14. * The fix switches fd into full-path mode when the pattern contains a `/`
  15. * and prepends `**\/` so the pattern can match against the absolute candidate
  16. * path that fd feeds to the matcher.
  17. */
  18. describe("issue #3302 find returns no results for path-based glob patterns", () => {
  19. let tempRoot: string;
  20. beforeEach(() => {
  21. tempRoot = mkdtempSync(join(tmpdir(), "pi-3302-"));
  22. mkdirSync(join(tempRoot, "some", "parent", "child"), { recursive: true });
  23. mkdirSync(join(tempRoot, "src", "foo", "bar"), { recursive: true });
  24. writeFileSync(join(tempRoot, "some", "parent", "child", "file.ext"), "");
  25. writeFileSync(join(tempRoot, "some", "parent", "child", "test.spec.ts"), "");
  26. writeFileSync(join(tempRoot, "src", "foo", "bar", "example.spec.ts"), "");
  27. });
  28. afterEach(() => {
  29. rmSync(tempRoot, { recursive: true, force: true });
  30. });
  31. async function runFind(pattern: string): Promise<string[]> {
  32. const def = createFindToolDefinition(tempRoot);
  33. // The find tool implementation does not touch ctx; pass a minimal stub.
  34. const ctx = {} as Parameters<typeof def.execute>[4];
  35. const result = (await def.execute("call-1", { pattern }, undefined, undefined, ctx)) as {
  36. content: Array<{ type: string; text?: string }>;
  37. };
  38. const text = result.content[0]?.text ?? "";
  39. if (text === "No files found matching pattern") return [];
  40. return text
  41. .split("\n")
  42. .map((l) => l.trim())
  43. .filter((l) => l.length > 0 && !l.startsWith("["));
  44. }
  45. it("basename pattern still matches (regression-safe)", async () => {
  46. const files = await runFind("*.spec.ts");
  47. expect(files.sort()).toEqual(["some/parent/child/test.spec.ts", "src/foo/bar/example.spec.ts"]);
  48. });
  49. it("directory-prefixed pattern with ** tail matches subtree", async () => {
  50. const files = await runFind("some/parent/child/**");
  51. // Matches files (and possibly directories) under the subtree. Assert the two files are present.
  52. expect(files).toContain("some/parent/child/file.ext");
  53. expect(files).toContain("some/parent/child/test.spec.ts");
  54. });
  55. it("leading ** wildcard with path segments matches", async () => {
  56. const files = await runFind("**/parent/child/*");
  57. expect(files.sort()).toContain("some/parent/child/file.ext");
  58. expect(files.sort()).toContain("some/parent/child/test.spec.ts");
  59. });
  60. it("src/**/*.spec.ts matches nested spec file", async () => {
  61. const files = await runFind("src/**/*.spec.ts");
  62. expect(files).toEqual(["src/foo/bar/example.spec.ts"]);
  63. });
  64. });