inat/test/i18n.test.ts
2026-11-21 14:02:32 +00:00

62 lines
2.3 KiB
TypeScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { describe, it } from "node:test";
import {
LOCALES,
LOCALES_DIR,
loadMessages,
REQUIRED_KEYS,
validateMessages,
} from "../src/i18n.ts";
describe("Требование: файлы переводов на трёх языках", () => {
const ruKeys = Object.keys(JSON.parse(readFileSync(`${LOCALES_DIR}/ru.json`, "utf8"))).sort();
for (const locale of LOCALES) {
it(`Scenario Outline: полный файл перевода — ${locale}`, () => {
const data: unknown = JSON.parse(readFileSync(`${LOCALES_DIR}/${locale}.json`, "utf8"));
assert.deepEqual(validateMessages(data), []);
assert.deepEqual(Object.keys(data as object).sort(), ruKeys);
const m = loadMessages(locale);
for (const key of [
"about.text",
"services.text",
"contacts.text",
"meta.title",
"meta.description",
] as const) {
assert.ok(m[key].trim().length > 0, `${locale}: ${key} is empty`);
}
});
}
it("Scenario: пропущенный ключ", () => {
const ru = loadMessages("ru") as Record<string, string>;
const { "contacts.heading": _removed, ...broken } = ru;
assert.deepEqual(validateMessages(broken), ["missing key contacts.heading"]);
});
it("пустое значение и лишний ключ тоже считаются ошибкой", () => {
const ru = loadMessages("ru") as Record<string, string>;
const problems = validateMessages({ ...ru, "about.text": " ", "extra.key": "x" });
assert.deepEqual(problems, ["empty key about.text", "unknown key extra.key"]);
assert.deepEqual(validateMessages([]), ["file must contain a JSON object"]);
});
it("заглушки помечены во всех языках", () => {
const markers = { ru: "[ЗАГЛУШКА]", kk: "[ҮЛГІ]", en: "[PLACEHOLDER]" } as const;
const placeholderKeys = REQUIRED_KEYS.filter(
(k) => !k.endsWith("heading") && !k.endsWith("Label") && !k.startsWith("notFound."),
);
for (const locale of LOCALES) {
const m = loadMessages(locale);
for (const key of placeholderKeys) {
assert.ok(
m[key].startsWith(markers[locale]),
`${locale}: ${key} has no placeholder marker`,
);
}
}
});
});