genlayer 0.12.4 → 0.12.5

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 (66) hide show
  1. package/.env.example +4 -0
  2. package/CHANGELOG.md +2 -0
  3. package/dist/index.js +406 -80
  4. package/esbuild.config.dev.js +1 -2
  5. package/esbuild.config.prod.js +1 -1
  6. package/eslint.config.js +2 -1
  7. package/package.json +5 -3
  8. package/src/commands/contracts/call.ts +16 -20
  9. package/src/commands/contracts/deploy.ts +107 -25
  10. package/src/commands/contracts/index.ts +14 -3
  11. package/src/commands/general/init.ts +0 -1
  12. package/src/commands/scaffold/index.ts +16 -0
  13. package/src/commands/scaffold/new.ts +34 -0
  14. package/src/index.ts +2 -0
  15. package/src/lib/actions/BaseAction.ts +6 -6
  16. package/src/lib/config/simulator.ts +2 -2
  17. package/templates/default/LICENSE +21 -0
  18. package/templates/default/README.md +101 -0
  19. package/templates/default/__init__.py +0 -0
  20. package/templates/default/app/.env.example +2 -0
  21. package/templates/default/app/.vscode/extensions.json +3 -0
  22. package/templates/default/app/README.md +5 -0
  23. package/templates/default/app/index.html +17 -0
  24. package/templates/default/app/package-lock.json +4920 -0
  25. package/templates/default/app/package.json +23 -0
  26. package/templates/default/app/postcss.config.js +6 -0
  27. package/templates/default/app/public/favicon.png +0 -0
  28. package/templates/default/app/src/App.vue +16 -0
  29. package/templates/default/app/src/components/Address.vue +38 -0
  30. package/templates/default/app/src/components/BetsScreen.vue +329 -0
  31. package/templates/default/app/src/logic/FootballBets.js +100 -0
  32. package/templates/default/app/src/main.js +5 -0
  33. package/templates/default/app/src/services/genlayer.js +19 -0
  34. package/templates/default/app/src/style.css +3 -0
  35. package/templates/default/app/tailwind.config.js +8 -0
  36. package/templates/default/app/vite.config.js +7 -0
  37. package/templates/default/config/__init__.py +0 -0
  38. package/templates/default/config/genlayer_config.py +14 -0
  39. package/templates/default/contracts/__init__.py +0 -0
  40. package/templates/default/contracts/football_bets.py +119 -0
  41. package/templates/default/deploy/deployScript.ts +31 -0
  42. package/templates/default/package-lock.json +3231 -0
  43. package/templates/default/package.json +7 -0
  44. package/templates/default/requirements.txt +6 -0
  45. package/templates/default/test/__init__.py +0 -0
  46. package/templates/default/test/football_bets_get_contract_schema_for_code.py +124 -0
  47. package/templates/default/test/test_football_bet_success_draw.py +108 -0
  48. package/templates/default/test/test_football_bet_success_win.py +106 -0
  49. package/templates/default/test/test_football_bet_unsuccess.py +107 -0
  50. package/templates/default/tools/__init__.py +0 -0
  51. package/templates/default/tools/accounts.py +5 -0
  52. package/templates/default/tools/calldata.py +224 -0
  53. package/templates/default/tools/request.py +134 -0
  54. package/templates/default/tools/response.py +52 -0
  55. package/templates/default/tools/structure.py +39 -0
  56. package/templates/default/tools/transactions.py +28 -0
  57. package/templates/default/tools/types.py +214 -0
  58. package/templates/default/tsconfig.json +7 -0
  59. package/tests/actions/call.test.ts +38 -76
  60. package/tests/actions/deploy.test.ts +200 -30
  61. package/tests/actions/new.test.ts +80 -0
  62. package/tests/commands/call.test.ts +6 -1
  63. package/tests/commands/deploy.test.ts +12 -1
  64. package/tests/commands/new.test.ts +68 -0
  65. package/tests/index.test.ts +4 -0
  66. package/vitest.config.ts +1 -1
@@ -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
  });
