Spaces:
Running
Running
Download web/src/lib/i18n/coverage.test.ts from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/lib/i18n/coverage.test.ts
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/web/src/lib/i18n/coverage.test.ts
-
curl -L -o coverage.test.ts https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/lib/i18n/coverage.test.ts
2.42 kB
| import { describe, expect, it } from "vitest"; | |
| import { en, ur } from "./messages"; | |
| type Tree = { [k: string]: string | readonly string[] | Tree }; | |
| /** Flattens a message tree to dotted keys; arrays become key.0, key.1, ... */ | |
| function flatten(tree: Tree, prefix = ""): Map<string, string> { | |
| const out = new Map<string, string>(); | |
| for (const [k, v] of Object.entries(tree)) { | |
| const key = prefix ? `${prefix}.${k}` : k; | |
| if (typeof v === "string") out.set(key, v); | |
| else if (Array.isArray(v)) v.forEach((s, i) => out.set(`${key}.${i}`, String(s))); | |
| else for (const [ck, cv] of flatten(v as Tree, key)) out.set(ck, cv); | |
| } | |
| return out; | |
| } | |
| // Values that are legitimately the same in both languages: codes, formats, brand-free tokens. | |
| const SAME_ALLOWED = new Set<string>(["workflows.tabs.json", "nlrules.version"]); | |
| const enFlat = flatten(en as unknown as Tree); | |
| const urFlat = flatten(ur as unknown as Tree); | |
| describe("Urdu translation coverage", () => { | |
| it("has exactly the same keys as English", () => { | |
| const missing = [...enFlat.keys()].filter((k) => !urFlat.has(k)); | |
| const extra = [...urFlat.keys()].filter((k) => !enFlat.has(k)); | |
| expect({ missing, extra }).toEqual({ missing: [], extra: [] }); | |
| }); | |
| it("has no empty Urdu values", () => { | |
| const empty = [...urFlat].filter(([, v]) => !v.trim()).map(([k]) => k); | |
| expect(empty).toEqual([]); | |
| }); | |
| it("translates every value (except an explicit allowlist)", () => { | |
| const same = [...enFlat] | |
| .filter(([k, v]) => urFlat.get(k) === v && !SAME_ALLOWED.has(k)) | |
| .map(([k]) => k); | |
| expect(same).toEqual([]); | |
| }); | |
| it("keeps the same {placeholders} in both languages", () => { | |
| const vars = (s: string) => [...s.matchAll(/\{(\w+)\}/g)].map((m) => m[1]).sort(); | |
| const mismatched = [...enFlat] | |
| .filter(([k, v]) => JSON.stringify(vars(v)) !== JSON.stringify(vars(urFlat.get(k) ?? ""))) | |
| .map(([k]) => k); | |
| expect(mismatched).toEqual([]); | |
| }); | |
| it("writes Urdu in Arabic script (Nastaliq), not transliterated Latin", () => { | |
| // A value may embed codes (COD, JSON, ORD-1021, {vars}) but must contain Urdu letters. | |
| const latinOnly = [...urFlat] | |
| .filter(([k]) => !SAME_ALLOWED.has(k)) | |
| .filter(([, v]) => !/[-ۿ]/.test(v)) | |
| .map(([k, v]) => `${k}=${v}`); | |
| expect(latinOnly).toEqual([]); | |
| }); | |
| }); | |