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

88 lines
3.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { remixConfigRoutes } from "@react-router/remix-config-routes-adapter";
import { defaultDisabledPluginRouteFilePatterns, defaultIgnoredRouteFilePatterns } from "./routes";
import { createRoutesFromFolders } from "./utils/compat/v1-route-convention";
import { resolveRepoPath } from "./utils/testing/repoPaths";
/**
* AK-SET-01 Slice 0 — baseline of every tenant-settings route.
*
* The settings tree mixes the dot convention (`privacy.export.tsx`) with the
* folder convention (`members/new.tsx`). Moving a leaf between hubs can drop a
* route from the manifest without any test failing, because nothing else
* asserts on the resolved URL of a settings subpage.
*
* This snapshot is that missing assertion: it pins URL -> route file for the
* whole subtree. A hub move is expected to change it — the point is that the
* diff has to be read and approved, not that it never moves.
*/
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];
const SETTINGS_FILE_PREFIX = "routes/app.$tenant/settings";
function normalizeRouteFile(file: string) {
return file.replaceAll("\\", "/");
}
function joinSegments(parentPath: string, segment: string | undefined) {
if (!segment) return parentPath;
const trimmed = segment.replace(/^\/+|\/+$/g, "");
if (!trimmed) return parentPath;
return parentPath === "/" ? `/${trimmed}` : `${parentPath}/${trimmed}`;
}
/** Collect `urlPath -> route file` for every descendant of the settings subtree. */
function collectSettingsRoutes(manifest: RouteManifest) {
const collected: string[] = [];
const visit = (entry: RouteManifestEntry, parentPath: string) => {
const fullPath = joinSegments(parentPath, entry.path);
const file = normalizeRouteFile(entry.file);
if (file.startsWith(SETTINGS_FILE_PREFIX)) {
// `index: true` routes carry no own segment, so mark them explicitly —
// otherwise a layout and its index render as the same line.
collected.push(`${fullPath}${entry.index ? " (index)" : ""} -> ${file}`);
}
Object.values(entry.children ?? {}).forEach((child) => visit(child as RouteManifestEntry, fullPath));
};
Object.values(manifest).forEach((entry) => visit(entry, "/"));
return collected.sort();
}
describe("tenant settings route manifest", () => {
it("pins every settings URL to its route file", async () => {
const manifest = await routes;
expect(collectSettingsRoutes(manifest)).toMatchSnapshot();
});
it("keeps mail, download and CSV resource endpoints on their published URLs", async () => {
// AK-SET-01 endpoint rule: these URLs are handed out to the outside world
// (deletion-confirmation mails, invoice downloads). A hub move must never
// relocate them, because old mails cannot be rewritten.
const manifest = await routes;
const files = collectSettingsRoutes(manifest).map((line) => line.split(" -> ")[1]);
expect(files).toEqual(
expect.arrayContaining([
"routes/app.$tenant/settings/privacy.delete.confirm.$token.tsx",
"routes/app.$tenant/settings/privacy.export.$jobId.download.ts",
"routes/app.$tenant/settings/subscription/invoices.$invoiceId.download.tsx",
"routes/app.$tenant/settings/chat/audit.$id.attachments.$fileId.$variant.tsx",
"routes/app.$tenant/settings/logs/export.ts",
"routes/app.$tenant/settings/logs/events-export.ts",
])
);
});
});