107 lines
3.7 KiB
JavaScript
107 lines
3.7 KiB
JavaScript
import { describe, expect, test } from "vitest";
|
|
import { extractHardcodedStrings, extractIntlFormattingUsages, renderMarkdown } from "./i18n-audit.mjs";
|
|
|
|
describe("i18n-audit renderMarkdown", () => {
|
|
test("renders a clean baseline when there are no hits", () => {
|
|
const md = renderMarkdown(
|
|
{ scannedFileCount: 5, fileHitCount: 0, totalHardcodedStrings: 0, intlFileCount: 0, totalIntlFormattingUsages: 0, files: [], intlFiles: [] },
|
|
new Date("2026-05-17T00:00:00Z"),
|
|
);
|
|
expect(md).toContain("# i18n Audit — Baseline 2026-05-17");
|
|
expect(md).toContain("No hardcoded strings detected. Audit clean.");
|
|
expect(md).toContain("apps/web/app/{routes,modules,routeModules,components}/**/*.{ts,tsx}");
|
|
});
|
|
|
|
test("includes top-offender + detail tables when hits exist", () => {
|
|
const md = renderMarkdown(
|
|
{
|
|
scannedFileCount: 2,
|
|
fileHitCount: 1,
|
|
totalHardcodedStrings: 2,
|
|
intlFileCount: 0,
|
|
totalIntlFormattingUsages: 0,
|
|
files: [
|
|
{
|
|
filePath: "apps/web/app/components/Foo.tsx",
|
|
hits: [
|
|
{ line: 12, text: "Save changes" },
|
|
{ line: 34, text: "Cancel" },
|
|
],
|
|
},
|
|
],
|
|
intlFiles: [],
|
|
},
|
|
new Date("2026-05-17T00:00:00Z"),
|
|
);
|
|
expect(md).toContain("| `apps/web/app/components/Foo.tsx` | 2 |");
|
|
expect(md).toContain("Save changes");
|
|
expect(md).toContain("Cancel");
|
|
});
|
|
|
|
test("reports locale-sensitive formatting signals separately from hardcoded JSX text", () => {
|
|
const md = renderMarkdown(
|
|
{
|
|
scannedFileCount: 1,
|
|
fileHitCount: 0,
|
|
totalHardcodedStrings: 0,
|
|
intlFileCount: 1,
|
|
totalIntlFormattingUsages: 3,
|
|
files: [],
|
|
intlFiles: [
|
|
{
|
|
filePath: "apps/web/app/routes/admin.tsx",
|
|
hits: [
|
|
{ line: 10, expression: "toLocaleString" },
|
|
{ line: 11, expression: "DateUtils.dateYMD" },
|
|
{ line: 12, expression: "NumberUtils.intFormat" },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
new Date("2026-05-17T00:00:00Z"),
|
|
);
|
|
|
|
expect(md).toContain("| Files with locale-sensitive formatting signals | 1 |");
|
|
expect(md).toContain("## Formatting migration signals");
|
|
expect(md).toContain("| `apps/web/app/routes/admin.tsx:10` | `toLocaleString` |");
|
|
expect(md).toContain("| `apps/web/app/routes/admin.tsx:11` | `DateUtils.dateYMD` |");
|
|
expect(md).toContain("| `apps/web/app/routes/admin.tsx:12` | `NumberUtils.intFormat` |");
|
|
});
|
|
});
|
|
|
|
describe("i18n-audit extractHardcodedStrings", () => {
|
|
test("detects visible JSX text", () => {
|
|
expect(extractHardcodedStrings("<h1>Visible heading</h1>")).toEqual([{ line: 1, text: "Visible heading" }]);
|
|
});
|
|
|
|
test("ignores html fragments inside string literals", () => {
|
|
expect(
|
|
extractHardcodedStrings(
|
|
[
|
|
'const html = "<p>Watch the demo video:</p>";',
|
|
"const body = '<strong>It works!</strong>';",
|
|
"const template = `<section>Configure account</section>`;",
|
|
].join("\n"),
|
|
),
|
|
).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("i18n-audit extractIntlFormattingUsages", () => {
|
|
test("finds toLocale, DateUtils, and NumberUtils usages", () => {
|
|
expect(
|
|
extractIntlFormattingUsages(
|
|
[
|
|
"const amount = value.toLocaleString();",
|
|
"const date = DateUtils.dateYMD(createdAt);",
|
|
"const count = NumberUtils.intFormat(total);",
|
|
].join("\n"),
|
|
),
|
|
).toEqual([
|
|
{ line: 1, expression: "toLocaleString" },
|
|
{ line: 2, expression: "DateUtils.dateYMD" },
|
|
{ line: 3, expression: "NumberUtils.intFormat" },
|
|
]);
|
|
});
|
|
});
|