import request from 'supertest' import { expect } from 'chai' import { generateSimpleViteManifest, mockApp, mockConfig, mockFetch } from '../spec/util.js' import { Response } from 'node-fetch' 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() 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' } }) ) } }) 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).get('/index.html') expect(responseBeforeUpdate.statusCode).to.equal(200) expect(responseBeforeUpdate.headers.version).to.equal('85101541') const job = await getQueue('update-version').add({}) const newVersion = await job.finished() expect(newVersion).to.equal('85102502') const responseAfterUpdate = await request(app).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).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()) // pause the queue to prevent any further updates const queue = getQueue('update-version') await new Promise(resolve => queue.on('global:completed', (jobId, result) => { queue.pause() resolve() })) const responseAfterUpdate = await request(app).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).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).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() 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).get('/index.html') expect(response.statusCode).to.equal(200) expect(response.headers.version).to.equal('12345') }) }) })