/*
 *
 * @copyright Copyright (c) OX Software GmbH, Germany <info@open-xchange.com>
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OX App Suite. If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>.
 *
 * Any use of the work other than as authorized under this license or copyright law is prohibited.
 *
 */

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 () {
    await mockConfig({ baseUrls: ['http://ui-server/'] })
    await 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)
  })
})