157 lines
7.3 KiB
TypeScript
157 lines
7.3 KiB
TypeScript
import { readFileSync } from 'node:fs'
|
||
import path from 'node:path'
|
||
|
||
import { describe, expect, it } from 'bun:test'
|
||
|
||
import { describeApiError, localizeApiMessage } from '../src/i18n/api-errors'
|
||
import { translateLegacyAdminText } from '../src/i18n/LegacyI18nBridge'
|
||
import { legacyUiTextEnUS } from '../src/i18n/legacy-ui'
|
||
import i18n from '../src/i18n'
|
||
import { enUS, zhCN } from '../src/i18n/resources'
|
||
|
||
function collectLeafKeys(node: Record<string, unknown>, prefix = ''): string[] {
|
||
const keys: string[] = []
|
||
for (const [key, value] of Object.entries(node)) {
|
||
const nextKey = prefix ? `${prefix}.${key}` : key
|
||
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
||
keys.push(...collectLeafKeys(value as Record<string, unknown>, nextKey))
|
||
} else {
|
||
keys.push(nextKey)
|
||
}
|
||
}
|
||
return keys.sort()
|
||
}
|
||
|
||
describe('UI copy and i18n audit contracts', () => {
|
||
it('keeps resources.ts zhCN and enUS keys in 1:1 parity without brand/jargon leaks', () => {
|
||
const zhKeys = collectLeafKeys(zhCN as unknown as Record<string, unknown>)
|
||
const enKeys = collectLeafKeys(enUS as unknown as Record<string, unknown>)
|
||
expect(zhKeys).toEqual(enKeys)
|
||
expect(zhCN.auth.loginTitle).toBe('登录智能星球控制台')
|
||
expect(enUS.auth.loginTitle).toBe('Log in to Intelligent Planet Console')
|
||
expect(zhCN.docs.footerLanguage).toBe('语言')
|
||
expect(zhCN.docs.footerTheme).toBe('主题')
|
||
})
|
||
|
||
it('has zero single-character keys or duplicate keys in legacy-ui.ts', () => {
|
||
for (const key of Object.keys(legacyUiTextEnUS)) {
|
||
expect(key.trim().length).toBeGreaterThan(1)
|
||
}
|
||
const sourcePath = path.resolve(import.meta.dir, '../src/i18n/legacy-ui.ts')
|
||
const raw = readFileSync(sourcePath, 'utf8')
|
||
const keys: string[] = []
|
||
for (const line of raw.split('\n')) {
|
||
const match = line.match(/^\s*(?:'([^']+)'|"([^"]+)")\s*:/)
|
||
if (match) keys.push(match[1] ?? match[2])
|
||
}
|
||
const unique = new Set(keys)
|
||
expect(keys.length).toBe(unique.size)
|
||
})
|
||
|
||
it('has zero duplicate keys in Earth EXACT_TRANSLATIONS', () => {
|
||
const earthI18nPath = path.resolve(import.meta.dir, '../public/earth/js/i18n.js')
|
||
const raw = readFileSync(earthI18nPath, 'utf8')
|
||
const blockMatch = raw.match(/const EXACT_TRANSLATIONS = \{([\s\S]*?)\n\};/)
|
||
expect(blockMatch).not.toBeNull()
|
||
const keys: string[] = []
|
||
for (const line of (blockMatch?.[1] || '').split('\n')) {
|
||
const match = line.match(/^\s*"([^"]+)"\s*:/)
|
||
if (match) keys.push(match[1])
|
||
}
|
||
const seen = new Set<string>()
|
||
const duplicates: string[] = []
|
||
for (const key of keys) {
|
||
if (seen.has(key)) duplicates.push(key)
|
||
seen.add(key)
|
||
}
|
||
expect(duplicates).toEqual([])
|
||
})
|
||
|
||
it('preserves Keep List terminology and lossless zh-CN translation', () => {
|
||
expect(translateLegacyAdminText('数据源', 'zh-CN')).toBe('数据源')
|
||
expect(translateLegacyAdminText('Live 新闻', 'zh-CN')).toBe('Live 新闻')
|
||
expect(translateLegacyAdminText('Live 新闻', 'en-US')).toBe('Live News')
|
||
expect(translateLegacyAdminText('Web Search 状态', 'en-US')).toContain('Web Search')
|
||
expect(translateLegacyAdminText('AI 参谋 Playground', 'en-US')).toBe('AI Advisor Playground')
|
||
})
|
||
|
||
it('translates transport errors cleanly without JSON dumps', async () => {
|
||
const prevLang = i18n.language
|
||
try {
|
||
await i18n.changeLanguage('zh-CN')
|
||
expect(localizeApiMessage('Network Error')).toBe('网络请求失败,请检查网络连接')
|
||
expect(localizeApiMessage('timeout of 5000ms exceeded')).toBe('请求超时,请稍后重试')
|
||
expect(localizeApiMessage('Request failed with status code 502')).toBe('请求失败(状态码 502)')
|
||
expect(describeApiError({ response: { data: { foo: 'bar' } } })).toBe('操作失败')
|
||
|
||
await i18n.changeLanguage('en-US')
|
||
expect(localizeApiMessage('Network Error')).toBe('Network request failed')
|
||
expect(localizeApiMessage('timeout of 5000ms exceeded')).toBe('Request timed out')
|
||
expect(localizeApiMessage('Request failed with status code 502')).toBe('Request failed (HTTP 502)')
|
||
expect(describeApiError({ response: { data: { detail: '后端错误' } } })).toBe('Operation failed')
|
||
} finally {
|
||
await i18n.changeLanguage(prevLang || 'zh-CN')
|
||
}
|
||
})
|
||
|
||
it('translates collector fields, built-in descriptions, and avoids partial Chinglish rewrites', () => {
|
||
const cjk = /[\u3400-\u9fff]/
|
||
expect(translateLegacyAdminText('凭证状态', 'en-US')).toBe('Credential status')
|
||
expect(translateLegacyAdminText('默认地址', 'en-US')).toBe('Default URL')
|
||
expect(translateLegacyAdminText('覆盖内置配置', 'en-US')).toBe('Override built-in config')
|
||
expect(translateLegacyAdminText('配置 ID', 'en-US')).toBe('Config ID')
|
||
expect(translateLegacyAdminText('API Key 名称', 'en-US')).toBe('API Key name')
|
||
expect(translateLegacyAdminText('API Key 位置', 'en-US')).toBe('API Key location')
|
||
expect(translateLegacyAdminText('内置采集器默认配置:AISStream 实时船舶', 'en-US')).toBe(
|
||
'Built-in collector default config: AISStream Live Vessels',
|
||
)
|
||
expect(translateLegacyAdminText('测试自定义采集器备注', 'en-US')).toBe('测试自定义采集器备注')
|
||
|
||
const plainPagesPath = path.resolve(import.meta.dir, '../src/admin/pages/PlainResourcePages.tsx')
|
||
const plainPagesRaw = readFileSync(plainPagesPath, 'utf8')
|
||
const fieldLabelsMatch = plainPagesRaw.match(/const fieldLabels: Record<string, string> = \{([\s\S]*?)\n\}/)
|
||
expect(fieldLabelsMatch).not.toBeNull()
|
||
for (const line of (fieldLabelsMatch?.[1] || '').split('\n')) {
|
||
const m = line.match(/^\s*[a-zA-Z0-9_]+\s*:\s*'([^']+)'/)
|
||
if (!m) continue
|
||
const label = m[1]
|
||
if (label.trim().length <= 1 || !cjk.test(label)) continue
|
||
expect(cjk.test(translateLegacyAdminText(label, 'en-US'))).toBe(false)
|
||
}
|
||
|
||
const dashboardPath = path.resolve(import.meta.dir, '../src/admin/pages/Dashboard.tsx')
|
||
const dashboardRaw = readFileSync(dashboardPath, 'utf8')
|
||
const restartMatch = dashboardRaw.match(/const RESTART_ACTION_OPTIONS[\s\S]*?=\s*\[([\s\S]*?)\n\]/)
|
||
expect(restartMatch).not.toBeNull()
|
||
for (const m of (restartMatch?.[1] || '').matchAll(/(?:label|description):\s*'([^']+)'/g)) {
|
||
expect(cjk.test(translateLegacyAdminText(m[1], 'en-US'))).toBe(false)
|
||
}
|
||
})
|
||
|
||
it('keeps key Earth HTML/JS copy in EXACT_TRANSLATIONS without CJK leaks', () => {
|
||
const cjk = /[\u3400-\u9fff]/
|
||
const earthI18nPath = path.resolve(import.meta.dir, '../public/earth/js/i18n.js')
|
||
const raw = readFileSync(earthI18nPath, 'utf8')
|
||
const blockMatch = raw.match(/const EXACT_TRANSLATIONS = \{([\s\S]*?)\n\};/)
|
||
expect(blockMatch).not.toBeNull()
|
||
const entries = new Map<string, string>()
|
||
for (const line of (blockMatch?.[1] || '').split('\n')) {
|
||
const match = line.match(/^\s*"([^"]+)"\s*:\s*"([^"]*)"/)
|
||
if (match) entries.set(match[1], match[2])
|
||
}
|
||
for (const [k, v] of entries.entries()) {
|
||
if (k === '中文') continue
|
||
expect(cjk.test(v)).toBe(false)
|
||
}
|
||
for (const key of [
|
||
'BGP 观测',
|
||
'全球海底光缆',
|
||
'在 AI 参谋中分析',
|
||
'正在检查 Web Search 状态,稍候即可定位。',
|
||
'多物件联合分析对比栏',
|
||
]) {
|
||
expect(entries.has(key)).toBe(true)
|
||
}
|
||
})
|
||
})
|