|
| 1 | +import { describe, it } from "node:test"; |
| 2 | +import assert from "node:assert"; |
| 3 | +import { PathRewriter } from "./PathRewriter.js"; |
| 4 | + |
| 5 | +describe("PathRewriter", () => { |
| 6 | + it("should be disabled by default", () => { |
| 7 | + const rewriter = new PathRewriter(); |
| 8 | + assert.strictEqual(rewriter.isEnabled(), false); |
| 9 | + assert.strictEqual(rewriter.rewritePath("/some/path.js"), "/some/path.js"); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should parse path mapping correctly", () => { |
| 13 | + const rewriter = new PathRewriter("/app/src:/workspace/src"); |
| 14 | + assert.strictEqual(rewriter.isEnabled(), true); |
| 15 | + |
| 16 | + const mapping = rewriter.getMapping(); |
| 17 | + assert.strictEqual(mapping.from, "/app/src"); |
| 18 | + assert.strictEqual(mapping.to, "/workspace/src"); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should rewrite paths correctly", () => { |
| 22 | + const rewriter = new PathRewriter("/app/src:/workspace/src"); |
| 23 | + |
| 24 | + assert.strictEqual( |
| 25 | + rewriter.rewritePath("/app/src/utils/helper.js"), |
| 26 | + "/workspace/src/utils/helper.js", |
| 27 | + ); |
| 28 | + |
| 29 | + assert.strictEqual( |
| 30 | + rewriter.rewritePath("/app/src/index.js"), |
| 31 | + "/workspace/src/index.js", |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should handle paths without leading slash in fromPath", () => { |
| 36 | + const rewriter = new PathRewriter("/app/src:/workspace/src"); |
| 37 | + |
| 38 | + assert.strictEqual( |
| 39 | + rewriter.rewritePath("app/src/utils/helper.js"), |
| 40 | + "/workspace/src/utils/helper.js", |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should not rewrite paths that don't match", () => { |
| 45 | + const rewriter = new PathRewriter("/app/src:/workspace/src"); |
| 46 | + |
| 47 | + assert.strictEqual( |
| 48 | + rewriter.rewritePath("/other/path/file.js"), |
| 49 | + "/other/path/file.js", |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should handle empty or null paths", () => { |
| 54 | + const rewriter = new PathRewriter("/app/src:/workspace/src"); |
| 55 | + |
| 56 | + assert.strictEqual(rewriter.rewritePath(""), ""); |
| 57 | + assert.strictEqual(rewriter.rewritePath(null), null); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should normalize trailing slashes", () => { |
| 61 | + const rewriter = new PathRewriter("/app/src/:/workspace/src/"); |
| 62 | + |
| 63 | + const mapping = rewriter.getMapping(); |
| 64 | + assert.strictEqual(mapping.from, "/app/src"); |
| 65 | + assert.strictEqual(mapping.to, "/workspace/src"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should throw error on invalid format", () => { |
| 69 | + assert.throws(() => { |
| 70 | + new PathRewriter("invalid-mapping"); |
| 71 | + }, /Invalid path-mapping format/); |
| 72 | + |
| 73 | + assert.throws(() => { |
| 74 | + new PathRewriter("/from/path"); |
| 75 | + }, /Invalid path-mapping format/); |
| 76 | + |
| 77 | + assert.throws(() => { |
| 78 | + new PathRewriter(":/to/path"); |
| 79 | + }, /Paths cannot be empty/); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should handle complex paths", () => { |
| 83 | + const rewriter = new PathRewriter( |
| 84 | + "/var/lib/docker/overlay2/hash/merged/app:./src", |
| 85 | + ); |
| 86 | + |
| 87 | + assert.strictEqual( |
| 88 | + rewriter.rewritePath( |
| 89 | + "/var/lib/docker/overlay2/hash/merged/app/utils/helper.js", |
| 90 | + ), |
| 91 | + "./src/utils/helper.js", |
| 92 | + ); |
| 93 | + }); |
| 94 | +}); |
0 commit comments