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 { 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 redirects on registered admin routes", () => { const updateButton = fs.readFileSync(resolveRepoPath("apps/web/app/components/layouts/buttons/UpdateAvailableButton.tsx"), "utf8"); const adminRoutes = collectAdminRoutePaths(); expect(updateButton).not.toContain('adminPath("version"'); expect(updateButton).toContain('adminPath("settings"'); expect(adminRoutes).toContain("settings"); expect(adminRoutes).not.toContain("version"); }); });