genlayer 0.0.31 → 0.0.32-beta.0

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,636 @@
1
+ /* eslint-disable import/no-named-as-default-member */
2
+ import {jest} from "@jest/globals";
3
+ import inquirer from "inquirer";
4
+
5
+ import simulatorService from "../../src/lib/services/simulator";
6
+ import {initAction} from "../../src/commands/general/init";
7
+
8
+ // Default options for the action
9
+ const defaultActionOptions = {numValidators: 5, branch: "main"};
10
+
11
+ describe("init action", () => {
12
+ let error: jest.Mock<any>;
13
+ let log: jest.Mock<any>;
14
+ let inquirerPrompt: jest.Mock<any>;
15
+
16
+ let simServCheckRequirements: jest.Mock<any>;
17
+ let simServResetDockerContainers: jest.Mock<any>;
18
+ let simServResetDockerImages: jest.Mock<any>;
19
+ let simServDownloadSimulator: jest.Mock<any>;
20
+ let simServUpdateSimulator: jest.Mock<any>;
21
+ let simServgetAiProvidersOptions: jest.Mock<any>;
22
+ let simServConfigSimulator: jest.Mock<any>;
23
+ let simServRunSimulator: jest.Mock<any>;
24
+ let simServWaitForSimulator: jest.Mock<any>;
25
+ let simServPullOllamaModel: jest.Mock<any>;
26
+ let simServClearAccAndTxsDb: jest.Mock<any>;
27
+ let simServInitializeDatabase: jest.Mock<any>;
28
+ let simServDeleteAllValidators: jest.Mock<any>;
29
+ let simServCreateRandomValidators: jest.Mock<any>;
30
+ let simServOpenFrontend: jest.Mock<any>;
31
+ let simServRedEnvConfigVariable: jest.Mock<any>;
32
+
33
+ beforeEach(() => {
34
+ jest.clearAllMocks();
35
+
36
+ error = jest.spyOn(console, "error").mockImplementation(() => {}) as jest.Mock<any>;
37
+ log = jest.spyOn(console, "log").mockImplementation(() => {}) as jest.Mock<any>;
38
+ inquirerPrompt = jest.spyOn(inquirer, "prompt") as jest.Mock<any>;
39
+
40
+ simServCheckRequirements = jest.spyOn(simulatorService, "checkRequirements") as jest.Mock<any>;
41
+ simServResetDockerContainers = jest.spyOn(simulatorService, "resetDockerContainers") as jest.Mock<any>;
42
+ simServResetDockerImages = jest.spyOn(simulatorService, "resetDockerImages") as jest.Mock<any>;
43
+ simServDownloadSimulator = jest.spyOn(simulatorService, "downloadSimulator") as jest.Mock<any>;
44
+ simServUpdateSimulator = jest.spyOn(simulatorService, "updateSimulator") as jest.Mock<any>;
45
+ simServConfigSimulator = jest.spyOn(simulatorService, "configSimulator") as jest.Mock<any>;
46
+ simServgetAiProvidersOptions = jest.spyOn(simulatorService, "getAiProvidersOptions") as jest.Mock<any>;
47
+ simServRunSimulator = jest.spyOn(simulatorService, "runSimulator") as jest.Mock<any>;
48
+ simServWaitForSimulator = jest.spyOn(simulatorService, "waitForSimulatorToBeReady") as jest.Mock<any>;
49
+ simServPullOllamaModel = jest.spyOn(simulatorService, "pullOllamaModel") as jest.Mock<any>;
50
+ simServClearAccAndTxsDb = jest.spyOn(
51
+ simulatorService,
52
+ "clearAccountsAndTransactionsDatabase",
53
+ ) as jest.Mock<any>;
54
+ simServInitializeDatabase = jest.spyOn(simulatorService, "initializeDatabase") as jest.Mock<any>;
55
+ simServDeleteAllValidators = jest.spyOn(simulatorService, "deleteAllValidators") as jest.Mock<any>;
56
+ simServCreateRandomValidators = jest.spyOn(simulatorService, "createRandomValidators") as jest.Mock<any>;
57
+ simServOpenFrontend = jest.spyOn(simulatorService, "openFrontend") as jest.Mock<any>;
58
+ simServRedEnvConfigVariable = jest.spyOn(simulatorService, "readEnvConfigValue") as jest.Mock<any>;
59
+ });
60
+
61
+ afterEach(() => {
62
+ jest.restoreAllMocks();
63
+ });
64
+
65
+ test("if both requirements are missing, then the execution fails", async () => {
66
+ // Given
67
+ simServCheckRequirements.mockResolvedValue({git: false, docker: false});
68
+
69
+ // When
70
+ await initAction(defaultActionOptions, simulatorService);
71
+
72
+ // Then
73
+ expect(error).toHaveBeenCalledTimes(1);
74
+ expect(error).toHaveBeenCalledWith(
75
+ "Git and Docker are not installed. Please install them and try again.\n",
76
+ );
77
+ });
78
+
79
+ test("if only docker is missing, then the execution fails", async () => {
80
+ // Given
81
+ simServCheckRequirements.mockResolvedValue({git: true, docker: false});
82
+
83
+ // When
84
+ await initAction(defaultActionOptions, simulatorService);
85
+
86
+ // Then
87
+ expect(error).toHaveBeenCalledTimes(1);
88
+ expect(error).toHaveBeenCalledWith("Docker is not installed. Please install Docker and try again.\n");
89
+ });
90
+
91
+ test("if only git is missing, then the execution fails", async () => {
92
+ // Given
93
+ simServCheckRequirements.mockResolvedValue({git: false, docker: true});
94
+
95
+ // When
96
+ await initAction(defaultActionOptions, simulatorService);
97
+
98
+ // Then
99
+ expect(error).toHaveBeenCalledTimes(1);
100
+ expect(error).toHaveBeenCalledWith("Git is not installed. Please install Git and try again.\n");
101
+ });
102
+
103
+ test("if check requirements fail, then the execution aborts", async () => {
104
+ // Given
105
+ simServCheckRequirements.mockRejectedValue(new Error("Error"));
106
+
107
+ // When
108
+ await initAction(defaultActionOptions, simulatorService);
109
+
110
+ // Then
111
+ expect(error).toHaveBeenCalledTimes(1);
112
+ expect(error).toHaveBeenCalledWith(new Error("Error"));
113
+ });
114
+
115
+ test("if reset is not confirmed, abort", async () => {
116
+ // Given
117
+ inquirerPrompt.mockResolvedValue({confirmReset: false});
118
+ simServCheckRequirements.mockResolvedValue({git: true, docker: true});
119
+
120
+ // When
121
+ await initAction(defaultActionOptions, simulatorService);
122
+
123
+ // Then
124
+ expect(log).toHaveBeenCalledTimes(1);
125
+ expect(log).toHaveBeenNthCalledWith(1, "Aborted!");
126
+ });
127
+
128
+ test("if resetDockerContainers fail, then the execution aborts", async () => {
129
+ // Given
130
+ inquirerPrompt.mockResolvedValue({confirmReset: true});
131
+ simServResetDockerContainers.mockRejectedValue(new Error("Error"));
132
+
133
+ // When
134
+ await initAction(defaultActionOptions, simulatorService);
135
+
136
+ // Then
137
+ expect(error).toHaveBeenCalledTimes(1);
138
+ expect(error).toHaveBeenCalledWith(new Error("Error"));
139
+ });
140
+
141
+ test("if resetDockerImages fail, then the execution aborts", async () => {
142
+ // Given
143
+ inquirerPrompt.mockResolvedValue({confirmReset: true});
144
+ simServResetDockerImages.mockRejectedValue(new Error("Error"));
145
+
146
+ // When
147
+ await initAction(defaultActionOptions, simulatorService);
148
+
149
+ // Then
150
+ expect(error).toHaveBeenCalledTimes(1);
151
+ expect(error).toHaveBeenCalledWith(new Error("Error"));
152
+ });
153
+
154
+ test("if download is not confirmed, abort", async () => {
155
+ // Given
156
+ inquirerPrompt.mockResolvedValue({confirmReset: true, confirmDownload: false});
157
+ simServCheckRequirements.mockResolvedValue({git: true, docker: true});
158
+
159
+ // When
160
+ await initAction(defaultActionOptions, simulatorService);
161
+
162
+ // Then
163
+ expect(log).toHaveBeenCalledTimes(3);
164
+ expect(log).toHaveBeenNthCalledWith(3, "Aborted!");
165
+ });
166
+
167
+ test("if not already installed, it should download and install the simulator", async () => {
168
+ // Given
169
+ inquirerPrompt.mockResolvedValue({confirmReset: true, confirmDownload: true, selectedLlmProviders: []});
170
+ simServDownloadSimulator.mockResolvedValue({wasInstalled: false});
171
+ simServConfigSimulator.mockRejectedValue(new Error("Error")); // This will stop the execution
172
+
173
+ // When
174
+ await initAction(defaultActionOptions, simulatorService);
175
+
176
+ // Then
177
+ expect(simulatorService.downloadSimulator).toHaveBeenCalled();
178
+ expect(simulatorService.updateSimulator).not.toHaveBeenCalled();
179
+ });
180
+
181
+ test("if already installed, it should update the simulator", async () => {
182
+ // Given
183
+ inquirerPrompt.mockResolvedValue({confirmReset: true, confirmDownload: true, selectedLlmProviders: []});
184
+ simServDownloadSimulator.mockResolvedValue({wasInstalled: true});
185
+ simServUpdateSimulator.mockResolvedValue(true);
186
+ simServConfigSimulator.mockRejectedValue(new Error("Error")); // This will stop the execution
187
+
188
+ // When
189
+ await initAction(defaultActionOptions, simulatorService);
190
+
191
+ // Then
192
+ expect(simulatorService.downloadSimulator).toHaveBeenCalled();
193
+ expect(simulatorService.updateSimulator).toHaveBeenCalled();
194
+ });
195
+
196
+ test("should prompt for LLM providers and call configSimulator with selected providers", async () => {
197
+ // Given
198
+ inquirerPrompt.mockResolvedValue({
199
+ confirmReset: true,
200
+ confirmDownload: true,
201
+ selectedLlmProviders: ["openai", "heuristai"],
202
+ openai: "API_KEY1",
203
+ heuristai: "API_KEY2",
204
+ });
205
+ simServgetAiProvidersOptions.mockReturnValue([
206
+ {name: "OpenAI", value: "openai"},
207
+ {name: "Heurist", value: "heuristai"},
208
+ ]);
209
+ simServConfigSimulator.mockResolvedValue(true);
210
+ simServRunSimulator.mockRejectedValue(new Error("Error")); // This will stop the execution
211
+
212
+ // When
213
+ await initAction(defaultActionOptions, simulatorService);
214
+
215
+ // Then
216
+ expect(inquirerPrompt).toHaveBeenNthCalledWith(3, [
217
+ {
218
+ type: "checkbox",
219
+ name: "selectedLlmProviders",
220
+ message: "Select which LLM providers do you want to use:",
221
+ choices: [
222
+ {name: "OpenAI", value: "openai"},
223
+ {name: "Heurist", value: "heuristai"},
224
+ ],
225
+ validate: expect.any(Function),
226
+ },
227
+ ]);
228
+ expect(simulatorService.configSimulator).toHaveBeenCalledWith({
229
+ OPENAIKEY: "API_KEY1",
230
+ HEURISTAIAPIKEY: "API_KEY2",
231
+ });
232
+ });
233
+
234
+ test("if configSimulator fails, then the execution aborts", async () => {
235
+ // Given
236
+ inquirerPrompt.mockResolvedValue({confirmReset: true, confirmDownload: true, selectedLlmProviders: []});
237
+ simServConfigSimulator.mockRejectedValue(new Error("Error"));
238
+
239
+ // When
240
+ await initAction(defaultActionOptions, simulatorService);
241
+
242
+ // Then
243
+ expect(error).toHaveBeenCalledTimes(1);
244
+ expect(error).toHaveBeenCalledWith(new Error("Error"));
245
+ });
246
+
247
+ test("if runSimulator fails, then the execution aborts", async () => {
248
+ // Given
249
+ inquirerPrompt.mockResolvedValue({confirmReset: true, confirmDownload: true, selectedLlmProviders: []});
250
+ simServRunSimulator.mockRejectedValue(new Error("Error"));
251
+
252
+ // When
253
+ await initAction(defaultActionOptions, simulatorService);
254
+
255
+ // Then
256
+ expect(error).toHaveBeenCalledTimes(1);
257
+ expect(error).toHaveBeenCalledWith(new Error("Error"));
258
+ });
259
+
260
+ test("should run the simulator after all configurations", async () => {
261
+ // Given
262
+ inquirerPrompt.mockResolvedValue({
263
+ confirmReset: true,
264
+ confirmDownload: true,
265
+ selectedLlmProviders: ["openai", "heuristai"],
266
+ openai: "API_KEY1",
267
+ heuristai: "API_KEY2",
268
+ });
269
+ simServgetAiProvidersOptions.mockReturnValue([
270
+ {name: "OpenAI", value: "openai"},
271
+ {name: "Heurist", value: "heuristai"},
272
+ ]);
273
+ simServConfigSimulator.mockResolvedValue(true);
274
+ simServRunSimulator.mockResolvedValue(true);
275
+ simServWaitForSimulator.mockRejectedValue(new Error("Error")); // This will stop the execution
276
+
277
+ // When
278
+ await initAction(defaultActionOptions, simulatorService);
279
+
280
+ // Then
281
+ expect(simulatorService.runSimulator).toHaveBeenCalled();
282
+ });
283
+
284
+ test("should abort if waiting for the simulator returns ERROR", async () => {
285
+ // Given
286
+ inquirerPrompt.mockResolvedValue({
287
+ confirmReset: true,
288
+ confirmDownload: true,
289
+ selectedLlmProviders: ["openai", "heuristai"],
290
+ openai: "API_KEY1",
291
+ heuristai: "API_KEY2",
292
+ });
293
+ simServgetAiProvidersOptions.mockReturnValue([
294
+ {name: "OpenAI", value: "openai"},
295
+ {name: "Heurist", value: "heuristai"},
296
+ ]);
297
+ simServConfigSimulator.mockResolvedValue(true);
298
+ simServRunSimulator.mockResolvedValue(true);
299
+ simServWaitForSimulator.mockResolvedValue({
300
+ initialized: false,
301
+ errorCode: "ERROR",
302
+ errorMessage: "errorMessage",
303
+ });
304
+
305
+ // When
306
+ await initAction(defaultActionOptions, simulatorService);
307
+
308
+ // Then
309
+ expect(log).toHaveBeenCalledWith("errorMessage");
310
+ expect(error).toHaveBeenCalledWith("Unable to initialize the GenLayer simulator. Please try again.");
311
+ });
312
+
313
+ test("should abort if waiting for the simulator returns TIMEOUT", async () => {
314
+ // Given
315
+ inquirerPrompt.mockResolvedValue({
316
+ confirmReset: true,
317
+ confirmDownload: true,
318
+ selectedLlmProviders: ["openai", "heuristai"],
319
+ openai: "API_KEY1",
320
+ heuristai: "API_KEY2",
321
+ });
322
+ simServgetAiProvidersOptions.mockReturnValue([
323
+ {name: "OpenAI", value: "openai"},
324
+ {name: "Heurist", value: "heuristai"},
325
+ ]);
326
+ simServConfigSimulator.mockResolvedValue(true);
327
+ simServRunSimulator.mockResolvedValue(true);
328
+ simServWaitForSimulator.mockResolvedValue({
329
+ initialized: false,
330
+ errorCode: "TIMEOUT",
331
+ errorMessage: "errorMessage",
332
+ });
333
+
334
+ // When
335
+ await initAction(defaultActionOptions, simulatorService);
336
+
337
+ // Then
338
+ expect(error).toHaveBeenCalledWith(
339
+ "The simulator is taking too long to initialize. Please try again after the simulator is ready.",
340
+ );
341
+ });
342
+
343
+ test("should abort if waiting for the simulator throws an error", async () => {
344
+ // Given
345
+ inquirerPrompt.mockResolvedValue({
346
+ confirmReset: true,
347
+ confirmDownload: true,
348
+ selectedLlmProviders: ["openai", "heuristai"],
349
+ openai: "API_KEY1",
350
+ heuristai: "API_KEY2",
351
+ });
352
+ simServgetAiProvidersOptions.mockReturnValue([
353
+ {name: "OpenAI", value: "openai"},
354
+ {name: "Heurist", value: "heuristai"},
355
+ ]);
356
+ simServConfigSimulator.mockResolvedValue(true);
357
+ simServRunSimulator.mockResolvedValue(true);
358
+ simServWaitForSimulator.mockRejectedValue(new Error("Error"));
359
+
360
+ // When
361
+ await initAction(defaultActionOptions, simulatorService);
362
+
363
+ // Then
364
+ expect(error).toHaveBeenCalledWith(new Error("Error"));
365
+ });
366
+
367
+ test("should pull llama3 from Ollama if ollama is in providers", async () => {
368
+ // Given
369
+ inquirerPrompt.mockResolvedValue({
370
+ confirmReset: true,
371
+ confirmDownload: true,
372
+ selectedLlmProviders: ["openai", "heuristai", "ollama"],
373
+ openai: "API_KEY1",
374
+ heuristai: "API_KEY2",
375
+ ollama: "API_KEY2",
376
+ });
377
+ simServgetAiProvidersOptions.mockReturnValue([
378
+ {name: "OpenAI", value: "openai"},
379
+ {name: "Heurist", value: "heuristai"},
380
+ {name: "Ollama", value: "ollama"},
381
+ ]);
382
+ simServConfigSimulator.mockResolvedValue(true);
383
+ simServRunSimulator.mockResolvedValue(true);
384
+ simServWaitForSimulator.mockResolvedValue({
385
+ initialized: true,
386
+ });
387
+ simServPullOllamaModel.mockResolvedValue(true);
388
+ simServClearAccAndTxsDb.mockRejectedValue(new Error("Error")); // This will stop the execution
389
+
390
+ // When
391
+ await initAction(defaultActionOptions, simulatorService);
392
+
393
+ // Then
394
+ expect(simServPullOllamaModel).toHaveBeenCalled();
395
+ });
396
+
397
+ test("shouldn't pull llama3 from Ollama if ollama is not in providers", async () => {
398
+ // Given
399
+ inquirerPrompt.mockResolvedValue({
400
+ confirmReset: true,
401
+ confirmDownload: true,
402
+ selectedLlmProviders: ["openai", "heuristai"],
403
+ openai: "API_KEY1",
404
+ heuristai: "API_KEY2",
405
+ });
406
+ simServgetAiProvidersOptions.mockReturnValue([
407
+ {name: "OpenAI", value: "openai"},
408
+ {name: "Heurist", value: "heuristai"},
409
+ ]);
410
+ simServConfigSimulator.mockResolvedValue(true);
411
+ simServRunSimulator.mockResolvedValue(true);
412
+ simServWaitForSimulator.mockResolvedValue({
413
+ initialized: true,
414
+ });
415
+ simServPullOllamaModel.mockResolvedValue(true);
416
+ simServClearAccAndTxsDb.mockRejectedValue(new Error("Error")); // This will stop the execution
417
+
418
+ // When
419
+ await initAction(defaultActionOptions, simulatorService);
420
+
421
+ // Then
422
+ expect(simServPullOllamaModel).not.toHaveBeenCalled();
423
+ });
424
+
425
+ test("should abort if clear accounts and transactions throws an error", async () => {
426
+ // Given
427
+ inquirerPrompt.mockResolvedValue({
428
+ confirmReset: true,
429
+ confirmDownload: true,
430
+ selectedLlmProviders: ["openai", "heuristai"],
431
+ openai: "API_KEY1",
432
+ heuristai: "API_KEY2",
433
+ });
434
+ simServgetAiProvidersOptions.mockReturnValue([
435
+ {name: "OpenAI", value: "openai"},
436
+ {name: "Heurist", value: "heuristai"},
437
+ ]);
438
+ simServConfigSimulator.mockResolvedValue(true);
439
+ simServRunSimulator.mockResolvedValue(true);
440
+ simServWaitForSimulator.mockResolvedValue({
441
+ initialized: true,
442
+ });
443
+ simServPullOllamaModel.mockResolvedValue(true);
444
+ simServClearAccAndTxsDb.mockRejectedValue(new Error("Error"));
445
+
446
+ // When
447
+ await initAction(defaultActionOptions, simulatorService);
448
+
449
+ // Then
450
+ expect(error).toHaveBeenCalledWith(new Error("Error"));
451
+ });
452
+
453
+ test("should abort if initialize database throws an error", async () => {
454
+ // Given
455
+ inquirerPrompt.mockResolvedValue({
456
+ confirmReset: true,
457
+ confirmDownload: true,
458
+ selectedLlmProviders: ["openai", "heuristai"],
459
+ openai: "API_KEY1",
460
+ heuristai: "API_KEY2",
461
+ });
462
+ simServgetAiProvidersOptions.mockReturnValue([
463
+ {name: "OpenAI", value: "openai"},
464
+ {name: "Heurist", value: "heuristai"},
465
+ ]);
466
+ simServConfigSimulator.mockResolvedValue(true);
467
+ simServRunSimulator.mockResolvedValue(true);
468
+ simServWaitForSimulator.mockResolvedValue({
469
+ initialized: true,
470
+ });
471
+ simServPullOllamaModel.mockResolvedValue(true);
472
+ simServClearAccAndTxsDb.mockResolvedValue(true);
473
+ simServInitializeDatabase.mockRejectedValue(new Error("Error"));
474
+
475
+ // When
476
+ await initAction(defaultActionOptions, simulatorService);
477
+
478
+ // Then
479
+ expect(error).toHaveBeenCalledWith(new Error("Error"));
480
+ });
481
+
482
+ test("should abort if database create db is not successful", async () => {
483
+ // Given
484
+ inquirerPrompt.mockResolvedValue({
485
+ confirmReset: true,
486
+ confirmDownload: true,
487
+ selectedLlmProviders: ["openai", "heuristai"],
488
+ openai: "API_KEY1",
489
+ heuristai: "API_KEY2",
490
+ });
491
+ simServgetAiProvidersOptions.mockReturnValue([
492
+ {name: "OpenAI", value: "openai"},
493
+ {name: "Heurist", value: "heuristai"},
494
+ ]);
495
+ simServConfigSimulator.mockResolvedValue(true);
496
+ simServRunSimulator.mockResolvedValue(true);
497
+ simServWaitForSimulator.mockResolvedValue({
498
+ initialized: true,
499
+ });
500
+ simServPullOllamaModel.mockResolvedValue(true);
501
+ simServClearAccAndTxsDb.mockResolvedValue(true);
502
+ simServInitializeDatabase.mockResolvedValue({createResponse: false, tablesResponse: true});
503
+
504
+ // When
505
+ await initAction(defaultActionOptions, simulatorService);
506
+
507
+ // Then
508
+ expect(error).toHaveBeenCalledWith("Unable to initialize the database. Please try again.");
509
+ });
510
+
511
+ test("should abort if database table creation is not successful", async () => {
512
+ // Given
513
+ inquirerPrompt.mockResolvedValue({
514
+ confirmReset: true,
515
+ confirmDownload: true,
516
+ selectedLlmProviders: ["openai", "heuristai"],
517
+ openai: "API_KEY1",
518
+ heuristai: "API_KEY2",
519
+ });
520
+ simServgetAiProvidersOptions.mockReturnValue([
521
+ {name: "OpenAI", value: "openai"},
522
+ {name: "Heurist", value: "heuristai"},
523
+ ]);
524
+ simServConfigSimulator.mockResolvedValue(true);
525
+ simServRunSimulator.mockResolvedValue(true);
526
+ simServWaitForSimulator.mockResolvedValue({
527
+ initialized: true,
528
+ });
529
+ simServPullOllamaModel.mockResolvedValue(true);
530
+ simServClearAccAndTxsDb.mockResolvedValue(true);
531
+ simServInitializeDatabase.mockResolvedValue({createResponse: true, tablesResponse: false});
532
+
533
+ // When
534
+ await initAction(defaultActionOptions, simulatorService);
535
+
536
+ // Then
537
+ expect(error).toHaveBeenCalledWith("Unable to initialize the database. Please try again.");
538
+ });
539
+
540
+ test("should abort if deleteAllValidators throws an error", async () => {
541
+ // Given
542
+ inquirerPrompt.mockResolvedValue({
543
+ confirmReset: true,
544
+ confirmDownload: true,
545
+ selectedLlmProviders: ["openai", "heuristai"],
546
+ openai: "API_KEY1",
547
+ heuristai: "API_KEY2",
548
+ });
549
+ simServgetAiProvidersOptions.mockReturnValue([
550
+ {name: "OpenAI", value: "openai"},
551
+ {name: "Heurist", value: "heuristai"},
552
+ ]);
553
+ simServConfigSimulator.mockResolvedValue(true);
554
+ simServRunSimulator.mockResolvedValue(true);
555
+ simServWaitForSimulator.mockResolvedValue({
556
+ initialized: true,
557
+ });
558
+ simServPullOllamaModel.mockResolvedValue(true);
559
+ simServClearAccAndTxsDb.mockResolvedValue(true);
560
+ simServInitializeDatabase.mockResolvedValue({createResponse: true, tablesResponse: true});
561
+ simServDeleteAllValidators.mockRejectedValue(new Error("Error"));
562
+
563
+ // When
564
+ await initAction(defaultActionOptions, simulatorService);
565
+
566
+ // Then
567
+ expect(error).toHaveBeenCalledWith("Unable to initialize the validators.");
568
+ });
569
+
570
+ test("should abort if createRandomValidators throws an error", async () => {
571
+ // Given
572
+ inquirerPrompt.mockResolvedValue({
573
+ confirmReset: true,
574
+ confirmDownload: true,
575
+ selectedLlmProviders: ["openai", "heuristai"],
576
+ openai: "API_KEY1",
577
+ heuristai: "API_KEY2",
578
+ });
579
+ simServgetAiProvidersOptions.mockReturnValue([
580
+ {name: "OpenAI", value: "openai"},
581
+ {name: "Heurist", value: "heuristai"},
582
+ ]);
583
+ simServConfigSimulator.mockResolvedValue(true);
584
+ simServRunSimulator.mockResolvedValue(true);
585
+ simServWaitForSimulator.mockResolvedValue({
586
+ initialized: true,
587
+ });
588
+ simServPullOllamaModel.mockResolvedValue(true);
589
+ simServClearAccAndTxsDb.mockResolvedValue(true);
590
+ simServInitializeDatabase.mockResolvedValue({createResponse: true, tablesResponse: true});
591
+ simServDeleteAllValidators.mockResolvedValue(true);
592
+ simServCreateRandomValidators.mockRejectedValue(new Error("Error"));
593
+
594
+ // When
595
+ await initAction(defaultActionOptions, simulatorService);
596
+
597
+ // Then
598
+ expect(error).toHaveBeenCalledWith("Unable to initialize the validators.");
599
+ });
600
+
601
+ test("should open the frontend if everything went well", async () => {
602
+ // Given
603
+ inquirerPrompt.mockResolvedValue({
604
+ confirmReset: true,
605
+ confirmDownload: true,
606
+ selectedLlmProviders: ["openai", "heuristai"],
607
+ openai: "API_KEY1",
608
+ heuristai: "API_KEY2",
609
+ });
610
+ simServgetAiProvidersOptions.mockReturnValue([
611
+ {name: "OpenAI", value: "openai"},
612
+ {name: "Heurist", value: "heuristai"},
613
+ ]);
614
+ simServConfigSimulator.mockResolvedValue(true);
615
+ simServRunSimulator.mockResolvedValue(true);
616
+ simServWaitForSimulator.mockResolvedValue({
617
+ initialized: true,
618
+ });
619
+ simServPullOllamaModel.mockResolvedValue(true);
620
+ simServClearAccAndTxsDb.mockResolvedValue(true);
621
+ simServInitializeDatabase.mockResolvedValue({createResponse: true, tablesResponse: true});
622
+ simServDeleteAllValidators.mockResolvedValue(true);
623
+ simServCreateRandomValidators.mockResolvedValue(true);
624
+ simServOpenFrontend.mockResolvedValue(true);
625
+ simServRedEnvConfigVariable.mockReturnValue("8080");
626
+
627
+ // When
628
+ await initAction(defaultActionOptions, simulatorService);
629
+
630
+ // Then
631
+ const frontendUrl = simulatorService.getFrontendUrl();
632
+ expect(log).toHaveBeenCalledWith(
633
+ `GenLayer simulator initialized successfully! Go to ${frontendUrl} in your browser to access it.`,
634
+ );
635
+ });
636
+ });