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