forked from NillionNetwork/client-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.test.ts
More file actions
195 lines (166 loc) · 5.52 KB
/
Copy pathclient.test.ts
File metadata and controls
195 lines (166 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { NadaValue } from "@nillion/client-wasm";
import { beforeAll, describe, expect, it } from "vitest";
import { ZodError } from "zod";
import { createSignerFromKey } from "#/payment";
import type { ProgramId, Uuid } from "#/types";
import { type ValuesPermissions, ValuesPermissionsBuilder } from "#/types";
import { type VmClient, VmClientBuilder } from "#/vm";
import { Env, PrivateKeyPerSuite, loadProgram } from "./helpers";
describe("Client", () => {
let client: VmClient;
beforeAll(async () => {
const signer = await createSignerFromKey(PrivateKeyPerSuite.VmClient);
client = await new VmClientBuilder()
.seed("tests")
.bootnodeUrl(Env.bootnodeUrl)
.chainUrl(Env.nilChainUrl)
.signer(signer)
.build();
});
it("builder rejects if missing values", async () => {
try {
const builder = new VmClientBuilder();
await builder.build();
} catch (e) {
expect(e).toBeInstanceOf(ZodError);
expect((e as ZodError).issues).toHaveLength(4);
}
expect.assertions(2);
});
it("can query pool status", async () => {
const status = await client.queryPoolStatus().build().invoke();
expect(status.offsets.length).toBeGreaterThan(0);
});
describe.sequential("values", () => {
const expectedName = "foo";
const expectedValue = "42";
const expectedUpdatedValue = "39";
let expectedPermissions: ValuesPermissions;
let expectedId: string;
it("can store", async () => {
expectedId = await client
.storeValues()
.ttl(1)
.value(expectedName, NadaValue.new_secret_integer(expectedValue))
.build()
.invoke();
expect(expectedId).toHaveLength(36);
});
it("can retrieve", async () => {
const data = await client
.retrieveValues()
.id(expectedId)
.build()
.invoke();
const values = data[expectedName]!;
expect(values).toBeDefined();
expect(values.type).toBe("SecretInteger");
expect(values.value).toBe(expectedValue);
});
it("can update", async () => {
await client
.storeValues()
.ttl(1)
.id(expectedId)
.value(expectedName, NadaValue.new_secret_integer(expectedUpdatedValue))
.build()
.invoke();
const data = await client
.retrieveValues()
.id(expectedId)
.build()
.invoke();
const values = data[expectedName]!;
expect(values.type).toBe("SecretInteger");
expect(values.value).toBe(expectedUpdatedValue);
});
it("can retrieve permissions", async () => {
expectedPermissions = await client
.retrievePermissions()
.id(expectedId)
.build()
.invoke();
expect(expectedPermissions).toBeDefined();
expect(expectedPermissions.owner).toEqual(client.id);
expect(expectedPermissions.retrieve).toContainEqual(client.id);
expect(expectedPermissions.update).toContainEqual(client.id);
expect(expectedPermissions._delete).toContainEqual(client.id);
});
it("can update permissions", async () => {
await client
.updatePermissions()
.valuesId(expectedId)
.revokeDelete(client.id)
.build()
.invoke();
const updatedPermissions = await client
.retrievePermissions()
.id(expectedId)
.build()
.invoke();
expect(updatedPermissions).toBeDefined();
expect(updatedPermissions.owner).toEqual(client.id);
expect(updatedPermissions.retrieve).toContainEqual(client.id);
expect(updatedPermissions.update).toContainEqual(client.id);
expect(updatedPermissions._delete).not.toContainEqual(client.id);
});
it("can overwrite permissions", async () => {
const next = ValuesPermissionsBuilder.default(client.id);
const permissions = await client
.overwritePermissions()
.permissions(next)
.id(expectedId)
.build()
.invoke();
expect(permissions).toBeDefined();
expect(permissions.owner).toEqual(client.id);
expect(permissions.retrieve).toContainEqual(client.id);
expect(permissions.update).toContainEqual(client.id);
expect(permissions._delete).toContainEqual(client.id);
});
it("can delete", async () => {
const actual = await client
.deleteValues()
.id(expectedId)
.build()
.invoke();
expect(actual).toEqual(expectedId);
});
});
describe.sequential("compute", () => {
const name = "addition_division.nada.bin";
let programId: ProgramId;
let computeResultId: Uuid;
it("can upload program", async () => {
const program = loadProgram(name);
programId = await client
.storeProgram()
.name(name)
.program(program)
.build()
.invoke();
expect(programId).toBeTruthy();
});
it("can invoke compute", async () => {
computeResultId = await client
.invokeCompute()
.program(programId)
.inputParty("Party1", client.id)
.outputParty("Party1", [client.id])
.computeTimeValues("A", NadaValue.new_secret_integer("1"))
.computeTimeValues("B", NadaValue.new_secret_integer("4"))
.build()
.invoke();
expect(computeResultId).toBeTruthy();
});
it("can retrieve compute result", async () => {
const result = await client
.retrieveComputeResult()
.id(computeResultId)
.build()
.invoke();
expect(result).toBeTruthy();
expect(result.my_output?.value).toBe("3");
});
});
});