movehat 0.1.3 → 0.1.4

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 (58) hide show
  1. package/README.md +159 -80
  2. package/dist/commands/run.d.ts.map +1 -1
  3. package/dist/commands/run.js +40 -3
  4. package/dist/commands/run.js.map +1 -1
  5. package/dist/core/AccountManager.d.ts +186 -0
  6. package/dist/core/AccountManager.d.ts.map +1 -0
  7. package/dist/core/AccountManager.js +363 -0
  8. package/dist/core/AccountManager.js.map +1 -0
  9. package/dist/fork/manager.d.ts +33 -0
  10. package/dist/fork/manager.d.ts.map +1 -1
  11. package/dist/fork/manager.js +61 -0
  12. package/dist/fork/manager.js.map +1 -1
  13. package/dist/fork/storage.d.ts +10 -0
  14. package/dist/fork/storage.d.ts.map +1 -1
  15. package/dist/fork/storage.js +28 -1
  16. package/dist/fork/storage.js.map +1 -1
  17. package/dist/helpers/index.d.ts +8 -1
  18. package/dist/helpers/index.d.ts.map +1 -1
  19. package/dist/helpers/index.js +4 -0
  20. package/dist/helpers/index.js.map +1 -1
  21. package/dist/helpers/setup.d.ts.map +1 -1
  22. package/dist/helpers/setup.js +5 -4
  23. package/dist/helpers/setup.js.map +1 -1
  24. package/dist/helpers/setupLocalTesting.d.ts +60 -0
  25. package/dist/helpers/setupLocalTesting.d.ts.map +1 -0
  26. package/dist/helpers/setupLocalTesting.js +277 -0
  27. package/dist/helpers/setupLocalTesting.js.map +1 -0
  28. package/dist/helpers/testFixtures.d.ts +115 -0
  29. package/dist/helpers/testFixtures.d.ts.map +1 -0
  30. package/dist/helpers/testFixtures.js +163 -0
  31. package/dist/helpers/testFixtures.js.map +1 -0
  32. package/dist/node/LocalNodeManager.d.ts +68 -0
  33. package/dist/node/LocalNodeManager.d.ts.map +1 -0
  34. package/dist/node/LocalNodeManager.js +237 -0
  35. package/dist/node/LocalNodeManager.js.map +1 -0
  36. package/dist/runtime.d.ts.map +1 -1
  37. package/dist/runtime.js +91 -46
  38. package/dist/runtime.js.map +1 -1
  39. package/dist/templates/README.md +17 -4
  40. package/dist/templates/move/sources/Counter.move +12 -4
  41. package/dist/templates/tests/Counter.test.ts +98 -57
  42. package/dist/types/config.d.ts +24 -0
  43. package/dist/types/config.d.ts.map +1 -1
  44. package/package.json +1 -1
  45. package/src/commands/run.ts +43 -3
  46. package/src/core/AccountManager.ts +439 -0
  47. package/src/fork/manager.ts +74 -0
  48. package/src/fork/storage.ts +33 -1
  49. package/src/helpers/index.ts +18 -1
  50. package/src/helpers/setup.ts +4 -3
  51. package/src/helpers/setupLocalTesting.ts +335 -0
  52. package/src/helpers/testFixtures.ts +222 -0
  53. package/src/node/LocalNodeManager.ts +297 -0
  54. package/src/runtime.ts +108 -47
  55. package/src/templates/README.md +17 -4
  56. package/src/templates/move/sources/Counter.move +12 -4
  57. package/src/templates/tests/Counter.test.ts +98 -57
  58. package/src/types/config.ts +32 -0
@@ -1,72 +1,113 @@
1
1
  // @ts-nocheck - This is a template file, dependencies are installed in user projects
2
- import { describe, it, before } from "mocha";
2
+ import { describe, it, before, after } from "mocha";
3
3
  import { expect } from "chai";
4
- import { getMovehat, type MovehatRuntime } from "movehat";
4
+ import { setupTestFixture, teardownTestFixture, type TestFixture } from "movehat/helpers";
5
5
 
