Spaces:
Running

File size: 2,421 Bytes
03608f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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([]);
  });
});