import { describe, expect, it } from "vitest"; import fs from "node:fs"; import path from "node:path"; const repoRoot = process.cwd(); const routesDir = path.join(repoRoot, "apps/web/app/routes/app.$tenant"); function read(relative: string): string { return fs.readFileSync(path.join(routesDir, relative), "utf8"); } function localeCrm(locale: string): Record { const json = JSON.parse(fs.readFileSync(path.join(repoRoot, `apps/web/public/locales/${locale}/translations.json`), "utf8")); return json.crm as Record; } function hasKey(root: Record, dotted: string): boolean { // `dotted` is a `crm.…` key; resolve the part after the `crm.` prefix. const segments = dotted.replace(/^crm\./, "").split("."); let node: unknown = root; for (const segment of segments) { if (!node || typeof node !== "object") return false; node = (node as Record)[segment]; } return typeof node === "string"; } describe("CRM contact routes (R-P6 slice 4)", () => { it("build the list/detail view-models server-side via the shared loaders", () => { const list = read("contacts._index.tsx"); expect(list).toContain("export const loader"); expect(list).toContain("export const action"); expect(list).toContain("loadKontaktListData"); expect(list).toContain("handleKontaktListAction"); // The route may own a fetcher (the create dialog's live duplicate check posts // to the new-route via useFetcher and the result is injected as a prop). The // layer rule (DoD A.5) applies to the presentational screen, not the route: // the dialog component itself must not call useFetcher / useLoaderData. // Match real hook calls (`useFetcher(` / `useFetcher<`) with no space, so the // "No useLoaderData / useFetcher (DoD A.5)" comment in the file is not a hit. const newDialog = fs.readFileSync(path.join(repoRoot, "apps/web/app/components/screens/crm/KontaktNewDialog.tsx"), "utf8"); expect(newDialog).not.toMatch(/useFetcher[(<]/); expect(newDialog).not.toMatch(/useLoaderData[(<]/); const companies = read("contacts.companies._index.tsx"); expect(companies).toContain('variant: "companies"'); const detail = read("contacts.$id.tsx"); expect(detail).toContain("loadKontaktDetailData"); expect(detail).toContain("handleKontaktDetailAction"); const newRoute = read("contacts.new.tsx"); expect(newRoute).toContain('intent === "checkDuplicates"'); expect(newRoute).toContain("findDuplicateKontakte"); const exportRoute = read("contacts.export.tsx"); expect(exportRoute).toContain("buildKontaktCsv"); expect(exportRoute).toContain("text/csv"); }); it("gates admin routes on crm.admin and the lists on crm.read_own", () => { expect(read("contacts._index.tsx") + read("contacts.$id.tsx")).toContain("crm.read_own"); for (const file of ["contacts.admin.custom-fields.tsx", "contacts.admin.tags.tsx", "contacts.admin.duplicates.tsx"]) { expect(read(file)).toContain('requireCrmPermission(ctx, "crm.admin")'); } }); it("has de + en translations for the keys the routes reference", () => { const keys = [ "crm.contacts.title", "crm.companies.title", "crm.new.title", "crm.locked.headline", "crm.empty.contacts.headline", "crm.kontaktTyp.privatperson", "crm.kontaktStatus.kunde", "crm.orgMerkmal.gmbh", "crm.kontaktArt.hauptkontakt", "crm.datentyp.waehrung", "crm.activity.contact_created", "crm.duplicate.reason.email", "crm.duplicate.matchType.hard", "crm.error.nameRequired", "crm.error.tagNameExists", "crm.detail.section.activity", "crm.admin.customFields.title", "crm.admin.tags.usage", "crm.pagination.page", ]; const de = localeCrm("de"); const en = localeCrm("en"); for (const key of keys) { expect(hasKey(de, key), `de missing ${key}`).toBe(true); expect(hasKey(en, key), `en missing ${key}`).toBe(true); } }); });