import request from 'supertest' import { expect } from 'chai' import { generateSimpleViteManifest, mockApp, mockConfig, mockFetch } from '../spec/util.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() mockConfig({ baseUrls: ['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(), 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' } }) ) } }) await import('../src/redis.js').then(({ client }) => client.flushdb()) app = await mockApp() }) afterEach(async function () { const { getQueues, closeQueue } = await import('../src/redis.js') for (const queue of getQueues()) { await closeQueue(queue.name) } td.reset() // reset, after the queues were removed process.env.REDIS_PREFIX = 'ui-middleware' }) it('with manually triggered job', async function () { const responseBeforeUpdate = await request(app.server).get('/index.html') expect(responseBeforeUpdate.statusCode).to.equal(200) expect(responseBeforeUpdate.headers.version).to.equal('85101541') const { getQueue } = await import('../src/redis.js') // 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 () { const responseBeforeUpdate = await request(app.server).get('/index.html') expect(responseBeforeUpdate.statusCode).to.equal(200) expect(responseBeforeUpdate.headers.version).to.equal('85101541') // speed up the update process const { subClient, getQueue } = await import('../src/redis.js') const queue = getQueue('update-version') queue.add({}, { jobId: 'update-version-job', repeat: { every: 100 } }) // wait for the update event to happen await new Promise(resolve => { const key = getRedisKey({ name: 'updateLatestVersion' }) subClient.subscribe(key) subClient.on('message', async (channel, version) => { if (channel !== key) return 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 () { const responseBeforeUpdate = await request(app.server).get('/index.html') expect(responseBeforeUpdate.statusCode).to.equal(200) expect(responseBeforeUpdate.headers.version).to.equal('85101541') const { pubClient } = await import('../src/redis.js') // 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() mockConfig({ baseUrls: ['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' } }) ) } }) const { client } = await import('../src/redis.js') await client.flushdb() // preconfigure redis await client.set(getRedisKey({ name: 'latestVersion' }), '12345') 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') }) }) })