genlayer 0.10.0 → 0.10.2
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.
- package/CHANGELOG.md +14 -0
- package/dist/index.js +288 -23
- package/docker-compose.yml +13 -3
- package/package.json +1 -1
- package/src/commands/validators/index.ts +94 -0
- package/src/commands/validators/validators.ts +274 -0
- package/src/index.ts +2 -0
- package/src/lib/actions/BaseAction.ts +19 -0
- package/src/lib/clients/jsonRpcClient.ts +19 -20
- package/src/lib/config/simulator.ts +1 -1
- package/src/lib/services/simulator.ts +2 -2
- package/tests/actions/validators.test.ts +619 -0
- package/tests/commands/validator.test.ts +127 -0
- package/tests/index.test.ts +4 -0
- package/tests/libs/jsonRpcClient.test.ts +2 -3
- package/tests/services/simulator.test.ts +15 -1
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
import { describe, test, vi, beforeEach, afterEach, expect } from "vitest";
|
|
2
|
+
import { ValidatorsAction } from "../../src/commands/validators/validators";
|
|
3
|
+
import { rpcClient } from "../../src/lib/clients/jsonRpcClient";
|
|
4
|
+
import inquirer from "inquirer";
|
|
5
|
+
|
|
6
|
+
vi.mock("../../src/lib/clients/jsonRpcClient", () => ({
|
|
7
|
+
rpcClient: {
|
|
8
|
+
request: vi.fn(),
|
|
9
|
+
},
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
vi.mock("inquirer");
|
|
13
|
+
|
|
14
|
+
describe("ValidatorsAction", () => {
|
|
15
|
+
let validatorsAction: ValidatorsAction;
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
vi.clearAllMocks();
|
|
19
|
+
validatorsAction = new ValidatorsAction();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
vi.restoreAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("getValidator", () => {
|
|
27
|
+
test("should fetch a specific validator by address", async () => {
|
|
28
|
+
const mockAddress = "mocked_address";
|
|
29
|
+
const mockResponse = { result: { id: 1, name: "Validator1" } };
|
|
30
|
+
vi.mocked(rpcClient.request).mockResolvedValue(mockResponse);
|
|
31
|
+
|
|
32
|
+
console.log = vi.fn();
|
|
33
|
+
|
|
34
|
+
await validatorsAction.getValidator({ address: mockAddress });
|
|
35
|
+
|
|
36
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
37
|
+
method: "sim_getValidator",
|
|
38
|
+
params: [mockAddress],
|
|
39
|
+
});
|
|
40
|
+
expect(console.log).toHaveBeenCalledWith("Validator Details:", mockResponse.result);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("should fetch all validators when no address is provided", async () => {
|
|
44
|
+
const mockResponse = { result: [{ id: 1 }, { id: 2 }] };
|
|
45
|
+
vi.mocked(rpcClient.request).mockResolvedValue(mockResponse);
|
|
46
|
+
|
|
47
|
+
console.log = vi.fn();
|
|
48
|
+
|
|
49
|
+
await validatorsAction.getValidator({});
|
|
50
|
+
|
|
51
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
52
|
+
method: "sim_getAllValidators",
|
|
53
|
+
params: [],
|
|
54
|
+
});
|
|
55
|
+
expect(console.log).toHaveBeenCalledWith("All Validators:", mockResponse.result);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("should log an error if an exception occurs while fetching a specific validator", async () => {
|
|
59
|
+
const mockAddress = "mocked_address";
|
|
60
|
+
const mockError = new Error("Unexpected error");
|
|
61
|
+
|
|
62
|
+
vi.mocked(rpcClient.request).mockRejectedValue(mockError);
|
|
63
|
+
|
|
64
|
+
console.error = vi.fn();
|
|
65
|
+
|
|
66
|
+
await validatorsAction.getValidator({ address: mockAddress });
|
|
67
|
+
|
|
68
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
69
|
+
method: "sim_getValidator",
|
|
70
|
+
params: [mockAddress],
|
|
71
|
+
});
|
|
72
|
+
expect(console.error).toHaveBeenCalledWith("Error fetching validators:", mockError);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("should log an error if an exception occurs while fetching all validators", async () => {
|
|
76
|
+
const mockError = new Error("Unexpected error");
|
|
77
|
+
|
|
78
|
+
vi.mocked(rpcClient.request).mockRejectedValue(mockError);
|
|
79
|
+
|
|
80
|
+
console.error = vi.fn();
|
|
81
|
+
|
|
82
|
+
await validatorsAction.getValidator({});
|
|
83
|
+
|
|
84
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
85
|
+
method: "sim_getAllValidators",
|
|
86
|
+
params: [],
|
|
87
|
+
});
|
|
88
|
+
expect(console.error).toHaveBeenCalledWith("Error fetching validators:", mockError);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("deleteValidator", () => {
|
|
93
|
+
test("should delete a specific validator", async () => {
|
|
94
|
+
const mockAddress = "mocked_address";
|
|
95
|
+
vi.mocked(inquirer.prompt).mockResolvedValue({ confirmAction: true });
|
|
96
|
+
vi.mocked(rpcClient.request).mockResolvedValue({ result: { id: 1 } });
|
|
97
|
+
|
|
98
|
+
console.log = vi.fn();
|
|
99
|
+
|
|
100
|
+
await validatorsAction.deleteValidator({ address: mockAddress });
|
|
101
|
+
|
|
102
|
+
expect(inquirer.prompt).toHaveBeenCalled();
|
|
103
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
104
|
+
method: "sim_deleteValidator",
|
|
105
|
+
params: [mockAddress],
|
|
106
|
+
});
|
|
107
|
+
expect(console.log).toHaveBeenCalledWith("Deleted Address:", { id: 1 });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("should delete all validators when no address is provided", async () => {
|
|
111
|
+
vi.mocked(inquirer.prompt).mockResolvedValue({ confirmAction: true });
|
|
112
|
+
vi.mocked(rpcClient.request).mockResolvedValue({});
|
|
113
|
+
|
|
114
|
+
console.log = vi.fn();
|
|
115
|
+
|
|
116
|
+
await validatorsAction.deleteValidator({});
|
|
117
|
+
|
|
118
|
+
expect(inquirer.prompt).toHaveBeenCalled();
|
|
119
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
120
|
+
method: "sim_deleteAllValidators",
|
|
121
|
+
params: [],
|
|
122
|
+
});
|
|
123
|
+
expect(console.log).toHaveBeenCalledWith("Successfully deleted all validators");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("should abort deletion if user declines confirmation", async () => {
|
|
127
|
+
vi.mocked(inquirer.prompt).mockResolvedValue({ confirmAction: false });
|
|
128
|
+
|
|
129
|
+
console.log = vi.fn();
|
|
130
|
+
|
|
131
|
+
await validatorsAction.deleteValidator({ address: "mocked_address" })
|
|
132
|
+
|
|
133
|
+
expect(inquirer.prompt).toHaveBeenCalled();
|
|
134
|
+
expect(console.log).toHaveBeenCalledWith("Operation aborted!");
|
|
135
|
+
expect(rpcClient.request).not.toHaveBeenCalled();
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe("countValidators", () => {
|
|
140
|
+
test("should count all validators", async () => {
|
|
141
|
+
const mockResponse = { result: 42 };
|
|
142
|
+
vi.mocked(rpcClient.request).mockResolvedValue(mockResponse);
|
|
143
|
+
|
|
144
|
+
console.log = vi.fn();
|
|
145
|
+
|
|
146
|
+
await validatorsAction.countValidators();
|
|
147
|
+
|
|
148
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
149
|
+
method: "sim_countValidators",
|
|
150
|
+
params: [],
|
|
151
|
+
});
|
|
152
|
+
expect(console.log).toHaveBeenCalledWith("Total Validators:", 42);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("should log an error if an exception occurs while counting validators", async () => {
|
|
156
|
+
const mockError = new Error("Unexpected error");
|
|
157
|
+
|
|
158
|
+
vi.mocked(rpcClient.request).mockRejectedValue(mockError);
|
|
159
|
+
|
|
160
|
+
console.error = vi.fn();
|
|
161
|
+
|
|
162
|
+
await validatorsAction.countValidators();
|
|
163
|
+
|
|
164
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
165
|
+
method: "sim_countValidators",
|
|
166
|
+
params: [],
|
|
167
|
+
});
|
|
168
|
+
expect(console.error).toHaveBeenCalledWith("Error counting validators:", mockError);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe("createValidator", () => {
|
|
173
|
+
test("should create a validator with selected provider and model", async () => {
|
|
174
|
+
const mockProvidersAndModels = [
|
|
175
|
+
{
|
|
176
|
+
provider: "Provider1",
|
|
177
|
+
is_available: true,
|
|
178
|
+
is_model_available: true,
|
|
179
|
+
model: "Model1",
|
|
180
|
+
config: { max_tokens: 500 },
|
|
181
|
+
plugin: "Plugin1",
|
|
182
|
+
plugin_config: { api_key_env_var: "KEY1" },
|
|
183
|
+
},
|
|
184
|
+
];
|
|
185
|
+
const mockResponse = { result: { id: 123 } };
|
|
186
|
+
|
|
187
|
+
vi.mocked(rpcClient.request)
|
|
188
|
+
.mockResolvedValueOnce({ result: mockProvidersAndModels })
|
|
189
|
+
.mockResolvedValueOnce(mockResponse);
|
|
190
|
+
|
|
191
|
+
vi.mocked(inquirer.prompt)
|
|
192
|
+
.mockResolvedValueOnce({ selectedProvider: "Provider1" })
|
|
193
|
+
.mockResolvedValueOnce({ selectedModel: "Model1" });
|
|
194
|
+
|
|
195
|
+
console.log = vi.fn();
|
|
196
|
+
|
|
197
|
+
await validatorsAction.createValidator({ stake: "10" });
|
|
198
|
+
|
|
199
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
200
|
+
method: "sim_getProvidersAndModels",
|
|
201
|
+
params: [],
|
|
202
|
+
});
|
|
203
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
204
|
+
method: "sim_createValidator",
|
|
205
|
+
params: [
|
|
206
|
+
10,
|
|
207
|
+
"Provider1",
|
|
208
|
+
"Model1",
|
|
209
|
+
{ max_tokens: 500 },
|
|
210
|
+
"Plugin1",
|
|
211
|
+
{ api_key_env_var: "KEY1" },
|
|
212
|
+
],
|
|
213
|
+
});
|
|
214
|
+
expect(console.log).toHaveBeenCalledWith("Validator successfully created:", { id: 123 });
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("should log an error for invalid stake", async () => {
|
|
218
|
+
console.error = vi.fn();
|
|
219
|
+
|
|
220
|
+
await validatorsAction.createValidator({ stake: "invalid" });
|
|
221
|
+
|
|
222
|
+
expect(console.error).toHaveBeenCalledWith("Invalid stake. Please provide a positive integer.");
|
|
223
|
+
expect(rpcClient.request).not.toHaveBeenCalled();
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test("should log an error if no providers or models are available", async () => {
|
|
227
|
+
vi.mocked(rpcClient.request).mockResolvedValueOnce({ result: [] });
|
|
228
|
+
|
|
229
|
+
console.error = vi.fn();
|
|
230
|
+
|
|
231
|
+
await validatorsAction.createValidator({ stake: "10" });
|
|
232
|
+
|
|
233
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
234
|
+
method: "sim_getProvidersAndModels",
|
|
235
|
+
params: [],
|
|
236
|
+
});
|
|
237
|
+
expect(console.error).toHaveBeenCalledWith("No providers or models available.");
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test("should log an error if no models are available for the selected provider", async () => {
|
|
241
|
+
const mockProvidersAndModels = [
|
|
242
|
+
{ provider: "Provider1", is_available: true, is_model_available: false },
|
|
243
|
+
];
|
|
244
|
+
|
|
245
|
+
vi.mocked(rpcClient.request).mockResolvedValueOnce({ result: mockProvidersAndModels });
|
|
246
|
+
vi.mocked(inquirer.prompt).mockResolvedValueOnce({ selectedProvider: "Provider1" });
|
|
247
|
+
|
|
248
|
+
console.error = vi.fn();
|
|
249
|
+
|
|
250
|
+
await validatorsAction.createValidator({ stake: "10" });
|
|
251
|
+
|
|
252
|
+
expect(console.error).toHaveBeenCalledWith("No models available for the selected provider.");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test("should log an error if selected model details are not found", async () => {
|
|
256
|
+
const mockProvidersAndModels = [
|
|
257
|
+
{
|
|
258
|
+
provider: "Provider1",
|
|
259
|
+
is_available: true,
|
|
260
|
+
is_model_available: true,
|
|
261
|
+
model: "Model1",
|
|
262
|
+
},
|
|
263
|
+
];
|
|
264
|
+
|
|
265
|
+
vi.mocked(rpcClient.request).mockResolvedValueOnce({ result: mockProvidersAndModels });
|
|
266
|
+
vi.mocked(inquirer.prompt)
|
|
267
|
+
.mockResolvedValueOnce({ selectedProvider: "Provider1" })
|
|
268
|
+
.mockResolvedValueOnce({ selectedModel: "NonExistentModel" });
|
|
269
|
+
|
|
270
|
+
console.error = vi.fn();
|
|
271
|
+
|
|
272
|
+
await validatorsAction.createValidator({ stake: "10" });
|
|
273
|
+
|
|
274
|
+
expect(console.error).toHaveBeenCalledWith("Selected model details not found.");
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("should log an error if an exception occurs during the process", async () => {
|
|
278
|
+
const mockError = new Error("Unexpected error");
|
|
279
|
+
vi.mocked(rpcClient.request).mockRejectedValue(mockError);
|
|
280
|
+
|
|
281
|
+
console.error = vi.fn();
|
|
282
|
+
|
|
283
|
+
await validatorsAction.createValidator({ stake: "10" });
|
|
284
|
+
|
|
285
|
+
expect(console.error).toHaveBeenCalledWith("Error creating validator:", mockError);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
test("should use user-provided config if specified", async () => {
|
|
289
|
+
const mockProvidersAndModels = [
|
|
290
|
+
{
|
|
291
|
+
provider: "Provider1",
|
|
292
|
+
is_available: true,
|
|
293
|
+
is_model_available: true,
|
|
294
|
+
model: "Model1",
|
|
295
|
+
config: { max_tokens: 500 },
|
|
296
|
+
plugin: "Plugin1",
|
|
297
|
+
plugin_config: { api_key_env_var: "KEY1" },
|
|
298
|
+
},
|
|
299
|
+
];
|
|
300
|
+
const mockResponse = { result: { id: 123 } };
|
|
301
|
+
|
|
302
|
+
vi.mocked(rpcClient.request)
|
|
303
|
+
.mockResolvedValueOnce({ result: mockProvidersAndModels })
|
|
304
|
+
.mockResolvedValueOnce(mockResponse);
|
|
305
|
+
|
|
306
|
+
vi.mocked(inquirer.prompt)
|
|
307
|
+
.mockResolvedValueOnce({ selectedProvider: "Provider1" })
|
|
308
|
+
.mockResolvedValueOnce({ selectedModel: "Model1" });
|
|
309
|
+
|
|
310
|
+
console.log = vi.fn();
|
|
311
|
+
|
|
312
|
+
const customConfig = '{"custom_key":"custom_value"}';
|
|
313
|
+
await validatorsAction.createValidator({ stake: "10", config: customConfig });
|
|
314
|
+
|
|
315
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
316
|
+
method: "sim_createValidator",
|
|
317
|
+
params: [
|
|
318
|
+
10,
|
|
319
|
+
"Provider1",
|
|
320
|
+
"Model1",
|
|
321
|
+
{ custom_key: "custom_value" },
|
|
322
|
+
"Plugin1",
|
|
323
|
+
{ api_key_env_var: "KEY1" },
|
|
324
|
+
],
|
|
325
|
+
});
|
|
326
|
+
expect(console.log).toHaveBeenCalledWith("Validator successfully created:", { id: 123 });
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
describe("createRandomValidators", () => {
|
|
330
|
+
test("should create random validators with valid count and providers", async () => {
|
|
331
|
+
const mockResponse = { result: { success: true } };
|
|
332
|
+
vi.mocked(rpcClient.request).mockResolvedValue(mockResponse);
|
|
333
|
+
|
|
334
|
+
console.log = vi.fn();
|
|
335
|
+
|
|
336
|
+
await validatorsAction.createRandomValidators({ count: "5", providers: ["Provider1", "Provider2"], models: [] });
|
|
337
|
+
|
|
338
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
339
|
+
method: "sim_createRandomValidators",
|
|
340
|
+
params: [5, 1, 10, ["Provider1", "Provider2"], []],
|
|
341
|
+
});
|
|
342
|
+
expect(console.log).toHaveBeenCalledWith("Creating 5 random validator(s)...");
|
|
343
|
+
expect(console.log).toHaveBeenCalledWith("Providers: Provider1, Provider2");
|
|
344
|
+
expect(console.log).toHaveBeenCalledWith("Random validators successfully created:", mockResponse.result);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
test("should create random validators with valid count, providers and models", async () => {
|
|
348
|
+
const mockResponse = { result: { success: true } };
|
|
349
|
+
vi.mocked(rpcClient.request).mockResolvedValue(mockResponse);
|
|
350
|
+
|
|
351
|
+
console.log = vi.fn();
|
|
352
|
+
|
|
353
|
+
await validatorsAction.createRandomValidators({ count: "10", providers: ["Provider3"], models: ["Model1", "Model2"] });
|
|
354
|
+
|
|
355
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
356
|
+
method: "sim_createRandomValidators",
|
|
357
|
+
params: [10, 1, 10, ["Provider3"], ["Model1", "Model2"]],
|
|
358
|
+
});
|
|
359
|
+
expect(console.log).toHaveBeenCalledWith("Creating 10 random validator(s)...");
|
|
360
|
+
expect(console.log).toHaveBeenCalledWith("Providers: Provider3");
|
|
361
|
+
expect(console.log).toHaveBeenCalledWith("Models: Model1, Model2");
|
|
362
|
+
expect(console.log).toHaveBeenCalledWith("Random validators successfully created:", mockResponse.result);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
test("should create random validators with default provider message when providers list is empty", async () => {
|
|
366
|
+
const mockResponse = { result: { success: true } };
|
|
367
|
+
vi.mocked(rpcClient.request).mockResolvedValue(mockResponse);
|
|
368
|
+
|
|
369
|
+
console.log = vi.fn();
|
|
370
|
+
|
|
371
|
+
await validatorsAction.createRandomValidators({ count: "3", providers: [], models: [] });
|
|
372
|
+
|
|
373
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
374
|
+
method: "sim_createRandomValidators",
|
|
375
|
+
params: [3, 1, 10, [], []],
|
|
376
|
+
});
|
|
377
|
+
expect(console.log).toHaveBeenCalledWith("Creating 3 random validator(s)...");
|
|
378
|
+
expect(console.log).toHaveBeenCalledWith("Providers: None");
|
|
379
|
+
expect(console.log).toHaveBeenCalledWith("Random validators successfully created:", mockResponse.result);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test("should throw an error for invalid count", async () => {
|
|
383
|
+
console.error = vi.fn();
|
|
384
|
+
|
|
385
|
+
await validatorsAction.createRandomValidators({ count: "invalid", providers: ["Provider1"], models: [] });
|
|
386
|
+
|
|
387
|
+
expect(console.error).toHaveBeenCalledWith("Invalid count. Please provide a positive integer.");
|
|
388
|
+
expect(rpcClient.request).not.toHaveBeenCalled();
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
test("should log an error if rpc request fails", async () => {
|
|
392
|
+
const mockError = new Error("RPC failure");
|
|
393
|
+
vi.mocked(rpcClient.request).mockRejectedValue(mockError);
|
|
394
|
+
|
|
395
|
+
console.error = vi.fn();
|
|
396
|
+
|
|
397
|
+
await validatorsAction.createRandomValidators({ count: "5", providers: ["Provider1"], models: [] });
|
|
398
|
+
|
|
399
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
400
|
+
method: "sim_createRandomValidators",
|
|
401
|
+
params: [5, 1, 10, ["Provider1"], []],
|
|
402
|
+
});
|
|
403
|
+
expect(console.error).toHaveBeenCalledWith("Error creating random validators:", mockError);
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
describe("updateValidator", () => {
|
|
408
|
+
test("should fetch and update a validator with new stake", async () => {
|
|
409
|
+
const mockAddress = "mocked_address";
|
|
410
|
+
const mockCurrentValidator = {
|
|
411
|
+
result: {
|
|
412
|
+
address: "mocked_address",
|
|
413
|
+
stake: 100,
|
|
414
|
+
provider: "Provider1",
|
|
415
|
+
model: "Model1",
|
|
416
|
+
config: { max_tokens: 500 },
|
|
417
|
+
},
|
|
418
|
+
};
|
|
419
|
+
const mockResponse = { result: { success: true } };
|
|
420
|
+
|
|
421
|
+
vi.mocked(rpcClient.request)
|
|
422
|
+
.mockResolvedValueOnce(mockCurrentValidator)
|
|
423
|
+
.mockResolvedValueOnce(mockResponse);
|
|
424
|
+
|
|
425
|
+
console.log = vi.fn();
|
|
426
|
+
|
|
427
|
+
await validatorsAction.updateValidator({ address: mockAddress, stake: "200" });
|
|
428
|
+
|
|
429
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
430
|
+
method: "sim_getValidator",
|
|
431
|
+
params: [mockAddress],
|
|
432
|
+
});
|
|
433
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
434
|
+
method: "sim_updateValidator",
|
|
435
|
+
params: [
|
|
436
|
+
"mocked_address",
|
|
437
|
+
"200",
|
|
438
|
+
"Provider1",
|
|
439
|
+
"Model1",
|
|
440
|
+
{ max_tokens: 500 },
|
|
441
|
+
],
|
|
442
|
+
});
|
|
443
|
+
expect(console.log).toHaveBeenCalledWith("Validator successfully updated:", mockResponse.result);
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
test("should fetch and update a validator with new provider and model", async () => {
|
|
447
|
+
const mockAddress = "mocked_address";
|
|
448
|
+
const mockCurrentValidator = {
|
|
449
|
+
result: {
|
|
450
|
+
address: "mocked_address",
|
|
451
|
+
stake: "100",
|
|
452
|
+
provider: "Provider1",
|
|
453
|
+
model: "Model1",
|
|
454
|
+
config: { max_tokens: 500 },
|
|
455
|
+
},
|
|
456
|
+
};
|
|
457
|
+
const mockResponse = { result: { success: true } };
|
|
458
|
+
|
|
459
|
+
vi.mocked(rpcClient.request)
|
|
460
|
+
.mockResolvedValueOnce(mockCurrentValidator)
|
|
461
|
+
.mockResolvedValueOnce(mockResponse);
|
|
462
|
+
|
|
463
|
+
console.log = vi.fn();
|
|
464
|
+
|
|
465
|
+
await validatorsAction.updateValidator({
|
|
466
|
+
address: mockAddress,
|
|
467
|
+
provider: "Provider2",
|
|
468
|
+
model: "Model2",
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
472
|
+
method: "sim_getValidator",
|
|
473
|
+
params: [mockAddress],
|
|
474
|
+
});
|
|
475
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
476
|
+
method: "sim_updateValidator",
|
|
477
|
+
params: [
|
|
478
|
+
"mocked_address",
|
|
479
|
+
"100",
|
|
480
|
+
"Provider2",
|
|
481
|
+
"Model2",
|
|
482
|
+
{ max_tokens: 500 },
|
|
483
|
+
],
|
|
484
|
+
});
|
|
485
|
+
expect(console.log).toHaveBeenCalledWith("Validator successfully updated:", mockResponse.result);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
test("should fetch and update a validator with new config", async () => {
|
|
489
|
+
const mockAddress = "mocked_address";
|
|
490
|
+
const mockCurrentValidator = {
|
|
491
|
+
result: {
|
|
492
|
+
address: "mocked_address",
|
|
493
|
+
stake: "100",
|
|
494
|
+
provider: "Provider1",
|
|
495
|
+
model: "Model1",
|
|
496
|
+
config: { max_tokens: 500 },
|
|
497
|
+
},
|
|
498
|
+
};
|
|
499
|
+
const mockResponse = { result: { success: true } };
|
|
500
|
+
|
|
501
|
+
vi.mocked(rpcClient.request)
|
|
502
|
+
.mockResolvedValueOnce(mockCurrentValidator)
|
|
503
|
+
.mockResolvedValueOnce(mockResponse);
|
|
504
|
+
|
|
505
|
+
console.log = vi.fn();
|
|
506
|
+
|
|
507
|
+
const newConfig = '{"max_tokens":1000}';
|
|
508
|
+
await validatorsAction.updateValidator({ address: mockAddress, config: newConfig });
|
|
509
|
+
|
|
510
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
511
|
+
method: "sim_getValidator",
|
|
512
|
+
params: [mockAddress],
|
|
513
|
+
});
|
|
514
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
515
|
+
method: "sim_updateValidator",
|
|
516
|
+
params: [
|
|
517
|
+
"mocked_address",
|
|
518
|
+
"100",
|
|
519
|
+
"Provider1",
|
|
520
|
+
"Model1",
|
|
521
|
+
{ max_tokens: 1000 },
|
|
522
|
+
],
|
|
523
|
+
});
|
|
524
|
+
expect(console.log).toHaveBeenCalledWith("Validator successfully updated:", mockResponse.result);
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
test("should throw an error if validator is not found", async () => {
|
|
528
|
+
const mockAddress = "mocked_address";
|
|
529
|
+
const mockResponse = { result: null };
|
|
530
|
+
|
|
531
|
+
vi.mocked(rpcClient.request).mockResolvedValue(mockResponse);
|
|
532
|
+
|
|
533
|
+
console.error = vi.fn();
|
|
534
|
+
|
|
535
|
+
await validatorsAction.updateValidator({ address: mockAddress });
|
|
536
|
+
|
|
537
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
538
|
+
method: "sim_getValidator",
|
|
539
|
+
params: [mockAddress],
|
|
540
|
+
});
|
|
541
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
542
|
+
"Error updating validator:",
|
|
543
|
+
new Error(`Validator with address ${mockAddress} not found.`)
|
|
544
|
+
);
|
|
545
|
+
expect(rpcClient.request).toHaveBeenCalledTimes(1);
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
test("should log an error if updateValidator RPC call fails", async () => {
|
|
549
|
+
const mockAddress = "mocked_address";
|
|
550
|
+
const mockCurrentValidator = {
|
|
551
|
+
result: {
|
|
552
|
+
address: "mocked_address",
|
|
553
|
+
stake: "100",
|
|
554
|
+
provider: "Provider1",
|
|
555
|
+
model: "Model1",
|
|
556
|
+
config: { max_tokens: 500 },
|
|
557
|
+
},
|
|
558
|
+
};
|
|
559
|
+
const mockError = new Error("RPC failure");
|
|
560
|
+
|
|
561
|
+
vi.mocked(rpcClient.request)
|
|
562
|
+
.mockResolvedValueOnce(mockCurrentValidator)
|
|
563
|
+
.mockRejectedValueOnce(mockError);
|
|
564
|
+
|
|
565
|
+
console.error = vi.fn();
|
|
566
|
+
|
|
567
|
+
await validatorsAction.updateValidator({ address: mockAddress, stake: "200" });
|
|
568
|
+
|
|
569
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
570
|
+
method: "sim_getValidator",
|
|
571
|
+
params: [mockAddress],
|
|
572
|
+
});
|
|
573
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
574
|
+
method: "sim_updateValidator",
|
|
575
|
+
params: [
|
|
576
|
+
"mocked_address",
|
|
577
|
+
"200",
|
|
578
|
+
"Provider1",
|
|
579
|
+
"Model1",
|
|
580
|
+
{ max_tokens: 500 },
|
|
581
|
+
],
|
|
582
|
+
});
|
|
583
|
+
expect(console.error).toHaveBeenCalledWith("Error updating validator:", mockError);
|
|
584
|
+
});
|
|
585
|
+
});
|
|
586
|
+
test("should log an error for invalid stake value", async () => {
|
|
587
|
+
const mockAddress = "mocked_address";
|
|
588
|
+
const mockCurrentValidator = {
|
|
589
|
+
result: {
|
|
590
|
+
address: "mocked_address",
|
|
591
|
+
stake: 100,
|
|
592
|
+
provider: "Provider1",
|
|
593
|
+
model: "Model1",
|
|
594
|
+
config: { max_tokens: 500 },
|
|
595
|
+
},
|
|
596
|
+
};
|
|
597
|
+
|
|
598
|
+
vi.mocked(rpcClient.request).mockResolvedValue(mockCurrentValidator);
|
|
599
|
+
|
|
600
|
+
console.error = vi.fn();
|
|
601
|
+
|
|
602
|
+
await validatorsAction.updateValidator({ address: mockAddress, stake: "-10" });
|
|
603
|
+
|
|
604
|
+
expect(rpcClient.request).toHaveBeenCalledWith({
|
|
605
|
+
method: "sim_getValidator",
|
|
606
|
+
params: [mockAddress],
|
|
607
|
+
});
|
|
608
|
+
expect(console.error).toHaveBeenCalledWith("Invalid stake value. Stake must be a positive integer.");
|
|
609
|
+
expect(rpcClient.request).toHaveBeenCalledTimes(1);
|
|
610
|
+
});
|
|
611
|
+
test("should log an error if model is provided without provider", async () => {
|
|
612
|
+
console.error = vi.fn();
|
|
613
|
+
|
|
614
|
+
await validatorsAction.createValidator({ stake: "10", model: "Model1" });
|
|
615
|
+
|
|
616
|
+
expect(console.error).toHaveBeenCalledWith("You must specify a provider if using a model.");
|
|
617
|
+
expect(rpcClient.request).not.toHaveBeenCalled();
|
|
618
|
+
});
|
|
619
|
+
});
|