|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#30311 — an app the server served is named as served. |
| 5 | + * |
| 6 | + * Since objectstack#30353 `/meta/app` serves an overridden app (a `.custom` |
| 7 | + * pair) under the ruled label order: the override's own text over the base |
| 8 | + * package's translation. Every console surface that names an app handed that |
| 9 | + * served label to `useObjectLabel().appLabel` as its FALLBACK, and the bundle's |
| 10 | + * `apps.<name>.label` — the base package's string — won it back. Now the served |
| 11 | + * label is drawn as given, and the bundle is asked only for text no server |
| 12 | + * translated. |
| 13 | + * |
| 14 | + * Subject: the real `useServedAppLabel` over the real `useObjectLabel`, inside a |
| 15 | + * real `I18nProvider` holding the packaged catalogue, as the console loads it. |
| 16 | + * |
| 17 | + * Directions, written before the run: the renamed cells RED before the change |
| 18 | + * (the bundle's packaged string in both languages), GREEN after; the unedited, |
| 19 | + * draft, keyed, unlabelled and description-only cells GREEN on both sides. |
| 20 | + */ |
| 21 | +import { describe, it, expect } from 'vitest'; |
| 22 | +import { readdirSync, readFileSync } from 'node:fs'; |
| 23 | +import { dirname, join, relative, resolve } from 'node:path'; |
| 24 | +import { fileURLToPath } from 'node:url'; |
| 25 | +import { renderHook } from '@testing-library/react'; |
| 26 | +import type { ReactNode } from 'react'; |
| 27 | +import { I18nProvider } from '@object-ui/i18n'; |
| 28 | +import { useServedAppLabel } from '../useServedAppLabel'; |
| 29 | + |
| 30 | +/** The packaged catalogue: the base package translates its app. */ |
| 31 | +const BUNDLE = { |
| 32 | + en: { crm: { apps: { crm: { label: 'CRM', description: 'Sell more' } } } }, |
| 33 | + 'zh-CN': { crm: { apps: { crm: { label: '客户管理', description: '多卖一点' } } } }, |
| 34 | +}; |
| 35 | +const PACKAGED = { en: 'CRM', 'zh-CN': '客户管理' } as const; |
| 36 | +const PACKAGED_DESCRIPTION = { en: 'Sell more', 'zh-CN': '多卖一点' } as const; |
| 37 | +const RENAMED = 'Pipeline Desk (renamed-30311)'; |
| 38 | +const LANGUAGES = ['en', 'zh-CN'] as const; |
| 39 | + |
| 40 | +function labelsIn(language: 'en' | 'zh-CN') { |
| 41 | + const wrapper = ({ children }: { children: ReactNode }) => ( |
| 42 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false, resources: BUNDLE }} persistLanguage={false}> |
| 43 | + {children} |
| 44 | + </I18nProvider> |
| 45 | + ); |
| 46 | + return renderHook(() => useServedAppLabel(), { wrapper }).result.current; |
| 47 | +} |
| 48 | + |
| 49 | +describe('useServedAppLabel — a served app label is drawn as served (objectstack#30311)', () => { |
| 50 | + it.each(LANGUAGES)('%s: an overridden app shows the label the server served, not the base package\'s', (language) => { |
| 51 | + const { appLabel } = labelsIn(language); |
| 52 | + expect(appLabel({ name: 'crm', label: RENAMED })).toBe(RENAMED); |
| 53 | + }); |
| 54 | + |
| 55 | + it.each(LANGUAGES)('%s: an unedited app shows the translation the server put in', (language) => { |
| 56 | + const { appLabel } = labelsIn(language); |
| 57 | + expect(appLabel({ name: 'crm', label: PACKAGED[language] })).toBe(PACKAGED[language]); |
| 58 | + }); |
| 59 | + |
| 60 | + it('zh-CN: an inline locale map is resolved in the active language', () => { |
| 61 | + const { appLabel } = labelsIn('zh-CN'); |
| 62 | + expect(appLabel({ name: 'crm', label: { en: 'Pipeline Desk', 'zh-CN': '管道台' } })).toBe('管道台'); |
| 63 | + }); |
| 64 | + |
| 65 | + it.each(LANGUAGES)('%s: control — a pending draft under ?preview=draft is named by the bundle', (language) => { |
| 66 | + const { appLabel } = labelsIn(language); |
| 67 | + expect(appLabel({ name: 'crm', label: RENAMED, _draft: true })).toBe(PACKAGED[language]); |
| 68 | + }); |
| 69 | + |
| 70 | + it.each(LANGUAGES)('%s: control — a keyed label is not served text and keeps the bundle', (language) => { |
| 71 | + const { appLabel } = labelsIn(language); |
| 72 | + const keyed = { key: 'crm.unknown_key', defaultValue: 'Fallback' }; |
| 73 | + expect(appLabel({ name: 'crm', label: keyed })).toBe(PACKAGED[language]); |
| 74 | + // With no bundle entry for the app, the key's defaultValue names it, as before. |
| 75 | + expect(appLabel({ name: 'other', label: keyed })).toBe('Fallback'); |
| 76 | + }); |
| 77 | + |
| 78 | + it.each(LANGUAGES)('%s: control — an app with no label is named by the bundle, else its machine name', (language) => { |
| 79 | + const { appLabel } = labelsIn(language); |
| 80 | + expect(appLabel({ name: 'crm' })).toBe(PACKAGED[language]); |
| 81 | + expect(appLabel({ name: 'other' })).toBe('other'); |
| 82 | + expect(appLabel({ name: 'crm', label: '' })).toBe(PACKAGED[language]); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('useServedAppLabel — a served app description is drawn as served (objectstack#30311)', () => { |
| 87 | + it.each(LANGUAGES)('%s: an overridden description shows as served', (language) => { |
| 88 | + const { appDescription } = labelsIn(language); |
| 89 | + expect(appDescription({ name: 'crm', description: 'Deals, fast (renamed-30311)' })).toBe('Deals, fast (renamed-30311)'); |
| 90 | + }); |
| 91 | + |
| 92 | + it.each(LANGUAGES)('%s: control — a translation-only description still comes from the bundle', (language) => { |
| 93 | + const { appDescription } = labelsIn(language); |
| 94 | + expect(appDescription({ name: 'crm' })).toBe(PACKAGED_DESCRIPTION[language]); |
| 95 | + expect(appDescription({ name: 'crm', description: 'Draft text', _draft: true })).toBe(PACKAGED_DESCRIPTION[language]); |
| 96 | + expect(appDescription({ name: 'other' })).toBeUndefined(); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +/** |
| 101 | + * The census that keeps the class closed: no app-shell source names an app |
| 102 | + * through the bundle's `appLabel` / `appDescription` directly — every surface |
| 103 | + * goes through `useServedAppLabel`, which asks the bundle only for text no |
| 104 | + * server translated. Read by name, not by intent: a surface that reaches the |
| 105 | + * bundle another way is left to review. |
| 106 | + */ |
| 107 | +describe('useServedAppLabel — no app-shell surface names an app through the bundle directly (objectstack#30311)', () => { |
| 108 | + const HERE = dirname(fileURLToPath(import.meta.url)); |
| 109 | + const SRC = resolve(HERE, '../..'); |
| 110 | + const sources = (dir: string): string[] => |
| 111 | + readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { |
| 112 | + const path = join(dir, entry.name); |
| 113 | + if (entry.isDirectory()) return entry.name === '__tests__' ? [] : sources(path); |
| 114 | + return /\.tsx?$/.test(entry.name) && !/\.test\.tsx?$/.test(entry.name) ? [path] : []; |
| 115 | + }); |
| 116 | + const BUNDLE_APP_READ = /\{[^}]*\bapp(?:Label|Description)\b[^}]*\}\s*=\s*useObjectLabel\(\)/; |
| 117 | + |
| 118 | + it('reads every source file under app-shell/src', () => { |
| 119 | + // Positive control: the walk reaches the hook itself and a known caller. |
| 120 | + const files = sources(SRC).map((p) => relative(SRC, p)); |
| 121 | + expect(files).toContain(join('hooks', 'useServedAppLabel.ts')); |
| 122 | + expect(files).toContain(join('layout', 'AppSwitcher.tsx')); |
| 123 | + }); |
| 124 | + |
| 125 | + it('none destructures appLabel or appDescription from useObjectLabel()', () => { |
| 126 | + // The hook itself is the one reader of the bundle's app lookups. |
| 127 | + const offenders = sources(SRC) |
| 128 | + .map((p) => relative(SRC, p)) |
| 129 | + .filter((p) => p !== join('hooks', 'useServedAppLabel.ts')) |
| 130 | + .filter((p) => BUNDLE_APP_READ.test(readFileSync(join(SRC, p), 'utf8'))); |
| 131 | + expect(offenders).toEqual([]); |
| 132 | + }); |
| 133 | + |
| 134 | + it('control — the pattern matches the shape it bans', () => { |
| 135 | + expect(BUNDLE_APP_READ.test('const { appLabel } = useObjectLabel();')).toBe(true); |
| 136 | + expect(BUNDLE_APP_READ.test('const { objectLabel, appDescription } = useObjectLabel();')).toBe(true); |
| 137 | + expect(BUNDLE_APP_READ.test('const { objectLabel } = useObjectLabel();')).toBe(false); |
| 138 | + // The hook's own read is the shape too, which is why it alone is exempt. |
| 139 | + const hook = readFileSync(join(SRC, 'hooks', 'useServedAppLabel.ts'), 'utf8'); |
| 140 | + expect(BUNDLE_APP_READ.test(hook)).toBe(true); |
| 141 | + }); |
| 142 | +}); |
0 commit comments