Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Initial work on #57 with cross-browser + node testing [do not merge] #77

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
some-test.js
npm-debug.log
testem.log
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"merge-stream": "^1.0.0",
"multiaddr": "^1.0.0",
"multipart-stream": "^2.0.0",
"testem": "^0.9.8",
"vinyl": "^0.5.1",
"vinyl-fs-browser": "^2.1.1-1",
"vinyl-multipart-stream": "^1.2.6"
Expand All @@ -33,7 +34,7 @@
"uglify-js": "^2.4.24"
},
"scripts": {
"test": "./node_modules/.bin/mocha",
"test": "./node_modules/.bin/testem",
"lint": "./node_modules/.bin/standard",
"build": "./node_modules/.bin/browserify -t brfs -s ipfsAPI -e ./src/index.js | tee dist/ipfsapi.js | ./node_modules/.bin/uglifyjs -m > dist/ipfsapi.min.js"
},
Expand Down
4 changes: 4 additions & 0 deletions test/build_and_start_daemon.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /bin/bash
API_ORIGIN="*" ipfs daemon&
sleep 5
./node_modules/.bin/browserify test/test.js -o browserified.js
152 changes: 85 additions & 67 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
var ipfsd = require('ipfsd-ctl')
// var ipfsd = require('ipfsd-ctl')
var ipfsApi = require('../src/index.js')
var assert = require('assert')
var fs = require('fs')
var path = require('path')
var File = require('vinyl')
// TODO not sure how to deal with these in the browser
// var fs = require('fs')
// var path = require('path')
// var File = require('vinyl')

// this comment is used by mocha, do not delete
/*global describe, before, it*/
/*global describe, before, it, xit*/

function log () {
var args = [' #']
args = args.concat(Array.prototype.slice.call(arguments))
console.log.apply(console, args)
}
// this comment is for temporary undefined variables
/*global File, path, testfile, fs*/

var testfile = __dirname + '/testfile.txt'
var test_string = 'IPFS workcs'
var test_string_hash = 'QmcJRDiJUw5TehYkidky4Gcxgn6dc9LiDPDbotNabD5say'

