|
| 1 | +import { equal } from "node:assert/strict"; |
| 2 | +import { describe, it } from "node:test"; |
| 3 | +import { shortenPath } from "../src/paths.ts"; |
| 4 | + |
| 5 | +describe("shortenPath", () => { |
| 6 | + it("shortens a long unix path to 3 segments", () => { |
| 7 | + equal( |
| 8 | + shortenPath( |
| 9 | + "/Users/someuser/dev/rnx-kit/packages/metro-resolver-symlinks/src/metro-resolver.ts" |
| 10 | + ), |
| 11 | + ".../metro-resolver-symlinks/src/metro-resolver.ts" |
| 12 | + ); |
| 13 | + }); |
| 14 | + |
| 15 | + it("keeps 4 segments when the boundary falls on a src dir", () => { |
| 16 | + equal( |
| 17 | + shortenPath( |
| 18 | + "/Users/someuser/dev/rnx-kit/packages/my-package/src/utils/helpers.ts" |
| 19 | + ), |
| 20 | + ".../my-package/src/utils/helpers.ts" |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it("detects lib as a src dir", () => { |
| 25 | + equal( |
| 26 | + shortenPath("/a/b/c/d/lib/utils/index.ts"), |
| 27 | + ".../d/lib/utils/index.ts" |
| 28 | + ); |
| 29 | + }); |
| 30 | + |
| 31 | + it("detects dist as a src dir", () => { |
| 32 | + equal( |
| 33 | + shortenPath("/a/b/c/d/dist/utils/index.js"), |
| 34 | + ".../d/dist/utils/index.js" |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it("detects bin as a src dir", () => { |
| 39 | + equal(shortenPath("/a/b/c/d/bin/utils/cli.js"), ".../d/bin/utils/cli.js"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("returns path unchanged when it has fewer segments than requested", () => { |
| 43 | + equal(shortenPath("foo/bar.ts"), "foo/bar.ts"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("does not shorten when result would be longer than original", () => { |
| 47 | + // /a/b/c.ts is short enough that prepending "..." would not save space |
| 48 | + equal(shortenPath("/a/b/c.ts"), "/a/b/c.ts"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns path unchanged when separators are fewer than segments", () => { |
| 52 | + equal(shortenPath("a/b/c.ts"), "a/b/c.ts"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("returns empty string for empty input", () => { |
| 56 | + equal(shortenPath(""), ""); |
| 57 | + }); |
| 58 | + |
| 59 | + it("returns bare filename unchanged", () => { |
| 60 | + equal(shortenPath("file.ts"), "file.ts"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("handles windows-style backslash separators", () => { |
| 64 | + equal( |
| 65 | + shortenPath("C:\\Users\\me\\dev\\packages\\my-pkg\\src\\index.ts"), |
| 66 | + "...\\my-pkg\\src\\index.ts" |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it("respects custom segment count", () => { |
| 71 | + equal(shortenPath("/a/b/c/d/e.ts", 2), ".../d/e.ts"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("custom segments=1 returns just the filename with ellipsis", () => { |
| 75 | + equal(shortenPath("/a/b/c/d/e.ts", 1), ".../e.ts"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("src dir check still applies with custom segment count", () => { |
| 79 | + equal(shortenPath("/a/b/c/src/e.ts", 2), ".../c/src/e.ts"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("does not trigger src dir check for non-boundary segments", () => { |
| 83 | + equal(shortenPath("/a/b/c/d/src/file.ts"), ".../d/src/file.ts"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("handles trailing separator", () => { |
| 87 | + // trailing / creates an empty segment, so 3 segments = empty + "e" + "d" |
| 88 | + equal(shortenPath("/a/b/c/d/e/"), ".../d/e/"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("src dir check only extends by one extra segment", () => { |
| 92 | + // Even if the 4th segment is also a src dir, we only get one extra |
| 93 | + equal( |
| 94 | + shortenPath("/longer/lib/src/utils/helpers.ts"), |
| 95 | + ".../lib/src/utils/helpers.ts" |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it("does not shorten when src dir extension would exceed original length", () => { |
| 100 | + // /a/lib/src/utils/helpers.ts — the src dir extension would place |
| 101 | + // the cut at index 2, inside the ellipsis guard, so it returns unchanged |
| 102 | + equal( |
| 103 | + shortenPath("/a/lib/src/utils/helpers.ts"), |
| 104 | + "/a/lib/src/utils/helpers.ts" |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
0 commit comments