genlayer 0.0.34 → 0.1.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.
@@ -1,193 +1,154 @@
1
- /* eslint-disable import/no-named-as-default-member */
2
- import {jest} from "@jest/globals";
1
+ import { vi, describe, beforeEach, afterEach, test, expect } from "vitest";
3
2
  import inquirer from "inquirer";
4
-
5
3
  import simulatorService from "../../src/lib/services/simulator";
6
- import {initAction} from "../../src/commands/general/init";
4
+ import { initAction } from "../../src/commands/general/init";
5
+ import { tmpdir } from "os";
6
+ import {mkdtempSync} from "fs";
7
+ import {join} from "path";
7
8
 
8
- // Default options for the action
9
- const defaultActionOptions = {numValidators: 5, branch: "main"};
9
+ const tempDir = mkdtempSync(join(tmpdir(), "test-initAction-"));
10
+ const defaultActionOptions = { numValidators: 5, branch: "main", location: tempDir };
10
11
 
11
12
  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 simServDeleteAllValidators: jest.Mock<any>;
27
- let simServCreateRandomValidators: jest.Mock<any>;
28
- let simServOpenFrontend: jest.Mock<any>;
29
- let simServRedEnvConfigVariable: jest.Mock<any>;
13
+ let error: ReturnType<any>;
14
+ let log: ReturnType<any>;
15
+ let inquirerPrompt: ReturnType<any>;
16
+
17
+ let simServCheckInstallRequirements: ReturnType<any>;
18
+ let simServCheckVersionRequirements: ReturnType<any>;
19
+ let simServResetDockerContainers: ReturnType<any>;
20
+ let simServResetDockerImages: ReturnType<any>;
21
+ let simServDownloadSimulator: ReturnType<any>;
22
+ let simServgetAiProvidersOptions: ReturnType<any>;
23
+ let simServConfigSimulator: ReturnType<any>;
24
+ let simServRunSimulator: ReturnType<any>;
25
+ let simServWaitForSimulator: ReturnType<any>;
26
+ let simServPullOllamaModel: ReturnType<any>;
27
+ let simServDeleteAllValidators: ReturnType<any>;
28
+ let simServCreateRandomValidators: ReturnType<any>;
29
+ let simServOpenFrontend: ReturnType<any>;
30
+ let simGetSimulatorUrl: ReturnType<any>;
30
31
 
31
32
  beforeEach(() => {
32
- jest.clearAllMocks();
33
-
34
- error = jest.spyOn(console, "error").mockImplementation(() => {}) as jest.Mock<any>;
35
- log = jest.spyOn(console, "log").mockImplementation(() => {}) as jest.Mock<any>;
36
- inquirerPrompt = jest.spyOn(inquirer, "prompt") as jest.Mock<any>;
37
-
38
- simServCheckRequirements = jest.spyOn(simulatorService, "checkRequirements") as jest.Mock<any>;
39
- simServResetDockerContainers = jest.spyOn(simulatorService, "resetDockerContainers") as jest.Mock<any>;
40
- simServResetDockerImages = jest.spyOn(simulatorService, "resetDockerImages") as jest.Mock<any>;
41
- simServDownloadSimulator = jest.spyOn(simulatorService, "downloadSimulator") as jest.Mock<any>;
42
- simServUpdateSimulator = jest.spyOn(simulatorService, "updateSimulator") as jest.Mock<any>;
43
- simServConfigSimulator = jest.spyOn(simulatorService, "configSimulator") as jest.Mock<any>;
44
- simServgetAiProvidersOptions = jest.spyOn(simulatorService, "getAiProvidersOptions") as jest.Mock<any>;
45
- simServRunSimulator = jest.spyOn(simulatorService, "runSimulator") as jest.Mock<any>;
46
- simServWaitForSimulator = jest.spyOn(simulatorService, "waitForSimulatorToBeReady") as jest.Mock<any>;
47
- simServPullOllamaModel = jest.spyOn(simulatorService, "pullOllamaModel") as jest.Mock<any>;
48
- simServDeleteAllValidators = jest.spyOn(simulatorService, "deleteAllValidators") as jest.Mock<any>;
49
- simServCreateRandomValidators = jest.spyOn(simulatorService, "createRandomValidators") as jest.Mock<any>;
50
- simServOpenFrontend = jest.spyOn(simulatorService, "openFrontend") as jest.Mock<any>;
51
- simServRedEnvConfigVariable = jest.spyOn(simulatorService, "readEnvConfigValue") as jest.Mock<any>;
33
+ vi.clearAllMocks();
34
+
35
+ error = vi.spyOn(console, "error").mockImplementation(() => {});
36
+ log = vi.spyOn(console, "log").mockImplementation(() => {});
37
+ inquirerPrompt = vi.spyOn(inquirer, "prompt");
38
+
39
+ simServCheckInstallRequirements = vi.spyOn(simulatorService, "checkInstallRequirements");
40
+ simServCheckVersionRequirements = vi.spyOn(simulatorService, "checkVersionRequirements");
41
+ simServResetDockerContainers = vi.spyOn(simulatorService, "resetDockerContainers");
42
+ simServResetDockerImages = vi.spyOn(simulatorService, "resetDockerImages");
43
+ simServDownloadSimulator = vi.spyOn(simulatorService, "downloadSimulator");
44
+ simServConfigSimulator = vi.spyOn(simulatorService, "configSimulator");
45
+ simServgetAiProvidersOptions = vi.spyOn(simulatorService, "getAiProvidersOptions");
46
+ simServRunSimulator = vi.spyOn(simulatorService, "runSimulator");
47
+ simServWaitForSimulator = vi.spyOn(simulatorService, "waitForSimulatorToBeReady");
48
+ simServPullOllamaModel = vi.spyOn(simulatorService, "pullOllamaModel");
49
+ simServDeleteAllValidators = vi.spyOn(simulatorService, "deleteAllValidators");
50
+ simServCreateRandomValidators = vi.spyOn(simulatorService, "createRandomValidators");
51
+ simServOpenFrontend = vi.spyOn(simulatorService, "openFrontend");
52
+ simGetSimulatorUrl = vi.spyOn(simulatorService, "getFrontendUrl")
52
53
  });
