import { describe, it, expect } from "vitest"; import { readdirSync, readFileSync, statSync } from "node:fs"; import { join, relative } from "node:path"; import ts from "typescript"; import { DEFAULT_MESSAGES } from "./defaultMessages"; import BASELINE from "./hardcodedCopy.baseline.json"; // add-portal-i18n-guard: user-visible copy goes through t(code) so admins can edit and translate // it in /messages. A non-test .tsx with JSX text or a literal copy attribute fails, unless it is in // the baseline of files not yet migrated. The baseline only shrinks: a migrated file must leave it. const ROOT = join(__dirname, "..", ".."); // solvway-admin-portal/app const COPY_ATTRS = new Set(["placeholder", "aria-label", "title", "alt", "label"]); const HAS_LETTER = /\p{L}/u; function sources(dir: string, ext: RegExp): string[] { return readdirSync(dir).flatMap((name) => { const p = join(dir, name); if (statSync(p).isDirectory()) return name === "node_modules" ? [] : sources(p, ext); return ext.test(name) && !/\.test\.tsx?$/.test(name) ? [p] : []; }); } /** `line: text` for every hardcoded copy literal in a .tsx source. */ function hardcodedCopy(file: string, text: string): string[] { const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX); const hits: string[] = []; const hit = (node: ts.Node, copy: string) => hits.push(`${sf.getLineAndCharacterOfPosition(node.getStart()).line + 1}: ${copy}`); const visit = (node: ts.Node) => { if (ts.isJsxText(node)) { const copy = node.text.replace(/&\w+;/g, "").trim(); if (HAS_LETTER.test(copy)) hit(node, copy); } else if (ts.isJsxAttribute(node) && COPY_ATTRS.has(node.name.getText()) && node.initializer && ts.isStringLiteral(node.initializer) && HAS_LETTER.test(node.initializer.text)) { hit(node, node.getText()); } ts.forEachChild(node, visit); }; visit(sf); return hits; } describe("hardcoded copy sweep", () => { const files = sources(ROOT, /\.tsx$/).map((f) => relative(ROOT, f)); const baseline = new Set(BASELINE); const flagged = new Map(files.map((f) => [f, hardcodedCopy(f, readFileSync(join(ROOT, f), "utf8"))])); it("flags JSX text and literal copy attributes, not t() calls", () => { expect(hardcodedCopy("x.tsx", `const A = () =>

Free shipping

;`)) .toEqual(["1: title=\"Hi\"", "1: Free shipping"]); expect(hardcodedCopy("x.tsx", `const A = () => ;`)) .toEqual(["1: label=\"Close\""]); expect(hardcodedCopy("x.tsx", `const A = () =>

{t("FreeShipping")}  ยท

;`)) .toEqual([]); }); it("no file outside the baseline hardcodes copy (use t(code) + DEFAULT_MESSAGES)", () => { const hits = [...flagged].filter(([f]) => !baseline.has(f)) .flatMap(([f, h]) => h.map((line) => `${f}:${line}`)); expect(hits).toEqual([]); }); it("baseline lists only files that still hardcode copy (remove migrated files)", () => { expect([...baseline].filter((f) => !flagged.get(f)?.length)).toEqual([]); }); it("every t(\"Code\") has an English default in DEFAULT_MESSAGES", () => { const missing = sources(ROOT, /\.tsx?$/).flatMap((f) => [...readFileSync(f, "utf8").matchAll(/\bt\(\s*["']([A-Za-z0-9_.]+)["']/g)] .filter((m) => !(m[1] in DEFAULT_MESSAGES)) .map((m) => `${relative(ROOT, f)}: ${m[1]}`)); expect(missing).toEqual([]); }); });