| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import { describe, expect, it } from 'vitest';
- import {
- buildChartSeries,
- classifySheet,
- formatBytes,
- formatNumber,
- formatPercent,
- isNumericColumn,
- isPlainIntegerColumn,
- pickLabelColumn,
- pickValueColumn,
- relativeTime,
- truncate,
- } from '../src/lib/format';
- describe('formatNumber', () => {
- it('整数加千分位', () => {
- expect(formatNumber(1234567)).toBe('1,234,567');
- });
- it('小数保留有效位', () => {
- expect(formatNumber('400316.13')).toBe('400,316.13');
- });
- it('无法解析时原样返回', () => {
- expect(formatNumber('—')).toBe('—');
- expect(formatNumber(null)).toBe('—');
- });
- });
- describe('formatPercent', () => {
- it('小数转百分比', () => {
- expect(formatPercent(0.217)).toBe('21.70%');
- });
- it('非法值返回占位符', () => {
- expect(formatPercent('abc')).toBe('—');
- });
- });
- describe('formatBytes', () => {
- it('分级换算', () => {
- expect(formatBytes(0)).toBe('0 B');
- expect(formatBytes(512)).toBe('512 B');
- expect(formatBytes(2048)).toBe('2.0 KB');
- expect(formatBytes(5 * 1024 * 1024)).toBe('5.0 MB');
- });
- });
- describe('relativeTime', () => {
- const now = Date.parse('2026-11-17T12:00:00+08:00');
- it('刚刚 / 分钟 / 小时 / 天', () => {
- expect(relativeTime('2026-11-17T11:59:40+08:00', now)).toBe('刚刚');
- expect(relativeTime('2026-11-17T11:30:00+08:00', now)).toBe('30 分钟前');
- expect(relativeTime('2026-11-17T09:00:00+08:00', now)).toBe('3 小时前');
- expect(relativeTime('2026-11-15T12:00:00+08:00', now)).toBe('2 天前');
- });
- it('超过一周显示日期', () => {
- expect(relativeTime('2026-10-01T12:00:00+08:00', now)).toBe('2026-10-01');
- });
- });
- describe('classifySheet', () => {
- it('识别 MLCC 各层级', () => {
- expect(classifySheet('1.0-3.0_MLCC')).toEqual({ product: 'MLCC', level: '指数' });
- expect(classifySheet('4.2.3 (1-4)_MLCC')).toEqual({ product: 'MLCC', level: '重点分析' });
- expect(classifySheet('4.3_MLCC').level).toBe('供应链');
- expect(classifySheet('4.2.1_MLCC').level).toBe('结构');
- });
- it('识别存储器与补充表', () => {
- expect(classifySheet('5.2.6_存储器').product).toBe('存储器');
- expect(classifySheet('样本权重').product).toBe('补充');
- expect(classifySheet('raw').product).toBe('原始');
- });
- });
- describe('图表与列推断', () => {
- const columns = ['基础类目', '年度', '实际金额(美元)', '成交数量'];
- it('优先选金额列与类目列', () => {
- expect(pickValueColumn(columns)).toBe('实际金额(美元)');
- expect(pickLabelColumn(columns)).toBe('基础类目');
- });
- it('没有数值列时返回 null', () => {
- expect(pickValueColumn(['品牌', '型号'])).toBeNull();
- });
- it('构造柱状图数据并按绝对值取最大', () => {
- const series = buildChartSeries(
- [
- { 品牌: 'A', 金额: '100' },
- { 品牌: 'B', 金额: '-250' },
- { 品牌: 'C', 金额: '50' },
- ],
- '品牌',
- '金额',
- );
- expect(series.labels).toEqual(['A', 'B', 'C']);
- expect(series.values).toEqual([100, -250, 50]);
- expect(series.max).toBe(250);
- });
- it('限制柱数', () => {
- const rows = Array.from({ length: 20 }, (_, index) => ({ 品牌: `B${index}`, 金额: index }));
- expect(buildChartSeries(rows, '品牌', '金额', 5).labels.length).toBe(5);
- });
- it('数值列判断', () => {
- expect(isNumericColumn('实际金额(美元)')).toBe(true);
- expect(isNumericColumn('品牌')).toBe(false);
- });
- });
- describe('truncate', () => {
- it('超长截断', () => {
- expect(truncate('一二三四五', 3)).toBe('一二三…');
- expect(truncate('短', 3)).toBe('短');
- });
- });
- describe('isPlainIntegerColumn', () => {
- it('年份/月份/名次不加千分位', () => {
- expect(isPlainIntegerColumn('年度')).toBe(true);
- expect(isPlainIntegerColumn('月度')).toBe(true);
- expect(isPlainIntegerColumn('排名')).toBe(true);
- expect(isPlainIntegerColumn('实际金额(美元)')).toBe(false);
- expect(isPlainIntegerColumn('金额指数')).toBe(false);
- });
- });
|