Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
server_test.js 5.44 KiB
import request from 'supertest'
import { generateSimpleViteManifest, mockApp, mockConfig, mockFetch } from './util.js'
import { expect } from 'chai'
import * as td from 'testdouble'
import { Response } from 'node-fetch'

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

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

  afterEach(function () {
    td.reset()
    process.env.CACHE_TTL = 30000
  })

  // Some say, this is not necessary to test
  // But failing startups due to code errors in the probes will cause this to not work
  // Therefore, we should keep this
  it('is healthy', async function () {
    const response = await request(app).get('/healthy')
    expect(response.statusCode).to.equal(200)
  })

  it('fetches manifest data', async function () {
    const response = await request(app).get('/manifests')
    expect(response.statusCode).to.equal(200)
    expect(response.body).to.deep.equal([{ namespace: 'test', path: 'example' }])
  })

  describe('when configuration changes', function () {
    let prevConfig
    beforeEach(async function () {
      prevConfig = fetchConfig['http://ui-server']
    })

    afterEach(function () {
      fetchConfig['http://ui-server'] = prevConfig
    })

    it('caches manifest data', async function () {
      const response = await request(app).get('/manifests')
      expect(response.statusCode).to.equal(200)
      expect(response.body).to.deep.equal([{ namespace: 'test', path: 'example' }])

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

      await new Promise(resolve => setTimeout(resolve, 150))

      const response2 = await request(app).get('/manifests')
      expect(response2.statusCode).to.equal(200)
      expect(response2.body).to.deep.equal([{ namespace: 'test', path: 'example' }])
    })

    it('refreshes manifest data after caching timeout', async function () {
      process.env.CACHE_TTL = 0
      app = await mockApp()

      const response = await request(app).get('/manifests')
      expect(response.statusCode).to.equal(200)
      expect(response.body).to.deep.equal([{ namespace: 'test', path: 'example' }])

      const refreshedCache = new Promise(resolve => {
        fetchConfig['http://ui-server'] = {
          '/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }),
          '/example.js': () => {
            try {
              return new Response('new content')
            } finally {
              resolve()
            }
          }
        }
      })

      // trigger update
      await request(app).get('/manifests')
      // wait some time
      await refreshedCache

      const response2 = await request(app).get('/manifests')
      expect(response2.statusCode).to.equal(200)
      expect(response2.body).to.deep.equal([{ namespace: 'other', path: 'example' }])
    })

    it('only updates the version hash when the caches are warm', async function () {
      process.env.CACHE_TTL = 0

      // fetch the file to get the initial version
      let response = await request(app).get('/example.js')
      expect(response.text).to.equal('')
      const version = response.headers.version
      await new Promise(resolve => setTimeout(resolve, 1))

      // update resources
      let resolveExampleJs
      fetchConfig['http://ui-server'] = {
        '/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }),
        '/example.js': () => new Promise(resolve => (resolveExampleJs = resolve))
      }

      // fetch file again while the update is still processing
      // this will also trigger the update
      response = await request(app).get('/example.js')
      expect(response.text).to.equal('')
      expect(response.headers.version).to.equal(version)

      // fetch once again. this will not trigger an update of the file-cache
      response = await request(app).get('/example.js')
      expect(response.text).to.equal('')
      expect(response.headers.version).to.equal(version)

      // resolve the response to the example js file. This will finish the cache warmup
      resolveExampleJs(new Response('new content'))

      // fetch the file again. Content and version should be updated
      response = await request(app).get('/example.js')
      expect(response.text).to.equal('new content')
      expect(response.headers.version).not.to.equal(version)
    })
  })

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

    afterEach(function () {
      delete fetchConfig['http://ui-server2']
      app = prevApp
    })

    it('can load multiple configurations', async function () {
      await request(app)
        .get('/manifests')
        .then(response => {
          expect(response.statusCode).to.equal(200)
          expect(response.body).to.deep.equal([
            { namespace: 'test', path: 'example' },
            { namespace: 'thing', path: 'example2' }
          ])
        })
    })
  })
})