path-utils.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { mkdtempSync, readdirSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join, resolve } from "node:path";
  4. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  5. import { expandPath, resolveReadPath, resolveToCwd } from "../src/core/tools/path-utils.ts";
  6. describe("path-utils", () => {
  7. describe("expandPath", () => {
  8. it("should expand ~ to home directory", () => {
  9. const result = expandPath("~");
  10. expect(result).not.toContain("~");
  11. });
  12. it("should expand ~/path to home directory", () => {
  13. const result = expandPath("~/Documents/file.txt");
  14. expect(result).not.toContain("~/");
  15. });
  16. it("should keep tilde-prefixed filenames literal", () => {
  17. expect(expandPath("~draft.md")).toBe("~draft.md");
  18. expect(expandPath("@~draft.md")).toBe("~draft.md");
  19. });
  20. it("should normalize Unicode spaces", () => {
  21. // Non-breaking space (U+00A0) should become regular space
  22. const withNBSP = "file\u00A0name.txt";
  23. const result = expandPath(withNBSP);
  24. expect(result).toBe("file name.txt");
  25. });
  26. });
  27. describe("resolveToCwd", () => {
  28. it("should resolve absolute paths as-is", () => {
  29. const absolutePath = resolve(tmpdir(), "absolute", "path", "file.txt");
  30. const result = resolveToCwd(absolutePath, resolve(tmpdir(), "some", "cwd"));
  31. expect(result).toBe(absolutePath);
  32. });
  33. it("should resolve relative paths against cwd", () => {
  34. const result = resolveToCwd("relative/file.txt", "/some/cwd");
  35. expect(result).toBe(resolve("/some/cwd", "relative/file.txt"));
  36. });
  37. it("should resolve tilde-prefixed filenames against cwd", () => {
  38. const cwd = join(tmpdir(), "pi-path-utils-cwd");
  39. expect(resolveToCwd("~draft.md", cwd)).toBe(resolve(cwd, "~draft.md"));
  40. expect(resolveToCwd("@~draft.md", cwd)).toBe(resolve(cwd, "~draft.md"));
  41. });
  42. });
  43. describe("resolveReadPath", () => {
  44. let tempDir: string;
  45. beforeEach(() => {
  46. tempDir = mkdtempSync(join(tmpdir(), "path-utils-test-"));
  47. });
  48. afterEach(() => {
  49. // Clean up temp files and directory
  50. try {
  51. const files = readdirSync(tempDir);
  52. for (const file of files) {
  53. unlinkSync(join(tempDir, file));
  54. }
  55. rmdirSync(tempDir);
  56. } catch {
  57. // Ignore cleanup errors
  58. }
  59. });
  60. it("should resolve existing file path", () => {
  61. const fileName = "test-file.txt";
  62. writeFileSync(join(tempDir, fileName), "content");
  63. const result = resolveReadPath(fileName, tempDir);
  64. expect(result).toBe(join(tempDir, fileName));
  65. });
  66. it("should handle NFC vs NFD Unicode normalization (macOS filenames with accents)", () => {
  67. // macOS stores filenames in NFD (decomposed) form:
  68. // é = e + combining acute accent (U+0301)
  69. // Users typically type in NFC (composed) form:
  70. // é = single character (U+00E9)
  71. //
  72. // Note: macOS APFS normalizes Unicode automatically, so both paths work.
  73. // This test verifies the NFD variant fallback works on systems that don't.
  74. // NFD: e (U+0065) + combining acute accent (U+0301)
  75. const nfdFileName = "file\u0065\u0301.txt";
  76. // NFC: é as single character (U+00E9)
  77. const nfcFileName = "file\u00e9.txt";
  78. // Verify they have different byte sequences
  79. expect(nfdFileName).not.toBe(nfcFileName);
  80. expect(Buffer.from(nfdFileName)).not.toEqual(Buffer.from(nfcFileName));
  81. // Create file with NFD name
  82. writeFileSync(join(tempDir, nfdFileName), "content");
  83. // User provides NFC path - should find the file (via filesystem normalization or our fallback)
  84. const result = resolveReadPath(nfcFileName, tempDir);
  85. // Result should contain the accented character (either NFC or NFD form)
  86. expect(result).toContain(tempDir);
  87. expect(result).toMatch(/file.+\.txt$/);
  88. });
  89. it("should handle curly quotes vs straight quotes (macOS filenames)", () => {
  90. // macOS uses curly apostrophe (U+2019) in screenshot filenames:
  91. // Capture d'écran (U+2019)
  92. // Users typically type straight apostrophe (U+0027):
  93. // Capture d'ecran (U+0027)
  94. const curlyQuoteName = "Capture d\u2019cran.txt"; // U+2019 right single quotation mark
  95. const straightQuoteName = "Capture d'cran.txt"; // U+0027 apostrophe
  96. // Verify they are different
  97. expect(curlyQuoteName).not.toBe(straightQuoteName);
  98. // Create file with curly quote name (simulating macOS behavior)
  99. writeFileSync(join(tempDir, curlyQuoteName), "content");
  100. // User provides straight quote path - should find the curly quote file
  101. const result = resolveReadPath(straightQuoteName, tempDir);
  102. expect(result).toBe(join(tempDir, curlyQuoteName));
  103. });
  104. it("should handle combined NFC + curly quote (French macOS screenshots)", () => {
  105. // Full macOS screenshot filename: "Capture d'écran" with NFD é and curly quote
  106. // Note: macOS APFS normalizes NFD to NFC, so the actual file on disk uses NFC
  107. const nfcCurlyName = "Capture d\u2019\u00e9cran.txt"; // NFC + curly quote (how APFS stores it)
  108. const nfcStraightName = "Capture d'\u00e9cran.txt"; // NFC + straight quote (user input)
  109. // Verify they are different
  110. expect(nfcCurlyName).not.toBe(nfcStraightName);
  111. // Create file with macOS-style name (curly quote)
  112. writeFileSync(join(tempDir, nfcCurlyName), "content");
  113. // User provides straight quote path - should find the curly quote file
  114. const result = resolveReadPath(nfcStraightName, tempDir);
  115. expect(result).toBe(join(tempDir, nfcCurlyName));
  116. });
  117. it("should handle macOS screenshot AM/PM variant with narrow no-break space", () => {
  118. // macOS uses narrow no-break space (U+202F) before AM/PM in screenshot names
  119. const macosName = "Screenshot 2024-01-01 at 10.00.00\u202FAM.png"; // U+202F
  120. const userName = "Screenshot 2024-01-01 at 10.00.00 AM.png"; // regular space
  121. // Create file with macOS-style name
  122. writeFileSync(join(tempDir, macosName), "content");
  123. // User provides regular space path
  124. const result = resolveReadPath(userName, tempDir);
  125. // This works because tryMacOSScreenshotPath() handles this case
  126. expect(result).toBe(join(tempDir, macosName));
  127. });
  128. it("should handle macOS screenshot lowercase am/pm variant (en_AU locale)", () => {
  129. // Some locales like en_AU use lowercase am/pm in screenshot names
  130. const macosName = "Screenshot 2024-01-01 at 10.00.00\u202Fam.png"; // U+202F + lowercase
  131. const userName = "Screenshot 2024-01-01 at 10.00.00 am.png"; // regular space + lowercase
  132. // Create file with macOS-style name
  133. writeFileSync(join(tempDir, macosName), "content");
  134. // User provides regular space path
  135. const result = resolveReadPath(userName, tempDir);
  136. // This works because tryMacOSScreenshotPath() uses case-insensitive matching
  137. expect(result).toBe(join(tempDir, macosName));
  138. });
  139. });
  140. });