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

describe('version mismatches', function () {
  let app

  beforeEach(async function () {
    await mockConfig({ baseUrls: ['http://ui-server/'] })
    await mockRedis()
    mockFetch({
      'http://ui-server': {
        '/manifest.json': generateSimpleViteManifest({
          'foo.js': { },
          'bar.js': { }
        }),
        '/foo.js': td.when(td.func()(td.matchers.anything(), td.matchers.anything())).thenReturn(
          new Response('foo1', { headers: { version: '1' } }),
          new Response('foo2', { headers: { version: '2' } })
        ),
        '/bar.js': () => new Response('bar', { headers: { version: '2' } }),
        '/whatever.js': () => new Response('whatever', { headers: { version: '2' } }),
        '/meta.json': td.when(td.func()(td.matchers.anything(), td.matchers.anything())).thenReturn(
          new Response(JSON.stringify({ }), { headers: { 'Content-Type': 'application/json', version: '1' } }),
          new Response(JSON.stringify({ }), { headers: { 'Content-Type': 'application/json', version: '2' } })
        )
      }
    })
    app = await injectApp()
  })

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

  it('detects version mismatches when files are fetched', async function () {
    // get foo.js with initial version
    let response = await app.inject({ url: '/foo.js' })
    expect(response.statusCode).to.equal(200)
    expect(response.body).to.equal('foo1')
    expect(response.headers.version).to.equal('85101541')

    // get bar.js. This will cause the server to detect the version mismatch
    response = await app.inject({ url: '/bar.js' })
    expect(response.statusCode).to.equal(404)

    // get foo.js again. Since the versions should coincide now, the client should receive the new file
    response = await app.inject({ url: '/foo.js' })
    expect(response.statusCode).to.equal(200)
    expect(response.body).to.equal('foo2')
    expect(response.headers.version).to.equal('85102502')
  })

  it('detects version mismatches in files not referenced in manifest.json when files are fetched', async function () {
    // get foo.js with initial version
    let response = await app.inject({ url: '/foo.js' })
    expect(response.statusCode).to.equal(200)
    expect(response.body).to.equal('foo1')
    expect(response.headers.version).to.equal('85101541')

    // get bar.js. This will cause the server to detect the version mismatch
    response = await app.inject({ url: '/whatever.js' })
    expect(response.statusCode).to.equal(404)

    // get foo.js again. Since the versions should coincide now, the client should receive the new file
    response = await app.inject({ url: '/foo.js' })
    expect(response.statusCode).to.equal(200)
    expect(response.body).to.equal('foo2')
    expect(response.headers.version).to.equal('85102502')
  })
})