ic-mops 0.36.1 → 0.37.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 (106) hide show
  1. package/bin/moc-wrapper.sh +3 -0
  2. package/bin/mops.ts +3 -0
  3. package/cache.ts +2 -2
  4. package/cli.ts +61 -4
  5. package/commands/add.ts +3 -1
  6. package/commands/bench-replica.ts +5 -3
  7. package/commands/docs.ts +9 -6
  8. package/commands/install-all.ts +7 -2
  9. package/commands/install.ts +5 -1
  10. package/commands/publish.ts +2 -0
  11. package/commands/test/test.ts +42 -19
  12. package/commands/toolchain/index.ts +325 -0
  13. package/commands/toolchain/moc.ts +58 -0
  14. package/commands/toolchain/pocket-ic.ts +74 -0
  15. package/commands/toolchain/toolchain-utils.ts +83 -0
  16. package/commands/toolchain/wasmtime.ts +45 -0
  17. package/dist/bin/mops.d.ts +1 -1
  18. package/dist/cache.js +2 -2
  19. package/dist/cli.d.ts +0 -1
  20. package/dist/cli.js +52 -3
  21. package/dist/commands/add.js +2 -1
  22. package/dist/commands/bench-replica.d.ts +1 -1
  23. package/dist/commands/bench-replica.js +5 -3
  24. package/dist/commands/docs.js +9 -6
  25. package/dist/commands/install-all.js +5 -2
  26. package/dist/commands/install.js +5 -1
  27. package/dist/commands/publish.js +1 -0
  28. package/dist/commands/test/test.js +41 -19
  29. package/dist/commands/toolchain/index.d.ts +26 -0
  30. package/dist/commands/toolchain/index.js +274 -0
  31. package/dist/commands/toolchain/moc.d.ts +5 -1
  32. package/dist/commands/toolchain/moc.js +18 -6
  33. package/dist/commands/toolchain/mocv.js +0 -1
  34. package/dist/commands/toolchain/pocket-ic.d.ts +12 -0
  35. package/dist/commands/toolchain/pocket-ic.js +62 -0
  36. package/dist/commands/toolchain/toolchain-utils.d.ts +2 -2
  37. package/dist/commands/toolchain/toolchain-utils.js +22 -6
  38. package/dist/commands/toolchain/wasmtime.d.ts +5 -1
  39. package/dist/commands/toolchain/wasmtime.js +16 -4
  40. package/dist/integrity.js +40 -15
  41. package/dist/mops.js +2 -2
  42. package/dist/package.json +9 -4
  43. package/dist/pic-js/examples/clock/tests/clock/index.d.ts +1 -0
  44. package/dist/pic-js/examples/clock/tests/clock/index.js +5 -0
  45. package/dist/pic-js/examples/clock/tests/jest.config.d.ts +3 -0
  46. package/dist/pic-js/examples/clock/tests/jest.config.js +8 -0
  47. package/dist/pic-js/examples/clock/tests/src/clock.spec.d.ts +1 -0
  48. package/dist/pic-js/examples/clock/tests/src/clock.spec.js +48 -0
  49. package/dist/pic-js/examples/counter/tests/counter/index.d.ts +1 -0
  50. package/dist/pic-js/examples/counter/tests/counter/index.js +5 -0
  51. package/dist/pic-js/examples/counter/tests/jest.config.d.ts +3 -0
  52. package/dist/pic-js/examples/counter/tests/jest.config.js +8 -0
  53. package/dist/pic-js/examples/counter/tests/src/counter.spec.d.ts +1 -0
  54. package/dist/pic-js/examples/counter/tests/src/counter.spec.js +80 -0
  55. package/dist/pic-js/examples/todo/tests/jest.config.d.ts +3 -0
  56. package/dist/pic-js/examples/todo/tests/jest.config.js +8 -0
  57. package/dist/pic-js/examples/todo/tests/src/todo.spec.d.ts +1 -0
  58. package/dist/pic-js/examples/todo/tests/src/todo.spec.js +211 -0
  59. package/dist/pic-js/examples/todo/tests/todo/index.d.ts +1 -0
  60. package/dist/pic-js/examples/todo/tests/todo/index.js +5 -0
  61. package/dist/pic-js/packages/pic/src/error.d.ts +12 -0
  62. package/dist/pic-js/packages/pic/src/error.js +36 -0
  63. package/dist/pic-js/packages/pic/src/http-client.d.ts +15 -0
  64. package/dist/pic-js/packages/pic/src/http-client.js +37 -0
  65. package/dist/pic-js/packages/pic/src/identity.d.ts +66 -0
  66. package/dist/pic-js/packages/pic/src/identity.js +86 -0
  67. package/dist/pic-js/packages/pic/src/index.d.ts +4 -0
  68. package/dist/pic-js/packages/pic/src/index.js +8 -0
  69. package/dist/pic-js/packages/pic/src/management-canister.d.ts +30 -0
  70. package/dist/pic-js/packages/pic/src/management-canister.js +43 -0
  71. package/dist/pic-js/packages/pic/src/pocket-ic-actor.d.ts +83 -0
  72. package/dist/pic-js/packages/pic/src/pocket-ic-actor.js +58 -0
  73. package/dist/pic-js/packages/pic/src/pocket-ic-client-types.d.ts +61 -0
  74. package/dist/pic-js/packages/pic/src/pocket-ic-client-types.js +2 -0
  75. package/dist/pic-js/packages/pic/src/pocket-ic-client.d.ts +24 -0
  76. package/dist/pic-js/packages/pic/src/pocket-ic-client.js +123 -0
  77. package/dist/pic-js/packages/pic/src/pocket-ic-server.d.ts +10 -0
  78. package/dist/pic-js/packages/pic/src/pocket-ic-server.js +55 -0
  79. package/dist/pic-js/packages/pic/src/pocket-ic-types.d.ts +40 -0
  80. package/dist/pic-js/packages/pic/src/pocket-ic-types.js +2 -0
  81. package/dist/pic-js/packages/pic/src/pocket-ic.d.ts +447 -0
  82. package/dist/pic-js/packages/pic/src/pocket-ic.js +551 -0
  83. package/dist/pic-js/packages/pic/src/util/candid.d.ts +1 -0
  84. package/dist/pic-js/packages/pic/src/util/candid.js +7 -0
  85. package/dist/pic-js/packages/pic/src/util/encoding.d.ts +5 -0
  86. package/dist/pic-js/packages/pic/src/util/encoding.js +19 -0
  87. package/dist/pic-js/packages/pic/src/util/fs.d.ts +4 -0
  88. package/dist/pic-js/packages/pic/src/util/fs.js +29 -0
  89. package/dist/pic-js/packages/pic/src/util/index.d.ts +5 -0
  90. package/dist/pic-js/packages/pic/src/util/index.js +21 -0
  91. package/dist/pic-js/packages/pic/src/util/os.d.ts +4 -0
  92. package/dist/pic-js/packages/pic/src/util/os.js +19 -0
  93. package/dist/pic-js/packages/pic/src/util/poll.d.ts +5 -0
  94. package/dist/pic-js/packages/pic/src/util/poll.js +28 -0
  95. package/dist/templates/mops-test.yml +4 -4
  96. package/dist/types.d.ts +7 -0
  97. package/dist/vessel.js +5 -1
  98. package/global.d.ts +2 -1
  99. package/integrity.ts +57 -17
  100. package/mops.ts +3 -3
  101. package/package.json +9 -4
  102. package/templates/mops-test.yml +4 -4
  103. package/types.ts +10 -1
  104. package/vessel.ts +6 -1
  105. package/bun.lockb +0 -0
  106. package/cli-local.ts +0 -3
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createActorClass = void 0;
4
+ const candid_1 = require("@dfinity/candid");
5
+ const principal_1 = require("@dfinity/principal");
6
+ function createActorClass(interfaceFactory, canisterId, pocketIcClient) {
7
+ const service = interfaceFactory({ IDL: candid_1.IDL });
8
+ let sender = null;
9
+ function decodeReturnValue(types, msg) {
10
+ const returnValues = candid_1.IDL.decode(types, msg);
11
+ switch (returnValues.length) {
12
+ case 0:
13
+ return undefined;
14
+ case 1:
15
+ return returnValues[0];
16
+ default:
17
+ return returnValues;
18
+ }
19
+ }
20
+ function createActorMethod(methodName, func) {
21
+ if (func.annotations.includes('query') ||
22
+ func.annotations.includes('composite_query')) {
23
+ return createQueryMethod(methodName, func);
24
+ }
25
+ return createCallMethod(methodName, func);
26
+ }
27
+ function getSender() {
28
+ return sender ?? principal_1.Principal.anonymous();
29
+ }
30
+ function createQueryMethod(methodName, func) {
31
+ return async function (...args) {
32
+ const arg = candid_1.IDL.encode(func.argTypes, args);
33
+ const sender = getSender();
34
+ const response = await pocketIcClient.queryCall(canisterId, sender, methodName, new Uint8Array(arg));
35
+ return decodeReturnValue(func.retTypes, response);
36
+ };
37
+ }
38
+ function createCallMethod(methodName, func) {
39
+ return async function (...args) {
40
+ const arg = candid_1.IDL.encode(func.argTypes, args);
41
+ const sender = getSender();
42
+ const response = await pocketIcClient.updateCall(canisterId, sender, methodName, new Uint8Array(arg));
43
+ return decodeReturnValue(func.retTypes, response);
44
+ };
45
+ }
46
+ function Actor() { }
47
+ Actor.prototype.setPrincipal = function (newSender) {
48
+ sender = newSender;
49
+ };
50
+ Actor.prototype.setIdentity = function (identity) {
51
+ sender = identity.getPrincipal();
52
+ };
53
+ service._fields.forEach(([methodName, func]) => {
54
+ Actor.prototype[methodName] = createActorMethod(methodName, func);
55
+ });
56
+ return Actor;
57
+ }
58
+ exports.createActorClass = createActorClass;
@@ -0,0 +1,61 @@
1
+ export interface CreateInstanceSuccessResponse {
2
+ Created: {
3
+ instance_id: number;
4
+ };
5
+ }
6
+ export interface CreateInstanceErrorResponse {
7
+ Error: {
8
+ message: string;
9
+ };
10
+ }
11
+ export type CreateInstanceResponse = CreateInstanceSuccessResponse | CreateInstanceErrorResponse;
12
+ export interface CanisterCallRequest {
13
+ sender: string;
14
+ canister_id: string;
15
+ method: string;
16
+ payload: string;
17
+ }
18
+ export interface CanisterCallSuccessResponse {
19
+ Ok: {
20
+ Reply: string;
21
+ };
22
+ }
23
+ export interface CanisterCallErrorResponse {
24
+ Err: {
25
+ code: string;
26
+ description: string;
27
+ };
28
+ }
29
+ export type CanisterCallResponse = CanisterCallSuccessResponse | CanisterCallErrorResponse;
30
+ export interface GetTimeResponse {
31
+ nanos_since_epoch: number;
32
+ }
33
+ export interface SetTimeRequest {
34
+ nanos_since_epoch: number;
35
+ }
36
+ export interface CheckCanisterExistsRequest {
37
+ canister_id: string;
38
+ }
39
+ export interface GetCanisterCyclesBalanceRequest {
40
+ canister_id: string;
41
+ }
42
+ export interface GetCanisterCyclesBalanceResponse {
43
+ cycles: number;
44
+ }
45
+ export interface AddCanisterCyclesRequest {
46
+ canister_id: string;
47
+ amount: number;
48
+ }
49
+ export interface AddCanisterCyclesResponse {
50
+ cycles: number;
51
+ }
52
+ export interface SetStableMemoryRequest {
53
+ canister_id: string;
54
+ blob_id: number[];
55
+ }
56
+ export interface GetStableMemoryRequest {
57
+ canister_id: string;
58
+ }
59
+ export interface GetStableMemoryResponse {
60
+ blob: string;
61
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,24 @@
1
+ import { Principal } from '@dfinity/principal';
2
+ export declare class PocketIcClient {
3
+ private readonly instanceUrl;
4
+ private readonly serverUrl;
5
+ private constructor();
6
+ static create(url: string): Promise<PocketIcClient>;
7
+ private static createInstance;
8
+ deleteInstance(): Promise<void>;
9
+ tick(): Promise<void>;
10
+ getTime(): Promise<number>;
11
+ setTime(time: number): Promise<void>;
12
+ fetchRootKey(): Promise<Uint8Array>;
13
+ checkCanisterExists(canisterId: Principal): Promise<boolean>;
14
+ getCyclesBalance(canisterId: Principal): Promise<number>;
15
+ addCycles(canisterId: Principal, amount: number): Promise<number>;
16
+ uploadBlob(blob: Uint8Array): Promise<Uint8Array>;
17
+ setStableMemory(canisterId: Principal, blobId: Uint8Array): Promise<void>;
18
+ getStableMemory(canisterId: Principal): Promise<Uint8Array>;
19
+ updateCall(canisterId: Principal, sender: Principal, method: string, payload: Uint8Array): Promise<Uint8Array>;
20
+ queryCall(canisterId: Principal, sender: Principal, method: string, payload: Uint8Array): Promise<Uint8Array>;
21
+ private canisterCall;
22
+ private post;
23
+ private get;
24
+ }
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PocketIcClient = void 0;
4
+ const util_1 = require("./util");
5
+ const http_client_1 = require("./http-client");
6
+ const PROCESSING_TIME_HEADER = 'processing-timeout-ms';
7
+ const PROCESSING_TIME_VALUE_MS = 300000;
8
+ const PROCESSING_HEADER = {
9
+ [PROCESSING_TIME_HEADER]: PROCESSING_TIME_VALUE_MS.toString(),
10
+ };
11
+ class PocketIcClient {
12
+ constructor(instanceUrl, serverUrl) {
13
+ this.instanceUrl = instanceUrl;
14
+ this.serverUrl = serverUrl;
15
+ }
16
+ static async create(url) {
17
+ const instanceId = await PocketIcClient.createInstance(url);
18
+ return new PocketIcClient(`${url}/instances/${instanceId}`, url);
19
+ }
20
+ static async createInstance(url) {
21
+ const response = await http_client_1.HttpClient.post(`${url}/instances`);
22
+ if ('Error' in response) {
23
+ throw new Error(response.Error.message);
24
+ }
25
+ return response.Created.instance_id;
26
+ }
27
+ async deleteInstance() {
28
+ await fetch(this.instanceUrl, {
29
+ method: 'DELETE',
30
+ });
31
+ }
32
+ async tick() {
33
+ return await this.post('/update/tick');
34
+ }
35
+ async getTime() {
36
+ const response = await this.get('/read/get_time');
37
+ return response.nanos_since_epoch / 1000000;
38
+ }
39
+ async setTime(time) {
40
+ await this.post('/update/set_time', {
41
+ nanos_since_epoch: time * 1000000,
42
+ });
43
+ }
44
+ async fetchRootKey() {
45
+ return await this.post('/read/root_key');
46
+ }
47
+ async checkCanisterExists(canisterId) {
48
+ return await this.post('/read/canister_exists', { canister_id: (0, util_1.base64EncodePrincipal)(canisterId) });
49
+ }
50
+ async getCyclesBalance(canisterId) {
51
+ const response = await this.post('/read/get_cycles', {
52
+ canister_id: (0, util_1.base64EncodePrincipal)(canisterId),
53
+ });
54
+ return response.cycles;
55
+ }
56
+ async addCycles(canisterId, amount) {
57
+ const response = await this.post('/update/add_cycles', {
58
+ canister_id: (0, util_1.base64EncodePrincipal)(canisterId),
59
+ amount,
60
+ });
61
+ return response.cycles;
62
+ }
63
+ async uploadBlob(blob) {
64
+ const response = await fetch(`${this.serverUrl}/blobstore`, {
65
+ method: 'POST',
66
+ body: blob,
67
+ });
68
+ const responseText = await response.text();
69
+ return new Uint8Array((0, util_1.hexDecode)(responseText));
70
+ }
71
+ async setStableMemory(canisterId, blobId) {
72
+ const request = {
73
+ canister_id: (0, util_1.base64EncodePrincipal)(canisterId),
74
+ blob_id: Array.from(blobId),
75
+ };
76
+ const response = await fetch(`${this.instanceUrl}/update/set_stable_memory`, {
77
+ method: 'POST',
78
+ headers: {
79
+ ...http_client_1.JSON_HEADER,
80
+ ...PROCESSING_HEADER,
81
+ },
82
+ body: JSON.stringify(request),
83
+ });
84
+ (0, http_client_1.handleFetchError)(response);
85
+ }
86
+ async getStableMemory(canisterId) {
87
+ const response = await this.post('/read/get_stable_memory', {
88
+ canister_id: (0, util_1.base64EncodePrincipal)(canisterId),
89
+ });
90
+ return (0, util_1.base64Decode)(response.blob);
91
+ }
92
+ async updateCall(canisterId, sender, method, payload) {
93
+ return await this.canisterCall('/update/execute_ingress_message', canisterId, sender, method, payload);
94
+ }
95
+ async queryCall(canisterId, sender, method, payload) {
96
+ return await this.canisterCall('/read/query', canisterId, sender, method, payload);
97
+ }
98
+ async canisterCall(endpoint, canisterId, sender, method, payload) {
99
+ let rawCanisterCall = {
100
+ sender: (0, util_1.base64EncodePrincipal)(sender),
101
+ canister_id: (0, util_1.base64EncodePrincipal)(canisterId),
102
+ method,
103
+ payload: (0, util_1.base64Encode)(payload),
104
+ };
105
+ const response = await this.post(endpoint, rawCanisterCall);
106
+ if ('Err' in response) {
107
+ throw new Error(response.Err.description);
108
+ }
109
+ return (0, util_1.base64Decode)(response.Ok.Reply);
110
+ }
111
+ async post(endpoint, body) {
112
+ return await http_client_1.HttpClient.post(`${this.instanceUrl}${endpoint}`, {
113
+ body,
114
+ headers: PROCESSING_HEADER,
115
+ });
116
+ }
117
+ async get(endpoint) {
118
+ return await http_client_1.HttpClient.get(`${this.instanceUrl}${endpoint}`, {
119
+ headers: PROCESSING_HEADER,
120
+ });
121
+ }
122
+ }
123
+ exports.PocketIcClient = PocketIcClient;
@@ -0,0 +1,10 @@
1
+ export declare class PocketIcServer {
2
+ private readonly serverProcess;
3
+ private readonly url;
4
+ private constructor();
5
+ static start(): Promise<PocketIcServer>;
6
+ getUrl(): string;
7
+ stop(): void;
8
+ private static getBinPath;
9
+ private static assertBinExists;
10
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PocketIcServer = void 0;
4
+ const node_child_process_1 = require("node:child_process");
5
+ const node_path_1 = require("node:path");
6
+ const error_1 = require("./error");
7
+ const util_1 = require("./util");
8
+ class PocketIcServer {
9
+ constructor(serverProcess, portNumber) {
10
+ this.serverProcess = serverProcess;
11
+ this.url = `http://127.0.0.1:${portNumber}`;
12
+ }
13
+ static async start() {
14
+ const binPath = this.getBinPath();
15
+ await this.assertBinExists(binPath);
16
+ const pid = process.ppid;
17
+ const picFilePrefix = `pocket_ic_${pid}`;
18
+ const portFilePath = (0, util_1.tmpFile)(`${picFilePrefix}.port`);
19
+ const readyFilePath = (0, util_1.tmpFile)(`${picFilePrefix}.ready`);
20
+ const serverProcess = (0, node_child_process_1.spawn)(binPath, ['--pid', pid.toString()], {
21
+ stdio: 'ignore',
22
+ });
23
+ serverProcess.on('error', error => {
24
+ if ((0, util_1.isArm)() && (0, util_1.isDarwin)()) {
25
+ throw new error_1.BinStartMacOSArmError(error);
26
+ }
27
+ throw new error_1.BinStartError(error);
28
+ });
29
+ return await (0, util_1.poll)(async () => {
30
+ const isPocketIcReady = await (0, util_1.exists)(readyFilePath);
31
+ if (isPocketIcReady) {
32
+ const portString = await (0, util_1.readFileAsString)(portFilePath);
33
+ const port = parseInt(portString);
34
+ return new PocketIcServer(serverProcess, port);
35
+ }
36
+ throw new error_1.BinTimeoutError();
37
+ });
38
+ }
39
+ getUrl() {
40
+ return this.url;
41
+ }
42
+ stop() {
43
+ this.serverProcess.unref();
44
+ }
45
+ static getBinPath() {
46
+ return (0, node_path_1.resolve)(__dirname, '..', 'pocket-ic');
47
+ }
48
+ static async assertBinExists(binPath) {
49
+ const binExists = await (0, util_1.exists)(binPath);
50
+ if (!binExists) {
51
+ throw new error_1.BinNotFoundError(binPath);
52
+ }
53
+ }
54
+ }
55
+ exports.PocketIcServer = PocketIcServer;
@@ -0,0 +1,40 @@
1
+ import { Principal } from '@dfinity/principal';
2
+ import { ActorInterface, Actor } from './pocket-ic-actor';
3
+ /**
4
+ * A canister testing fixture for PocketIC that provides essential testing primitives
5
+ * such as an {@link Actor} and CanisterId.
6
+ *
7
+ * @category Types
8
+ * @see [Principal](https://agent-js.icp.xyz/principal/classes/Principal.html)
9
+ */
10
+ export interface CanisterFixture<T = ActorInterface> {
11
+ /**
12
+ * The {@link Actor} instance.
13
+ */
14
+ actor: Actor<T>;
15
+ /**
16
+ * The Principal of the canister.
17
+ */
18
+ canisterId: Principal;
19
+ }
20
+ /**
21
+ * Options for creating a canister.
22
+ *
23
+ * @param cycles The amount of cycles to send to the canister.
24
+ * Defaults to 1_000_000_000_000_000_000n.
25
+ * @param controllers The controllers of the canister.
26
+ * Defaults to the sender, which defaults to the anonymous principal.
27
+ * @param computeAllocation The compute allocation of the canister.
28
+ * @param memoryAllocation The memory allocation of the canister.
29
+ * @param freezingThreshold The freezing threshold of the canister.
30
+ *
31
+ * @category Types
32
+ * @see [Principal](https://agent-js.icp.xyz/principal/classes/Principal.html)
33
+ */
34
+ export interface CreateCanisterOptions {
35
+ cycles?: bigint;
36
+ controllers?: Principal[];
37
+ computeAllocation?: bigint;
38
+ memoryAllocation?: bigint;
39
+ freezingThreshold?: bigint;
40
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });