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

describe('UI Middleware', function () {
  let app
  let fetchConfig

  beforeEach(async function () {
    mockConfig({ baseUrls: ['http://ui-server/'] })
    mockRedis()
    mockFetch(fetchConfig = {
      'http://ui-server': {
        '/manifest.json': generateSimpleViteManifest({ 'example.js': 'test' }),
        '/example.js': ''
      }
    })
    app = await injectApp()
  })

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

  it('fetches manifest data', async function () {
    const response = await app.inject({ url: '/manifests' })
    const body = await decompressBrotli(response.rawPayload)
    expect(response.statusCode).to.equal(200)
    expect(response.headers['content-encoding']).to.equal('br')
    expect(body).to.deep.equal([{ namespace: 'test', path: 'example' }])
  })

  it('caches manifest data when configuration changes', async function () {
    const response = await app.inject({ url: '/manifests' })
    const body = await decompressBrotli(response.rawPayload)
    expect(response.statusCode).to.equal(200)
    expect(response.headers['content-encoding']).to.equal('br')
    expect(body).to.deep.equal([{ namespace: 'test', path: 'example' }])

    fetchConfig['http://ui-server'] = {
      '/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }),
      '/example.js': ''
    }

    await wait(150)

    const response2 = await app.inject({ url: '/manifests' })
    const body2 = await decompressBrotli(response.rawPayload)
    expect(response2.statusCode).to.equal(200)
    expect(response2.headers['content-encoding']).to.equal('br')
    expect(body2).to.deep.equal([{ namespace: 'test', path: 'example' }])
  })

  describe('multiple configurations', function () {
    beforeEach(async function () {
      mockConfig({ baseUrls: ['http://ui-server/', 'http://ui-server2/'] })
      fetchConfig['http://ui-server2'] = {
        '/manifest.json': generateSimpleViteManifest({ 'example2.js': 'thing' }),
        '/example2.js': ''
      }
      app = await injectApp()
    })

    it('can load multiple configurations', async function () {
      const response = await app.inject({ url: '/manifests' })
      const body = await decompressBrotli(response.rawPayload)
      expect(response.statusCode).to.equal(200)
      expect(response.headers['content-encoding']).to.equal('br')
      expect(body).to.deep.equal([
        { namespace: 'test', path: 'example' },
        { namespace: 'thing', path: 'example2' }
      ])
    })
  })
})