Skip to content

Commit bdd3c09

Browse files
committed
test: positive controls for the namespace-frame rule of the i18n call-site gate
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibxDqYaHT7GBjAY9FzB
1 parent 7e802e1 commit bdd3c09

2 files changed

Lines changed: 134 additions & 1 deletion

File tree

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
holesOf,
1818
PACK_HOOK,
1919
PACK_NAMESPACE_DIRS,
20+
PACK_NAMESPACE_FRAMES,
2021
readVocabulary,
2122
RESERVED_OPTION_NAMES,
2223
unresolvableSpellings,
@@ -302,6 +303,120 @@ describe('the pack-shaped namespaces are part of the en pack (objectstack#30588)
302303
});
303304
});
304305

306+
describe('a key only a namespace defines is refused outside its frame (objectstack#30645)', () => {
307+
const NS_EN = `${PACK_NAMESPACE_DIRS[0]}en.ts`;
308+
const FRAME = PACK_NAMESPACE_FRAMES[PACK_NAMESPACE_DIRS[0]][0];
309+
const NS_PACK =
310+
"const en = { shop: { title: 'Shop', row_one: '{{count}} row', row_other: '{{count}} rows', " +
311+
"status: { open: 'Open', closed: 'Closed' } } } as const;\nexport default en;\n";
312+
const RESIDENT = "const en = { shop: { kept: 'Kept', tag: { a: 'A' } } } as const;\nexport default en;\n";
313+
const page = (body: string) =>
314+
`import { useObjectTranslation } from '${I18N_PKG}';\nexport function P({ s }: { s: string }) { const { t } = useObjectTranslation(); return ${body}; }\n`;
315+
const outsideFrame = (root: string) => findingsOf(root, 'namespace-key-outside-frame');
316+
317+
it('every namespace directory is declared with a non-empty frame (one list, read both ways)', () => {
318+
expect(Object.keys(PACK_NAMESPACE_FRAMES)).toEqual([...PACK_NAMESPACE_DIRS]);
319+
for (const dir of PACK_NAMESPACE_DIRS) expect(PACK_NAMESPACE_FRAMES[dir].length).toBeGreaterThan(0);
320+
});
321+
322+
it('turns red on a namespace-only key spelled outside the frame (the positive control)', () => {
323+
const root = repoWith({
324+
'packages/i18n/src/locales/en.ts': RESIDENT,
325+
[NS_EN]: NS_PACK,
326+
'packages/studio/src/page.tsx': page("t('shop.title')"),
327+
});
328+
// fixture-address: the finding the gate reports for the fixture page written just above
329+
expect(outsideFrame(root)).toEqual(['shop.title@packages/studio/src/page.tsx:2']);
330+
const finding = analyze(root, { families: [] }).findings.find(
331+
(f: { reason: string }) => f.reason === 'namespace-key-outside-frame',
332+
);
333+
expect(finding?.expected).toBe(PACK_NAMESPACE_DIRS[0]);
334+
});
335+
336+
it('reads the same key inside the frame, and counts it there', () => {
337+
const root = repoWith({
338+
'packages/i18n/src/locales/en.ts': RESIDENT,
339+
[NS_EN]: NS_PACK,
340+
[`${FRAME}Page.tsx`]: page("t('shop.title')"),
341+
});
342+
expect(outsideFrame(root)).toEqual([]);
343+
expect(analyze(root, { families: [] }).framedReads.get(PACK_NAMESPACE_DIRS[0])).toBe(1);
344+
});
345+
346+
it('stays silent on a resident key outside the frame', () => {
347+
const root = repoWith({
348+
'packages/i18n/src/locales/en.ts': RESIDENT,
349+
[NS_EN]: NS_PACK,
350+
'packages/studio/src/page.tsx': page("t('shop.kept')"),
351+
});
352+
expect(outsideFrame(root)).toEqual([]);
353+
expect(analyze(root, { families: [] }).framedReads.get(PACK_NAMESPACE_DIRS[0])).toBe(0);
354+
});
355+
356+
it('refuses a plural base whose forms live only in the namespace', () => {
357+
const root = repoWith({
358+
'packages/i18n/src/locales/en.ts': RESIDENT,
359+
[NS_EN]: NS_PACK,
360+
'packages/studio/src/page.tsx': page("t('shop.row', { count: 2 })"),
361+
});
362+
// fixture-address: the finding the gate reports for the fixture page written just above
363+
expect(outsideFrame(root)).toEqual(['shop.row@packages/studio/src/page.tsx:2']);
364+
});
365+
366+
it('refuses a template head only the namespace answers, and passes one a resident leaf answers', () => {
367+
const root = repoWith({
368+
'packages/i18n/src/locales/en.ts': RESIDENT,
369+
[NS_EN]: NS_PACK,
370+
'packages/studio/src/a.tsx': page('t(`shop.status.${s}`)'),
371+
'packages/studio/src/b.tsx': page('t(`shop.tag.${s}`)'),
372+
});
373+
// fixture-address: the finding the gate reports for the fixture page written just above
374+
expect(outsideFrame(root)).toEqual(['shop.status.*@packages/studio/src/a.tsx:2']);
375+
});
376+
377+
it('refuses a returnObjects subtree that only the namespace fills', () => {
378+
const root = repoWith({
379+
'packages/i18n/src/locales/en.ts': RESIDENT,
380+
[NS_EN]: NS_PACK,
381+
'packages/studio/src/page.tsx': page("t('shop.status', { returnObjects: true })"),
382+
});
383+
// fixture-address: the finding the gate reports for the fixture page written just above
384+
expect(outsideFrame(root)).toEqual(['shop.status.*@packages/studio/src/page.tsx:2']);
385+
});
386+
387+
it('a second namespace inherits the rule from its declaration alone', () => {
388+
const SECOND = 'packages/i18n/src/namespaces/billing/';
389+
const frames = { ...PACK_NAMESPACE_FRAMES, [SECOND]: ['packages/app-shell/src/console/billing/'] };
390+
const root = repoWith({
391+
'packages/i18n/src/locales/en.ts': RESIDENT,
392+
[`${SECOND}en.ts`]: "const en = { billing: { title: 'Billing' } } as const;\nexport default en;\n",
393+
'packages/app-shell/src/console/billing/Page.tsx': page("t('billing.title')"),
394+
'packages/studio/src/page.tsx': page("t('billing.title')"),
395+
});
396+
const result = analyze(root, { families: [], frames });
397+
const refused = result.findings
398+
.filter((f: { reason: string }) => f.reason === 'namespace-key-outside-frame')
399+
.map((f: { detail: string; file: string; expected: string }) => `${f.detail}@${f.file} <- ${f.expected}`);
400+
expect(refused).toEqual([`billing.title@packages/studio/src/page.tsx <- ${SECOND}`]);
401+
expect(result.framedReads.get(SECOND)).toBe(1);
402+
});
403+
404+
it('refuses a namespace declared without a frame', () => {
405+
const root = repoWith({ 'packages/i18n/src/locales/en.ts': RESIDENT });
406+
expect(() => analyze(root, { families: [], frames: { 'packages/i18n/src/namespaces/billing/': [] } })).toThrow(
407+
/declares no frame/,
408+
);
409+
});
410+
411+
it('on this tree: no namespace key is read outside its frame, and every frame reads some', () => {
412+
const outside = REPO_ANALYSIS.findings.filter(
413+
(f: { reason: string }) => f.reason === 'namespace-key-outside-frame',
414+
);
415+
expect(outside).toEqual([]);
416+
for (const dir of PACK_NAMESPACE_DIRS) expect(REPO_ANALYSIS.framedReads.get(dir)).toBeGreaterThan(0);
417+
});
418+
});
419+
305420
describe('the file walk', () => {
306421
it('reads sources but not tests, type declarations or build output', () => {
307422
const root = repoWith({

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
* it, and the rule is the class rather than the instance: the first full run
121121
* found 24 such sites in four files, every one of them on a key `en` defines.
122122
*
123-
* ## Eight failure classes
123+
* ## Nine failure classes
124124
*
125125
* (The heading said "Five" while the list below held seven — a stale count
126126
* corrected here rather than left to grow by one more.)
@@ -481,6 +481,24 @@
481481
* moved to JSON on objectui#6923's ruling instead of being duplicated here
482482
* and pinned.
483483
*
484+
* 9. `namespace-key-outside-frame` (objectstack#30645) — the key EXISTS, but
485+
* only in a lazy pack-shaped namespace, and the call site is outside the
486+
* directories that namespace's frame wraps (`PACK_NAMESPACE_FRAMES`). Every
487+
* other class reads the namespace as part of the `en` pack, which is right
488+
* under the frame and wrong everywhere else: nothing fetches the namespace
489+
* there, so the user reads the raw key. Judged on a literal key (plural
490+
* forms included: a key is namespace-only when every form that resolves is),
491+
* on a `returnObjects` subtree and on a template head, each refused only
492+
* when NO resident leaf answers it. Not judged: a key built by a helper or
493+
* spelled in a table and passed as a variable — the same blind spot class 1
494+
* has. Inside the frame's directories, which files a page OUTSIDE the frame
495+
* imports is the namespace's own boundary test's census, not this gate's.
496+
* The rule reads the frame off the one namespace list, so a second namespace
497+
* is judged the day it is declared. HARD from day one (0 on the tree it
498+
* landed against); the run's `Namespace frames:` line prints, per
499+
* namespace, how many call-site keys its frame reads, and a namespace whose
500+
* frame reads none fails the run as a collapsed scan.
501+
*
484502
* ## Dynamic keys: the explicit policy
485503
*
486504
* A key that is not a string literal cannot be resolved statically. Those call

0 commit comments

Comments
 (0)