Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
update-version_test.js 5.75 KiB
import request from 'supertest'
import { expect } from 'chai'
import { generateSimpleViteManifest, mockApp, mockConfig, mockFetch } from '../spec/util.js'
import { client, closeQueue, getQueue, getQueues, pubClient } from '../src/redis.js'
import * as td from 'testdouble'
import { getRedisKey } from '../src/util.js'

describe('Updates the version', function () {
  let app

  beforeEach(async function () {
    // need to set the redis-prefix. Otherwise, the bull workers will interfere
    process.env.REDIS_PREFIX = Math.random().toString()
    await client.flushdb()
    mockConfig({ urls: ['http://ui-server/'] })
    mockFetch({
      'http://ui-server': {
        '/manifest.json': generateSimpleViteManifest({
          'index.html': {}
        }),
        '/index.html': () => new Response('<html><head></head><body>it\'s me</body></html>', { headers: { 'content-type': 'text/html' } }),
        '/meta.json': td.when(td.func()(td.matchers.anything())).thenReturn(
          new Response(JSON.stringify({ commitSha: '1' }), { headers: { 'Content-Type': 'application/json' } }),
          new Response(JSON.stringify({ commitSha: '2' }), { headers: { 'Content-Type': 'application/json' } }),
          new Response(JSON.stringify({ commitSha: '2' }), { headers: { 'Content-Type': 'application/json' } })
        )
      }
    })
    app = await mockApp()
  })

  afterEach(async function () {
    td.reset()
    process.env.CACHE_TTL = '30000'
    for (const queue of getQueues()) {
      await closeQueue(queue.name)
    }
    // reset, after the queues were removed
    process.env.REDIS_PREFIX = 'ui-middleware'
  })

  it('with manually triggered job', async function () {
    // need to do this with dynamic import such that the mocked config is used
    await import('../src/create-queues.js').then(({ default: createQueues }) => createQueues())

    const responseBeforeUpdate = await request(app.server).get('/index.html')
    expect(responseBeforeUpdate.statusCode).to.equal(200)
    expect(responseBeforeUpdate.headers.version).to.equal('85101541')

    // update has only been registered but not executed yet
    expect(await getQueue('update-version').add({}).then(job => job.finished())).to.equal('85101541')
    // update is executed with the second iteration
    expect(await getQueue('update-version').add({}).then(job => job.finished())).to.equal('85102502')

    const responseAfterUpdate = await request(app.server).get('/index.html')
    expect(responseAfterUpdate.statusCode).to.equal(200)
    expect(responseAfterUpdate.headers.version).to.equal('85102502')
  })

  it('with automatically triggered job', async function () {
    process.env.CACHE_TTL = '100'

    const responseBeforeUpdate = await request(app.server).get('/index.html')
    expect(responseBeforeUpdate.statusCode).to.equal(200)
    expect(responseBeforeUpdate.headers.version).to.equal('85101541')

    // need to do this with dynamic import such that the mocked config is used
    await import('../src/create-queues.js').then(({ default: createQueues }) => createQueues())

    const queue = getQueue('update-version')
    let count = 0
    await new Promise(resolve => queue.on('global:completed', (jobId, result) => {
      // only resolve when the second job has been completed as the "update" job needs to be executed twice
      if (++count === 1) return
      // pause the queue to prevent any further updates
      queue.pause()
      resolve()
    }))

    const responseAfterUpdate = await request(app.server).get('/index.html')
    expect(responseAfterUpdate.statusCode).to.equal(200)
    expect(responseAfterUpdate.headers.version).to.equal('85102502')
  })

  it('receives version update via redis event', async function () {
    // need to do this with dynamic import such that the mocked config is used
    await import('../src/create-queues.js').then(({ default: createQueues }) => createQueues())

    const responseBeforeUpdate = await request(app.server).get('/index.html')
    expect(responseBeforeUpdate.statusCode).to.equal(200)
    expect(responseBeforeUpdate.headers.version).to.equal('85101541')

    // just publish event, don't change the value on redis.
    pubClient.publish(getRedisKey({ name: 'updateLatestVersion' }), '1234')
    await new Promise(resolve => setTimeout(resolve, 10))

    const responseAfterUpdate = await request(app.server).get('/index.html')
    expect(responseAfterUpdate.statusCode).to.equal(200)
    expect(responseAfterUpdate.headers.version).to.equal('1234')
  })

  describe('with initial version', function () {
    beforeEach(async function () {
      td.reset()
      // need to set the redis-prefix. Otherwise, the bull workers will interfere
      process.env.REDIS_PREFIX = Math.random().toString()
      await client.flushdb()
      // preconfigure redis
      await client.set(getRedisKey({ name: 'latestVersion' }), '12345')
      mockConfig({ urls: ['http://ui-server/'] })
      mockFetch({
        'http://ui-server': {
          '/manifest.json': generateSimpleViteManifest({
            'index.html': {}
          }),
          '/index.html': () => new Response('<html><head></head><body>it\'s me</body></html>', { headers: { 'content-type': 'text/html' } }),
          '/meta.json': td.when(td.func()(td.matchers.anything())).thenReturn(
            new Response(JSON.stringify({ commitSha: '1' }), { headers: { 'Content-Type': 'application/json' } }),
            new Response(JSON.stringify({ commitSha: '2' }), { headers: { 'Content-Type': 'application/json' } })
          )
        }
      })
      app = await mockApp()
    })

    it('uses version from redis if present', async function () {
      app = await mockApp()

      const response = await request(app.server).get('/index.html')
      expect(response.statusCode).to.equal(200)
      expect(response.headers.version).to.equal('12345')
    })
  })
})