Skip to content

Commit

Permalink
test(import): 🧪 Backfill Tests (#1276)
Browse files Browse the repository at this point in the history
* test(import): 🧪 Backfill Tests

Signed-off-by: Nishant Arora <[email protected]>

* Update test/functional/lib/ipfs-import.test.js

Co-authored-by: Marcin Rataj <[email protected]>

* Update test/functional/lib/ipfs-import.test.js

Co-authored-by: Marcin Rataj <[email protected]>

* test(import): 🧪 Backfilling tests for copyImportResultsToFiles

Signed-off-by: Nishant Arora <[email protected]>

---------

Signed-off-by: Nishant Arora <[email protected]>
Co-authored-by: Marcin Rataj <[email protected]>
  • Loading branch information
whizzzkid and lidel authored Sep 15, 2023
1 parent ee8cfa2 commit a91bd9a
Showing 1 changed file with 119 additions and 2 deletions.
121 changes: 119 additions & 2 deletions test/functional/lib/ipfs-import.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'
import { expect } from 'chai'
import { describe, it } from 'mocha'
import { formatImportDirectory } from '../../../add-on/src/lib/ipfs-import.js'
import { describe, it, afterEach } from 'mocha'
import sinon from 'sinon'
import browser from 'sinon-chrome'
import { createIpfsImportHandler, formatImportDirectory } from '../../../add-on/src/lib/ipfs-import.js'

describe('ipfs-import.js', function () {
describe('formatImportDirectory', function () {
Expand Down Expand Up @@ -30,4 +32,119 @@ describe('ipfs-import.js', function () {
expect(formatImportDirectory(path)).to.equal('/ipfs-companion-imports/2017-11-05_120101/')
})
})

describe('createIpfsImportHandler', function () {
const sandbox = sinon.createSandbox()
const getState = sandbox.stub()
const getIpfs = sandbox.stub()
const ipfsPathValidator = sandbox.stub()
const runtime = {
hasNativeProtocolHandler: false
}
const hasNativeProtocolHandlerStub = sandbox.stub(runtime, 'hasNativeProtocolHandler')
const copier = sandbox.stub()

const ipfsImportHandler = createIpfsImportHandler(getState, getIpfs, ipfsPathValidator, runtime, copier)

afterEach(function () {
sandbox.restore()
})

describe('openFilesAtWebUI', function () {
it('should open the webui with the correct path', async function () {
const state = { webuiRootUrl: 'http://localhost:5001' }
const mfsPath = '/my-directory'
const url = `${state.webuiRootUrl}#/files${mfsPath}`
getState.returns(state)
await ipfsImportHandler.openFilesAtWebUI(mfsPath)
sinon.assert.calledWith(browser.tabs.create, { url })
})
})

describe('openFilesAtGateway', function () {
it('should open the gateway with the correct path', async function () {
const ipfs = {
files: {
stat: () => Promise.resolve({
cid: 'bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa'
})
}
}
const state = { pubGwURLString: 'https://ipfs.io' }
const mfsPath = '/my-directory'
const url = `${state.pubGwURLString}/ipfs/bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa`
getState.returns(state)
getIpfs.returns(ipfs)
await ipfsImportHandler.openFilesAtGateway(mfsPath)
sinon.assert.calledWith(browser.tabs.create, { url })
})

it('should open the gateway with the correct path when native protocol handlers are set', async function () {
const ipfs = {
files: {
stat: () => Promise.resolve({
cid: 'bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa'
})
}
}
getIpfs.returns(ipfs)
getState.returns({ pubGwURLString: 'https://ipfs.io' })
hasNativeProtocolHandlerStub.value(true)
const mfsPath = '/my-directory'
const url = 'ipfs://bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa'
await ipfsImportHandler.openFilesAtGateway(mfsPath)
sinon.assert.calledWith(browser.tabs.create, { url })
})
})

describe('copyImportResultsToFiles', function () {
it('should copy files to the correct directory', async function () {
const cpSpy = sandbox.spy()
const mkdirSpy = sandbox.spy()
const ipfs = {
files: {
cp: cpSpy,
mkdir: mkdirSpy
}
}
const resultFiles = [
{
cid: 'bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa',
path: '/tmp/test-file1'
},
{
cid: 'bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqb',
path: ''
},
{
cid: 'bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqc',
path: '/tmp/test-file2'
}
]
getIpfs.returns(ipfs)
await ipfsImportHandler.copyImportResultsToFiles(resultFiles, '/my-directory')
sinon.assert.calledWith(mkdirSpy, '/my-directory')
expect(cpSpy.getCalls()[0].args).to.deep.equal([
'/ipfs/bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqa',
'/my-directory/tmp/test-file1'
])
expect(cpSpy.getCalls()[1].args).to.deep.equal([
'/ipfs/bafybeicgmdpvw4duutrmdxl4a7gc52sxyuk7nz5gby77afwdteh3jc5bqc',
'/my-directory/tmp/test-file2'
])
})
})

describe('copyShareLink', function () {
// TODO: implement
})

describe('preloadFilesAtPublicGateway', function () {
// TODO: implement
})

describe('filesCpImportCurrentTab', function () {
// TODO: implement
})
})
})

0 comments on commit a91bd9a

Please sign in to comment.