2021-07-13 12:34:51 -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')
|
2022-03-25 22:12:11 -04:00
|
|
|
const config = require('../../../config')
|
2021-07-13 12:34:51 -07:00
|
|
|
|
|
|
|
|
describe('#IPFS', () => {
|
|
|
|
|
let uut
|
|
|
|
|
let sandbox
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
const ipfs = IPFSMock.create()
|
2021-07-16 18:51:38 -07:00
|
|
|
const bchjs = {}
|
|
|
|
|
uut = new IPFSCoordAdapter({ ipfs, bchjs })
|
2021-07-13 12:34:51 -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.'
|
2021-07-16 18:51:38 -07:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should throw an error if bchjs instance is not included', () => {
|
|
|
|
|
try {
|
|
|
|
|
const ipfs = IPFSMock.create()
|
|
|
|
|
uut = new IPFSCoordAdapter({ ipfs })
|
|
|
|
|
|
|
|
|
|
assert.fail('Unexpected code path')
|
|
|
|
|
} catch (err) {
|
|
|
|
|
assert.include(
|
|
|
|
|
err.message,
|
|
|
|
|
'Instance of bch-js must be passed when instantiating ipfs-coord.'
|
2021-07-13 12:34:51 -07:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
})
|
2022-03-25 22:12:11 -04:00
|
|
|
it('should return a promise that resolves into an instance of IPFS in production mode', async () => {
|
|
|
|
|
uut.config.isProduction = true
|
|
|
|
|
// Mock dependencies.
|
|
|
|
|
uut.IpfsCoord = IPFSCoordMock
|
|
|
|
|
|
|
|
|
|
const result = await uut.start()
|
|
|
|
|
// console.log('result: ', result)
|
|
|
|
|
assert.equal(result, true)
|
|
|
|
|
config.isProduction = false
|
|
|
|
|
})
|
2021-07-13 12:34:51 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('#attachRPCRouter', () => {
|
2021-08-23 17:06:20 -07:00
|
|
|
it('should attach a router output', async () => {
|
2021-07-13 12:34:51 -07:00
|
|
|
// Mock dependencies
|
|
|
|
|
uut.ipfsCoord = {
|
|
|
|
|
privateLog: {},
|
|
|
|
|
ipfs: {
|
|
|
|
|
orbitdb: {
|
|
|
|
|
privateLog: {}
|
|
|
|
|
}
|
2021-08-23 17:06:20 -07:00
|
|
|
},
|
|
|
|
|
adapters: {
|
2022-03-05 08:24:36 -08:00
|
|
|
pubsub: {
|
|
|
|
|
privateLog: () => {
|
|
|
|
|
}
|
2021-08-23 17:06:20 -07:00
|
|
|
}
|
2021-07-13 12:34:51 -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-13 12:34:51 -07:00
|
|
|
})
|
2022-06-20 07:01:43 -07:00
|
|
|
|
|
|
|
|
describe('#subscribeToChat', () => {
|
|
|
|
|
it('should subscribe to the chat channel', async () => {
|
|
|
|
|
// Mock dependencies
|
|
|
|
|
uut.ipfsCoord = {
|
|
|
|
|
adapters: {
|
|
|
|
|
pubsub: {
|
|
|
|
|
subscribeToPubsubChannel: async () => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await uut.subscribeToChat()
|
|
|
|
|
})
|
|
|
|
|
})
|
2021-07-13 12:34:51 -07:00
|
|
|
})
|