/* globals describe, it */

'use strict'

const expect = require('chai').expect
const base58 = require('bs58')
const fs = require('fs')
const IPFS = require('../../src/ipfs-core')
const Block = require('ipfs-merkle-dag').Block

const isNode = !global.window

// FIXME remove this (it is only here because otherwise webpack is not loading Buffer and it is needed to the tests bellow)
const noop = new Buffer([]) // eslint-disable-line

const fileA = isNode
  ? fs.readFileSync(process.cwd() + '/tests/repo-example/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data')
  : require('buffer!./../repo-example/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data')

// console.log('=>', fileA)
// console.log('=>', fileA.length)

describe('block', () => {
  var ipfs

  it('get', done => {
    ipfs = new IPFS()
    const b58mh = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe'
    const mh = new Buffer(base58.decode(b58mh))
    ipfs.block.get(mh, (err, block) => {
      expect(err).to.not.exist
      const eq = fileA.equals(block.data)
      expect(eq).to.equal(true)
      done()
    })
  })
  it('put', done => {
    var b = new Block('random data')
    ipfs.block.put(b, function (err) {
      expect(err).to.not.exist
      ipfs.block.get(b.key, function (err, block) {
        expect(err).to.not.exist
        expect(b.data.equals(block.data)).to.equal(true)
        expect(b.key.equals(block.key)).to.equal(true)
        done()
      })
    })
  })

  it('rm', done => {
    var b = new Block('I will not last long enough')
    ipfs.block.put(b, function (err) {
      expect(err).to.not.exist
      ipfs.block.get(b.key, function (err, block) {
        expect(err).to.not.exist
        ipfs.block.del(b.key, function (err) {
          expect(err).to.not.exist
          ipfs.block.get(b.key, function (err, block) {
            expect(err).to.exist
            done()
          })
        })
      })
    })
  })

  it('stat', done => {
    const mh = new Buffer(base58
        .decode('QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe'))
    ipfs.block.stat(mh, (err, stats) => {
      expect(err).to.not.exist
      expect(stats.Key.equals(mh)).to.equal(true)
      expect(stats.Size).to.equal(309)
      done()
    })
  })
})

@@ -256,184 +256,8 @@
 ow%0A%0A
-// FIXME remove this (it is only here because otherwise webpack is not loading Buffer and it is needed to the tests bellow)%0Aconst noop = new Buffer(%5B%5D) // eslint-disable-line%0A%0A
 cons
@@ -560,68 +560,98 @@
 %0A// 
-console.log('=%3E', fileA)%0A// console.log('=%3E', fileA.length)%0A
+TODO use arrow funtions again when https://github.com/webpack/webpack/issues/1944 is fixed
 %0Ades
@@ -668,14 +668,20 @@
 ck',
+ function
  ()
- =%3E
  %7B%0A 
@@ -699,31 +699,39 @@
   it('get', 
+function (
 done
- =%3E
+)
  %7B%0A    ipfs 
@@ -1866,31 +1866,39 @@
  it('stat', 
+function (
 done
- =%3E
+)
  %7B%0A    const

Use regular functions on tests that use Buffer