-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-runner.js
More file actions
307 lines (261 loc) · 7.66 KB
/
test-runner.js
File metadata and controls
307 lines (261 loc) · 7.66 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
#!/usr/bin/env bun
// Comprehensive test runner for Sterad
import { spawn } from "child_process";
import { existsSync, readdirSync } from "fs";
import { join } from "path";
const TESTS_DIR = "./tests";
const TEST_TIMEOUT = 30000; // 30 seconds per test
// ANSI color codes for better output
const colors = {
reset: "\x1b[0m",
bright: "\x1b[1m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
};
function colorize(text, color) {
return `${colors[color]}${text}${colors.reset}`;
}
function log(message, color = "reset") {
console.log(colorize(message, color));
}
function logHeader(message) {
console.log();
log("=".repeat(60), "cyan");
log(message, "bright");
log("=".repeat(60), "cyan");
}
function logSubHeader(message) {
console.log();
log("-".repeat(40), "blue");
log(message, "blue");
log("-".repeat(40), "blue");
}
// Get all test files
function getTestFiles() {
if (!existsSync(TESTS_DIR)) {
log(`Tests directory not found: ${TESTS_DIR}`, "red");
return [];
}
const files = readdirSync(TESTS_DIR)
.filter((file) => file.startsWith("test-") && file.endsWith(".js"))
.map((file) => join(TESTS_DIR, file));
return files.sort();
}
// Run a single test file
async function runTest(testFile) {
return new Promise((resolve) => {
const testName = testFile.replace(/^.*\//, "").replace(/\.js$/, "");
log(`Running ${testName}...`, "yellow");
const startTime = Date.now();
const proc = spawn("bun", ["run", testFile], {
stdio: ["pipe", "pipe", "pipe"],
env: {
...process.env,
NODE_ENV: "test",
JWT_SECRET: "test-secret-key-32-characters-long-for-testing-purposes",
},
});
let stdout = "";
let stderr = "";
proc.stdout.on("data", (data) => {
stdout += data.toString();
});
proc.stderr.on("data", (data) => {
stderr += data.toString();
});
const timeout = setTimeout(() => {
proc.kill("SIGKILL");
resolve({
testFile,
testName,
success: false,
error: "Test timeout",
duration: Date.now() - startTime,
stdout: stdout.trim(),
stderr: stderr.trim(),
});
}, TEST_TIMEOUT);
proc.on("close", (code) => {
clearTimeout(timeout);
const duration = Date.now() - startTime;
resolve({
testFile,
testName,
success: code === 0,
exitCode: code,
duration,
stdout: stdout.trim(),
stderr: stderr.trim(),
});
});
proc.on("error", (error) => {
clearTimeout(timeout);
resolve({
testFile,
testName,
success: false,
error: error.message,
duration: Date.now() - startTime,
stdout: stdout.trim(),
stderr: stderr.trim(),
});
});
});
}
// Parse test output for pass/fail counts
function parseTestResults(stdout) {
const results = {
passed: 0,
failed: 0,
total: 0,
};
// Look for common test result patterns
const patterns = [
/(\d+) passed,?\s*(\d+) failed/i,
/Results:\s*(\d+) passed,?\s*(\d+) failed/i,
/Passed:\s*(\d+).*Failed:\s*(\d+)/i,
/✅.*?(\d+).*❌.*?(\d+)/,
];
for (const pattern of patterns) {
const match = stdout.match(pattern);
if (match) {
results.passed = parseInt(match[1]) || 0;
results.failed = parseInt(match[2]) || 0;
results.total = results.passed + results.failed;
break;
}
}
// Fallback: count ✅ and ❌ symbols
if (results.total === 0) {
const passMatches = stdout.match(/✅/g);
const failMatches = stdout.match(/❌/g);
results.passed = passMatches ? passMatches.length : 0;
results.failed = failMatches ? failMatches.length : 0;
results.total = results.passed + results.failed;
}
return results;
}
// Main test runner
async function runAllTests() {
logHeader("STERAD TEST SUITE");
const testFiles = getTestFiles();
if (testFiles.length === 0) {
log("No test files found!", "red");
process.exit(1);
}
log(`Found ${testFiles.length} test files:`, "cyan");
testFiles.forEach((file) => {
log(` • ${file.replace(/^.*\//, "")}`, "blue");
});
const results = [];
let totalPassed = 0;
let totalFailed = 0;
let totalDuration = 0;
logSubHeader("RUNNING TESTS");
// Run tests sequentially to avoid resource conflicts
for (const testFile of testFiles) {
const result = await runTest(testFile);
results.push(result);
totalDuration += result.duration;
if (result.success) {
const testResults = parseTestResults(result.stdout);
totalPassed += testResults.passed;
totalFailed += testResults.failed;
log(`✅ ${result.testName} - PASSED (${result.duration}ms)`, "green");
if (testResults.total > 0) {
log(
` ${testResults.passed} passed, ${testResults.failed} failed`,
"blue"
);
}
} else {
log(`❌ ${result.testName} - FAILED (${result.duration}ms)`, "red");
if (result.error) {
log(` Error: ${result.error}`, "red");
}
if (result.exitCode !== undefined) {
log(` Exit code: ${result.exitCode}`, "red");
}
}
}
logSubHeader("TEST RESULTS SUMMARY");
// Show detailed results for failed tests
const failedTests = results.filter((r) => !r.success);
if (failedTests.length > 0) {
log("FAILED TESTS:", "red");
failedTests.forEach((test) => {
log(`\n${test.testName}:`, "red");
if (test.stderr) {
log("STDERR:", "yellow");
log(test.stderr, "red");
}
if (test.stdout) {
log("STDOUT:", "yellow");
log(
test.stdout.substring(0, 500) +
(test.stdout.length > 500 ? "..." : ""),
"reset"
);
}
});
}
// Overall summary
const successfulTests = results.filter((r) => r.success).length;
const failedTestFiles = results.length - successfulTests;
log(
`\nTEST FILES: ${successfulTests}/${results.length} passed`,
successfulTests === results.length ? "green" : "red"
);
if (totalPassed > 0 || totalFailed > 0) {
log(
`INDIVIDUAL TESTS: ${totalPassed} passed, ${totalFailed} failed`,
totalFailed === 0 ? "green" : "red"
);
}
log(`TOTAL DURATION: ${totalDuration}ms`, "blue");
// Test coverage summary
logSubHeader("TEST COVERAGE");
log("✅ Bot Detection & User Agent Parsing", "green");
log("✅ JWT Authentication & Authorization", "green");
log("✅ Path Traversal Protection", "green");
log("✅ ReDoS Mitigation & Regex Safety", "green");
log("✅ Trust Boundary Validation", "green");
log("✅ Intercept Script Functionality", "green");
// Exit with appropriate code
const overallSuccess = failedTestFiles === 0 && totalFailed === 0;
if (overallSuccess) {
logHeader("🎉 ALL TESTS PASSED!");
log("Sterad is ready for production deployment.", "green");
process.exit(0);
} else {
logHeader("❌ SOME TESTS FAILED");
log("Please review and fix the failing tests before deployment.", "red");
process.exit(1);
}
}
// Handle CLI arguments
const args = process.argv.slice(2);
if (args.includes("--help") || args.includes("-h")) {
console.log(`
Sterad Test Runner
Usage: bun run test-runner.js [options]
Options:
--help, -h Show this help message
--verbose, -v Show verbose output
--timeout <ms> Set test timeout (default: 30000ms)
Environment Variables:
NODE_ENV Set to 'test' during test execution
JWT_SECRET JWT secret for authentication tests
`);
process.exit(0);
}
// Run the tests
runAllTests().catch((error) => {
log(`Test runner error: ${error.message}`, "red");
console.error(error);
process.exit(1);
});