53
54
 
54
55
  afterEach(() => {
55
- jest.restoreAllMocks();
56
+ vi.restoreAllMocks();
56
57
  });
57
58
 
58
59
  test("if both requirements are missing, then the execution fails", async () => {
59
- // Given
60
- simServCheckRequirements.mockResolvedValue({git: false, docker: false});
60
+ simServCheckInstallRequirements.mockResolvedValue({ git: false, docker: false });
61
61
 
62
- // When
63
62
  await initAction(defaultActionOptions, simulatorService);
64
63
 
65
- // Then
66
- expect(error).toHaveBeenCalledTimes(1);
67
64
  expect(error).toHaveBeenCalledWith(
68
- "Git and Docker are not installed. Please install them and try again.\n",
65
+ "Git and Docker are not installed. Please install them and try again.\n"
69
66
  );
70
67
  });
71
68
 
72
69
  test("if only docker is missing, then the execution fails", async () => {
73
- // Given
74
- simServCheckRequirements.mockResolvedValue({git: true, docker: false});
70
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: false });
75
71
 
76
- // When
77
72
  await initAction(defaultActionOptions, simulatorService);
78
73
 
79
- // Then
80
- expect(error).toHaveBeenCalledTimes(1);
81
74
  expect(error).toHaveBeenCalledWith("Docker is not installed. Please install Docker and try again.\n");
82
75
  });
83
76
 
84
77
  test("if only git is missing, then the execution fails", async () => {
85
- // Given
86
- simServCheckRequirements.mockResolvedValue({git: false, docker: true});
78
+ simServCheckInstallRequirements.mockResolvedValue({ git: false, docker: true });
87
79
 
88
- // When
89
80
  await initAction(defaultActionOptions, simulatorService);
90
81
 
91
- // Then
92
- expect(error).toHaveBeenCalledTimes(1);
93
82
  expect(error).toHaveBeenCalledWith("Git is not installed. Please install Git and try again.\n");
94
83
  });
95
84
 
