VNOX-Web/tests/copy.test.ts
loki5512344 d3a6dd316b
feat(web): rewrite site copy in plain language and split i18n dictionaries
Move RU/EN strings to src/i18n/{ru,en}.ts, add og/meta tags that follow the
selected language, remove the long dash from all text, add copy guidelines and
tests for key parity, HTML fallback text and dash-free sources and bundle.
2026-10-24 16:55:05 +02:00

102 lines
3.8 KiB
TypeScript

import { test } from "bun:test";
import assert from "node:assert/strict";
import { readdir, readFile, stat } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { en } from "../src/i18n/en";
import { ru } from "../src/i18n/ru";
import { messages, languages } from "../src/i18n";
import { browserBundle } from "../scripts/browser";
const root = fileURLToPath(new URL("../", import.meta.url));
const LONG_DASH = String.fromCharCode(0x2014);
const text = (path: string) => readFile(root + path, "utf8");
async function walk(dir: string): Promise<string[]> {
const out: string[] = [];
for (const entry of await readdir(root + dir, { withFileTypes: true })) {
const path = `${dir}/${entry.name}`;
if (entry.isDirectory()) out.push(...(await walk(path)));
else if (/\.(ts|css|html|md|svg|conf|py|json)$/.test(entry.name))
out.push(path);
}
return out;
}
test("ru and en dictionaries have identical, non-empty keys", () => {
assert.deepEqual(Object.keys(ru).sort(), Object.keys(en).sort());
assert.deepEqual([...languages].sort(), ["en", "ru"]);
for (const catalog of Object.values(messages))
for (const [key, value] of Object.entries(catalog))
assert.ok(value.trim().length > 0, key);
});
test("every translation key used in code exists and every key is used", async () => {
const code = (await text("src/app.ts")) + (await text("public/index.html"));
const used = new Set<string>();
for (const m of code.matchAll(
/(?:data-i18n(?:-aria|-alt)?="|\btr\(\s*|\bt\(\s*)["`]?([A-Za-z]+)/g,
))
used.add(m[1]!);
// Keys built from a template, e.g. faq${key} and faq${key}Answer.
for (const part of ["Server", "Central", "Release"]) {
used.add(`faq${part}`);
used.add(`faq${part}Answer`);
}
for (const m of code.matchAll(/\b(?:tr|t)\(\s*"([A-Za-z]+)"/g)) used.add(m[1]!);
for (const m of code.matchAll(/\(\[((?:"[a-z]+",?\s*)+)\] as const\)/g))
for (const k of m[1]!.matchAll(/"([a-z]+)"/g)) used.add(k[1]!);
for (const key of Object.keys(en)) {
if (!used.has(key)) {
// Keys chosen through ternaries are still named literally in app.ts.
assert.ok(code.includes(`"${key}"`), `unused key: ${key}`);
}
}
for (const key of used)
if (/^[a-z][A-Za-z]+$/.test(key) && !(key in en)) {
// Ignore non-key words matched by the loose regex.
assert.ok(
!code.includes(`data-i18n="${key}"`),
`missing key in dictionary: ${key}`,
);
}
});
test("static HTML fallback text matches the Russian dictionary", async () => {
const html = await text("public/index.html");
const flat = (value: string) => value.replace(/\s+/g, " ").trim();
for (const m of html.matchAll(
/data-i18n="([A-Za-z]+)"[^>]*>([^<]*)</g,
)) {
const key = m[1] as keyof typeof ru;
assert.ok(key in ru, key);
assert.equal(flat(m[2]!), flat(ru[key]), `fallback for ${key}`);
}
assert.ok(html.includes(`<title>${ru.title}</title>`));
assert.ok(html.includes(`content="${ru.description}"`));
assert.ok(html.includes(`property="og:title"\n content="${ru.title}"`));
assert.match(html, /<html lang="ru">/);
});
test("the long dash is absent from sources, docs, public/ and the browser bundle", async () => {
const files = [
"README.md",
...(await walk("src")),
...(await walk("public")),
...(await walk("docs")),
...(await walk("tests")),
...(await walk("scripts")),
];
const bad: string[] = [];
for (const file of files)
if ((await text(file)).includes(LONG_DASH)) bad.push(file);
const bundle = await (await browserBundle(true)).text();
if (bundle.includes(LONG_DASH)) bad.push("browser bundle");
try {
await stat(root + "dist");
for (const file of await walk("dist"))
if ((await text(file)).includes(LONG_DASH)) bad.push(file);
} catch {
/* dist/ is optional. */
}
assert.deepEqual(bad, []);
});