mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
Two readings, side by side, never merged. The person's own rating leads — larger, first, and returned exactly as it was stored — and the comparison sits beside it with its range and the number of days it was built from. The deviation is measured against the range, not against its midpoint: inside it is a day that went as the account's own past days suggest, and only outside it is worth a sentence. The sentence is a counterfactual and says so. Never "your mood is 5.4", always "a value around 5.4 would have been expected". The explanation names what the comparison weighted for that day, out of what the job stored for that day, in the words the capture sheet already uses — a person reads "Workload" here because that is what the slider they moved was called. The route declares the whole record rather than the mind section, and the reason is written at the declaration: the comparison reads the day's context AND the figures the sleep, activity and recovery modules hold, and it names them, so a delegate holding only the mind section would receive a derived view of sections the owner declined to share. A scoped grant is refused outright rather than served a contribution list with the linked features quietly removed, which would read as "only the mood fields mattered" and be false. The causal-verb ban is a test now rather than a review habit: the copy guard reads all six bundles and fails on a causal construction in any of them, and on a statement that quotes a value without the days behind it. Both halves were watched failing.
377 lines
13 KiB
TypeScript
377 lines
13 KiB
TypeScript
/**
|
|
* Copy-matches-behaviour guard.
|
|
*
|
|
* Four classes of copy that must not drift away from the code they describe,
|
|
* each pinned here so the pair fails loudly if either half moves. The first
|
|
* three are repairs — copy that had already drifted. The fourth arrives with
|
|
* the feature it governs, because a wording rule that depends on review is a
|
|
* wording rule that survives until the fifth locale.
|
|
*
|
|
* 1. DELETE CONFIRMATIONS. A confirmation is the moment a user weighs the
|
|
* consequence. Four dialogs promised permanence on surfaces that render
|
|
* an Undo affordance (measurements bulk, mood bulk, labs, illness) — a
|
|
* cautious user declines a safe delete, and the warning loses weight
|
|
* where it is true. This test pins both directions: undo-able surfaces
|
|
* must offer the undo sentence and must NOT claim finality; the
|
|
* genuinely permanent ones must keep their warning.
|
|
*
|
|
* 2. SLEEP DEBT. v1.19.0 replaced the summed shortfall with a running
|
|
* balance that credits surplus sleep. The labels still described the old
|
|
* arithmetic, so the word contradicted the number for anyone who caught
|
|
* up after a short night.
|
|
*
|
|
* 3. GLUCOSE REFERENCE BAND. The declared diabetes opt-in has a server
|
|
* preference and a route; the copy for its web control must exist in
|
|
* every locale, since a missing key silently degrades to the raw key on
|
|
* a surface that makes a clinical-adjacent claim.
|
|
*
|
|
* Reversibility evidence for the undo-able set:
|
|
* - measurements: `POST /api/measurements/restore`, undo toast in
|
|
* `src/components/measurements/measurement-list.tsx`
|
|
* - mood: `POST /api/mood-entries/restore`, undo toast in
|
|
* `src/components/mood/mood-list.tsx`
|
|
* - labs: `POST /api/labs/restore`, undo toast in
|
|
* `src/components/labs/lab-history-list.tsx`
|
|
* - illness: `POST /api/illness/episodes/[id]/restore`, undo toast in
|
|
* `src/components/illness/episode-menu.tsx`
|
|
*/
|
|
import { readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
const MESSAGES = join(__dirname, "../../messages");
|
|
const LOCALES = ["de", "en", "es", "fr", "it", "pl"] as const;
|
|
|
|
function bundle(locale: string): Record<string, unknown> {
|
|
return JSON.parse(readFileSync(join(MESSAGES, `${locale}.json`), "utf8"));
|
|
}
|
|
|
|
function resolve(obj: Record<string, unknown>, path: string): string {
|
|
let cur: unknown = obj;
|
|
for (const seg of path.split(".")) {
|
|
expect(cur, `${path} is missing at "${seg}"`).toBeTypeOf("object");
|
|
cur = (cur as Record<string, unknown>)[seg];
|
|
}
|
|
expect(typeof cur, `${path} must resolve to a string`).toBe("string");
|
|
return cur as string;
|
|
}
|
|
|
|
/**
|
|
* Per-locale phrases that assert a deletion cannot be taken back. A dialog on
|
|
* an undo-able surface must contain none of these.
|
|
*/
|
|
const FINALITY_PHRASES: Record<string, readonly string[]> = {
|
|
en: ["cannot be undone", "can't be undone", "permanently", "permanent"],
|
|
de: ["nicht rückgängig", "endgültig", "dauerhaft", "unwiderruflich"],
|
|
es: ["no se puede deshacer", "permanente", "irreversible"],
|
|
fr: [
|
|
"irréversible",
|
|
"définitivement",
|
|
"définitive",
|
|
"ne peut pas être annulé",
|
|
],
|
|
it: [
|
|
"non può essere annullata",
|
|
"non può essere annullato",
|
|
"definitivamente",
|
|
"definitiva",
|
|
"permanente",
|
|
],
|
|
pl: ["nie można cofnąć", "nie można tego cofnąć", "trwale", "nieodwracaln"],
|
|
};
|
|
|
|
/** Per-locale phrase that promises the short undo window. */
|
|
const UNDO_PHRASES: Record<string, string> = {
|
|
en: "undo",
|
|
de: "rückgängig",
|
|
es: "deshacerlo",
|
|
fr: "annuler cette action",
|
|
it: "annullare",
|
|
pl: "cofnąć",
|
|
};
|
|
|
|
/** Confirmation bodies whose surface renders an Undo affordance. */
|
|
const UNDOABLE_CONFIRM_KEYS = [
|
|
"measurements.deleteConfirmDescription",
|
|
"measurements.bulkDeleteConfirmBody",
|
|
"mood.deleteConfirmDescription",
|
|
"mood.bulkDeleteConfirmBody",
|
|
"labs.deleteConfirmDescription",
|
|
"illness.deleteConfirm.body",
|
|
] as const;
|
|
|
|
/**
|
|
* Confirmation bodies whose delete really is unrecoverable for the user —
|
|
* hard delete or a tombstone with no restore path. These MUST keep a
|
|
* finality warning; softening them would be the mirror-image bug.
|
|
*/
|
|
const PERMANENT_CONFIRM_KEYS = [
|
|
// hard delete + cascade of every recorded value
|
|
"labs.biomarker.deleteConfirmDescription",
|
|
// `?purge=true` hard-deletes the tag off every past entry
|
|
"mood.manage.purgeBody",
|
|
// GDPR Art. 17 account purge
|
|
"settings.deleteAccountConfirmDescription",
|
|
// hard delete of every user-scoped row except the sign-in credentials
|
|
"settings.dangerZoneConfirmDescription",
|
|
// hard delete since v1.32.40 — the tombstone served nothing and the
|
|
// dialog already promised permanence
|
|
"measurementReminders.deleteConfirmDescription",
|
|
"records.allergies.deleteConfirmDescription",
|
|
] as const;
|
|
|
|
describe("delete confirmations describe what the surface actually does", () => {
|
|
for (const locale of LOCALES) {
|
|
const msgs = bundle(locale);
|
|
|
|
describe(locale, () => {
|
|
for (const key of UNDOABLE_CONFIRM_KEYS) {
|
|
it(`${key} offers undo and claims no finality`, () => {
|
|
const copy = resolve(msgs, key).toLowerCase();
|
|
|
|
for (const phrase of FINALITY_PHRASES[locale]) {
|
|
expect(
|
|
copy,
|
|
`${locale}/${key} claims finality ("${phrase}") but the surface renders an Undo`,
|
|
).not.toContain(phrase.toLowerCase());
|
|
}
|
|
|
|
expect(
|
|
copy,
|
|
`${locale}/${key} should tell the user the delete can be undone`,
|
|
).toContain(UNDO_PHRASES[locale].toLowerCase());
|
|
});
|
|
}
|
|
|
|
for (const key of PERMANENT_CONFIRM_KEYS) {
|
|
it(`${key} keeps its finality warning`, () => {
|
|
const copy = resolve(msgs, key).toLowerCase();
|
|
const warns = FINALITY_PHRASES[locale].some((p) =>
|
|
copy.includes(p.toLowerCase()),
|
|
);
|
|
expect(
|
|
warns,
|
|
`${locale}/${key} deletes irrecoverably — the copy must say so`,
|
|
).toBe(true);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
describe("sleep debt is labelled as a balance, not a summed deficit", () => {
|
|
/**
|
|
* The figure is a running balance: a short night adds, a long night pays it
|
|
* down. Deficit/shortfall vocabulary describes the pre-v1.19.0 sum.
|
|
*/
|
|
const STALE_SUM_VOCABULARY: Record<string, readonly string[]> = {
|
|
en: ["cumulative", "shortfall", "accumulated"],
|
|
de: ["kumuliert", "defizit", "aufgelaufen"],
|
|
es: ["acumulado", "acumulada", "déficit"],
|
|
fr: ["cumulé", "cumulée", "déficit"],
|
|
it: ["cumulato", "cumulata", "accumulato", "deficit"],
|
|
pl: ["skumulowan", "niedobór", "niedoboru"],
|
|
};
|
|
|
|
for (const locale of LOCALES) {
|
|
const msgs = bundle(locale);
|
|
|
|
it(`${locale} debt captions carry no summed-deficit wording`, () => {
|
|
for (const key of [
|
|
"insights.sleep.debt.caption",
|
|
"insights.sleep.debt.clearCaption",
|
|
]) {
|
|
const copy = resolve(msgs, key).toLowerCase();
|
|
for (const term of STALE_SUM_VOCABULARY[locale]) {
|
|
expect(
|
|
copy,
|
|
`${locale}/${key} still describes the pre-v1.19.0 summed shortfall ("${term}")`,
|
|
).not.toContain(term);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
it("the explainer states that catch-up sleep pays the balance down", () => {
|
|
const copy = resolve(bundle("en"), "insights.sleep.debt.computedInfo");
|
|
expect(copy.toLowerCase()).toContain("balance");
|
|
expect(copy.toLowerCase()).toContain("catch-up");
|
|
});
|
|
});
|
|
|
|
describe("the glucose reference band control is translated everywhere", () => {
|
|
const KEYS = [
|
|
"settings.glucoseReference.title",
|
|
"settings.glucoseReference.description",
|
|
"settings.glucoseReference.enable",
|
|
"settings.glucoseReference.explainer",
|
|
"settings.glucoseReference.disclaimer",
|
|
"settings.glucoseReference.error",
|
|
] as const;
|
|
|
|
for (const locale of LOCALES) {
|
|
it(`${locale} carries every key`, () => {
|
|
const msgs = bundle(locale);
|
|
for (const key of KEYS) {
|
|
expect(resolve(msgs, key).length).toBeGreaterThan(0);
|
|
}
|
|
});
|
|
}
|
|
|
|
it("the disclaimer refuses the diagnosis reading (EN)", () => {
|
|
const copy = resolve(
|
|
bundle("en"),
|
|
"settings.glucoseReference.disclaimer",
|
|
).toLowerCase();
|
|
expect(copy).toContain("not a diagnosis");
|
|
expect(copy).toContain("never inferred");
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 4. THE MOOD PROGNOSIS. The forecast is a comparison with somebody's own past
|
|
* days and it may never be phrased as a cause. "A value around 5.4 would
|
|
* have been expected" is what the arithmetic supports; "overtime lowered
|
|
* your mood" is not, and the difference is not a matter of taste — the model
|
|
* is a correlation over a dozen columns and a causal sentence would be a
|
|
* claim it cannot make about somebody's health.
|
|
*
|
|
* The second half is the count. A forecast built from thirty days and one
|
|
* built from three hundred render identically, so every statement that
|
|
* quotes a value has to quote what it rests on. Both halves are a test here
|
|
* rather than a review habit, because a review habit does not survive the
|
|
* fifth locale.
|
|
*/
|
|
describe("the mood prognosis never states a cause", () => {
|
|
/**
|
|
* Causal constructions, per locale. Multi-word where a single word would
|
|
* over-match — Italian `causa` is a noun in ordinary use and only `a causa
|
|
* di` is the construction being banned.
|
|
*/
|
|
const CAUSAL_PHRASES: Record<string, readonly string[]> = {
|
|
de: [
|
|
"weil",
|
|
"verursacht",
|
|
"führt zu",
|
|
"sorgt für",
|
|
"wegen",
|
|
"verbessert deine",
|
|
"verschlechtert deine",
|
|
"senkt deine",
|
|
"hebt deine",
|
|
],
|
|
en: [
|
|
"because",
|
|
"causes",
|
|
"caused",
|
|
"leads to",
|
|
"due to",
|
|
"makes you",
|
|
"lowers your",
|
|
"raises your",
|
|
"improves your",
|
|
"worsens your",
|
|
],
|
|
es: [
|
|
"porque",
|
|
"provoca",
|
|
"debido a",
|
|
"a causa de",
|
|
"mejora tu",
|
|
"empeora tu",
|
|
],
|
|
fr: [
|
|
"parce que",
|
|
"provoque",
|
|
"en raison de",
|
|
"à cause de",
|
|
"améliore ton",
|
|
"dégrade ton",
|
|
],
|
|
it: [
|
|
"perché",
|
|
"provoca",
|
|
"a causa di",
|
|
"migliora il tuo",
|
|
"peggiora il tuo",
|
|
],
|
|
pl: ["ponieważ", "powoduje", "z powodu", "wywołuje", "poprawia twój"],
|
|
};
|
|
|
|
/** Every leaf string under `insights.mood.prognosis`, flattened. */
|
|
function prognosisStrings(locale: string): Array<[string, string]> {
|
|
const root = (
|
|
bundle(locale).insights as Record<string, Record<string, unknown>>
|
|
).mood.prognosis as Record<string, unknown>;
|
|
expect(
|
|
root,
|
|
`${locale} carries no insights.mood.prognosis block`,
|
|
).toBeTypeOf("object");
|
|
return Object.entries(root).filter(
|
|
(entry): entry is [string, string] => typeof entry[1] === "string",
|
|
);
|
|
}
|
|
|
|
for (const locale of LOCALES) {
|
|
it(`${locale} carries the block, and every string in it`, () => {
|
|
const strings = prognosisStrings(locale);
|
|
// The positive control. Every assertion below passes on an empty list,
|
|
// so the count is the evidence and the empty offender set is only the
|
|
// verdict.
|
|
expect(strings.length).toBeGreaterThanOrEqual(17);
|
|
for (const [key, value] of strings) {
|
|
expect(value.length, `${locale}/${key} is empty`).toBeGreaterThan(0);
|
|
}
|
|
});
|
|
|
|
it(`${locale} names no cause`, () => {
|
|
const offenders = prognosisStrings(locale)
|
|
.filter(([, value]) =>
|
|
CAUSAL_PHRASES[locale].some((phrase) =>
|
|
value.toLowerCase().includes(phrase),
|
|
),
|
|
)
|
|
.map(([key]) => key);
|
|
expect(
|
|
offenders,
|
|
`${locale} prognosis copy states a cause; the forecast is a comparison with the account's own past days and cannot support one`,
|
|
).toEqual([]);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Statements that quote a value must quote what it rests on. The list is
|
|
* explicit rather than derived, so adding a statement key forces a decision
|
|
* here instead of inheriting silence.
|
|
*/
|
|
const COUNTED_STATEMENTS: Record<string, readonly string[]> = {
|
|
statement: ["{value}", "{n}"],
|
|
learning: ["{entries}", "{threshold}"],
|
|
noPattern: ["{entries}"],
|
|
band: ["{low}", "{high}"],
|
|
};
|
|
|
|
for (const locale of LOCALES) {
|
|
it(`${locale} keeps the count beside the claim`, () => {
|
|
const strings = new Map(prognosisStrings(locale));
|
|
for (const [key, placeholders] of Object.entries(COUNTED_STATEMENTS)) {
|
|
const copy = strings.get(key);
|
|
expect(copy, `${locale}/${key} is missing`).toBeTypeOf("string");
|
|
for (const placeholder of placeholders) {
|
|
expect(
|
|
copy,
|
|
`${locale}/${key} quotes a value without ${placeholder}; a forecast without the days behind it cannot be weighed by the reader`,
|
|
).toContain(placeholder);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
it("the forecast is phrased as a counterfactual, not as a measurement (EN + DE)", () => {
|
|
const en = resolve(bundle("en"), "insights.mood.prognosis.statement");
|
|
expect(en.toLowerCase()).toContain("would have been expected");
|
|
const de = resolve(bundle("de"), "insights.mood.prognosis.statement");
|
|
expect(de.toLowerCase()).toContain("zu erwarten gewesen");
|
|
});
|
|
});
|