Skip to content

Commit 0245e5d

Browse files
committed
test(i18n gates): read the marketplace namespace as part of each language
The call-site-keys, dead-keys and en-drift gates now walk the lazy namespace's partial packs together with each pack, refuse a key defined in both, and keep a namespace file optional for fixture roots. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L6bhgXDwFw71MwSG9FKYDQ
1 parent 69ab56e commit 0245e5d

4 files changed

Lines changed: 139 additions & 14 deletions

File tree

‎ui/scripts/__tests__/check-i18n-call-site-keys.test.ts‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
HAND_ROLLED_TABLES,
1717
holesOf,
1818
PACK_HOOK,
19+
PACK_NAMESPACE_DIRS,
1920
readVocabulary,
2021
RESERVED_OPTION_NAMES,
2122
unresolvableSpellings,
@@ -95,10 +96,27 @@ const tempRoots: string[] = [];
9596
* AST walk reports only workspace-package specifiers and would not have
9697
* caught a relative one. Computing the path keeps the premise true instead of stepping
9798
* around the pin that guards it.
99+
*
100+
* The gate's `en` is the pack PLUS the `en` partial pack of every pack-shaped
101+
* namespace (`PACK_NAMESPACE_DIRS`, objectstack#30588), so the module-evaluated
102+
* side is the same union, merged the way the runtime merges it.
98103
*/
104+
const evaluatedPack = async (rel: string): Promise<Record<string, unknown>> =>
105+
(await import(pathToFileURL(path.join(repoRoot, rel)).href)).default;
106+
const mergedPacks = (into: Record<string, unknown>, from: Record<string, unknown>): Record<string, unknown> => {
107+
const out: Record<string, unknown> = { ...into };
108+
for (const [key, value] of Object.entries(from)) {
109+
const existing = out[key];
110+
out[key] =
111+
typeof existing === 'object' && existing !== null && typeof value === 'object' && value !== null
112+
? mergedPacks(existing as Record<string, unknown>, value as Record<string, unknown>)
113+
: value;
114+
}
115+
return out;
116+
};
99117
const realEn: unknown = (
100-
await import(pathToFileURL(path.join(repoRoot, 'packages/i18n/src/locales/en.ts')).href)
101-
).default;
118+
await Promise.all(PACK_NAMESPACE_DIRS.map((dir) => evaluatedPack(`${dir}en.ts`)))
119+
).reduce(mergedPacks, await evaluatedPack('packages/i18n/src/locales/en.ts'));
102120

