diff --git a/src/adapters/ipfs/index.js b/src/adapters/ipfs/index.js index aeccc2b..1fd7fb8 100644 --- a/src/adapters/ipfs/index.js +++ b/src/adapters/ipfs/index.js @@ -6,9 +6,10 @@ const IpfsAdapter = require('./ipfs') const IpfsCoordAdapter = require('./ipfs-coord') class IPFS { - constructor (localConfig) { + constructor (localConfig = {}) { // Encapsulate dependencies this.ipfsAdapter = new IpfsAdapter() + this.IpfsCoordAdapter = IpfsCoordAdapter this.ipfsCoordAdapter = {} // placeholder @@ -28,11 +29,13 @@ class IPFS { this.ipfs = this.ipfsAdapter.ipfs // Start ipfs-coord - this.ipfsCoordAdapter = new IpfsCoordAdapter({ + this.ipfsCoordAdapter = new this.IpfsCoordAdapter({ ipfs: this.ipfs }) await this.ipfsCoordAdapter.start() console.log('ipfs-coord is ready.') + + return true } catch (err) { console.error('Error in adapters/ipfs/index.js/start()') throw err diff --git a/src/adapters/ipfs/ipfs-coord.js b/src/adapters/ipfs/ipfs-coord.js index 2df22e8..7406afd 100644 --- a/src/adapters/ipfs/ipfs-coord.js +++ b/src/adapters/ipfs/ipfs-coord.js @@ -26,6 +26,7 @@ class IpfsCoordAdapter { // Encapsulate dependencies this.IpfsCoord = IpfsCoord + this.ipfsCoord = {} this.bchjs = new BCHJS() // this.rpc = new JSONRPC() this.config = config diff --git a/test/unit/adapters/ipfs-coord.adapter.unit.js b/test/unit/adapters/ipfs-coord.adapter.unit.js new file mode 100644 index 0000000..137ac45 --- /dev/null +++ b/test/unit/adapters/ipfs-coord.adapter.unit.js @@ -0,0 +1,81 @@ +/* + Unit tests for the IPFS Adapter. +*/ + +const assert = require('chai').assert +const sinon = require('sinon') + +const IPFSCoordAdapter = require('../../../src/adapters/ipfs/ipfs-coord') +const IPFSMock = require('../mocks/ipfs-mock') +const IPFSCoordMock = require('../mocks/ipfs-coord-mock') + +describe('#IPFS', () => { + let uut + let sandbox + + beforeEach(() => { + const ipfs = IPFSMock.create() + uut = new IPFSCoordAdapter({ ipfs }) + + sandbox = sinon.createSandbox() + }) + + afterEach(() => sandbox.restore()) + + describe('#constructor', () => { + it('should throw an error if ipfs instance is not included', () => { + try { + uut = new IPFSCoordAdapter() + + assert.fail('Unexpected code path') + } catch (err) { + assert.include( + err.message, + 'Instance of IPFS must be passed when instantiating ipfs-coord.' + ) + } + }) + }) + + describe('#start', () => { + it('should return a promise that resolves into an instance of IPFS.', async () => { + // Mock dependencies. + uut.IpfsCoord = IPFSCoordMock + + const result = await uut.start() + // console.log('result: ', result) + + assert.equal(result, true) + }) + }) + + describe('#attachRPCRouter', () => { + it('should attached a router output', async () => { + // Mock dependencies + uut.ipfsCoord = { + privateLog: {}, + ipfs: { + orbitdb: { + privateLog: {} + } + } + } + + const router = console.log + + uut.attachRPCRouter(router) + }) + + it('should throw an error if ipfs-coord has not been instantiated', () => { + try { + const router = console.log + + uut.attachRPCRouter(router) + + assert.fail('Unexpected code path') + } catch (err) { + assert.include(err.message, 'Cannot read property') + } + }) + }) +}) diff --git a/test/unit/adapters/ipfs-index.adapter.unit.js b/test/unit/adapters/ipfs-index.adapter.unit.js new file mode 100644 index 0000000..36190f4 --- /dev/null +++ b/test/unit/adapters/ipfs-index.adapter.unit.js @@ -0,0 +1,49 @@ +/* + Unit tests for the index.js file for the IPFS and ipfs-coord libraries. +*/ + +const assert = require('chai').assert +const sinon = require('sinon') + +const IPFSLib = require('../../../src/adapters/ipfs') +const IPFSMock = require('../mocks/ipfs-mock') +const IPFSCoordMock = require('../mocks/ipfs-coord-mock') + +describe('#IPFS', () => { + let uut + let sandbox + + beforeEach(() => { + uut = new IPFSLib() + + sandbox = sinon.createSandbox() + }) + + afterEach(() => sandbox.restore()) + + describe('#start', () => { + it('should return a promise that resolves into an instance of IPFS.', async () => { + // Mock dependencies. + uut.ipfsAdapter = new IPFSMock() + uut.IpfsCoordAdapter = IPFSCoordMock + + const result = await uut.start() + + assert.equal(result, true) + }) + + it('should catch and throw an error', async () => { + try { + // Force an error + sandbox.stub(uut.ipfsAdapter, 'start').rejects(new Error('test error')) + + await uut.start() + + assert.fail('Unexpected code path.') + } catch (err) { + console.log(err) + assert.include(err.message, 'test error') + } + }) + }) +}) diff --git a/test/unit/adapters/ipfs.adapter.unit.js b/test/unit/adapters/ipfs.adapter.unit.js new file mode 100644 index 0000000..0a41200 --- /dev/null +++ b/test/unit/adapters/ipfs.adapter.unit.js @@ -0,0 +1,50 @@ +/* + Unit tests for the IPFS Adapter. +*/ + +const assert = require('chai').assert +const sinon = require('sinon') + +const IPFSLib = require('../../../src/adapters/ipfs/ipfs') +const IPFSMock = require('../mocks/ipfs-mock') + +describe('#IPFS', () => { + let uut + let sandbox + + beforeEach(() => { + uut = new IPFSLib() + + sandbox = sinon.createSandbox() + }) + + afterEach(() => sandbox.restore()) + + describe('#start', () => { + it('should return a promise that resolves into an instance of IPFS.', async () => { + // Mock dependencies. + uut.IPFS = IPFSMock + + const result = await uut.start() + // console.log('result: ', result) + + assert.equal(uut.isReady, true) + + assert.property(result, 'config') + }) + + it('should catch and throw an error', async () => { + try { + // Force an error + sandbox.stub(uut.IPFS, 'create').rejects(new Error('test error')) + + await uut.start() + + assert.fail('Unexpected code path.') + } catch (err) { + console.log(err) + assert.include(err.message, 'test error') + } + }) + }) +}) diff --git a/test/unit/mocks/ipfs-coord-mock.js b/test/unit/mocks/ipfs-coord-mock.js new file mode 100644 index 0000000..b1924a1 --- /dev/null +++ b/test/unit/mocks/ipfs-coord-mock.js @@ -0,0 +1,13 @@ +/* + Mocks for the ipfs-coord library +*/ + +class IPFSCoord { + async isReady () { + return true + } + + async start () {} +} + +module.exports = IPFSCoord diff --git a/test/unit/mocks/ipfs-mock.js b/test/unit/mocks/ipfs-mock.js new file mode 100644 index 0000000..ff45da4 --- /dev/null +++ b/test/unit/mocks/ipfs-mock.js @@ -0,0 +1,31 @@ +/* + Mocks for the js-ipfs +*/ + +class IPFS { + constructor () { + this.ipfs = {} + } + + static create () { + const mockIpfs = new MockIpfsInstance() + + return mockIpfs + } + + async start () {} +} + +class MockIpfsInstance { + constructor () { + this.config = { + profiles: { + apply: () => {} + } + } + } + + stop () {} +} + +module.exports = IPFS