Files
co-lite/solvway-admin-portal/app/design-tokens.test.ts
T

42 lines
2.4 KiB
TypeScript

// polish-admin-controls: colours come only from the theme tokens
// "No component may contain a colour literal"). A Tailwind palette class (text-green-700,
// bg-amber-50, border-gray-300) or bg-white / text-white / border-black bypasses the tokens, so it
// ignores dark mode and a client re-skin. This test fails on any such class outside the allowlist.
import { readFileSync, globSync } from "fs";
import { resolve } from "path";
import { describe, expect, it } from "vitest";
const ROOT = resolve(__dirname, "..");
const FILES = globSync("app/**/*.tsx", { cwd: ROOT }).filter((f) => !/\.test\.tsx$/.test(f)).sort();
const PALETTE =
/(?<![\w-])(?:[\w[\]&=:.-]+:)?(?:text|bg|border|ring|ring-offset|from|via|to|fill|stroke|outline|divide|decoration|shadow|accent|caret|placeholder)-(?:slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-\d{2,3}(?:\/\d+)?(?![\w-])|(?<![\w-])(?:[\w[\]&=:.-]+:)?(?:bg|text|border)-(?:white|black)(?:\/\d+)?(?![\w-])/g;
// file -> classes that are allowed there, with the reason.
const ALLOW: Record<string, string[]> = {
// Modal scrims: a translucent black overlay is the same in both themes by design.
"app/components/ui/dialog.tsx": ["bg-black/50"],
"app/components/ui/sheet.tsx": ["bg-black/40"],
// Print-only hand-over sheet: printed on white paper regardless of theme.
"app/inventory/delivery-orders/pickup/[uuid]/page.tsx": ["border-black"],
// A QR code needs a light quiet zone to scan; a theme surface would put dark modules on dark in
// dark mode.
"app/pos/_components/SplitTenderPanel.tsx": ["bg-white"],
// A sent email's HTML is authored for a white mail-client canvas; a theme surface would put the
// email's dark text on dark in dark mode.
"app/email-templates/logs/[id]/page.tsx": ["bg-white"],
// Same reason for the saved-email detail (add-admin-email-listing).
"app/emails/[id]/page.tsx": ["bg-white"],
// The template preview frame shows the same email HTML (F09 moved it into a sandboxed iframe).
"app/email-templates/TemplatePreviewPane.tsx": ["bg-white"],
};
describe.each(FILES)("%s", (file) => {
it("uses theme tokens, not palette colour classes", () => {
const src = readFileSync(resolve(ROOT, file), "utf8");
const allowed = new Set(ALLOW[file] ?? []);
const found = [...new Set(src.match(PALETTE) ?? [])].filter((c) => !allowed.has(c.split(":").pop()!));
expect(found).toEqual([]);
});
});