Files: 535100729b49473f07a4c560fa6b81aa2b369544 / tests / index.js
2492 bytesRaw
1 | const tape = require('tape') |
2 | const IPFS = require('ipfs') |
3 | const Hypervisor = require('../') |
4 | const Message = require('primea-message') |
5 | |
6 | const node = new IPFS() |
7 | |
8 | class BaseContainer { |
9 | constructor (kernel) { |
10 | this.kernel = kernel |
11 | } |
12 | |
13 | static createState (code) { |
14 | return { |
15 | nonce: [0], |
16 | ports: {} |
17 | } |
18 | } |
19 | } |
20 | |
21 | node.on('error', err => { |
22 | console.log(err) |
23 | }) |
24 | |
25 | node.on('start', () => { |
26 | tape.only('basic', async t => { |
27 | const message = new Message() |
28 | const expectedState = { |
29 | '/': 'zdpuAn1R5shTypKNBHT8Js2uBnbUcujHfnPNrKbKRNL1AyAt5' |
30 | } |
31 | |
32 | class testVMContainer extends BaseContainer { |
33 | run (m) { |
34 | t.true(m === message, 'should recive a message') |
35 | } |
36 | } |
37 | |
38 | try { |
39 | const hypervisor = new Hypervisor({dag: node.dag}) |
40 | hypervisor.addVM('test', testVMContainer) |
41 | const port = hypervisor.createPort('test') |
42 | |
43 | await hypervisor.send(port, message) |
44 | await hypervisor.createStateRoot(port, Infinity) |
45 | |
46 | t.deepEquals(port, expectedState, 'expected') |
47 | } catch (e) { |
48 | console.log(e) |
49 | } |
50 | t.end() |
51 | }) |
52 | |
53 | tape('one child contract', async t => { |
54 | const message = new Message() |
55 | const expectedState = { |
56 | '/': 'zdpuAwUPELiXpnd66Wum84VRPEsUGB7cUuxUESDMXmpVj6prc' |
57 | } |
58 | |
59 | class testVMContainer2 extends BaseContainer { |
60 | run (m) { |
61 | t.true(m === message, 'should recive a message 2') |
62 | return new Promise((resolve, reject) => { |
63 | setTimeout(() => { |
64 | console.log('resolve!!') |
65 | this.kernel.incrementTicks(1) |
66 | resolve() |
67 | }, 200) |
68 | }) |
69 | } |
70 | } |
71 | |
72 | class testVMContainer extends BaseContainer { |
73 | async run (m) { |
74 | const port = await this.kernel.createPort(this.kernel.ports, 'test2', 'child') |
75 | await this.kernel.send(port, m) |
76 | this.kernel.incrementTicks(1) |
77 | } |
78 | } |
79 | |
80 | try { |
81 | const hypervisor = new Hypervisor({dag: node.dag}) |
82 | hypervisor.addVM('test', testVMContainer) |
83 | hypervisor.addVM('test2', testVMContainer2) |
84 | const port = hypervisor.createPort('test') |
85 | |
86 | await hypervisor.send(port, message) |
87 | await hypervisor.createStateRoot(port, Infinity) |
88 | console.log('create state root') |
89 | |
90 | // await hypervisor.graph.tree(port, Infinity) |
91 | // console.log(JSON.stringify(port, null, 2)) |
92 | // t.deepEquals(port, expectedState, 'expected') |
93 | } catch (e) { |
94 | console.log(e) |
95 | } |
96 | |
97 | t.end() |
98 | node.stop(() => { |
99 | // process.exit() |
100 | }) |
101 | }) |
102 | }) |
103 |
Built with git-ssb-web