Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
malteish committed Sep 4, 2024
1 parent 374c484 commit a5091f3
Showing 1 changed file with 11 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getMetrics } from './getMetrics';
import { Redis, RedisPrefix } from '../getDatabase';
import { MetricsMap, IntervalMetric } from './metricTypes';
import { MetricsObject, IntervalMetric } from './metricTypes';

describe('getMetrics', () => {
let mockRedis: jest.Mocked<Redis>;
Expand All @@ -12,28 +12,20 @@ describe('getMetrics', () => {
} as unknown as jest.Mocked<Redis>;
});

it('should return an empty map when no metrics are found', async () => {
it('should return an empty object when no metrics are found', async () => {
mockRedis.keys.mockResolvedValue([]);

const getMetricsFunc = getMetrics(mockRedis);
const result = await getMetricsFunc();

expect(result).toBeInstanceOf(Map);
expect(result.size).toBe(0);
expect(result).toEqual({});
});

it('should correctly parse and return metrics', async () => {
const timestamp = Math.floor(Date.now() / 1000);
const timestamp = Math.floor(Date.now() / 1000) - 86400; // Use yesterday's timestamp
const mockKeys = [`${RedisPrefix.MetricsMessageCount}${timestamp}`];

console.log('timestamp: ', timestamp);
console.log('mockKeys: ', mockKeys);

const date = new Date(timestamp * 1000);
console.log('date: ', date);

mockRedis.keys.mockResolvedValue(mockKeys);
// @ts-ignore
mockRedis.get.mockImplementation((key: string) => {
if (key.includes(RedisPrefix.MetricsMessageCount))
return Promise.resolve('10');
Expand All @@ -47,27 +39,10 @@ describe('getMetrics', () => {
const getMetricsFunc = getMetrics(mockRedis);
const result = await getMetricsFunc();

expect(result).toBeInstanceOf(Map);
expect(result.size).toBe(1);

console.log('result: ', result);

const expectedDate = new Date(timestamp * 1000);

// Find the key that matches our expected date
// javascript Date are checked by reference, not value, which is why
// we need to do this.
const matchingKey = Array.from(result.keys()).find(
(key) => key.getTime() === expectedDate.getTime(),
);

if (!matchingKey) {
throw new Error('Matching key not found');
}

const metric = result.get(matchingKey);
expect(Object.keys(result).length).toBe(1);

console.log('metric: ', metric);
const expectedDate = new Date(timestamp * 1000).toISOString();
const metric = result[expectedDate];

expect(metric).toEqual({
messageCount: 10,
Expand All @@ -77,7 +52,7 @@ describe('getMetrics', () => {
});

it('should handle missing metric values', async () => {
const timestamp = Math.floor(Date.now() / 1000);
const timestamp = Math.floor(Date.now() / 1000) - 86400; // Use yesterday's timestamp
const mockKeys = [`${RedisPrefix.MetricsMessageCount}${timestamp}`];

mockRedis.keys.mockResolvedValue(mockKeys);
Expand All @@ -86,11 +61,10 @@ describe('getMetrics', () => {
const getMetricsFunc = getMetrics(mockRedis);
const result = await getMetricsFunc();

expect(result).toBeInstanceOf(Map);
expect(result.size).toBe(1);
expect(Object.keys(result).length).toBe(1);

const expectedDate = Array.from(result.keys())[0];
const metric = result.get(expectedDate);
const expectedDate = new Date(timestamp * 1000).toISOString();
const metric = result[expectedDate];

expect(metric).toEqual({
messageCount: 0,
Expand Down

0 comments on commit a5091f3

Please sign in to comment.