Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore: dedupe tests and fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Sep 16, 2020
1 parent 050e84b commit dc9d91e
Show file tree
Hide file tree
Showing 51 changed files with 588 additions and 1,520 deletions.
54 changes: 54 additions & 0 deletions packages/interface-ipfs-core/src/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const testTimeout = require('./utils/test-timeout')
const echoUrl = (text) => `${process.env.ECHO_SERVER}/download?data=${encodeURIComponent(text)}`
const redirectUrl = (url) => `${process.env.ECHO_SERVER}/redirect?to=${encodeURI(url)}`
const uint8ArrayFromString = require('uint8arrays/from-string')
const { nanoid } = require('nanoid')
const last = require('it-last')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand Down Expand Up @@ -362,5 +364,57 @@ module.exports = (common, options) => {
expect(file.cid.codec).to.equal('dag-pb')
expect(file.size).to.equal(18)
})

it('should not error when passed null options', async () => {
await ipfs.add(uint8ArrayFromString(nanoid()), null)
})

it('should add a file with a v1 CID', async () => {
const file = await ipfs.add(Uint8Array.from([0, 1, 2]), {
cidVersion: 1
})

expect(file.cid.toString()).to.equal('bafkreifojmzibzlof6xyh5auu3r5vpu5l67brf3fitaf73isdlglqw2t7q')
expect(file.size).to.equal(3)
})

const testFiles = Array.from(Array(1005), (_, i) => ({
path: 'test-folder/' + i,
content: uint8ArrayFromString('some content ' + i)
}))

describe('without sharding', () => {
let ipfs

before(async function () {
const ipfsd = await common.spawn({
ipfsOptions: { EXPERIMENTAL: { sharding: false } }
})
ipfs = ipfsd.api
})

it('should be able to add dir without sharding', async () => {
const { path, cid } = await last(ipfs.addAll(testFiles))
expect(path).to.eql('test-folder')
expect(cid.toString()).to.eql('QmWWM8ZV6GPhqJ46WtKcUaBPNHN5yQaFsKDSQ1RE73w94Q')
})
})

describe('with sharding', () => {
let ipfs

before(async function () {
const ipfsd = await common.spawn({
ipfsOptions: { EXPERIMENTAL: { sharding: false } }
})
ipfs = ipfsd.api
})

it('should be able to add dir with sharding', async () => {
const { path, cid } = await last(ipfs.addAll(testFiles))
expect(path).to.eql('test-folder')
expect(cid.toString()).to.eql('Qmb3JNLq2KcvDTSGT23qNQkMrr4Y4fYMktHh6DtC7YatLa')
})
})
})
}
4 changes: 3 additions & 1 deletion packages/interface-ipfs-core/src/bitswap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const { createSuite } = require('../utils/suite')
const tests = {
stat: require('./stat'),
wantlist: require('./wantlist'),
wantlistForPeer: require('./wantlist-for-peer')
wantlistForPeer: require('./wantlist-for-peer'),
transfer: require('./transfer'),
unwant: require('./unwant')
}

module.exports = createSuite(tests)
87 changes: 87 additions & 0 deletions packages/interface-ipfs-core/src/bitswap/transfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* eslint-env mocha */
'use strict'

const { getDescribe, getIt, expect } = require('../utils/mocha')
const { isWebWorker } = require('ipfs-utils/src/env')
const CID = require('cids')
const randomBytes = require('iso-random-stream/src/random')
const Block = require('ipld-block')
const concat = require('it-concat')
const { nanoid } = require('nanoid')
const uint8ArrayFromString = require('uint8arrays/from-string')
const pmap = require('p-map')
const multihashing = require('multihashing-async')