describe('ipfs node api', function () {
var ipfs, ipfsNode
before(function (done) {
this.timeout(20000)
log('ipfs node setup')

ipfsd.disposable(function (err, node) {
if (err) throw err
log('ipfs init done')
ipfsNode = node
// TODO files in the browser?
// var testfile = __dirname + '/testfile.txt'

ipfsNode.startDaemon(function (err, ignore) {
if (err) throw err
log('ipfs daemon running')

ipfs = ipfsApi(ipfsNode.apiAddr)
done()
})
})
describe('ipfs node api', function () {
var ipfs
before(function () {
ipfs = ipfsApi('localhost', '5001')
// TODO maybe optionally load ipfsd in nodejs environment
// this.timeout(20000)
// log('ipfs node setup')

// ipfsd.disposable(function (err, node) {
// if (err) throw err
// log('ipfs init done')
// ipfsNode = node

// ipfsNode.startDaemon(function (err, ignore) {
// if (err) throw err
// log('ipfs daemon running')

// ipfs = ipfsApi(ipfsNode.apiAddr)
// done()
// })
// })
})

it('has the api object', function () {
assert(ipfs)
assert(ipfs.id)
})

it('add file', function (done) {
// TODO need vinyl in browser
xit('add file', function (done) {
this.timeout(10000)

var file = new File({
Expand All @@ -65,17 +70,16 @@ describe('ipfs node api', function () {
it('add buffer', function (done) {
this.timeout(10000)

var buf = new Buffer(fs.readFileSync(testfile))
var buf = new Buffer(test_string)
ipfs.add(buf, function (err, res) {
if (err) throw err

assert.equal(res.length, 1)
assert.equal(res[0].Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
assert.equal(res.Hash, test_string_hash)
done()
})
})

it('add path', function (done) {
// TODO not sure how to deal with this is a browser either
xit('add path', function (done) {
this.timeout(10000)

ipfs.add(testfile, function (err, res) {
Expand All @@ -90,16 +94,10 @@ describe('ipfs node api', function () {
it('cat', function (done) {
this.timeout(10000)

ipfs.cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, stream) {
ipfs.cat(test_string_hash, function (err, res) {
if (err) throw err
var buf = ''
stream
.on('error', function (err) { throw err })
.on('data', function (data) { buf += data })
.on('end', function () {
assert.equal(buf, fs.readFileSync(testfile))
done()
})
assert.equal(res, test_string)
done()
})
})

Expand Down Expand Up @@ -132,18 +130,19 @@ describe('ipfs node api', function () {
})
})

// TODO not sure how to deal with this
// var testConfPath = __dirname + '/testconfig.json'
// var testConf = fs.readFileSync(testConfPath).toString()
// var readConf
// before(function (done) {
// ipfs.config.replace(testConfPath, function (err) {
// if (err) throw err
// ipfs.config.show(function (err, res) {
// if (err) throw err
// readConf = res
// done()
// })
// })
// ipfs.config.replace(testConfPath, function (err) {
// if (err) throw err
// ipfs.config.show(function (err, res) {
// if (err) throw err
// readConf = res
// done()
// })
// })
// })

// it('config replace/show', function () {
Expand Down Expand Up @@ -180,7 +179,8 @@ describe('ipfs node api', function () {
})
})

it('block.get', function (done) {
// TODO not working because of buffer probably
xit('block.get', function (done) {
this.timeout(10000)

ipfs.block.get(blorbKey, function (err, res) {
Expand Down Expand Up @@ -218,7 +218,8 @@ describe('ipfs node api', function () {
})
})

it('object.data', function (done) {
// TODO buffers
xit('object.data', function (done) {
this.timeout(10000)
ipfs.object.data(testObjectHash, function (err, stream) {
if (err) throw err
Expand All @@ -233,7 +234,8 @@ describe('ipfs node api', function () {
})
})

it('refs', function (done) {
// TODO
xit('refs', function (done) {
this.timeout(10000)
ipfs.refs(initDocs, {'format': '<src> <dst> <linkname>'}, function (err, objs) {
if (err) throw err
Expand Down Expand Up @@ -269,7 +271,8 @@ describe('ipfs node api', function () {
})
})

it('puts and gets a key value pair in the DHT', function (done) {
// TODO
xit('puts and gets a key value pair in the DHT', function (done) {
this.timeout(20000)

ipfs.dht.put('scope', 'interplanetary', function (err, cb) {
Expand All @@ -287,17 +290,32 @@ describe('ipfs node api', function () {
})
})

it('test for error after daemon stops', function (done) {
this.timeout(6000)
var nodeStopped
ipfsNode.stopDaemon(function () {
if (!nodeStopped) {
nodeStopped = true
ipfs.id(function (err, res) {
assert.equal(err.code, 'ECONNREFUSED')
done()
})
}
// TODO not applicable anymore
// it('test for error after daemon stops', function (done) {
// this.timeout(6000)
// var nodeStopped
// ipfsNode.stopDaemon(function () {
// if (!nodeStopped) {
// nodeStopped = true
// ipfs.id(function (err, res) {
// assert.equal(err.code, 'ECONNREFUSED')
// done()
// })
// }
// })
// })

// TODO incomplete test
xit('can return a valid list of peers from findprovs', function (done) {
ipfs.dht.findprovs(testObjectHash, function (err, peers) {
console.log(typeof peers)
console.log(typeof [])
console.log(err, peers)
// Should be more than one items since there is always at least one
// "routing not found" error in the array
console.log(peers.length)
assert(peers.length > 1)
done()
})
})
})
17 changes: 17 additions & 0 deletions testem.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"framework": "mocha",
"serve_files": [ "browserified.js" ],
"src_files": [
"src/index.js",
"test/test.js"
],
"launchers": {
"Node": {
"command": "./node_modules/.bin/mocha test/test.js -R tap",
"protocol": "tap"
}
},
"before_tests": "./test/build_and_start_daemon.sh",
"on_exit": "rm browserified.js;killall ipfs daemon",
"launch_in_dev": [ "chrome", "node", "firefox" ]
}