// node import * as assert from 'assert'; import sleep from 'sleep-promise'; // npm import * as rp from 'request-promise'; import { Server } from 'http'; import { config, log } from '../config'; import { Worker } from '../lib/workers/worker'; import { init } from '../server'; import { CacheManager, DynamodbCacheManager, ICacheManager, RedisCacheManager, } from '../lib/cache'; import { connectApiInit, createTokenVerifierMock, getOrCreateOrganization, IMockOrganization, } from './utils/zypline-mock'; import { ensureTestDynamoDBTable, flushDynamoDBTable } from './utils/dynamodb'; import { GenericRequestPromise } from '../utils/types'; describe('Segment Endpoint', () => { let mediaviewServer: { close: () => Promise }; let connectApi: Server; let videoWorker: Worker; let audioWorker: Worker; let cacheManagerInstance: ICacheManager; let organization: IMockOrganization; before(async () => { cacheManagerInstance = CacheManager.getInstance(); if (cacheManagerInstance instanceof RedisCacheManager) { await cacheManagerInstance.options.client.flushdbAsync(); } else if (cacheManagerInstance instanceof DynamodbCacheManager) { await ensureTestDynamoDBTable(); await flushDynamoDBTable(); } videoWorker = new Worker(config, config.rrtq.videoNamespace); audioWorker = new Worker(config, config.rrtq.audioNamespace); mediaviewServer = await init(createTokenVerifierMock(), 10 * 60 * 1000, 1); connectApi = connectApiInit(); organization = getOrCreateOrganization('test'); }); after(async () => { await videoWorker.stop(); await audioWorker.stop(); connectApi.close(); await mediaviewServer.close(); cacheManagerInstance.stopCacheGC(); CacheManager.destroyInstance(); }); it('should load a video segment', async () => { const response = await (rp.get({ url: 'http://localhost:3000/segment?fileId=70ad0ea2-5ff8-4c02-8f3d-16f985867151&virtualTrackId=458e622c-a57b-4436-bff7-f0efe6e4e52a&index=0', headers: { 'X-Organization-Slug': organization.slug, Authorization: 'Bearer smth smth smth', }, timeout: 10 * 60 * 1000, resolveWithFullResponse: true, }) as GenericRequestPromise<{ body: Buffer }, any>); assert.strictEqual(response.body.length, 98161); }); it('should load audio segment', async () => { const response = await (rp.get({ url: 'http://localhost:3000/segment?fileId=70ad0ea2-5ff8-4c02-8f3d-16f985867151&virtualTrackId=b86ccdd5-2739-4e13-9388-8bed7e0c1d3f&index=0', headers: { 'X-Organization-Slug': organization.slug, Authorization: 'Bearer smth smth smth', }, timeout: 10 * 60 * 1000, }) as GenericRequestPromise); // was 51848 assert.strictEqual(response.length, 54724); }); it('should load audio segment with custom audio mapping', async () => { const response = await (rp.get({ url: 'http://localhost:3000/segment?fileId=b232f209-f934-4d89-9e13-9eae62c17cb6&virtualTrackId=customAudio&customAudioTrack=%5B%5B%22743132cf-fe67-4827-8651-fd3b20c67955%22%2C%5B0%5D%2C%5B%22FL%22%5D%5D%2C%20%5B%22f23e1d54-e7ef-47fc-b58a-cc043a8653bd%22%2C%5B0%5D%2C%5B%22FR%22%5D%5D%5D&index=0', headers: { 'X-Organization-Slug': organization.slug, Authorization: 'Bearer smth smth smth', }, timeout: 10 * 60 * 1000, }) as GenericRequestPromise); // ToDo Test this. Seems weird this changed. assert.strictEqual(response.length, 21739); }); it('should load a ts segment from an ACES source', async () => { const response = await (rp.get({ url: 'http://localhost:3000/segment?fileId=6810b98e-8507-44ce-8c50-355912b40a15&virtualTrackId=51615358-455f-46fd-86be-b525d18b1e80&index=0', headers: { 'X-Organization-Slug': organization.slug, Authorization: 'Bearer smth smth smth', }, timeout: 10 * 60 * 1000, }) as GenericRequestPromise); log.info(`Length ${response.length}`); assert.ok(response.length >= 130000); }); it('should load an audio segment after it was deleted from S3', async () => { const fileId = '70ad0ea2-5ff8-4c02-8f3d-16f985867151'; await rp.get({ url: `http://localhost:3000/segment?fileId=${fileId}&virtualTrackId=b86ccdd5-2739-4e13-9388-8bed7e0c1d3f&index=0`, headers: { 'X-Organization-Slug': organization.slug, Authorization: 'Bearer smth smth smth', }, timeout: 10 * 60 * 1000, }); await organization.clearS3Cache(); // wait at least 10 seconds so the rrtq task will expire await sleep(12000); const response = await (rp.get({ url: `http://localhost:3000/segment?fileId=${fileId}&virtualTrackId=b86ccdd5-2739-4e13-9388-8bed7e0c1d3f&index=0`, headers: { 'X-Organization-Slug': organization.slug, Authorization: 'Bearer smth smth smth', }, timeout: 10 * 60 * 1000, }) as GenericRequestPromise); // was 51848 assert.strictEqual(response.length, 54724); }); it('should load a video segment after it was deleted from S3', async () => { const fileId = '70ad0ea2-5ff8-4c02-8f3d-16f985867151'; await rp.get({ url: `http://localhost:3000/segment?fileId=${fileId}&virtualTrackId=458e622c-a57b-4436-bff7-f0efe6e4e52a&index=0`, headers: { 'X-Organization-Slug': organization.slug, Authorization: 'Bearer smth smth smth', }, timeout: 10 * 60 * 1000, }); await organization.clearS3Cache(); await sleep(30000); const response = await (rp.get({ url: `http://localhost:3000/segment?fileId=${fileId}&virtualTrackId=458e622c-a57b-4436-bff7-f0efe6e4e52a&index=0`, headers: { 'X-Organization-Slug': organization.slug, Authorization: 'Bearer smth smth smth', }, timeout: 10 * 60 * 1000, }) as GenericRequestPromise); assert.strictEqual(response.length, 98161); }); });