forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresilience.test.js
More file actions
206 lines (161 loc) · 6.49 KB
/
Copy pathresilience.test.js
File metadata and controls
206 lines (161 loc) · 6.49 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
jest.mock('../src/logger', () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
}));
const mockCacheGet = jest.fn();
const mockCacheSet = jest.fn();
const mockIsConnected = jest.fn();
jest.mock('../src/services/cache', () => ({
get: mockCacheGet,
set: mockCacheSet,
del: jest.fn(),
getClient: jest.fn(() => ({ scan: jest.fn(async () => ['0', []]) })),
isConnected: mockIsConnected,
}));
const mockStellarFetch = jest.fn();
const mockCoingeckoFetch = jest.fn();
const mockCmcFetch = jest.fn();
jest.mock('../src/services/sources/stellarDex', () => ({ fetchPrice: mockStellarFetch }));
jest.mock('../src/services/sources/coingecko', () => ({ fetchPrice: mockCoingeckoFetch }));
jest.mock('../src/services/sources/coinmarketcap', () => ({ fetchPrice: mockCmcFetch }));
const logger = require('../src/logger');
const priceOracle = require('../src/services/priceOracle');
beforeEach(() => {
mockCacheGet.mockReset();
mockCacheSet.mockReset();
mockIsConnected.mockReset();
mockStellarFetch.mockReset();
mockCoingeckoFetch.mockReset();
mockCmcFetch.mockReset();
priceOracle.resetCircuitBreakers();
logger.info.mockClear();
logger.warn.mockClear();
logger.error.mockClear();
// Default: sources return a price
mockStellarFetch.mockResolvedValue(0.10);
mockCoingeckoFetch.mockResolvedValue(null);
mockCmcFetch.mockResolvedValue(null);
});
describe('cache.get failure — falls back to source fetch', () => {
test('returns price data when cache.get throws', async () => {
mockCacheGet.mockRejectedValue(new Error('ECONNREFUSED'));
mockCacheSet.mockResolvedValue(undefined);
const result = await priceOracle.getPrice('XLM');
expect(result.price_usd).toBe(0.10);
expect(result.redis_unavailable).toBe(true);
});
test('sets redis_unavailable: true on cache.get error', async () => {
mockCacheGet.mockRejectedValue(new Error('ECONNREFUSED'));
const result = await priceOracle.getPrice('XLM');
expect(result.redis_unavailable).toBe(true);
});
test('logs a warning (not an error) on cache.get failure', async () => {
mockCacheGet.mockRejectedValue(new Error('Stream not writeable'));
await priceOracle.getPrice('XLM');
expect(logger.warn).toHaveBeenCalledWith(
expect.stringContaining('Cache read failed'),
expect.objectContaining({ error: 'Stream not writeable' })
);
expect(logger.error).not.toHaveBeenCalled();
});
test('does not throw — no unhandled rejection', async () => {
mockCacheGet.mockRejectedValue(new Error('ECONNREFUSED'));
await expect(priceOracle.getPrice('XLM')).resolves.toBeDefined();
});
});
describe('cache.set failure — logs warning, returns price anyway', () => {
test('returns price data when cache.set throws', async () => {
mockCacheGet.mockResolvedValue(null);
mockCacheSet.mockRejectedValue(new Error('ECONNREFUSED'));
const result = await priceOracle.getPrice('XLM');
expect(result.price_usd).toBe(0.10);
expect(result.redis_unavailable).toBe(true);
});
test('logs a warning on cache.set failure', async () => {
mockCacheGet.mockResolvedValue(null);
mockCacheSet.mockRejectedValue(new Error('offline queue full'));
await priceOracle.getPrice('XLM');
expect(logger.warn).toHaveBeenCalledWith(
expect.stringContaining('Cache write failed'),
expect.objectContaining({ error: 'offline queue full' })
);
});
test('does not throw when cache.set fails', async () => {
mockCacheGet.mockResolvedValue(null);
mockCacheSet.mockRejectedValue(new Error('ECONNREFUSED'));
await expect(priceOracle.getPrice('XLM')).resolves.toBeDefined();
});
});
describe('cache working normally', () => {
test('returns cached price with redis_unavailable: false', async () => {
mockCacheGet.mockResolvedValue({
price: 0.12,
source: 'stellar_dex',
fetchedAt: Date.now() - 30000,
sourcesAttempted: ['stellar_dex'],
});
const result = await priceOracle.getPrice('XLM');
expect(result.price_usd).toBe(0.12);
expect(result.redis_unavailable).toBe(false);
expect(mockStellarFetch).not.toHaveBeenCalled();
});
test('fetchFreshPrice sets redis_unavailable: false when cache.set succeeds', async () => {
mockCacheGet.mockResolvedValue(null);
mockCacheSet.mockResolvedValue(undefined);
const result = await priceOracle.fetchFreshPrice('XLM');
expect(result.redis_unavailable).toBe(false);
});
});
describe('all sources unavailable during Redis outage', () => {
test('returns null price with redis_unavailable: true', async () => {
mockCacheGet.mockRejectedValue(new Error('ECONNREFUSED'));
mockStellarFetch.mockResolvedValue(null);
mockCoingeckoFetch.mockResolvedValue(null);
mockCmcFetch.mockResolvedValue(null);
const result = await priceOracle.getPrice('XLM');
expect(result.price_usd).toBeNull();
expect(result.redis_unavailable).toBe(true);
expect(result.is_stale).toBe(true);
});
});
describe('price source circuit breakers', () => {
test('opens after repeated source failures and skips the failing source', async () => {
mockCacheGet.mockResolvedValue(null);
mockCacheSet.mockResolvedValue(undefined);
mockStellarFetch.mockResolvedValue(null);
mockCoingeckoFetch.mockResolvedValue(null);
mockCmcFetch.mockResolvedValue(null);
await priceOracle.fetchFreshPrice('XLM');
await priceOracle.fetchFreshPrice('XLM');
await priceOracle.fetchFreshPrice('XLM');
expect(priceOracle.getCircuitStates()).toMatchObject({
stellar_dex: 'open',
coingecko: 'open',
coinmarketcap: 'open',
});
mockStellarFetch.mockClear();
await priceOracle.fetchFreshPrice('XLM');
expect(mockStellarFetch).not.toHaveBeenCalled();
expect(logger.info).toHaveBeenCalledWith(
'Circuit breaker open, skipping source call',
expect.objectContaining({ source: 'stellar_dex', state: 'open' })
);
});
});
describe('refreshAllCachedPrices when Redis is down', () => {
test('skips refresh cycle when isConnected returns false', async () => {
mockIsConnected.mockReturnValue(false);
await priceOracle.refreshAllCachedPrices();
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Redis unavailable'));
expect(mockStellarFetch).not.toHaveBeenCalled();
});
});
describe('cache.isConnected', () => {
test('cache module exports isConnected function', () => {
const cache = require('../src/services/cache');
expect(typeof cache.isConnected).toBe('function');
});
});