'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 = {
    body: {
      responseType: 'text',
      responseText: 'Hello world',
      status: 200,
      statusText: 'OK'
    },
    headers: {
      'content-type': 'text/plain'
    }
  }

  const req = new Request()

  return new Promise((resolve) => {
    readCache(req)(JSON.stringify(value))
      .then((res) => {
        t.equals(res.body, value.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()
      })
  })
})

@@ -516,16 +516,46 @@
 lue = %7B%0A
+    expires: 0,%0A    data: %7B%0A  
     body
@@ -560,24 +560,26 @@
 dy: %7B%0A      
+  
 responseType
@@ -588,16 +588,18 @@
 'text',%0A
+  
       re
@@ -627,24 +627,26 @@
 rld',%0A      
+  
 status: 200,
@@ -646,16 +646,18 @@
 s: 200,%0A
+  
       st
@@ -679,11 +679,15 @@
     
+  
 %7D,%0A
+  
     
@@ -694,24 +694,16 @@
 headers:
- %7B%0A     
  'conten
@@ -712,12 +712,10 @@
 type
-'
 : 
-'
 text
@@ -806,68 +806,52 @@
     
-readCache(req)(JSON.stringify(value))%0A      .then((res) =%3E %7B
+  .then((res) =%3E %7B%0A    readCache(req)(value)
 %0A   
@@ -880,16 +880,21 @@
 , value.
+data.
 body.res

fix test to support value format