Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
test: add ipns tests (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored and Alan Shaw committed Oct 24, 2018
1 parent b271ea8 commit 1c691e8
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
127 changes: 127 additions & 0 deletions test/ipns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const series = require('async/series')
const os = require('os')
const path = require('path')
const hat = require('hat')

const DaemonFactory = require('ipfsd-ctl')

const spawnJsDaemon = (dir, callback) => {
DaemonFactory.create({ type: 'js' })
.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 512 },
args: ['--offline']
}, callback)
}

const spawnGoDaemon = (dir, callback) => {
DaemonFactory.create()
.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 1024 },
args: ['--offline']
}, callback)
}

const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'

const publishAndResolve = (publisherDaemon, resolverDaemon, callback) => {
let nodeId
let sameDaemon = false

if (typeof resolverDaemon === 'function') {
callback = resolverDaemon
resolverDaemon = publisherDaemon
sameDaemon = true
}

const stopPublisherAndStartResolverDaemon = (callback) => {
series([
(cb) => publisherDaemon.stop(cb),
(cb) => setTimeout(cb, 2000),
(cb) => resolverDaemon.start(cb)
], callback)
}

series([
(cb) => publisherDaemon.init(cb),
(cb) => publisherDaemon.start(cb),
(cb) => publisherDaemon.api.id((err, res) => {
expect(err).to.not.exist()
nodeId = res.id
cb()
}),
(cb) => publisherDaemon.api.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => sameDaemon ? cb() : stopPublisherAndStartResolverDaemon(cb),
(cb) => {
resolverDaemon.api.name.resolve(nodeId, { local: true }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.equal(ipfsRef)
cb()
})
},
(cb) => resolverDaemon.stop(cb),
(cb) => setTimeout(cb, 2000),
(cb) => resolverDaemon.cleanup(cb)
], callback)
}

describe('ipns locally using the same repo across implementations', () => {
it('should publish an ipns record to a js daemon and resolve it using the same js daemon', function (done) {
this.timeout(120 * 1000)
const dir = path.join(os.tmpdir(), hat())

spawnJsDaemon(dir, (err, jsDaemon) => {
expect(err).to.not.exist()
publishAndResolve(jsDaemon, done)
})
})

it('should publish an ipns record to a go daemon and resolve it using the same go daemon', function (done) {
this.timeout(160 * 1000)
const dir = path.join(os.tmpdir(), hat())

spawnGoDaemon(dir, (err, goDaemon) => {
expect(err).to.not.exist()
publishAndResolve(goDaemon, done)
})
})

it('should publish an ipns record to a js daemon and resolve it using a go daemon through the reuse of the same repo', function (done) {
this.timeout(120 * 1000)
const dir = path.join(os.tmpdir(), hat())

series([
(cb) => spawnJsDaemon(dir, cb),
(cb) => spawnGoDaemon(dir, cb)
], (err, daemons) => {
expect(err).to.not.exist()

publishAndResolve(daemons[0], daemons[1], done)
})
})

it('should publish an ipns record to a go daemon and resolve it using a js daemon through the reuse of the same repo', function (done) {
this.timeout(160 * 1000)
const dir = path.join(os.tmpdir(), hat())

series([
(cb) => spawnGoDaemon(dir, cb),
(cb) => spawnJsDaemon(dir, cb)
], (err, daemons) => {
expect(err).to.not.exist()

publishAndResolve(daemons[0], daemons[1], done)
})
})
})
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('./pubsub')
require('./circuit')
require('./repo')
require('./exchange-files')
require('./ipns')
require('./kad-dht')
require('./pin')
require('./files')

0 comments on commit 1c691e8

Please sign in to comment.