Repository navigation
Expand file tree
/
Copy pathsetup.js
More file actions
290 lines (243 loc) · 6.7 KB
/
Copy pathsetup.js
File metadata and controls
290 lines (243 loc) · 6.7 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
/**
* Test setup and global configuration
*/
import { afterAll, afterEach, beforeAll, beforeEach } from 'vitest';
// Global test configuration
const TEST_CONFIG = {
timeout: 30000, // 30 seconds
retries: 2,
bail: false
};
// Global test state
let testStartTime;
let testMetrics = {
totalTests: 0,
passedTests: 0,
failedTests: 0,
skippedTests: 0
};
/**
* Global setup - runs once before all tests
*/
beforeAll(async () => {
console.log('🚀 Starting Xget test suite...');
testStartTime = Date.now();
// Initialize test environment
await setupTestEnvironment();
// Verify test dependencies
await verifyTestDependencies();
console.log('✅ Test environment initialized');
});
/**
* Global teardown - runs once after all tests
*/
afterAll(async () => {
const duration = Date.now() - testStartTime;
console.log('\n📊 Test Suite Summary:');
console.log(` Duration: ${duration}ms`);
console.log(` Total: ${testMetrics.totalTests}`);
console.log(` Passed: ${testMetrics.passedTests}`);
console.log(` Failed: ${testMetrics.failedTests}`);
console.log(` Skipped: ${testMetrics.skippedTests}`);
// Cleanup test environment
await cleanupTestEnvironment();
console.log('🏁 Test suite completed');
});
/**
* Setup before each test
*/
beforeEach(async context => {
testMetrics.totalTests++;
// Reset any global state
resetGlobalState();
// Setup test-specific environment
await setupTestCase(context);
});
/**
* Cleanup after each test
*/
afterEach(async context => {
// Update test metrics based on result
if (context.meta?.result === 'pass') {
testMetrics.passedTests++;
} else if (context.meta?.result === 'fail') {
testMetrics.failedTests++;
} else if (context.meta?.result === 'skip') {
testMetrics.skippedTests++;
}
// Cleanup test-specific resources
await cleanupTestCase(context);
});
/**
* Setup test environment
*/
async function setupTestEnvironment() {
// Verify Cloudflare Workers environment
if (typeof globalThis.fetch === 'undefined') {
throw new Error('fetch is not available in test environment');
}
// Setup global test utilities
globalThis.TEST_CONFIG = TEST_CONFIG;
// Initialize performance monitoring
if (typeof performance === 'undefined') {
globalThis.performance = {
now: () => Date.now()
};
}
// Setup console overrides for testing
setupConsoleOverrides();
}
/**
* Verify test dependencies
*/
async function verifyTestDependencies() {
const requiredGlobals = ['Request', 'Response', 'Headers', 'URL', 'URLSearchParams'];
for (const global of requiredGlobals) {
if (typeof globalThis[global] === 'undefined') {
throw new Error(`Required global ${global} is not available`);
}
}
// Verify SELF is available for Cloudflare Workers testing
try {
const { SELF } = await import('cloudflare:test');
if (!SELF) {
throw new Error('SELF is not available');
}
} catch (error) {
console.warn('Warning: Cloudflare Workers test environment not available');
}
}
/**
* Setup console overrides for testing
*/
function setupConsoleOverrides() {
const originalConsole = { ...console };
// Store original console methods
globalThis.originalConsole = originalConsole;
// Override console methods for testing
console.warn = (...args) => {
if (process.env.NODE_ENV !== 'test' || process.env.VERBOSE_TESTS) {
originalConsole.warn(...args);
}
};
console.log = (...args) => {
if (process.env.NODE_ENV !== 'test' || process.env.VERBOSE_TESTS) {
originalConsole.log(...args);
}
};
}
/**
* Reset global state between tests
*/
function resetGlobalState() {
// Clear any cached data
if (globalThis.testCache) {
globalThis.testCache.clear();
}
// Reset performance counters
if (globalThis.testPerformance) {
globalThis.testPerformance.reset();
}
}
/**
* Setup individual test case
*/
async function setupTestCase(context) {
// Create test-specific cache
globalThis.testCache = new Map();
// Setup test-specific performance monitoring
globalThis.testPerformance = {
marks: new Map(),
mark: name => {
globalThis.testPerformance.marks.set(name, performance.now());
},
measure: (name, startMark, endMark) => {
const start = globalThis.testPerformance.marks.get(startMark) || 0;
const end = globalThis.testPerformance.marks.get(endMark) || performance.now();
return end - start;
},
reset: () => {
globalThis.testPerformance.marks.clear();
}
};
// Mark test start time
globalThis.testPerformance.mark('test-start');
}
/**
* Cleanup individual test case
*/
async function cleanupTestCase(context) {
// Mark test end time
globalThis.testPerformance.mark('test-end');
// Log test performance if verbose
if (process.env.VERBOSE_TESTS) {
const duration = globalThis.testPerformance.measure('test-duration', 'test-start', 'test-end');
console.log(`Test "${context.meta?.name}" took ${duration.toFixed(2)}ms`);
}
// Cleanup test-specific resources
if (globalThis.testCache) {
globalThis.testCache.clear();
}
if (globalThis.testPerformance) {
globalThis.testPerformance.reset();
}
}
/**
* Cleanup test environment
*/
async function cleanupTestEnvironment() {
// Restore original console
if (globalThis.originalConsole) {
Object.assign(console, globalThis.originalConsole);
delete globalThis.originalConsole;
}
// Cleanup global test utilities
delete globalThis.TEST_CONFIG;
delete globalThis.testCache;
delete globalThis.testPerformance;
}
/**
* Custom test utilities available globally
*/
globalThis.testUtils = {
/**
* Create a test timeout
*/
timeout: ms =>
new Promise((_, reject) => {
setTimeout(() => reject(new Error(`Test timed out after ${ms}ms`)), ms);
}),
/**
* Wait for a condition to be true
*/
waitFor: async (condition, timeout = 5000, interval = 100) => {
const start = Date.now();
while (Date.now() - start < timeout) {
if (await condition()) {
return true;
}
await new Promise(resolve => setTimeout(resolve, interval));
}
throw new Error(`Condition not met within ${timeout}ms`);
},
/**
* Retry a function with exponential backoff
*/
retry: async (fn, maxRetries = 3, baseDelay = 100) => {
let lastError;
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error) {
lastError = error;
if (i < maxRetries - 1) {
const delay = baseDelay * Math.pow(2, i);
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
throw lastError;
}
};
// Export test configuration for use in other files
export { TEST_CONFIG, testMetrics };