const makeBlock = async () => {
const d = uint8ArrayFromString(`IPFS is awesome ${nanoid()}`)
const h = await multihashing(d, 'sha2-256')

return new Block(d, new CID(h))
}

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} factory
* @param {Object} options
*/
module.exports = (factory, options) => {
const describe = getDescribe(options)
const it = getIt(options)

describe('transfer blocks', function () {
this.timeout(60 * 1000)

afterEach(() => factory.clean())

describe('transfer a block between', () => {
it('2 peers', async function () {
// webworkers are not dialable because webrtc is not available
const remote = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
const local = (await factory.spawn()).api
await local.swarm.connect(remote.peerId.addresses[0])
const block = await makeBlock()

await local.block.put(block)
const b = await remote.block.get(block.cid)

expect(b.data).to.eql(block.data)
})

it('3 peers', async () => {
const blocks = await Promise.all([...Array(6).keys()].map(() => makeBlock()))
const remote1 = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
const remote2 = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
const local = (await factory.spawn()).api
await local.swarm.connect(remote1.peerId.addresses[0])
await local.swarm.connect(remote2.peerId.addresses[0])
await remote1.swarm.connect(remote2.peerId.addresses[0])

await remote1.block.put(blocks[0])
await remote1.block.put(blocks[1])
await remote2.block.put(blocks[2])
await remote2.block.put(blocks[3])
await local.block.put(blocks[4])
await local.block.put(blocks[5])

await pmap(blocks, async (block) => {
expect(await remote1.block.get(block.cid)).to.eql(block)
expect(await remote2.block.get(block.cid)).to.eql(block)
expect(await local.block.get(block.cid)).to.eql(block)
}, { concurrency: 3 })
})
})

describe('transfer a file between', () => {
it('2 peers', async () => {
const content = randomBytes(1024 * 1024 * 10)
const remote = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
const local = (await factory.spawn()).api
local.swarm.connect(remote.peerId.addresses[0])

const file = await remote.add({ path: 'awesome.txt', content })
const data = await concat(local.cat(file.cid))
expect(data.slice()).to.eql(content)
})
})
})
}
31 changes: 31 additions & 0 deletions packages/interface-ipfs-core/src/bitswap/unwant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-env mocha */
'use strict'

const { getDescribe, getIt, expect } = require('../utils/mocha')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} factory
* @param {Object} options
*/
module.exports = (factory, options) => {
const describe = getDescribe(options)
const it = getIt(options)

describe('.bitswap.unwant', function () {
this.timeout(60 * 1000)

after(() => factory.clean())

it('should throw error for invalid CID input', async () => {
const ipfs = (await factory.spawn()).api

try {
await ipfs.bitswap.unwant('INVALID CID')
} catch (err) {
expect(err).to.exist()
expect(err.code).to.equal('ERR_INVALID_CID')
}
})
})
}
5 changes: 5 additions & 0 deletions packages/interface-ipfs-core/src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const CID = require('cids')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const testTimeout = require('../utils/test-timeout')
const all = require('it-all')
const { nanoid } = require('nanoid')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand Down Expand Up @@ -98,5 +99,9 @@ module.exports = (common, options) => {
return expect(ipfs.block.put([blob, blob])).to.eventually.be.rejected
.and.be.an.instanceOf(Error)
})

it('should not error when passed null options', () => {
return ipfs.block.put(uint8ArrayFromString(nanoid()), null)
})
})
}
6 changes: 6 additions & 0 deletions packages/interface-ipfs-core/src/block/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,11 @@ module.exports = (common, options) => {
expect(result).to.have.property('error').that.is.an('Error')
.with.property('message').that.includes('pinned')
})

it('should throw error for invalid CID input', () => {
return expect(all(ipfs.block.rm('INVALID CID')))
.to.eventually.be.rejected()
.and.to.have.a.property('code').that.equals('ERR_INVALID_CID')
})
})
}
6 changes: 6 additions & 0 deletions packages/interface-ipfs-core/src/block/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
const CID = require('cids')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const testTimeout = require('../utils/test-timeout')
const { nanoid } = require('nanoid')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand Down Expand Up @@ -49,5 +50,10 @@ module.exports = (common, options) => {
return expect(ipfs.block.stat('invalid')).to.eventually.be.rejected
.and.be.an.instanceOf(Error)
})

it('should not error when passed null options', async () => {
const block = await ipfs.block.put(uint8ArrayFromString(nanoid()))
return ipfs.block.stat(block.cid, null)
})
})
}
10 changes: 10 additions & 0 deletions packages/interface-ipfs-core/src/bootstrap/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,15 @@ module.exports = (common, options) => {
const list = await ipfs.bootstrap.list()
expect(list).to.have.property('Peers').that.deep.equals([validIp4])
})

