git ssb

0+

wanderer🌟 / js-primea-objects



Tree: ebdf92c319d4593e768f02f5893912cd051b6acb

Files: ebdf92c319d4593e768f02f5893912cd051b6acb / tests / index.js

3373 bytesRaw
1const Buffer = require('safe-buffer').Buffer
2const tape = require('tape')
3const cbor = require('borc')
4const objects = require('../')
5const utils = require('../utils')
6
7tape('system objects', t => {
8 t.equals(objects.getType(), 'invalid')
9 t.equals(objects.getType(true), 'invalid')
10 const msg = new objects.Message({
11 ticks: 100
12 })
13
14 t.equals(objects.getType(msg), 'message')
15 t.equals(msg.ticks, 100)
16 msg.ticks = 10
17 t.equals(msg.ticks, 10)
18
19 const id = new objects.ID(Buffer.from([0x1]))
20 const enid = cbor.encode(id)
21 const rid = objects.decoder.decodeFirst(enid)
22 t.equals(rid.toString(), '01')
23 t.equals(objects.getType(id), 'id')
24
25 const modRef = new objects.ModuleRef({'name': ['i32']}, id)
26 const enmod = cbor.encode(modRef)
27 const rmodRef = objects.decoder.decodeFirst(enmod)
28 t.equals(objects.getType(modRef), 'mod')
29 t.deepEquals(modRef, rmodRef)
30
31 const funcRef = rmodRef.getFuncRef('name')
32 funcRef.gas = 1000
33 const funcRef2 = funcRef.copy()
34 funcRef2.gas = 500
35 t.equals(funcRef.gas, 1000, 'should have correct gas amount')
36 const enFuncRef = cbor.encode(funcRef)
37 const rFuncRef = objects.decoder.decodeFirst(enFuncRef)
38 t.deepEquals(funcRef, rFuncRef)
39 t.equals(objects.getType(rFuncRef), 'func')
40
41 const link = new cbor.Tagged(42, 'data')
42 const enLink = cbor.encode(link)
43 const rLink = objects.decoder.decodeFirst(enLink)
44 t.deepEquals(rLink, {'/': 'data'})
45 t.equals(objects.getType(rLink), 'link')
46
47 t.equals(objects.getType([]), 'elem')
48 t.equals(objects.getType(Buffer.from([])), 'data')
49
50 t.end()
51})
52
53tape('actor IDs', t => {
54 const id0 = { nonce: 0, parent: null }
55 const hashedId0 = objects.generateActorId(id0)
56 t.deepEquals(hashedId0.id, Buffer.from('372a08b828598122fc64c4aa94735c770f25bbbc', 'hex'))
57
58 const id00 = { nonce: 0, parent: hashedId0 }
59 const hashedId00 = objects.generateActorId(id00)
60 t.deepEquals(hashedId00.id, Buffer.from('10d7d4be8663c37d8ea7cff89b7c01c059ebbc80', 'hex'))
61
62 const id01 = { nonce: 1, parent: hashedId0 }
63 const hashedId01 = objects.generateActorId(id01)
64 t.deepEquals(hashedId01.id, Buffer.from('0ca311b75efd27e7daf6eec8b51b5c1fe33ff233', 'hex'))
65
66 t.end()
67})
68
69tape('utils', t => {
70 const id = new objects.ID(Buffer.from([0x1]))
71 const obj = [
72 new objects.ModuleRef({'name': ['i32']}, id),
73 new objects.FunctionRef({
74 identifier: [false, 'main'],
75 actorID: id,
76 params: ['i32'],
77 gas: 100
78 }),
79 Buffer.from([1, 2, 3, 4])
80 ]
81 const jsonFull = utils.toJSON(obj)
82 t.deepEquals(JSON.stringify(jsonFull), '[{"@ModuleRef":{"id":"0x01","exports":{"name":["i32"]}}},{"@FunctionRef":{"actorID":"0x01","private":false,"name":"main","gas":100,"params":["i32"]}},"0x01020304"]')
83
84 const json = utils.toJSON(obj, false)
85 t.deepEquals(JSON.stringify(json), '[{"@ModuleRef":{"id":"0x01"}},{"@FunctionRef":{"actorID":"0x01","private":false,"name":"main","gas":100}},"0x01020304"]')
86
87 const newObj = [
88 new objects.ModuleRef(undefined, id),
89 new objects.FunctionRef({
90 identifier: [false, 'main'],
91 actorID: id,
92 gas: 100
93 }),
94 Buffer.from([1, 2, 3, 4])
95 ]
96 t.deepEquals(utils.fromJSON(json), newObj)
97
98 t.deepEquals(utils.fromJSON(jsonFull), obj)
99
100 const hashedId01 = utils.actorRefToId([0, 1])
101 t.deepEquals(hashedId01.id, Buffer.from('0ca311b75efd27e7daf6eec8b51b5c1fe33ff233', 'hex'))
102
103 t.end()
104})
105

Built with git-ssb-web