2021-07-11 19:33:26 -07:00
|
|
|
/*
|
|
|
|
|
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()
|
2022-03-05 08:24:50 -08:00
|
|
|
uut = new IPFSCoordAdapter({ ipfs })
|
2021-07-11 19:33:26 -07:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
})
|
2021-09-30 15:12:47 -07:00
|
|
|
|
|
|
|
|
it('should get the public IP address if this node is a Circuit Relay', async () => {
|
|
|
|
|
// Mock dependencies.
|
|
|
|
|
uut.IpfsCoord = IPFSCoordMock
|
|
|
|
|
sandbox.stub(uut.publicIp, 'v4').returns('123')
|
|
|
|
|
|
|
|
|
|
// Force Circuit Relay
|
|
|
|
|
uut.config.isCircuitRelay = true
|
|
|
|
|
|
|
|
|
|
const result = await uut.start()
|
|
|
|
|
// console.log('result: ', result)
|
|
|
|
|
|
|
|
|
|
assert.equal(result, true)
|
|
|
|
|
})
|
2021-07-11 19:33:26 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('#attachRPCRouter', () => {
|
|
|
|
|
it('should attached a router output', async () => {
|
|
|
|
|
// Mock dependencies
|
|
|
|
|
uut.ipfsCoord = {
|
|
|
|
|
privateLog: {},
|
|
|
|
|
ipfs: {
|
|
|
|
|
orbitdb: {
|
|
|
|
|
privateLog: {}
|
|
|
|
|
}
|
2021-08-24 10:26:46 -07:00
|
|
|
},
|
|
|
|
|
adapters: {
|
2022-03-05 08:24:36 -08:00
|
|
|
pubsub: {
|
|
|
|
|
privateLog: () => {
|
|
|
|
|
}
|
2021-08-24 10:26:46 -07:00
|
|
|
}
|
2021-07-11 19:33:26 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const router = console.log
|
|
|
|
|
|
|
|
|
|
uut.attachRPCRouter(router)
|
|
|
|
|
})
|
|
|
|
|
|
2021-09-30 15:12:47 -07:00
|
|
|
it('should catch and throw an error', () => {
|
|
|
|
|
try {
|
|
|
|
|
// Force an error
|
|
|
|
|
delete uut.ipfsCoord.adapters
|
|
|
|
|
|
|
|
|
|
const router = console.log
|
|
|
|
|
|
|
|
|
|
uut.attachRPCRouter(router)
|
|
|
|
|
|
|
|
|
|
assert.fail('Unexpected code path')
|
|
|
|
|
} catch (err) {
|
2022-03-08 09:39:54 -08:00
|
|
|
assert.include(err.message, 'Cannot read')
|
2021-09-30 15:12:47 -07:00
|
|
|
}
|
|
|
|
|
})
|
2021-07-11 19:33:26 -07:00
|
|
|
})
|
|
|
|
|
})
|