-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-console-batch.js
More file actions
256 lines (225 loc) · 6.69 KB
/
test-console-batch.js
File metadata and controls
256 lines (225 loc) · 6.69 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
const { chromium } = require('playwright');
const fs = require('fs');
const path = require('path');
const os = require('os');
const http = require('http');
const SERVER_PORT = Number.parseInt(process.env.SERVER_PORT || '38123', 10);
const SERVER_URL = process.env.SERVER_URL || `http://localhost:${SERVER_PORT}`;
const BATCH_SIZE = Number.parseInt(process.env.BATCH_SIZE || '5', 10);
const LOAD_TIMEOUT = Number.parseInt(process.env.LOAD_TIMEOUT || '5000', 10);
const SETTLE_MS = Number.parseInt(process.env.SETTLE_MS || '10000', 10);
const DEFAULT_LIST_PATH = path.join(__dirname, 'test', 'batch-files.txt');
const LIST_PATH = process.env.FILE_LIST || process.argv[2] || DEFAULT_LIST_PATH;
const RUN_ID = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
if (!Number.isFinite(BATCH_SIZE) || BATCH_SIZE < 1) {
console.log('BATCH_SIZE must be a positive integer');
process.exit(1);
}
if (!Number.isFinite(LOAD_TIMEOUT) || LOAD_TIMEOUT < 1000) {
console.log('LOAD_TIMEOUT must be >= 1000 ms');
process.exit(1);
}
if (!Number.isFinite(SETTLE_MS) || SETTLE_MS < 0) {
console.log('SETTLE_MS must be >= 0 ms');
process.exit(1);
}
function writeStdout(line) {
try {
fs.writeSync(1, line + '\n');
} catch (err) {
process.stdout.write(line + '\n');
}
}
function httpRequest(options) {
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
const chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => {
resolve({
status: res.statusCode,
body: Buffer.concat(chunks).toString('utf8')
});
});
});
req.on('error', reject);
req.end();
});
}
async function checkServer() {
try {
const response = await httpRequest({
hostname: 'localhost',
port: SERVER_PORT,
path: '/healthcheck',
method: 'GET'
});
if (response.status !== 200) {
throw new Error(`Healthcheck failed with status ${response.status}`);
}
} catch (error) {
writeStdout(`Server is not running at ${SERVER_URL}`);
writeStdout('Start the server with: npm run server');
process.exit(1);
}
}
function normalizeListEntry(raw) {
if (!raw) return null;
let value = raw.trim();
if (!value || value.startsWith('#')) return null;
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
value = value.slice(1, -1).trim();
}
if (!value) return null;
if (value === '~') {
value = os.homedir();
} else if (value.startsWith('~/')) {
value = path.join(os.homedir(), value.slice(2));
}
if (!path.isAbsolute(value)) {
value = path.resolve(process.cwd(), value);
}
return value;
}
function loadFileList(listPath) {
if (!fs.existsSync(listPath)) {
writeStdout(`File list not found: ${listPath}`);
writeStdout('Provide a list file path as the first argument or set FILE_LIST.');
process.exit(1);
}
const lines = fs.readFileSync(listPath, 'utf8').split(/\r?\n/);
const files = lines
.map(normalizeListEntry)
.filter((entry) => entry);
if (files.length === 0) {
writeStdout(`File list is empty: ${listPath}`);
process.exit(1);
}
return files;
}
function validateFilePaths(files) {
const missing = [];
for (const filePath of files) {
if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) {
missing.push(filePath);
}
}
if (missing.length > 0) {
writeStdout('The following files do not exist:');
missing.forEach((filePath) => writeStdout(`- ${filePath}`));
process.exit(1);
}
}
function buildBaseEntry(filePath, fileIndex) {
return {
ts: new Date().toISOString(),
runId: RUN_ID,
fileIndex,
filePath
};
}
let hasFailed = false;
let browser = null;
function fail(reason, entry) {
if (hasFailed) return;
hasFailed = true;
const payload = {
...entry,
reason,
fatal: true
};
writeStdout('[error] browser log error detected');
writeStdout(JSON.stringify(payload));
process.exit(1);
}
function attachPageLogging(page, filePath, fileIndex) {
page.on('console', (msg) => {
const type = msg.type();
if (type !== 'error' && type !== 'assert') {
return;
}
const location = msg.location();
const entry = {
...buildBaseEntry(filePath, fileIndex),
event: 'console',
level: type,
text: msg.text(),
url: location.url || null,
line: Number.isFinite(location.lineNumber) ? location.lineNumber : null,
column: Number.isFinite(location.columnNumber) ? location.columnNumber : null
};
fail('console error', entry);
});
page.on('pageerror', (error) => {
const entry = {
...buildBaseEntry(filePath, fileIndex),
event: 'pageerror',
level: 'error',
text: error.message,
stack: error.stack || null
};
fail('pageerror', entry);
});
page.on('crash', () => {
const entry = {
...buildBaseEntry(filePath, fileIndex),
event: 'crash',
level: 'error',
text: 'Page crashed'
};
fail('page crashed', entry);
});
}
async function openDocument(filePath, fileIndex) {
if (hasFailed) return;
const context = await browser.newContext({
viewport: { width: 1280, height: 720 }
});
const page = await context.newPage();
attachPageLogging(page, filePath, fileIndex);
const url = `${SERVER_URL}/open?filepath=${encodeURIComponent(filePath)}`;
try {
await page.goto(url, { waitUntil: 'load', timeout: LOAD_TIMEOUT });
await page.waitForTimeout(SETTLE_MS);
} catch (error) {
if (!hasFailed) {
const entry = {
...buildBaseEntry(filePath, fileIndex),
event: 'navigation_error',
level: 'error',
text: error.message,
url
};
fail('navigation error', entry);
}
} finally {
try {
await context.close();
} catch (err) {
if (!hasFailed) {
writeStdout(`Context close warning: ${err.message}`);
}
}
}
}
async function run() {
await checkServer();
const files = loadFileList(LIST_PATH);
validateFilePaths(files);
writeStdout(`Batch run ${RUN_ID} starting (${files.length} files, batch size ${BATCH_SIZE})`);
writeStdout(`Server: ${SERVER_URL}`);
writeStdout(`List: ${LIST_PATH}`);
browser = await chromium.launch({ headless: true });
for (let i = 0; i < files.length; i += BATCH_SIZE) {
if (hasFailed) return;
const batch = files.slice(i, i + BATCH_SIZE);
await Promise.all(batch.map((filePath, idx) => openDocument(filePath, i + idx)));
}
await browser.close();
writeStdout('Batch console log check completed with no errors.');
process.exit(0);
}
run().catch((error) => {
writeStdout(`Unhandled error: ${error.message}`);
process.exit(1);
});