From 1f3c639100231b3ed0b4ce59bc10963a055037c1 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 10 Mar 2024 07:43:06 -0700 Subject: [PATCH] fix(tests): Back to 100% unit test coverage --- package-lock.json | 1 - package.json | 1 - src/controllers/rest-api/ipfs/controller.js | 75 --------------------- src/controllers/rest-api/ipfs/index.js | 2 - test/unit/adapters/ipfs.adapter.unit.js | 4 +- 5 files changed, 2 insertions(+), 81 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2c34445..27f0c7e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 1abfd31..a418efa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/controllers/rest-api/ipfs/controller.js b/src/controllers/rest-api/ipfs/controller.js index 1bf3cb0..d883288 100644 --- a/src/controllers/rest-api/ipfs/controller.js +++ b/src/controllers/rest-api/ipfs/controller.js @@ -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. diff --git a/src/controllers/rest-api/ipfs/index.js b/src/controllers/rest-api/ipfs/index.js index ccb5c69..d787f73 100644 --- a/src/controllers/rest-api/ipfs/index.js +++ b/src/controllers/rest-api/ipfs/index.js @@ -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()) diff --git a/test/unit/adapters/ipfs.adapter.unit.js b/test/unit/adapters/ipfs.adapter.unit.js index 5930abb..c1d0dc2 100644 --- a/test/unit/adapters/ipfs.adapter.unit.js +++ b/test/unit/adapters/ipfs.adapter.unit.js @@ -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 () => {