96
- test("if check requirements fail, then the execution aborts", async () => {
97
- // Given
98
- simServCheckRequirements.mockRejectedValue(new Error("Error"));
85
+ test("if check install requirements fail, then the execution aborts", async () => {
86
+ simServCheckInstallRequirements.mockRejectedValue(new Error("Error"));
99
87
 
100
- // When
101
88
  await initAction(defaultActionOptions, simulatorService);
102
89
 
103
- // Then
104
- expect(error).toHaveBeenCalledTimes(1);
105
90
  expect(error).toHaveBeenCalledWith(new Error("Error"));
106
91
  });
107
92
 
108
- test("if reset is not confirmed, abort", async () => {
109
- // Given
110
- inquirerPrompt.mockResolvedValue({confirmReset: false});
111
- simServCheckRequirements.mockResolvedValue({git: true, docker: true});
112
-
113
- // When
114
- await initAction(defaultActionOptions, simulatorService);
115
-
116
- // Then
117
- expect(log).toHaveBeenCalledTimes(1);
118
- expect(log).toHaveBeenNthCalledWith(1, "Aborted!");
119
- });
120
-
121
- test("if resetDockerContainers fail, then the execution aborts", async () => {
122
- // Given
123
- inquirerPrompt.mockResolvedValue({confirmReset: true});
124
- simServResetDockerContainers.mockRejectedValue(new Error("Error"));
93
+ test("if both versions are too low, then the execution fails", async () => {
94
+ const mockVersionNumber = "99.9.9";
95
+ simServCheckVersionRequirements.mockResolvedValue({
96
+ node: mockVersionNumber,
97
+ docker: mockVersionNumber,
98
+ });
125
99
 
126
- // When
127
100
  await initAction(defaultActionOptions, simulatorService);
128
101
 
129
- // Then
130
- expect(error).toHaveBeenCalledTimes(1);
131
- expect(error).toHaveBeenCalledWith(new Error("Error"));
102
+ expect(error).toHaveBeenCalledWith(
103
+ `Docker version ${mockVersionNumber} or higher is required. Please update Docker and try again.\nNode version ${mockVersionNumber} or higher is required. Please update Node and try again.\n`
104
+ );
132
105
  });
133
106
 
134
- test("if resetDockerImages fail, then the execution aborts", async () => {
135
- // Given
136
- inquirerPrompt.mockResolvedValue({confirmReset: true});
137
- simServResetDockerImages.mockRejectedValue(new Error("Error"));
107
+ test("if only docker version is too low, then the execution fails", async () => {
108
+ const mockVersionNumber = "99.9.9";
109
+ simServCheckVersionRequirements.mockResolvedValue({
110
+ docker: mockVersionNumber,
111
+ });
138
112
 
139
- // When
140
113
  await initAction(defaultActionOptions, simulatorService);
141
114
 
142
- // Then
143
- expect(error).toHaveBeenCalledTimes(1);
144
- expect(error).toHaveBeenCalledWith(new Error("Error"));
115
+ expect(error).toHaveBeenCalledWith(
116
+ `Docker version ${mockVersionNumber} or higher is required. Please update Docker and try again.\n`
117
+ );
145
118
  });
146
119
 
147
- test("if download is not confirmed, abort", async () => {
148
- // Given
149
- inquirerPrompt.mockResolvedValue({confirmReset: true, confirmDownload: false});
150
- simServCheckRequirements.mockResolvedValue({git: true, docker: true});
120
+ test("if only node version is too low, then the execution fails", async () => {
121
+ const mockVersionNumber = "99.9.9";
122
+ simServCheckVersionRequirements.mockResolvedValue({
123
+ node: mockVersionNumber
124
+ });
151
125
 
152
- // When
153
126
  await initAction(defaultActionOptions, simulatorService);
154
127
 
155
- // Then
156
- expect(log).toHaveBeenCalledTimes(3);
157
- expect(log).toHaveBeenNthCalledWith(3, "Aborted!");
128
+ expect(error).toHaveBeenCalledWith(
129
+ `Node version ${mockVersionNumber} or higher is required. Please update Node and try again.\n`
130
+ );
158
131
  });
159
132
 
160
- test("if not already installed, it should download and install the simulator", async () => {
161
- // Given
162
- inquirerPrompt.mockResolvedValue({confirmReset: true, confirmDownload: true, selectedLlmProviders: []});
163
- simServDownloadSimulator.mockResolvedValue({wasInstalled: false});
164
- simServConfigSimulator.mockRejectedValue(new Error("Error")); // This will stop the execution
133
+ test("if reset is not confirmed, abort", async () => {
134
+ inquirerPrompt.mockResolvedValue({ confirmReset: false });
135
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
165
136
 
166
- // When
167
137
  await initAction(defaultActionOptions, simulatorService);
168
138
 
169
- // Then
170
- expect(simulatorService.downloadSimulator).toHaveBeenCalled();
171
- expect(simulatorService.updateSimulator).not.toHaveBeenCalled();
139
+ expect(log).toHaveBeenCalledWith("Aborted!");
172
140
  });
173
141
 
174
- test("if already installed, it should update the simulator", async () => {
175
- // Given
176
- inquirerPrompt.mockResolvedValue({confirmReset: true, confirmDownload: true, selectedLlmProviders: []});
177
- simServDownloadSimulator.mockResolvedValue({wasInstalled: true});
178
- simServUpdateSimulator.mockResolvedValue(true);
179
- simServConfigSimulator.mockRejectedValue(new Error("Error")); // This will stop the execution
142
+ test("if resetDockerContainers fail, then the execution aborts", async () => {
143
+ inquirerPrompt.mockResolvedValue({ confirmReset: true });
144
+ simServResetDockerContainers.mockRejectedValue(new Error("Error"));
180
145
 
181
- // When
182
146
  await initAction(defaultActionOptions, simulatorService);
183
147
 
184
- // Then
185
- expect(simulatorService.downloadSimulator).toHaveBeenCalled();
186
- expect(simulatorService.updateSimulator).toHaveBeenCalled();
148
+ expect(error).toHaveBeenCalledWith(new Error("Error"));
187
149
  });
188
150
 
189
- test("should prompt for LLM providers and call configSimulator with selected providers", async () => {
190
- // Given
151
+ test("should open the frontend if everything went well", async () => {
191
152
  inquirerPrompt.mockResolvedValue({
192
153
  confirmReset: true,
193
154
  confirmDownload: true,
@@ -196,313 +157,269 @@ describe("init action", () => {
196
157
  heuristai: "API_KEY2",
197
158
  });
198
159
  simServgetAiProvidersOptions.mockReturnValue([
199
- {name: "OpenAI", value: "openai"},
200
- {name: "Heurist", value: "heuristai"},
160
+ { name: "OpenAI", value: "openai" },
161
+ { name: "Heurist", value: "heuristai" },
201
162
  ]);
202
163
  simServConfigSimulator.mockResolvedValue(true);
203
- simServRunSimulator.mockRejectedValue(new Error("Error")); // This will stop the execution
164
+ simServRunSimulator.mockResolvedValue(true);
165
+ simServWaitForSimulator.mockResolvedValue({ initialized: true });
166
+ simServPullOllamaModel.mockResolvedValue(true);
167
+ simServDeleteAllValidators.mockResolvedValue(true);
168
+ simServCreateRandomValidators.mockResolvedValue(true);
169
+ simServOpenFrontend.mockResolvedValue(true);
170
+ simGetSimulatorUrl.mockResolvedValue('http://localhost:8080/');
204
171
 
205
- // When
206
172
  await initAction(defaultActionOptions, simulatorService);
207
173
 
208
- // Then
209
- expect(inquirerPrompt).toHaveBeenNthCalledWith(3, [
210
- {
211
- type: "checkbox",
212
- name: "selectedLlmProviders",
213
- message: "Select which LLM providers do you want to use:",
214
- choices: [
215
- {name: "OpenAI", value: "openai"},
216
- {name: "Heurist", value: "heuristai"},
217
- ],
218
- validate: expect.any(Function),
219
- },
220
- ]);
221
- expect(simulatorService.configSimulator).toHaveBeenCalledWith({
222
- OPENAIKEY: "API_KEY1",
223
- HEURISTAIAPIKEY: "API_KEY2",
224
- });
174
+ const frontendUrl = simulatorService.getFrontendUrl();
175
+ expect(log).toHaveBeenCalledWith(
176
+ `GenLayer simulator initialized successfully! Go to ${frontendUrl} in your browser to access it.`
177
+ );
225
178
  });
226
179
 
227
180
  test("if configSimulator fails, then the execution aborts", async () => {
228
- // Given
229
- inquirerPrompt.mockResolvedValue({confirmReset: true, confirmDownload: true, selectedLlmProviders: []});
181
+ inquirerPrompt.mockResolvedValue({ confirmReset: true, confirmDownload: true, selectedLlmProviders: [] });
230
182
  simServConfigSimulator.mockRejectedValue(new Error("Error"));
231
183
 
232
- // When
233
184
  await initAction(defaultActionOptions, simulatorService);
234
185
 
235
- // Then
236
- expect(error).toHaveBeenCalledTimes(1);
237
186
  expect(error).toHaveBeenCalledWith(new Error("Error"));
238
187
  });
239
188
 
240
189
  test("if runSimulator fails, then the execution aborts", async () => {
241
- // Given
242
- inquirerPrompt.mockResolvedValue({confirmReset: true, confirmDownload: true, selectedLlmProviders: []});
190
+ inquirerPrompt.mockResolvedValue({ confirmReset: true, confirmDownload: true, selectedLlmProviders: [] });
243
191
  simServRunSimulator.mockRejectedValue(new Error("Error"));
244
192
 
245
- // When
246
193
  await initAction(defaultActionOptions, simulatorService);
247
194
 
248
- // Then
249
- expect(error).toHaveBeenCalledTimes(1);
250
195
  expect(error).toHaveBeenCalledWith(new Error("Error"));
251
196
  });
252
197
 
253
- test("should run the simulator after all configurations", async () => {
254
- // Given
198
+ test("should pull Ollama model if 'ollama' is in providers", async () => {
255
199
  inquirerPrompt.mockResolvedValue({
256
200
  confirmReset: true,
257
201
  confirmDownload: true,
258
- selectedLlmProviders: ["openai", "heuristai"],
202
+ selectedLlmProviders: ["openai", "heuristai", "ollama"],
259
203
  openai: "API_KEY1",
260
204
  heuristai: "API_KEY2",
205
+ ollama: "API_KEY3",
261
206
  });
262
207
  simServgetAiProvidersOptions.mockReturnValue([
263
- {name: "OpenAI", value: "openai"},
264
- {name: "Heurist", value: "heuristai"},
208
+ { name: "OpenAI", value: "openai" },
209
+ { name: "Heurist", value: "heuristai" },
210
+ { name: "Ollama", value: "ollama" },
265
211
  ]);
266
212
  simServConfigSimulator.mockResolvedValue(true);
267
213
  simServRunSimulator.mockResolvedValue(true);
268
- simServWaitForSimulator.mockRejectedValue(new Error("Error")); // This will stop the execution
214
+ simServWaitForSimulator.mockResolvedValue({ initialized: true });
215
+ simServPullOllamaModel.mockResolvedValue(true);
216
+ simServDeleteAllValidators.mockResolvedValue(true);
269
217
 
270
- // When
271
218
  await initAction(defaultActionOptions, simulatorService);
272
219
 
273
- // Then
274
- expect(simulatorService.runSimulator).toHaveBeenCalled();
220
+ expect(simServPullOllamaModel).toHaveBeenCalled();
275
221
  });
276
222
 
277
- test("should abort if waiting for the simulator returns ERROR", async () => {
278
- // Given
279
- inquirerPrompt.mockResolvedValue({
280
- confirmReset: true,
281
- confirmDownload: true,
282
- selectedLlmProviders: ["openai", "heuristai"],
283
- openai: "API_KEY1",
284
- heuristai: "API_KEY2",
285
- });
286
- simServgetAiProvidersOptions.mockReturnValue([
287
- {name: "OpenAI", value: "openai"},
288
- {name: "Heurist", value: "heuristai"},
289
- ]);
290
- simServConfigSimulator.mockResolvedValue(true);
291
- simServRunSimulator.mockResolvedValue(true);
292
- simServWaitForSimulator.mockResolvedValue({
293
- initialized: false,
294
- errorCode: "ERROR",
295
- errorMessage: "errorMessage",
296
- });
223
+ test("logs error if checkVersionRequirements throws", async () => {
224
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
225
+ const errorMsg = new Error("checkVersionRequirements error");
226
+ simServCheckVersionRequirements.mockRejectedValueOnce(errorMsg);
297
227
 
298
- // When
299
228
  await initAction(defaultActionOptions, simulatorService);
300
229
 
301
- // Then
302
- expect(log).toHaveBeenCalledWith("errorMessage");
303
- expect(error).toHaveBeenCalledWith("Unable to initialize the GenLayer simulator. Please try again.");
230
+ expect(error).toHaveBeenCalledWith(errorMsg);
304
231
  });
305
232
 
306
- test("should abort if waiting for the simulator returns TIMEOUT", async () => {
307
- // Given
308
- inquirerPrompt.mockResolvedValue({
309
- confirmReset: true,
310
- confirmDownload: true,
311
- selectedLlmProviders: ["openai", "heuristai"],
312
- openai: "API_KEY1",
313
- heuristai: "API_KEY2",
314
- });
315
- simServgetAiProvidersOptions.mockReturnValue([
316
- {name: "OpenAI", value: "openai"},
317
- {name: "Heurist", value: "heuristai"},
318
- ]);
319
- simServConfigSimulator.mockResolvedValue(true);
320
- simServRunSimulator.mockResolvedValue(true);
321
- simServWaitForSimulator.mockResolvedValue({
322
- initialized: false,
323
- errorCode: "TIMEOUT",
324
- errorMessage: "errorMessage",
325
- });
233
+ test("logs 'Aborted!' if confirmDownload is false", async () => {
234
+ inquirerPrompt
235
+ .mockResolvedValueOnce({ confirmReset: true })
236
+ .mockResolvedValueOnce({ confirmDownload: false });
237
+
238
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
239
+ simServResetDockerContainers.mockResolvedValue(true);
240
+ simServResetDockerImages.mockResolvedValue(true);
326
241
 
327
- // When
328
242
  await initAction(defaultActionOptions, simulatorService);
329
243
 
330
- // Then
331
- expect(error).toHaveBeenCalledWith(
332
- "The simulator is taking too long to initialize. Please try again after the simulator is ready.",
333
- );
244
+ expect(log).toHaveBeenCalledWith("Aborted!");
334
245
  });
335
246
 
336
- test("should abort if waiting for the simulator throws an error", async () => {
337
- // Given
338
- inquirerPrompt.mockResolvedValue({
339
- confirmReset: true,
340
- confirmDownload: true,
341
- selectedLlmProviders: ["openai", "heuristai"],
342
- openai: "API_KEY1",
343
- heuristai: "API_KEY2",
344
- });
345
- simServgetAiProvidersOptions.mockReturnValue([
346
- {name: "OpenAI", value: "openai"},
347
- {name: "Heurist", value: "heuristai"},
348
- ]);
247
+ test("logs error if resetDockerContainers throws", async () => {
248
+ inquirerPrompt.mockResolvedValue({ confirmReset: true });
249
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
250
+ const errorMsg = new Error("resetDockerContainers error");
251
+ simServResetDockerContainers.mockRejectedValueOnce(errorMsg);
252
+
253
+ await initAction(defaultActionOptions, simulatorService);
254
+
255
+ expect(error).toHaveBeenCalledWith(errorMsg);
256
+ });
257
+
258
+ test("prompts for LLM providers and validates that at least one is selected", async () => {
259
+ inquirerPrompt
260
+ .mockResolvedValueOnce({ confirmReset: true })
261
+ .mockResolvedValueOnce({ confirmDownload: true })
262
+ .mockImplementation((questions: any) => {
263
+ if (questions[0].type === "checkbox") {
264
+ const validateFunction = questions[0].validate;
265
+ expect(validateFunction([])).toBe("You must choose at least one option.");
266
+ expect(validateFunction(["openai"])).toBe(true);
267
+ return Promise.resolve({ selectedLlmProviders: ["openai"] });
268
+ }
269
+
270
+ if (questions[0].type === "input") {
271
+ const validateFunction = questions[0].validate;
272
+ expect(validateFunction("")).toBe("Please enter a valid API Key for OpenAI.");
273
+ expect(validateFunction("API_KEY1")).toBe(true);
274
+ return Promise.resolve({ openai: "API_KEY1" });
275
+ }
276
+ });
277
+
278
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
279
+ simServResetDockerContainers.mockResolvedValue(true);
280
+ simServResetDockerImages.mockResolvedValue(true);
281
+ simServDownloadSimulator.mockResolvedValue({ wasInstalled: false });
349
282
  simServConfigSimulator.mockResolvedValue(true);
350
283
  simServRunSimulator.mockResolvedValue(true);
351
- simServWaitForSimulator.mockRejectedValue(new Error("Error"));
284
+ simServWaitForSimulator.mockResolvedValue({ initialized: true });
285
+ simServDeleteAllValidators.mockResolvedValue(true);
286
+ simServCreateRandomValidators.mockResolvedValue(true);
287
+ simServOpenFrontend.mockResolvedValue(true);
352
288
 
353
- // When
354
289
  await initAction(defaultActionOptions, simulatorService);
290
+ });
355
291
 
356
- // Then
357
- expect(error).toHaveBeenCalledWith(new Error("Error"));
292
+ test("logs error if downloadSimulator throws", async () => {
293
+ inquirerPrompt.mockResolvedValue({ confirmReset: true, confirmDownload: true });
294
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
295
+ simServResetDockerContainers.mockResolvedValue(true);
296
+ simServResetDockerImages.mockResolvedValue(true);
297
+ const errorMsg = new Error("downloadSimulator error");
298
+ simServDownloadSimulator.mockRejectedValueOnce(errorMsg);
299
+
300
+ await initAction(defaultActionOptions, simulatorService);
301
+
302
+ expect(error).toHaveBeenCalledWith(errorMsg);
358
303
  });
359
304
 
360
- test("should pull llama3 from Ollama if ollama is in providers", async () => {
361
- // Given
362
- inquirerPrompt.mockResolvedValue({
363
- confirmReset: true,
364
- confirmDownload: true,
365
- selectedLlmProviders: ["openai", "heuristai", "ollama"],
366
- openai: "API_KEY1",
367
- heuristai: "API_KEY2",
368
- ollama: "API_KEY2",
369
- });
370
- simServgetAiProvidersOptions.mockReturnValue([
371
- {name: "OpenAI", value: "openai"},
372
- {name: "Heurist", value: "heuristai"},
373
- {name: "Ollama", value: "ollama"},
374
- ]);
305
+ test("logs error message if simulator fails to initialize with ERROR code", async () => {
306
+ inquirerPrompt
307
+ .mockResolvedValueOnce({ confirmReset: true })
308
+ .mockResolvedValueOnce({ confirmDownload: true })
309
+ .mockResolvedValueOnce({ selectedLlmProviders: ["openai"] })
310
+ .mockResolvedValueOnce({ openai: "API_KEY1" });
311
+
312
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
313
+ simServResetDockerContainers.mockResolvedValue(true);
314
+ simServResetDockerImages.mockResolvedValue(true);
315
+ simServDownloadSimulator.mockResolvedValue({ wasInstalled: false });
375
316
  simServConfigSimulator.mockResolvedValue(true);
376
317
  simServRunSimulator.mockResolvedValue(true);
318
+
377
319
  simServWaitForSimulator.mockResolvedValue({
378
- initialized: true,
320
+ initialized: false,
321
+ errorCode: "ERROR",
322
+ errorMessage: "Simulator failed to initialize due to configuration error.",
379
323
  });
380
- simServPullOllamaModel.mockResolvedValue(true);
381
- simServDeleteAllValidators.mockRejectedValue(new Error("Error")); // This will stop the execution
382
324
 
383
- // When
384
325
  await initAction(defaultActionOptions, simulatorService);
385
326
 
386
- // Then
387
- expect(simServPullOllamaModel).toHaveBeenCalled();
327
+ expect(log).toHaveBeenCalledWith("Simulator failed to initialize due to configuration error.");
328
+ expect(error).toHaveBeenCalledWith("Unable to initialize the GenLayer simulator. Please try again.");
388
329
  });
389
330
 
390
- test("shouldn't pull llama3 from Ollama if ollama is not in providers", async () => {
391
- // Given
331
+ test("logs error if runSimulator throws", async () => {
392
332
  inquirerPrompt.mockResolvedValue({
393
333
  confirmReset: true,
394
334
  confirmDownload: true,
395
- selectedLlmProviders: ["openai", "heuristai"],
335
+ selectedLlmProviders: ["openai"],
396
336
  openai: "API_KEY1",
397
- heuristai: "API_KEY2",
398
337
  });
399
- simServgetAiProvidersOptions.mockReturnValue([
400
- {name: "OpenAI", value: "openai"},
401
- {name: "Heurist", value: "heuristai"},
402
- ]);
338
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
339
+ simServResetDockerContainers.mockResolvedValue(true);
340
+ simServResetDockerImages.mockResolvedValue(true);
341
+ simServDownloadSimulator.mockResolvedValue({ wasInstalled: false });
403
342
  simServConfigSimulator.mockResolvedValue(true);
404
- simServRunSimulator.mockResolvedValue(true);
405
- simServWaitForSimulator.mockResolvedValue({
406
- initialized: true,
407
- });
408
- simServPullOllamaModel.mockResolvedValue(true);
409
- simServDeleteAllValidators.mockRejectedValue(new Error("Error")); // This will stop the execution
343
+ const errorMsg = new Error("runSimulator error");
344
+ simServRunSimulator.mockRejectedValueOnce(errorMsg);
410
345
 
411
- // When
412
346
  await initAction(defaultActionOptions, simulatorService);
413
347
 
414
- // Then
415
- expect(simServPullOllamaModel).not.toHaveBeenCalled();
348
+ expect(error).toHaveBeenCalledWith(errorMsg);
416
349
  });
417
350
 
418
- test("should abort if deleteAllValidators throws an error", async () => {
419
- // Given
351
+ test("logs specific message if waitForSimulatorToBeReady returns TIMEOUT errorCode", async () => {
420
352
  inquirerPrompt.mockResolvedValue({
421
353
  confirmReset: true,
422
354
  confirmDownload: true,
423
- selectedLlmProviders: ["openai", "heuristai"],
355
+ selectedLlmProviders: ["openai"],
424
356
  openai: "API_KEY1",
425
- heuristai: "API_KEY2",
426
357
  });
427
- simServgetAiProvidersOptions.mockReturnValue([
428
- {name: "OpenAI", value: "openai"},
429
- {name: "Heurist", value: "heuristai"},
430
- ]);
358
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
359
+ simServResetDockerContainers.mockResolvedValue(true);
360
+ simServResetDockerImages.mockResolvedValue(true);
361
+ simServDownloadSimulator.mockResolvedValue({ wasInstalled: false });
431
362
  simServConfigSimulator.mockResolvedValue(true);
432
363
  simServRunSimulator.mockResolvedValue(true);
433
364
  simServWaitForSimulator.mockResolvedValue({
434
- initialized: true,
365
+ initialized: false,
366
+ errorCode: "TIMEOUT",
367
+ errorMessage: "errorMessage",
435
368
  });
436
- simServPullOllamaModel.mockResolvedValue(true);
437
- simServDeleteAllValidators.mockRejectedValue(new Error("Error"));
438
369
 
439
- // When
440
370
  await initAction(defaultActionOptions, simulatorService);
441
371
 
442
- // Then
443
- expect(error).toHaveBeenCalledWith("Unable to initialize the validators.");
372
+ expect(error).toHaveBeenCalledWith(
373
+ "The simulator is taking too long to initialize. Please try again after the simulator is ready."
374
+ );
444
375
  });
445
376
 
446
- test("should abort if createRandomValidators throws an error", async () => {
447
- // Given
448
- inquirerPrompt.mockResolvedValue({
449
- confirmReset: true,
450
- confirmDownload: true,
451
- selectedLlmProviders: ["openai", "heuristai"],
452
- openai: "API_KEY1",
453
- heuristai: "API_KEY2",
454
- });
455
- simServgetAiProvidersOptions.mockReturnValue([
456
- {name: "OpenAI", value: "openai"},
457
- {name: "Heurist", value: "heuristai"},
458
- ]);
377
+ test("catches and logs error if waitForSimulatorToBeReady throws an exception", async () => {
378
+ inquirerPrompt
379
+ .mockResolvedValueOnce({ confirmReset: true })
380
+ .mockResolvedValueOnce({ confirmDownload: true })
381
+ .mockResolvedValueOnce({ selectedLlmProviders: ["openai"] })
382
+ .mockResolvedValueOnce({ openai: "API_KEY1" });
383
+
384
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
385
+ simServResetDockerContainers.mockResolvedValue(true);
386
+ simServResetDockerImages.mockResolvedValue(true);
387
+ simServDownloadSimulator.mockResolvedValue({ wasInstalled: false });
459
388
  simServConfigSimulator.mockResolvedValue(true);
460
389
  simServRunSimulator.mockResolvedValue(true);
461
- simServWaitForSimulator.mockResolvedValue({
462
- initialized: true,
463
- });
464
- simServPullOllamaModel.mockResolvedValue(true);
465
- simServDeleteAllValidators.mockResolvedValue(true);
466
- simServCreateRandomValidators.mockRejectedValue(new Error("Error"));
467
390
 
468
- // When
391
+ const errorMsg = new Error("Unexpected simulator error");
392
+ simServWaitForSimulator.mockRejectedValueOnce(errorMsg);
393
+
469
394
  await initAction(defaultActionOptions, simulatorService);
470
395
 
471
- // Then
472
- expect(error).toHaveBeenCalledWith("Unable to initialize the validators.");
396
+ expect(error).toHaveBeenCalledWith(errorMsg);
473
397
  });
474
398
 
475
- test("should open the frontend if everything went well", async () => {
476
- // Given
477
- inquirerPrompt.mockResolvedValue({
478
- confirmReset: true,
479
- confirmDownload: true,
480
- selectedLlmProviders: ["openai", "heuristai"],
481
- openai: "API_KEY1",
482
- heuristai: "API_KEY2",
483
- });
484
- simServgetAiProvidersOptions.mockReturnValue([
485
- {name: "OpenAI", value: "openai"},
486
- {name: "Heurist", value: "heuristai"},
487
- ]);
399
+ test("catches and logs error if openFrontend throws an exception", async () => {
400
+ inquirerPrompt
401
+ .mockResolvedValueOnce({ confirmReset: true })
402
+ .mockResolvedValueOnce({ confirmDownload: true })
403
+ .mockResolvedValueOnce({ selectedLlmProviders: ["openai"] })
404
+ .mockResolvedValueOnce({ openai: "API_KEY1" });
405
+
406
+ simServCheckInstallRequirements.mockResolvedValue({ git: true, docker: true });
407
+ simServResetDockerContainers.mockResolvedValue(true);
408
+ simServResetDockerImages.mockResolvedValue(true);
409
+ simServDownloadSimulator.mockResolvedValue({ wasInstalled: false });
488
410
  simServConfigSimulator.mockResolvedValue(true);
489
411
  simServRunSimulator.mockResolvedValue(true);
490
- simServWaitForSimulator.mockResolvedValue({
491
- initialized: true,
492
- });
493
- simServPullOllamaModel.mockResolvedValue(true);
412
+ simServWaitForSimulator.mockResolvedValue({ initialized: true });
494
413
  simServDeleteAllValidators.mockResolvedValue(true);
495
414
  simServCreateRandomValidators.mockResolvedValue(true);
496
- simServOpenFrontend.mockResolvedValue(true);
497
- simServRedEnvConfigVariable.mockReturnValue("8080");
498
415
 
499
- // When
416
+ const errorMsg = new Error("Failed to open frontend");
417
+ simServOpenFrontend.mockImplementationOnce(() => {
418
+ throw errorMsg;
419
+ });
420
+
500
421
  await initAction(defaultActionOptions, simulatorService);
501
422
 
502
- // Then
503
- const frontendUrl = simulatorService.getFrontendUrl();
504
- expect(log).toHaveBeenCalledWith(
505
- `GenLayer simulator initialized successfully! Go to ${frontendUrl} in your browser to access it.`,
506
- );
423
+ expect(error).toHaveBeenCalledWith(errorMsg);
507
424
  });
508
425
  });