format.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { describe, expect, it } from 'vitest';
  2. import {
  3. buildChartSeries,
  4. classifySheet,
  5. formatBytes,
  6. formatNumber,
  7. formatPercent,
  8. isNumericColumn,
  9. isPlainIntegerColumn,
  10. pickLabelColumn,
  11. pickValueColumn,
  12. relativeTime,
  13. truncate,
  14. } from '../src/lib/format';
  15. describe('formatNumber', () => {
  16. it('整数加千分位', () => {
  17. expect(formatNumber(1234567)).toBe('1,234,567');
  18. });
  19. it('小数保留有效位', () => {
  20. expect(formatNumber('400316.13')).toBe('400,316.13');
  21. });
  22. it('无法解析时原样返回', () => {
  23. expect(formatNumber('—')).toBe('—');
  24. expect(formatNumber(null)).toBe('—');
  25. });
  26. });
  27. describe('formatPercent', () => {
  28. it('小数转百分比', () => {
  29. expect(formatPercent(0.217)).toBe('21.70%');
  30. });
  31. it('非法值返回占位符', () => {
  32. expect(formatPercent('abc')).toBe('—');
  33. });
  34. });
  35. describe('formatBytes', () => {
  36. it('分级换算', () => {
  37. expect(formatBytes(0)).toBe('0 B');
  38. expect(formatBytes(512)).toBe('512 B');
  39. expect(formatBytes(2048)).toBe('2.0 KB');
  40. expect(formatBytes(5 * 1024 * 1024)).toBe('5.0 MB');
  41. });
  42. });
  43. describe('relativeTime', () => {
  44. const now = Date.parse('2026-11-17T12:00:00+08:00');
  45. it('刚刚 / 分钟 / 小时 / 天', () => {
  46. expect(relativeTime('2026-11-17T11:59:40+08:00', now)).toBe('刚刚');
  47. expect(relativeTime('2026-11-17T11:30:00+08:00', now)).toBe('30 分钟前');
  48. expect(relativeTime('2026-11-17T09:00:00+08:00', now)).toBe('3 小时前');
  49. expect(relativeTime('2026-11-15T12:00:00+08:00', now)).toBe('2 天前');
  50. });
  51. it('超过一周显示日期', () => {
  52. expect(relativeTime('2026-10-01T12:00:00+08:00', now)).toBe('2026-10-01');
  53. });
  54. });
  55. describe('classifySheet', () => {
  56. it('识别 MLCC 各层级', () => {
  57. expect(classifySheet('1.0-3.0_MLCC')).toEqual({ product: 'MLCC', level: '指数' });
  58. expect(classifySheet('4.2.3 (1-4)_MLCC')).toEqual({ product: 'MLCC', level: '重点分析' });
  59. expect(classifySheet('4.3_MLCC').level).toBe('供应链');
  60. expect(classifySheet('4.2.1_MLCC').level).toBe('结构');
  61. });
  62. it('识别存储器与补充表', () => {
  63. expect(classifySheet('5.2.6_存储器').product).toBe('存储器');
  64. expect(classifySheet('样本权重').product).toBe('补充');
  65. expect(classifySheet('raw').product).toBe('原始');
  66. });
  67. });
  68. describe('图表与列推断', () => {
  69. const columns = ['基础类目', '年度', '实际金额(美元)', '成交数量'];
  70. it('优先选金额列与类目列', () => {
  71. expect(pickValueColumn(columns)).toBe('实际金额(美元)');
  72. expect(pickLabelColumn(columns)).toBe('基础类目');
  73. });
  74. it('没有数值列时返回 null', () => {
  75. expect(pickValueColumn(['品牌', '型号'])).toBeNull();
  76. });
  77. it('构造柱状图数据并按绝对值取最大', () => {
  78. const series = buildChartSeries(
  79. [
  80. { 品牌: 'A', 金额: '100' },
  81. { 品牌: 'B', 金额: '-250' },
  82. { 品牌: 'C', 金额: '50' },
  83. ],
  84. '品牌',
  85. '金额',
  86. );
  87. expect(series.labels).toEqual(['A', 'B', 'C']);
  88. expect(series.values).toEqual([100, -250, 50]);
  89. expect(series.max).toBe(250);
  90. });
  91. it('限制柱数', () => {
  92. const rows = Array.from({ length: 20 }, (_, index) => ({ 品牌: `B${index}`, 金额: index }));
  93. expect(buildChartSeries(rows, '品牌', '金额', 5).labels.length).toBe(5);
  94. });
  95. it('数值列判断', () => {
  96. expect(isNumericColumn('实际金额(美元)')).toBe(true);
  97. expect(isNumericColumn('品牌')).toBe(false);
  98. });
  99. });
  100. describe('truncate', () => {
  101. it('超长截断', () => {
  102. expect(truncate('一二三四五', 3)).toBe('一二三…');
  103. expect(truncate('短', 3)).toBe('短');
  104. });
  105. });
  106. describe('isPlainIntegerColumn', () => {
  107. it('年份/月份/名次不加千分位', () => {
  108. expect(isPlainIntegerColumn('年度')).toBe(true);
  109. expect(isPlainIntegerColumn('月度')).toBe(true);
  110. expect(isPlainIntegerColumn('排名')).toBe(true);
  111. expect(isPlainIntegerColumn('实际金额(美元)')).toBe(false);
  112. expect(isPlainIntegerColumn('金额指数')).toBe(false);
  113. });
  114. });