Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 35e6295

Browse files
committed
fix: code review for tests
1 parent fcc6e02 commit 35e6295

File tree

4 files changed

+170
-97
lines changed

4 files changed

+170
-97
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"cids": "~0.5.5",
9191
"class-is": "^1.1.0",
9292
"datastore-core": "~0.4.0",
93-
"datastore-pubsub": "~0.0.2",
93+
"datastore-pubsub": "ipfs/js-datastore-pubsub#feat/encode-record-store-keys",
9494
"debug": "^4.1.0",
9595
"deep-extend": "~0.6.0",
9696
"err-code": "^1.1.2",

src/core/ipns/publisher.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class IpnsPublisher {
6767

6868
let keys
6969
try {
70-
keys = ipns.getIdKeys(peerId.id)
70+
keys = ipns.getIdKeys(peerId.toBytes())
7171
} catch (err) {
7272
log.error(err)
7373
return callback(err)

test/cli/name-pubsub.js

+85-95
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,21 @@ describe('name-pubsub', () => {
8383

8484
// Connect
8585
before(function () {
86-
return ipfsA('swarm', 'connect', bMultiaddr).then((out) => {
87-
expect(out).to.eql(`connect ${bMultiaddr} success\n`)
88-
})
86+
return ipfsA('swarm', 'connect', bMultiaddr)
87+
.then((out) => {
88+
expect(out).to.eql(`connect ${bMultiaddr} success\n`)
89+
})
8990
})
9091

9192
after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done))
9293

9394
describe('pubsub commands', () => {
94-
before(function (done) {
95-
this.timeout(50 * 1000)
96-
done()
97-
})
98-
9995
it('should get enabled state of pubsub', function () {
100-
return ipfsA('name pubsub state').then((res) => {
101-
expect(res).to.exist()
102-
expect(res).to.have.string('enabled') // enabled
103-
})
96+
return ipfsA('name pubsub state')
97+
.then((res) => {
98+
expect(res).to.exist()
99+
expect(res).to.have.string('enabled') // enabled
100+
})
104101
})
105102

106103
it('should subscribe on name resolve', function () {
@@ -110,19 +107,17 @@ describe('name-pubsub', () => {
110107
.catch((err) => {
111108
expect(err).to.exist() // Not available (subscribed)
112109

113-
return Promise.all([
114-
ipfsB('pubsub ls'),
115-
ipfsB('name pubsub subs')
116-
])
117-
.then((res) => {
118-
expect(res).to.exist()
119-
120-
expect(res[0]).to.exist()
121-
expect(res[0]).to.have.string('/ipns/') // have an ipns subscribtion
110+
return ipfsB('pubsub ls')
111+
})
112+
.then((res) => {
113+
expect(res).to.exist()
114+
expect(res).to.have.string('/record/') // have a record ipns subscribtion
122115

123-
expect(res[1]).to.exist()
124-
expect(res[1]).to.have.string(`/ipns/${nodeAId.id}`) // have subscription
125-
})
116+
return ipfsB('name pubsub subs')
117+
})
118+
.then((res) => {
119+
expect(res).to.exist()
120+
expect(res).to.have.string(`/ipns/${nodeAId.id}`) // have subscription
126121
})
127122
})
128123

@@ -135,29 +130,27 @@ describe('name-pubsub', () => {
135130
expect(res).to.have.string('no subscription') // tried to cancel a not yet subscribed id
136131

137132
return ipfsA(`name resolve ${nodeBId.id}`)
138-
.catch((err) => {
139-
expect(err).to.exist() // Not available (subscribed now)
140-
141-
return ipfsA(`name pubsub cancel /ipns/${nodeBId.id}`)
142-
.then((res) => {
143-
expect(res).to.exist()
144-
expect(res).to.have.string('canceled') // canceled now
145-
146-
return Promise.all([
147-
ipfsA('pubsub ls'),
148-
ipfsA('name pubsub subs')
149-
])
150-
.then((res) => {
151-
expect(res).to.exist()
152-
153-
expect(res[0]).to.exist()
154-
expect(res[0]).to.not.have.string('/ipns/') // ipns subscribtion not available
155-
156-
expect(res[1]).to.exist()
157-
expect(res[1]).to.not.have.string(`/ipns/${nodeBId.id}`) // ipns subscribtion not available
158-
})
159-
})
160-
})
133+
})
134+
.catch((err) => {
135+
expect(err).to.exist() // Not available (subscribed now)
136+
137+
return ipfsA(`name pubsub cancel /ipns/${nodeBId.id}`)
138+
})
139+
.then((res) => {
140+
expect(res).to.exist()
141+
expect(res).to.have.string('canceled') // canceled now
142+
143+
return ipfsA('pubsub ls')
144+
})
145+
.then((res) => {
146+
expect(res).to.exist()
147+
expect(res).to.not.have.string('/ipns/') // ipns subscribtion not available
148+
149+
return ipfsA('name pubsub subs')
150+
})
151+
.then((res) => {
152+
expect(res).to.exist()
153+
expect(res).to.not.have.string(`/ipns/${nodeBId.id}`) // ipns subscribtion not available
161154
})
162155
})
163156
})
@@ -167,10 +160,11 @@ describe('name-pubsub', () => {
167160

168161
before(function (done) {
169162
this.timeout(50 * 1000)
170-
ipfsA('add src/init-files/init-docs/readme').then((out) => {
171-
cidAdded = out.split(' ')[1]
172-
done()
173-
})
163+
ipfsA('add src/init-files/init-docs/readme')
164+
.then((out) => {
165+
cidAdded = out.split(' ')[1]
166+
done()
167+
})
174168
})
175169

176170
it('should publish the received record to the subscriber', function () {
@@ -182,30 +176,28 @@ describe('name-pubsub', () => {
182176
expect(res).to.satisfy(checkAll([emptyDirCid])) // Empty dir received (subscribed)
183177

184178
return ipfsA(`name resolve ${nodeBId.id}`)
185-
.catch((err) => {
186-
expect(err).to.exist() // Not available (subscribed now)
187-
188-
return ipfsB(`name publish ${cidAdded}`)
189-
.then((res) => {
190-
// published to IpfsB and published through pubsub to ipfsa
191-
expect(res).to.exist()
192-
expect(res).to.satisfy(checkAll([cidAdded, nodeBId.id]))
193-
194-
return Promise.all([
195-
ipfsB(`name resolve ${nodeBId.id}`),
196-
ipfsA(`name resolve ${nodeBId.id}`)
197-
])
198-
.then((res) => {
199-
expect(res).to.exist()
200-
201-
expect(res[0]).to.exist()
202-
expect(res[0]).to.satisfy(checkAll([cidAdded]))
203-
204-
expect(res[1]).to.exist()
205-
expect(res[1]).to.satisfy(checkAll([cidAdded])) // value propagated to node B
206-
})
207-
})
208-
})
179+
})
180+
.catch((err) => {
181+
expect(err).to.exist() // Not available (subscribed now)
182+
183+
return ipfsB(`name publish ${cidAdded}`)
184+
})
185+
.then((res) => {
186+
// published to IpfsB and published through pubsub to ipfsa
187+
expect(res).to.exist()
188+
expect(res).to.satisfy(checkAll([cidAdded, nodeBId.id]))
189+
190+
return ipfsB(`name resolve ${nodeBId.id}`)
191+
})
192+
.then((res) => {
193+
expect(res).to.exist()
194+
expect(res).to.satisfy(checkAll([cidAdded]))
195+
196+
return ipfsA(`name resolve ${nodeBId.id}`)
197+
})
198+
.then((res) => {
199+
expect(res).to.exist()
200+
expect(res).to.satisfy(checkAll([cidAdded])) // value propagated to node B
209201
})
210202
})
211203
})
@@ -235,30 +227,28 @@ describe('name-pubsub', () => {
235227

236228
after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done))
237229

238-
it('should get disabled state of pubsub', function (done) {
239-
ipfsA('name pubsub state').then((res) => {
240-
expect(res).to.exist()
241-
expect(res).to.have.string('disabled')
242-
243-
done()
244-
})
230+
it('should get disabled state of pubsub', function () {
231+
return ipfsA('name pubsub state')
232+
.then((res) => {
233+
expect(res).to.exist()
234+
expect(res).to.have.string('disabled')
235+
})
245236
})
246237

247-
it('should get error getting the available subscriptions', function (done) {
248-
ipfsA('name pubsub subs').catch((err) => {
249-
expect(err).to.exist() // error as it is disabled
250-
expect(err.toString()).to.have.string('IPNS pubsub subsystem is not enabled')
251-
done()
252-
})
238+
it('should get error getting the available subscriptions', function () {
239+
return ipfsA('name pubsub subs')
240+
.catch((err) => {
241+
expect(err).to.exist() // error as it is disabled
242+
expect(err.toString()).to.have.string('IPNS pubsub subsystem is not enabled')
243+
})
253244
})
254245

255-
it('should get error canceling a subscription', function (done) {
256-
ipfsA('name pubsub cancel /ipns/QmSWxaPcGgf4TDnFEBDWz2JnbHywF14phmY9hNcAeBEK5v').catch((err) => {
257-
expect(err).to.exist() // error as it is disabled
258-
expect(err.toString()).to.have.string('IPNS pubsub subsystem is not enabled')
259-
260-
done()
261-
})
246+
it('should get error canceling a subscription', function () {
247+
return ipfsA('name pubsub cancel /ipns/QmSWxaPcGgf4TDnFEBDWz2JnbHywF14phmY9hNcAeBEK5v')
248+
.catch((err) => {
249+
expect(err).to.exist() // error as it is disabled
250+
expect(err.toString()).to.have.string('IPNS pubsub subsystem is not enabled')
251+
})
262252
})
263253
})
264254
})

test/core/name-pubsub.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* eslint max-nested-callbacks: ["error", 6] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const hat = require('hat')
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
10+
11+
const parallel = require('async/parallel')
12+
13+
const isNode = require('detect-node')
14+
const IPFS = require('../../src')
15+
16+
const DaemonFactory = require('ipfsd-ctl')
17+
const df = DaemonFactory.create({ type: 'proc' })
18+
19+
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'
20+
21+
describe('name-pubsub', function () {
22+
if (!isNode) {
23+
return
24+
}
25+
26+
let nodes
27+
let nodeA
28+
let nodeB
29+
let idA
30+
31+
const createNode = (callback) => {
32+
df.spawn({
33+
exec: IPFS,
34+
args: [`--pass ${hat()}`, '--enable-namesys-pubsub'],
35+
config: { Bootstrap: [] }
36+
}, callback)
37+
}
38+
39+
before(function (done) {
40+
this.timeout(40 * 1000)
41+
42+
parallel([
43+
(cb) => createNode(cb),
44+
(cb) => createNode(cb)
45+
], (err, _nodes) => {
46+
expect(err).to.not.exist()
47+
48+
nodes = _nodes
49+
nodeA = _nodes[0].api
50+
nodeB = _nodes[1].api
51+
52+
parallel([
53+
(cb) => nodeA.id(cb),
54+
(cb) => nodeB.id(cb)
55+
], (err, ids) => {
56+
expect(err).to.not.exist()
57+
58+
idA = ids[0]
59+
nodeA.swarm.connect(ids[1].addresses[0], done)
60+
})
61+
})
62+
})
63+
64+
after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done))
65+
66+
it('should publish and then resolve correctly', function (done) {
67+
nodeB.name.resolve(idA.id, (err) => {
68+
expect(err).to.exist()
69+
70+
nodeA.name.publish(ipfsRef, { resolve: false }, (err, res) => {
71+
expect(err).to.not.exist()
72+
expect(res).to.exist()
73+
74+
nodeB.name.resolve(idA.id, (err, res) => {
75+
expect(err).to.not.exist()
76+
expect(res).to.exist()
77+
expect(res.path).to.equal(ipfsRef)
78+
done()
79+
})
80+
})
81+
})
82+
})
83+
})

0 commit comments

Comments
 (0)