genlayer 0.12.1 → 0.12.2-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.
Files changed (44) hide show
  1. package/.github/workflows/publish-beta.yml +2 -2
  2. package/.github/workflows/publish.yml +2 -2
  3. package/.github/workflows/validate-code.yml +1 -1
  4. package/CHANGELOG.md +6 -0
  5. package/dist/index.js +24736 -108331
  6. package/docker-compose.yml +1 -1
  7. package/esbuild.config.dev.js +18 -0
  8. package/esbuild.config.js +10 -5
  9. package/esbuild.config.prod.js +16 -0
  10. package/eslint.config.js +59 -0
  11. package/package.json +12 -9
  12. package/scripts/postinstall.js +10 -6
  13. package/src/commands/config/getSetReset.ts +25 -18
  14. package/src/commands/contracts/deploy.ts +105 -25
  15. package/src/commands/contracts/index.ts +5 -1
  16. package/src/commands/general/index.ts +10 -4
  17. package/src/commands/general/init.ts +136 -189
  18. package/src/commands/general/start.ts +76 -77
  19. package/src/commands/general/stop.ts +6 -5
  20. package/src/commands/keygen/create.ts +9 -11
  21. package/src/commands/update/index.ts +3 -8
  22. package/src/commands/update/ollama.ts +56 -56
  23. package/src/commands/validators/validators.ts +48 -55
  24. package/src/lib/actions/BaseAction.ts +75 -4
  25. package/src/lib/config/simulator.ts +1 -1
  26. package/src/lib/services/simulator.ts +3 -2
  27. package/tests/actions/create.test.ts +18 -30
  28. package/tests/actions/deploy.test.ts +200 -30
  29. package/tests/actions/getSetReset.test.ts +29 -42
  30. package/tests/actions/init.test.ts +240 -475
  31. package/tests/actions/ollama.test.ts +40 -55
  32. package/tests/actions/start.test.ts +107 -108
  33. package/tests/actions/stop.test.ts +23 -4
  34. package/tests/actions/validators.test.ts +273 -142
  35. package/tests/commands/call.test.ts +4 -1
  36. package/tests/commands/deploy.test.ts +11 -0
  37. package/tests/commands/init.test.ts +11 -12
  38. package/tests/commands/up.test.ts +31 -23
  39. package/tests/commands/update.test.ts +2 -5
  40. package/tests/libs/baseAction.test.ts +175 -0
  41. package/tests/services/simulator.test.ts +15 -0
  42. package/.eslintrc.js +0 -58
  43. package/esbuild.config.dev +0 -16
  44. package/esbuild.config.prod +0 -16
@@ -3,16 +3,22 @@ import fs from "fs";
3
3
  import { createClient, createAccount } from "genlayer-js";
4
4
  import { DeployAction, DeployOptions } from "../../src/commands/contracts/deploy";
5
5
  import { getPrivateKey } from "../../src/lib/accounts/getPrivateKey";
6
+ import { buildSync } from "esbuild";
7
+ import { pathToFileURL } from "url";
6
8
 
7
9
  vi.mock("fs");
8
10
  vi.mock("genlayer-js");
11
+ vi.mock("esbuild", () => ({
12
+ buildSync: vi.fn(),
13
+ }));
9
14
  vi.mock("../../src/lib/accounts/getPrivateKey");
10
15
 
