-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathutils.test.ts
More file actions
706 lines (619 loc) · 26.2 KB
/
utils.test.ts
File metadata and controls
706 lines (619 loc) · 26.2 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
import { describe, expect, it, beforeAll } from "vitest";
import {
aNow,
checkSilenceUpdate,
cleanFileName,
formatBytes,
normalizeResponseHeaders,
stringMatching,
stripUndefined,
toCamelCase,
} from "./utils";
import { ltever, versionCompare } from "@App/pkg/utils/semver";
import { nextTimeDisplay, nextTimeInfo } from "./cron";
describe.concurrent("aNow", () => {
// aNow >= Date.now();
it.sequential("aNow is greater than or equal to Date.now()", () => {
const p1 = Date.now();
const p2 = aNow();
const p3 = Date.now();
expect(p2).greaterThanOrEqual(p1);
// aNow() 与 Date.now() 的值应非常接近
expect(p2).greaterThan(p1 - 0.01);
expect(p2).lessThan(p3 + 0.01);
});
// 在 vitest 环境只能实测 aNow() 的严格增加
it.sequential("aNow is Strictly Increasing", () => {
const p1 = [aNow(), aNow(), aNow(), aNow(), aNow(), aNow()];
expect(p1[0]).lessThan(p1[1]);
expect(p1[1]).lessThan(p1[2]);
expect(p1[2]).lessThan(p1[3]);
expect(p1[3]).lessThan(p1[4]);
expect(p1[4]).lessThan(p1[5]);
const p2 = [...p1].sort();
expect(p1).toEqual(p2);
});
});
const assertNextTimeInfo = (expr: string, date: Date, expected: any) => {
const actual = nextTimeInfo(expr, date);
const result = {
next: actual.next.toFormat(actual.format),
once: actual.once,
};
// 1) 失败时讯息包含 expr / expected / actual
// 2) 用 soft,方便一次看到多笔失败(可选)
expect
.soft(
result,
[
"",
"",
`expr: ${expr}`,
`date: ${date.toISOString()}`,
`expected: ${JSON.stringify(expected)}`,
`actual: ${JSON.stringify(result)}`,
"",
"",
].join("\n")
)
.toEqual(expected);
};
describe.concurrent("nextTimeDisplay ERROR SAFE", () => {
it.concurrent.each([
["* * * once * once"],
["* * once * once"],
["* once(2,4) once(4-5) * *"],
["* * 1 A *"],
["* once 1.2 * *"],
["* 3 1**2 * *"],
["* 1^2 F * *"],
["1 1 * *"],
["* 3"],
])("错误Cron表达式: %s", (expr) => {
// 确保无效表达式不会抛出异常
expect(() => nextTimeDisplay(expr)).not.toThrow();
});
});
describe.concurrent("nextTimeInfo1", () => {
const date = new Date("2025-12-17T11:47:17.629"); // 2025-12-17 11:47:17.629 (本地时区)
// 让程序先执行一下,避免超时问题
beforeAll(() => {
nextTimeDisplay("* * * * *");
});
it.concurrent.each([
["* * * * * *", { next: "2025-12-17 11:47:18", once: "" }],
["* * * * *", { next: "2025-12-17 11:48:00", once: "" }],
["* 1-3,5 * * *", { next: "2025-12-18 01:00:00", once: "" }],
["* 3-8/2 * * *", { next: "2025-12-18 03:00:00", once: "" }],
])("标准Cron表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["once * * * *", { next: "2025-12-17 11:48:00", once: "minute" }],
["* once * * *", { next: "2025-12-17 12:00:00", once: "hour" }],
["* * once * *", { next: "2025-12-18", once: "day" }],
["* * * once *", { next: "2026-01", once: "month" }],
["* * * * once", { next: "2025-12-22", once: "week" }],
["once(*) * * * *", { next: "2025-12-17 11:48:00", once: "minute" }],
["* once(*) * * *", { next: "2025-12-17 12:00:00", once: "hour" }],
["* * once(*) * *", { next: "2025-12-18", once: "day" }],
["* * * once(*) *", { next: "2026-01", once: "month" }],
["* * * * once(*)", { next: "2025-12-22", once: "week" }],
["once(5-7) * * * *", { next: "2025-12-17 12:05:00", once: "minute" }],
["* once(5-7) * * *", { next: "2025-12-18 05:00:00", once: "hour" }],
["* * once(5-7) * *", { next: "2026-01-05", once: "day" }],
["* * * once(5-7) *", { next: "2026-05", once: "month" }],
["* * * * once(5-7)", { next: "2025-12-26", once: "week" }],
])("once表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["once * * * *", { next: "2025-12-17 11:48:00", once: "minute" }],
["* once * * * *", { next: "2025-12-17 11:48:00", once: "minute" }],
["45 once * * * *", { next: "2025-12-17 11:48:45", once: "minute" }],
["once 1-3,5 * * *", { next: "2025-12-18 01:00:00", once: "minute" }],
["once 3-8/2 * * *", { next: "2025-12-18 03:00:00", once: "minute" }],
])("每分钟一次表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["* once * * *", { next: "2025-12-17 12:00:00", once: "hour" }],
["* * once * * *", { next: "2025-12-17 12:00:00", once: "hour" }],
["10 once * * *", { next: "2025-12-17 12:10:00", once: "hour" }],
["* 10 once * * *", { next: "2025-12-17 12:10:00", once: "hour" }],
["45 10 once * * *", { next: "2025-12-17 12:10:45", once: "hour" }],
["1-3,5 once * * *", { next: "2025-12-17 12:01:00", once: "hour" }],
["3-8/2 once * * *", { next: "2025-12-17 12:03:00", once: "hour" }],
])("每小时一次表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["* * once * *", { next: "2025-12-18", once: "day" }],
["* * * once * *", { next: "2025-12-18", once: "day" }],
["45 * * once * *", { next: "2025-12-18", once: "day" }],
["33,44 */7 * once * *", { next: "2025-12-18", once: "day" }],
["* * once * 3,6", { next: "2025-12-20", once: "day" }],
])("每天一次表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["* * * once *", { next: "2026-01", once: "month" }],
["* * * * once *", { next: "2026-01", once: "month" }],
["45 * * * once *", { next: "2026-01", once: "month" }],
["33,44 */7 * * once *", { next: "2026-01", once: "month" }],
["* * * once 3,6", { next: "2026-01", once: "month" }],
])("每月一次表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["* * * * once", { next: "2025-12-22", once: "week" }],
["* * * * * once", { next: "2025-12-22", once: "week" }],
["45 * * * * once", { next: "2025-12-22", once: "week" }],
["33,44 */7 * * * once", { next: "2025-12-22", once: "week" }],
["* * 5 * once", { next: "2026-01-05", once: "week" }],
])("每星期一次表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
});
describe.concurrent("nextTimeInfo2", () => {
const date = new Date("2025-12-31T23:59:59.999"); // 2025-12-31 23:59:59.999(本地时区)
// 让程序先执行一下,避免超时问题
beforeAll(() => {
nextTimeDisplay("* * * * *");
});
it.concurrent.each([
["* * * * * *", { next: "2026-01-01 00:00:00", once: "" }],
["* * * * *", { next: "2026-01-01 00:00:00", once: "" }],
])("标准 Cron 表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["once * * * *", { next: "2026-01-01 00:00:00", once: "minute" }],
["* once * * * *", { next: "2026-01-01 00:00:00", once: "minute" }],
["45 once * * * *", { next: "2026-01-01 00:00:45", once: "minute" }],
])("每分钟一次表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["* once * * *", { next: "2026-01-01 00:00:00", once: "hour" }],
["* * once * * *", { next: "2026-01-01 00:00:00", once: "hour" }],
["10 once * * *", { next: "2026-01-01 00:10:00", once: "hour" }],
["* 10 once * * *", { next: "2026-01-01 00:10:00", once: "hour" }],
["45 10 once * * *", { next: "2026-01-01 00:10:45", once: "hour" }],
])("每小时一次表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["* * once * *", { next: "2026-01-01", once: "day" }],
["* * * once * *", { next: "2026-01-01", once: "day" }],
["45 * * once * *", { next: "2026-01-01", once: "day" }],
])("每天一次表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["* * * once *", { next: "2026-01", once: "month" }],
["* * * * once *", { next: "2026-01", once: "month" }],
["45 * * * once *", { next: "2026-01", once: "month" }],
])("每月一次表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
it.concurrent.each([
["* * * * once", { next: "2026-01-05", once: "week" }],
["* * * * * once", { next: "2026-01-05", once: "week" }],
["45 * * * * once", { next: "2026-01-05", once: "week" }],
])("每星期一次表达式: %s", (expr, expected) => {
assertNextTimeInfo(expr, date, expected);
});
});
describe.concurrent("ltever", () => {
it.concurrent("semver", () => {
expect(ltever("1.0.0", "1.0.1")).toBe(true);
expect(ltever("1.0.0", "1.0.0")).toBe(true);
expect(ltever("1.0.1", "1.0.0")).toBe(false);
expect(ltever("3.2.01", "3.2.1")).toBe(true); // equal
expect(ltever("3.2.1", "3.2.01")).toBe(true); // equal
});
it.concurrent("any", () => {
expect(ltever("1.2.3.4", "1.2.3.4")).toBe(true);
expect(ltever("1.2.3.4", "1.2.3.5")).toBe(true);
expect(ltever("1.2.3.4", "1.2.3.3")).toBe(false);
});
});
describe.concurrent("versionCompare", () => {
const twoWayTest = (a: string, b: string, c: number) => versionCompare(a, b) === c && versionCompare(b, a) === -c;
it.concurrent("test", () => {
// 整数版本号
expect(twoWayTest("0", "1", -1)).toBe(true);
expect(twoWayTest("1", "3", -1)).toBe(true);
expect(twoWayTest("3", "2", 1)).toBe(true);
expect(twoWayTest("2", "16", -1)).toBe(true);
expect(twoWayTest("16", "19", -1)).toBe(true);
expect(twoWayTest("19", "20", -1)).toBe(true);
// 日期版本号
expect(twoWayTest("2022.10.01", "2022.10.03", -1)).toBe(true);
expect(twoWayTest("2022.10.03", "2022.10.02", 1)).toBe(true);
expect(twoWayTest("2022.10.02", "2022.09.22", 1)).toBe(true);
expect(twoWayTest("2022.09.22", "2022.09.02", 1)).toBe(true);
expect(twoWayTest("2022.09.02", "2022.09.11", -1)).toBe(true);
// 日期版本号
expect(twoWayTest("2022.10.1", "2022.10.3", -1)).toBe(true);
expect(twoWayTest("2022.10.3", "2022.10.2", 1)).toBe(true);
expect(twoWayTest("2022.10.2", "2022.9.22", 1)).toBe(true);
expect(twoWayTest("2022.9.22", "2022.9.2", 1)).toBe(true);
expect(twoWayTest("2022.9.2", "2022.9.11", -1)).toBe(true);
// 日期版本号
expect(twoWayTest("2022-10-01", "2022-10-03", -1)).toBe(true);
expect(twoWayTest("2022-10-03", "2022-10-02", 1)).toBe(true);
expect(twoWayTest("2022-10-02", "2022-09-22", 1)).toBe(true);
expect(twoWayTest("2022-09-22", "2022-09-02", 1)).toBe(true);
expect(twoWayTest("2022-09-02", "2022-09-11", -1)).toBe(true);
// 日期版本号
expect(twoWayTest("2022-10-1", "2022-10-3", -1)).toBe(true);
expect(twoWayTest("2022-10-3", "2022-10-2", 1)).toBe(true);
expect(twoWayTest("2022-10-2", "2022-9-22", 1)).toBe(true);
expect(twoWayTest("2022-9-22", "2022-9-2", 1)).toBe(true);
expect(twoWayTest("2022-9-2", "2022-9-11", -1)).toBe(true);
// 日期版本号
expect(twoWayTest("2022/10/01", "2022/10/03", -1)).toBe(true);
expect(twoWayTest("2022/10/03", "2022/10/02", 1)).toBe(true);
expect(twoWayTest("2022/10/02", "2022/09/22", 1)).toBe(true);
expect(twoWayTest("2022/09/22", "2022/09/02", 1)).toBe(true);
expect(twoWayTest("2022/09/02", "2022/09/11", -1)).toBe(true);
// 日期版本号
expect(twoWayTest("2022/10/1", "2022/10/3", -1)).toBe(true);
expect(twoWayTest("2022/10/3", "2022/10/2", 1)).toBe(true);
expect(twoWayTest("2022/10/2", "2022/9/22", 1)).toBe(true);
expect(twoWayTest("2022/9/22", "2022/9/2", 1)).toBe(true);
expect(twoWayTest("2022/9/2", "2022/9/11", -1)).toBe(true);
// 忽略非英文字符的变更进行对比
expect(twoWayTest("2022/10/1", "2022-10-3", -1)).toBe(true);
expect(twoWayTest("2022/10/3", "2022-10-2", 1)).toBe(true);
expect(twoWayTest("2022/10/2", "2022-9-22", 1)).toBe(true);
expect(twoWayTest("2022/9/22", "2022-9-2", 1)).toBe(true);
expect(twoWayTest("2022/9/2", "2022-9-11", -1)).toBe(true);
// semver 对比 (semver.compare)
expect(twoWayTest("3.2.01", "3.2.1", 0)).toBe(true); // equal
expect(twoWayTest("3.02.1", "3.2.1", 0)).toBe(true); // equal
expect(twoWayTest("3.02.0", "3.2.0", 0)).toBe(true); // equal
expect(twoWayTest("4.5.12", "4.5.15", -1)).toBe(true);
expect(twoWayTest("4.5.12", "4.5.12-alpha.1", 1)).toBe(true);
expect(twoWayTest("4.5.12", "4.5.12", 0)).toBe(true);
// 其他 等价
expect(twoWayTest("3.2", "3", 1)).toBe(true);
expect(twoWayTest("3.2.0", "3.2", 0)).toBe(true);
expect(twoWayTest("3.2.0.0", "3.2.0", 0)).toBe(true);
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/version/format
// 3.2 大于 3.2pre, 3.2alpha, ....
expect(twoWayTest("3.2pre", "3.2", -1)).toBe(true);
expect(twoWayTest("3.2pre-2", "3.2", -1)).toBe(true);
expect(twoWayTest("3.2alpha", "3.2", -1)).toBe(true);
expect(twoWayTest("3.2alpha1", "3.2", -1)).toBe(true);
expect(twoWayTest("3.2beta", "3.2", -1)).toBe(true);
expect(twoWayTest("3.2beta2", "3.2", -1)).toBe(true);
// 其他
expect(twoWayTest("3", "2.5.1", 1)).toBe(true);
expect(twoWayTest("2.5", "2.4.1", 1)).toBe(true);
expect(twoWayTest("3.2.1.0", "3.2.0", 1)).toBe(true);
expect(twoWayTest("v0.0.1.20210226040352", "v0.0.1.20210226040629", -1)).toBe(true);
expect(twoWayTest("v0.0.1.20210226040352", "v0.0.1.20210226040352a", 1)).toBe(true);
expect(twoWayTest("2018042901", "2018052201", -1)).toBe(true);
// 其他
expect(twoWayTest("1.2", "1.3", -1)).toBe(true);
expect(twoWayTest("1.3a", "1.3b", -1)).toBe(true);
expect(twoWayTest("1.3b", "1.3", -1)).toBe(true);
expect(twoWayTest("1.3a1", "1.3a2", -1)).toBe(true);
expect(twoWayTest("1.3a1", "1.3a2", -1)).toBe(true);
// 其他
expect(twoWayTest("v.3", "v.5", -1)).toBe(true);
expect(twoWayTest("v.9", "v.10", -1)).toBe(true);
expect(twoWayTest("v.10", "v.15", -1)).toBe(true);
// 其他
expect(twoWayTest("v3", "v5", -1)).toBe(true);
expect(twoWayTest("v9", "v10", -1)).toBe(true);
expect(twoWayTest("v10", "v15", -1)).toBe(true);
// 不区分大小写
expect(twoWayTest("v3", "V5", -1)).toBe(true);
expect(twoWayTest("v9", "V10", -1)).toBe(true);
expect(twoWayTest("v10", "V15", -1)).toBe(true);
expect(twoWayTest("V3", "v5", -1)).toBe(true);
expect(twoWayTest("V9", "v10", -1)).toBe(true);
expect(twoWayTest("V10", "v15", -1)).toBe(true);
// 其他
expect(twoWayTest("a3", "a5", -1)).toBe(true);
expect(twoWayTest("a9", "a10", -1)).toBe(true);
expect(twoWayTest("a10", "a15", -1)).toBe(true);
expect(twoWayTest("a15", "b1", -1)).toBe(true);
// npm版本号格式
expect(twoWayTest("1.0.0", "1.0.1", -1)).toBe(true);
expect(twoWayTest("1.0.0", "1.0.0", 0)).toBe(true);
expect(twoWayTest("1.0.1", "1.0.0", 1)).toBe(true);
// 一般格式
expect(twoWayTest("1.2.3.4", "1.2.3.4", 0)).toBe(true);
expect(twoWayTest("1.2.3.4", "1.2.3.5", -1)).toBe(true);
expect(twoWayTest("1.2.3.4", "1.2.3.3", 1)).toBe(true);
// 异常测试
expect(twoWayTest("", "", 0)).toBe(true);
expect(twoWayTest("", "0", 0)).toBe(true);
expect(twoWayTest("", "1", -1)).toBe(true);
// 中文版本号测试 (简单)
expect(twoWayTest("第1版", "第2版", -1)).toBe(true);
expect(twoWayTest("第3版", "第9版", -1)).toBe(true);
expect(twoWayTest("第9版", "第11版", -1)).toBe(true);
expect(twoWayTest("第9版.2", "第9版.3", -1)).toBe(true);
expect(twoWayTest("第9版.3", "第9版.11", -1)).toBe(true);
// 中文版本号测试 (简单)
expect(twoWayTest("版1", "版2", -1)).toBe(true);
expect(twoWayTest("版3", "版9", -1)).toBe(true);
expect(twoWayTest("版9", "版11", -1)).toBe(true);
expect(twoWayTest("版9.2", "版9.3", -1)).toBe(true);
expect(twoWayTest("版9.3", "版9.11", -1)).toBe(true);
});
});
describe.concurrent("checkSilenceUpdate", () => {
it.concurrent("true", () => {
expect(
checkSilenceUpdate(
{
connect: ["www.baidu.com"],
},
{
connect: ["www.baidu.com"],
}
)
).toBe(true);
expect(
checkSilenceUpdate(
{
connect: ["www.baidu.com", "scriptcat.org"],
},
{
connect: ["scriptcat.org"],
}
)
).toBe(true);
});
it.concurrent("false", () => {
expect(
checkSilenceUpdate(
{
connect: ["www.baidu.com"],
},
{
connect: ["www.google.com"],
}
)
).toBe(false);
expect(
checkSilenceUpdate(
{
connect: ["www.baidu.com"],
},
{
connect: ["www.baidu.com", "scriptcat.org"],
}
)
).toBe(false);
});
});
describe.concurrent("cleanFileName", () => {
it.concurrent("should replace illegal characters with dashes", () => {
expect(cleanFileName("file\\name")).toBe("file-name");
expect(cleanFileName("file:name")).toBe("file-name");
expect(cleanFileName("file*name")).toBe("file-name");
});
it.concurrent("should trim spaces", () => {
expect(cleanFileName(" file ")).toBe("file");
});
it.concurrent("should handle empty string", () => {
expect(cleanFileName("")).toBe("");
});
it.concurrent("should handle valid filename", () => {
expect(cleanFileName("valid_file.txt")).toBe("valid_file.txt");
expect(cleanFileName("file/name.txt")).toBe("file/name.txt");
});
});
describe.concurrent("stringMatching", () => {
describe.concurrent("无通配符的情况", () => {
it.concurrent("应该使用 includes 检查", () => {
expect(stringMatching("hello world", "hello")).toBe(true);
expect(stringMatching("hello world", "world")).toBe(true);
expect(stringMatching("hello world", "test")).toBe(false);
});
});
describe.concurrent("星号通配符 (*) - 匹配零个或多个字符", () => {
it.concurrent("go*le search 示例", () => {
// 基于需求:go*le search 能找到 gole search, google search, goule search, gommarle search
expect(stringMatching("gole search", "go*le search")).toBe(true);
expect(stringMatching("google search", "go*le search")).toBe(true);
expect(stringMatching("goule search", "go*le search")).toBe(true);
expect(stringMatching("gommarle search", "go*le search")).toBe(true);
expect(stringMatching("ga search", "go*le search")).toBe(false);
});
it.concurrent("其他星号匹配场景", () => {
expect(stringMatching("test file", "test*")).toBe(true);
expect(stringMatching("filename.txt", "*file*")).toBe(true);
expect(stringMatching("hello", "h*o")).toBe(true);
});
});
describe.concurrent("问号通配符 (?) - 匹配单个字符", () => {
it.concurrent("goog?e search 示例", () => {
// 基于需求:goog?e search 能找到 googae search, googbe search, google search
expect(stringMatching("googae search", "goog?e search")).toBe(true);
expect(stringMatching("googbe search", "goog?e search")).toBe(true);
expect(stringMatching("google search", "goog?e search")).toBe(true);
expect(stringMatching("goog search", "goog?e search")).toBe(false); // 缺少一个字符
expect(stringMatching("googlle search", "goog?e search")).toBe(false); // 多了一个字符
});
it.concurrent("其他问号匹配场景", () => {
expect(stringMatching("test", "t?st")).toBe(true);
expect(stringMatching("file.txt", "file.?xt")).toBe(true);
expect(stringMatching("hello", "h?llo")).toBe(true);
});
});
describe.concurrent("混合通配符", () => {
it.concurrent("星号和问号组合", () => {
expect(stringMatching("google search result", "go*g?e search*")).toBe(true);
expect(stringMatching("test file name", "t?st * name")).toBe(true);
});
});
describe.concurrent("边界情况", () => {
it.concurrent("空字符串和空模式", () => {
expect(stringMatching("", "")).toBe(true);
expect(stringMatching("test", "")).toBe(true);
expect(stringMatching("", "test")).toBe(false);
});
it.concurrent("特殊字符", () => {
expect(stringMatching("file.txt", "file.*")).toBe(true);
expect(stringMatching("user@domain.com", "user@*")).toBe(true);
});
});
});
describe.concurrent("toCamelCase", () => {
it.concurrent("应当将蛇形命名转换为驼峰命名", () => {
expect(toCamelCase("cloud_sync")).toBe("CloudSync");
expect(toCamelCase("cat_file_storage")).toBe("CatFileStorage");
expect(toCamelCase("enable_eslint")).toBe("EnableEslint");
expect(toCamelCase("eslint_config")).toBe("EslintConfig");
});
it.concurrent("应当正确处理单词配置键", () => {
expect(toCamelCase("language")).toBe("Language");
});
it.concurrent("应当正确处理多下划线配置键", () => {
expect(toCamelCase("editor_type_definition")).toBe("EditorTypeDefinition");
expect(toCamelCase("script_list_column_width")).toBe("ScriptListColumnWidth");
});
});
describe.concurrent("formatBytes", () => {
it.concurrent("应当正确格式化字节大小", () => {
// 0 字节
expect(formatBytes(0)).toBe("0 B");
// 字节单位
expect(formatBytes(100)).toBe("100.00 B");
expect(formatBytes(512)).toBe("512.00 B");
// KB 单位
expect(formatBytes(1024)).toBe("1.00 KB");
expect(formatBytes(2048)).toBe("2.00 KB");
expect(formatBytes(1536)).toBe("1.50 KB");
// MB 单位
expect(formatBytes(1048576)).toBe("1.00 MB");
expect(formatBytes(2097152)).toBe("2.00 MB");
expect(formatBytes(1234567)).toBe("1.18 MB");
// 自定义小数位数
expect(formatBytes(1536, 0)).toBe("2 KB");
expect(formatBytes(1536, 1)).toBe("1.5 KB");
});
});
describe.concurrent("normalizeResponseHeaders", () => {
it.concurrent("returns empty string for empty input", () => {
expect(normalizeResponseHeaders("")).toBe("");
});
it.concurrent("returns empty string for falsy-like empty string (only case possible with string type)", () => {
expect(normalizeResponseHeaders(String(""))).toBe("");
});
it.concurrent("keeps valid header lines and outputs name:value joined with CRLF", () => {
const input = "Content-Type: text/plain\nX-Test: abc\n";
expect(normalizeResponseHeaders(input)).toBe("Content-Type:text/plain\r\nX-Test:abc");
});
it.concurrent("accepts LF and CRLF line endings", () => {
const input = "A: 1\r\nB: 2\r\n";
expect(normalizeResponseHeaders(input)).toBe("A:1\r\nB:2");
});
it.concurrent("parses last line even without a trailing newline", () => {
const input = "A: 1\nB: 2";
expect(normalizeResponseHeaders(input)).toBe("A:1\r\nB:2");
});
it.concurrent("does NOT skip lines where value starts immediately after ':'", () => {
const input = "A:1\nB:2\n";
expect(normalizeResponseHeaders(input)).toBe("A:1\r\nB:2");
});
it.concurrent("works with non-ASCII characters", () => {
const input = "X-名前: 値\n";
expect(normalizeResponseHeaders(input)).toBe("X-名前:値");
});
it.concurrent("does not include a trailing CRLF at the end of output", () => {
const input = "A: 1\nB: 2\n";
expect(normalizeResponseHeaders(input).endsWith("\r\n")).toBe(false);
});
it.concurrent("standard test", () => {
const input = `content-type: text/html; charset=utf-8\r\n
server: Apache/2.4.41 (Ubuntu)\r\n
set-cookie: sessionid=abc123; Path=/; HttpOnly\r\n
cache-control: no-store, no-cache, must-revalidate\r\n
expires: Thu, 19 Nov 1981 08:52:00 GMT\r\n
pragma: no-cache\r\n
x-frame-options: SAMEORIGIN\r\n
x-content-type-options: nosniff\r\n
content-security-policy: default-src 'self'\r\n
access-control-allow-origin: *\r\n`
.split(/\s*\r\n\s*/)
.join("\r\n");
const expected = `content-type:text/html; charset=utf-8\r\n
server:Apache/2.4.41 (Ubuntu)\r\n
set-cookie:sessionid=abc123; Path=/; HttpOnly\r\n
cache-control:no-store, no-cache, must-revalidate\r\n
expires:Thu, 19 Nov 1981 08:52:00 GMT\r\n
pragma:no-cache\r\n
x-frame-options:SAMEORIGIN\r\n
x-content-type-options:nosniff\r\n
content-security-policy:default-src 'self'\r\n
access-control-allow-origin:*`
.split(/\s*\r\n\s*/)
.join("\r\n");
expect(normalizeResponseHeaders(input)).toBe(expected);
expect(normalizeResponseHeaders(expected)).toBe(expected);
});
});
describe("stripUndefined", () => {
// 基本功能:移除 undefined 值
it("应移除所有 undefined 属性", () => {
const input = { a: 1, b: undefined, c: "hello" };
const result = stripUndefined(input);
expect(result).toEqual({ a: 1, c: "hello" });
expect("b" in result).toBe(false);
});
// 保留 null 值
it("应保留 null 值", () => {
const input = { a: null, b: undefined };
const result = stripUndefined(input);
expect(result).toEqual({ a: null });
});
// 保留假值(false、0、空字符串)
it("应保留假值(false、0、空字符串)", () => {
const input = { a: false, b: 0, c: "", d: undefined };
const result = stripUndefined(input);
expect(result).toEqual({ a: false, b: 0, c: "" });
});
// 空对象
it("空对象应返回空对象", () => {
expect(stripUndefined({})).toEqual({});
});
// 全部为 undefined
it("所有属性均为 undefined 时应返回空对象", () => {
const input = { a: undefined, b: undefined };
expect(stripUndefined(input)).toEqual({});
});
// 无 undefined 时原样返回
it("无 undefined 属性时应返回相同内容", () => {
const input = { a: 1, b: "hello", c: true };
expect(stripUndefined(input)).toEqual(input);
});
// 嵌套对象(不递归处理)
it("嵌套对象内的 undefined 不应被递归移除", () => {
const input = { a: { b: undefined }, c: 1 };
const result = stripUndefined(input);
expect(result).toEqual({ a: { b: undefined }, c: 1 });
});
// 不修改原始对象
it("不应修改原始对象", () => {
const input = { a: 1, b: undefined };
stripUndefined(input);
expect(input).toEqual({ a: 1, b: undefined });
expect("b" in input).toBe(true);
});
// 保留数组值
it("应保留数组值", () => {
const input = { a: [1, 2, 3], b: undefined };
const result = stripUndefined(input);
expect(result).toEqual({ a: [1, 2, 3] });
});
});