Non-blocking suggestions of the independent reviews of PRs #490 to #493, applied in a follow-up link above #493 (Max's decision of 2026-10-24): - locales.keys.test.ts: a variable name that differs by its case only is a gap (`{{version}}` against `{{Version}}`, review 27037, H8); the header says that `{{- name}}` and `{{name}}` render alike with `escapeValue: false`. - db.test.ts: a `)` glued to a word ends no path (`C:\Users\jean\Projet(2024)final\x.db`, review 27073, R10). - TemplateActions.delete.test.ts: three templates, the middle one deleted, so a handler bound to the first or the last fails (review 27088, R11). - ProfileSwitcher.wiring.test.ts: a locked profile that has a PIN opens the upsell, never the PIN entry; in Base it asks for the PIN (review 27084, R5). - elementTree.ts: an absence is proved with `find`, never with `holding` (review 27088, S2). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
290 lines
12 KiB
TypeScript
290 lines
12 KiB
TypeScript
// Guard: fr.json and en.json declare the same keys (#453), and each key the
|
|
// same interpolation variables (#480).
|
|
//
|
|
// i18next serves fr.json, the default language, and en.json, the fallback
|
|
// (`src/i18n/config.ts`). A key written in fr.json only shows raw to an
|
|
// English user; one written in en.json only shows in English to a French
|
|
// user. Neither breaks the build. These tests hold both files to one set of
|
|
// keys.
|
|
//
|
|
// A key is the dotted path of a leaf, array indices included
|
|
// (`docs.dashboard.features.6`) and plural suffixes too
|
|
// (`import.sources.fileCount_one`). A path that is an array in one file
|
|
// and an object in the other, or a leaf in one and an object or an array in
|
|
// the other, is a gap as well: `t(key, { returnObjects: true })` would hand
|
|
// the component another shape.
|
|
//
|
|
// Of the values, only the `{{…}}` tokens are compared (#480): a text left
|
|
// untranslated in the other file passes. A key that writes `{{count}}` in
|
|
// fr.json and `{{n}}` in en.json shows a raw `{{…}}` in one of the two
|
|
// languages, and a format option left out on one side (`{{filter}}` against
|
|
// `{{filter, lowercase}}`) keeps the capital the sentence meant to drop: each
|
|
// leaf both files hold carries one set of tokens, format options included.
|
|
// Neither file nests a key (`$t(…)`) or writes the unescaped form (`{{- …}}`)
|
|
// yet: a nested key would not be compared, and `{{- name}}` would read as the
|
|
// token `- name`, a gap against `{{name}}`. With `escapeValue: false`
|
|
// (src/i18n/config.ts), both forms render the same text in this app: such a
|
|
// gap would be conservative, like a format name written in another case, not
|
|
// an escaping defect.
|
|
|
|
import { describe, it, expect } from "vitest";
|
|
import fr from "./locales/fr.json";
|
|
import en from "./locales/en.json";
|
|
|
|
type NodeKind = "object" | "array" | "leaf";
|
|
|
|
/**
|
|
* Calls `onNode` for every node under the root of a locale tree, with its
|
|
* dotted path (array indices included), its kind and its value.
|
|
*/
|
|
function walk(tree: unknown, onNode: (path: string, kind: NodeKind, value: unknown) => void): void {
|
|
const visit = (value: unknown, path: string[]): void => {
|
|
const kind: NodeKind = Array.isArray(value)
|
|
? "array"
|
|
: typeof value === "object" && value !== null
|
|
? "object"
|
|
: "leaf";
|
|
if (path.length > 0) onNode(path.join("."), kind, value);
|
|
if (kind === "leaf") return;
|
|
// An array's entries are its indices, as strings: `features.6`.
|
|
for (const [key, item] of Object.entries(value as object)) visit(item, [...path, key]);
|
|
};
|
|
visit(tree, []);
|
|
}
|
|
|
|
/** Every node under the root of a locale tree, by dotted path (array indices included), with its kind. */
|
|
function nodesOf(tree: unknown): Map<string, NodeKind> {
|
|
const nodes = new Map<string, NodeKind>();
|
|
walk(tree, (path, kind) => nodes.set(path, kind));
|
|
return nodes;
|
|
}
|
|
|
|
/** Every leaf of a locale tree, by the dotted path `nodesOf` gives it, with its text. */
|
|
function leavesOf(tree: unknown): Map<string, string> {
|
|
const leaves = new Map<string, string>();
|
|
walk(tree, (path, kind, value) => {
|
|
if (kind === "leaf") leaves.set(path, String(value));
|
|
});
|
|
return leaves;
|
|
}
|
|
|
|
/**
|
|
* The keys of `from` that `into` lacks, sorted: the leaves of `from` with
|
|
* nothing at the same path in `into`. A path `into` holds with another kind is
|
|
* left to `kindGaps`.
|
|
*/
|
|
function missingKeys(from: Map<string, NodeKind>, into: Map<string, NodeKind>): string[] {
|
|
return [...from]
|
|
.filter(([path, kind]) => kind === "leaf" && !into.has(path))
|
|
.map(([path]) => path)
|
|
.sort();
|
|
}
|
|
|
|
/** The paths both trees hold with different kinds, sorted, as `path: <kind> in <a>, <kind> in <b>`. */
|
|
function kindGaps(
|
|
a: Map<string, NodeKind>,
|
|
b: Map<string, NodeKind>,
|
|
nameA: string,
|
|
nameB: string,
|
|
): string[] {
|
|
return [...a]
|
|
.filter(([path, kind]) => b.has(path) && b.get(path) !== kind)
|
|
.map(([path, kind]) => `${path}: ${kind} in ${nameA}, ${b.get(path)} in ${nameB}`)
|
|
.sort();
|
|
}
|
|
|
|
/**
|
|
* The `{{…}}` tokens of a text, sorted, each once. A token is what i18next
|
|
* reads between `{{` and the next `}}`, format options included
|
|
* (`filter, lowercase`), without the whitespace i18next ignores: around the
|
|
* name and around each format option.
|
|
*/
|
|
function variablesOf(text: string): string[] {
|
|
const tokens = [...text.matchAll(/\{\{(.+?)\}\}/g)].map(([, inner]) =>
|
|
inner
|
|
.split(",")
|
|
.map((part) => part.trim())
|
|
.join(", "),
|
|
);
|
|
return [...new Set(tokens)].sort();
|
|
}
|
|
|
|
/**
|
|
* The leaves both maps hold with other `{{…}}` tokens, sorted, as
|
|
* `path: {{a}}, {{b}} in <a>; {{a}}, {{c}} in <b>`, every token of each side
|
|
* listed. A path only one side holds, or holds with another kind, is left to
|
|
* `missingKeys` and `kindGaps`.
|
|
*/
|
|
function variableGaps(
|
|
a: Map<string, string>,
|
|
b: Map<string, string>,
|
|
nameA: string,
|
|
nameB: string,
|
|
): string[] {
|
|
const listed = (tokens: string[]): string =>
|
|
tokens.length > 0 ? tokens.map((token) => `{{${token}}}`).join(", ") : "no variable";
|
|
const gaps: string[] = [];
|
|
for (const [path, text] of a) {
|
|
const other = b.get(path);
|
|
if (other === undefined) continue;
|
|
const inA = variablesOf(text);
|
|
const inB = variablesOf(other);
|
|
const same = inA.length === inB.length && inA.every((token, i) => token === inB[i]);
|
|
if (!same) gaps.push(`${path}: ${listed(inA)} in ${nameA}; ${listed(inB)} in ${nameB}`);
|
|
}
|
|
return gaps.sort();
|
|
}
|
|
|
|
const FR = nodesOf(fr);
|
|
const EN = nodesOf(en);
|
|
|
|
describe("locale keys: fr.json and en.json (#453)", () => {
|
|
it("reads both files whole, array items included", () => {
|
|
// The canary: an empty or unread file would make the comparison pass on
|
|
// nothing, and so would one file imported twice.
|
|
expect(en, "en.json and fr.json are two modules").not.toBe(fr);
|
|
for (const [name, nodes] of [["fr.json", FR], ["en.json", EN]] as const) {
|
|
expect(nodes.get("common.cancel"), name).toBe("leaf");
|
|
expect(nodes.get("dashboard.help.tips"), name).toBe("array");
|
|
expect(nodes.get("dashboard.help.tips.0"), name).toBe("leaf");
|
|
}
|
|
});
|
|
|
|
it("en.json declares every key of fr.json", () => {
|
|
expect(missingKeys(FR, EN), "keys of fr.json missing from en.json").toEqual([]);
|
|
});
|
|
|
|
it("fr.json declares every key of en.json", () => {
|
|
expect(missingKeys(EN, FR), "keys of en.json missing from fr.json").toEqual([]);
|
|
});
|
|
|
|
it("gives every path both files hold the same kind: object, array or leaf", () => {
|
|
expect(kindGaps(FR, EN, "fr.json", "en.json"), "paths of another kind").toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("locale keys: the comparison (#453)", () => {
|
|
it("names, on each side apart, the keys the other side lacks, array indices included", () => {
|
|
const a = nodesOf({ list: ["one", "two"], only: { a: "x" } });
|
|
const b = nodesOf({ list: ["one"], only: { b: "y" } });
|
|
expect(missingKeys(a, b)).toEqual(["list.1", "only.a"]);
|
|
expect(missingKeys(b, a)).toEqual(["only.b"]);
|
|
// Leaves only: the container `zz` of a missing branch is not listed.
|
|
expect(missingKeys(nodesOf({ zz: { only: "x" } }), nodesOf({}))).toEqual(["zz.only"]);
|
|
});
|
|
|
|
it("holds an array against an object with the same indices as a gap", () => {
|
|
const a = nodesOf({ list: ["one"] });
|
|
const b = nodesOf({ list: { 0: "one" } });
|
|
expect(missingKeys(a, b)).toEqual([]);
|
|
expect(missingKeys(b, a)).toEqual([]);
|
|
expect(kindGaps(a, b, "a", "b")).toEqual(["list: array in a, object in b"]);
|
|
});
|
|
|
|
it("holds a leaf against an object or an array as a gap", () => {
|
|
const leaf = nodesOf({ x: "text" });
|
|
expect(kindGaps(leaf, nodesOf({ x: { y: "text" } }), "a", "b")).toEqual([
|
|
"x: leaf in a, object in b",
|
|
]);
|
|
expect(kindGaps(leaf, nodesOf({ x: ["text"] }), "a", "b")).toEqual(["x: leaf in a, array in b"]);
|
|
// The leaf is not reported missing as well: `x` exists on the other side, with another kind.
|
|
expect(missingKeys(leaf, nodesOf({ x: ["text"] }))).toEqual([]);
|
|
});
|
|
|
|
it("never compares values", () => {
|
|
const a = nodesOf({ x: "un", list: ["a", "b"] });
|
|
const b = nodesOf({ x: "one", list: ["c", "d"] });
|
|
expect(missingKeys(a, b)).toEqual([]);
|
|
expect(missingKeys(b, a)).toEqual([]);
|
|
expect(kindGaps(a, b, "a", "b")).toEqual([]);
|
|
});
|
|
});
|
|
|
|
const FR_LEAVES = leavesOf(fr);
|
|
const EN_LEAVES = leavesOf(en);
|
|
|
|
describe("locale values: interpolation variables (#480)", () => {
|
|
it("reads the tokens of each file, format options included", () => {
|
|
// The canary: texts read as empty, or tokens read as nothing, would make
|
|
// the comparison pass on nothing, and so would one file read twice.
|
|
for (const [name, leaves] of [["fr.json", FR_LEAVES], ["en.json", EN_LEAVES]] as const) {
|
|
expect(variablesOf(leaves.get("import.rootFiles.message_one") ?? ""), name).toEqual([
|
|
"count",
|
|
"folder",
|
|
"names",
|
|
]);
|
|
expect(variablesOf(leaves.get("onboarding.card.transactions.uncategorized") ?? ""), name).toContain(
|
|
"filter, lowercase",
|
|
);
|
|
}
|
|
expect(EN_LEAVES.get("import.rootFiles.message_one"), "en.json and fr.json are read apart").not.toBe(
|
|
FR_LEAVES.get("import.rootFiles.message_one"),
|
|
);
|
|
});
|
|
|
|
it("gives every leaf both files hold the same {{…}} tokens", () => {
|
|
expect(variableGaps(FR_LEAVES, EN_LEAVES, "fr.json", "en.json"), "leaves with other variables").toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("locale values: the variable comparison (#480)", () => {
|
|
const gaps = (a: object, b: object): string[] => variableGaps(leavesOf(a), leavesOf(b), "a", "b");
|
|
|
|
it("reads each token whole, format options included, sorted and each once", () => {
|
|
expect(variablesOf("{{names}}: {{count}} files in {{folder}}")).toEqual(["count", "folder", "names"]);
|
|
expect(variablesOf("Only {{filter, lowercase}} ones")).toEqual(["filter, lowercase"]);
|
|
expect(variablesOf("{{n}} of {{n}}")).toEqual(["n"]);
|
|
expect(variablesOf("No variable")).toEqual([]);
|
|
});
|
|
|
|
it("drops only the whitespace i18next ignores, around the name and each format option", () => {
|
|
expect(variablesOf("{{ count }}, {{filter,lowercase}}, {{ filter , lowercase }}")).toEqual([
|
|
"count",
|
|
"filter, lowercase",
|
|
]);
|
|
});
|
|
|
|
it("names a variable renamed on one side, with every token of each side", () => {
|
|
expect(gaps({ x: "{{count}} in {{folder}}" }, { x: "{{count}} in {{dir}}" })).toEqual([
|
|
"x: {{count}}, {{folder}} in a; {{count}}, {{dir}} in b",
|
|
]);
|
|
// A name that differs by its case only is another variable: i18next looks
|
|
// `{{Version}}` up as written, and shows it raw, braces included, when the
|
|
// call passes `version`.
|
|
expect(gaps({ x: "{{version}}" }, { x: "{{Version}}" })).toEqual(["x: {{version}} in a; {{Version}} in b"]);
|
|
});
|
|
|
|
it("names a variable missing on either side", () => {
|
|
expect(gaps({ x: "{{count}} files" }, { x: "Files" })).toEqual(["x: {{count}} in a; no variable in b"]);
|
|
expect(gaps({ x: "Files" }, { x: "{{count}} files" })).toEqual(["x: no variable in a; {{count}} in b"]);
|
|
expect(gaps({ x: "{{a}} and {{b}}" }, { x: "{{b}}" })).toEqual(["x: {{a}}, {{b}} in a; {{b}} in b"]);
|
|
});
|
|
|
|
it("holds another format option, or none, on one side as a gap", () => {
|
|
expect(gaps({ x: "Only {{filter, lowercase}}" }, { x: "Only {{filter}}" })).toEqual([
|
|
"x: {{filter, lowercase}} in a; {{filter}} in b",
|
|
]);
|
|
expect(gaps({ x: "{{filter, lowercase}}" }, { x: "{{filter, uppercase}}" })).toEqual([
|
|
"x: {{filter, lowercase}} in a; {{filter, uppercase}} in b",
|
|
]);
|
|
});
|
|
|
|
it("lists every leaf in gap, array items included, not only the first", () => {
|
|
expect(
|
|
gaps(
|
|
{ z: "{{a}}", list: ["{{n}}", "{{m}}"], same: "{{s}}" },
|
|
{ z: "{{b}}", list: ["{{n}}", "{{k}}"], same: "{{s}}" },
|
|
),
|
|
).toEqual(["list.1: {{m}} in a; {{k}} in b", "z: {{a}} in a; {{b}} in b"]);
|
|
});
|
|
|
|
it("passes the same variables in another order, a repeated variable and a key without variable", () => {
|
|
expect(gaps({ x: "{{a}}, then {{b}}" }, { x: "{{b}}, then {{a}}" })).toEqual([]);
|
|
expect(gaps({ x: "{{count}} of {{count}}" }, { x: "{{count}}" })).toEqual([]);
|
|
expect(gaps({ x: "Annuler" }, { x: "Cancel" })).toEqual([]);
|
|
});
|
|
|
|
it("leaves a path one side lacks, or holds with another kind, to the key comparison", () => {
|
|
expect(gaps({ only: "{{a}}", x: { y: "{{a}}" } }, { x: "{{b}}" })).toEqual([]);
|
|
});
|
|
});
|