paths.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
  2. import { homedir, tmpdir } from "node:os";
  3. import { join, resolve } from "node:path";
  4. import { pathToFileURL } from "node:url";
  5. import { afterEach, describe, expect, it } from "vitest";
  6. import { canonicalizePath, getCwdRelativePath, isLocalPath, normalizePath, resolvePath } from "../src/utils/paths.ts";
  7. let tempDir: string;
  8. afterEach(() => {
  9. if (tempDir) {
  10. rmSync(tempDir, { recursive: true, force: true });
  11. tempDir = "";
  12. }
  13. });
  14. function createTempDir(): string {
  15. tempDir = mkdtempSync(join(tmpdir(), "pi-paths-"));
  16. return tempDir;
  17. }
  18. describe("canonicalizePath", () => {
  19. it("returns the real path for a regular file", () => {
  20. const dir = createTempDir();
  21. const file = join(dir, "file.txt");
  22. writeFileSync(file, "hello");
  23. expect(canonicalizePath(file)).toBe(realpathSync(file));
  24. });
  25. it("resolves symlinks to their targets", () => {
  26. const dir = createTempDir();
  27. const target = join(dir, "target.txt");
  28. const link = join(dir, "link.txt");
  29. writeFileSync(target, "hello");
  30. symlinkSync(target, link);
  31. expect(canonicalizePath(link)).toBe(realpathSync(target));
  32. });
  33. it("resolves directory symlinks", () => {
  34. const dir = createTempDir();
  35. const targetDir = join(dir, "target-dir");
  36. const linkDir = join(dir, "link-dir");
  37. mkdirSync(targetDir);
  38. symlinkSync(targetDir, linkDir, "dir");
  39. expect(canonicalizePath(linkDir)).toBe(realpathSync(targetDir));
  40. });
  41. it("falls back to the raw path when the target does not exist", () => {
  42. const dir = createTempDir();
  43. const nonexistent = join(dir, "no-such-file");
  44. expect(canonicalizePath(nonexistent)).toBe(nonexistent);
  45. });
  46. it("falls back to the raw path for a dangling symlink", () => {
  47. const dir = createTempDir();
  48. const target = join(dir, "target.txt");
  49. const link = join(dir, "link.txt");
  50. // Create a symlink whose target does not exist.
  51. symlinkSync(target, link);
  52. // realpathSync would throw, so canonicalizePath returns the link path.
  53. expect(canonicalizePath(link)).toBe(link);
  54. });
  55. });
  56. describe("getCwdRelativePath", () => {
  57. it("keeps cwd-relative names that start with dots", () => {
  58. const cwd = join(tmpdir(), "pi-paths-cwd");
  59. expect(getCwdRelativePath(join(cwd, "..config", "AGENTS.md"), cwd)).toBe(join("..config", "AGENTS.md"));
  60. });
  61. it("rejects parent-directory traversals", () => {
  62. const cwd = join(tmpdir(), "pi-paths-cwd");
  63. expect(getCwdRelativePath(join(cwd, "..", "AGENTS.md"), cwd)).toBeUndefined();
  64. });
  65. });
  66. describe("resolvePath", () => {
  67. it("expands only home tilde shortcuts", () => {
  68. const cwd = join(tmpdir(), "pi-paths-cwd");
  69. expect(normalizePath("~")).toBe(homedir());
  70. expect(normalizePath("~/file.txt")).toBe(join(homedir(), "file.txt"));
  71. expect(resolvePath("~draft.md", cwd)).toBe(resolve(cwd, "~draft.md"));
  72. expect(normalizePath("~draft.md")).toBe("~draft.md");
  73. });
  74. it("resolves relative paths against the base directory", () => {
  75. const cwd = join(tmpdir(), "pi-paths-cwd");
  76. expect(resolvePath("subdir/file.txt", cwd)).toBe(resolve(cwd, "subdir/file.txt"));
  77. expect(resolvePath("subdir/file.txt", pathToFileURL(cwd).href)).toBe(resolve(cwd, "subdir/file.txt"));
  78. });
  79. it("accepts file URLs", () => {
  80. const dir = createTempDir();
  81. const filePath = join(dir, "file with spaces.txt");
  82. expect(resolvePath(pathToFileURL(filePath).href, join(dir, "base"))).toBe(resolve(filePath));
  83. });
  84. it("throws for invalid file URLs", () => {
  85. expect(() => resolvePath("file:///%E0%A4%A")).toThrow();
  86. });
  87. it("preserves POSIX absolute paths with literal percent sequences", () => {
  88. if (process.platform === "win32") {
  89. return;
  90. }
  91. const dir = createTempDir();
  92. for (const filePath of [join(dir, "report%2026.md"), join(dir, "foo%2Fbar"), join(dir, "malformed%A.md")]) {
  93. expect(resolvePath(filePath, join(dir, "base"))).toBe(resolve(filePath));
  94. }
  95. });
  96. it("does not treat Windows file URL pathname strings as native paths", () => {
  97. if (process.platform !== "win32") {
  98. return;
  99. }
  100. const dir = createTempDir();
  101. const filePath = join(dir, "dir", "SKILL.md");
  102. const pathname = pathToFileURL(filePath).pathname;
  103. expect(pathname).toMatch(/^\/[A-Za-z]:/);
  104. expect(resolvePath(pathname, "E:\\project")).toBe(resolve(pathname));
  105. });
  106. });
  107. describe("isLocalPath", () => {
  108. it("returns true for bare names", () => {
  109. expect(isLocalPath("my-package")).toBe(true);
  110. });
  111. it("returns true for relative paths", () => {
  112. expect(isLocalPath("./foo")).toBe(true);
  113. });
  114. it("returns true for file URLs", () => {
  115. expect(isLocalPath("file:///tmp/foo")).toBe(true);
  116. });
  117. it("returns false for npm: protocol", () => {
  118. expect(isLocalPath("npm:package")).toBe(false);
  119. });
  120. it("returns false for git: protocol", () => {
  121. expect(isLocalPath("git://repo")).toBe(false);
  122. });
  123. it("returns false for https: protocol", () => {
  124. expect(isLocalPath("https://example.com")).toBe(false);
  125. });
  126. });