Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes.test.ts
T

340 lines
18 KiB
TypeScript

import { matchRoutes } from "react-router";
import { describe, expect, it } from "vitest";
import { remixConfigRoutes } from "@react-router/remix-config-routes-adapter";
import { createCoreRouteConfig, createPortalRouteConfig, defaultDisabledPluginRouteFilePatterns, defaultIgnoredRouteFilePatterns } from "./routes";
import { createRoutesFromFolders } from "./utils/compat/v1-route-convention";
import { resolveRepoPath } from "./utils/testing/repoPaths";
const routes = remixConfigRoutes(async (defineRoutes) => {
return createRoutesFromFolders(defineRoutes, {
appDirectory: resolveRepoPath("apps/web/app"),
ignoredFilePatterns: [...defaultIgnoredRouteFilePatterns, ...defaultDisabledPluginRouteFilePatterns],
});
});
type RouteManifest = Awaited<typeof routes>;
type RouteManifestEntry = RouteManifest[number];
function flattenRoutes(manifest: RouteManifest) {
const flattened: RouteManifestEntry[] = [];
const visit = (entry: RouteManifestEntry) => {
flattened.push(entry);
Object.values(entry.children ?? {}).forEach(visit);
};
Object.values(manifest).forEach(visit);
return flattened;
}
function normalizeRouteFile(file: string) {
return file.replaceAll("\\", "/");
}
describe("app route discovery", () => {
it("matches the reviewed production runtime manifests", async () => {
const coreRoutes = flattenRoutes(await createCoreRouteConfig("production"));
const portalRoutes = flattenRoutes(createPortalRouteConfig());
// 510 + the three reviewed WF-P8 routes (S04 board settings, S05 field
// template, S02 contact-search resource) + the reviewed tenant-bound
// product-acquisition return route from BILL-13 + P4's reviewed
// link-target search resource behind the file detail's link dialog + the
// reviewed BILL-14 public registration plan-advice resource + the UI
// preferences appearance route + BILL-20's paid welcome/setup route.
expect(coreRoutes).toHaveLength(518);
expect(new Set(coreRoutes.map((entry) => normalizeRouteFile(entry.file))).size).toBe(518);
expect(coreRoutes.find((entry) => normalizeRouteFile(entry.file) === "routes/auth.registration-plan-advice.ts")).toMatchObject({
path: "auth/registration-plan-advice",
});
expect(portalRoutes).toHaveLength(34);
expect(new Set(portalRoutes.map((entry) => normalizeRouteFile(entry.file))).size).toBe(18);
});
it("keeps the local Customer Portal preview out of production builds", async () => {
const productionFiles = flattenRoutes(await createCoreRouteConfig("production")).map((entry) => normalizeRouteFile(entry.file));
const developmentFiles = flattenRoutes(await createCoreRouteConfig("development")).map((entry) => normalizeRouteFile(entry.file));
expect(productionFiles).not.toContain("routes/[_]dev.customer-portal-preview.settings.tsx");
expect(developmentFiles).toContain("routes/[_]dev.customer-portal-preview.settings.tsx");
});
it("keeps Core and Customer Portal routes in isolated manifests", async () => {
const coreConfig = await createCoreRouteConfig();
const portalConfig = createPortalRouteConfig();
const corePortalLayouts = coreConfig.filter((entry) => entry.file === "routes/customer-portal/layout.tsx");
const portalLayouts = portalConfig.filter((entry) => entry.file === "routes/customer-portal/layout.tsx");
const standardPortalLayout = portalLayouts.find((entry) => entry.path === ":tenant");
const customHostPortalLayout = portalLayouts.find((entry) => entry.id === "routes/customer-portal/layout.__custom-host");
expect(corePortalLayouts).toEqual([]);
expect(portalLayouts).toHaveLength(2);
expect(standardPortalLayout).toMatchObject({
id: "routes/customer-portal/layout",
path: ":tenant",
children: [
{ index: true, file: "routes/customer-portal/dashboard.tsx", id: "routes/customer-portal/dashboard" },
{ path: "projects", file: "routes/customer-portal/projects.tsx", id: "routes/customer-portal/projects" },
{ path: "upload", file: "routes/customer-portal/upload.tsx", id: "routes/customer-portal/upload" },
{ path: "settings", file: "routes/customer-portal/settings.tsx", id: "routes/customer-portal/settings" },
{
path: "branding/logo",
file: "routes/customer-portal/branding-logo.ts",
id: "routes/customer-portal/branding-logo",
},
{
path: "exports/:exportId",
file: "routes/customer-portal/export-download.ts",
id: "routes/customer-portal/export-download",
},
{
path: "projects/:projectId/upload",
file: "routes/customer-portal/project-upload.ts",
id: "routes/customer-portal/project-upload",
},
{
path: "projects/:projectId/complaints/:formId",
file: "routes/customer-portal/project-complaint.ts",
id: "routes/customer-portal/project-complaint",
},
{
path: "projects/:projectId",
file: "routes/customer-portal/project-detail.tsx",
id: "routes/customer-portal/project-detail",
},
{
path: "documents/:releaseId",
file: "routes/customer-portal/document-download.ts",
id: "routes/customer-portal/document-download",
},
{ path: "logout", file: "routes/customer-portal/logout.ts", id: "routes/customer-portal/logout" },
{ path: "*", file: "routes/customer-portal/auth.tsx", id: "routes/customer-portal/auth" },
],
});
expect(customHostPortalLayout).toMatchObject({
path: undefined,
children: expect.arrayContaining([
{ index: true, file: "routes/customer-portal/custom-host-dispatch.tsx", id: "routes/customer-portal/dashboard.__custom-host" },
{ path: "projects", file: "routes/customer-portal/custom-host-dispatch.tsx", id: "routes/customer-portal/projects.__custom-host" },
{
path: "branding/logo",
file: "routes/customer-portal/branding-logo.ts",
id: "routes/customer-portal/branding-logo.__custom-host",
},
{ path: "login/*", file: "routes/customer-portal/custom-host-dispatch.tsx", id: "routes/customer-portal/auth.login.__custom-host" },
]),
});
const publicMatches = matchRoutes(portalConfig, "/acme/projects/project-1");
const customProjectsMatches = matchRoutes(portalConfig, "/projects/project-1");
const internalMatches = matchRoutes(portalConfig, "/__portal/acme/projects/project-1");
const corePublicMatches = matchRoutes(coreConfig, "/acme/projects/project-1");
expect(publicMatches?.map((match) => match.route.file)).toContain("routes/customer-portal/project-detail.tsx");
expect(publicMatches?.map((match) => match.route.id)).toEqual(["routes/customer-portal/layout", "routes/customer-portal/project-detail"]);
for (const [pathname, expectedLeafId] of [
["/", "routes/customer-portal/dashboard.__custom-host"],
["/login", "routes/customer-portal/auth.login.__custom-host"],
["/forgot-password", "routes/customer-portal/auth.forgot-password.__custom-host"],
["/setup/setup-token", "routes/customer-portal/auth.setup.__custom-host"],
]) {
const customMatches = matchRoutes(portalConfig, pathname);
expect(customMatches?.map((match) => match.route.id)).toEqual(["routes/customer-portal/layout.__custom-host", expectedLeafId]);
expect(customMatches?.at(-1)?.params.tenant).toBeUndefined();
}
expect(customProjectsMatches?.map((match) => match.route.id)).toEqual([
"routes/customer-portal/layout.__custom-host",
"routes/customer-portal/project-detail.__custom-host",
]);
for (const [pathname, expectedIds, expectedFile] of [
[
"/projects",
["routes/customer-portal/layout.__custom-host", "routes/customer-portal/projects.__custom-host"],
"routes/customer-portal/custom-host-dispatch.tsx",
],
["/projects/projects", ["routes/customer-portal/layout", "routes/customer-portal/projects"], "routes/customer-portal/projects.tsx"],
[
"/upload",
["routes/customer-portal/layout.__custom-host", "routes/customer-portal/upload.__custom-host"],
"routes/customer-portal/custom-host-dispatch.tsx",
],
[
"/setup",
["routes/customer-portal/layout.__custom-host", "routes/customer-portal/auth.setup.__custom-host"],
"routes/customer-portal/custom-host-dispatch.tsx",
],
]) {
const collisionMatches = matchRoutes(portalConfig, pathname);
expect(collisionMatches?.map((match) => match.route.id)).toEqual(expectedIds);
expect(collisionMatches?.at(-1)?.route.file).toBe(expectedFile);
}
expect(internalMatches?.map((match) => match.route.file)).not.toContain("routes/customer-portal/project-detail.tsx");
expect(corePublicMatches?.map((match) => match.route.file)).not.toContain("routes/customer-portal/project-detail.tsx");
expect(portalConfig.map((entry) => entry.file)).toEqual(
expect.arrayContaining(["routes/healthz.tsx", "routes/readyz.tsx", "routes/healthcheck.tsx", "routes/api.metrics.ts"])
);
expect(matchRoutes(portalConfig, "/api/metrics")?.at(-1)?.route.id).toBe("routes/api.metrics.portal");
});
it("excludes colocated test and spec files from the production route manifest", async () => {
const routeFiles = flattenRoutes(await routes).map((entry) => normalizeRouteFile(entry.file));
expect(routeFiles).not.toEqual(expect.arrayContaining([expect.stringMatching(/\.test\.[tj]sx?$/), expect.stringMatching(/\.spec\.[tj]sx?$/)]));
expect(routeFiles).not.toEqual(expect.arrayContaining([expect.stringMatching(/\.server\.[tj]sx?$/)]));
expect(routeFiles).not.toEqual(expect.arrayContaining(["routes/admin/404.tsx", "routes/admin/sales/settings.route-titles.ts"]));
expect(routeFiles).toEqual(expect.arrayContaining(["routes/forgot-password.tsx", "routes/invitation.$id.tsx"]));
expect(routeFiles).toEqual(expect.arrayContaining(["routes/401.tsx", "routes/403.tsx", "routes/404.tsx", "routes/500.tsx"]));
expect(routeFiles).toEqual(expect.arrayContaining(["routes/plan-required.$tenant.tsx", "routes/feature-disabled.tsx"]));
expect(routeFiles).toEqual(expect.arrayContaining(["routes/[.]well-known.oauth-authorization-server.ts"]));
});
it("registers the Sentry client smoke route at the compatibility-convention URL", async () => {
const matches = matchRoutes(await routes, "/client-throw");
expect(matches?.at(-1)?.route.file).toBe("routes/__test.client-throw.tsx");
});
it("exposes OAuth authorization-server metadata under .well-known", async () => {
const routePaths = flattenRoutes(await routes)
.map((entry) => entry.path)
.filter((path): path is string => typeof path === "string");
expect(routePaths).toEqual(expect.arrayContaining([".well-known/oauth-authorization-server"]));
});
it("registers the fixed KWP setup and OAuth callback paths", async () => {
const routePaths = flattenRoutes(await routes)
.map((entry) => entry.path)
.filter((path): path is string => typeof path === "string");
expect(routePaths).toEqual(expect.arrayContaining(["integrations/kwp/setup", "integrations/kwp/oauth/callback"]));
});
it("registers the tenant-bound product acquisition return path", async () => {
const matches = matchRoutes(await routes, "/subscribe/acme/product/acquisition-1/success");
expect(matches?.at(-1)?.route.file).toBe("routes/subscribe.$tenant.product.$acquisition.success.tsx");
});
it("exposes promoted tenant app routes without legacy /app prefixes", async () => {
const routePaths = flattenRoutes(await routes)
.map((entry) => entry.path)
.filter((path): path is string => typeof path === "string");
expect(routePaths).not.toEqual(expect.arrayContaining(["app/:tenant"]));
expect(routePaths).not.toEqual(expect.arrayContaining([expect.stringMatching(/^app\/:tenant\//)]));
expect(routePaths).toEqual(expect.arrayContaining([":tenant"]));
expect(routePaths).toEqual(expect.arrayContaining(["plan-required/:tenant"]));
});
it("exposes admin routes only through admin-host aliases", async () => {
const flattenedRoutes = flattenRoutes(await routes);
const legacyAdminRoutes = flattenedRoutes.filter(
(entry) => (entry.id === "routes/admin" || entry.id.startsWith("routes/admin/")) && !entry.id.endsWith(".__host")
);
const adminHostRoutes = flattenedRoutes.filter(
(entry) => entry.id === "routes/admin.__host" || (entry.id.startsWith("routes/admin/") && entry.id.endsWith(".__host"))
);
const routePaths = flattenedRoutes.map((entry) => entry.path).filter((path): path is string => typeof path === "string");
expect(legacyAdminRoutes).toEqual([]);
expect(adminHostRoutes.length).toBeGreaterThan(0);
expect(routePaths).not.toEqual(expect.arrayContaining(["admin", expect.stringMatching(/^admin\//)]));
});
it("keeps /select-tenant on one host-dispatching manifest route", async () => {
const flattenedRoutes = flattenRoutes(await routes);
const matches = matchRoutes(await routes, "/select-tenant");
expect(flattenedRoutes.find((entry) => entry.id === "routes/admin/select-tenant.__host")).toBeUndefined();
expect(matches?.map((match) => match.route.id)).toEqual(["routes/select-tenant"]);
});
it("does not expose the removed public pricing marketing routes", async () => {
const flattenedRoutes = flattenRoutes(await routes);
const routeFiles = flattenedRoutes.map((entry) => normalizeRouteFile(entry.file));
const routePaths = flattenedRoutes.map((entry) => entry.path).filter((path): path is string => typeof path === "string");
expect(routeFiles).not.toEqual(expect.arrayContaining(["routes/pricing.tsx", "routes/pricing.$session.success.tsx"]));
expect(routePaths).not.toEqual(expect.arrayContaining(["pricing", "pricing/:session/success"]));
expect(routePaths).toEqual(expect.arrayContaining(["subscribe/:tenant"]));
});
it("registers product-core tenant routes and keeps disabled plugin route trees out of the core app", async () => {
const flattenedRoutes = flattenRoutes(await routes);
const routeFiles = flattenedRoutes.map((entry) => normalizeRouteFile(entry.file));
const routePaths = flattenedRoutes.map((entry) => entry.path).filter((path): path is string => typeof path === "string");
expect(routeFiles).not.toEqual(
expect.arrayContaining([
expect.stringMatching(/^routes\/admin\/(?:__workflows|ai|chat|crm|email-marketing|help-desk|internal\/ai|knowledge-base|prompts)(?:\.tsx|\/)/),
expect.stringMatching(/^routes\/app\.\$tenant\/internal\/ai(?:\.tsx|\/)/),
expect.stringMatching(/^routes\/help\//),
expect.stringMatching(/^routes\/internal\/api\/__workflows\//),
expect.stringMatching(/^routes\/mcp\//),
])
);
expect(routeFiles).toEqual(
expect.arrayContaining([
// R-P6: the entity-builder CRM route tree was removed; the typed CRM
// lives under app.$tenant/contacts.* now.
"routes/app.$tenant/contacts._index.tsx",
"routes/app.$tenant/email-marketing.tsx",
"routes/app.$tenant/chat._index.tsx",
"routes/app.$tenant/chat.$id.tsx",
"routes/app.$tenant/__workflows/workflow-engine.tsx",
"routes/app.$tenant/telephony.push-subscription.ts",
])
);
expect(routePaths).not.toEqual(
expect.arrayContaining([
"ai",
"help-desk",
"knowledge-base",
"mcp",
"prompts",
"help/:slug/:lang?/articles/:article",
"help/:slug/:lang?/categories/:category",
"help/:slug/:lang?",
"internal/api/workflows",
"internal/api/workflows/run/:id",
"internal/api/workflows/executions/:id",
"internal/api/workflows/executions/:id/continue",
"internal/api/workflows/stream/:executionId",
])
);
expect(routePaths).toEqual(expect.arrayContaining(["chat", "chat/:id", "email-marketing", "workflow-engine", "telephony/push-subscription"]));
});
it("does not keep internal workflow API proxy targets registered", async () => {
const flattenedRoutes = flattenRoutes(await routes);
const routeFiles = flattenedRoutes.map((entry) => normalizeRouteFile(entry.file));
const routePaths = flattenedRoutes.map((entry) => entry.path).filter((path): path is string => typeof path === "string");
expect(routeFiles).not.toEqual(
expect.arrayContaining([
"routes/internal/api/__workflows/workflows.run.$id.tsx",
"routes/internal/api/__workflows/workflows.executions.$id.tsx",
"routes/internal/api/__workflows/workflows.executions.$id.continue.tsx",
"routes/internal/api/__workflows/workflows.executions.$id.continue.start.tsx",
"routes/internal/api/__workflows/workflows.stream.$executionId.tsx",
])
);
expect(routePaths).not.toEqual(
expect.arrayContaining([
"internal/api/workflows/run/:id",
"internal/api/workflows/executions/:id",
"internal/api/workflows/executions/:id/continue",
"internal/api/workflows/executions/:id/continue/start",
"internal/api/workflows/stream/:executionId",
])
);
});
it("does not expose removed fake/demo admin entity routes", async () => {
const flattenedRoutes = flattenRoutes(await routes);
const routeFiles = flattenedRoutes.map((entry) => normalizeRouteFile(entry.file));
const routePaths = flattenedRoutes.map((entry) => entry.path).filter((path): path is string => typeof path === "string");
expect(routeFiles).not.toEqual(expect.arrayContaining([["routes", "admin", "entities", "fake-rows.tsx"].join("/")]));
expect(routePaths).not.toEqual(expect.arrayContaining(["entities/fake-rows"]));
});
});