Files: 153d7d5e0c516e7c72aadb4a57a7087c0a50ace6 / tests / apiTests.js
1815 bytesRaw
1 | const tape = require('tape') |
2 | const Hypervisor = require('../hypervisor.js') |
3 | const Message = require('primea-message/atomic') |
4 | const Graph = require('ipld-graph-builder') |
5 | const IPFS = require('ipfs') |
6 | |
7 | const ipfs = new IPFS() |
8 | const graph = new Graph(ipfs) |
9 | |
10 | ipfs.on('start', async () => { |
11 | tape('send and reciving messages', async t => { |
12 | const root = {} |
13 | try { |
14 | const hypervisor = new Hypervisor(graph, root) |
15 | const path = 'two/three' |
16 | await hypervisor.set(path, { |
17 | code: message => { |
18 | t.pass('got message') |
19 | t.end() |
20 | return {} |
21 | } |
22 | }) |
23 | hypervisor.send('one', new Message({ |
24 | to: path |
25 | })) |
26 | } catch (e) { |
27 | console.log(e) |
28 | } |
29 | }) |
30 | |
31 | tape('reverts', async t => { |
32 | try { |
33 | const root = {} |
34 | const hypervisor = new Hypervisor(graph, root) |
35 | const path = 'one/two/three' |
36 | const path2 = 'one/two/three/four' |
37 | await hypervisor.set(path, { |
38 | code: async (message, kernel) => { |
39 | console.log('here!!') |
40 | await kernel.send('four', new Message()) |
41 | throw new Error('vm exception') |
42 | } |
43 | }) |
44 | |
45 | await hypervisor.set(path2, { |
46 | code: (message, kernel) => { |
47 | kernel.graph.set(kernel.state, 'something', { |
48 | somevalue: 'value' |
49 | }) |
50 | return 'done!' |
51 | } |
52 | }) |
53 | |
54 | const message = new Message({ |
55 | to: path.split('/').slice(1) |
56 | }) |
57 | hypervisor.send(path.split('/')[0], message) |
58 | const result = await message.result() |
59 | t.equals(result.exception, true) |
60 | const expectedRoot = '{"one":{"two":{"three":{"/":{"four":{"/":{}}}}}}}' |
61 | t.equals(JSON.stringify(root), expectedRoot, 'should produce correct root') |
62 | } catch (e) { |
63 | console.log(e) |
64 | } |
65 | t.end() |
66 | process.exit() |
67 | }) |
68 | }) |
69 |
Built with git-ssb-web