const BlockStore = require('./')
const test = require('ava')
const fs = require('fs')
const path = require('path')
const mongoose = require('mongoose')
const {Types} = mongoose
const {ObjectId} = Types
const tempFileId = (new ObjectId()).toString() + '.png'

test.serial('createBucket', t => {
  return BlockStore.createBucket('files')
    .catch(err => t.fail(err))
    .then(() => t.pass())
})

test.serial('listBuckets', t => {
  return BlockStore.listBuckets()
    .then(buckets => t.true(buckets.length >= 1))
})

test.serial('set', t => {
  const testFile = fs.createReadStream(path.resolve(__dirname, './config.js'))
  const stats    = fs.statSync(path.resolve(__dirname, './config.js'))
  return BlockStore.set({id: tempFileId, stream: testFile, size: 1757, contentType: 'application/javascript'})
    .then(etag => {
      t.truthy(etag)
    }).catch(err => t.fail(err))
})

test.serial('get', t => {
  return BlockStore.get({id: tempFileId})
    .then(file => {
      t.truthy(file)
    }).catch(err => t.fail(err))
})

test.serial('get: fail on bad name', t => {
  return BlockStore.get({id: ''})
    .then(file => t.fail())
    .catch(err => t.pass())
})

test.serial('find', t => {
  return BlockStore.find({prefix: tempFileId})
    .then(files => {
      t.truthy(files.length >= 1)
    }).catch(err => t.fail(err))
})

test.serial('stat', t => {
  return BlockStore.stat({id: tempFileId})
    .then(file => {
      t.truthy(file.size >= 100)
    }).catch(err => t.fail(err))
})

test.serial('remove', t => {
  return BlockStore.remove({id: tempFileId})
    .then(result => {
      // console.log('result', result, typeof result)
      t.truthy(result)
    }).catch(err => t.fail(err))
})

test.serial('signedSetUrl', t => {
  return BlockStore.signedSetUrl({id: tempFileId})
    .then(url => {
      t.truthy(url)
    }).catch(err => t.fail(err))
})

test.serial('signedSetUrl', t => {
  return BlockStore.signedSetUrl({id: tempFileId})
    .then(url => {
      t.truthy(url)
    }).catch(err => t.fail(err))
})