103121
/**
104122
* The workspace specifier that appears inside the FIXTURE SOURCES below — text
@@ -234,6 +252,55 @@ describe('the parsed en key set equals the module vitest actually evaluates', ()
234252
});
235253
});
236254

255+
describe('the pack-shaped namespaces are part of the en pack (objectstack#30588)', () => {
256+
const NS_EN = `${PACK_NAMESPACE_DIRS[0]}en.ts`;
257+
258+
it('reads a key the namespace defines as a pack key', () => {
259+
const root = repoWith({
260+
'packages/i18n/src/locales/en.ts': EN_FIXTURE,
261+
[NS_EN]: "const en = { shop: { title: 'Shop', install: { go: 'Install' } } } as const;\nexport default en;\n",
262+
});
263+
const keys = collectEnKeys(root);
264+
expect(keys.leaves.has('shop.title')).toBe(true);
265+
expect(keys.values.get('shop.install.go')).toBe('Install');
266+
expect(keys.branches.has('shop')).toBe(true);
267+
expect(keys.leaves.has('common.save')).toBe(true);
268+
});
269+
270+
it('merges a group both files hold, key by key', () => {
271+
const root = repoWith({
272+
'packages/i18n/src/locales/en.ts': "const en = { shop: { kept: 'Kept' } } as const;\nexport default en;\n",
273+
[NS_EN]: "const en = { shop: { moved: 'Moved' } } as const;\nexport default en;\n",
274+
});
275+
const keys = collectEnKeys(root);
276+
expect([...keys.leaves].sort()).toEqual(['shop.kept', 'shop.moved']);
277+
});
278+
279+
it('refuses a key defined in both homes', () => {
280+
const root = repoWith({
281+
'packages/i18n/src/locales/en.ts': "const en = { shop: { title: 'A' } } as const;\nexport default en;\n",
282+
[NS_EN]: "const en = { shop: { title: 'B' } } as const;\nexport default en;\n",
283+
});
284+
expect(() => collectEnKeys(root)).toThrow(/`shop\.title` is defined in/);
285+
});
286+
287+
it('refuses a key in one home that is a group in the other', () => {
288+
const root = repoWith({
289+
'packages/i18n/src/locales/en.ts': "const en = { shop: { title: 'A' } } as const;\nexport default en;\n",
290+
[NS_EN]: "const en = { shop: { title: { short: 'B' } } } as const;\nexport default en;\n",
291+
});
292+
expect(() => collectEnKeys(root)).toThrow(/`shop\.title` is a group in/);
293+
});
294+
295+
it('reports a call site asking for a namespace key the namespace no longer defines', () => {
296+
const root = repoWith({
297+
'packages/i18n/src/locales/en.ts': EN_FIXTURE,
298+
'packages/x/src/page.tsx': `import { useObjectTranslation } from '${I18N_PKG}';\nexport function P() { const { t } = useObjectTranslation(); return t('shop.title'); }\n`,
299+
});
300+
expect(findingsOf(root, 'missing-key')).toEqual(['shop.title@packages/x/src/page.tsx:2']);
301+
});
302+
});
303+
237304
describe('the file walk', () => {
238305
it('reads sources but not tests, type declarations or build output', () => {
239306
const root = repoWith({

‎ui/scripts/__tests__/check-i18n-en-drift.test.ts‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ISSUE_PATTERN,
1515
LEDGER_PATH,
1616
LOCALES,
17+
namespacePackPaths,
1718
packPath,
1819
parsePack,
1920
readLedger,
@@ -448,6 +449,58 @@ describe('the objectui#3625 commit, reconstructed in a real git repo', () => {
448449
});
449450
});
450451

452+
// -- the pack-shaped namespaces (objectstack#30588) ---------------------------
453+
454+
describe('a language is its pack plus its namespace partial packs', () => {
455+
const NS = namespacePackPaths('en')[0].replace(/en\.ts$/, '');
456+
const nsFiles = (trees: Record<string, Nested>): Record<string, string> =>
457+
Object.fromEntries(LOCALES.map((lang) => [`${NS}${lang}.ts`, packSource(lang, trees[lang])]));
458+
459+
it('names one partial pack per language per namespace', () => {
460+
expect(namespacePackPaths('zh')).toEqual(['packages/i18n/src/namespaces/marketplace/zh.ts']);
461+
});
462+
463+
it('a group MOVED from the pack to its namespace is no drift and no key-set change', () => {
464+
const before = packFiles(treesFor((lang) => ({ common: { save: `save-${lang}` }, shop: { title: `shop-${lang}` } })));
465+
const after = {
466+
...packFiles(treesFor((lang) => ({ common: { save: `save-${lang}` } }))),
467+
...nsFiles(treesFor((lang) => ({ shop: { title: `shop-${lang}` } }))),
468+
};
469+
const { root, shas } = repoWithCommits([before, after]);
470+
const { code, output } = runGate(root, [...FIXTURE_FLOOR, '--base', shas[0], '--head', shas[1]]);
471+
expect(code).toBe(0);
472+
expect(output).toContain('0 en value(s) changed (0 key(s) added, 0 removed');
473+
});
474+
475+
it('an en value changed INSIDE the namespace is drift the nine must follow', () => {
476+
const base = treesFor((lang) => ({ shop: { title: `shop-${lang}` } }));
477+
const before = { ...packFiles(treesFor(() => ({ common: { save: 's' } }))), ...nsFiles(base) };
478+
const after = {
479+
...packFiles(treesFor(() => ({ common: { save: 's' } }))),
480+
...nsFiles({ ...base, en: { shop: { title: 'Shop, renamed' } } }),
481+
};
482+
const { root, shas } = repoWithCommits([before, after]);
483+
const { code, output } = runGateExpectingFailure(root, [
484+
...FIXTURE_FLOOR,
485+
'--base',
486+
shas[0],
487+
'--head',
488+
shas[1],
489+
]);
490+
expect(code).toBe(1);
491+
expect(output).toContain('shop.title');
492+
});
493+
494+
it('refuses a key defined both in a pack and in its namespace', () => {
495+
const files = {
496+
...packFiles(treesFor((lang) => ({ shop: { title: `pack-${lang}` } }))),
497+
...nsFiles(treesFor((lang) => ({ shop: { title: `ns-${lang}` } }))),
498+
};
499+
const { root } = repoWithCommits([files]);
500+
expect(() => readPacks(root, null)).toThrow(/shop\.title is defined both in/);
501+
});
502+
});
503+
451504
// -- the waiver ledger --------------------------------------------------------
452505

453506
/** The reconstructed drift plus a ledger, as a two-commit repo the CLI can read. */
@@ -606,7 +659,10 @@ describe('the shipped ledger', () => {
606659
});
607660

