forked from Karanjot786/TermUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress.test.ts
More file actions
136 lines (111 loc) · 4.13 KB
/
Copy pathstress.test.ts
File metadata and controls
136 lines (111 loc) · 4.13 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
// packages/adapters/src/chalk/stress.test.ts
// Stress tests for chalk adapter under high-volume output.
import { describe, it, expect, afterEach } from 'vitest';
import { chalkToTermUI } from './index.js';
function buildAnsiString(sequence: string, text: string): string {
return `${sequence}${text}\x1B[0m`;
}
const MIXED_SEQUENCES = [
'\x1B[31m', // red
'\x1B[32m', // green
'\x1B[33m', // yellow
'\x1B[34m', // blue
'\x1B[35m', // magenta
'\x1B[36m', // cyan
'\x1B[1m', // bold
'\x1B[4m', // underline
'\x1B[7m', // reverse
'\x1B[9m', // strikethrough
];
function mixedAnsi(text: string): string {
let result = text;
for (const seq of MIXED_SEQUENCES) {
result = buildAnsiString(seq, result);
}
return result;
}
describe('chalk adapter stress tests', () => {
afterEach(() => {
delete process.env.NO_COLOR;
});
it('handles 1,000 rapid successive calls without error', () => {
const originalNoColor = process.env.NO_COLOR;
delete process.env.NO_COLOR;
const texts = Array.from({ length: 1000 }, (_, i) => `line-${i}`);
const ansiTexts = texts.map((t, i) => {
const seq = MIXED_SEQUENCES[i % MIXED_SEQUENCES.length];
return buildAnsiString(seq, t);
});
expect(() => {
for (const input of ansiTexts) {
chalkToTermUI(input);
}
}).not.toThrow();
});
it('handles 5,000 rapid successive calls without error or slowdown', () => {
const originalNoColor = process.env.NO_COLOR;
delete process.env.NO_COLOR;
const texts = Array.from({ length: 5000 }, (_, i) => `output-${i}`);
const start = performance.now();
for (const text of texts) {
chalkToTermUI(mixedAnsi(text));
}
const elapsed = performance.now() - start;
// 5,000 mixed-ANSI calls should complete in under 500ms
expect(elapsed).toBeLessThan(500);
});
it('produces consistent output at 1,000-call scale', () => {
const originalNoColor = process.env.NO_COLOR;
delete process.env.NO_COLOR;
const results: string[] = [];
for (let i = 0; i < 1000; i++) {
results.push(chalkToTermUI(mixedAnsi(`item-${i}`)));
}
// Each result should contain the original text
expect(results[0]).toContain('item-0');
expect(results[999]).toContain('item-999');
// All results should have ANSI codes intact (no NO_COLOR)
for (const result of results) {
expect(result).toContain('\x1B[');
}
});
it('strips ANSI when NO_COLOR is set during high-volume calls', () => {
process.env.NO_COLOR = '1';
const texts = Array.from({ length: 1000 }, (_, i) => `line-${i}`);
const ansiTexts = texts.map((t, i) => {
const seq = MIXED_SEQUENCES[i % MIXED_SEQUENCES.length];
return buildAnsiString(seq, t);
});
const results: string[] = [];
for (const input of ansiTexts) {
results.push(chalkToTermUI(input));
}
// All results should be stripped of ANSI codes
for (const result of results) {
expect(result).not.toContain('\x1B[');
expect(result).toMatch(/^line-\d+$/);
}
});
it('handles deeply nested ANSI sequences', () => {
const originalNoColor = process.env.NO_COLOR;
delete process.env.NO_COLOR;
// Stack all sequences 10 times
let deeplyNested = 'text';
for (let i = 0; i < 10; i++) {
deeplyNested = mixedAnsi(deeplyNested);
}
// Should not throw and should preserve text content
const result = chalkToTermUI(deeplyNested);
expect(result).toBeDefined();
expect(result.length).toBeGreaterThan(0);
});
it('handles empty strings at high volume', () => {
const originalNoColor = process.env.NO_COLOR;
delete process.env.NO_COLOR;
expect(() => {
for (let i = 0; i < 1000; i++) {
chalkToTermUI('');
}
}).not.toThrow();
});
});