Files
co-lite/solvway-admin-portal/app/lib/i18n/hardcodedCopy.sweep.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

76 lines
3.4 KiB
TypeScript

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<string>(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 = () => <p title="Hi">Free shipping</p>;`))
.toEqual(["1: title=\"Hi\"", "1: Free shipping"]);
expect(hardcodedCopy("x.tsx", `const A = () => <IconButton name="x" label="Close" />;`))
.toEqual(["1: label=\"Close\""]);
expect(hardcodedCopy("x.tsx", `const A = () => <p title={t("Hi")}>{t("FreeShipping")} &nbsp;·</p>;`))
.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([]);
});
});