+fs = require 'fs'
+path = require 'path'
+
 describe "NativeCompileCache", ->
   nativeCompileCache = require '../src/native-compile-cache'
   [fakeCacheStore, cachedFiles] = []

    expect(cachedFiles[1].cacheBuffer.length).toBeGreaterThan(0)
    expect(fn2()).toBe(2)

    fakeCacheStore.has.andReturn(true)
    fakeCacheStore.get.andReturn(cachedFiles[0].cacheBuffer)
    fakeCacheStore.set.reset()

    fn1 = require('./fixtures/native-cache/file-1')

    expect(fakeCacheStore.set).not.toHaveBeenCalled()
    expect(fn1()).toBe(1)

  it "deletes previously cached code when the cache is an invalid file", ->
    fakeCacheStore.has.andReturn(true)

    nativeCompileCache.setCacheStore(fakeCacheStore)
    nativeCompileCache.install()

    fs.writeFileSync path.resolve('./spec/fixtures/native-cache/file-4'), """
    module.exports = function () { return "file-4" }
    """

  afterEach ->
    fs.unlinkSync path.resolve('./spec/fixtures/native-cache/file-4')

  it "writes and reads from the cache storage when requiring files", ->
    fakeCacheStore.has.andCallFake (cacheKey, invalidationKey) ->
      fakeCacheStore.get(cacheKey, invalidationKey)?
    fakeCacheStore.get.andCallFake (cacheKey, invalidationKey) ->
      for entry in cachedFiles
        continue if entry.cacheKey isnt cacheKey
        continue if entry.invalidationKey isnt invalidationKey
        return entry.cacheBuffer
      return
    fakeCacheStore.set.andCallFake (cacheKey, invalidationKey, cacheBuffer) ->
      cachedFiles.push({cacheKey, invalidationKey, cacheBuffer})

    fn1 = require('./fixtures/native-cache/file-1')
    fn2 = require('./fixtures/native-cache/file-2')
    fn4 = require('./fixtures/native-cache/file-4')

    expect(cachedFiles.length).toBe(3)

    expect(cachedFiles[0].cacheKey).toBe(require.resolve('./fixtures/native-cache/file-1'))
    expect(cachedFiles[0].cacheBuffer).toBeInstanceOf(Uint8Array)
