Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit fb149cc

Browse files
committed
fix: refactor tests
1 parent 8f246d4 commit fb149cc

File tree

4 files changed

+125
-95
lines changed

4 files changed

+125
-95
lines changed

src/name/publish.js

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ module.exports = (createCommon, options) => {
5353
})
5454
})
5555

56+
it('should publish correctly with the lifetime option and resolve', async () => {
57+
const [{ path }] = await ipfs.add(Buffer.from('should publish correctly with the lifetime option and resolve'))
58+
await ipfs.name.publish(path, { 'allow-offline': true, resolve: false, lifetime: '2h' })
59+
60+
return expect(ipfs.name.resolve(`/ipns/${nodeId}`)).to.become(`/ipfs/${path}`)
61+
})
62+
5663
it('should publish correctly when the file was not added but resolve is disabled', function (done) {
5764
this.timeout(50 * 1000)
5865

src/name/resolve.js

+97-94
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@
22
/* eslint-env mocha */
33
'use strict'
44

5-
const hat = require('hat')
6-
7-
const { fixture } = require('./utils')
85
const { spawnNodeWithId } = require('../utils/spawn')
96
const { getDescribe, getIt, expect } = require('../utils/mocha')
7+
const delay = require('../utils/delay')
108

119
module.exports = (createCommon, options) => {
1210
const describe = getDescribe(options)
1311
const it = getIt(options)
14-
const common = createCommon()
1512

1613
describe('.name.resolve offline', function () {
17-
const keyName = hat()
14+
const common = createCommon()
1815
let ipfs
1916
let nodeId
20-
let keyId
2117

2218
before(function (done) {
2319
common.setup((err, factory) => {
@@ -28,136 +24,143 @@ module.exports = (createCommon, options) => {
2824

2925
ipfs = node
3026
nodeId = node.peerId.id
31-
32-
ipfs.add(fixture.data, { pin: false }, done)
27+
done()
3328
})
3429
})
3530
})
3631

3732
after((done) => common.teardown(done))
3833

39-
it('should resolve a record with the default params after a publish', function (done) {
40-
const value = fixture.cid
34+
// This test should be changed to the recursive logic when recursive === true is the default
35+
it('should resolve a record default options', async () => {
36+
const [{ path }] = await ipfs.add(Buffer.from('should resolve a record default options'))
37+
await ipfs.name.publish(path, { 'allow-offline': true })
38+
return expect(ipfs.name.resolve(`/ipns/${nodeId}`))
39+
.to.become(`/ipfs/${path}`)
40+
})
4141

42-
ipfs.name.publish(value, { 'allow-offline': true }, (err, res) => {
43-
expect(err).to.not.exist()
44-
expect(res).to.exist()
42+
it('should resolve a record recursive === false', async () => {
43+
const [{ path }] = await ipfs.add(Buffer.from('should resolve a record recursive === false'))
44+
await ipfs.name.publish(path, { 'allow-offline': true })
45+
return expect(ipfs.name.resolve(`/ipns/${nodeId}`, { recursive: false }))
46+
.to.become(`/ipfs/${path}`)
47+
})
4548

46-
ipfs.name.resolve(nodeId, (err, res) => {
47-
expect(err).to.not.exist()
48-
expect(res).to.equal(`/ipfs/${value}`)
49+
it('should resolve a record recursive === true', async () => {
50+
const [{ path }] = await ipfs.add(Buffer.from('should resolve a record recursive === true'))
4951

50-
done()
51-
})
52-
})
52+
const { id: keyId } = await ipfs.key.gen('key-name', { type: 'rsa', size: 2048 })
53+
54+
await ipfs.name.publish(path, { 'allow-offline': true })
55+
await ipfs.name.publish(`/ipns/${nodeId}`, { 'allow-offline': true, key: 'key-name' })
56+
57+
return expect(ipfs.name.resolve(`/ipns/${keyId}`, { recursive: true }))
58+
.to.become(`/ipfs/${path}`)
5359
})
5460

55-
it('should not get the entry if its validity time expired', function (done) {
61+
// This test should be changed to the recursive logic when recursive === true is the default
62+
it('should resolve a record default options with remainder', async () => {
63+
const [{ path }] = await ipfs.add(Buffer.from('should resolve a record with the default params with remainder'))
64+
await ipfs.name.publish(path, { 'allow-offline': true })
65+
return expect(ipfs.name.resolve(`/ipns/${nodeId}/remainder/file.txt`))
66+
.to.become(`/ipfs/${path}/remainder/file.txt`)
67+
})
68+
69+
it('should resolve a record recursive === false with remainder', async () => {
70+
const [{ path }] = await ipfs.add(Buffer.from('should resolve a record recursive = false with remainder'))
71+
await ipfs.name.publish(path, { 'allow-offline': true })
72+
return expect(ipfs.name.resolve(`/ipns/${nodeId}/remainder/file.txt`, { recursive: false }))
73+
.to.become(`/ipfs/${path}/remainder/file.txt`)
74+
})
75+
76+
it('should resolve a record recursive === true with remainder', async () => {
77+
const [{ path }] = await ipfs.add(Buffer.from('should resolve a record recursive = true with remainder'))
78+
79+
const { id: keyId } = await ipfs.key.gen('key-name-remainder', { type: 'rsa', size: 2048 })
80+
81+
await ipfs.name.publish(path, { 'allow-offline': true })
82+
await ipfs.name.publish(`/ipns/${nodeId}`, { 'allow-offline': true, key: 'key-name-remainder' })
83+
84+
return expect(ipfs.name.resolve(`/ipns/${keyId}/remainder/file.txt`, { recursive: true }))
85+
.to.be.eventually.equal(`/ipfs/${path}/remainder/file.txt`)
86+
})
87+
88+
it('should not get the entry if its validity time expired', async () => {
5689
const publishOptions = {
57-
resolve: true,
5890
lifetime: '100ms',
5991
ttl: '10s',
60-
key: 'self',
6192
'allow-offline': true
6293
}
6394

64-
ipfs.add(Buffer.from('should not get the entry if its validity time expired'), (err, r) => {
65-
expect(err).to.not.exist()
66-
ipfs.name.publish(r[0].path, publishOptions, (err, res) => {
67-
expect(err).to.not.exist()
68-
expect(res).to.exist()
69-
70-
// guarantee that the record has an expired validity.
71-
setTimeout(function () {
72-
ipfs.name.resolve(nodeId, (err, res) => {
73-
// go only has 1 possible error https://github.com/ipfs/go-ipfs/blob/master/namesys/interface.go#L51
74-
// so here we just expect an Error and don't match the error type to expiration
75-
expect(err).to.exist()
76-
77-
done()
78-
})
79-
}, 500)
80-
})
81-
})
95+
// we add new data instead of re-using fixture to make sure lifetime handling doesn't break
96+
const [{ path }] = await ipfs.add(Buffer.from('should not get the entry if its validity time expired'))
97+
await ipfs.name.publish(path, publishOptions)
98+
await delay(500)
99+
// go only has 1 possible error https://github.com/ipfs/go-ipfs/blob/master/namesys/interface.go#L51
100+
// so here we just expect an Error and don't match the error type to expiration
101+
return expect(ipfs.name.resolve(nodeId)).to.be.rejected()
82102
})
103+
})
83104

84-
it('should recursively resolve to an IPFS hash', function (done) {
85-
const value = fixture.cid
86-
const publishOptions = {
87-
resolve: false,
88-
lifetime: '24h',
89-
ttl: '10s',
90-
key: 'self',
91-
'allow-offline': true
92-
}
105+
describe('.name.resolve dns', function () {
106+
const common = createCommon()
107+
let ipfs
108+
this.retries(3)
93109

94-
// Generate new key
95-
ipfs.key.gen(keyName, { type: 'rsa', size: 2048 }, (err, key) => {
110+
before(function (done) {
111+
common.setup((err, factory) => {
96112
expect(err).to.not.exist()
97113

98-
keyId = key.id
99-
100-
// publish ipfs
101-
ipfs.name.publish(value, publishOptions, (err, res) => {
114+
spawnNodeWithId(factory, (err, node) => {
102115
expect(err).to.not.exist()
103-
expect(res).to.exist()
104-
105-
publishOptions.key = keyName
106-
107-
// publish ipns with the generated key
108-
ipfs.name.publish(`/ipns/${nodeId}`, publishOptions, (err, res) => {
109-
expect(err).to.not.exist()
110-
expect(res).to.exist()
111116

112-
const resolveOptions = {
113-
nocache: false,
114-
recursive: true
115-
}
116-
117-
// recursive resolve (will get ipns first, and will resolve again to find the ipfs)
118-
ipfs.name.resolve(keyId, resolveOptions, (err, res) => {
119-
expect(err).to.not.exist()
120-
expect(res).to.exist()
121-
expect(res).to.equal(`/ipfs/${value}`)
122-
123-
done()
124-
})
125-
})
117+
ipfs = node
118+
done()
126119
})
127120
})
128121
})
129122

130-
it('should resolve /ipns/<hash>/remainder/file.txt', async () => {
131-
const add = await ipfs.add(Buffer.from('fake remainder'))
132-
await ipfs.name.publish(add[0].path, { 'allow-offline': true })
123+
after((done) => common.teardown(done))
133124

134-
const resolve = await ipfs.name.resolve(`/ipns/${nodeId}/remainder/file.txt`)
125+
// This test should be changed to the recursive logic when recursive === true is the default
126+
it('should resolve /ipns/ipfs.io', async () => {
127+
return expect(ipfs.name.resolve('/ipns/ipfs.io'))
128+
.to.eventually.match(/\/ipns\/.+$/)
129+
})
135130

136-
return expect(resolve).to.match(/\/ipfs\/.+\/remainder\/file.txt$/)
131+
it('should resolve /ipns/ipfs.io recursive === false', async () => {
132+
return expect(ipfs.name.resolve('/ipns/ipfs.io', { recursive: false }))
133+
.to.eventually.match(/\/ipns\/.+$/)
137134
})
138135

139-
it('should resolve ipfs.io', async () => {
140-
const r = await ipfs.name.resolve('ipfs.io', { recursive: false })
141-
return expect(r).to.match(/\/ipns\/.+$/)
136+
it('should resolve /ipns/ipfs.io recursive === true', async () => {
137+
return expect(ipfs.name.resolve('/ipns/ipfs.io', { recursive: true }))
138+
.to.eventually.match(/\/ipfs\/.+$/)
142139
})
143140

144-
it('should resolve ipfs.io with remainder', async () => {
145-
const r = await ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg', { recursive: false })
146-
return expect(r).to.match(/\/ipns\/.+\/images\/ipfs-logo.svg$/)
141+
// This test should be changed to the recursive logic when recursive === true is the default
142+
it('should resolve /ipns/ipfs.io with remainder', async () => {
143+
return expect(ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg'))
144+
.to.eventually.match(/\/ipns\/.+\/images\/ipfs-logo.svg$/)
147145
})
148146

149-
it('should resolve /ipns/ipfs.io recursive', async () => {
150-
const r = await ipfs.name.resolve('ipfs.io', { recursive: true })
151-
return expect(r).to.match(/\/ipfs\/.+$/)
147+
it('should resolve /ipns/ipfs.io with remainder recursive === false', async () => {
148+
return expect(ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg', { recursive: false }))
149+
.to.eventually.match(/\/ipns\/.+\/images\/ipfs-logo.svg$/)
152150
})
153151

154-
it('should resolve /ipns/ipfs.io recursive with remainder', async () => {
155-
const r = await ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg', { recursive: true })
156-
return expect(r).to.match(/\/ipfs\/.+\/images\/ipfs-logo.svg$/)
152+
it('should resolve /ipns/ipfs.io with remainder recursive === true', async () => {
153+
return expect(ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg', { recursive: true }))
154+
.to.eventually.match(/\/ipfs\/.+\/images\/ipfs-logo.svg$/)
157155
})
158156

159157
it('should fail to resolve /ipns/ipfs.a', async () => {
160158
return expect(ipfs.name.resolve('ipfs.a')).to.be.rejected()
161159
})
160+
161+
it('should resolve ipns path with hamt-shard recursive === true', async () => {
162+
return expect(ipfs.name.resolve('/ipns/tr.wikipedia-on-ipfs.org/wiki/Anasayfa.html', { recursive: true }))
163+
.to.eventually.match(/\/ipfs\/.+$/)
164+
})
162165
})
163166
}

src/utils/delay.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
/**
4+
* Promise version of setTimeout
5+
* @example
6+
* ```js
7+
* async function something() {
8+
* console.log("this might take some time....");
9+
* await delay(5000);
10+
* console.log("done!")
11+
* }
12+
*
13+
* something();
14+
* ```
15+
* @param {number} ms
16+
* @return {Promise}
17+
*/
18+
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
19+
20+
module.exports = delay

src/utils/mocha.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const chai = require('chai')
55
const dirtyChai = require('dirty-chai')
66
const chaiAsPromised = require('chai-as-promised')
77

8-
chai.use(dirtyChai)
98
chai.use(chaiAsPromised)
9+
chai.use(dirtyChai)
1010

1111
module.exports.expect = chai.expect
1212

0 commit comments

Comments
 (0)