Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(import): 🧪 Backfill Tests #1276

Merged
merged 4 commits into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})
})
})