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

[WIP] feat: adds tests for name.publish and name.resolve #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions SPEC/NAME.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ Name API
}
```

`callback` must follow `function (err, name) {}` signature, where `err` is an error if the operation was not successful. `name` is an object that contains the IPNS hash and the IPFS hash, such as:
`callback` must follow `function (err, name) {}` signature, where `err` is an error if the operation was not successful. `name` is an object that contains the IPNS hash and the IPFS hash, such as:

```JavaScript
{
name: "/ipns/QmHash.."
name: "QmHash.."
value: "/ipfs/QmHash.."
}
```
Expand Down
14 changes: 1 addition & 13 deletions js/src/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,7 @@ const waterfall = require('async/waterfall')
const series = require('async/series')
const parallel = require('async/parallel')
const CID = require('cids')

function spawnWithId (factory, callback) {
waterfall([
(cb) => factory.spawnNode(cb),
(node, cb) => node.id((err, peerId) => {
if (err) {
return cb(err)
}
node.peerId = peerId
cb(null, node)
})
], callback)
}
const { spawnWithId } = require('./utils/spawn')

module.exports = (common) => {
describe('.dht', function () {
Expand Down
1 change: 1 addition & 0 deletions js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ exports.key = require('./key')
exports.stats = require('./stats')
exports.repo = require('./repo')
exports.bootstrap = require('./bootstrap')
exports.name = require('./name')
92 changes: 92 additions & 0 deletions js/src/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */

'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const timesSeries = require('async/timesSeries')
const auto = require('async/auto')
const loadFixture = require('aegir/fixtures')
const { spawnWithId } = require('./utils/spawn')

const testFiles = [
loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core'),
loadFixture('js/test/fixtures/test-folder/alice.txt', 'interface-ipfs-core')
]

module.exports = (common) => {
describe.only('.name', function () {
this.timeout(50 * 1000)

let nodes
let files

beforeEach(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)

auto({
factory: cb => common.setup(cb),
// Create two nodes
nodes: ['factory', (res, cb) => {
timesSeries(2, (i, cb) => spawnWithId(res.factory, cb), cb)
}],
// Connect up the two nodes
connect0: ['nodes', (res, cb) => {
res.nodes[0].swarm.connect(res.nodes[1].peerId.addresses[0], cb)
}],
connect1: ['nodes', (res, cb) => {
res.nodes[1].swarm.connect(res.nodes[0].peerId.addresses[0], cb)
}],
// Add files to node 0
files: ['nodes', (res, cb) => res.nodes[0].files.add(testFiles, cb)]
}, (err, res) => {
if (err) return done(err)
nodes = res.nodes
files = res.files
done()
})
})

afterEach((done) => common.teardown(done))

describe('callback API', () => {
it('should publish and resolve', function (done) {
// Publish takes ages on go-ipfs
// https://github.com/ipfs/go-ipfs/issues/4475
this.timeout(5 * 60 * 1000)

auto({
publish: cb => {
nodes[0].name.publish(files[0].hash, (err, res) => {
expect(err).to.not.exist()
expect(res).to.deep.equal({
name: nodes[0].peerId.id,
value: `/ipfs/${files[0].hash}`
})
cb()
})
},
resolve0: ['publish', (_, cb) => {
nodes[0].name.resolve(`/ipns/${nodes[0].peerId.id}`, (err, res) => {
expect(err).to.not.exist()
expect(res).to.equal(`/ipfs/${files[0].hash}`)
cb()
})
}],
resolve1: ['publish', (_, cb) => {
nodes[1].name.resolve(`/ipns/${nodes[0].peerId.id}`, (err, res) => {
expect(err).to.not.exist()
expect(res).to.equal(`/ipfs/${files[0].hash}`)
cb()
})
}]
}, done)
})
})
})
}
14 changes: 1 addition & 13 deletions js/src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const parallel = require('async/parallel')
const whilst = require('async/whilst')
const each = require('async/each')
const hat = require('hat')
const { spawnWithId } = require('./utils/spawn')

// On Browsers it will be false, but the tests currently aren't run
// there anyway
Expand All @@ -36,19 +37,6 @@ function waitForPeers (ipfs, topic, peersToWait, callback) {
}, 500)
}

function spawnWithId (factory, callback) {
waterfall([
(cb) => factory.spawnNode(cb),
(node, cb) => node.id((err, res) => {
if (err) {
return cb(err)
}
node.peerId = res
cb(null, node)
})
], callback)
}

function makeCheck (n, done) {
let i = 0
return (err) => {
Expand Down
16 changes: 16 additions & 0 deletions js/src/utils/spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const waterfall = require('async/waterfall')

function spawnWithId (factory, callback) {
waterfall([
(cb) => factory.spawnNode(cb),
(node, cb) => node.id((err, peerId) => {
if (err) {
return cb(err)
}
node.peerId = peerId
cb(null, node)
})
], callback)
}

exports.spawnWithId = spawnWithId