This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpubsub.spec.js
149 lines (115 loc) · 3.66 KB
/
pubsub.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* eslint-env mocha */
import { expect } from 'aegir/utils/chai.js'
import { cli } from './utils/cli.js'
import sinon from 'sinon'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
describe('pubsub', () => {
let ipfs
beforeEach(() => {
ipfs = {
pubsub: {
ls: sinon.stub(),
peers: sinon.stub(),
publish: sinon.stub(),
subscribe: sinon.stub()
}
}
})
describe('ls', () => {
const defaultOptions = {
timeout: undefined
}
it('should list subscriptions', async () => {
const subName = 'sub-name'
ipfs.pubsub.ls.withArgs(defaultOptions).resolves([
subName
])
const out = await cli('pubsub ls', { ipfs })
expect(out).to.equal(`${subName}\n`)
})
it('should list subscriptions with timeout', async () => {
const subName = 'sub-name'
ipfs.pubsub.ls.withArgs({
...defaultOptions,
timeout: 1000
}).resolves([
subName
])
const out = await cli('pubsub ls --timeout=1s', { ipfs })
expect(out).to.equal(`${subName}\n`)
})
it('should strip control characters from sub names', async () => {
const subName = 'sub-name'
const junkSubName = 'sub-nam\b\te\n'
ipfs.pubsub.ls.withArgs(defaultOptions).resolves([
junkSubName
])
const out = await cli('pubsub ls', { ipfs })
expect(out).to.equal(`${subName}\n`)
})
})
describe('peers', () => {
const defaultOptions = {
timeout: undefined
}
it('should list topic peers', async () => {
const subName = 'sub-name'
const peer = 'peer-id'
ipfs.pubsub.peers.withArgs(subName, defaultOptions).resolves([
peer
])
const out = await cli(`pubsub peers ${subName}`, { ipfs })
expect(out).to.equal(`${peer}\n`)
})
it('should list topic peers with a timeout', async () => {
const subName = 'sub-name'
const peer = 'peer-id'
ipfs.pubsub.peers.withArgs(subName, {
...defaultOptions,
timeout: 1000
}).resolves([
peer
])
const out = await cli(`pubsub peers ${subName} --timeout=1s`, { ipfs })
expect(out).to.equal(`${peer}\n`)
})
})
describe('pub', () => {
const defaultOptions = {
timeout: undefined
}
it('should publish message', async () => {
const subName = 'sub-name-1'
const data = 'data\r\nfirst\nZażółć gęślą jaźń😇'
await cli(`pubsub pub ${subName} "${data}"`, { ipfs })
expect(ipfs.pubsub.publish.calledWith(subName, uint8ArrayFromString(data), defaultOptions)).to.be.true()
})
it('should publish message with timeout', async () => {
const subName = 'sub-name-2'
const data = 'data\r\nsecond\nZażółć gęślą jaźń😇'
await cli(`pubsub pub ${subName} "${data}" --timeout=1s`, { ipfs })
expect(ipfs.pubsub.publish.calledWith(subName, uint8ArrayFromString(data), {
...defaultOptions,
timeout: 1000
})).to.be.true()
})
})
describe('sub', () => {
const defaultOptions = {
timeout: undefined
}
it('should subscribe', async () => {
const subName = 'sub\nname'
await cli(`pubsub sub "${subName}"`, { ipfs })
expect(ipfs.pubsub.subscribe.calledWith(subName, sinon.match.func, defaultOptions)).to.be.true()
})
it('should subscribe with a timeout', async () => {
const subName = 'sub-name'
await cli(`pubsub sub "${subName}" --timeout=1s`, { ipfs })
expect(ipfs.pubsub.subscribe.calledWith(subName, sinon.match.func, {
...defaultOptions,
timeout: 1000
})).to.be.true()
})
})
})