|
1 | 1 | import path from "path"; |
2 | 2 | import fs from "fs"; |
3 | 3 | import { createNodeFileSystem } from "@/vfs/createNodeFileSystem"; |
| 4 | +import { makeSafeName } from "@/vfs/utils"; |
4 | 5 |
|
5 | 6 | describe("createNodeFileSystem", () => { |
6 | 7 | it("should open file system", () => { |
@@ -46,4 +47,72 @@ describe("createNodeFileSystem", () => { |
46 | 47 | fs.rmSync(realPathDir2, { recursive: true, force: true }); |
47 | 48 | } |
48 | 49 | }); |
| 50 | + |
| 51 | + it("should truncate and hash long filenames", () => { |
| 52 | + const baseDir = path.resolve(__dirname, "./__testdata"); |
| 53 | + const vfs = createNodeFileSystem(baseDir, false); |
| 54 | + |
| 55 | + const longName = "A".repeat(300); |
| 56 | + const content = "Test content"; |
| 57 | + const ext = ".md"; |
| 58 | + |
| 59 | + const inputPath = vfs.resolve(`${longName}${ext}`); |
| 60 | + const dir = path.dirname(inputPath); |
| 61 | + const expectedSafeName = makeSafeName(longName, ext); |
| 62 | + const expectedFullPath = path.join(dir, expectedSafeName); |
| 63 | + |
| 64 | + try { |
| 65 | + if (fs.existsSync(expectedFullPath)) { |
| 66 | + fs.unlinkSync(expectedFullPath); |
| 67 | + } |
| 68 | + |
| 69 | + vfs.writeFile(inputPath, content); |
| 70 | + expect(fs.existsSync(expectedFullPath)).toBe(true); |
| 71 | + |
| 72 | + const actualContent = fs.readFileSync(expectedFullPath, "utf8"); |
| 73 | + expect(actualContent).toBe(content); |
| 74 | + |
| 75 | + expect(expectedSafeName.length).toBeLessThanOrEqual(255); |
| 76 | + expect(expectedSafeName).toMatch( |
| 77 | + new RegExp( |
| 78 | + `^${longName.slice(0, 255 - ext.length - 9)}_[0-9a-f]{8}${ext}$`, |
| 79 | + ), |
| 80 | + ); |
| 81 | + } finally { |
| 82 | + if (fs.existsSync(expectedFullPath)) { |
| 83 | + fs.unlinkSync(expectedFullPath); |
| 84 | + } |
| 85 | + } |
| 86 | + }); |
| 87 | + it("should not truncate or hash short filenames", () => { |
| 88 | + const baseDir = path.resolve(__dirname, "./__testdata"); |
| 89 | + const vfs = createNodeFileSystem(baseDir, false); |
| 90 | + |
| 91 | + const shortName = "short-filename"; |
| 92 | + const content = "Test content"; |
| 93 | + const ext = ".md"; |
| 94 | + |
| 95 | + const inputPath = vfs.resolve(`${shortName}${ext}`); |
| 96 | + const dir = path.dirname(inputPath); |
| 97 | + const expectedSafeName = makeSafeName(shortName, ext); |
| 98 | + const expectedFullPath = path.join(dir, expectedSafeName); |
| 99 | + |
| 100 | + try { |
| 101 | + if (fs.existsSync(expectedFullPath)) { |
| 102 | + fs.unlinkSync(expectedFullPath); |
| 103 | + } |
| 104 | + |
| 105 | + vfs.writeFile(inputPath, content); |
| 106 | + expect(fs.existsSync(expectedFullPath)).toBe(true); |
| 107 | + |
| 108 | + const actualContent = fs.readFileSync(expectedFullPath, "utf8"); |
| 109 | + expect(actualContent).toBe(content); |
| 110 | + |
| 111 | + expect(expectedSafeName).toBe(`${shortName}${ext}`); |
| 112 | + } finally { |
| 113 | + if (fs.existsSync(expectedFullPath)) { |
| 114 | + fs.unlinkSync(expectedFullPath); |
| 115 | + } |
| 116 | + } |
| 117 | + }); |
49 | 118 | }); |
0 commit comments