import assert from 'node:assert/strict'; import { test } from 'node:test'; import { catalogs, messageRows } from '../src/lib/i18n/messages.ts'; import { get } from 'svelte/store'; import { LANGUAGE_KEY, locale, normalizeLocale, setLocale, t, translate, syncLocale } from '../src/lib/i18n/index.ts'; test('Every catalog entry has all translations and preserves its placeholders', () => { const seen = new Set(); for (const [english, ...translations] of messageRows) { assert(!seen.has(english), `Duplicate message: ${english}`); seen.add(english); const placeholders = (value: string) => [...value.matchAll(/\{(\w+)\}/g)].map(match => match[1]).sort(); for (const value of translations) { assert(value.trim(), `Empty translation: ${english}`); assert.deepEqual(placeholders(value), placeholders(english), english); } } assert.equal(Object.keys(catalogs.fr).length, messageRows.length); }); test('Locale parsing rejects unsupported values and interpolation preserves user text', () => { assert.equal(normalizeLocale('ja'), 'ja'); assert.equal(normalizeLocale('zh-CN'), 'zh-CN'); for (const value of [null, undefined, 'constructor', 'de', [], {}]) assert.equal(normalizeLocale(value), 'en'); assert.equal(translate('fr', 'Missing source text'), 'Missing source text'); assert.equal(translate('ja', 'Close {name}', { name: '' }), 'を閉じる'); }); test('Language saves before reactive changes and syncs across windows', () => { const stored = new Map(); const events = new Map(); const priorStorage = globalThis.localStorage, priorWindow = globalThis.window, priorDocument = globalThis.document; try { globalThis.localStorage = { getItem: (key: string) => stored.get(key) ?? null, setItem: (key: string, value: string) => stored.set(key, value) } as Storage; globalThis.window = { addEventListener: (key: string, handler: Function) => events.set(key, handler), removeEventListener: (key: string) => events.delete(key) } as unknown as Window & typeof globalThis; globalThis.document = { documentElement: { lang: '' } } as unknown as Document; const stop = syncLocale(); setLocale('fr'); assert.equal(stored.get(LANGUAGE_KEY), 'fr'); assert.equal(get(t)('Settings'), 'Paramètres'); assert.equal(document.documentElement.lang, 'fr'); stored.set(LANGUAGE_KEY, 'zh-CN'); events.get('storage')!({ key: LANGUAGE_KEY }); assert.equal(get(locale), 'zh-CN'); assert.equal(get(t)('Settings'), '设置'); globalThis.localStorage.setItem = () => { throw new Error('Storage unavailable'); }; assert.throws(() => setLocale('ja')); assert.equal(get(locale), 'zh-CN'); stop(); assert.equal(events.size, 0); } finally { globalThis.localStorage = priorStorage; globalThis.window = priorWindow; globalThis.document = priorDocument; locale.set('en'); } }); import { readFileSync, readdirSync } from 'node:fs'; import path from 'node:path'; import ts from 'typescript'; function sourceFiles(dir: string): string[] { return readdirSync(dir, { withFileTypes: true }).flatMap(entry => entry.isDirectory() ? sourceFiles(path.join(dir, entry.name)) : /\.(ts|svelte)$/.test(entry.name) ? [path.join(dir, entry.name)] : []); } test('Every static host translation call has all three catalog entries', () => { const missing: string[] = []; for (const file of sourceFiles('src/lib')) { const source = readFileSync(file, 'utf8'); for (const match of source.matchAll(/(?:\$t|\btr)\(\s*((?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'))/g)) { const parsed = ts.createSourceFile('literal.ts', match[1], ts.ScriptTarget.Latest, true); const statement = parsed.statements[0]; const value = ts.isExpressionStatement(statement) && ts.isStringLiteral(statement.expression) ? statement.expression.text : ''; if (value && !Object.hasOwn(catalogs.fr, value)) missing.push(`${file}: ${value}`); } } assert.deepEqual(missing, []); }); test('Keyboard identifiers remain untranslated in event handlers', () => { const failures: string[] = []; for (const file of sourceFiles('src/lib')) { const source = readFileSync(file, 'utf8'); for (const line of source.split(/\r?\n/)) { if (/event\.(?:key|code)\s*[!=]==?\s*(?:tr|\$t)\(/.test(line) || /(?:tr|\$t)\([^\n]+\]\.(?:includes|indexOf)\(event\.(?:key|code)/.test(line)) failures.push(`${file}: ${line.trim()}`); } } assert.deepEqual(failures, []); });