@checksum-ai/runtime 2.0.24-alpha.1 → 2.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,502 +0,0 @@
1
- const fs = require("fs");
2
- const { join } = require("path");
3
-
4
- // Args
5
- const on = process.argv[2] !== "off";
6
-
7
- // -------- [Modifiers] -------- //
8
-
9
- // Amends the file with the given entry point text and append text
10
- // When "on" is true, the append text is added to the entry point,
11
- // otherwise the append text is completely removed from the file
12
- function amend(filePath, entryPointText, appendText) {
13
- const data = fs.readFileSync(filePath, "utf8");
14
- if (!data.includes(entryPointText)) {
15
- throw new Error("Entry point not found!", entryPointText);
16
- }
17
- // Ignore if the append text is already present
18
- if (on && data.includes(appendText)) {
19
- return;
20
- }
21
- // Add or clear according to on state
22
- const result = on
23
- ? data.replace(entryPointText, entryPointText + appendText)
24
- : data.replace(appendText, "");
25
-
26
- // Write
27
- fs.writeFileSync(filePath, result, "utf8");
28
- }
29
-
30
- // Replaces content.
31
- // When "on" is true, the new content is written to the file replacing the original content,
32
- // otherwise the original content is restored.
33
- function replaceContent(filePath, originalContent, newContent) {
34
- // Read the file content
35
- const fileContent = fs.readFileSync(filePath, "utf8");
36
-
37
- // add a marker for newContent that can be later recognized for "off" state
38
- newContent = `/* checksumai */ ${newContent}`;
39
-
40
- if (on && fileContent.includes(newContent)) {
41
- return;
42
- }
43
-
44
- // Join the lines back into a single string
45
- const updatedContent = on
46
- ? fileContent.replace(originalContent, newContent)
47
- : fileContent.replace(newContent, originalContent);
48
-
49
- // Write the modified content back to the file
50
- fs.writeFileSync(filePath, updatedContent, "utf8");
51
- }
52
-
53
- function doesFileExist(filePath) {
54
- if (!fs.existsSync(filePath)) {
55
- console.warn("File not found", filePath);
56
- return false;
57
- }
58
- return true;
59
- }
60
-
61
- // -------- [Modifications] -------- //
62
-
63
- // Remove conditions for injecting Playwright scripts
64
- function alwaysInjectScripts(projectRoot) {
65
- const file = join(
66
- projectRoot,
67
- "node_modules/playwright-core/lib/server/browserContext.js"
68
- );
69
- if (!doesFileExist(file)) {
70
- return;
71
- }
72
-
73
- const originalContent = 'if ((0, import_debug.debugMode)() === "console")';
74
-
75
- const newContent = "";
76
-
77
- replaceContent(file, originalContent, newContent);
78
- }
79
-
80
- // Add implementation for generateSelectorAndLocator and inject to Playwright console API
81
- function addGenerateSelectorAndLocator(projectRoot) {
82
- const file = join(
83
- projectRoot,
84
- "node_modules/playwright-core/lib/generated/injectedScriptSource.js"
85
- );
86
- if (!doesFileExist(file)) {
87
- return;
88
- }
89
- const entryPointText1 = "this._generateLocator(element, language),\\n ";
90
- const appendText1 =
91
- "generateSelectorAndLocator: (element, language) => this._generateSelectorAndLocator(element, language),\\n asLocator,\\n ";
92
- amend(file, entryPointText1, appendText1);
93
-
94
- const entryPointText2 = `return asLocator(language || "javascript", selector);\\n }\\n `;
95
- const appendText2 =
96
- '_generateSelectorAndLocator(element, language) {\\n if (!(element instanceof Element))\\n throw new Error(`Usage: playwright.locator(element).`);\\n const selector = this._injectedScript.generateSelectorSimple(element);\\n return {selector, locator: asLocator(language || \\"javascript\\", selector)};\\n }\\n ';
97
- amend(file, entryPointText2, appendText2);
98
- }
99
-
100
- // -------- [Runtime modifications] -------- //
101
-
102
- function expect(projectRoot) {
103
- const file = join(
104
- projectRoot,
105
- "node_modules/playwright/lib/matchers/expect.js"
106
- );
107
- if (!doesFileExist(file)) {
108
- return;
109
- }
110
- let originalContent, newContent;
111
-
112
- // originalContent = `return (...args) => {
113
- // const testInfo = (0, _globals.currentTestInfo)();`;
114
- // newContent = `return (...args) => {
115
- // let noSoft = false;
116
- // if (args.find(arg=>arg==='no-soft')){
117
- // noSoft = true;
118
- // args.pop();
119
- // }
120
- // const testInfo = (0, _globals.currentTestInfo)();`;
121
- // replaceContent(file, originalContent, newContent);
122
-
123
- // originalContent = `step.complete({
124
- // error
125
- // })`;
126
- // newContent = `step.complete({
127
- // error,
128
- // noSoft
129
- // })`;
130
- // replaceContent(file, originalContent, newContent);
131
-
132
- // originalContent = `if (this._info.isSoft) testInfo._failWithError(error);else throw error;`;
133
- // newContent = `if (this._info.isSoft && !noSoft) testInfo._failWithError(error);else throw error;`;
134
- // replaceContent(file, originalContent, newContent);
135
- }
136
-
137
- function testInfo(projectRoot) {
138
- const file = join(
139
- projectRoot,
140
- "node_modules/playwright/lib/worker/testInfo.js"
141
- );
142
- if (!doesFileExist(file)) {
143
- return;
144
- }
145
- let originalContent, newContent;
146
- let entryPointText, appendText;
147
-
148
- // originalContent = `const filteredStack = (0, _util.filteredStackTrace)((0, _utils.captureRawStack)());`;
149
- // newContent = `const filteredStack = (0, _util.filteredStackTrace)((0, _utils.captureRawStack)().filter(s=>!s.includes('@checksum-ai/runtime')));`;
150
- // replaceContent(file, originalContent, newContent);
151
-
152
- entryPointText = `location = location || filteredStack[0];`;
153
- appendText = `\nif (this._checksumInternal) {
154
- location = undefined;
155
- this._checksumInternal = false;
156
- }
157
- if (this._checksumNoLocation){
158
- location = undefined;
159
- }`;
160
- amend(file, entryPointText, appendText);
161
-
162
- originalContent = `if (childStep.error && childStep.infectParentStepsWithError) {`;
163
- newContent = `if (childStep.error && childStep.infectParentStepsWithError && !step.preventInfectParentStepsWithError) {`;
164
- replaceContent(file, originalContent, newContent);
165
-
166
- originalContent = `_failWithError(error) {`;
167
- newContent = `addError(error, message) {
168
- const serialized = (0, import_util2.testInfoError)(error);
169
- serialized.message = [message, serialized.message].join('\\n\\n');
170
- serialized.stack = [message, serialized.stack].join('\\n\\n');
171
- const step = error[stepSymbol];
172
- if (step && step.boxedStack) serialized.stack = \`\${error.name}: \${error.message}\\n\${(0, import_utils.stringifyStackFrames)(step.boxedStack).join('\\n')}\`;
173
- this.errors.push(serialized);
174
- }
175
- _failWithError(error) {`;
176
- replaceContent(file, originalContent, newContent);
177
- }
178
-
179
- function testType(projectRoot) {
180
- const file = join(
181
- projectRoot,
182
- "node_modules/playwright/lib/common/testType.js"
183
- );
184
- if (!doesFileExist(file)) {
185
- return;
186
- }
187
-
188
- entryPointText = `return await (0, import_utils.currentZone)().with("stepZone", step).run(async () => {`;
189
- // entryPointText = `return await _utils.zones.run('stepZone', step, async () => {`;
190
- appendText = `\nif (options.obtainStep){
191
- options.obtainStep(step);
192
- }`;
193
- amend(file, entryPointText, appendText);
194
- }
195
-
196
- function indexContent(projectRoot) {
197
- const file = join(projectRoot, "node_modules/playwright/lib/index.js");
198
- if (!doesFileExist(file)) {
199
- return;
200
- }
201
- let originalContent, newContent;
202
- originalContent = `const browser = await playwright[browserName].launch();`;
203
- newContent = `
204
- let browser = playwright[browserName];
205
- try {
206
- const { playwrightExtra } = workerInfo?.project?.use || {};
207
- if (playwrightExtra && playwrightExtra?.length) {
208
- const pw = require("playwright-extra")
209
- const PupeteerExtraPlugin = require("puppeteer-extra-plugin").PuppeteerExtraPlugin
210
- const chromium = pw.chromium;
211
-
212
- playwrightExtra.forEach((plugin, i) => {
213
- try {
214
- if(!(plugin instanceof PupeteerExtraPlugin)){
215
- console.warn(\`Plugin at index \${i} is not an instance of PupeteerExtraPlugin\`);
216
- }
217
- chromium.use(plugin);
218
- } catch (e) {
219
- console.warn(e);
220
- }
221
- });
222
- browser = chromium;
223
- }
224
- } catch (e) {
225
- console.warn(
226
- "CHECKSUM: Failed to load Playwright Extra, using Playwright instead.",
227
- e
228
- );
229
- }
230
- browser = await browser.launch();
231
- `;
232
- replaceContent(file, originalContent, newContent);
233
- }
234
-
235
- function channelOwner(projectRoot) {
236
- const file = join(
237
- projectRoot,
238
- "node_modules/playwright-core/lib/client/channelOwner.js"
239
- );
240
- if (!doesFileExist(file)) {
241
- return;
242
- }
243
- let originalContent, newContent;
244
- let entryPointText, appendText;
245
-
246
- entryPointText = `async _wrapApiCall(func, options) {`;
247
-
248
- appendText = `\nif (this._checksumInternal){
249
- options = options || {};
250
- options.internal = true;
251
- }`;
252
- amend(file, entryPointText, appendText);
253
-
254
- entryPointText = `const apiZone = { title: options?.title, apiName: stackTrace.apiName, frames: stackTrace.frames, internal: options?.internal ?? false, reported: false, userData: void 0, stepId: void 0 };`;
255
-
256
- appendText = `\nif (!apiZone.internal && this._checksumTitle){
257
- apiZone.apiName = this._checksumTitle;
258
- this._checksumTitle = undefined;
259
- }
260
- if (!apiZone.apiName){
261
- options = options || {};
262
- options.internal = true;
263
- apiZone.internal = true;
264
- apiZone.reported = true;
265
- }
266
- if (apiZone.apiName && apiZone.apiName.startsWith('proxy')) {
267
- apiZone.apiName = apiZone.apiName.replace('proxy', 'page');
268
- }`;
269
- amend(file, entryPointText, appendText);
270
- }
271
-
272
- function stackTrace(projectRoot) {
273
- const file = join(
274
- projectRoot,
275
- "node_modules/playwright-core/lib/utils/isomorphic/stackTrace.js"
276
- );
277
- if (!doesFileExist(file)) {
278
- return;
279
- }
280
-
281
- let originalContent, newContent;
282
-
283
- // Create a regex for getting a file for each line in the stacktrace that overrides the original regex
284
- // This regex is only used for getting files, the original regex is sitll used for the rest of the content
285
- originalContent = `let file = match[7];`;
286
- const fileRe = /(\/.*?\.[a-zA-Z0-9]+)(?=:\d+:\d+)/;
287
- newContent = `
288
- const fileRe = new RegExp(${JSON.stringify(fileRe.source)}, "${
289
- fileRe.flags
290
- }");
291
- const m = fileRe.exec(match[0] ?? "");
292
- let file = m ? m[1] : undefined;
293
- `;
294
- replaceContent(file, originalContent, newContent);
295
-
296
- // Filter out checksum-ai/runtime from stack traces
297
- originalContent = `return stack.split("\\n");`;
298
- newContent = `return stack.split("\\n").filter(s=>!s.includes('@checksum-ai/runtime'));`;
299
- replaceContent(file, originalContent, newContent);
300
- }
301
-
302
- function reportTraceFile(projectRoot) {
303
- const file = join(
304
- projectRoot,
305
- "node_modules/playwright/lib/reporters/html.js"
306
- );
307
- if (!doesFileExist(file)) {
308
- return;
309
- }
310
-
311
- let originalContent, newContent;
312
-
313
- originalContent = `const buffer = import_fs.default.readFileSync(a.path);`;
314
- newContent = `let buffer = import_fs.default.readFileSync(a.path);
315
- if (a.name === "trace") {
316
- let retries = 2;
317
- while (!buffer.slice(0,100).toString().startsWith("checksum-playwright-trace") && retries > 0) {
318
- Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 5000);
319
- buffer = import_fs.default.readFileSync(a.path)
320
- retries--;
321
- }
322
- }`;
323
- replaceContent(file, originalContent, newContent);
324
- }
325
-
326
- function htmlReporter(projectRoot) {
327
- const file = join(
328
- projectRoot,
329
- "node_modules/playwright/lib/reporters/html.js"
330
- );
331
- if (!doesFileExist(file)) {
332
- return;
333
- }
334
-
335
- let originalContent, newContent;
336
-
337
- // Filter out runtime files from snippet generation
338
- originalContent = `function createSnippets(stepsInFile) {
339
- for (const file of stepsInFile.keys()) {
340
- let source;
341
- try {
342
- source = import_fs.default.readFileSync(file, "utf-8") + "\\n//";
343
- } catch (e) {
344
- continue;
345
- }`;
346
- newContent = `function createSnippets(stepsInFile) {
347
- for (const file of stepsInFile.keys()) {
348
- // Skip runtime files to reduce report size
349
- if (file.includes('@checksum-ai/runtime') || file.includes('node_modules')) {
350
- continue;
351
- }
352
- let source;
353
- try {
354
- source = import_fs.default.readFileSync(file, "utf-8") + "\\n//";
355
- } catch (e) {
356
- continue;
357
- }`;
358
- replaceContent(file, originalContent, newContent);
359
-
360
- // Also filter from error codeframe generation
361
- originalContent = `function createErrorCodeframe(message, location) {
362
- let source;
363
- try {
364
- source = import_fs.default.readFileSync(location.file, "utf-8") + "\\n//";
365
- } catch (e) {
366
- return;
367
- }`;
368
- newContent = `function createErrorCodeframe(message, location) {
369
- // Skip runtime files to reduce report size
370
- if (location.file && (location.file.includes('@checksum-ai/runtime') || location.file.includes('node_modules'))) {
371
- return;
372
- }
373
- let source;
374
- try {
375
- source = import_fs.default.readFileSync(location.file, "utf-8") + "\\n//";
376
- } catch (e) {
377
- return;
378
- }`;
379
- replaceContent(file, originalContent, newContent);
380
-
381
- // Filter out steps with locations in runtime/node_modules files completely
382
- originalContent = `_createTestStep(dedupedStep, result) {
383
- const { step, duration, count } = dedupedStep;
384
- const skipped = dedupedStep.step.annotations?.find((a) => a.type === "skip");
385
- let title = step.title;
386
- if (skipped)
387
- title = \`\${title} (skipped\${skipped.description ? ": " + skipped.description : ""})\`;
388
- const testStep = {
389
- title,
390
- startTime: step.startTime.toISOString(),
391
- duration,
392
- steps: dedupeSteps(step.steps).map((s) => this._createTestStep(s, result)),
393
- attachments: step.attachments.map((s) => {
394
- const index = result.attachments.indexOf(s);
395
- if (index === -1)
396
- throw new Error("Unexpected, attachment not found");
397
- return index;
398
- }),
399
- location: this._relativeLocation(step.location),
400
- error: step.error?.message,
401
- count,
402
- skipped: !!skipped
403
- };
404
- if (step.location)
405
- this._stepsInFile.set(step.location.file, testStep);
406
- return testStep;
407
- }`;
408
- newContent = `_createTestStep(dedupedStep, result) {
409
- const { step, duration, count } = dedupedStep;
410
- // Skip "Evaluate" steps with locations in runtime/node_modules files
411
- if (step.location && step.title === "Evaluate" && (step.location.file.includes('@checksum-ai/runtime') || step.location.file.includes('node_modules'))) {
412
- // Return null to indicate this step should be filtered out
413
- return null;
414
- }
415
- const skipped = dedupedStep.step.annotations?.find((a) => a.type === "skip");
416
- let title = step.title;
417
- if (skipped)
418
- title = \`\${title} (skipped\${skipped.description ? ": " + skipped.description : ""})\`;
419
- const testStep = {
420
- title,
421
- startTime: step.startTime.toISOString(),
422
- duration,
423
- steps: dedupeSteps(step.steps).map((s) => this._createTestStep(s, result)).filter(s => s !== null),
424
- attachments: step.attachments.map((s) => {
425
- const index = result.attachments.indexOf(s);
426
- if (index === -1)
427
- throw new Error("Unexpected, attachment not found");
428
- return index;
429
- }),
430
- location: this._relativeLocation(step.location),
431
- error: step.error?.message,
432
- count,
433
- skipped: !!skipped
434
- };
435
- if (step.location)
436
- this._stepsInFile.set(step.location.file, testStep);
437
- return testStep;
438
- }`;
439
- replaceContent(file, originalContent, newContent);
440
-
441
- // Also filter steps when creating test results
442
- originalContent = `steps: dedupeSteps(result.steps).map((s) => this._createTestStep(s, result)),`;
443
- newContent = `steps: dedupeSteps(result.steps).map((s) => this._createTestStep(s, result)).filter(s => s !== null),`;
444
- replaceContent(file, originalContent, newContent);
445
-
446
- // Normalize locations for steps from runtime/index.js - hide location if it's index.js from runtime
447
- originalContent = `_relativeLocation(location) {
448
- if (!location)
449
- return void 0;
450
- const file = (0, import_utils.toPosixPath)(import_path.default.relative(this._config.rootDir, location.file));
451
- return {
452
- file,
453
- line: location.line,
454
- column: location.column
455
- };
456
- }`;
457
- newContent = `_relativeLocation(location) {
458
- if (!location)
459
- return void 0;
460
- // Hide location for steps from runtime/index.js to reduce clutter
461
- if (location.file && location.file.includes('@checksum-ai/runtime') && location.file.endsWith('index.js')) {
462
- return void 0;
463
- }
464
- const file = (0, import_utils.toPosixPath)(import_path.default.relative(this._config.rootDir, location.file));
465
- return {
466
- file,
467
- line: location.line,
468
- column: location.column
469
- };
470
- }`;
471
- replaceContent(file, originalContent, newContent);
472
- }
473
-
474
- // -------- [Run] -------- //
475
-
476
- const isRuntime = true || process.env.RUNTIME === "true";
477
-
478
- function run(projectPath) {
479
- try {
480
- if (fs.existsSync(projectPath)) {
481
- alwaysInjectScripts(projectPath);
482
- addGenerateSelectorAndLocator(projectPath);
483
- if (isRuntime) {
484
- expect(projectPath);
485
- testInfo(projectPath);
486
- testType(projectPath);
487
- channelOwner(projectPath);
488
- stackTrace(projectPath);
489
- indexContent(projectPath);
490
- reportTraceFile(projectPath);
491
- htmlReporter(projectPath);
492
- }
493
- } else {
494
- console.warn("Project path not found", projectPath);
495
- }
496
- } catch (e) {
497
- // ignore
498
- console.error(e);
499
- }
500
- }
501
-
502
- module.exports = run;