/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import test from 'node:test'; import { createInstance } from 'i18next'; import { LANGUAGE_CODES } from '../../i18n/catalog.js'; import { commissionLabel, commissionLabels, commissionLevelName, commissionActorName, } from './labels.js'; const resources = Object.fromEntries( LANGUAGE_CODES.map((code) => [ code, JSON.parse( readFileSync( new URL(`../../i18n/locales/${code}.json`, import.meta.url), 'utf8', ), ), ]), ); test('audit operators show account names, distinguish system actions and missing accounts', () => { const t = (key) => key; assert.equal( commissionActorName(t, { actor_id: 5, actor_username: 'admin.account' }), 'admin.account', ); assert.equal(commissionActorName(t, { actor_id: 0 }), '系统'); assert.equal(commissionActorName(t, { actor_id: '0' }), '系统'); assert.equal(commissionActorName(t, { actor_id: 99 }), '未知用户'); assert.equal(commissionActorName(t, {}), '未知用户'); }); test('commission labels follow the selected language and preserve custom values', async () => { const i18n = createInstance(); await i18n.init({ lng: 'zh', resources, fallbackLng: false, keySeparator: false, nsSeparator: false, }); assert.equal(commissionLabel(i18n.t, 'level', 'default'), '默认'); assert.equal(commissionLabel(i18n.t, 'level', 'standard'), '标准'); assert.equal(commissionLabel(i18n.t, 'level', 'premium'), '高级'); assert.equal(commissionLabel(i18n.t, 'operation', 'fee_set'), '录入手续费'); for (const [action, expected] of Object.entries({ ADJ_CREATED: '创建调整单', ADJ_UPDATED: '修改调整单', ADJ_DELETED: '作废调整单', BILL_CREATED: '新增', BILL_ISSUED: '正式出账', FEE_ENTERED: '录入手续费', PAID_REGISTERED: '登记已打款', PARTNER_CONFIRMED: '伙伴确认', PARTNER_REJECTED: '伙伴驳回', AUTO_CONFIRMED: '逾期视同确认', BLOCK_DETECTED: '出账阻断', BLOCK_CLEARED: '解除出账阻断', DEADLINE_CHANGED: '修改确认截止日期', RECALC: '刷新金额', })) { assert.equal(commissionLabel(i18n.t, 'operation', action), expected); } await i18n.changeLanguage('en'); assert.equal(commissionLabel(i18n.t, 'level', 'default'), 'Default'); assert.equal(commissionLabel(i18n.t, 'level', 'premium'), 'Premium'); assert.equal(commissionLabel(i18n.t, 'status', 'active'), 'Active'); assert.equal(commissionLabel(i18n.t, 'operation', 'fee_set'), 'Set fee'); assert.equal(commissionLabel(i18n.t, 'level', 'VIP-custom'), 'VIP-custom'); assert.equal( commissionLabel(i18n.t, 'operation', 'new_action'), 'new_action', ); assert.equal(commissionLabel(i18n.t, 'level', 'toString'), 'toString'); assert.equal(commissionLabel(i18n.t, 'level', null), '-'); assert.equal( commissionLevelName(i18n.t, { key: 'default', name: 'default' }), 'Default', ); assert.equal( commissionLevelName(i18n.t, { key: 'default', name: '定制等级' }), '定制等级', ); await i18n.changeLanguage('zh-TW'); assert.equal(commissionLabel(i18n.t, 'level', 'default'), '預設'); }); test('every supported language has non-empty commission enum translations', () => { for (const code of LANGUAGE_CODES) { for (const labels of Object.values(commissionLabels)) { for (const key of Object.values(labels)) { assert.ok( resources[code].translation[key]?.trim(), `${code} is missing ${key}`, ); } } } }); test('commission page translations retain interpolation variables in every language', () => { const source = ['index.jsx', 'operations.jsx'] .map((file) => readFileSync(new URL(`./${file}`, import.meta.url), 'utf8')) .join('\n'); const keys = new Set( [...source.matchAll(/\bt\('([^']+)'/gu)].map((match) => match[1]), ); const variables = (text) => [...text.matchAll(/\{\{(\w+)\}\}/gu)].map((match) => match[1]).sort(); for (const code of LANGUAGE_CODES) { for (const key of keys) { const translated = resources[code].translation[key]; assert.ok(translated?.trim(), `${code} is missing ${key}`); assert.deepEqual( variables(translated), variables(key), `${code}: ${key}`, ); } } });