it('add a peer to the bootstrap list', async () => {
const peer = '/ip4/111.111.111.111/tcp/1001/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb'

const res = await ipfs.bootstrap.add(peer)
expect(res).to.be.eql({ Peers: [peer] })

const list = await ipfs.bootstrap.list()
expect(list.Peers).to.include(peer)
})
})
}
19 changes: 19 additions & 0 deletions packages/interface-ipfs-core/src/bootstrap/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

const { getDescribe, getIt, expect } = require('../utils/mocha')
const testTimeout = require('../utils/test-timeout')
const multiaddr = require('multiaddr')
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand Down Expand Up @@ -34,5 +36,22 @@ module.exports = (common, options) => {
const peers = res.Peers
expect(peers).to.exist()
})

it('bootstrap list should contain dialable nodes', async () => {
const list = await ipfs.bootstrap.list()
const onlyWss = list.some(addr => {
const ma = multiaddr(addr)

return ma.protos().some(proto => proto.name === 'wss')
})

expect(list).to.not.be.empty()

if (isBrowser || isWebWorker) {
expect(onlyWss).to.be.true()
} else {
expect(onlyWss).to.be.false()
}
})
})
}
13 changes: 13 additions & 0 deletions packages/interface-ipfs-core/src/bootstrap/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,18 @@ module.exports = (common, options) => {
const peers = rmRes.Peers
expect(peers).to.have.property('length').that.is.equal(1)
})

it('removes a peer from the bootstrap list', async () => {
const peer = '/ip4/111.111.111.111/tcp/1001/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb'
await ipfs.bootstrap.add(peer)
let list = await ipfs.bootstrap.list()
expect(list.Peers).to.include(peer)

const res = await ipfs.bootstrap.rm(peer)
expect(res).to.be.eql({ Peers: [peer] })

list = await ipfs.bootstrap.list()
expect(list.Peers).to.not.include(peer)
})
})
}
12 changes: 12 additions & 0 deletions packages/interface-ipfs-core/src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,17 @@ module.exports = (common, options) => {
const result = await ipfs.dag.get(cid)
expect(result.value).to.deep.equal(buf)
})

it('should throw error for invalid string CID input', () => {
return expect(ipfs.dag.get('INVALID CID'))
.to.eventually.be.rejected()
.and.to.have.property('code').that.equals('ERR_INVALID_CID')
})

it('should throw error for invalid buffer CID input', () => {
return expect(ipfs.dag.get(uint8ArrayFromString('INVALID CID')))
.to.eventually.be.rejected()
.and.to.have.property('code').that.equals('ERR_INVALID_CID')
})
})
}
6 changes: 6 additions & 0 deletions packages/interface-ipfs-core/src/dag/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,11 @@ module.exports = (common, options) => {
'Data'
])
})

it('should throw error for invalid CID input', () => {
return expect(all(ipfs.dag.tree('INVALID CID')))
.to.eventually.be.rejected()
.and.to.have.property('code').that.equals('ERR_INVALID_CID')
})
})
}
41 changes: 41 additions & 0 deletions packages/interface-ipfs-core/src/dht/disabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-env mocha */
'use strict'

const { getDescribe, getIt, expect } = require('../utils/mocha')
const uint8ArrayFromString = require('uint8arrays/from-string')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} factory
* @param {Object} options
*/
module.exports = (factory, options) => {
const describe = getDescribe(options)
const it = getIt(options)

describe('disabled', function () {
this.timeout(80 * 1000)

let ipfs

before(async () => {
ipfs = (await factory.spawn({
ipfsOptions: {
config: {
Routing: {
Type: 'none'
}
}
}
})).api
})

after(() => factory.clean())

it('should error when DHT not available', async () => {
await expect(ipfs.dht.put(uint8ArrayFromString('a'), uint8ArrayFromString('b')))
.to.eventually.be.rejected()
.and.to.have.property('code', 'ERR_NOT_ENABLED')
})
})
}
3 changes: 2 additions & 1 deletion packages/interface-ipfs-core/src/dht/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const tests = {
findPeer: require('./find-peer'),
provide: require('./provide'),
findProvs: require('./find-provs'),
query: require('./query')
query: require('./query'),
disabled: require('./disabled')
}

module.exports = createSuite(tests)
Loading

0 comments on commit dc9d91e

Please sign in to comment.