@checksum-ai/runtime 1.1.10 → 1.1.12-alpha

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.
@@ -0,0 +1,265 @@
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
+ const originalContent =
73
+ "if ((0, _utils.debugMode)() === 'console') await this.extendInjectedScript(consoleApiSource.source);";
74
+
75
+ const newContent =
76
+ "await this.extendInjectedScript(consoleApiSource.source);";
77
+
78
+ replaceContent(file, originalContent, newContent);
79
+ }
80
+
81
+ // Add implementation for generateSelectorAndLocator and inject to Playwright console API
82
+ function addGenerateSelectorAndLocator(projectRoot) {
83
+ const file = join(
84
+ projectRoot,
85
+ "node_modules/playwright-core/lib/generated/consoleApiSource.js"
86
+ );
87
+ if (!doesFileExist(file)) {
88
+ return;
89
+ }
90
+ const entryPointText1 = "this._generateLocator(element, language),\\n ";
91
+ const appendText1 =
92
+ "generateSelectorAndLocator: (element, language) => this._generateSelectorAndLocator(element, language),\\n asLocator,\\n ";
93
+ amend(file, entryPointText1, appendText1);
94
+
95
+ const entryPointText2 =
96
+ 'return asLocator(language || \\"javascript\\", selector);\\n }\\n ';
97
+ const appendText2 =
98
+ '_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 ';
99
+ amend(file, entryPointText2, appendText2);
100
+ }
101
+
102
+ // -------- [Runtime modifications] -------- //
103
+
104
+ function expect(projectRoot) {
105
+ const file = join(
106
+ projectRoot,
107
+ "node_modules/playwright/lib/matchers/expect.js"
108
+ );
109
+ if (!doesFileExist(file)) {
110
+ return;
111
+ }
112
+ let originalContent, newContent;
113
+
114
+ // originalContent = `return (...args) => {
115
+ // const testInfo = (0, _globals.currentTestInfo)();`;
116
+ // newContent = `return (...args) => {
117
+ // let noSoft = false;
118
+ // if (args.find(arg=>arg==='no-soft')){
119
+ // noSoft = true;
120
+ // args.pop();
121
+ // }
122
+ // const testInfo = (0, _globals.currentTestInfo)();`;
123
+ // replaceContent(file, originalContent, newContent);
124
+
125
+ // originalContent = `step.complete({
126
+ // error
127
+ // })`;
128
+ // newContent = `step.complete({
129
+ // error,
130
+ // noSoft
131
+ // })`;
132
+ // replaceContent(file, originalContent, newContent);
133
+
134
+ // originalContent = `if (this._info.isSoft) testInfo._failWithError(error);else throw error;`;
135
+ // newContent = `if (this._info.isSoft && !noSoft) testInfo._failWithError(error);else throw error;`;
136
+ // replaceContent(file, originalContent, newContent);
137
+ }
138
+
139
+ function testInfo(projectRoot) {
140
+ const file = join(
141
+ projectRoot,
142
+ "node_modules/playwright/lib/worker/testInfo.js"
143
+ );
144
+ if (!doesFileExist(file)) {
145
+ return;
146
+ }
147
+ let originalContent, newContent;
148
+ let entryPointText, appendText;
149
+
150
+ // originalContent = `const filteredStack = (0, _util.filteredStackTrace)((0, _utils.captureRawStack)());`;
151
+ // newContent = `const filteredStack = (0, _util.filteredStackTrace)((0, _utils.captureRawStack)().filter(s=>!s.includes('@checksum-ai/runtime')));`;
152
+ // replaceContent(file, originalContent, newContent);
153
+
154
+ entryPointText = `data.location = data.location || filteredStack[0];`;
155
+ appendText = `\nif (this._checksumInternal) {
156
+ data.location = undefined;
157
+ this._checksumInternal = false;
158
+ }
159
+ if (this._checksumNoLocation){
160
+ data.location = undefined;
161
+ }`;
162
+ amend(file, entryPointText, appendText);
163
+
164
+ originalContent = `if (!step.error) {`;
165
+ newContent = `if (!step.error && !step.preventInfectParentStepsWithError) {`;
166
+ replaceContent(file, originalContent, newContent);
167
+
168
+ originalContent = `_failWithError(error) {`;
169
+ newContent = `addError(error, message) {
170
+ const serialized = (0, _util.serializeError)(error);
171
+ serialized.message = [message, serialized.message].join('\\n\\n');
172
+ serialized.stack = [message, serialized.stack].join('\\n\\n');
173
+ const step = error[stepSymbol];
174
+ if (step && step.boxedStack) serialized.stack = \`\${error.name}: \${error.message}\\n\${(0, _utils.stringifyStackFrames)(step.boxedStack).join('\\n')}\`;
175
+ this.errors.push(serialized);
176
+ }
177
+ _failWithError(error) {`;
178
+ replaceContent(file, originalContent, newContent);
179
+ }
180
+
181
+ function testType(projectRoot) {
182
+ const file = join(
183
+ projectRoot,
184
+ "node_modules/playwright/lib/common/testType.js"
185
+ );
186
+ if (!doesFileExist(file)) {
187
+ return;
188
+ }
189
+
190
+ entryPointText = `return await _utils.zones.run('stepZone', step, async () => {`;
191
+ appendText = `\nif (options.obtainStep){
192
+ options.obtainStep(step);
193
+ }`;
194
+ amend(file, entryPointText, appendText);
195
+ }
196
+
197
+ function channelOwner(projectRoot) {
198
+ const file = join(
199
+ projectRoot,
200
+ "node_modules/playwright-core/lib/client/channelOwner.js"
201
+ );
202
+ if (!doesFileExist(file)) {
203
+ return;
204
+ }
205
+ let originalContent, newContent;
206
+ let entryPointText, appendText;
207
+
208
+ entryPointText = `async _wrapApiCall(func, isInternal = false) {`;
209
+ appendText = `\nif (this._checksumInternal){
210
+ isInternal = true;
211
+ }`;
212
+ amend(file, entryPointText, appendText);
213
+
214
+ // originalContent = `const stack = (0, _stackTrace.captureRawStack)();`;
215
+ // newContent = `const stack = (0, _stackTrace.captureRawStack)().filter(s=>!s.includes('@checksum-ai/runtime'));`;
216
+ // replaceContent(file, originalContent, newContent);
217
+
218
+ entryPointText = `let apiName = stackTrace.apiName;`;
219
+ appendText = `\nif (!isInternal && this._checksumTitle){
220
+ apiName = this._checksumTitle;
221
+ this._checksumTitle = undefined;
222
+ }`;
223
+ amend(file, entryPointText, appendText);
224
+ }
225
+
226
+ function stackTrace(projectRoot) {
227
+ const file = join(
228
+ projectRoot,
229
+ "node_modules/playwright-core/lib/utils/stackTrace.js"
230
+ );
231
+ if (!doesFileExist(file)) {
232
+ return;
233
+ }
234
+
235
+ let originalContent, newContent;
236
+ originalContent = `return stack.split('\\n');`;
237
+ newContent = `return stack.split('\\n').filter(s=>!s.includes('@checksum-ai/runtime'));`;
238
+ replaceContent(file, originalContent, newContent);
239
+ }
240
+
241
+ // -------- [Run] -------- //
242
+
243
+ const isRuntime = true || process.env.RUNTIME === "true";
244
+
245
+ function run(projectPath) {
246
+ try {
247
+ if (fs.existsSync(projectPath)) {
248
+ alwaysInjectScripts(projectPath);
249
+ addGenerateSelectorAndLocator(projectPath);
250
+ if (isRuntime) {
251
+ expect(projectPath);
252
+ testInfo(projectPath);
253
+ testType(projectPath);
254
+ channelOwner(projectPath);
255
+ stackTrace(projectPath);
256
+ }
257
+ } else {
258
+ // console.warn("Project path not found", projectPath);
259
+ }
260
+ } catch (e) {
261
+ // ignore
262
+ }
263
+ }
264
+
265
+ module.exports = run;