|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { pathMatcher } from "./path-matcher"; |
| 3 | + |
| 4 | +describe("pathMatcher", () => { |
| 5 | + describe("basic matching", () => { |
| 6 | + it("extracts a single param", () => { |
| 7 | + expect(pathMatcher("/[workspace]", "/acme")).toEqual({ workspace: "acme" }); |
| 8 | + }); |
| 9 | + |
| 10 | + it("extracts multiple params", () => { |
| 11 | + expect(pathMatcher("/[workspace]/[project]", "/acme/myproject")).toEqual({ |
| 12 | + workspace: "acme", |
| 13 | + project: "myproject", |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + it("matches literal segments", () => { |
| 18 | + expect(pathMatcher("/app/settings", "/app/settings")).toEqual({}); |
| 19 | + }); |
| 20 | + |
| 21 | + it("returns null on literal mismatch", () => { |
| 22 | + expect(pathMatcher("/app/settings", "/app/profile")).toBeNull(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("returns null on segment count mismatch (too few)", () => { |
| 26 | + expect(pathMatcher("/[a]/[b]/[c]", "/x/y")).toBeNull(); |
| 27 | + }); |
| 28 | + |
| 29 | + it("returns null on segment count mismatch (too many)", () => { |
| 30 | + expect(pathMatcher("/[a]/[b]", "/x/y/z")).toBeNull(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("matches root path against empty pattern", () => { |
| 34 | + expect(pathMatcher("/", "/")).toEqual({}); |
| 35 | + }); |
| 36 | + |
| 37 | + it("handles trailing slashes", () => { |
| 38 | + expect(pathMatcher("/[workspace]/", "/acme/")).toEqual({ workspace: "acme" }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("decodes URL-encoded values", () => { |
| 42 | + expect(pathMatcher("/[name]", "/hello%20world")).toEqual({ name: "hello world" }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("mixes literal and param segments", () => { |
| 46 | + expect(pathMatcher("/app/[workspace]/settings", "/app/acme/settings")).toEqual({ |
| 47 | + workspace: "acme", |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns null when literal segment in the middle mismatches", () => { |
| 52 | + expect(pathMatcher("/app/[workspace]/settings", "/app/acme/profile")).toBeNull(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe("prefix mode", () => { |
| 57 | + it("matches with fewer segments", () => { |
| 58 | + expect(pathMatcher("/[workspace]/[project]/[taskId]", "/acme", { prefix: true })).toEqual({ |
| 59 | + workspace: "acme", |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("matches partial prefix", () => { |
| 64 | + expect( |
| 65 | + pathMatcher("/[workspace]/[project]/[taskId]", "/acme/myproject", { prefix: true }) |
| 66 | + ).toEqual({ |
| 67 | + workspace: "acme", |
| 68 | + project: "myproject", |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it("matches full pattern in prefix mode", () => { |
| 73 | + expect(pathMatcher("/[workspace]/[project]", "/acme/myproject", { prefix: true })).toEqual({ |
| 74 | + workspace: "acme", |
| 75 | + project: "myproject", |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("returns null when path has more segments than pattern", () => { |
| 80 | + expect(pathMatcher("/[workspace]", "/acme/extra/stuff", { prefix: true })).toBeNull(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("returns null for root path against non-empty pattern", () => { |
| 84 | + expect(pathMatcher("/[workspace]", "/", { prefix: true })).toBeNull(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("validates literal segments in prefix mode", () => { |
| 88 | + expect(pathMatcher("/app/[workspace]", "/wrong/acme", { prefix: true })).toBeNull(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("matches literal-only prefix", () => { |
| 92 | + expect(pathMatcher("/app/settings", "/app", { prefix: true })).toEqual({}); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe("not option", () => { |
| 97 | + it("excludes exact path", () => { |
| 98 | + expect(pathMatcher("/[page]", "/auth", { not: "/auth" })).toBeNull(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("excludes path prefix", () => { |
| 102 | + expect(pathMatcher("/[page]/[sub]", "/auth/login", { not: "/auth" })).toBeNull(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("does not do partial string match", () => { |
| 106 | + // "/auth" should NOT exclude "/authentication" |
| 107 | + expect(pathMatcher("/[page]", "/authentication", { not: "/auth" })).toEqual({ |
| 108 | + page: "authentication", |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it("supports array of exclusions", () => { |
| 113 | + expect(pathMatcher("/[page]", "/admin", { not: ["/auth", "/admin"] })).toBeNull(); |
| 114 | + expect(pathMatcher("/[page]", "/auth", { not: ["/auth", "/admin"] })).toBeNull(); |
| 115 | + expect(pathMatcher("/[page]", "/home", { not: ["/auth", "/admin"] })).toEqual({ |
| 116 | + page: "home", |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it("works combined with prefix mode", () => { |
| 121 | + expect( |
| 122 | + pathMatcher("/[workspace]/[project]", "/auth/login", { not: "/auth", prefix: true }) |
| 123 | + ).toBeNull(); |
| 124 | + expect( |
| 125 | + pathMatcher("/[workspace]/[project]", "/acme", { not: "/auth", prefix: true }) |
| 126 | + ).toEqual({ workspace: "acme" }); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments