fix(tests): Back to 100% unit test coverage

This commit is contained in:
Chris Troutner
2024-03-10 07:43:06 -07:00
parent e6efc4f49b
commit 1f3c639100
5 changed files with 2 additions and 81 deletions
-1
View File
@@ -27,7 +27,6 @@
"glob": "7.1.6",
"helia": "2.1.0",
"helia-coord": "1.5.4",
"ipfs-unixfs-exporter": "13.5.0",
"jsonrpc-lite": "2.2.0",
"jsonwebtoken": "8.5.1",
"jwt-bch-lib": "1.3.0",
-1
View File
@@ -42,7 +42,6 @@
"glob": "7.1.6",
"helia": "2.1.0",
"helia-coord": "1.5.4",
"ipfs-unixfs-exporter": "13.5.0",
"jsonrpc-lite": "2.2.0",
"jsonwebtoken": "8.5.1",
"jwt-bch-lib": "1.3.0",
@@ -3,8 +3,6 @@
*/
// Global npm libraries
import { exporter } from 'ipfs-unixfs-exporter'
import fs from 'fs'
// Local libraries
import wlogger from '../../../adapters/wlogger.js'
@@ -28,7 +26,6 @@ class IpfsRESTControllerLib {
// Encapsulate dependencies
// this.UserModel = this.adapters.localdb.Users
// this.userUseCases = this.useCases.user
this.fs = fs
// Bind 'this' object to all subfunctions
this.getStatus = this.getStatus.bind(this)
@@ -37,7 +34,6 @@ class IpfsRESTControllerLib {
this.handleError = this.handleError.bind(this)
this.connect = this.connect.bind(this)
this.getThisNode = this.getThisNode.bind(this)
this.downloadFile = this.downloadFile.bind(this)
}
/**
@@ -129,77 +125,6 @@ class IpfsRESTControllerLib {
}
}
async downloadFile (ctx) {
try {
// const { cid } = ctx.params
// const multiaddr = ctx.request.body.multiaddr
// const getDetails = ctx.request.body.getDetails
const { cid, fileName, path } = ctx.request.body
console.log(`downloadFile() retrieving this CID: ${cid}, with fileName: ${fileName}, and path: ${path}`)
// const file = await this.adapters.ipfs.ipfs.blockstore.get(cid)
// console.log('file: ', file)
// return file
// const ipfsFs = this.adapters.ipfs.ipfs.fs
// console.log('ipfsFs: ', ipfsFs)
//
// const buf = []
// for await (const chunk of ipfsFs.cat(cid)) {
// // text += decoder.decode(chunk, {
// // stream: true
// // })
//
// buf.push(chunk)
// }
// console.log(`buf: `, buf)
const blockstore = this.adapters.ipfs.ipfs.blockstore
const entry = await exporter(cid, blockstore)
console.info(entry.cid) // Qmqux
console.info(entry.path) // Qmbaz/foo/bar.txt
console.info(entry.name) // bar.txt
console.log('entry: ', entry)
// console.info(entry.unixfs.fileSize()) // 4
// stream content from unixfs node
// const bytes = new Uint8Array(Number(entry.size))
// let offset = 0
const filePath = `${path}/${fileName}`
const writableStream = fs.createWriteStream(filePath)
writableStream.on('error', (error) => {
console.log(`An error occured while writing to the file. Error: ${error.message}`)
})
writableStream.on('finish', () => {
console.log(`CID ${cid} downloaded to ${filePath}`)
})
for await (const buf of entry.content()) {
// bytes.set(buf, offset)
// offset += buf.length
writableStream.write(buf)
}
writableStream.end()
// console.info(bytes) // 0, 1, 2, 3
ctx.body = {
success: true
}
} catch (err) {
wlogger.error('Error in ipfs/controller.js/downloadFile(): ', err)
this.handleError(ctx, err)
}
}
// DRY error handler
handleError (ctx, err) {
// If an HTTP status is specified by the buisiness logic, use that.
-2
View File
@@ -56,8 +56,6 @@ class IpfsRouter {
this.router.post('/relays', this.ipfsRESTController.getRelays)
this.router.post('/connect', this.ipfsRESTController.connect)
this.router.get('/node', this.ipfsRESTController.getThisNode)
// this.router.get('/download/:cid', this.ipfsRESTController.downloadFile)
this.router.post('/download', this.ipfsRESTController.downloadFile)
// Attach the Controller routes to the Koa app.
app.use(this.router.routes())
+2 -2
View File
@@ -177,12 +177,12 @@ describe('#IPFS-adapter', () => {
createKey: async () => {}
})
sandbox.stub(uut, 'createLibp2p').resolves()
sandbox.stub(uut, 'createHelia').resolves(true)
sandbox.stub(uut, 'createHelia').resolves({})
const result = await uut.createNode()
// console.log('result: ', result)
assert.equal(result, true)
assert.property(result, 'fs')
})
it('should not use circuit relay when CONNECT_PREF is set to direct', async () => {