-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathcontroller.spec.ts
232 lines (195 loc) · 6.69 KB
/
controller.spec.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* eslint-env mocha */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable no-loop-func */
import { expect } from 'aegir/chai'
import * as kubo from 'kubo'
import { create as createKuboRPCClient } from 'kubo-rpc-client'
import merge from 'merge-options'
import { isBrowser, isWebWorker, isNode, isElectronMain } from 'wherearewe'
import { createFactory } from '../src/index.js'
import { repoExists } from '../src/kubo/utils.js'
import type { Factory, KuboOptions, SpawnOptions, KuboNode } from '../src/index.js'
const types: Array<KuboOptions & SpawnOptions> = [{
type: 'kubo'
}, {
type: 'kubo',
remote: true
}]
describe('Node API', function () {
this.timeout(60000)
let factory: Factory<KuboNode>
before(async () => {
factory = createFactory({
type: 'kubo',
test: true,
bin: isNode || isElectronMain ? kubo.path() : undefined,
rpc: createKuboRPCClient,
disposable: true
})
await factory.spawn({ type: 'kubo' })
})
afterEach(async () => {
await factory.clean()
})
describe('init', () => {
describe('should work with defaults', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const node = await factory.spawn(opts)
if (!(isBrowser || isWebWorker)) {
const info = await node.info()
await expect(repoExists(info.repo)).to.eventually.be.true()
}
})
}
})
describe('should work with a initialized repo', () => {
for (const opts of types) {
let repo: string
beforeEach(async () => {
const existingNode = await factory.spawn({
disposable: false
})
const existingNodeInfo = await existingNode.info()
await existingNode.stop()
if (!(isBrowser || isWebWorker)) {
await expect(repoExists(existingNodeInfo.repo)).to.eventually.be.true()
}
repo = existingNodeInfo.repo
})
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const node = await factory.spawn(merge(opts, {
repo,
init: false
}))
if (!(isBrowser || isWebWorker)) {
const info = await node.info()
await expect(repoExists(info.repo)).to.eventually.be.true()
}
})
}
})
describe('should apply config', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const node = await factory.spawn(merge(opts, {
init: {
config: {
Addresses: {
API: '/ip4/127.0.0.1/tcp/1111'
}
}
},
start: true
}))
const config = await node.api.config.get('Addresses.API')
expect(config).to.be.eq('/ip4/127.0.0.1/tcp/1111')
await node.stop()
})
}
})
describe('should return version in info', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const node = await factory.spawn(opts)
const info = await node.info()
expect(info.version).to.be.a('string')
await node.stop()
})
}
})
describe('should return pid in info', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const node = await factory.spawn(opts)
const info = await node.info()
expect(info.pid).to.be.a('number')
await node.stop()
})
}
})
})
describe('start', () => {
describe('should work with defaults', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const ctl = await factory.spawn(opts)
await expect(ctl.api.isOnline()).to.eventually.be.true()
await ctl.stop()
})
}
})
})
describe('cleanup', () => {
describe('should delete the repo', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const node = await factory.spawn(opts)
const info = await node.info()
await node.stop()
await node.cleanup()
if (!(isBrowser || isWebWorker)) {
expect(await repoExists(info.repo)).to.be.false()
}
})
}
})
})
describe('stop', () => {
describe('should stop the node', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const node = await factory.spawn(opts)
await node.stop()
await expect(node.api.isOnline()).to.eventually.be.false()
})
}
})
describe('should not clean with disposable false', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const node = await factory.spawn(merge(opts, {
disposable: false
}))
const info = await node.info()
await node.stop()
if (!(isBrowser || isWebWorker)) {
expect(await repoExists(info.repo)).to.be.true()
}
})
}
})
describe('should clean with disposable true', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const node = await factory.spawn(merge(opts, {
disposable: true
}))
const info = await node.info()
await node.stop()
if (!(isBrowser || isWebWorker)) {
expect(await repoExists(info.repo)).to.be.false()
}
})
}
})
})
describe('info', () => {
describe('should return the node info', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const node = await factory.spawn(opts)
const info = await node.info()
expect(info).to.have.property('version').that.is.a('string')
expect(info).to.have.property('api').that.is.a('string')
expect(info).to.have.property('peerId').that.is.a('string')
expect(info).to.have.property('repo').that.is.a('string')
expect(info).to.have.property('pid').that.is.a('number')
expect(info).to.have.property('multiaddrs').that.is.an('array')
expect(info).to.have.property('gateway').that.is.a('string').that.matches(/http:\/\/127.0.0.1:\d+/)
await node.stop()
})
}
})
})
})