UNPKG

14.8 kBJavaScriptView Raw
1function expectFetch(...urls) {
2 expect(fetch.mock.calls.length).toEqual(urls.length);
3 urls.forEach((url, i) => {
4 expect(fetch.mock.calls[i][0]).toEqual(url);
5 });
6}
7
8function mock(body, status = 200) {
9 return [
10 JSON.stringify(body),
11 {
12 status,
13 },
14 ];
15}
16
17function mockAbi() {
18 return `{
19 "version": "eosio::abi/1.0",
20 "types": [],
21 "structs": [{
22 "name": "args",
23 "base": "",
24 "fields": [{
25 "name": "name",
26 "type": "string"
27 },{
28 "name": "value",
29 "type": "string"
30 }
31 ]
32 },{
33 "name": "endpoint_url",
34 "base": "",
35 "fields": [{
36 "name": "base_right",
37 "type": "string"
38 },{
39 "name": "url",
40 "type": "string"
41 },{
42 "name": "method",
43 "type": "string"
44 },{
45 "name": "matches_params",
46 "type": "args[]"
47 },{
48 "name": "token_life_span",
49 "type": "uint64"
50 },{
51 "name": "is_default",
52 "type": "bool"
53 }
54 ]
55 },{
56 "name": "right_reg",
57 "base": "",
58 "fields": [{
59 "name": "id",
60 "type": "uint64"
61 },{
62 "name": "right_name",
63 "type": "string"
64 },{
65 "name": "owner",
66 "type": "name"
67 },{
68 "name": "urls",
69 "type": "endpoint_url[]"
70 },{
71 "name": "issuer_whitelist",
72 "type": "name[]"
73 }
74 ]
75 },{
76 "name": "param_type",
77 "base": "",
78 "fields": [{
79 "name": "type",
80 "type": "string"
81 },{
82 "name": "values",
83 "type": "args[]"
84 }
85 ]
86 },{
87 "name": "params",
88 "base": "",
89 "fields": [{
90 "name": "params",
91 "type": "args[]"
92 }
93 ]
94 },{
95 "name": "right",
96 "base": "",
97 "fields": [{
98 "name": "right_name",
99 "type": "string"
100 },{
101 "name": "description",
102 "type": "string"
103 },{
104 "name": "price_in_cpu",
105 "type": "string"
106 },{
107 "name": "additional_url_params",
108 "type": "params[]"
109 }
110 ]
111 },{
112 "name": "instrument_data",
113 "base": "",
114 "fields": [{
115 "name": "issuer",
116 "type": "name"
117 },{
118 "name": "instrument_class",
119 "type": "string"
120 },{
121 "name": "description",
122 "type": "string"
123 },{
124 "name": "instrument_template",
125 "type": "string"
126 },{
127 "name": "security_type",
128 "type": "string"
129 },{
130 "name": "parameter_rules",
131 "type": "param_type[]"
132 },{
133 "name": "rights",
134 "type": "right[]"
135 },{
136 "name": "parent_instrument_id",
137 "type": "uint64"
138 },{
139 "name": "data",
140 "type": "args[]"
141 },{
142 "name": "mutability",
143 "type": "uint8"
144 }
145 ]
146 },{
147 "name": "token",
148 "base": "",
149 "fields": [{
150 "name": "id",
151 "type": "uint64"
152 },{
153 "name": "owner",
154 "type": "name"
155 },{
156 "name": "minted_by",
157 "type": "name"
158 },{
159 "name": "minted_at",
160 "type": "uint64"
161 },{
162 "name": "instrument",
163 "type": "instrument_data"
164 },{
165 "name": "revoked",
166 "type": "bool"
167 },{
168 "name": "start_time",
169 "type": "uint64"
170 },{
171 "name": "end_time",
172 "type": "uint64"
173 },{
174 "name": "template_hash",
175 "type": "uint64"
176 },{
177 "name": "class_hash",
178 "type": "uint64"
179 }
180 ]
181 },{
182 "name": "accountdata",
183 "base": "",
184 "fields": [{
185 "name": "owner",
186 "type": "name"
187 },{
188 "name": "balance",
189 "type": "uint64"
190 },{
191 "name": "instruments",
192 "type": "uint64[]"
193 }
194 ]
195 },{
196 "name": "account",
197 "base": "",
198 "fields": [{
199 "name": "balance",
200 "type": "asset"
201 }
202 ]
203 },{
204 "name": "currencystat",
205 "base": "",
206 "fields": [{
207 "name": "supply",
208 "type": "asset"
209 },{
210 "name": "max_supply",
211 "type": "asset"
212 },{
213 "name": "issuer",
214 "type": "name"
215 }
216 ]
217 },{
218 "name": "right_param",
219 "base": "",
220 "fields": [{
221 "name": "right_name",
222 "type": "string"
223 },{
224 "name": "urls",
225 "type": "endpoint_url[]"
226 },{
227 "name": "whitelist",
228 "type": "name[]"
229 }
230 ]
231 },{
232 "name": "offer_data",
233 "base": "",
234 "fields": [{
235 "name": "id",
236 "type": "uint64"
237 },{
238 "name": "name",
239 "type": "string"
240 },{
241 "name": "rights",
242 "type": "right_param[]"
243 }
244 ]
245 },{
246 "name": "offer_params",
247 "base": "",
248 "fields": [{
249 "name": "right_name",
250 "type": "string"
251 },{
252 "name": "right_description",
253 "type": "string"
254 },{
255 "name": "right_price_in_cpu",
256 "type": "string"
257 },{
258 "name": "api_name",
259 "type": "string"
260 },{
261 "name": "api_description",
262 "type": "string"
263 },{
264 "name": "api_price_in_cpu",
265 "type": "string"
266 },{
267 "name": "api_payment_model",
268 "type": "string"
269 },{
270 "name": "api_additional_url_params",
271 "type": "string"
272 }
273 ]
274 },{
275 "name": "publishapi",
276 "base": "",
277 "fields": [{
278 "name": "creator",
279 "type": "name"
280 },{
281 "name": "issuer",
282 "type": "name"
283 },{
284 "name": "api_instrument_license_price_in_cpu",
285 "type": "string"
286 },{
287 "name": "api_instrument_lifetime_in_seconds",
288 "type": "string"
289 },{
290 "name": "api_instrument_start_date",
291 "type": "string"
292 },{
293 "name": "api_instrument_end_date",
294 "type": "string"
295 },{
296 "name": "api_instrument_valid_forever",
297 "type": "uint8"
298 },{
299 "name": "api_instrument_mutability",
300 "type": "uint8"
301 },{
302 "name": "api_instrument_security_type",
303 "type": "string"
304 },{
305 "name": "right_params",
306 "type": "offer_params[]"
307 },{
308 "name": "api_instrument_parameter_rules",
309 "type": "param_type[]"
310 },{
311 "name": "offer_mutability",
312 "type": "uint8"
313 },{
314 "name": "offer_security_type",
315 "type": "string"
316 },{
317 "name": "offer_template",
318 "type": "string"
319 },{
320 "name": "offer_start_time",
321 "type": "uint64"
322 },{
323 "name": "offer_end_time",
324 "type": "uint64"
325 },{
326 "name": "offer_override_id",
327 "type": "uint64"
328 }
329 ]
330 },{
331 "name": "licenseapi",
332 "base": "",
333 "fields": [{
334 "name": "creator",
335 "type": "name"
336 },{
337 "name": "buyer",
338 "type": "name"
339 },{
340 "name": "offer_id",
341 "type": "uint64"
342 },{
343 "name": "offer_template",
344 "type": "string"
345 },{
346 "name": "override_instrument_id",
347 "type": "uint64"
348 }
349 ]
350 }
351 ],
352 "actions": [{
353 "name": "publishapi",
354 "type": "publishapi",
355 "ricardian_contract": ""
356 },{
357 "name": "licenseapi",
358 "type": "licenseapi",
359 "ricardian_contract": ""
360 }
361 ],
362 "tables": [{
363 "name": "rights",
364 "index_type": "i64",
365 "key_names": [
366 "id"
367 ],
368 "key_types": [
369 "uint64"
370 ],
371 "type": "right_reg"
372 },{
373 "name": "tokens",
374 "index_type": "i64",
375 "key_names": [
376 "id"
377 ],
378 "key_types": [
379 "uint64"
380 ],
381 "type": "token"
382 },{
383 "name": "account",
384 "index_type": "i64",
385 "key_names": [
386 "owner"
387 ],
388 "key_types": [
389 "name"
390 ],
391 "type": "accountdata"
392 },{
393 "name": "accounts",
394 "index_type": "i64",
395 "key_names": [
396 "balance"
397 ],
398 "key_types": [
399 "asset"
400 ],
401 "type": "account"
402 },{
403 "name": "stat",
404 "index_type": "i64",
405 "key_names": [
406 "supply"
407 ],
408 "key_types": [
409 "asset"
410 ],
411 "type": "currencystat"
412 },{
413 "name": "offersdata",
414 "index_type": "i64",
415 "key_names": [
416 "id"
417 ],
418 "key_types": [
419 "uint64"
420 ],
421 "type": "offer_data"
422 }
423 ],
424 "ricardian_clauses": [],
425 "error_messages": [],
426 "abi_extensions": [],
427 "variants": []
428 }`;
429}
430
431function mockAccount(account = {}) {
432 return mock([{
433 account_name: 'y4dcmrzgiyte',
434 cpu_limit: {
435 available: 12342451,
436 max: 12342451,
437 used: 0,
438 },
439 cpu_weight: 10000,
440 created: '2018-07-27T05:08:50.500',
441 head_block_num: 925723,
442 head_block_time: '2018-08-01T12:42:12.000',
443 last_code_update: '1970-01-01T00:00:00.000',
444 net_limit: {
445 available: 64709992,
446 max: 64709992,
447 used: 0,
448 },
449 net_weight: 10000,
450 permissions: [{
451 parent: '',
452 perm_name: 'owner',
453 required_auth: {
454 accounts: [],
455 keys: [{
456 key: 'EOS7xvHh4RwHJhMYXQE8r3d6AkVpxYUQEiopQi5jM1q1JYRyJZZzX',
457 weight: 1,
458 }],
459 threshold: 1,
460 waits: [],
461 },
462 },{
463 parent: 'owner',
464 perm_name: 'active',
465 required_auth: {
466 accounts: [],
467 keys: [{
468 key: 'EOS5QygD8vsKRXuVR8JMgLPjWwqzUyVGAJyvYaK7ffU4oPDmgwgqX',
469 weight: 1,
470 }],
471 threshold: 1,
472 waits: [],
473 },
474 },{
475 parent: 'active',
476 perm_name: 'custom',
477 required_auth: {
478 accounts: [],
479 keys: [{
480 key: 'EOS5QygD8vsKRXuVR8JMgLPjWwqzUyVGAJyvYaK7ffU4oPDmgwgqX',
481 weight: 1,
482 }],
483 threshold: 1,
484 waits: [],
485 },
486 }],
487 privileged: false,
488 ram_quota: 8150,
489 ram_usage: 2996,
490 refund_request: null,
491 self_delegated_bandwidth: null,
492 total_resources: {
493 cpu_weight: '1.0000 SYS',
494 net_weight: '1.0000 SYS',
495 owner: 'y4dcmrzgiyte',
496 ram_bytes: 8150,
497 },
498 voter_info: null,
499 ...account,
500 }]);
501}
502
503function mockBlock(block = {}) {
504 return mock([{
505 timestamp: '2018-07-30T14:24:24.000',
506 producer: 'eosio',
507 confirmed: 0,
508 previous: '00090a02e194bf83b406638a2165c69abdb6524aab0e0d9323e5788871501af9',
509 transaction_mroot: '0000000000000000000000000000000000000000000000000000000000000000',
510 action_mroot: 'b6ad4c65a79b1b43d223cfcbe3445b40e6fbd308a769fd8db9eed7404ecf2df7',
511 schedule_version: 0,
512 new_producers: null,
513 header_extensions: [],
514 producer_signature: 'SIG_K1_KbGoYqtV83Y7FeJ72sNNvmW7o3AMEMVug9HFPDovYTS6gZGERMdUs8neva44nMHB7qnUeSGn8A6PcuvZ9GR6mStChzMSD5',
515 transactions: [],
516 block_extensions: [],
517 id: '00090a0384aa271b99b94d25a3d069c4387625e972d05c21ffa17180d1f09ec2',
518 block_num: 592387,
519 ref_block_prefix: 625850777,
520 ...block,
521 }]);
522}
523
524function mockCode() {
525 return '';
526}
527
528function mockError(error = {}) {
529 return new Error({
530 code: 500,
531 message: 'Internal Service Error',
532 error: {
533 code: 3100002,
534 name: 'unknown_block_exception',
535 what: 'Unknown block',
536 details: [],
537 ...error,
538 },
539 });
540}
541
542function mockInfo(info = {}) {
543 return mock([{
544 server_version: '75635168',
545 chain_id: 'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f',
546 head_block_num: 591911,
547 last_irreversible_block_num: 591910,
548 last_irreversible_block_id: '00090826ea7ed488caa5bf6b1e3ce25b5bd34f388249cc6893b473bc01c3416f',
549 head_block_id: '00090827d92a0b18d95562ddb45bc213a09a2ab0d1a408e7d00e62e0cc70e69c',
550 head_block_time: '2018-07-30T14:20:26.000',
551 head_block_producer: 'eosio',
552 virtual_block_cpu_limit: 200000000,
553 virtual_block_net_limit: 1048576000,
554 block_cpu_limit: 199900,
555 lock_net_limit: 1048576,
556 server_version_string: "v1.4.2",
557 ...info,
558 }]);
559}
560
561function mockInstrument(instrument = {}) {
562 const innerInstrument = {
563 issuer: 'company.acnt',
564 instrument_class: 'company.apiVoucher',
565 description: 'process an image and returns the list of objects found',
566 instrument_template: '',
567 security_type: 'pass',
568 rights: [{
569 right_name: 'company.manager.licenseApi',
570 description: 'creates an api voucher to access com.company.right',
571 price_in_cpu: '0',
572 additional_url_params: [],
573 }],
574 parent_instrument_id: 1,
575 data: [],
576 mutability: 1,
577 ...instrument.instrument,
578 };
579
580 return {
581 id: 0,
582 owner: 'app.acnt',
583 minted_by: 'app.acnt',
584 minted_at: Math.floor(Date.now() / 1000),
585 revoked: 0,
586 start_time: Math.floor(Date.now() / 1000) - 1,
587 end_time: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30, // Expires in 30 days
588 ...instrument,
589 instrument: innerInstrument,
590 };
591}
592
593function mockInstruments(instruments = [{}]) {
594 let idx = 1;
595 return mock({
596 rows: instruments.map((instrument) => {
597 const instr = mockInstrument({
598 id: idx,
599 ...instrument,
600 });
601 idx += 1;
602 return instr;
603 }),
604 });
605}
606
607function mockTransaction(transaction = {}) {
608 return {
609 transaction_id: '0',
610 ...transaction,
611 };
612}
613
614module.exports = {
615 expectFetch,
616 mock,
617 mockAbi,
618 mockAccount,
619 mockBlock,
620 mockCode,
621 mockError,
622 mockInfo,
623 mockInstrument,
624 mockInstruments,
625 mockTransaction,
626};