220 lines
10 KiB
TypeScript
220 lines
10 KiB
TypeScript
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
|
|
import { createRoutePath, createRoutesFromFolders } from "./v1-route-convention";
|
|
import type { RouteManifest } from "node_modules/@react-router/remix-config-routes-adapter/dist/manifest";
|
|
|
|
const routePathCases: Array<{
|
|
routeId: string;
|
|
expected: string | undefined;
|
|
note: string;
|
|
}> = [
|
|
{ routeId: "index", expected: undefined, note: "root index" },
|
|
{ routeId: "admin/index", expected: "admin", note: "nested index" },
|
|
{ routeId: "__auth/login", expected: "login", note: "pathless layout segment" },
|
|
{ routeId: "account/(settings)", expected: "account/settings?", note: "optional segment" },
|
|
{ routeId: "docs/[sitemap.xml]", expected: "docs/sitemap.xml", note: "escaped dot segment" },
|
|
{ routeId: "files/$", expected: "files/*", note: "splat segment" },
|
|
];
|
|
|
|
let tempAppDirectory: string | undefined;
|
|
|
|
function writeRouteFixture(relativePath: string): void {
|
|
if (!tempAppDirectory) {
|
|
throw new Error("tempAppDirectory must be initialized before writing fixtures");
|
|
}
|
|
|
|
const absolutePath = path.join(tempAppDirectory, relativePath);
|
|
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
|
fs.writeFileSync(absolutePath, "export default function Route() { return null; }\n");
|
|
}
|
|
|
|
function defineRouteManifest(callback: Parameters<typeof createRoutesFromFolders>[0] extends (callback: infer Callback) => RouteManifest ? Callback : never): RouteManifest {
|
|
const routes: RouteManifest = Object.create(null);
|
|
const parentRoutes: Array<{ id: string }> = [];
|
|
|
|
const defineRoute = (routePath: string | undefined, file: string, optionsOrChildren?: { id?: string; index?: boolean } | (() => void), children?: () => void) => {
|
|
const options = typeof optionsOrChildren === "function" ? {} : (optionsOrChildren ?? {});
|
|
const routeChildren = typeof optionsOrChildren === "function" ? optionsOrChildren : children;
|
|
const route = {
|
|
id: options.id ?? file.replace(/\.[a-z0-9]+$/i, ""),
|
|
file,
|
|
path: routePath || undefined,
|
|
index: options.index ? true : undefined,
|
|
parentId: parentRoutes.length > 0 ? parentRoutes[parentRoutes.length - 1].id : "root",
|
|
};
|
|
|
|
routes[route.id] = route;
|
|
|
|
if (routeChildren) {
|
|
parentRoutes.push(route);
|
|
routeChildren();
|
|
parentRoutes.pop();
|
|
}
|
|
};
|
|
|
|
callback(defineRoute);
|
|
|
|
return routes;
|
|
}
|
|
|
|
describe("v1 route convention path creation", () => {
|
|
afterEach(() => {
|
|
if (tempAppDirectory) {
|
|
fs.rmSync(tempAppDirectory, { recursive: true, force: true });
|
|
tempAppDirectory = undefined;
|
|
}
|
|
});
|
|
|
|
it.each(routePathCases)("creates route paths for $note", ({ routeId, expected }) => {
|
|
expect(createRoutePath(routeId)).toBe(expected);
|
|
});
|
|
|
|
it("never returns empty-string paths for supported route naming patterns", () => {
|
|
for (const { routeId } of routePathCases) {
|
|
const routePath = createRoutePath(routeId);
|
|
|
|
expect(routePath === undefined || (typeof routePath === "string" && routePath.length > 0)).toBe(true);
|
|
expect(routePath).not.toBe("");
|
|
}
|
|
});
|
|
|
|
it("keeps escaped, pathless, and index artifacts from becoming empty strings", () => {
|
|
const artifactRouteIds = ["[index]", "__layout/index", "__layout/[.]index"];
|
|
const paths = artifactRouteIds.map((routeId) => createRoutePath(routeId));
|
|
|
|
expect(paths).toEqual(["index", undefined, "."]);
|
|
expect(paths).not.toContain("");
|
|
});
|
|
|
|
it("excludes ignored dotfiles, css artifacts, and colocated tests from generated route manifests", () => {
|
|
tempAppDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "v1-route-convention-"));
|
|
|
|
writeRouteFixture("routes/_index.tsx");
|
|
writeRouteFixture("routes/blog.tsx");
|
|
writeRouteFixture("routes/blog/$slug.tsx");
|
|
writeRouteFixture("routes/blog/.draft.tsx");
|
|
writeRouteFixture("routes/blog/styles.css");
|
|
writeRouteFixture("routes/blog/$slug.test.tsx");
|
|
writeRouteFixture("routes/blog/helpers.spec.ts");
|
|
writeRouteFixture("routes/blog/real.specimen.tsx");
|
|
writeRouteFixture("routes/.hidden.tsx");
|
|
writeRouteFixture("routes/root.css");
|
|
|
|
const manifest = createRoutesFromFolders(defineRouteManifest, {
|
|
appDirectory: tempAppDirectory,
|
|
ignoredFilePatterns: ["**/.*", "**/*.css", "**/*.test.ts", "**/*.test.tsx", "**/*.spec.ts", "**/*.spec.tsx"],
|
|
});
|
|
const entries = Object.values(manifest);
|
|
const routeIds = entries.map((entry) => entry.id);
|
|
const routeFiles = entries.map((entry) => entry.file);
|
|
|
|
expect(routeFiles).toEqual(["routes/_index.tsx", "routes/blog.tsx", "routes/blog/real.specimen.tsx", "routes/blog/$slug.tsx"]);
|
|
expect(routeIds).toEqual(["routes/_index", "routes/blog", "routes/blog/real.specimen", "routes/blog/$slug"]);
|
|
expect(new Set(routeIds).size).toBe(routeIds.length);
|
|
expect(routeIds.every((id) => id.length > 0)).toBe(true);
|
|
expect(routeFiles).not.toEqual(
|
|
expect.arrayContaining([expect.stringContaining(".hidden"), expect.stringContaining(".draft"), expect.stringContaining(".css"), expect.stringContaining(".test"), expect.stringContaining(".spec")])
|
|
);
|
|
});
|
|
|
|
it("promotes the production tenant route directory shape without synthetic parent routes", () => {
|
|
tempAppDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "v1-route-convention-"));
|
|
|
|
writeRouteFixture("routes/app.$tenant.tsx");
|
|
writeRouteFixture("routes/app.$tenant/dashboard.tsx");
|
|
writeRouteFixture("routes/app.$tenant/settings.tsx");
|
|
writeRouteFixture("routes/app.$tenant/settings/subscription.tsx");
|
|
writeRouteFixture("routes/app.$tenant/settings/members.tsx");
|
|
|
|
const manifest = createRoutesFromFolders(defineRouteManifest, {
|
|
appDirectory: tempAppDirectory,
|
|
});
|
|
const promotedTenantRoutes = Object.fromEntries(
|
|
Object.values(manifest)
|
|
.filter((entry) => entry.id.startsWith("routes/app.$tenant"))
|
|
.map((entry) => [entry.id, { file: entry.file, path: entry.path, parentId: entry.parentId }])
|
|
);
|
|
|
|
expect(promotedTenantRoutes).toMatchObject({
|
|
"routes/app.$tenant": { file: "routes/app.$tenant.tsx", path: ":tenant", parentId: "root" },
|
|
"routes/app.$tenant/dashboard": { file: "routes/app.$tenant/dashboard.tsx", path: "dashboard", parentId: "routes/app.$tenant" },
|
|
"routes/app.$tenant/settings": { file: "routes/app.$tenant/settings.tsx", path: "settings", parentId: "routes/app.$tenant" },
|
|
"routes/app.$tenant/settings/members": { file: "routes/app.$tenant/settings/members.tsx", path: "members", parentId: "routes/app.$tenant/settings" },
|
|
"routes/app.$tenant/settings/subscription": { file: "routes/app.$tenant/settings/subscription.tsx", path: "subscription", parentId: "routes/app.$tenant/settings" },
|
|
});
|
|
expect(Object.values(promotedTenantRoutes).map((entry) => entry.path)).not.toEqual(
|
|
expect.arrayContaining(["app/:tenant", expect.stringMatching(/^app\/:tenant\//), expect.stringMatching(/^:tenant\/:tenant/)])
|
|
);
|
|
expect(Object.values(promotedTenantRoutes).every((entry) => typeof entry.file === "string")).toBe(true);
|
|
});
|
|
|
|
it("ignores server-only files when requested by the route config", () => {
|
|
tempAppDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "v1-route-convention-"));
|
|
|
|
writeRouteFixture("routes/app.$tenant.tsx");
|
|
writeRouteFixture("routes/app.$tenant/portalAccess.server.ts");
|
|
|
|
const manifest = createRoutesFromFolders(defineRouteManifest, {
|
|
appDirectory: tempAppDirectory,
|
|
ignoredFilePatterns: ["**/*.server.ts", "**/*.server.tsx"],
|
|
});
|
|
|
|
expect(Object.values(manifest).map((entry) => entry.file)).toEqual(["routes/app.$tenant.tsx"]);
|
|
});
|
|
|
|
it("ignores route directories when requested by the route config", () => {
|
|
tempAppDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "v1-route-convention-"));
|
|
|
|
writeRouteFixture("routes/admin/ai/index.tsx");
|
|
writeRouteFixture("routes/admin/ai/chats.tsx");
|
|
writeRouteFixture("routes/admin/dashboard.tsx");
|
|
|
|
const manifest = createRoutesFromFolders(defineRouteManifest, {
|
|
appDirectory: tempAppDirectory,
|
|
ignoredFilePatterns: ["admin/ai/**"],
|
|
});
|
|
|
|
expect(Object.values(manifest).map((entry) => entry.file)).toEqual(["routes/admin/dashboard.tsx"]);
|
|
});
|
|
|
|
it("adds canonical admin host aliases under an admin data/layout parent", () => {
|
|
tempAppDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "v1-route-convention-"));
|
|
|
|
writeRouteFixture("routes/admin.tsx");
|
|
writeRouteFixture("routes/admin/dashboard.tsx");
|
|
writeRouteFixture("routes/admin/mfa-challenge.tsx");
|
|
writeRouteFixture("routes/admin/settings.tsx");
|
|
writeRouteFixture("routes/admin/settings/profile.tsx");
|
|
writeRouteFixture("routes/admin/crm.tsx");
|
|
writeRouteFixture("routes/admin/crm/index.tsx");
|
|
|
|
const manifest = createRoutesFromFolders(defineRouteManifest, {
|
|
appDirectory: tempAppDirectory,
|
|
});
|
|
const adminHostRoutes = Object.fromEntries(
|
|
Object.values(manifest)
|
|
.filter((entry) => entry.id.includes(".__host"))
|
|
.map((entry) => [entry.id, { file: entry.file, path: entry.path, index: entry.index, parentId: entry.parentId }])
|
|
);
|
|
|
|
expect(adminHostRoutes).toMatchObject({
|
|
"routes/admin.__host": { file: "routes/admin.tsx", path: undefined, parentId: "root" },
|
|
"routes/admin/settings.__host": { file: "routes/admin/settings.tsx", path: undefined, parentId: "routes/admin.__host" },
|
|
"routes/admin/settings/profile.__host": {
|
|
file: "routes/admin/settings/profile.tsx",
|
|
path: "settings/profile",
|
|
parentId: "routes/admin/settings.__host",
|
|
},
|
|
"routes/admin/crm.__host": { file: "routes/admin/crm.tsx", path: "crm", parentId: "routes/admin.__host" },
|
|
"routes/admin/crm/index.__host": { file: "routes/admin/crm/index.tsx", path: undefined, index: true, parentId: "routes/admin/crm.__host" },
|
|
});
|
|
expect(adminHostRoutes).not.toHaveProperty("routes/admin/dashboard.__host");
|
|
expect(adminHostRoutes).not.toHaveProperty("routes/admin/mfa-challenge.__host");
|
|
expect(manifest).not.toHaveProperty("routes/admin");
|
|
expect(Object.values(manifest)).not.toEqual(expect.arrayContaining([expect.objectContaining({ id: "routes/admin", path: "admin" })]));
|
|
});
|
|
});
|