forked from Devsol-01/Nestera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.service.spec.ts
More file actions
153 lines (134 loc) · 4.8 KB
/
Copy pathanalytics.service.spec.ts
File metadata and controls
153 lines (134 loc) · 4.8 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { AnalyticsService } from './analytics.service';
import { User } from '../user/entities/user.entity';
import { ProcessedStellarEvent } from '../blockchain/entities/processed-event.entity';
import { LedgerTransaction } from '../blockchain/entities/transaction.entity';
import { SavingsService as BlockchainSavingsService } from '../blockchain/savings.service';
import { StellarService } from '../blockchain/stellar.service';
import { OracleService } from '../blockchain/oracle.service';
import { PortfolioTimeframe } from './dto/portfolio-timeline-query.dto';
describe('AnalyticsService', () => {
let service: AnalyticsService;
let userRepository: { findOne: jest.Mock };
let eventRepository: { find: jest.Mock };
let transactionRepository: { find: jest.Mock };
let blockchainSavingsService: { getUserSavingsBalance: jest.Mock };
let stellarService: { getHorizonServer: jest.Mock };
let oracleService: {
convertXLMToUsd: jest.Mock;
convertToUsd: jest.Mock;
convertAQUAToUsd: jest.Mock;
getXLMPrice: jest.Mock;
};
beforeEach(async () => {
userRepository = {
findOne: jest.fn(),
};
eventRepository = {
find: jest.fn(),
};
transactionRepository = {
find: jest.fn(),
};
blockchainSavingsService = {
getUserSavingsBalance: jest.fn(),
};
stellarService = {
getHorizonServer: jest.fn(),
};
oracleService = {
convertXLMToUsd: jest.fn(),
convertToUsd: jest.fn(),
convertAQUAToUsd: jest.fn(),
getXLMPrice: jest.fn().mockResolvedValue(0.12),
};
const module: TestingModule = await Test.createTestingModule({
providers: [
AnalyticsService,
{
provide: getRepositoryToken(User),
useValue: userRepository,
},
{
provide: getRepositoryToken(ProcessedStellarEvent),
useValue: eventRepository,
},
{
provide: getRepositoryToken(LedgerTransaction),
useValue: transactionRepository,
},
{
provide: BlockchainSavingsService,
useValue: blockchainSavingsService,
},
{
provide: StellarService,
useValue: stellarService,
},
{
provide: OracleService,
useValue: oracleService,
},
],
}).compile();
service = module.get<AnalyticsService>(AnalyticsService);
});
it('calculates 1W portfolio timeline correctly by working backward', async () => {
const userId = 'user-1';
const publicKey = 'GABC123';
const now = new Date('2024-03-24T12:00:00Z');
jest.useFakeTimers().setSystemTime(now);
userRepository.findOne.mockResolvedValue({ id: userId, publicKey });
blockchainSavingsService.getUserSavingsBalance.mockResolvedValue({
total: 1000,
});
// Events in reverse chronological order
eventRepository.find.mockResolvedValue([
{
eventType: 'Deposit',
eventData: { amount: 200, user: publicKey },
processedAt: new Date('2024-03-23T10:00:00Z'), // Yesterday
},
{
eventType: 'Withdrawal',
eventData: { amount: 100, user: publicKey },
processedAt: new Date('2024-03-22T10:00:00Z'), // 2 days ago
},
{
eventType: 'InterestAccrued',
eventData: { amount: 50, user: publicKey },
processedAt: new Date('2024-03-21T10:00:00Z'), // 3 days ago
},
]);
const result = await service.getPortfolioTimeline(
userId,
PortfolioTimeframe.WEEK,
);
// Expecting 7 data points (one per day)
expect(result).toHaveLength(7);
// Last point (today) should be current balance
expect(result[6].value).toBe(1000);
// Point 5 (yesterday balance before deposit)
// Balance(today) = 1000. Balance(yesterday) = 1000 - 200 = 800.
expect(result[5].value).toBe(1000); // Wait, my logic shows balance at the END of the period.
// My code:
// periodEnd = now - i * interval
// timeline.push({ date: periodEnd, value: runningBalance })
// runningBalance -= netChangeInPeriod
// Result[6] is i=0 (now): value 1000. runningBalance becomes 1000 - 0 = 1000.
// Result[5] is i=1 (now - 1d): value 1000. runningBalance becomes 1000 - 200 = 800.
// Result[4] is i=2 (now - 2d): value 800. runningBalance becomes 800 - (-100) = 900.
// Result[3] is i=3 (now - 3d): value 900. runningBalance becomes 900 - 50 = 850.
expect(result[6].value).toBe(1000);
expect(result[5].value).toBe(1000);
expect(result[4].value).toBe(800);
expect(result[3].value).toBe(900);
expect(result[2].value).toBe(850);
expect(result[1].value).toBe(850);
expect(result[0].value).toBe(850);
});
afterAll(() => {
jest.useRealTimers();
});
});