@@ -0,0 +1,80 @@
1
+ import { describe, test, vi, beforeEach, afterEach, expect } from "vitest";
2
+ import fs from "fs-extra";
3
+ import path from "path";
4
+ import { NewAction } from "../../src/commands/scaffold/new";
5
+
6
+ vi.mock("fs-extra");
7
+
8
+ describe("NewAction", () => {
9
+ let newAction: NewAction;
10
+ let mockExistsSync: any;
11
+ let mockCopySync: any;
12
+
13
+ beforeEach(() => {
14
+ vi.clearAllMocks();
15
+ newAction = new NewAction();
16
+
17
+ mockExistsSync = vi.mocked(fs.existsSync);
18
+ mockCopySync = vi.mocked(fs.copySync);
19
+
20
+ vi.spyOn(newAction as any, "startSpinner").mockImplementation(() => {});
21
+ vi.spyOn(newAction as any, "succeedSpinner").mockImplementation(() => {});
22
+ vi.spyOn(newAction as any, "failSpinner").mockImplementation(() => {});
23
+ });
24
+
25
+ afterEach(() => {
26
+ vi.restoreAllMocks();
27
+ });
28
+
29
+ test("should successfully create a new project", async () => {
30
+ mockExistsSync.mockReturnValue(false);
31
+ mockCopySync.mockImplementation(() => {});
32
+
33
+ await newAction.createProject("myProject", { path: ".", overwrite: false });
34
+
35
+ expect(newAction["startSpinner"]).toHaveBeenCalledWith("Creating new GenLayer project: myProject");
36
+ expect(mockCopySync).toHaveBeenCalledWith(expect.any(String), path.resolve(".", "myProject"));
37
+ expect(newAction["succeedSpinner"]).toHaveBeenCalledWith(
38
+ `Project "myProject" created successfully at ${path.resolve(".", "myProject")}`
39
+ );
40
+ });
41
+
42
+ test("should fail if project directory exists and overwrite is not set", async () => {
43
+ mockExistsSync.mockReturnValue(true);
44
+
45
+ await newAction.createProject("existingProject", { path: ".", overwrite: false });
46
+
47
+ expect(newAction["failSpinner"]).toHaveBeenCalledWith(
48
+ `Project directory "${path.resolve(".", "existingProject")}" already exists. Use --overwrite to replace it.`
49
+ );
50
+ expect(mockCopySync).not.toHaveBeenCalled();
51
+ });
52
+
53
+ test("should overwrite existing project if overwrite is set", async () => {
54
+ mockExistsSync.mockReturnValue(true);
55
+ mockCopySync.mockImplementation(() => {});
56
+
57
+ await newAction.createProject("overwriteProject", { path: ".", overwrite: true });
58
+
59
+ expect(newAction["startSpinner"]).toHaveBeenCalledWith("Creating new GenLayer project: overwriteProject");
60
+ expect(mockCopySync).toHaveBeenCalledWith(expect.any(String), path.resolve(".", "overwriteProject"));
61
+ expect(newAction["succeedSpinner"]).toHaveBeenCalledWith(
62
+ `Project "overwriteProject" created successfully at ${path.resolve(".", "overwriteProject")}`
63
+ );
64
+ });
65
+
66
+ test("should fail if an error occurs while copying files", async () => {
67
+ mockExistsSync.mockReturnValue(false);
68
+ const error = new Error("Mocked file system error");
69
+ mockCopySync.mockImplementation(() => {
70
+ throw error;
71
+ });
72
+
73
+ await newAction.createProject("errorProject", { path: ".", overwrite: false });
74
+
75
+ expect(newAction["failSpinner"]).toHaveBeenCalledWith(
76
+ "Error creating project \"errorProject\"",
77
+ error
78
+ );
79
+ });
80
+ });
@@ -4,6 +4,9 @@ import { vi, describe, beforeEach, afterEach, test, expect } from "vitest";
4
4
  import { initializeContractsCommands } from "../../src/commands/contracts";
5
5
 
6
6
  vi.mock("../../src/commands/contracts/call");
7
+ vi.mock("esbuild", () => ({
8
+ buildSync: vi.fn(),
9
+ }));
7
10
 
8
11
  describe("call command", () => {
9
12
  let program: Command;
@@ -39,12 +42,14 @@ describe("call command", () => {
39
42
  "1",
40
43
  "2",
41
44
  "Hello",
45
+ "false",
46
+ "true"
42
47
  ]);
43
48
  expect(CallAction).toHaveBeenCalledTimes(1);
44
49
  expect(CallAction.prototype.call).toHaveBeenCalledWith({
45
50
  contractAddress: "0xMockedContract",
46
51
  method: "updateData",
47
- args: ["1", "2", "Hello"]
52
+ args: [1, 2, "Hello", false, true]
48
53
  });
49
54
  });
50
55
 
@@ -4,6 +4,9 @@ import { initializeContractsCommands } from "../../src/commands/contracts";
4
4
  import { DeployAction } from "../../src/commands/contracts/deploy";
5
5
 
6
6
  vi.mock("../../src/commands/contracts/deploy");
7
+ vi.mock("esbuild", () => ({
8
+ buildSync: vi.fn(),
9
+ }));
7
10
 
