|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chai = require('chai') |
| 5 | +const dirtyChai = require('dirty-chai') |
| 6 | +const expect = chai.expect |
| 7 | +chai.use(dirtyChai) |
| 8 | + |
| 9 | +const series = require('async/series') |
| 10 | +const os = require('os') |
| 11 | +const path = require('path') |
| 12 | +const hat = require('hat') |
| 13 | + |
| 14 | +const DaemonFactory = require('ipfsd-ctl') |
| 15 | + |
| 16 | +const spawnJsDaemon = (dir, callback) => { |
| 17 | + DaemonFactory.create({ type: 'js' }) |
| 18 | + .spawn({ |
| 19 | + repoPath: dir, |
| 20 | + disposable: false, |
| 21 | + initOptions: { bits: 512 }, |
| 22 | + args: ['--offline'] |
| 23 | + }, callback) |
| 24 | +} |
| 25 | + |
| 26 | +const spawnGoDaemon = (dir, callback) => { |
| 27 | + DaemonFactory.create() |
| 28 | + .spawn({ |
| 29 | + repoPath: dir, |
| 30 | + disposable: false, |
| 31 | + initOptions: { bits: 1024 }, |
| 32 | + args: ['--offline'] |
| 33 | + }, callback) |
| 34 | +} |
| 35 | + |
| 36 | +const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU' |
| 37 | + |
| 38 | +const publishAndResolve = (publisherDaemon, resolverDaemon, callback) => { |
| 39 | + let nodeId |
| 40 | + let sameDaemon = false |
| 41 | + |
| 42 | + if (typeof resolverDaemon === 'function') { |
| 43 | + callback = resolverDaemon |
| 44 | + resolverDaemon = publisherDaemon |
| 45 | + sameDaemon = true |
| 46 | + } |
| 47 | + |
| 48 | + const stopPublisherAndStartResolverDaemon = (callback) => { |
| 49 | + series([ |
| 50 | + (cb) => publisherDaemon.stop(cb), |
| 51 | + (cb) => setTimeout(cb, 2000), |
| 52 | + (cb) => resolverDaemon.start(cb) |
| 53 | + ], callback) |
| 54 | + } |
| 55 | + |
| 56 | + series([ |
| 57 | + (cb) => publisherDaemon.init(cb), |
| 58 | + (cb) => publisherDaemon.start(cb), |
| 59 | + (cb) => publisherDaemon.api.id((err, res) => { |
| 60 | + expect(err).to.not.exist() |
| 61 | + nodeId = res.id |
| 62 | + cb() |
| 63 | + }), |
| 64 | + (cb) => publisherDaemon.api.name.publish(ipfsRef, { resolve: false }, cb), |
| 65 | + (cb) => sameDaemon ? cb() : stopPublisherAndStartResolverDaemon(cb), |
| 66 | + (cb) => { |
| 67 | + resolverDaemon.api.name.resolve(nodeId, { local: true }, (err, res) => { |
| 68 | + expect(err).to.not.exist() |
| 69 | + expect(res).to.equal(ipfsRef) |
| 70 | + cb() |
| 71 | + }) |
| 72 | + }, |
| 73 | + (cb) => resolverDaemon.stop(cb), |
| 74 | + (cb) => setTimeout(cb, 2000), |
| 75 | + (cb) => resolverDaemon.cleanup(cb) |
| 76 | + ], callback) |
| 77 | +} |
| 78 | + |
| 79 | +describe('ipns locally using the same repo across implementations', () => { |
| 80 | + it('should publish an ipns record to a js daemon and resolve it using the same js daemon', function (done) { |
| 81 | + this.timeout(120 * 1000) |
| 82 | + const dir = path.join(os.tmpdir(), hat()) |
| 83 | + |
| 84 | + spawnJsDaemon(dir, (err, jsDaemon) => { |
| 85 | + expect(err).to.not.exist() |
| 86 | + publishAndResolve(jsDaemon, done) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should publish an ipns record to a go daemon and resolve it using the same go daemon', function (done) { |
| 91 | + this.timeout(160 * 1000) |
| 92 | + const dir = path.join(os.tmpdir(), hat()) |
| 93 | + |
| 94 | + spawnGoDaemon(dir, (err, goDaemon) => { |
| 95 | + expect(err).to.not.exist() |
| 96 | + publishAndResolve(goDaemon, done) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + 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) { |
| 101 | + this.timeout(120 * 1000) |
| 102 | + const dir = path.join(os.tmpdir(), hat()) |
| 103 | + |
| 104 | + series([ |
| 105 | + (cb) => spawnJsDaemon(dir, cb), |
| 106 | + (cb) => spawnGoDaemon(dir, cb) |
| 107 | + ], (err, daemons) => { |
| 108 | + expect(err).to.not.exist() |
| 109 | + |
| 110 | + publishAndResolve(daemons[0], daemons[1], done) |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + 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) { |
| 115 | + this.timeout(160 * 1000) |
| 116 | + const dir = path.join(os.tmpdir(), hat()) |
| 117 | + |
| 118 | + series([ |
| 119 | + (cb) => spawnGoDaemon(dir, cb), |
| 120 | + (cb) => spawnJsDaemon(dir, cb) |
| 121 | + ], (err, daemons) => { |
| 122 | + expect(err).to.not.exist() |
| 123 | + |
| 124 | + publishAndResolve(daemons[0], daemons[1], done) |
| 125 | + }) |
| 126 | + }) |
| 127 | +}) |
0 commit comments