/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const BlockService = require('ipfs-block-service')
const dagCBOR = require('ipld-dag-cbor')

const IPLDResolver = require('../src')

module.exports = (repo) => {
  describe('IPLD format support', () => {
    let data, cid

    before((done) => {
      const bs = new BlockService(repo)
      const resolver = new IPLDResolver({ blockService: bs })

      data = { now: Date.now() }

      dagCBOR.util.cid(data, (err, c) => {
        expect(err).to.not.exist()
        cid = c
        resolver.put(data, { cid }, done)
      })
    })

    describe('Dynamic format loading', () => {
      it('should fail to dynamically load format', (done) => {
        const bs = new BlockService(repo)
        const resolver = new IPLDResolver({
          blockService: bs,
          formats: []
        })

        resolver.get(cid, '/', (err) => {
          expect(err).to.exist()
          expect(err.message).to.equal('No resolver found for codec "dag-cbor"')
          done()
        })
      })

      it('should fail to dynamically load format via loadFormat option', (done) => {
        const errMsg = 'BOOM' + Date.now()
        const bs = new BlockService(repo)
        const resolver = new IPLDResolver({
          blockService: bs,
          formats: [],
          loadFormat (codec, callback) {
            if (codec !== 'dag-cbor') return callback(new Error('unexpected codec'))
            setTimeout(() => callback(new Error(errMsg)))
          }
        })

        resolver.get(cid, '/', (err) => {
          expect(err).to.exist()
          expect(err.message).to.equal(errMsg)
          done()
        })
      })

      it('should dynamically load missing format', (done) => {
        const bs = new BlockService(repo)
        const resolver = new IPLDResolver({
          blockService: bs,
          formats: [],
          loadFormat (codec, callback) {
            if (codec !== 'dag-cbor') return callback(new Error('unexpected codec'))
            setTimeout(() => callback(null, dagCBOR))
          }
        })

        resolver.get(cid, '/', (err, result) => {
          expect(err).to.not.exist()
          expect(result.value).to.eql(data)
          done()
        })
      })
    })
  })
}

@@ -2163,32 +2163,539 @@
   %7D%0A        %7D)%0A%0A
+        resolver.get(cid, '/', (err, result) =%3E %7B%0A          expect(err).to.not.exist()%0A          expect(result.value).to.eql(data)%0A          done()%0A        %7D)%0A      %7D)%0A%0A      it('should not dynamically load format added statically', (done) =%3E %7B%0A        const bs = new BlockService(repo)%0A        const resolver = new IPLDResolver(%7B%0A          blockService: bs,%0A          formats: %5BdagCBOR%5D,%0A          loadFormat (codec) %7B%0A            throw new Error(%60unexpected load format $%7Bcodec%7D%60)%0A          %7D%0A        %7D)%0A%0A
         resolver

add test for not dynbamically loading existing format