'use strict'

import readCache from '../lib/read-cache'
import test from 'blue-tape'
import Request from './helpers/req'

test('readCache closure', t => {
  const closure = readCache({})

  t.equals(typeof closure, 'function', 'reading from cache should return a closure')
  t.end()
})

test('readCache closure is a promise', t => {
  const closure = readCache({})

  t.ok(closure() instanceof Promise, 'reading from cache should return a promise')
  t.end()
})

test('readCache success callback', t => {
  const value = {
    expires: 0,
    data: {
      body: {
        responseType: 'text',
        responseText: 'Hello world',
        status: 200,
        statusText: 'OK'
      },
      headers: 'content-type: text/plain'
    }
  }

  const req = new Request()

  return new Promise((resolve) => {
      .then((res) => {
    readCache(req)(value)
        t.equals(res.body, value.data.body.responseText, 'response should be defined')

        resolve()
      })
      .catch((err) => {
        // add test in case of error
        t.error(err, 'hydration should not throw')

        resolve()
      })
  })
})

test('readCache error callback', t => {
  return new Promise((resolve) => {
    readCache(req)(false)
      .then((res) => {
  const req = new Request()

        t.error('response should not be defined on cache miss')

        resolve()
      })
      .catch((err) => {
        // add test in case of error
        t.equals(err.reason, 'cache-miss', 'reading from cache should throw on cache miss')

        resolve()
      })
  })
})

@@ -113,16 +113,74 @@
 ers/req'
+%0Aimport debug from 'debug'%0A%0Aconst log = debug('readCache')
 %0A%0Atest('
@@ -845,40 +845,15 @@
 ise(
-(
 resolve
-) =%3E %7B%0A      .then((res)
  =%3E 
@@ -871,16 +871,21 @@
 ache(req
+, log
 )(value)
@@ -885,16 +885,37 @@
 (value)%0A
+      .then(res =%3E %7B%0A
         
@@ -1026,37 +1026,35 @@
 %7D)%0A      .catch(
-(
 err
-)
  =%3E %7B%0A        //
@@ -1208,16 +1208,45 @@
  t =%3E %7B%0A
+  const req = new Request()%0A%0A
   return
@@ -1262,17 +1262,15 @@
 ise(
-(
 resolve
-)
  =%3E 
@@ -1288,16 +1288,21 @@
 ache(req
+, log
 )(false)
@@ -1318,48 +1318,17 @@
 hen(
-(
 res
-)
  =%3E %7B%0A
-  const req = new Request()%0A%0A
     
@@ -1432,13 +1432,11 @@
 tch(
-(
 err
-)
  =%3E 

add log