99 lines
4.2 KiB
TypeScript
99 lines
4.2 KiB
TypeScript
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { describe, expect, it } from "vitest";
|
|
import { resolveRepoPath } from "~/utils/testing/repoPaths";
|
|
|
|
const deferredStaticAdminTargets = new Set(["members"]);
|
|
|
|
function walkFiles(relativeDirectory: string): string[] {
|
|
const absoluteDirectory = resolveRepoPath(relativeDirectory);
|
|
const files: string[] = [];
|
|
|
|
const visit = (directory: string) => {
|
|
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
const absolutePath = path.join(directory, entry.name);
|
|
if (entry.isDirectory()) {
|
|
visit(absolutePath);
|
|
} else if (/\.(ts|tsx)$/.test(entry.name) && !/\.test\./.test(entry.name) && !/\.server\./.test(entry.name)) {
|
|
files.push(absolutePath);
|
|
}
|
|
}
|
|
};
|
|
|
|
visit(absoluteDirectory);
|
|
return files;
|
|
}
|
|
|
|
function routePathFromAdminRouteFile(absolutePath: string): string {
|
|
const adminRoutesRoot = resolveRepoPath("apps/web/app/routes/admin");
|
|
const relativePath = path.relative(adminRoutesRoot, absolutePath).replaceAll(path.sep, "/").replace(/\.(ts|tsx)$/, "");
|
|
|
|
return relativePath
|
|
.split("/")
|
|
.flatMap((part) => part.split("."))
|
|
.filter((part) => part !== "" && part !== "index" && !part.startsWith("__"))
|
|
.join("/");
|
|
}
|
|
|
|
function normalizeAdminTarget(target: string): string {
|
|
return target.replace(/^\/+/, "").replace(/[?#].*$/, "").replace(/\/+$/, "");
|
|
}
|
|
|
|
function targetMatchesRoute(target: string, routePath: string): boolean {
|
|
const targetParts = normalizeAdminTarget(target).split("/").filter(Boolean);
|
|
const routeParts = routePath.split("/").filter(Boolean);
|
|
|
|
return targetParts.length === routeParts.length && routeParts.every((part, index) => part.startsWith("$") || part === targetParts[index]);
|
|
}
|
|
|
|
function stripComments(source: string): string {
|
|
return source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, "");
|
|
}
|
|
|
|
function staticAdminPathTargets(source: string): string[] {
|
|
return [...stripComments(source).matchAll(/adminPath\(\s*(["'`])([^"'`$]*?)\1/g)]
|
|
.map((match) => normalizeAdminTarget(match[2]))
|
|
.filter(Boolean);
|
|
}
|
|
|
|
function collectAdminRoutePaths(): Set<string> {
|
|
return new Set(["", ...walkFiles("apps/web/app/routes/admin").map(routePathFromAdminRouteFile)]);
|
|
}
|
|
|
|
describe("admin route targets", () => {
|
|
it("keeps static adminPath targets on registered admin route files", () => {
|
|
const routePaths = collectAdminRoutePaths();
|
|
const failures = walkFiles("apps/web/app")
|
|
.flatMap((absolutePath) =>
|
|
staticAdminPathTargets(fs.readFileSync(absolutePath, "utf8")).map((target) => ({ absolutePath, target }))
|
|
)
|
|
.filter(({ target }) => !deferredStaticAdminTargets.has(target))
|
|
.filter(({ target }) => ![...routePaths].some((routePath) => targetMatchesRoute(target, routePath)))
|
|
.map(({ absolutePath, target }) => `${path.relative(resolveRepoPath("."), absolutePath).replaceAll(path.sep, "/")} -> ${target}`);
|
|
|
|
expect(failures).toEqual([]);
|
|
});
|
|
|
|
it("keeps update and entity-view redirects on registered admin routes", () => {
|
|
const updateButton = fs.readFileSync(resolveRepoPath("apps/web/app/components/layouts/buttons/UpdateAvailableButton.tsx"), "utf8");
|
|
const entityViewsRoute = fs.readFileSync(resolveRepoPath("apps/web/app/routes/admin/entities/views.tsx"), "utf8");
|
|
const adminRoutes = collectAdminRoutePaths();
|
|
|
|
expect(updateButton).not.toContain('adminPath("version"');
|
|
expect(updateButton).toContain('adminPath("settings"');
|
|
expect(entityViewsRoute).not.toContain('adminPath("entities/views/all"');
|
|
expect(entityViewsRoute).toContain('adminPath("entities/views"');
|
|
expect(adminRoutes).toContain("settings");
|
|
expect(adminRoutes).toContain("entities/views");
|
|
expect(adminRoutes).not.toContain("version");
|
|
expect(adminRoutes).not.toContain("entities/views/all");
|
|
});
|
|
|
|
it("keeps onboarding session fallbacks on admin-scoped route targets", () => {
|
|
const onboardingSessionApi = fs.readFileSync(resolveRepoPath("apps/web/app/modules/onboarding/routes/api/sessions/OnboardingSessionOverviewApi.server.ts"), "utf8");
|
|
|
|
expect(onboardingSessionApi).not.toContain('redirect("/onboarding/sessions"');
|
|
expect(onboardingSessionApi).toContain('adminPath("onboarding/sessions"');
|
|
});
|
|
});
|