|
| 1 | +import type { Server } from 'node:http'; |
| 2 | +import { brotliCompress as brotliCompressCallback, constants as zlibConstants } from 'node:zlib'; |
| 3 | +import { promisify } from 'node:util'; |
| 4 | +import request, { Agent } from 'supertest'; |
| 5 | +import Bluebird from 'bluebird'; |
| 6 | +import { expect } from 'chai'; |
| 7 | + |
| 8 | +import { getTestServer } from '../../../utils/server.js'; |
| 9 | +import { measurementStoreClient } from '../../../../src/lib/sql/client.js'; |
| 10 | +import { getMeasurementRedisClient } from '../../../../src/lib/redis/measurement-client.js'; |
| 11 | +import { generateMeasurementId, roundIdTime } from '../../../../src/measurement/id.js'; |
| 12 | +import { getMeasurementKey } from '../../../../src/measurement/store.js'; |
| 13 | + |
| 14 | +const brotliCompress = promisify(brotliCompressCallback); |
| 15 | + |
| 16 | +describe('Get measurement', () => { |
| 17 | + let app: Server; |
| 18 | + let requestAgent: Agent; |
| 19 | + |
| 20 | + const buildMeasurementRecord = (id: string, time: Date) => { |
| 21 | + return { |
| 22 | + id, |
| 23 | + type: 'ping', |
| 24 | + status: 'finished', |
| 25 | + createdAt: time.toISOString(), |
| 26 | + updatedAt: time.toISOString(), |
| 27 | + target: 'example.com', |
| 28 | + probesCount: 1, |
| 29 | + results: [], |
| 30 | + }; |
| 31 | + }; |
| 32 | + |
| 33 | + before(async () => { |
| 34 | + app = await getTestServer(); |
| 35 | + requestAgent = request(app); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('errors', () => { |
| 39 | + it('should respond with a 404 for a non-existing measurement id', async () => { |
| 40 | + const nonExisting = generateMeasurementId(new Date()); |
| 41 | + |
| 42 | + await requestAgent |
| 43 | + .get(`/v1/measurements/${nonExisting}`) |
| 44 | + .expect(404) |
| 45 | + .expect((response) => { |
| 46 | + expect(response.body.error).to.include({ |
| 47 | + message: 'Couldn\'t find the requested measurement.', |
| 48 | + type: 'not_found', |
| 49 | + }); |
| 50 | + |
| 51 | + expect(response).to.matchApiSchema(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should respond 404 for an invalid id format', async () => { |
| 56 | + await requestAgent |
| 57 | + .get('/v1/measurements/invalid-id-123') |
| 58 | + .expect(404) |
| 59 | + .expect((response) => { |
| 60 | + expect(response.body.error.type).to.equal('not_found'); |
| 61 | + expect(response).to.matchApiSchema(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('success (from Redis)', () => { |
| 67 | + const redisKeysToCleanup: string[] = []; |
| 68 | + |
| 69 | + afterEach(async () => { |
| 70 | + const redis = getMeasurementRedisClient(); |
| 71 | + |
| 72 | + await Bluebird.map(redisKeysToCleanup.splice(0), (key) => { |
| 73 | + return redis.del(key); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return measurement JSON stored in Redis', async () => { |
| 78 | + const now = new Date(); |
| 79 | + const id = generateMeasurementId(now); |
| 80 | + const key = getMeasurementKey(id); |
| 81 | + const record = buildMeasurementRecord(id, now); |
| 82 | + |
| 83 | + const redis = getMeasurementRedisClient(); |
| 84 | + await redis.json.set(key, '$', record); |
| 85 | + redisKeysToCleanup.push(key); |
| 86 | + |
| 87 | + await requestAgent |
| 88 | + .get(`/v1/measurements/${id}`) |
| 89 | + .expect(200) |
| 90 | + .expect((response) => { |
| 91 | + expect(response.body).to.deep.equal(record); |
| 92 | + expect(response).to.matchApiSchema(); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('success (from Postgres offload)', () => { |
| 98 | + const redisKeysToCleanup: string[] = []; |
| 99 | + const dbRowsToCleanup: { table: string; id: string; createdAt: Date }[] = []; |
| 100 | + |
| 101 | + afterEach(async () => { |
| 102 | + const redis = getMeasurementRedisClient(); |
| 103 | + |
| 104 | + await Bluebird.map(redisKeysToCleanup.splice(0), (key) => { |
| 105 | + return redis.del(key); |
| 106 | + }); |
| 107 | + |
| 108 | + await Bluebird.map(dbRowsToCleanup.splice(0), (row) => { |
| 109 | + return measurementStoreClient(row.table) |
| 110 | + .where({ id: row.id, createdAt: row.createdAt }) |
| 111 | + .delete(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should return measurement JSON from the offloaded store when likely offloaded and older than 30 minutes', async () => { |
| 116 | + // Create an ID in the past (> 30 minutes) |
| 117 | + const createdAt = new Date(Date.now() - 40 * 60_000); |
| 118 | + const id = generateMeasurementId(createdAt); |
| 119 | + const roundedCreatedAt = roundIdTime(new Date(createdAt)); |
| 120 | + |
| 121 | + // Seed Postgres offload table for anonymous tier |
| 122 | + const table = 'measurement_anonymous'; |
| 123 | + const record = buildMeasurementRecord(id, createdAt); |
| 124 | + const compressed = await brotliCompress(JSON.stringify(record), { params: { [zlibConstants.BROTLI_PARAM_QUALITY]: 1 } }); |
| 125 | + await measurementStoreClient(table).insert({ id, createdAt: new Date(roundedCreatedAt), data: compressed }); |
| 126 | + dbRowsToCleanup.push({ table, id, createdAt: new Date(roundedCreatedAt) }); |
| 127 | + |
| 128 | + // Ensure Redis does not have this key |
| 129 | + const key = getMeasurementKey(id); |
| 130 | + const redis = getMeasurementRedisClient(); |
| 131 | + await redis.del(key); |
| 132 | + |
| 133 | + await requestAgent |
| 134 | + .get(`/v1/measurements/${id}`) |
| 135 | + .expect(200) |
| 136 | + .expect((response) => { |
| 137 | + expect(response.body).to.deep.equal(record); |
| 138 | + expect(response).to.matchApiSchema(); |
| 139 | + }); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments