This repository has been 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
/
swarm.js
200 lines (162 loc) · 6.05 KB
/
swarm.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'
const { expect } = require('aegir/utils/chai')
const cli = require('./utils/cli')
const sinon = require('sinon')
const { Multiaddr } = require('multiaddr')
describe('swarm', () => {
let ipfs
beforeEach(() => {
ipfs = {
swarm: {
connect: sinon.stub(),
peers: sinon.stub(),
addrs: sinon.stub(),
localAddrs: sinon.stub(),
disconnect: sinon.stub()
}
}
})
describe('connect', () => {
const defaultOptions = {
timeout: undefined
}
it('connect online', async () => {
const multiaddr = new Multiaddr('/ip4/123.123.123.123/tcp/482')
ipfs.swarm.connect.withArgs(multiaddr, defaultOptions).resolves()
const out = await cli(`swarm connect ${multiaddr}`, { ipfs, isDaemon: true })
expect(out).to.equal(`${multiaddr}\n`)
})
it('connect offline', async () => {
const multiaddr = new Multiaddr('/ip4/123.123.123.123/tcp/482')
const out = await cli.fail(`swarm connect ${multiaddr}`, { ipfs, isDaemon: false })
expect(out).to.include('This command must be run in online mode')
expect(ipfs.swarm.connect.called).to.be.false()
})
it('connect with timeout', async () => {
const multiaddr = new Multiaddr('/ip4/123.123.123.123/tcp/482')
ipfs.swarm.connect.withArgs(multiaddr, {
...defaultOptions,
timeout: 1000
}).resolves()
const out = await cli(`swarm connect ${multiaddr} --timeout=1s`, { ipfs, isDaemon: true })
expect(out).to.equal(`${multiaddr}\n`)
})
})
describe('peers', () => {
const defaultOptions = {
timeout: undefined
}
it('peers online', async () => {
ipfs.swarm.peers.withArgs(defaultOptions).resolves([{
peer: 'Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z',
addr: '/ip4/192.0.0.1/tcp/5001'
}, {
addr: '/ip4/192.0.0.2/tcp/5002/p2p/Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5a'
}])
const out = await cli('swarm peers', { ipfs, isDaemon: true })
expect(out).to.equal('/ip4/192.0.0.1/tcp/5001/p2p/Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z\n/ip4/192.0.0.2/tcp/5002/p2p/Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5a\n')
})
it('peers offline', async () => {
const out = await cli.fail('swarm peers', { ipfs, isDaemon: false })
expect(out).to.include('This command must be run in online mode')
expect(ipfs.swarm.peers.called).to.be.false()
})
it('peers with timeout', async () => {
ipfs.swarm.peers.withArgs({
...defaultOptions,
timeout: 1000
}).resolves([{
peer: 'Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z',
addr: '/ip4/192.0.0.1/tcp/5001'
}, {
addr: '/ip4/192.0.0.2/tcp/5002/p2p/Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5a'
}])
const out = await cli('swarm peers --timeout=1s', { ipfs, isDaemon: true })
expect(out).to.equal('/ip4/192.0.0.1/tcp/5001/p2p/Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z\n/ip4/192.0.0.2/tcp/5002/p2p/Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5a\n')
})
})
describe('addrs', () => {
const defaultOptions = {
timeout: undefined
}
it('addrs', async () => {
const peer = 'Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z'
const addr = `/ip4/192.0.0.2/tcp/5002/p2p/${peer}`
ipfs.swarm.addrs.withArgs(defaultOptions).resolves([{
id: peer,
addrs: [
new Multiaddr(addr)
]
}])
const out = await cli('swarm addrs', { ipfs })
expect(out).to.equal(`${peer} (1)\n\t${addr}\n`)
})
it('addrs with timeout', async () => {
const peer = 'Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z'
const addr = `/ip4/192.0.0.2/tcp/5002/p2p/${peer}`
ipfs.swarm.addrs.withArgs({
...defaultOptions,
timeout: 1000
}).resolves([{
id: peer,
addrs: [
new Multiaddr(addr)
]
}])
const out = await cli('swarm addrs --timeout=1s', { ipfs })
expect(out).to.equal(`${peer} (1)\n\t${addr}\n`)
})
describe('local', () => {
it('addrs local', async () => {
const addr = 'addr'
ipfs.swarm.localAddrs.withArgs(defaultOptions).resolves([
addr
])
const out = await cli('swarm addrs local', { ipfs, isDaemon: true })
expect(out).to.equal(`${addr}\n`)
})
it('addrs local offline', async () => {
const out = await cli.fail('swarm addrs local', { ipfs, isDaemon: false })
expect(out).to.include('This command must be run in online mode')
})
it('addrs local with timeout', async () => {
const addr = 'addr'
ipfs.swarm.localAddrs.withArgs({
...defaultOptions,
timeout: 1000
}).resolves([
addr
])
const out = await cli('swarm addrs local --timeout=1s', { ipfs, isDaemon: true })
expect(out).to.equal(`${addr}\n`)
})
})
})
describe('disconnect', () => {
const defaultOptions = {
timeout: undefined
}
it('disconnect online', async () => {
const multiaddr = new Multiaddr('/ip4/123.123.123.123/tcp/482')
ipfs.swarm.disconnect.withArgs(multiaddr, defaultOptions).resolves()
const out = await cli(`swarm disconnect ${multiaddr}`, { ipfs, isDaemon: true })
expect(out).to.equal(`${multiaddr}\n`)
})
it('disconnect offline', async () => {
const multiaddr = new Multiaddr('/ip4/123.123.123.123/tcp/482')
const out = await cli.fail(`swarm disconnect ${multiaddr}`, { ipfs, isDaemon: false })
expect(out).to.include('This command must be run in online mode')
})
it('disconnect with timeout', async () => {
const multiaddr = new Multiaddr('/ip4/123.123.123.123/tcp/482')
ipfs.swarm.disconnect.withArgs(multiaddr, {
...defaultOptions,
timeout: 1000
}).resolves()
const out = await cli(`swarm disconnect ${multiaddr} --timeout=1s`, { ipfs, isDaemon: true })
expect(out).to.equal(`${multiaddr}\n`)
})
})
})