import { generateSimpleViteManifest, injectApp, mockConfig, mockFetch, mockRedis } from './util.js'
import { expect } from 'chai'
import * as td from 'testdouble'
import sinon from 'sinon'
import RedisMock from 'ioredis-mock'

const sandbox = sinon.createSandbox()

describe('Redis', function () {
  let app
  let spy

  beforeEach(async function () {
    mockConfig({ baseUrls: ['http://ui-server/'] })
    mockRedis({}, false)
    mockFetch({
      'http://ui-server': {
        '/manifest.json': generateSimpleViteManifest({}),
        '/example.js': spy = sandbox.spy(() => {
          return new Response('this is example', { headers: { 'content-type': 'application/javascript' } })
        })
      }
    })
    app = await injectApp()
  })

  afterEach(async function () {
    td.reset()
    await new RedisMock().flushdb()
  })

  it('use internal cache, when redis is disabled', async function () {
    expect(spy.callCount).to.equal(0)
    let response = await app.inject({ url: '/example.js' })
    expect(response.statusCode).to.equal(200)
    expect(spy.callCount).to.equal(1)
    response = await app.inject({ url: '/example.js' })
    expect(response.statusCode).to.equal(200)
    expect(spy.callCount).to.equal(1)
  })
})