6
6
  describe("Counter Contract", () => {
7
- let mh: MovehatRuntime;
8
- let contractAddress: string;
7
+ let fixture: TestFixture<'counter'>;
9
8
 
10
9
  before(async function () {
11
- this.timeout(30000);
10
+ this.timeout(60000); // Allow time for local node startup + deployment
12
11
 
13
- // Initialize Movehat Runtime Environment
14
- // Uses testnet by default - no local setup required
15
- mh = await getMovehat();
12
+ // Setup local testing environment with auto-deployment
13
+ // This will:
14
+ // 1. Start a local Movement blockchain node
15
+ // 2. Generate and fund test accounts from local faucet
16
+ // 3. Auto-deploy the counter module
17
+ // 4. Return everything ready to use
18
+ //
19
+ // By default uses 'local-node' mode (full blockchain)
20
+ // For faster tests on existing state, pass { mode: 'fork' }
21
+ //
22
+ // Note: Use 'as const' for type inference
23
+ fixture = await setupTestFixture(['counter'] as const, ['alice', 'bob']);
16
24
 
17
- contractAddress = mh.account.accountAddress.toString();
18
-
19
- console.log(`\nTesting on ${mh.network.name}`);
20
- console.log(`Account: ${contractAddress}\n`);
25
+ console.log(`\n✅ Testing on local blockchain`);
26
+ console.log(` Deployer: ${fixture.accounts.deployer.accountAddress.toString()}`);
27
+ console.log(` Alice: ${fixture.accounts.alice.accountAddress.toString()}`);
28
+ console.log(` Bob: ${fixture.accounts.bob.accountAddress.toString()}\n`);
21
29
  });
22
30
 
23
31
  describe("Counter functionality", () => {
24
- it("should initialize counter using simulation", async function () {
25
- this.timeout(30000);
26
-
27
- // Build transaction
28
- const transaction = await mh.aptos.transaction.build.simple({
29
- sender: mh.account.accountAddress,
30
- data: {
31
- function: `${contractAddress}::counter::init`,
32
- functionArguments: []
33
- }
34
- });
35
-
36
- // Simulate transaction (no gas cost, instant)
37
- const [simulation] = await mh.aptos.transaction.simulate.simple({
38
- signerPublicKey: mh.account.publicKey,
39
- transaction
40
- });
41
-
42
- // Verify simulation succeeded
43
- expect(simulation.success).to.be.true;
44
- console.log(`Counter init simulated successfully`);
45
- console.log(`Gas used: ${simulation.gas_used}`);
32
+ it("should initialize with value 0", async () => {
33
+ const counter = fixture.contracts.counter; // Type-safe, no `!` needed
34
+ const deployer = fixture.accounts.deployer;
35
+
36
+ // Read counter value (returns string from view function)
37
+ const value = await counter.view<string>("get", [
38
+ deployer.accountAddress.toString()
39
+ ]);
40
+
41
+ console.log(` Counter value: ${value}`);
42
+
43
+ // Assert the counter is 0 (note: values from view are strings)
44
+ expect(parseInt(value)).to.equal(0);
45
+ });
46
+
47
+ it("should increment counter", async () => {
48
+ const counter = fixture.contracts.counter;
49
+ const deployer = fixture.accounts.deployer;
50
+
51
+ // Increment the counter
52
+ const tx = await counter.call(deployer, "increment", []);
53
+ console.log(` Transaction: ${tx.hash}`);
54
+
55
+ // Read new value
56
+ const value = await counter.view<string>("get", [
57
+ deployer.accountAddress.toString()
58
+ ]);
59
+
60
+ console.log(` New counter value: ${value}`);
61
+
62
+ // Should be 1 now
63
+ expect(parseInt(value)).to.equal(1);
64
+ });
65
+
66
+ it("alice can also increment counter", async () => {
67
+ const counter = fixture.contracts.counter;
68
+ const alice = fixture.accounts.alice;
69
+
70
+ // Alice increments her own counter
71
+ const tx = await counter.call(alice, "increment", []);
72
+ console.log(` Alice's transaction: ${tx.hash}`);
73
+
74
+ // Read counter value for Alice (each user has their own counter)
75
+ const aliceValue = await counter.view<string>("get", [
76
+ alice.accountAddress.toString()
77
+ ]);
78
+
79
+ console.log(` Alice's counter value: ${aliceValue}`);
80
+ expect(parseInt(aliceValue)).to.equal(1);
81
+
82
+ // Deployer's counter should still be 1 (unchanged)
83
+ const deployerValue = await counter.view<string>("get", [
84
+ fixture.accounts.deployer.accountAddress.toString()
85
+ ]);
86
+
87
+ console.log(` Deployer's counter value: ${deployerValue}`);
88
+ expect(parseInt(deployerValue)).to.equal(1);
46
89
  });
47
90
 
48
- it("should increment counter using simulation", async function () {
49
- this.timeout(30000);
50
-
51
- // Build increment transaction
52
- const transaction = await mh.aptos.transaction.build.simple({
53
- sender: mh.account.accountAddress,
54
- data: {
55
- function: `${contractAddress}::counter::increment`,
56
- functionArguments: []
57
- }
58
- });
59
-
60
- // Simulate transaction
61
- const [simulation] = await mh.aptos.transaction.simulate.simple({
62
- signerPublicKey: mh.account.publicKey,
63
- transaction
64
- });
65
-
66
- // Verify simulation succeeded
67
- expect(simulation.success).to.be.true;
68
- console.log(`Counter increment simulated successfully`);
69
- console.log(`Gas used: ${simulation.gas_used}`);
91
+ it("bob can also increment the counter", async () => {
92
+ const counter = fixture.contracts.counter;
93
+ const bob = fixture.accounts.bob;
94
+
95
+ // Bob increments his own counter
96
+ const tx = await counter.call(bob, "increment", []);
97
+ console.log(` Bob's transaction: ${tx.hash}`);
98
+
99
+ // Read counter value for Bob (each user has their own counter)
100
+ const bobValue = await counter.view<string>("get", [
101
+ bob.accountAddress.toString()
102
+ ]);
103
+
104
+ console.log(` Bob's counter value: ${bobValue}`);
105
+ expect(parseInt(bobValue)).to.equal(1);
70
106
  });
71
107
  });
72
- });
108
+
109
+ after(async () => {
110
+ // Cleanup: Stop local node and clear account pool
111
+ await teardownTestFixture();
112
+ });
113
+ });
@@ -33,4 +33,36 @@ export interface MovehatConfig {
33
33
  account: string; // Account address (derived from privateKey)
34
34
  namedAddresses: Record<string, string>; // Merged named addresses
35
35
  networkConfig: NetworkConfig; // Full network configuration
36
+ }
37
+
38
+ /**
39
+ * Testing mode for local environment
40
+ */
41
+ export type LocalTestingMode = 'local-node' | 'fork';
42
+
43
+ /**
44
+ * Options for setting up local testing environment
45
+ */
46
+ export interface LocalTestOptions {
47
+ mode?: LocalTestingMode; // Testing mode: 'local-node' (full blockchain) or 'fork' (read-only snapshot) (default: 'local-node')
48
+
49
+ // Local Node options (when mode='local-node')
50
+ nodeTestDir?: string; // Directory for node data (default: .movehat/local-node)
51
+ nodeForceRestart?: boolean; // Clean node state and start fresh (default: true)
52
+ nodeFaucetPort?: number; // Faucet port for local node (default: 8081)
53
+ nodeApiPort?: number; // API/RPC port for local node (default: 8080)
54
+ nodeReadyPort?: number; // Ready server port (default: 8070)
55
+ nodeSilent?: boolean; // Suppress node output (default: false)
56
+
57
+ // Fork options (when mode='fork')
58
+ forkNetwork?: 'testnet' | string; // Network to fork from (default: 'testnet')
59
+ forkName?: string; // Name for the fork (default: 'test-local')
60
+ forkPort?: number; // Fork server port (default: 8080)
61
+ forkResetState?: boolean; // Clear fork state before tests (default: true)
62
+
63
+ // Common options (both modes)
64
+ autoFund?: boolean; // Auto-fund accounts (default: true)
65
+ defaultBalance?: number; // Default balance in octas (default: 100_000_000 = 100 APT)
66
+ autoDeploy?: readonly string[]; // Modules to auto-deploy (accepts readonly arrays)
67
+ accountLabels?: readonly string[]; // Labels for pre-generated accounts (default: ['deployer', 'alice', 'bob'])
36
68
  }