1 |
|
2 | var pull = require('pull-stream')
|
3 | var tape = require('tape')
|
4 | var Muxrpc = require('../')
|
5 |
|
6 | var manifest = { hello: 'sync' }
|
7 | var api = {
|
8 | hello: function (n) {
|
9 | if(this._emit) this._emit('hello', n)
|
10 | console.log('hello from ' + this.id)
|
11 | return n + ':' + this.id
|
12 | }
|
13 | }
|
14 |
|
15 | tape('give a muxrpc instance an id', function (t) {
|
16 |
|
17 | var bob = Muxrpc(null, manifest) (api)
|
18 | var alice = Muxrpc(manifest, null) ()
|
19 | var as = alice.createStream()
|
20 | pull(as, bob.createStream(), as)
|
21 |
|
22 | bob.id = 'Alice'
|
23 |
|
24 | alice.hello('bob', function (err, data) {
|
25 | t.notOk(err)
|
26 | t.equal(data, 'bob:Alice')
|
27 | t.end()
|
28 | })
|
29 | })
|
30 |
|
31 |
|
32 | tape('initialize muxrpc with an id', function (t) {
|
33 |
|
34 | var bob = Muxrpc(null, manifest) (api, null, 'Alice')
|
35 | var alice = Muxrpc(manifest, null) ()
|
36 | var as = alice.createStream()
|
37 | pull(as, bob.createStream(), as)
|
38 |
|
39 | alice.hello('bob', function (err, data) {
|
40 | t.notOk(err)
|
41 | t.equal(data, 'bob:Alice')
|
42 | t.end()
|
43 | })
|
44 | })
|
45 |
|
46 |
|
47 | tape('emit an event from the called api function', function (t) {
|
48 |
|
49 | t.plan(3)
|
50 |
|
51 | var bob = Muxrpc(null, manifest) (api)
|
52 | var alice = Muxrpc(manifest, null) ()
|
53 | var as = alice.createStream()
|
54 | pull(as, bob.createStream(), as)
|
55 |
|
56 | bob.id = 'Alice'
|
57 |
|
58 | bob.on('hello', function (n) {
|
59 | console.log('HELLO')
|
60 | t.equal(n, 'bob')
|
61 | })
|
62 | alice.hello('bob', function (err, data) {
|
63 | t.notOk(err)
|
64 | t.equal(data, 'bob:Alice')
|
65 | t.end()
|
66 | })
|
67 | })
|