Files
HealthLog/src/lib/mood/__tests__/context-vocabulary.test.ts
T
l1137l 48bfbad4e6 fix(i18n): address the ru review — units, Cyrillic regexes, locale registry
Dose bank
- DRUG_UNIT learns мг / мкг / мл / ЕД / МЕ, so "2,4 мг" anchors a dose again.
- The bank matches verb-first Russian word order too ("принимайте 2 таблетки",
  "пропустите дозу метформина", "начните с 5 мг"), and the Cyrillic drug names
  people actually see on the box; the deferral words ("спросите врача",
  "как назначено") stand those shapes down.

Cyrillic-safe regexes
- \w and \b are ASCII in JavaScript: every stem in the ru bank and in
  population-norms/ru.ts is [а-яё]*, and the word edges are lookarounds
  (?<![а-яёa-z]), so «высокий риск» and «нормальный диапазон» match again.
- coach-prose-grounding: Russian month names in the genitive, a space as the
  thousands separator in parseLocaleNumber and numFrag, and Cyrillic-safe
  edges around the written-date pattern.

Registered everywhere a locale has to be
- targetLanguageName() returns "Russian", so the coach, the period narrative
  and the document descriptions stop saying "write in English".
- i18n-key-tool.mjs, split-description.mjs, LOINC name normalisation (it
  stripped Cyrillic, so no Russian lab name resolved), HOST_TLDS (ru/by/kz/ua),
  no-key-fallbacks (32 tables), country-label, the AUDIT-C standard drink.
- docs/api/openapi.yaml regenerated.

Questionnaires
- The PHQ-9 / GAD-7 source comments no longer claim no validated Russian
  version exists (Zolotareva 2023; Pfizer's official screeners).
- WHO-5 items are gender-neutral: «чувствовал(а)».
- "body fat" and "fat mass" both read «Жировая масса»; body fat is now
  «Процент жировой массы», which is what the two metrics measure.

Tests
- ru in the 13 locale tables, plus mood, onboarding, instruments, clinician
  view, status summary, describe screen, citation coverage and LOINC.
- i18n-no-dot-separator: the ru bundle is exempt from the dash rule (a spaced
  dash is Russian punctuation, not a fragment join); the middle dot still
  fails there.
- Two 800-line baselines raised by the 12 and 5 lines this change adds.

pnpm typecheck, pnpm openapi:check and the full suite (36 316 tests) pass.
2026-11-02 20:22:03 +00:00

145 lines
5.2 KiB
TypeScript

/**
* Pair guard binding `context-vocabulary.ts` to the six locale bundles.
*
* The generic i18n guards cannot carry this one. `contextValueLabelKey` builds
* its keys from a template with a literal `mood.context.` prefix, so the
* reverse-coverage matcher records the whole subtree as reachable and stops
* being able to tell a live label from a stale one — the exact degradation
* that file's own doc comment warns about, arriving here by construction
* rather than by accident. So the check that matters is written here, against
* the arrays themselves, in both directions:
*
* * every key the vocabulary names has a non-empty label in all six locales;
* * every label under those groups belongs to a key the vocabulary names.
*
* The second direction is the one that catches a value removed from the
* vocabulary whose translations were left behind, and the first is the one
* that catches a value added without them.
*
* Mutation check: drop `"garden"` from `LEISURE_CATEGORY_KEYS` and the orphan
* assertion goes red naming it in all six bundles; delete
* `mood.context.eventType.conflict` from `messages/pl.json` and the coverage
* assertion goes red naming pl.
*/
import { readFileSync, readdirSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
CONTACT_CIRCLE_KEYS,
CONTACT_EXTENT_KEYS,
CONTACT_FORM_KEYS,
CONTEXT_SECTION_KEYS,
EVENT_TYPE_KEYS,
LEISURE_CATEGORY_KEYS,
WORK_STATUS_KEYS,
CONTEXT_RATING_FIELDS,
contextSectionLabelKey,
contextValueLabelKey,
} from "@/lib/mood/context-vocabulary";
const MESSAGES = join(__dirname, "../../../../messages");
const LOCALES = readdirSync(MESSAGES)
.filter((f) => f.endsWith(".json"))
.map((f) => f.replace(/\.json$/, ""));
function bundle(locale: string): Record<string, unknown> {
return JSON.parse(
readFileSync(join(MESSAGES, `${locale}.json`), "utf8"),
) as Record<string, unknown>;
}
function resolve(root: Record<string, unknown>, key: string): unknown {
return key
.split(".")
.reduce<unknown>(
(node, part) =>
node && typeof node === "object"
? (node as Record<string, unknown>)[part]
: undefined,
root,
);
}
/** The value groups whose members come straight from a vocabulary array. */
const VALUE_GROUPS: Array<{ group: string; keys: readonly string[] }> = [
{ group: "workStatus", keys: WORK_STATUS_KEYS },
{ group: "contactCircles", keys: CONTACT_CIRCLE_KEYS },
{ group: "contactForm", keys: CONTACT_FORM_KEYS },
{ group: "contactExtent", keys: CONTACT_EXTENT_KEYS },
{ group: "leisureCategories", keys: LEISURE_CATEGORY_KEYS },
{ group: "eventType", keys: EVENT_TYPE_KEYS },
];
describe("mood context vocabulary labels", () => {
it("reads ten locales and a non-empty vocabulary", () => {
// Both ends have to be there before anything below proves anything. A
// guard that walks an empty list is a green light nobody earned.
expect(LOCALES.length).toBe(10);
expect(VALUE_GROUPS.length).toBeGreaterThan(0);
const total = VALUE_GROUPS.reduce((n, g) => n + g.keys.length, 0);
expect(total).toBeGreaterThan(30);
expect(CONTEXT_RATING_FIELDS.length).toBeGreaterThan(0);
});
for (const locale of LOCALES) {
it(`${locale} carries a label for every vocabulary value`, () => {
const root = bundle(locale);
const missing: string[] = [];
for (const { group, keys } of VALUE_GROUPS) {
for (const key of keys) {
const label = resolve(root, contextValueLabelKey(group, key));
if (typeof label !== "string" || label.trim() === "") {
missing.push(contextValueLabelKey(group, key));
}
}
}
for (const section of CONTEXT_SECTION_KEYS) {
const label = resolve(root, contextSectionLabelKey(section));
if (typeof label !== "string" || label.trim() === "") {
missing.push(contextSectionLabelKey(section));
}
}
for (const field of CONTEXT_RATING_FIELDS) {
for (const anchor of ["low", "high"] as const) {
const key = `mood.context.rating.${field}.${anchor}`;
const label = resolve(root, key);
if (typeof label !== "string" || label.trim() === "") {
missing.push(key);
}
}
}
expect(
missing,
`messages/${locale}.json is missing ${missing.length} context label(s)`,
).toEqual([]);
});
it(`${locale} carries no label the vocabulary no longer names`, () => {
const root = bundle(locale);
const orphans: string[] = [];
for (const { group, keys } of VALUE_GROUPS) {
const node = resolve(root, `mood.context.${group}`);
expect(
node && typeof node === "object",
`mood.context.${group} is absent from ${locale} — this matcher would prove nothing`,
).toBe(true);
for (const present of Object.keys(node as Record<string, unknown>)) {
if (!keys.includes(present)) {
orphans.push(`mood.context.${group}.${present}`);
}
}
}
expect(
orphans,
`messages/${locale}.json carries ${orphans.length} label(s) for values the vocabulary does not name`,
).toEqual([]);
});
}
});