<commit_before>'use strict'

import superapiCache from '../lib/index.js'
import MemoryStore from '../lib/memory.js'
import test from 'blue-tape'
import { spy } from 'sinon'

test('middleware configuration need a store', t => {
  t.throws(() => {
    superapiCache()
  }, /Cache middleware need to be provided a store/, 'should throw if no store property found')

  t.end()
})

test('middleware need a cache function ', t => {
  t.throws(() => {
    superapiCache({
      store: {}
    })
  }, /Cache middleware need a cache function/, 'should throw if no caching function provided')

  t.end()
})


test('middleware need a store and a cache function', t => {
  t.doesNotThrow(() => {
    superapiCache({
      store: {},
      cache: function () {}
    })
  }, 'should not throw with a valid configuration provided')

  t.end()
})

test('middleware need a store with a valid API', t => {
  const handler = superapiCache({
    store: {},
    cache: function () {}
  })

  const next = () => {
    return Promise.resolve()
  }

  t.throws(() => {
    handler({}, next, {})
  }, /store.getItem/, 'should throw if invalid store provided')

  t.end()
})


test('should call cache function by default', t => {
  const cacheFn = spy()
  const handler = superapiCache({
    store: new MemoryStore(),
    cache: cacheFn
  })<commit_msg>test(cache): add test about cache read export<commit_after>'use strict'

import superapiCache from '../lib/index.js'
import MemoryStore from '../lib/memory.js'
import readCache from '../lib/read-cache'

import test from 'blue-tape'
import { spy } from 'sinon'

test('middleware configuration need a store', t => {
  t.throws(() => {
    superapiCache()
  }, /Cache middleware need to be provided a store/, 'should throw if no store property found')

  t.end()
})

test('middleware need a cache function ', t => {
  t.throws(() => {
    superapiCache({
      store: {}
    })
  }, /Cache middleware need a cache function/, 'should throw if no caching function provided')

  t.end()
})


test('middleware need a store and a cache function', t => {
  t.doesNotThrow(() => {
    superapiCache({
      store: {},
      cache: function () {}
    })
  }, 'should not throw with a valid configuration provided')

  t.end()
})

test('middleware need a store with a valid API', t => {
  const handler = superapiCache({
    store: {},
    cache: function () {}
  })

  const next = () => {
    return Promise.resolve()
  }

  t.throws(() => {
    handler({}, next, {})
  }, /store.getItem/, 'should throw if invalid store provided')

  t.end()
})

test('cache read function', t => {
  t.ok(superapiCache.readCache, 'should export default readCache')
  t.equals(superapiCache.readCache, readCache, 'readCache should be the one')
  t.end()
})

test('should call cache function by default', t => {
  const cacheFn = spy()
  const handler = superapiCache({
    store: new MemoryStore(),
    cache: cacheFn
  })
{'fraction_non_alphanumeric': 0.1536618754277892, 'fraction_numerical': 0.0, 'mean_word_length': 3.187679083094556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1721497', 'n_tokens_mistral': 897, 'n_tokens_neox': 883, 'n_words': 299}