|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Every settings copy table this plugin writes carries every language on the |
| 4 | +// platform locale table, and a caller's code finds its own language's entry: |
| 5 | +// `zh-TW` the Traditional one (never the Simplified), a stored alias its |
| 6 | +// table language, anything else English. |
| 7 | + |
| 8 | +import { describe, it, expect } from 'vitest'; |
| 9 | +import { PLATFORM_LOCALE_TAGS, type PlatformLocaleTag } from '@objectstack/spec/system'; |
| 10 | +import type { SettingsCopyLocale } from './settings-copy-locale.js'; |
| 11 | +import { ALLOWED_IP_RANGES_REFUSAL_COPY, allowedIpRangesRefusalCopy } from './allowed-ip-ranges.js'; |
| 12 | +import { SIGN_IN_FLOOR_COPY, signInFloorCopy } from './sign-in-method-floor.js'; |
| 13 | +import { MFA_REQUIRED_ENROLLMENT_COPY, mfaRequiredEnrollmentCopy } from './mfa-required-enrollment-predicate.js'; |
| 14 | +import { |
| 15 | + ORGANIZATION_MFA_REFUSAL_COPY, |
| 16 | + ORGANIZATION_MFA_REQUIRED_NOTICE_COPY, |
| 17 | + organizationMfaRequiredNotice, |
| 18 | + organizationMfaWithoutEnrollmentRefusal, |
| 19 | +} from './organization-mfa-enrollment-guard.js'; |
| 20 | + |
| 21 | +/** The key each platform table tag is served by. */ |
| 22 | +const KEY_OF_TAG: Record<PlatformLocaleTag, SettingsCopyLocale> = { |
| 23 | + en: 'en', 'zh-Hans': 'zh-CN', 'zh-Hant': 'zh-Hant', es: 'es-ES', ar: 'ar', fr: 'fr', |
| 24 | + 'pt-BR': 'pt-BR', ru: 'ru', de: 'de', ja: 'ja-JP', ko: 'ko', |
| 25 | +}; |
| 26 | +const KEYS = PLATFORM_LOCALE_TAGS.map((tag) => KEY_OF_TAG[tag]); |
| 27 | + |
| 28 | +/** An entry's sentences, functions called with sample arguments. */ |
| 29 | +function sentences(entry: unknown): Record<string, string> { |
| 30 | + if (typeof entry === 'string') return { text: entry }; |
| 31 | + if (typeof entry === 'function') return { 0: entry(0), 1: entry(1), 7: entry(7), 22: entry(22) }; |
| 32 | + const out: Record<string, string> = {}; |
| 33 | + for (const [field, value] of Object.entries(entry as Record<string, unknown>)) { |
| 34 | + out[field] = typeof value === 'function' ? value('203.0.113.7') : String(value); |
| 35 | + } |
| 36 | + return out; |
| 37 | +} |
| 38 | + |
| 39 | +interface Table { |
| 40 | + name: string; |
| 41 | + table: Record<string, unknown>; |
| 42 | + /** The table's own lookup, answering the entry a caller's code is served. */ |
| 43 | + lookup(locale: string | undefined): unknown; |
| 44 | +} |
| 45 | + |
| 46 | +const TABLES: Table[] = [ |
| 47 | + { name: 'allowed IP ranges refusal', table: ALLOWED_IP_RANGES_REFUSAL_COPY, lookup: allowedIpRangesRefusalCopy }, |
| 48 | + { name: 'sign-in method floor', table: SIGN_IN_FLOOR_COPY, lookup: signInFloorCopy }, |
| 49 | + { name: 'auth.mfa_required refusal', table: MFA_REQUIRED_ENROLLMENT_COPY, lookup: mfaRequiredEnrollmentCopy }, |
| 50 | + { |
| 51 | + name: 'organization MFA refusal', |
| 52 | + table: ORGANIZATION_MFA_REFUSAL_COPY, |
| 53 | + lookup: (locale) => organizationMfaWithoutEnrollmentRefusal(locale).message, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: 'organization MFA notice', |
| 57 | + table: ORGANIZATION_MFA_REQUIRED_NOTICE_COPY, |
| 58 | + lookup: (locale) => organizationMfaRequiredNotice(7, locale), |
| 59 | + }, |
| 60 | +]; |
| 61 | + |
| 62 | +/** What a lookup answered, as comparable text. */ |
| 63 | +function served(t: Table, locale: string | undefined): string { |
| 64 | + const answer = t.lookup(locale); |
| 65 | + return JSON.stringify(typeof answer === 'string' ? answer : sentences(answer)); |
| 66 | +} |
| 67 | + |
| 68 | +/** The text a lookup must answer when it serves `key`. */ |
| 69 | +function expected(t: Table, key: SettingsCopyLocale): string { |
| 70 | + const entry = t.table[key]; |
| 71 | + if (t.name === 'organization MFA notice') return JSON.stringify((entry as (d: number) => string)(7)); |
| 72 | + return JSON.stringify(typeof entry === 'string' ? entry : sentences(entry)); |
| 73 | +} |
| 74 | + |
| 75 | +describe.each(TABLES)('$name copy carries the eleven platform languages', (t) => { |
| 76 | + it('has exactly one entry per platform locale table language', () => { |
| 77 | + expect(Object.keys(t.table).sort()).toEqual([...KEYS].sort()); |
| 78 | + expect(KEYS).toHaveLength(11); |
| 79 | + }); |
| 80 | + |
| 81 | + it('every entry carries every sentence, written in its own language (no English fill)', () => { |
| 82 | + const en = sentences(t.table.en); |
| 83 | + for (const key of KEYS) { |
| 84 | + const own = sentences(t.table[key]); |
| 85 | + expect(Object.keys(own).sort(), key).toEqual(Object.keys(en).sort()); |
| 86 | + for (const [field, text] of Object.entries(own)) { |
| 87 | + expect(text.trim(), `${key}.${field}`).not.toBe(''); |
| 88 | + if (key !== 'en') expect(text, `${key}.${field}`).not.toBe(en[field]); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it('each table tag, and each stored alias, is served its own language', () => { |
| 94 | + for (const tag of PLATFORM_LOCALE_TAGS) { |
| 95 | + expect(served(t, tag), tag).toBe(expected(t, KEY_OF_TAG[tag])); |
| 96 | + } |
| 97 | + for (const [code, key] of [ |
| 98 | + ['zh-TW', 'zh-Hant'], ['zh-HK', 'zh-Hant'], ['zh-CN', 'zh-CN'], ['zh', 'zh-CN'], |
| 99 | + ['ja-JP', 'ja-JP'], ['es-ES', 'es-ES'], ['pt', 'pt-BR'], ['en-US', 'en'], ['fr-CA', 'fr'], ['de-AT', 'de'], |
| 100 | + ] as const) { |
| 101 | + expect(served(t, code), code).toBe(expected(t, key)); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + it('zh-TW is never served the Simplified entry', () => { |
| 106 | + expect(served(t, 'zh-TW')).not.toBe(expected(t, 'zh-CN')); |
| 107 | + }); |
| 108 | + |
| 109 | + it('a code naming no language here, or none at all, is served English', () => { |
| 110 | + for (const code of ['xx', 'tlh', undefined]) { |
| 111 | + expect(served(t, code), String(code)).toBe(expected(t, 'en')); |
| 112 | + } |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('organization MFA notice plurals', () => { |
| 117 | + it('Russian and Arabic count days in the right form', () => { |
| 118 | + expect(organizationMfaRequiredNotice(1, 'ru')).toContain('1 день'); |
| 119 | + expect(organizationMfaRequiredNotice(3, 'ru')).toContain('3 дня'); |
| 120 | + expect(organizationMfaRequiredNotice(11, 'ru')).toContain('11 дней'); |
| 121 | + expect(organizationMfaRequiredNotice(21, 'ru')).toContain('21 день'); |
| 122 | + expect(organizationMfaRequiredNotice(2, 'ar')).toContain('يومان'); |
| 123 | + expect(organizationMfaRequiredNotice(7, 'ar')).toContain('7 أيام'); |
| 124 | + expect(organizationMfaRequiredNotice(14, 'ar')).toContain('14 يومًا'); |
| 125 | + }); |
| 126 | +}); |
0 commit comments