forked from InsurNiffy/niff-Stellar-shurance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolvency-monitoring.service.spec.ts
More file actions
156 lines (137 loc) · 5.48 KB
/
Copy pathsolvency-monitoring.service.spec.ts
File metadata and controls
156 lines (137 loc) · 5.48 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
154
155
156
import { Test, TestingModule } from '@nestjs/testing';
import { ConfigService } from '@nestjs/config';
import axios from 'axios';
import { SolvencyMonitoringService } from './solvency-monitoring.service';
import { PrismaService } from '../prisma/prisma.service';
import { SorobanService } from '../rpc/soroban.service';
import { RedisService } from '../cache/redis.service';
import { SOLVENCY_SNAPSHOT_REDIS_KEY } from './solvency.constants';
jest.mock('axios', () => ({
__esModule: true,
default: {
post: jest.fn().mockResolvedValue({ status: 200 }),
},
}));
const axiosPost = axios.post as jest.Mock;
describe('SolvencyMonitoringService', () => {
let service: SolvencyMonitoringService;
let prisma: { $queryRaw: jest.Mock };
let soroban: { simulateGetTreasuryBalance: jest.Mock };
let redis: { get: jest.Mock; set: jest.Mock };
let config: Record<string, string>;
beforeEach(async () => {
jest.clearAllMocks();
config = {
SOLVENCY_MONITORING_ENABLED: 'true',
SOLVENCY_BUFFER_THRESHOLD_STROOPS: '100',
SOLVENCY_SIMULATION_SOURCE_ACCOUNT: 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF',
CONTRACT_ID: 'CDEMO',
SOLVENCY_ALERT_WEBHOOK_URL: '',
SOLVENCY_ALERT_WEBHOOK_SECRET: '',
SOLVENCY_TENANT_ID: '',
};
prisma = {
$queryRaw: jest.fn().mockResolvedValue([{ s: '500' }]),
};
soroban = {
simulateGetTreasuryBalance: jest.fn().mockResolvedValue({
balanceStroops: '1000',
minResourceFee: '100',
}),
};
redis = {
get: jest.fn(),
set: jest.fn().mockResolvedValue(undefined),
};
const module: TestingModule = await Test.createTestingModule({
providers: [
SolvencyMonitoringService,
{
provide: ConfigService,
useValue: {
get: (key: string, def?: string) =>
Object.prototype.hasOwnProperty.call(config, key)
? config[key as keyof typeof config]
: def,
},
},
{ provide: PrismaService, useValue: prisma },
{ provide: SorobanService, useValue: soroban },
{ provide: RedisService, useValue: redis },
],
}).compile();
service = module.get(SolvencyMonitoringService);
});
describe('getLatestSnapshot', () => {
it('reads Redis only', async () => {
const snap = { status: 'ok' as const, checkedAt: 't', thresholdStroops: '0', alertEmitted: false };
redis.get.mockResolvedValueOnce(snap);
await expect(service.getLatestSnapshot()).resolves.toEqual(snap);
expect(redis.get).toHaveBeenCalledWith(SOLVENCY_SNAPSHOT_REDIS_KEY);
expect(soroban.simulateGetTreasuryBalance).not.toHaveBeenCalled();
});
});
describe('runSolvencyCheck', () => {
it('sets unknown when RPC fails without emitting buffer-low alert or webhook', async () => {
config.SOLVENCY_ALERT_WEBHOOK_URL = 'https://hooks.example/solvency';
soroban.simulateGetTreasuryBalance.mockRejectedValueOnce(new Error('rpc down'));
const snap = await service.runSolvencyCheck();
expect(snap.status).toBe('unknown');
expect(snap.alertEmitted).toBe(false);
expect(snap.rpcError).toContain('rpc down');
expect(snap.outstandingApprovedStroops).toBe('500');
expect(redis.set).toHaveBeenCalled();
expect(axiosPost).not.toHaveBeenCalled();
});
it('sets degraded and calls webhook when buffer is below threshold', async () => {
config.SOLVENCY_ALERT_WEBHOOK_URL = 'https://hooks.example/solvency';
config.SOLVENCY_ALERT_WEBHOOK_SECRET = 'secret';
prisma.$queryRaw.mockResolvedValueOnce([{ s: '950' }]);
soroban.simulateGetTreasuryBalance.mockResolvedValueOnce({
balanceStroops: '1000',
minResourceFee: '100',
});
const snap = await service.runSolvencyCheck();
expect(snap.status).toBe('degraded');
expect(snap.bufferStroops).toBe('50');
expect(snap.alertEmitted).toBe(true);
expect(axiosPost).toHaveBeenCalledWith(
'https://hooks.example/solvency',
expect.objectContaining({
event: 'solvency_buffer_low',
severity: 'critical',
bufferStroops: '50',
}),
expect.objectContaining({
headers: { 'X-Webhook-Secret': 'secret' },
}),
);
});
it('sets ok when buffer meets threshold', async () => {
prisma.$queryRaw.mockResolvedValueOnce([{ s: '200' }]);
soroban.simulateGetTreasuryBalance.mockResolvedValueOnce({
balanceStroops: '1000',
minResourceFee: '100',
});
const snap = await service.runSolvencyCheck();
expect(snap.status).toBe('ok');
expect(snap.bufferStroops).toBe('800');
expect(snap.alertEmitted).toBe(false);
expect(axiosPost).not.toHaveBeenCalled();
});
it('sets unknown when simulation source account is missing', async () => {
config.SOLVENCY_SIMULATION_SOURCE_ACCOUNT = '';
const snap = await service.runSolvencyCheck();
expect(snap.status).toBe('unknown');
expect(snap.skipReason).toContain('SOLVENCY_SIMULATION_SOURCE_ACCOUNT');
expect(soroban.simulateGetTreasuryBalance).not.toHaveBeenCalled();
});
it('sets unknown when monitoring disabled', async () => {
config.SOLVENCY_MONITORING_ENABLED = 'false';
const snap = await service.runSolvencyCheck();
expect(snap.status).toBe('unknown');
expect(snap.skipReason).toContain('SOLVENCY_MONITORING_ENABLED');
expect(prisma.$queryRaw).not.toHaveBeenCalled();
});
});
});