@dev-blinq/cucumber_client 1.0.1366-stage → 1.0.1367-stage

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,301 +1,5 @@
1
1
  import { loadArgs, showUsage, validateCLIArg } from "../cli_helpers.js";
2
- import { io } from "socket.io-client";
3
- import { BVTRecorder } from "./bvt_recorder.js";
4
- import { compareWithScenario } from "../code_gen/duplication_analysis.js";
5
- import { getAppDataDir } from "./app_dir.js";
6
- import { readdir } from "fs/promises";
7
- import socketLogger from "../utils/socket_logger.js";
8
-
9
- let port = process.env.EDITOR_PORT || 3003;
10
- const WS_URL = "http://localhost:" + port;
11
-
12
- const responseSize = (response) => {
13
- try {
14
- if (typeof response !== "string") {
15
- return new Blob([JSON.stringify(response)]).size;
16
- } else {
17
- return new Blob([response]).size;
18
- }
19
- } catch {
20
- return -1;
21
- }
22
- };
23
-
24
- class PromisifiedSocketServer {
25
- constructor(socket, routes) {
26
- this.socket = socket;
27
- this.routes = routes;
28
- }
29
- init() {
30
- this.socket.on("request", async (data) => {
31
- const { event, input, id, roomId, socketId } = data;
32
- if (event !== "recorderWindow.getCurrentChromiumPath") {
33
- socketLogger.info("Received request", { event, input, id, roomId, socketId });
34
- }
35
- try {
36
- const handler = this.routes[event];
37
- if (!handler) {
38
- console.error(`No handler found for event: ${event}`);
39
- return;
40
- }
41
- const response = await handler(input);
42
- if (event !== "recorderWindow.getCurrentChromiumPath") {
43
- socketLogger.info(`Sending response for ${event}, ${responseSize(response)} bytes`);
44
- }
45
- this.socket.emit("response", { id, value: response, roomId, socketId });
46
- } catch (error) {
47
- socketLogger.error("Error handling request", {
48
- event,
49
- input,
50
- id,
51
- roomId,
52
- socketId,
53
- error: error instanceof Error ? `${error.message}\n${error.stack}` : error,
54
- });
55
- this.socket.emit("response", {
56
- id,
57
- error: {
58
- message: error?.message,
59
- code: error?.code,
60
- info: error?.info,
61
- stack: error?.stack,
62
- },
63
- roomId,
64
- socketId,
65
- });
66
- }
67
- });
68
- }
69
- }
70
-
71
- const init = ({ envName, projectDir, roomId, TOKEN }) => {
72
- console.log("Connecting to " + WS_URL);
73
- const socket = io(WS_URL);
74
- socketLogger.init(socket, { context: "BVTRecorder", eventName: "BVTRecorder.log" });
75
- socket.on("connect", () => {
76
- socketLogger.info("Connected to BVTRecorder server");
77
- console.log("Connected to BVTRecorder server");
78
- });
79
- socket.on("disconnect", () => {
80
- socketLogger.info("Disconnected from server");
81
- console.log("Disconnected from server");
82
- });
83
- socket.emit("joinRoom", { id: roomId, window: "cucumber_client/bvt_recorder" });
84
- const recorder = new BVTRecorder({
85
- envName,
86
- projectDir,
87
- TOKEN,
88
- sendEvent: (event, data) => {
89
- socketLogger.info("Sending event", { event, data, roomId });
90
- socket.emit(event, data, roomId);
91
- },
92
- logger: socketLogger,
93
- });
94
- recorder
95
- .openBrowser()
96
- .then(() => {
97
- socketLogger.info("BVTRecorder.browserOpened");
98
- socket.emit("BVTRecorder.browserOpened", null, roomId);
99
- })
100
- .catch((e) => {
101
- socketLogger.error("BVTRecorder.browserLaunchFailed", e);
102
- socket.emit("BVTRecorder.browserLaunchFailed", e, roomId);
103
- });
104
- const timeOutForFunction = async (promise, timeout = 5000) => {
105
- const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve(), timeout));
106
-
107
- try {
108
- const res = await Promise.race([promise, timeoutPromise]);
109
- return res;
110
- } catch (error) {
111
- console.error(error);
112
- socketLogger.error(error);
113
- throw error;
114
- }
115
- };
116
-
117
- const promisifiedSocketServer = new PromisifiedSocketServer(socket, {
118
- "recorderWindow.openBrowser": async (input) => {
119
- return recorder
120
- .openBrowser(input)
121
- .then(() => {
122
- socketLogger.info("BVTRecorder.browserOpened");
123
- console.info("BVTRecorder.browserOpened");
124
- socket.emit("BVTRecorder.browserOpened", { roomId, window: "cucumber_client/bvt_recorder" });
125
- })
126
- .catch((e) => {
127
- socketLogger.error("Error opening browser", e);
128
- console.error("BVTRecorder.browserLaunchFailed", e);
129
- socket.emit("BVTRecorder.browserLaunchFailed", { roomId, window: "cucumber_client/bvt_recorder" });
130
- });
131
- },
132
- "recorderWindow.closeBrowser": async (input) => {
133
- return recorder.closeBrowser(input);
134
- },
135
- "recorderWindow.reOpenBrowser": async (input) => {
136
- return recorder
137
- .reOpenBrowser(input)
138
- .then(() => {
139
- socketLogger.info("BVTRecorder.browserOpened");
140
- console.log("BVTRecorder.browserOpened");
141
- socket.emit("BVTRecorder.browserOpened", null, roomId);
142
- })
143
- .catch((e) => {
144
- socketLogger.info("BVTRecorder.browserLaunchFailed");
145
- console.error("BVTRecorder.browserLaunchFailed", e);
146
- socket.emit("BVTRecorder.browserLaunchFailed", null, roomId);
147
- });
148
- },
149
- "recorderWindow.startRecordingInput": async (input) => {
150
- return timeOutForFunction(recorder.startRecordingInput(input));
151
- },
152
- "recorderWindow.stopRecordingInput": async (input) => {
153
- return timeOutForFunction(recorder.stopRecordingInput(input));
154
- },
155
- "recorderWindow.startRecordingText": async (input) => {
156
- // console.log("--- {{ }} -- : recorderWindow.startRecordingText", input);
157
- return timeOutForFunction(recorder.startRecordingText(input));
158
- },
159
- "recorderWindow.stopRecordingText": async (input) => {
160
- return timeOutForFunction(recorder.stopRecordingText(input));
161
- },
162
- "recorderWindow.startRecordingContext": async (input) => {
163
- return timeOutForFunction(recorder.startRecordingContext(input));
164
- },
165
- "recorderWindow.stopRecordingContext": async (input) => {
166
- return timeOutForFunction(recorder.stopRecordingContext(input));
167
- },
168
- "recorderWindow.runStep": async (input) => {
169
- return recorder.runStep(input);
170
- },
171
- "recorderWindow.saveScenario": async (input) => {
172
- return recorder.saveScenario(input);
173
- },
174
- "recorderWindow.getImplementedSteps": async (input) => {
175
- // console.log("recorderWindow.getImplementedSteps", input);
176
- return (await recorder.getImplementedSteps(input)).implementedSteps;
177
- },
178
- "recorderWindow.getImplementedScenarios": async (input) => {
179
- // console.log("recorderWindow.getImplementedScenarios", input);
180
- return (await recorder.getImplementedSteps(input)).scenarios;
181
- },
182
- "recorderWindow.getCurrentChromiumPath": async () => {
183
- // console.log("recorderWindow.getCurrentChromiumPath");
184
- return await recorder.getCurrentChromiumPath();
185
- },
186
- "recorderWindow.overwriteTestData": async (input) => {
187
- // console.log("recorderWindow.overwriteTestData");
188
- return await recorder.overwriteTestData(input);
189
- },
190
- "recorderWindow.generateStepName": async (input) => {
191
- return recorder.generateStepName(input);
192
- },
193
- "recorderWindow.getFeatureAndScenario": async (input) => {
194
- return recorder.generateScenarioAndFeatureNames(input);
195
- },
196
- "recorderWindow.generateCommandName": async (input) => {
197
- return recorder.generateCommandName(input);
198
- },
199
- "recorderWindow.loadTestData": async (input) => {
200
- return recorder.loadTestData(input);
201
- },
202
- "recorderWindow.discard": async (input) => {
203
- return await recorder.discardTestData(input);
204
- },
205
- "recorderWindow.addToTestData": async (input) => {
206
- return await recorder.addToTestData(input);
207
- },
208
- "recorderWindow.getScenarios": async () => {
209
- return recorder.getScenarios();
210
- },
211
- "recorderWindow.setShouldTakeScreenshot": async (input) => {
212
- return recorder.setShouldTakeScreenshot(input);
213
- },
214
- "recorderWindow.compareWithScenario": async ({ projectDir, scenario }, roomId) => {
215
- return await compareWithScenario(getAppDataDir(projectDir), scenario);
216
- },
217
- "recorderWindow.getCommandsForImplementedStep": async (input) => {
218
- return recorder.getCommandsForImplementedStep(input);
219
- },
220
- "recorderWindow.getNumberOfOccurrences": async (input) => {
221
- return recorder.getNumberOfOccurrences(input);
222
- },
223
- "recorderWindow.getFakeParams": async ({ parametersMap }) => {
224
- return recorder.fakeParams(parametersMap);
225
- },
226
- "recorderWindow.abortExecution": async (input) => {
227
- return recorder.abortExecution(input);
228
- },
229
- "recorderWindow.pauseExecution": async (input) => {
230
- return recorder.pauseExecution(input);
231
- },
232
- "recorderWindow.resumeExecution": async (input) => {
233
- return recorder.resumeExecution(input);
234
- },
235
- "recorderWindow.loadExistingScenario": async (input) => {
236
- return recorder.loadExistingScenario(input);
237
- },
238
- "recorderWindow.findRelatedTextInAllFrames": async (input) => {
239
- return recorder.findRelatedTextInAllFrames(input);
240
- },
241
- "recorderWindow.getReportFolder": async (input) => {
242
- return recorder.getReportFolder();
243
- },
244
- "recorderWindow.getSnapshotFiles": async (input) => {
245
- const snapshotFolder = recorder.getSnapshotFolder();
246
- if (snapshotFolder) {
247
- const files = await readdir(snapshotFolder);
248
- const ymlFiles = files.filter((file) => file.endsWith(".yml") || file.endsWith(".yaml"));
249
- return { folder: snapshotFolder, files: ymlFiles };
250
- } else return { folder: null, files: [] };
251
- },
252
- "recorderWindow.getCurrentPageTitle": async (input) => {
253
- return await recorder.getCurrentPageTitle();
254
- },
255
- "recorderWindow.getCurrentPageUrl": async (input) => {
256
- return await recorder.getCurrentPageUrl();
257
- },
258
- "recorderWindow.sendAriaSnapshot": async (input) => {
259
- const snapshot = input?.snapshot;
260
- const deselect = input?.deselect;
261
- if (deselect === true) {
262
- return await recorder.deselectAriaElements();
263
- }
264
- if (snapshot !== null) {
265
- return await recorder.processAriaSnapshot(snapshot);
266
- }
267
- },
268
- "recorderWindow.revertMode": async (input) => {
269
- await recorder.revertMode();
270
- },
271
- "recorderWindow.setMode": async (input) => {
272
- const mode = input?.mode;
273
- return recorder.setMode(mode);
274
- },
275
- "recorderWindow.getStepsAndCommandsForScenario": async (input) => {
276
- return await recorder.getStepsAndCommandsForScenario(input);
277
- },
278
- "recorderWindow.getNetworkEvents": async (input) => {
279
- return await recorder.getNetworkEvents(input);
280
- },
281
- "recorderWindow.initExecution": async (input) => {
282
- return await recorder.initExecution(input);
283
- },
284
- "recorderWindow.cleanupExecution": async (input) => {
285
- return await recorder.cleanupExecution(input);
286
- },
287
- "recorderWindow.resetExecution": async (input) => {
288
- return await recorder.resetExecution(input);
289
- },
290
- "recorderWindow.stopRecordingNetwork": async (input) => {
291
- return recorder.stopRecordingNetwork(input);
292
- },
293
- });
294
- socket.on("targetBrowser.command.event", async (input) => {
295
- return recorder.onAction(input);
296
- });
297
- promisifiedSocketServer.init();
298
- };
2
+ import { BVTRecorderInit } from "./bvt_init.js";
299
3
 
300
4
  const usage = `Usage: node bvt_recorder.js <projectDir> <envName> <roomId>`;
301
5
  const args = loadArgs();
@@ -304,6 +8,7 @@ const envName = args[1].split("=")[1];
304
8
  const roomId = args[2];
305
9
  const shouldTakeScreenshot = args[3];
306
10
  const TOKEN = process.env.TOKEN;
11
+
307
12
  try {
308
13
  validateCLIArg(projectDir, "projectDir");
309
14
  validateCLIArg(envName, "envName");
@@ -315,14 +20,5 @@ try {
315
20
  } catch (error) {
316
21
  showUsage(error, usage);
317
22
  }
318
- try {
319
- init({
320
- envName,
321
- projectDir,
322
- roomId,
323
- TOKEN,
324
- shouldTakeScreenshot: shouldTakeScreenshot ? shouldTakeScreenshot === "true" : false,
325
- });
326
- } catch (error) {
327
- console.error(error);
328
- }
23
+
24
+ BVTRecorderInit({ envName, projectDir, roomId, TOKEN });