All files / redis-smq-monitor/tests/common websocket-event.ts

100% Statements 30/30
85.71% Branches 6/7
100% Functions 3/3
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 6220x 20x 20x 20x   20x                           20x           13x 13x 13x 13x 13x 130x 130x   13x 284x 13x 13x 271x   13x     20x 8x 8x 8x 8x   8x 8x 8x 8x 8x       8x          
import { startWebsocketRateStreamWorker } from './websocket-rate-stream-worker';
import { delay } from 'bluebird';
import { startMonitorServer } from './monitor-server';
import * as supertest from 'supertest';
import { TimeSeriesResponseBodyDTO } from '../../src/common/dto/time-series/time-series-response.DTO';
import { getRedisInstance } from './redis-clients';
import { TTimeSeriesRange } from '../../types';
 
export interface ISuperTestResponse<TData> extends supertest.Response {
  body: {
    data?: TData;
    error?: {
      code: string;
      message: string;
      details: Record<string, any>;
    };
  };
}
 
export async function listenForWebsocketStreamEvents<
  TPayload = TTimeSeriesRange,
>(
  streamName: string,
  startFn: () => Promise<void> = startWebsocketRateStreamWorker,
) {
  await startFn();
  const subscribeClient = await getRedisInstance();
  subscribeClient.subscribe(streamName);
  const data: { ts: number; payload: TPayload }[] = [];
  subscribeClient.on('message', (channel, message) => {
    const payload: TPayload = JSON.parse(message);
    data.push({ ts: Date.now(), payload });
  });
  for (; true; ) {
    if (data.length === 10) {
      subscribeClient.unsubscribe(streamName);
      break;
    } else await delay(500);
  }
  return data;
}
 
export async function validateTimeSeriesFrom(url: string) {
  await startMonitorServer();
  const request = supertest('http://127.0.0.1:3000');
  const timestamp = Math.ceil(Date.now() / 1000);
  const from = timestamp - 60;
  const response1: ISuperTestResponse<TimeSeriesResponseBodyDTO['data']> =
    await request.get(`${url}?from=${from}&to=${timestamp}`);
  expect(response1.statusCode).toBe(200);
  const data = response1.body.data ?? [];
  expect(data.length).toEqual(60);
  expect(data[0]).toEqual({
    timestamp: from,
    value: 0,
  });
  expect(data[59]).toEqual({
    timestamp: timestamp - 1,
    value: 0,
  });
}