2021-07-11 19:33:26 -07:00
|
|
|
/*
|
|
|
|
|
Unit tests for the index.js file for the IPFS and ipfs-coord libraries.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-08 08:58:39 -07:00
|
|
|
import { assert } from 'chai'
|
2021-07-11 19:33:26 -07:00
|
|
|
|
2022-09-08 08:58:39 -07:00
|
|
|
import sinon from 'sinon'
|
|
|
|
|
import IPFSLib from '../../../src/adapters/ipfs/index.js'
|
|
|
|
|
// import create from '../mocks/ipfs-mock.js'
|
|
|
|
|
import IPFSCoordMock from '../mocks/ipfs-coord-mock.js'
|
2021-07-11 19:33:26 -07:00
|
|
|
|
2021-07-13 09:09:42 -07:00
|
|
|
describe('#IPFS-adapter-index', () => {
|
2021-07-11 19:33:26 -07:00
|
|
|
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.
|
2022-09-08 08:58:39 -07:00
|
|
|
uut.ipfsAdapter = {
|
|
|
|
|
start: async () => {}
|
|
|
|
|
}
|
2021-07-11 19:33:26 -07:00
|
|
|
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) {
|
2021-07-13 09:09:42 -07:00
|
|
|
// console.log(err)
|
2021-07-11 19:33:26 -07:00
|
|
|
assert.include(err.message, 'test error')
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-09-30 14:47:17 -07:00
|
|
|
|
|
|
|
|
it('should handle lock-file errors', async () => {
|
|
|
|
|
try {
|
|
|
|
|
// Force an error
|
|
|
|
|
sandbox
|
|
|
|
|
.stub(uut.ipfsAdapter, 'start')
|
|
|
|
|
.rejects(new Error('Lock already being held'))
|
|
|
|
|
|
|
|
|
|
// Prevent process from exiting
|
|
|
|
|
sandbox.stub(uut.process, 'exit').returns()
|
|
|
|
|
|
|
|
|
|
await uut.start()
|
|
|
|
|
|
|
|
|
|
assert.fail('Unexpected code path.')
|
|
|
|
|
} catch (err) {
|
|
|
|
|
assert.include(err.message, 'Lock already being held')
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-07-11 19:33:26 -07:00
|
|
|
})
|
|
|
|
|
})
|