|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { PlatformTranslationDataSchema, type PlatformTranslationBundle } from './translation.zod'; |
| 5 | +import { resolveSettingsBannerText, resolveSettingsBannerTitle } from './i18n-resolver'; |
| 6 | + |
| 7 | +/** |
| 8 | + * The `info_banner` slot of the platform settings translation group: |
| 9 | + * `settings.<namespace>.banners.<banner_id>.{title,text}`, keyed by the |
| 10 | + * specifier's `id`, beside `groups` / `keys` / `actions`. |
| 11 | + */ |
| 12 | +describe('settings.<namespace>.banners — the info_banner translation slot', () => { |
| 13 | + const banners = { |
| 14 | + beta_notice: { title: '注意', text: 'Beta 功能可能随时变更。' }, |
| 15 | + }; |
| 16 | + |
| 17 | + it('accepts a banner title and text on the platform face', () => { |
| 18 | + const result = PlatformTranslationDataSchema.safeParse({ settings: { feature_flags: { banners } } }); |
| 19 | + expect(result.success).toBe(true); |
| 20 | + }); |
| 21 | + |
| 22 | + it.each([ |
| 23 | + ['the specifier spelling `bannerText`', { bannerText: 'x' }, 'text'], |
| 24 | + ['the specifier spelling `label`', { label: 'x' }, 'title'], |
| 25 | + ['the group body spelling `description`', { description: 'x' }, 'text'], |
| 26 | + ])('refuses %s on a banner and names the leaf that holds it', (_what, leaf, expected) => { |
| 27 | + const result = PlatformTranslationDataSchema.safeParse({ |
| 28 | + settings: { feature_flags: { banners: { beta_notice: leaf } } }, |
| 29 | + }); |
| 30 | + expect(result.success).toBe(false); |
| 31 | + const issue = result.error?.issues.find((i) => i.code === 'unrecognized_keys'); |
| 32 | + expect(issue?.path).toEqual(['settings', 'feature_flags', 'banners', 'beta_notice']); |
| 33 | + expect(issue?.message).toContain(`→ \`${expected}\``); |
| 34 | + }); |
| 35 | + |
| 36 | + it('refuses the singular `banner` group and names `banners`', () => { |
| 37 | + const result = PlatformTranslationDataSchema.safeParse({ |
| 38 | + settings: { feature_flags: { banner: banners } }, |
| 39 | + }); |
| 40 | + expect(result.success).toBe(false); |
| 41 | + const issue = result.error?.issues.find((i) => i.code === 'unrecognized_keys'); |
| 42 | + expect(issue?.path).toEqual(['settings', 'feature_flags']); |
| 43 | + expect(issue?.message).toContain('→ `banners`'); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('resolveSettingsBannerTitle / resolveSettingsBannerText', () => { |
| 48 | + const bundle: PlatformTranslationBundle = { |
| 49 | + 'zh-CN': { |
| 50 | + settings: { |
| 51 | + feature_flags: { banners: { beta_notice: { title: '注意', text: 'Beta 功能可能随时变更。' } } }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }; |
| 55 | + |
| 56 | + it('resolves the requested locale', () => { |
| 57 | + const opts = { locale: 'zh-CN' }; |
| 58 | + expect(resolveSettingsBannerTitle(bundle, 'feature_flags', 'beta_notice', 'Heads up', opts)).toBe('注意'); |
| 59 | + expect(resolveSettingsBannerText(bundle, 'feature_flags', 'beta_notice', 'Beta features…', opts)) |
| 60 | + .toBe('Beta 功能可能随时变更。'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('walks the declared fallback chain when the requested locale has no entry', () => { |
| 64 | + const opts = { locale: 'ja-JP', fallbackChain: ['zh-CN'] }; |
| 65 | + expect(resolveSettingsBannerTitle(bundle, 'feature_flags', 'beta_notice', 'Heads up', opts)).toBe('注意'); |
| 66 | + expect(resolveSettingsBannerText(bundle, 'feature_flags', 'beta_notice', 'Beta features…', opts)) |
| 67 | + .toBe('Beta 功能可能随时变更。'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('falls back to the manifest literal when no locale in the chain has the banner', () => { |
| 71 | + const opts = { locale: 'ja-JP' }; |
| 72 | + expect(resolveSettingsBannerTitle(bundle, 'feature_flags', 'beta_notice', 'Heads up', opts)).toBe('Heads up'); |
| 73 | + expect(resolveSettingsBannerText(bundle, 'feature_flags', 'beta_notice', 'Beta features…', opts)) |
| 74 | + .toBe('Beta features…'); |
| 75 | + expect(resolveSettingsBannerText(bundle, 'feature_flags', 'other_notice', undefined, { locale: 'zh-CN' })) |
| 76 | + .toBeUndefined(); |
| 77 | + expect(resolveSettingsBannerTitle(undefined, 'feature_flags', 'beta_notice', 'Heads up')).toBe('Heads up'); |
| 78 | + }); |
| 79 | +}); |
0 commit comments