608661
it('has no entry that the current en pack contradicts', () => {
609-
const enAfter = parsePack(fs.readFileSync(path.join(repoRoot, packPath('en')), 'utf8'), 'en.ts');
662+
// The gate's own `en` population — the pack and its namespaces' partial
663+
// packs (objectstack#30588) — so a waived key that moved to a namespace is
664+
// still read where it now lives.
665+
const enAfter = readPacks(repoRoot, null).get('en')!;
610666
expect(checkLedger(ledger, enAfter)).toEqual([]);
611667
for (const entry of Object.values(ledger.waivers) as Array<{ issue: string }>) {
612668
expect(entry.issue).toMatch(ISSUE_PATTERN);

‎ui/scripts/check-i18n-call-site-keys.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,9 +1355,12 @@ export function collectEnKeys(root) {
13551355
const branches = new Set();
13561356
const values = new Map();
13571357
const homeOf = new Map();
1358+
// The pack is required; a namespace's `en` file is read when it exists. A
1359+
// missing one cannot hide anything: its keys simply are not defined, so every
1360+
// call site that asks for one is reported as asking for a missing key.
13581361
const files = [
13591362
join(root, 'packages/i18n/src/locales/en.ts'),
1360-
...PACK_NAMESPACE_DIRS.map((dir) => join(root, dir, 'en.ts')),
1363+
...PACK_NAMESPACE_DIRS.map((dir) => join(root, dir, 'en.ts')).filter((file) => existsSync(file)),
13611364
];
13621365
for (const file of files) {
13631366
const source = ts.createSourceFile(file, readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);

‎ui/scripts/check-i18n-en-drift.mjs‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,16 @@ function gitQuiet(root, args) {
354354
* The working tree is the default "after" side on purpose: an agent editing
355355
* `en.ts` gets the answer before committing, not after pushing.
356356
*
357-
* `requireNamespaces` is false only on the BASE side: a namespace that does not
358-
* exist yet at the base commit is a fact about history (its keys were still in
359-
* the pack there), not a stale extractor. On the head side a missing partial
360-
* pack is loud. A key defined both in a pack and in its namespace THROWS on
361-
* either side — two homes for one key cannot both be what renders.
357+
* A partial pack is read when it exists at that side and skipped when it does
358+
* not: a namespace absent at the base is history (its keys were still in the
359+
* pack there), and one absent anywhere holds no key, so a key it took with it
360+
* is a key-set change — `all-locales-key-parity`'s, never drift. A partial pack
361+
* that exists and cannot be parsed still throws, and so does a key defined both
362+
* in a pack and in its namespace: two homes for one key cannot both render.
362363
*
363364
* @returns {Map<string, Map<string, string>>} lang -> (key -> value)
364365
*/
365-
export function readPacks(root, ref, { requireNamespaces = true } = {}) {
366+
export function readPacks(root, ref) {
366367
const read = (rel, required) => {
367368
const label = ref === null ? rel : revPath(ref, rel);
368369
let text;
@@ -387,7 +388,7 @@ export function readPacks(root, ref, { requireNamespaces = true } = {}) {
387388
const pack = read(packPath(lang), true);
388389
const values = new Map(pack.values);
389390
for (const rel of namespacePackPaths(lang)) {
390-
const partial = read(rel, requireNamespaces);
391+
const partial = read(rel, false);
391392
if (partial === null) continue;
392393
for (const [key, value] of partial.values) {
393394
if (values.has(key)) {
@@ -625,9 +626,7 @@ export function applyLedger(findings, ledger, enAfter) {
625626
*/
626627
export function analyze(root, { base, head = null }) {
627628
const treeAtBase = treeExistsAt(root, base);
628-
const before = treeAtBase
629-
? readPacks(root, base, { requireNamespaces: false })
630-
: new Map(LOCALES.map((lang) => [lang, new Map()]));
629+
const before = treeAtBase ? readPacks(root, base) : new Map(LOCALES.map((lang) => [lang, new Map()]));
631630
const after = readPacks(root, head);
632631
const { findings, counters } = findDrift(before, after);
633632
return { before, after, findings, counters, treeAtBase };

0 commit comments

Comments
 (0)