forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.test.ts
More file actions
54 lines (47 loc) · 1.61 KB
/
Copy pathanalytics.test.ts
File metadata and controls
54 lines (47 loc) · 1.61 KB
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
const mockRedisStore = new Map<string, string>();
let mockLatestMetric: PlatformMetrics | null = null;
jest.mock('ioredis', () =>
jest.fn().mockImplementation(() => ({
set: jest.fn(async (key: string, value: string) => {
mockRedisStore.set(key, value);
return 'OK';
}),
get: jest.fn(async (key: string) => mockRedisStore.get(key) ?? null),
})),
);
jest.mock('@apla/clickhouse', () =>
jest.fn().mockImplementation(() => ({
insert: jest.fn(async (_table: string, metric: PlatformMetrics) => {
mockLatestMetric = metric;
return undefined;
}),
query: jest.fn(async () => (mockLatestMetric ? [mockLatestMetric] : [])),
})),
);
// Tests for analytics engine
import { cacheMetrics, getCachedMetrics } from './analytics.cache';
import { aggregateMetrics, getAggregatedMetrics } from './analytics.aggregate';
import { PlatformMetrics } from './analytics.metrics';
describe('Analytics Engine', () => {
const sampleMetric: PlatformMetrics = {
splitsPerSecond: 10,
activeUsers: 100,
paymentSuccessRate: 0.98,
averageSettlementTime: 2.5,
geographicDistribution: { US: 50, UK: 30, IN: 20 },
};
beforeEach(() => {
mockRedisStore.clear();
mockLatestMetric = null;
});
it('should cache metrics in Redis', async () => {
await cacheMetrics(sampleMetric);
const cached = await getCachedMetrics();
expect(cached).toEqual(sampleMetric);
});
it('should aggregate metrics in ClickHouse', async () => {
await aggregateMetrics(sampleMetric);
const aggregated = await getAggregatedMetrics();
expect(aggregated).toMatchObject(sampleMetric);
});
});