104 lines
4.7 KiB
TypeScript
104 lines
4.7 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
|
|
import CanonicalUrlUtils, { adminPath, adminUrl, apiPath, apiUrl, appPath, appUrl } from "./index.js";
|
|
|
|
describe("canonical URL package contract", () => {
|
|
test("builds host-aware app, admin, and API paths by default", () => {
|
|
expect(appPath({ tenantSlug: "acme", path: "dashboard" })).toBe("/acme/dashboard");
|
|
expect(adminPath("accounts")).toBe("/accounts");
|
|
expect(apiPath("users")).toBe("/v1/users");
|
|
});
|
|
|
|
test("builds host-aware app, admin, and API paths", () => {
|
|
expect(appPath({ tenantSlug: "acme", path: "dashboard", mode: "host-aware" })).toBe("/acme/dashboard");
|
|
expect(adminPath("accounts", { mode: "host-aware" })).toBe("/accounts");
|
|
expect(apiPath("users", { mode: "host-aware" })).toBe("/v1/users");
|
|
});
|
|
|
|
test("normalizes slashes and preserves query strings", () => {
|
|
expect(appPath({ tenantSlug: "acme", path: "//settings///users/?tab=billing" })).toBe("/acme/settings/users?tab=billing");
|
|
expect(appPath({ tenantSlug: "acme" })).toBe("/acme");
|
|
expect(appPath({ tenantSlug: "acme", path: "?tab=billing" })).toBe("/acme?tab=billing");
|
|
expect(adminPath()).toBe("/");
|
|
expect(adminPath("/")).toBe("/");
|
|
expect(adminPath({ path: "///settings//profile///" })).toBe("/settings/profile");
|
|
expect(apiPath({ path: "/webhooks//inbound#email", mode: "host-aware" })).toBe("/v1/webhooks/inbound#email");
|
|
expect(apiPath("/v1/health")).toBe("/v1/health");
|
|
expect(apiPath("/api/v1/health")).toBe("/v1/health");
|
|
expect(apiPath("/api/v1?active=true")).toBe("/v1?active=true");
|
|
expect(apiPath("/v1#docs")).toBe("/v1#docs");
|
|
expect(apiPath()).toBe("/v1");
|
|
});
|
|
|
|
test("encodes tenant slugs as one path segment", () => {
|
|
expect(appPath({ tenantSlug: " Acme Team/West ", path: "settings" })).toBe("/Acme%20Team%2FWest/settings");
|
|
expect(() => appPath({ tenantSlug: " ", path: "settings" })).toThrow("tenantSlug is required");
|
|
});
|
|
|
|
test("builds host-aware absolute URLs by default on the provided domain", () => {
|
|
expect(appUrl({ rootDomain: "example.com", tenantSlug: "acme", path: "dashboard" })).toBe("https://example.com/acme/dashboard");
|
|
expect(adminUrl({ rootDomain: "https://example.com", path: "/accounts/" })).toBe("https://example.com/accounts");
|
|
expect(apiUrl({ rootDomain: "example.com", protocol: "http", path: "users" })).toBe("http://example.com/v1/users");
|
|
});
|
|
|
|
test("builds host-aware absolute URLs with subdomain labels", () => {
|
|
expect(appUrl({ rootDomain: "example.com", appSubdomain: "app", tenantSlug: "acme", path: "dashboard", mode: "host-aware" })).toBe(
|
|
"https://app.example.com/acme/dashboard"
|
|
);
|
|
expect(adminUrl({ rootDomain: "example.com", adminSubdomain: "admin.", path: "accounts", mode: "host-aware" })).toBe("https://admin.example.com/accounts");
|
|
expect(apiUrl({ rootDomain: "example.com", apiSubdomain: "api", path: "users", mode: "host-aware" })).toBe("https://api.example.com/v1/users");
|
|
expect(apiUrl({ rootDomain: "example.com", apiSubdomain: " / ", path: "users", mode: "host-aware" })).toBe("https://example.com/v1/users");
|
|
});
|
|
|
|
test("rejects blank absolute URL roots before building hosts", () => {
|
|
expect(() => appUrl({ rootDomain: " ", tenantSlug: "acme", path: "dashboard" })).toThrow("rootDomain is required");
|
|
});
|
|
|
|
test("builds host-aware absolute URLs with local ports and preserves query/hash suffixes", () => {
|
|
expect(
|
|
appUrl({
|
|
rootDomain: "localhost:3000",
|
|
protocol: "http",
|
|
appSubdomain: "app",
|
|
tenantSlug: "acme/team",
|
|
path: "/settings/api/keys?tab=active#key",
|
|
mode: "host-aware",
|
|
})
|
|
).toBe("http://app.localhost:3000/acme%2Fteam/settings/api/keys?tab=active#key");
|
|
expect(
|
|
adminUrl({
|
|
rootDomain: "localhost:3000",
|
|
protocol: "http",
|
|
adminSubdomain: "admin",
|
|
path: "/users?tab=active#u1",
|
|
mode: "host-aware",
|
|
})
|
|
).toBe("http://admin.localhost:3000/users?tab=active#u1");
|
|
expect(
|
|
apiUrl({
|
|
rootDomain: "localhost:3001",
|
|
protocol: "http",
|
|
apiSubdomain: "api",
|
|
path: "/events?cursor=1#next",
|
|
mode: "host-aware",
|
|
})
|
|
).toBe("http://api.localhost:3001/v1/events?cursor=1#next");
|
|
});
|
|
|
|
test("does not rewrite removed app prefixes in path inputs", () => {
|
|
expect(
|
|
appUrl({
|
|
rootDomain: "example.com",
|
|
appSubdomain: "app",
|
|
tenantSlug: "acme",
|
|
path: "/app/acme/settings",
|
|
mode: "host-aware",
|
|
})
|
|
).toBe("https://app.example.com/acme/app/acme/settings");
|
|
});
|
|
|
|
test("supports default export helpers", () => {
|
|
expect(CanonicalUrlUtils.apiPath("usage", { mode: "host-aware", version: "v1" })).toBe("/v1/usage");
|
|
});
|
|
});
|