8
11
  describe("deploy command", () => {
9
12
  let program: Command;
@@ -42,7 +45,7 @@ describe("deploy command", () => {
42
45
  expect(DeployAction).toHaveBeenCalledTimes(1);
43
46
  expect(DeployAction.prototype.deploy).toHaveBeenCalledWith({
44
47
  contract: "./path/to/contract",
45
- args: ["1", "2", "3"]
48
+ args: [1, 2, 3]
46
49
  });
47
50
  });
48
51
 
@@ -66,4 +69,12 @@ describe("deploy command", () => {
66
69
  program.parse(["node", "test", "deploy", "--contract", "./path/to/contract"])
67
70
  ).not.toThrow();
68
71
  });
72
+
73
+ test("DeployAction.deployScripts is called without throwing errors", async () => {
74
+ program.parse(["node", "test", "deploy"]);
75
+ vi.mocked(DeployAction.prototype.deployScripts).mockResolvedValueOnce(undefined);
76
+ expect(() =>
77
+ program.parse(["node", "test", "deploy"])
78
+ ).not.toThrow();
79
+ });
69
80
  });
@@ -0,0 +1,68 @@
1
+ import { Command } from "commander";
2
+ import { vi, describe, beforeEach, afterEach, test, expect } from "vitest";
3
+ import { initializeScaffoldCommands } from "../../src/commands/scaffold";
4
+ import { NewAction } from "../../src/commands/scaffold/new";
5
+
6
+ vi.mock("../../src/commands/scaffold/new");
7
+
8
+ describe("new command", () => {
9
+ let program: Command;
10
+
11
+ beforeEach(() => {
12
+ program = new Command();
13
+ initializeScaffoldCommands(program);
14
+ vi.clearAllMocks();
15
+ });
16
+
17
+ afterEach(() => {
18
+ vi.restoreAllMocks();
19
+ });
20
+
21
+ test("NewAction.createProject is called with default options", async () => {
22
+ program.parse(["node", "test", "new", "myProject"]);
23
+ expect(NewAction).toHaveBeenCalledTimes(1);
24
+ expect(NewAction.prototype.createProject).toHaveBeenCalledWith("myProject", {
25
+ path: ".",
26
+ overwrite: false,
27
+ });
28
+ });
29
+
30
+ test("NewAction.createProject is called with custom path", async () => {
31
+ program.parse(["node", "test", "new", "myProject", "--path", "./customDir"]);
32
+ expect(NewAction).toHaveBeenCalledTimes(1);
33
+ expect(NewAction.prototype.createProject).toHaveBeenCalledWith("myProject", {
34
+ path: "./customDir",
35
+ overwrite: false,
36
+ });
37
+ });
38
+
39
+ test("NewAction.createProject is called with overwrite flag", async () => {
40
+ program.parse(["node", "test", "new", "myProject", "--overwrite"]);
41
+ expect(NewAction).toHaveBeenCalledTimes(1);
42
+ expect(NewAction.prototype.createProject).toHaveBeenCalledWith("myProject", {
43
+ path: ".",
44
+ overwrite: true,
45
+ });
46
+ });
47
+
48
+ test("NewAction is instantiated when the new command is executed", async () => {
49
+ program.parse(["node", "test", "new", "myProject"]);
50
+ expect(NewAction).toHaveBeenCalledTimes(1);
51
+ });
52
+
53
+ test("throws error for unrecognized options", async () => {
54
+ const newCommand = program.commands.find((cmd) => cmd.name() === "new");
55
+ newCommand?.exitOverride();
56
+ expect(() => program.parse(["node", "test", "new", "myProject", "--unknown"])).toThrowError(
57
+ "error: unknown option '--unknown'"
58
+ );
59
+ });
60
+
61
+ test("NewAction.createProject is called without throwing errors for valid options", async () => {
62
+ program.parse(["node", "test", "new", "myProject"]);
63
+ vi.mocked(NewAction.prototype.createProject).mockResolvedValueOnce(undefined);
64
+ expect(() =>
65
+ program.parse(["node", "test", "new", "myProject"])
66
+ ).not.toThrow();
67
+ });
68
+ });
@@ -33,6 +33,10 @@ vi.mock("../src/commands/update", () => ({
33
33
  initializeUpdateCommands: vi.fn(),
34
34
  }));
35
35
 
36
+ vi.mock("../src/commands/scaffold", () => ({
37
+ initializeScaffoldCommands: vi.fn(),
38
+ }));
39
+
36
40
 
37
41
  describe("CLI", () => {
38
42
  it("should initialize CLI", () => {
package/vitest.config.ts CHANGED
@@ -6,7 +6,7 @@ export default defineConfig({
6
6
  environment: 'jsdom',
7
7
  testTimeout: 10000,
8
8
  coverage: {
9
- exclude: [...configDefaults.exclude, '*.js', 'tests/**/*.ts', 'src/types', 'scripts'],
9
+ exclude: [...configDefaults.exclude, '*.js', 'tests/**/*.ts', 'src/types', 'scripts', 'templates'],
10
10
  }
11
11
  }
12
12
  });