|
1 | | -import { describe, expect, it } from "vite-plus/test"; |
| 1 | +import { describe, expect, it, vi } from "vite-plus/test"; |
2 | 2 | import { |
| 3 | + buildFileLinkParentSuffixByPath, |
3 | 4 | fileLinkLabel, |
4 | 5 | parseFileUrlHref, |
5 | 6 | splitFilePathPosition, |
6 | 7 | resolvePathLinkTarget, |
7 | 8 | } from "./fileLinks.ts"; |
8 | 9 |
|
| 10 | +describe("buildFileLinkParentSuffixByPath", () => { |
| 11 | + it.each([ |
| 12 | + { |
| 13 | + paths: ["", "/", "index.ts", "/repo/src/index.ts"], |
| 14 | + suffixes: [["/repo/src/index.ts", "repo/src"]], |
| 15 | + }, |
| 16 | + { paths: ["/repo/a.ts", "/repo/b.ts", "/repo/a.ts"], suffixes: [] }, |
| 17 | + { |
| 18 | + paths: ["/repo/src/index.ts", "/repo/test/index.ts"], |
| 19 | + suffixes: [ |
| 20 | + ["/repo/src/index.ts", "repo/src"], |
| 21 | + ["/repo/test/index.ts", "repo/test"], |
| 22 | + ], |
| 23 | + }, |
| 24 | + { |
| 25 | + paths: ["/repo/a/src/index.ts", "/repo/b/src/index.ts"], |
| 26 | + suffixes: [ |
| 27 | + ["/repo/a/src/index.ts", "a/src"], |
| 28 | + ["/repo/b/src/index.ts", "b/src"], |
| 29 | + ], |
| 30 | + }, |
| 31 | + { |
| 32 | + paths: ["/a/pkg/src/index.ts", "/b/pkg/src/index.ts"], |
| 33 | + suffixes: [ |
| 34 | + ["/a/pkg/src/index.ts", "a/pkg/src"], |
| 35 | + ["/b/pkg/src/index.ts", "b/pkg/src"], |
| 36 | + ], |
| 37 | + }, |
| 38 | + { |
| 39 | + paths: ["src/index.ts", "/repo/src/index.ts"], |
| 40 | + suffixes: [ |
| 41 | + ["src/index.ts", "src"], |
| 42 | + ["/repo/src/index.ts", "repo/src"], |
| 43 | + ], |
| 44 | + }, |
| 45 | + { |
| 46 | + paths: ["/repo//src/index.ts", "/repo/src/index.ts"], |
| 47 | + suffixes: [ |
| 48 | + ["/repo//src/index.ts", "repo/src"], |
| 49 | + ["/repo/src/index.ts", "repo/src"], |
| 50 | + ], |
| 51 | + }, |
| 52 | + { paths: [String.raw`C:\repo\src\index.ts`, "C:/repo/src/index.ts"], suffixes: [] }, |
| 53 | + { |
| 54 | + paths: [String.raw`C:\repo\src\index.ts`, String.raw`D:\repo\src\index.ts`], |
| 55 | + suffixes: [ |
| 56 | + ["C:/repo/src/index.ts", "C:/repo/src"], |
| 57 | + ["D:/repo/src/index.ts", "D:/repo/src"], |
| 58 | + ], |
| 59 | + }, |
| 60 | + { |
| 61 | + paths: [String.raw`\\one\share\src\index.ts`, String.raw`\\two\share\src\index.ts`], |
| 62 | + suffixes: [ |
| 63 | + ["//one/share/src/index.ts", "one/share/src"], |
| 64 | + ["//two/share/src/index.ts", "two/share/src"], |
| 65 | + ], |
| 66 | + }, |
| 67 | + ])("preserves suffixes for $paths", ({ paths, suffixes }) => { |
| 68 | + expect([...buildFileLinkParentSuffixByPath(paths)]).toEqual(suffixes); |
| 69 | + }); |
| 70 | + |
| 71 | + it("bounds suffix construction by path count for a same-basename group", () => { |
| 72 | + const paths = Array.from({ length: 128 }, (_, i) => `/repo/packages/pkg-${i}/src/index.ts`); |
| 73 | + using joins = vi.spyOn(Array.prototype, "join"); |
| 74 | + const result = buildFileLinkParentSuffixByPath(paths); |
| 75 | + const suffixConstructions = joins.mock.calls.length; |
| 76 | + |
| 77 | + expect(result).toEqual(new Map(paths.map((path, i) => [path, `pkg-${i}/src`]))); |
| 78 | + // Count work instead of elapsed time so a pairwise scan fails on every machine. |
| 79 | + expect(suffixConstructions).toBeLessThan(paths.length * 20); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
9 | 83 | describe("parseFileUrlHref", () => { |
10 | 84 | it.each([ |
11 | 85 | [ |
|
0 commit comments