2021-07-11 19:33:26 -07:00
|
|
|
/*
|
|
|
|
|
Unit tests for the IPFS Adapter.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-10-22 17:35:10 -07:00
|
|
|
// Global npm libraries
|
2022-09-08 08:58:39 -07:00
|
|
|
import { assert } from 'chai'
|
|
|
|
|
import sinon from 'sinon'
|
2023-10-22 17:35:10 -07:00
|
|
|
import cloneDeep from 'lodash.clonedeep'
|
|
|
|
|
|
|
|
|
|
// Local libraries
|
2022-09-08 08:58:39 -07:00
|
|
|
import IPFSLib from '../../../src/adapters/ipfs/ipfs.js'
|
2023-10-22 17:35:10 -07:00
|
|
|
// import create from '../mocks/ipfs-mock.js'
|
2022-09-08 08:58:39 -07:00
|
|
|
import config from '../../../config/index.js'
|
2023-10-22 17:35:10 -07:00
|
|
|
import createHeliaLib from '../mocks/helia-mock.js'
|
2021-07-11 19:33:26 -07:00
|
|
|
|
2022-03-15 18:58:55 -04:00
|
|
|
// config.isProduction = true;
|
2021-07-13 09:09:42 -07:00
|
|
|
describe('#IPFS-adapter', () => {
|
2021-07-11 19:33:26 -07:00
|
|
|
let uut
|
|
|
|
|
let sandbox
|
2023-10-22 17:35:10 -07:00
|
|
|
let ipfs
|
2021-07-11 19:33:26 -07:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
uut = new IPFSLib()
|
|
|
|
|
|
2023-10-22 17:35:10 -07:00
|
|
|
ipfs = cloneDeep(createHeliaLib)
|
|
|
|
|
|
2021-07-11 19:33:26 -07:00
|
|
|
sandbox = sinon.createSandbox()
|
|
|
|
|
})
|
|
|
|
|
|
2022-03-15 18:58:55 -04:00
|
|
|
afterEach(() => {
|
|
|
|
|
sandbox.restore()
|
|
|
|
|
})
|
|
|
|
|
|
2022-03-18 11:24:04 -07:00
|
|
|
describe('#constructor', () => {
|
2022-03-15 18:58:55 -04:00
|
|
|
it('should instantiate IPFS Lib in dev mode.', async () => {
|
|
|
|
|
const _uut = new IPFSLib()
|
|
|
|
|
assert.exists(_uut)
|
|
|
|
|
assert.isFunction(_uut.start)
|
|
|
|
|
assert.isFunction(_uut.stop)
|
|
|
|
|
})
|
2021-07-11 19:33:26 -07:00
|
|
|
|
2022-03-15 18:58:55 -04:00
|
|
|
it('should instantiate dev IPFS Lib in production mode.', async () => {
|
|
|
|
|
config.isProduction = true
|
|
|
|
|
const _uut = new IPFSLib()
|
|
|
|
|
assert.exists(_uut)
|
|
|
|
|
assert.isFunction(_uut.start)
|
|
|
|
|
assert.isFunction(_uut.stop)
|
|
|
|
|
config.isProduction = false
|
|
|
|
|
})
|
|
|
|
|
})
|
2022-03-18 11:24:04 -07:00
|
|
|
|
2021-07-11 19:33:26 -07:00
|
|
|
describe('#start', () => {
|
|
|
|
|
it('should return a promise that resolves into an instance of IPFS.', async () => {
|
|
|
|
|
// Mock dependencies.
|
2023-10-22 17:35:10 -07:00
|
|
|
sandbox.stub(uut, 'createNode').resolves(ipfs)
|
2021-07-11 19:33:26 -07:00
|
|
|
|
2022-03-15 18:58:55 -04:00
|
|
|
const result = await uut.start()
|
|
|
|
|
// console.log('result: ', result)
|
|
|
|
|
|
2023-10-22 17:35:10 -07:00
|
|
|
// Assert properties of the instance are set.
|
2022-03-15 18:58:55 -04:00
|
|
|
assert.equal(uut.isReady, true)
|
2023-10-22 17:35:10 -07:00
|
|
|
assert.property(uut, 'multiaddrs')
|
|
|
|
|
assert.property(uut, 'id')
|
2022-03-15 18:58:55 -04:00
|
|
|
|
2023-10-22 17:35:10 -07:00
|
|
|
// Output should be an instance of IPFS
|
|
|
|
|
assert.property(result, 'libp2p')
|
2022-03-15 18:58:55 -04:00
|
|
|
})
|
2021-07-11 19:33:26 -07:00
|
|
|
|
|
|
|
|
it('should catch and throw an error', async () => {
|
|
|
|
|
try {
|
|
|
|
|
// Force an error
|
2023-10-22 17:35:10 -07:00
|
|
|
sandbox.stub(uut, 'createNode').rejects(new Error('test error'))
|
2021-07-11 19:33:26 -07:00
|
|
|
|
|
|
|
|
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 15:20:51 -07:00
|
|
|
|
|
|
|
|
describe('#stop', () => {
|
|
|
|
|
it('should stop the IPFS node', async () => {
|
|
|
|
|
// Mock dependencies
|
|
|
|
|
uut.ipfs = {
|
2022-03-08 09:39:54 -08:00
|
|
|
stop: () => {
|
|
|
|
|
}
|
2021-09-30 15:20:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await uut.stop()
|
|
|
|
|
|
|
|
|
|
assert.equal(result, true)
|
|
|
|
|
})
|
|
|
|
|
})
|
2021-12-06 12:46:22 -08:00
|
|
|
|
2022-03-08 09:39:54 -08:00
|
|
|
// describe('#rmBlocksDir', () => {
|
|
|
|
|
// it('should delete the /blocks directory', () => {
|
|
|
|
|
// const result = uut.rmBlocksDir()
|
|
|
|
|
//
|
|
|
|
|
// assert.equal(result, true)
|
|
|
|
|
// })
|
|
|
|
|
//
|
|
|
|
|
// it('should catch and throw an error', () => {
|
|
|
|
|
// try {
|
|
|
|
|
// // Force an error
|
|
|
|
|
// sandbox.stub(uut.fs, 'rmdirSync').throws(new Error('test error'))
|
|
|
|
|
//
|
|
|
|
|
// uut.rmBlocksDir()
|
|
|
|
|
//
|
|
|
|
|
// assert.fail('Unexpected code path')
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// assert.equal(err.message, 'test error')
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
// })
|
2021-07-11 19:33:26 -07:00
|
|
|
})
|