Files
monkey-ai-web/test/unit/safe-display.test.js
T
zhanjunhao c492e2b7c0 fix(*): 全量 UI 审查与 undefined 展示异常修复
分析模块:补齐看板、排行、趋势及报表数值兜底
企业与用户中心:修复统计、成员、技能及资金字段展示
充值与交易:修复充值、发票、订单、订阅及支付字段拼接
平台页面:修复渠道、模型、服务监控与状态空值展示
通用能力:新增安全展示工具,补齐九语言文案与回归测试
文档记录:更新修复范围、影响及回滚说明
2026-11-20 10:18:17 +08:00

71 lines
2.5 KiB
JavaScript

/*
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 <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import assert from 'node:assert/strict';
import test from 'node:test';
import {
formatCurrency,
formatNumber,
formatPercent,
formatRatioPercent,
formatIdReference,
formatUserReference,
toFiniteNumber,
} from '../../src/helpers/safeDisplay.js';
test('toFiniteNumber rejects missing and non-finite display values', () => {
assert.equal(toFiniteNumber(undefined), null);
assert.equal(toFiniteNumber(''), null);
assert.equal(toFiniteNumber(' '), null);
assert.equal(toFiniteNumber('not-a-number'), null);
assert.equal(toFiniteNumber('12.5'), 12.5);
assert.equal(toFiniteNumber(Infinity), null);
});
test('currency and number formatters never expose invalid values', () => {
assert.equal(formatNumber('12.5', 2), '12.50');
assert.equal(formatNumber('bad'), '-');
assert.equal(formatCurrency('12.5'), '¥12.50');
assert.equal(formatCurrency(undefined), '-');
});
test('percent formatters never expose invalid values', () => {
assert.equal(formatPercent(undefined), '-');
assert.equal(formatPercent('12.5'), '12.5%');
assert.equal(formatRatioPercent(undefined, 10), '-');
assert.equal(formatRatioPercent(2, 0), '-');
assert.equal(formatRatioPercent(2, 4), '50.0%');
});
test('user references never interpolate an undefined id', () => {
assert.equal(formatUserReference('Alice', undefined), 'Alice');
assert.equal(formatUserReference('', 42), 'User#42');
assert.equal(formatUserReference(undefined, undefined), '-');
assert.equal(
formatUserReference(undefined, 'bad', 'Unknown user'),
'Unknown user',
);
});
test('id references never expose a missing identifier', () => {
assert.equal(formatIdReference(undefined), '-');
assert.equal(formatIdReference(''), '-');
assert.equal(formatIdReference(42), '#42');
});