git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 337e581214f9ce5a65ecad94f1830a0dc0891124

Files: 337e581214f9ce5a65ecad94f1830a0dc0891124 / tests / wasmContainer.js

1792 bytesRaw
1const tape = require('tape')
2const fs = require('fs')
3const Message = require('../message.js')
4const Hypervisor = require('../')
5const WasmContainer = require('../wasmContainer.js')
6
7const level = require('level-browserify')
8const RadixTree = require('dfinity-radix-tree')
9const db = level('./testdb')
10
11let tester
12
13class TestWasmContainer extends WasmContainer {
14 getInteface (funcRef) {
15 const orginal = super.getInteface(funcRef)
16 return Object.assign(orginal, {
17 test: {
18 check: (a, b) => {
19 tester.equals(a, b)
20 }
21 }
22 })
23 }
24}
25
26tape('basic', async t => {
27 t.plan(2)
28 tester = t
29 const expectedState = {
30 '/': Buffer.from('926de6b7eb39cfa8d7f8a44d1ef191d3bcb765a7', 'hex')
31 }
32
33 const tree = new RadixTree({
34 db: db
35 })
36
37 const wasm = fs.readFileSync('./wasm/reciever.wasm')
38
39 const hypervisor = new Hypervisor(tree)
40 hypervisor.registerContainer(TestWasmContainer)
41
42 const {exports} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
43
44 const message = new Message({
45 funcRef: exports.receive,
46 funcArguments: [5]
47 })
48 hypervisor.send(message)
49
50 const stateRoot = await hypervisor.createStateRoot()
51 t.deepEquals(stateRoot, expectedState, 'expected root!')
52})
53
54// Increment a counter.
55tape('increment', async t => {
56
57 const tree = new RadixTree({
58 db: db
59 })
60
61 const wasm = fs.readFileSync('./wasm/counter.wasm')
62
63 const hypervisor = new Hypervisor(tree)
64 hypervisor.registerContainer(TestWasmContainer)
65
66 const {exports} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
67
68 const message = new Message({
69 funcRef: exports.increment,
70 funcArguments: []
71 })
72 hypervisor.send(message)
73
74 const stateRoot = await hypervisor.createStateRoot()
75 t.end()
76
77 console.log(stateRoot)
78
79})

Built with git-ssb-web