one
Version:
One is a new React Framework that makes Vite serve both native and web.
118 lines (117 loc) • 5.16 kB
JavaScript
import { describe, expect, it } from "vitest";
import { encodeReservedFilenameChars, getLoaderPath, getPathFromLoaderPath, getPreloadPath } from "./cleanUrl.native.js";
function roundtrip(path) {
var loaderPath = getLoaderPath(path, false);
return getPathFromLoaderPath(loaderPath);
}
describe("cleanUrl roundtrip", function () {
it("simple path", function () {
expect(roundtrip("/docs/getting-started")).toBe("/docs/getting-started");
});
it("path with underscore prefix (/_/docs)", function () {
expect(roundtrip("/_/docs/getting-started")).toBe("/_/docs/getting-started");
});
it("path with underscore prefix (/_/terms)", function () {
expect(roundtrip("/_/terms")).toBe("/_/terms");
});
it("path with underscore in segment name", function () {
expect(roundtrip("/my_page/test")).toBe("/my_page/test");
});
it("deeply nested path", function () {
expect(roundtrip("/deep/nested/path/here")).toBe("/deep/nested/path/here");
});
it("root path", function () {
expect(roundtrip("/")).toBe("/");
});
it("single segment", function () {
expect(roundtrip("/about")).toBe("/about");
});
it("path with query string is stripped", function () {
var loaderPath = getLoaderPath("/docs/intro?foo=bar", false);
expect(getPathFromLoaderPath(loaderPath)).toBe("/docs/intro");
});
it("path with hash is stripped", function () {
var loaderPath = getLoaderPath("/docs/intro#section", false);
expect(getPathFromLoaderPath(loaderPath)).toBe("/docs/intro");
});
it("path with trailing slash", function () {
var loaderPath = getLoaderPath("/docs/intro/", false);
expect(getPathFromLoaderPath(loaderPath)).toBe("/docs/intro");
});
});
describe("getLoaderPath format", function () {
it("includes /assets/ prefix", function () {
var result = getLoaderPath("/docs/intro", false);
expect(result).toMatch(/^\/assets\//);
});
it("ends with loader postfix", function () {
var result = getLoaderPath("/docs/intro", false);
expect(result).toMatch(/_\d+_vxrn_loader\.js$/);
});
it("includes /_one prefix in dev mode", function () {
var originalEnv = process.env.NODE_ENV;
process.env.NODE_ENV = "development";
try {
var result = getLoaderPath("/docs/intro", false);
expect(result).toMatch(/^\/_one\/assets\//);
} finally {
process.env.NODE_ENV = originalEnv;
}
});
it("includes cache bust segment", function () {
var result = getLoaderPath("/docs/intro", false, "12345");
expect(result).toContain("_refetch_12345_");
});
});
describe("NTFS-reserved characters in route-derived names", function () {
it("roundtrips a root dynamic route pattern", function () {
expect(roundtrip("/:param")).toBe("/:param");
});
it("roundtrips a nested dynamic route pattern", function () {
expect(roundtrip("/dashboard/:appId")).toBe("/dashboard/:appId");
});
it("roundtrips a catch-all pattern", function () {
expect(roundtrip("/*")).toBe("/*");
});
it("roundtrips hostile concrete param values (chars that survive URL parsing raw)", function () {
expect(roundtrip("/blog/a:b*c")).toBe("/blog/a:b*c");
});
it("emits filesystem-legal names for the full reserved set via preload paths", function () {
expect(getPreloadPath('/blog/a:b*c"d<e>f|g?h')).not.toMatch(/[<>:"|?*]/);
});
it("roundtrips literal equals signs (self-escape)", function () {
expect(roundtrip("/foo=bar")).toBe("/foo=bar");
});
it("roundtrips equals followed by hex-looking characters", function () {
expect(roundtrip("/x=3ay")).toBe("/x=3ay");
});
it("emits no NTFS-reserved characters in loader filenames", function () {
expect(getLoaderPath("/dashboard/:appId", false)).not.toMatch(/[<>:"|?*]/);
});
it("emits no NTFS-reserved characters in preload filenames", function () {
expect(getPreloadPath("/:param")).not.toMatch(/[<>:"|?*]/);
expect(getPreloadPath("/*")).not.toMatch(/[<>:"|?*]/);
});
it("encodeReservedFilenameChars is identity for clean paths", function () {
expect(encodeReservedFilenameChars("/blog/my-slug.html")).toBe("/blog/my-slug.html");
});
it("encodes the html artifact path for non-prerendered dynamic routes", function () {
expect(encodeReservedFilenameChars("/dashboard/:appId.html")).toBe("/dashboard/=3aappId.html");
expect(encodeReservedFilenameChars("/*.html")).toBe("/=2a.html");
});
});
describe("getPathFromLoaderPath", function () {
it("strips /_one/assets prefix", function () {
expect(getPathFromLoaderPath("/_one/assets/docs_intro_999_vxrn_loader.js")).toBe("/docs/intro");
});
it("strips /assets prefix", function () {
expect(getPathFromLoaderPath("/assets/docs_intro_999_vxrn_loader.js")).toBe("/docs/intro");
});
it("strips refetch cache bust", function () {
expect(getPathFromLoaderPath("/assets/docs_intro_refetch_12345__999_vxrn_loader.js")).toBe("/docs/intro");
});
it("decodes escaped underscores back to literal underscores", function () {
expect(getPathFromLoaderPath("/assets/___docs_intro_999_vxrn_loader.js")).toBe("/_/docs/intro");
});
});
//# sourceMappingURL=cleanUrl.test.native.js.map