import fs from "node:fs"; import path from "node:path"; import { describe, expect, it } from "vitest"; const routeRoot = path.resolve(process.cwd(), "apps/web/app/routes/admin/knowledge-base"); function readRoute(relativePath: string) { return fs.readFileSync(path.join(routeRoot, relativePath), "utf8"); } function readRouteFiles(directory = routeRoot): { relativePath: string; source: string }[] { const entries = fs.readdirSync(directory, { withFileTypes: true }); return entries.flatMap((entry) => { const absolutePath = path.join(directory, entry.name); if (entry.isDirectory()) { return readRouteFiles(absolutePath); } if (!entry.name.endsWith(".tsx")) { return []; } return [ { relativePath: path.relative(routeRoot, absolutePath), source: fs.readFileSync(absolutePath, "utf8"), }, ]; }); } describe("admin knowledge-base route scoping", () => { it("scopes nested article routes to the current knowledge base and language", () => { for (const route of [ "bases.$slug/articles.$lang.$id.tsx", "bases.$slug/articles.$lang.$id.edit.tsx", "bases.$slug/articles.$lang.$id/settings.tsx", "bases.$slug/articles.$lang.tsx", "bases.$slug/categories.$lang.tsx", ]) { const source = readRoute(route); expect(source).toContain("getKbArticleByIdInKnowledgeBase"); expect(source).not.toMatch(/\bgetKbArticleById\(/); } }); it("scopes nested category and section route mutations to their parents", () => { const categoryRoute = readRoute("bases.$slug/categories.$lang/$id.tsx"); const sectionRoute = readRoute("bases.$slug/categories.$lang/$id.sections.$section.tsx"); const newSectionRoute = readRoute("bases.$slug/categories.$lang/$id.sections.new.tsx"); expect(categoryRoute).toContain("getKbCategoryByIdInKnowledgeBase"); expect(categoryRoute).toContain("updateKnowledgeBaseCategoryInKnowledgeBase"); expect(categoryRoute).toContain("deleteKnowledgeBaseCategoryInKnowledgeBase"); expect(categoryRoute).not.toMatch(/\bgetKbCategoryById\(/); expect(categoryRoute).not.toMatch(/\bupdateKnowledgeBaseCategory\(/); expect(categoryRoute).not.toMatch(/\bdeleteKnowledgeBaseCategory\(/); expect(sectionRoute).toContain("getKbCategorySectionByIdInCategory"); expect(sectionRoute).toContain("updateKnowledgeBaseCategorySectionInCategory"); expect(sectionRoute).toContain("deleteKnowledgeBaseCategorySectionInCategory"); expect(sectionRoute).not.toMatch(/\bgetKbCategorySectionById\(/); expect(sectionRoute).not.toMatch(/\bupdateKnowledgeBaseCategorySection\(/); expect(sectionRoute).not.toMatch(/\bdeleteKnowledgeBaseCategorySection\(/); expect(newSectionRoute).toContain("getKbCategoryByIdInKnowledgeBase"); expect(newSectionRoute).not.toMatch(/\bgetKbCategoryById\(/); }); it("does not return hardcoded English action response messages", () => { const responseLiteral = /(?:Response\.json|badRequest)\(\{\s*(?:error|success):\s*(?:"[^"]*[A-Za-z][^"]*"|'[^']*[A-Za-z][^']*')/; for (const { relativePath, source } of readRouteFiles()) { expect(source, relativePath).not.toMatch(responseLiteral); expect(source, relativePath).not.toContain("return Response.json({ error: e.message }"); expect(source, relativePath).not.toContain("badRequest({ error: error.message })"); } }); });