11
- describe("Deploy Action", () => {
16
+ describe("DeployAction", () => {
12
17
  let deployer: DeployAction;
13
18
  const mockClient = {
14
19
  deployContract: vi.fn(),
15
- waitForTransactionReceipt: vi.fn()
20
+ waitForTransactionReceipt: vi.fn(),
21
+ initializeConsensusSmartContract: vi.fn(),
16
22
  };
17
23
 
18
24
  const mockPrivateKey = "mocked_private_key";
@@ -23,6 +29,12 @@ describe("Deploy Action", () => {
23
29
  vi.mocked(createAccount).mockReturnValue({ privateKey: mockPrivateKey } as any);
24
30
  vi.mocked(getPrivateKey).mockReturnValue(mockPrivateKey);
25
31
  deployer = new DeployAction();
32
+
33
+ vi.spyOn(deployer as any, "startSpinner").mockImplementation(() => {});
34
+ vi.spyOn(deployer as any, "succeedSpinner").mockImplementation(() => {});
35
+ vi.spyOn(deployer as any, "failSpinner").mockImplementation(() => {});
36
+ vi.spyOn(deployer as any, "setSpinnerText").mockImplementation(() => {});
37
+ vi.spyOn(deployer as any, "log").mockImplementation(() => {});
26
38
  });
27
39
 
28
40
  afterEach(() => {
@@ -52,7 +64,6 @@ describe("Deploy Action", () => {
52
64
  expect(fs.existsSync).toHaveBeenCalledWith(contractPath);
53
65
  });
54
66
 
55
-
56
67
  test("deploys contract with args", async () => {
57
68
  const options: DeployOptions = {
58
69
  contract: "/mocked/contract/path",
@@ -63,7 +74,9 @@ describe("Deploy Action", () => {
63
74
  vi.mocked(fs.existsSync).mockReturnValue(true);
64
75
  vi.mocked(fs.readFileSync).mockReturnValue(contractContent);
65
76
  vi.mocked(mockClient.deployContract).mockResolvedValue("mocked_tx_hash");
66
- vi.mocked(mockClient.waitForTransactionReceipt).mockResolvedValue({data: {contractAddress: '0xdasdsadasdasdada'}});
77
+ vi.mocked(mockClient.waitForTransactionReceipt).mockResolvedValue({
78
+ data: { contract_address: "0xdasdsadasdasdada" },
79
+ });
67
80
 
68
81
  await deployer.deploy(options);
69
82
 
@@ -73,31 +86,15 @@ describe("Deploy Action", () => {
73
86
  args: [1, 2, 3],
74
87
  leaderOnly: false,
75
88
  });
76
- expect(mockClient.deployContract).toHaveResolvedWith("mocked_tx_hash");
77
- });
78
-
79
- test("throws error for both args and kwargs", async () => {
80
- const options: DeployOptions = {
81
- contract: "/mocked/contract/path",
82
- args: [1, 2, 3],
83
- kwargs: "key1=value1,key2=42",
84
- };
85
-
86
- await expect(deployer.deploy(options)).rejects.toThrowError(
87
- "Invalid usage: Please specify either `args` or `kwargs`, but not both."
88
- );
89
-
90
- expect(fs.readFileSync).not.toHaveBeenCalled();
91
- expect(mockClient.deployContract).not.toHaveBeenCalled();
89
+ expect(mockClient.deployContract).toHaveReturnedWith(Promise.resolve("mocked_tx_hash"));
92
90
  });
93
91
 
94
92
  test("throws error for missing contract", async () => {
95
- const options: DeployOptions = {
96
- };
93
+ const options: DeployOptions = {};
97
94
 
98
95
  await deployer.deploy(options);
99
96
 
100
- expect(fs.readFileSync).not.toHaveBeenCalled();
97
+ expect(deployer["failSpinner"]).toHaveBeenCalledWith("No contract specified for deployment.");
101
98
  expect(mockClient.deployContract).not.toHaveBeenCalled();
102
99
  });
103
100
 
@@ -114,15 +111,13 @@ describe("Deploy Action", () => {
114
111
  new Error("Mocked deployment error")
115
112
  );
116
113
 
117
- await expect(deployer.deploy(options)).rejects.toThrowError(
118
- "Contract deployment failed."
119
- );
114
+ await deployer.deploy(options);
120
115
 
121
- expect(fs.readFileSync).toHaveBeenCalledWith(options.contract, "utf-8");
116
+ expect(deployer["failSpinner"]).toHaveBeenCalledWith("Error deploying contract", expect.any(Error));
122
117
  expect(mockClient.deployContract).toHaveBeenCalled();
123
118
  });
124
119
 
125
- test("throws error if contract code is empty", async () => {
120
+ test("handles empty contract code", async () => {
126
121
  const options: DeployOptions = {
127
122
  contract: "/mocked/contract/path",
128
123
  };
@@ -132,8 +127,183 @@ describe("Deploy Action", () => {
132
127
 
133
128
  await deployer.deploy(options);
134
129
 
135
- expect(fs.existsSync).toHaveBeenCalledWith(options.contract);
136
- expect(fs.readFileSync).toHaveBeenCalledWith(options.contract, "utf-8");
130
+ expect(deployer["failSpinner"]).toHaveBeenCalledWith("Contract code is empty.");
137
131
  expect(mockClient.deployContract).not.toHaveBeenCalled();
138
132
  });
133
+
134
+ test("deployScripts executes scripts in order", async () => {
135
+ vi.mocked(fs.existsSync).mockReturnValue(true);
136
+ vi.mocked(fs.readdirSync).mockReturnValue([
137
+ "1_first.ts",
138
+ "2_second.js",
139
+ "10_last.ts",
140
+ ] as any);
141
+
142
+ vi.spyOn(deployer as any, "executeTsScript").mockResolvedValue(undefined);
143
+ vi.spyOn(deployer as any, "executeJsScript").mockResolvedValue(undefined);
144
+
145
+ await deployer.deployScripts();
146
+
147
+ expect(deployer["setSpinnerText"]).toHaveBeenCalledWith("Found 3 deploy scripts. Executing...");
148
+ expect(deployer["executeTsScript"]).toHaveBeenCalledWith(expect.stringMatching(/1_first.ts/));
149
+ expect(deployer["executeJsScript"]).toHaveBeenCalledWith(expect.stringMatching(/2_second.js/));
150
+ expect(deployer["executeTsScript"]).toHaveBeenCalledWith(expect.stringMatching(/10_last.ts/));
151
+ });
152
+
153
+ test("executeTsScript transpiles and executes TypeScript", async () => {
154
+ const filePath = "/mocked/script.ts";
155
+ const outFile = "/mocked/script.compiled.js";
156
+
157
+ vi.spyOn(deployer as any, "executeJsScript").mockResolvedValue(undefined);
158
+ vi.mocked(buildSync).mockImplementation((() => {}) as any);
159
+
160
+ await deployer["executeTsScript"](filePath);
161
+
162
+ expect(deployer["startSpinner"]).toHaveBeenCalledWith(`Transpiling TypeScript file: ${filePath}`);
163
+ expect(buildSync).toHaveBeenCalledWith({
164
+ entryPoints: [filePath],
165
+ outfile: outFile,
166
+ bundle: false,
167
+ platform: "node",
168
+ format: "esm",
169
+ target: "es2020",
170
+ sourcemap: false,
171
+ });
172
+
173
+ expect(deployer["executeJsScript"]).toHaveBeenCalledWith(filePath, outFile);
174
+ expect(fs.unlinkSync).toHaveBeenCalledWith(outFile);
175
+ });
176
+
177
+ test("deployScripts fails when deploy folder is missing", async () => {
178
+ vi.mocked(fs.existsSync).mockReturnValue(false);
179
+
180
+ await deployer.deployScripts();
181
+
182
+ expect(deployer["failSpinner"]).toHaveBeenCalledWith("No deploy folder found.");
183
+ });
184
+
185
+ test("deployScripts sorts and executes scripts correctly", async () => {
186
+ vi.mocked(fs.existsSync).mockReturnValue(true);
187
+ vi.mocked(fs.readdirSync).mockReturnValue([
188
+ "10_last.ts",
189
+ "2_second.js",
190
+ "1_first.ts"
191
+ ] as any);
192
+
193
+ vi.spyOn(deployer as any, "executeTsScript").mockResolvedValue(undefined);
194
+ vi.spyOn(deployer as any, "executeJsScript").mockResolvedValue(undefined);
195
+
196
+ await deployer.deployScripts();
197
+
198
+ expect(deployer["executeTsScript"]).toHaveBeenCalledWith(expect.stringContaining("1_first.ts"));
199
+ expect(deployer["executeJsScript"]).toHaveBeenCalledWith(expect.stringContaining("2_second.js"));
200
+ expect(deployer["executeTsScript"]).toHaveBeenCalledWith(expect.stringContaining("10_last.ts"));
201
+ });
202
+
203
+ test("deployScripts fails when no scripts are found", async () => {
204
+ vi.mocked(fs.existsSync).mockReturnValue(true);
205
+ vi.mocked(fs.readdirSync).mockReturnValue([]);
206
+
207
+ await deployer.deployScripts();
208
+
209
+ expect(deployer["failSpinner"]).toHaveBeenCalledWith("No deploy scripts found.");
210
+ });
211
+
212
+ test("deployScripts handles script execution errors", async () => {
213
+ vi.mocked(fs.existsSync).mockReturnValue(true);
214
+ vi.mocked(fs.readdirSync).mockReturnValue(["1_failing.ts"] as any);
215
+ vi.spyOn(deployer as any, "executeTsScript").mockRejectedValue(new Error("Script error"));
216
+
217
+ await deployer.deployScripts();
218
+
219
+ expect(deployer["failSpinner"]).toHaveBeenCalledWith(
220
+ expect.stringContaining("Error executing script:"),
221
+ expect.any(Error)
222
+ );
223
+ });
224
+
225
+ test("executeJsScript fails gracefully", async () => {
226
+ const filePath = "/mocked/script.js";
227
+
228
+ await deployer["executeJsScript"](filePath);
229
+
230
+ expect(deployer["failSpinner"]).toHaveBeenCalledWith(
231
+ expect.stringContaining("Error executing:"),
232
+ expect.any(Error)
233
+ );
234
+ });
235
+
236
+ test("deploy fails when contract code is empty", async () => {
237
+ const options: DeployOptions = { contract: "/mocked/contract/path" };
238
+
239
+ vi.mocked(fs.existsSync).mockReturnValue(true);
240
+ vi.mocked(fs.readFileSync).mockReturnValue("");
241
+
242
+ await deployer.deploy(options);
243
+
244
+ expect(deployer["failSpinner"]).toHaveBeenCalledWith("Contract code is empty.");
245
+ });
246
+
247
+ test("deployScripts correctly sorts mixed numbered and non-numbered scripts", async () => {
248
+ vi.mocked(fs.existsSync).mockReturnValue(true);
249
+ vi.mocked(fs.readdirSync).mockReturnValue([
250
+ "script.ts",
251
+ "2alpha_script.ts",
252
+ "3alpha_script.ts",
253
+ "blpha_script.ts",
254
+ "clpha_script.ts"
255
+ ] as any);
256
+
257
+ vi.spyOn(deployer as any, "executeTsScript").mockResolvedValue(undefined);
258
+ vi.spyOn(deployer as any, "executeJsScript").mockResolvedValue(undefined);
259
+
260
+ await deployer.deployScripts();
261
+
262
+ expect(deployer["executeTsScript"]).toHaveBeenCalledWith(expect.stringContaining("script.ts"));
263
+ expect(deployer["executeTsScript"]).toHaveBeenCalledWith(expect.stringContaining("2alpha_script.ts"));
264
+ expect(deployer["executeTsScript"]).toHaveBeenCalledWith(expect.stringContaining("3alpha_script.ts"));
265
+ expect(deployer["executeTsScript"]).toHaveBeenCalledWith(expect.stringContaining("blpha_script.ts"));
266
+ expect(deployer["executeTsScript"]).toHaveBeenCalledWith(expect.stringContaining("clpha_script.ts"));
267
+ });
268
+
269
+ test("executeJsScript fails if module has no default export", async () => {
270
+ const filePath = "/mocked/script.js";
271
+
272
+ vi.doMock(pathToFileURL(filePath).href, () => ({ default: "Not a function" }));
273
+
274
+ await deployer["executeJsScript"](filePath);
275
+
276
+ expect(deployer["failSpinner"]).toHaveBeenCalledWith(
277
+ expect.stringContaining("No \"default\" function found in:"),
278
+ );
279
+ });
280
+
281
+ test("executeJsScript successfully executes a script", async () => {
282
+ const filePath = "/mocked/script.js";
283
+ const mockFn = vi.fn(); // This mock function simulates the script execution
284
+
285
+ vi.doMock(pathToFileURL(filePath).href, () => ({ default: mockFn }));
286
+
287
+ await deployer["executeJsScript"](filePath);
288
+
289
+ expect(mockFn).toHaveBeenCalledWith(deployer["genlayerClient"]);
290
+
291
+ expect(deployer["succeedSpinner"]).toHaveBeenCalledWith(`Successfully executed: ${filePath}`);
292
+ });
293
+
294
+ test("executeTsScript fails when buildSync throws an error", async () => {
295
+ const filePath = "/mocked/script.ts";
296
+ const error = new Error("Build failed");
297
+
298
+ vi.mocked(buildSync).mockImplementation(() => {
299
+ throw error; // Simulate an error during transpilation
300
+ });
301
+
302
+ await deployer["executeTsScript"](filePath);
303
+
304
+ expect(deployer["failSpinner"]).toHaveBeenCalledWith(
305
+ `Error executing: ${filePath}`,
306
+ error
307
+ );
308
+ });
139
309
  });
@@ -10,92 +10,79 @@ describe("ConfigActions", () => {
10
10
  beforeEach(() => {
11
11
  configActions = new ConfigActions();
12
12
  vi.clearAllMocks();
13
- });
14
13
 
15
- new ConfigFileManager();
14
+ vi.spyOn(configActions as any, "startSpinner").mockImplementation(() => {});
15
+ vi.spyOn(configActions as any, "succeedSpinner").mockImplementation(() => {});
16
+ vi.spyOn(configActions as any, "failSpinner").mockImplementation(() => {});
17
+ });
16
18
 
17
19
  afterEach(() => {
18
20
  vi.restoreAllMocks();
19
21
  });
20
22
 
21
23
  test("set method writes key-value pair to the configuration", () => {
22
- const consoleLogSpy = vi.spyOn(console, "log");
23
-
24
24
  configActions.set("defaultNetwork=testnet");
25
25
 
26
- expect(configActions["configManager"].writeConfig).toHaveBeenCalledWith("defaultNetwork", "testnet");
27
- expect(consoleLogSpy).toHaveBeenCalledWith("Configuration updated: defaultNetwork=testnet");
26
+ expect(configActions["writeConfig"]).toHaveBeenCalledWith("defaultNetwork", "testnet");
27
+ expect(configActions["startSpinner"]).toHaveBeenCalledWith("Updating configuration: defaultNetwork");
28
+ expect(configActions["succeedSpinner"]).toHaveBeenCalledWith("Configuration successfully updated");
28
29
  });
29
30
 
30
- test("set method throws error for invalid format", () => {
31
- const consoleErrorSpy = vi.spyOn(console, "error");
32
- const processExitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
33
- throw new Error("process.exit");
34
- });
31
+ test("set method fails for invalid format", () => {
32
+ configActions.set("invalidFormat");
35
33
 
36
- expect(() => configActions.set("invalidFormat")).toThrowError("process.exit");
37
-
38
- expect(consoleErrorSpy).toHaveBeenCalledWith("Invalid format. Use key=value.");
39
- expect(processExitSpy).toHaveBeenCalledWith(1);
34
+ expect(configActions["failSpinner"]).toHaveBeenCalledWith("Invalid format. Use 'key=value'.");
35
+ expect(configActions["writeConfig"]).not.toHaveBeenCalled();
40
36
  });
41
37
 
42
38
  test("get method retrieves value for a specific key", () => {
43
39
  vi.mocked(ConfigFileManager.prototype.getConfigByKey).mockReturnValue("testnet");
44
40
 
45
- const consoleLogSpy = vi.spyOn(console, "log");
46
-
47
41
  configActions.get("defaultNetwork");
48
42
 
49
- expect(configActions["configManager"].getConfigByKey).toHaveBeenCalledWith("defaultNetwork");
50
- expect(consoleLogSpy).toHaveBeenCalledWith("defaultNetwork=testnet");
43
+ expect(configActions["getConfigByKey"]).toHaveBeenCalledWith("defaultNetwork");
44
+ expect(configActions["startSpinner"]).toHaveBeenCalledWith("Retrieving value for: defaultNetwork");
45
+ expect(configActions["succeedSpinner"]).toHaveBeenCalledWith("Configuration successfully retrieved", "defaultNetwork=testnet");
51
46
  });
52
47
 
53
- test("get method prints message when key has no value", () => {
48
+ test("get method prints failure message when key does not exist", () => {
54
49
  vi.mocked(ConfigFileManager.prototype.getConfigByKey).mockReturnValue(null);
55
50
 
56
- const consoleLogSpy = vi.spyOn(console, "log");
57
-
58
51
  configActions.get("nonexistentKey");
59
52
 
60
- expect(configActions["configManager"].getConfigByKey).toHaveBeenCalledWith("nonexistentKey");
61
- expect(consoleLogSpy).toHaveBeenCalledWith("No value set for key: nonexistentKey");
53
+ expect(configActions["getConfigByKey"]).toHaveBeenCalledWith("nonexistentKey");
54
+ expect(configActions["failSpinner"]).toHaveBeenCalledWith("No configuration found for 'nonexistentKey'.");
62
55
  });
63
56
 
64
57
  test("get method retrieves the entire configuration when no key is provided", () => {
65
58
  const mockConfig = { defaultNetwork: "testnet" };
66
59
  vi.mocked(ConfigFileManager.prototype.getConfig).mockReturnValue(mockConfig);
67
60
 
68
- const consoleLogSpy = vi.spyOn(console, "log");
69
-
70
61
  configActions.get();
71
62
 
72
- expect(configActions["configManager"].getConfig).toHaveBeenCalledTimes(1);
73
- expect(consoleLogSpy).toHaveBeenCalledWith("Current configuration:", JSON.stringify(mockConfig, null, 2));
63
+ expect(configActions["getConfig"]).toHaveBeenCalled();
64
+ expect(configActions["startSpinner"]).toHaveBeenCalledWith("Retrieving all configurations");
65
+ expect(configActions["succeedSpinner"]).toHaveBeenCalledWith("All configurations successfully retrieved", JSON.stringify(mockConfig, null, 2));
74
66
  });
75
67
 
76
68
  test("reset method removes key from configuration", () => {
77
69
  const mockConfig = { defaultNetwork: "testnet" };
78
70
  vi.mocked(ConfigFileManager.prototype.getConfig).mockReturnValue(mockConfig);
79
71
 
80
- const consoleLogSpy = vi.spyOn(console, "log");
81
-
82
72
  configActions.reset("defaultNetwork");
83
73
 
84
- expect(configActions["configManager"].getConfig).toHaveBeenCalledTimes(1);
85
- expect(configActions["configManager"].writeConfig).toHaveBeenCalledWith("defaultNetwork", undefined);
86
- expect(consoleLogSpy).toHaveBeenCalledWith("Configuration key reset: defaultNetwork");
74
+ expect(configActions["getConfig"]).toHaveBeenCalled();
75
+ expect(configActions["startSpinner"]).toHaveBeenCalledWith("Resetting configuration: defaultNetwork");
76
+ expect(configActions["writeConfig"]).toHaveBeenCalledWith("defaultNetwork", undefined);
77
+ expect(configActions["succeedSpinner"]).toHaveBeenCalledWith("Configuration successfully reset");
87
78
  });
88
79
 
89
- test("reset method prints message when key does not exist", () => {
90
- const mockConfig = {};
91
- vi.mocked(ConfigFileManager.prototype.getConfig).mockReturnValue(mockConfig);
92
-
93
- const consoleLogSpy = vi.spyOn(console, "log");
80
+ test("reset method prints failure message when key does not exist", () => {
81
+ vi.mocked(ConfigFileManager.prototype.getConfig).mockReturnValue({});
94
82
 
95
83
  configActions.reset("nonexistentKey");
96
84
 
97
- expect(configActions["configManager"].getConfig).toHaveBeenCalledTimes(1);
98
- expect(configActions["configManager"].writeConfig).not.toHaveBeenCalled();
99
- expect(consoleLogSpy).toHaveBeenCalledWith("Key does not exist in the configuration: nonexistentKey");
85
+ expect(configActions["getConfig"]).toHaveBeenCalled();
86
+ expect(configActions["failSpinner"]).toHaveBeenCalledWith("Configuration key 'nonexistentKey' does not exist.");
100
87
  });
101
- });
88
+ });