forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.test.ts
259 lines (220 loc) · 8.78 KB
/
format.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import dayjs from 'dayjs';
import { describe, expect, it } from 'vitest';
import { CNY_TO_USD } from '@/const/discover';
import {
formatDate,
formatIntergerNumber,
formatNumber,
formatPrice,
formatPriceByCurrency,
formatShortenNumber,
formatSize,
formatSpeed,
formatTime,
formatTokenNumber,
} from './format';
// 保留你已经编写的测试用例
describe('format', () => {
describe('formatSize', () => {
it('should format bytes to KB correctly', () => {
expect(formatSize(1024)).toBe('1.0 KB');
expect(formatSize(2048)).toBe('2.0 KB');
expect(formatSize(1536)).toBe('1.5 KB');
});
it('should format bytes to MB correctly', () => {
expect(formatSize(1048576)).toBe('1.0 MB');
expect(formatSize(2097152)).toBe('2.0 MB');
expect(formatSize(1572864)).toBe('1.5 MB');
});
it('should format bytes to GB correctly', () => {
expect(formatSize(1073741824)).toBe('1.0 GB');
expect(formatSize(2147483648)).toBe('2.0 GB');
expect(formatSize(1610612736)).toBe('1.5 GB');
});
it('should handle edge cases', () => {
expect(formatSize(0)).toBe('0.0 KB');
expect(formatSize(1023)).toBe('1.0 KB');
expect(formatSize(1073741823)).toBe('1024.0 MB');
});
it('should handle undefined input', () => {
expect(formatSize(undefined as any)).toBe('--');
});
it('should use custom fraction digits', () => {
expect(formatSize(1536, 2)).toBe('1.50 KB');
expect(formatSize(1572864, 3)).toBe('1.500 MB');
});
});
describe('formatSpeed', () => {
it('should format speed in KB/s correctly', () => {
expect(formatSpeed(10 * 1024)).toBe('10.00 KB/s');
expect(formatSpeed(999.99 * 1024)).toBe('999.99 KB/s');
});
it('should format speed in MB/s correctly', () => {
expect(formatSpeed(1024 * 1024)).toBe('1.00 MB/s');
expect(formatSpeed(10240 * 1024)).toBe('10.00 MB/s');
});
it('should format speed in GB/s correctly', () => {
expect(formatSpeed(1048576 * 1024)).toBe('1.00 GB/s');
expect(formatSpeed(10485760 * 1024)).toBe('10.00 GB/s');
});
it('should handle edge cases', () => {
expect(formatSpeed(0)).toBe('0.00 Byte/s');
expect(formatSpeed(1000 * 1024)).toBe('1000.00 KB/s');
expect(formatSpeed(1000.01 * 1024)).toBe('0.98 MB/s');
});
it('should handle undefined input', () => {
expect(formatSpeed(undefined as any)).toBe('--');
});
it('should use custom fraction digits', () => {
expect(formatSpeed(1024, 3)).toBe('1.000 KB/s');
});
});
describe('formatTime', () => {
it('should format time in seconds correctly', () => {
expect(formatTime(30)).toBe('30.0 s');
expect(formatTime(59.9)).toBe('59.9 s');
});
it('should format time in minutes correctly', () => {
expect(formatTime(60)).toBe('1.0 min');
expect(formatTime(3599)).toBe('60.0 min');
});
it('should format time in hours correctly', () => {
expect(formatTime(3600)).toBe('1.00 h');
expect(formatTime(7200)).toBe('2.00 h');
});
it('should handle edge cases', () => {
expect(formatTime(0)).toBe('0.0 s');
expect(formatTime(59.99)).toBe('60.0 s');
expect(formatTime(3599.99)).toBe('60.0 min');
});
it('should handle edge cases', () => {
expect(formatTime(0)).toBe('0.0 s');
expect(formatTime(59.99)).toBe('60.0 s');
expect(formatTime(3599.99)).toBe('60.0 min');
});
it('should handle non-number inputs', () => {
expect(formatTime('not a number' as any)).toBe('not a number');
expect(formatTime(undefined as any)).toBe('--');
});
});
describe('formatShortenNumber', () => {
it('should return the input if it is not a number', () => {
expect(formatShortenNumber('not a number')).toBe('not a number');
expect(formatShortenNumber(null)).toBe('--');
});
it('should format numbers less than 10,000 correctly', () => {
expect(formatShortenNumber(0)).toBe(0);
expect(formatShortenNumber(1234)).toBe('1,234');
expect(formatShortenNumber(9999)).toBe('9,999');
});
it('should format numbers between 10,000 and 9,999,999 correctly', () => {
expect(formatShortenNumber(10000)).toBe('10.0K');
expect(formatShortenNumber(123456)).toBe('123.5K');
expect(formatShortenNumber(9999999)).toBe('10000.0K');
});
it('should format numbers 10,000,000 and above correctly', () => {
expect(formatShortenNumber(10000000)).toBe('10.0M');
expect(formatShortenNumber(123456789)).toBe('123.5M');
expect(formatShortenNumber(9876543210)).toBe('9876.5M');
});
});
describe('formatNumber', () => {
it('should format numbers with commas correctly', () => {
expect(formatNumber(1000)).toBe('1,000');
expect(formatNumber(1000000)).toBe('1,000,000');
expect(formatNumber(1234567.89)).toBe('1,234,567.89');
});
it('should handle non-number inputs', () => {
expect(formatNumber('1000')).toBe('1,000');
expect(formatNumber('not a number')).toBe(Number.NaN.toString());
expect(formatNumber(0)).toBe('0');
expect(formatNumber(0, 1)).toBe('0.0');
expect(formatNumber(null)).toBe('--');
});
it('should handle fraction digits correctly', () => {
expect(formatNumber(1234.5678, 2)).toBe('1,234.57');
expect(formatNumber(1234.5678, 3)).toBe('1,234.568');
});
});
describe('formatIntergerNumber', () => {
it('should format numbers with commas correctly', () => {
expect(formatIntergerNumber(1000.12)).toBe('1,000');
expect(formatIntergerNumber(0)).toBe('0');
expect(formatIntergerNumber(null)).toBe('--');
});
});
describe('formatPrice', () => {
it('should format prices correctly', () => {
expect(formatPrice(1000)).toBe('1,000.00');
expect(formatPrice(1234.56)).toBe('1,234.56');
expect(formatPrice(0.99)).toBe('0.99');
expect(formatPrice(1000000.01)).toBe('1,000,000.01');
});
});
describe('formatPriceByCurrency', () => {
it('should format USD prices correctly', () => {
expect(formatPriceByCurrency(1000)).toBe('1,000.00');
expect(formatPriceByCurrency(1234.56, 'USD')).toBe('1,234.56');
});
it('should format CNY prices correctly', () => {
// Assuming CNY_TO_USD is 6.5
const CNY_TO_USD = 6.5;
expect(formatPriceByCurrency(1000, 'CNY')).toBe('140.06');
expect(formatPriceByCurrency(6500, 'CNY')).toBe('910.36');
});
it('should use the correct CNY_TO_USD conversion rate', () => {
const price = 1000;
const expectedCNY = formatPrice(price / CNY_TO_USD);
expect(formatPriceByCurrency(price, 'CNY')).toBe(expectedCNY);
});
});
describe('formatTokenNumber', () => {
it('should return "1K" for numbers between 1 and 1023', () => {
expect(formatTokenNumber(500)).toBe('1K');
expect(formatTokenNumber(1000)).toBe('1K');
});
it('should format numbers between 1024 and 41,983 correctly', () => {
expect(formatTokenNumber(1024)).toBe('1K');
expect(formatTokenNumber(2000)).toBe('2K');
expect(formatTokenNumber(2048)).toBe('2K');
expect(formatTokenNumber(4000)).toBe('4K');
expect(formatTokenNumber(4096)).toBe('4K');
expect(formatTokenNumber(32000)).toBe('32K');
expect(formatTokenNumber(65536)).toBe('64K');
});
it('should format numbers between 41,984 and 127,999 correctly', () => {
expect(formatTokenNumber(41984)).toBe('41K');
expect(formatTokenNumber(100000)).toBe('97K');
expect(formatTokenNumber(127999)).toBe('124K');
});
it('should return "128K" for 131,072', () => {
expect(formatTokenNumber(131072)).toBe('128K'); // Qwen
});
it('should format numbers between 128,000 and 999,999 correctly', () => {
expect(formatTokenNumber(128000)).toBe('128K');
expect(formatTokenNumber(200000)).toBe('200K'); // Claude
expect(formatTokenNumber(999999)).toBe('999K');
});
it('should format numbers 1,000,000 and above correctly', () => {
expect(formatTokenNumber(1000000)).toBe('1M');
expect(formatTokenNumber(1024000)).toBe('1M');
expect(formatTokenNumber(1048576)).toBe('1M'); // Gemini Flash
expect(formatTokenNumber(2000000)).toBe('2M');
expect(formatTokenNumber(2097152)).toBe('2M'); // Gemini Pro
});
});
describe('formatDate', () => {
it('should format date correctly', () => {
const date = new Date('2023-05-15T12:00:00Z');
expect(formatDate(date)).toBe('2023-05-15');
});
it('should handle undefined input', () => {
expect(formatDate(undefined)).toBe('--');
});
it('should use dayjs for formatting', () => {
const date = new Date('2023-05-15T12:00:00Z');
const expectedFormat = dayjs(date).format('YYYY-MM-DD');
expect(formatDate(date)).toBe(expectedFormat);
});
});
});