|
1 | 1 | import { IBtInfo, IGetFeeEstimatesResponse } from 'beignet'; |
2 | | -import { getFees } from '../src/utils/lightning'; |
| 2 | +import { checkRgsHealth, getFees } from '../src/utils/lightning'; |
3 | 3 |
|
4 | 4 | jest.mock('../src/utils/wallet', () => ({ |
5 | 5 | getSelectedNetwork: jest.fn(() => 'bitcoin'), |
6 | 6 | })); |
7 | 7 |
|
| 8 | +jest.mock('../src/store/helpers', () => ({ |
| 9 | + getStore: jest.fn(() => ({ |
| 10 | + settings: { |
| 11 | + rapidGossipSyncUrl: 'https://rgs.blocktank.to/snapshots/', |
| 12 | + }, |
| 13 | + })), |
| 14 | +})); |
| 15 | + |
8 | 16 | describe('getFees', () => { |
9 | 17 | const MEMPOOL_URL = 'https://mempool.space/api/v1/fees/recommended'; |
10 | 18 | const BLOCKTANK_URL = 'https://api1.blocktank.to/api/info'; |
@@ -206,3 +214,170 @@ describe('getFees', () => { |
206 | 214 | }); |
207 | 215 | }); |
208 | 216 |
|
| 217 | +describe('checkRgsHealth', () => { |
| 218 | + const RGS_URL = 'https://rgs.blocktank.to/snapshots/'; |
| 219 | + |
| 220 | + beforeEach(() => { |
| 221 | + jest.clearAllMocks(); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should detect healthy RGS (< 24 hours old)', async () => { |
| 225 | + const nowSeconds = Math.floor(Date.now() / 1000); |
| 226 | + const recentTimestamp = nowSeconds - 3600 * 12; // 12 hours ago |
| 227 | + |
| 228 | + const mockHtml = ` |
| 229 | + <a href="snapshot__calculated-at%3A${recentTimestamp}__range%3A10800-scope.lngossip">snapshot</a> |
| 230 | + `; |
| 231 | + |
| 232 | + (global.fetch as jest.Mock) = jest.fn(() => |
| 233 | + Promise.resolve({ |
| 234 | + ok: true, |
| 235 | + text: () => Promise.resolve(mockHtml), |
| 236 | + }), |
| 237 | + ); |
| 238 | + |
| 239 | + const result = await checkRgsHealth(); |
| 240 | + |
| 241 | + expect(result.isOk()).toBe(true); |
| 242 | + if (result.isOk()) { |
| 243 | + expect(result.value.isHealthy).toBe(true); |
| 244 | + expect(result.value.timestamp).toBe(recentTimestamp); |
| 245 | + expect(result.value.ageHours).toBeGreaterThan(11); |
| 246 | + expect(result.value.ageHours).toBeLessThan(13); |
| 247 | + } |
| 248 | + expect(fetch).toHaveBeenCalledWith(RGS_URL); |
| 249 | + }); |
| 250 | + |
| 251 | + it('should detect stale RGS (> 24 hours old)', async () => { |
| 252 | + const nowSeconds = Math.floor(Date.now() / 1000); |
| 253 | + const staleTimestamp = nowSeconds - 3600 * 48; // 48 hours ago (2 days) |
| 254 | + |
| 255 | + const mockHtml = ` |
| 256 | + <a href="snapshot__calculated-at%3A${staleTimestamp}__range%3A10800-scope.lngossip">snapshot</a> |
| 257 | + `; |
| 258 | + |
| 259 | + (global.fetch as jest.Mock) = jest.fn(() => |
| 260 | + Promise.resolve({ |
| 261 | + ok: true, |
| 262 | + text: () => Promise.resolve(mockHtml), |
| 263 | + }), |
| 264 | + ); |
| 265 | + |
| 266 | + const result = await checkRgsHealth(); |
| 267 | + |
| 268 | + expect(result.isOk()).toBe(true); |
| 269 | + if (result.isOk()) { |
| 270 | + expect(result.value.isHealthy).toBe(false); |
| 271 | + expect(result.value.timestamp).toBe(staleTimestamp); |
| 272 | + expect(result.value.ageHours).toBeGreaterThan(47); |
| 273 | + expect(result.value.ageHours).toBeLessThan(49); |
| 274 | + } |
| 275 | + }); |
| 276 | + |
| 277 | + it('should handle RGS endpoint returning non-200 status', async () => { |
| 278 | + (global.fetch as jest.Mock) = jest.fn(() => |
| 279 | + Promise.resolve({ |
| 280 | + ok: false, |
| 281 | + status: 404, |
| 282 | + }), |
| 283 | + ); |
| 284 | + |
| 285 | + const result = await checkRgsHealth(); |
| 286 | + |
| 287 | + expect(result.isErr()).toBe(true); |
| 288 | + if (result.isErr()) { |
| 289 | + expect(result.error.message).toContain('404'); |
| 290 | + } |
| 291 | + }); |
| 292 | + |
| 293 | + it('should handle missing timestamp in RGS HTML', async () => { |
| 294 | + const mockHtml = ` |
| 295 | + <a href="some-file-without-timestamp.lngossip">snapshot</a> |
| 296 | + `; |
| 297 | + |
| 298 | + (global.fetch as jest.Mock) = jest.fn(() => |
| 299 | + Promise.resolve({ |
| 300 | + ok: true, |
| 301 | + text: () => Promise.resolve(mockHtml), |
| 302 | + }), |
| 303 | + ); |
| 304 | + |
| 305 | + const result = await checkRgsHealth(); |
| 306 | + |
| 307 | + expect(result.isErr()).toBe(true); |
| 308 | + if (result.isErr()) { |
| 309 | + expect(result.error.message).toContain('Could not parse'); |
| 310 | + } |
| 311 | + }); |
| 312 | + |
| 313 | + it('should handle network timeout', async () => { |
| 314 | + jest.useFakeTimers(); |
| 315 | + |
| 316 | + (global.fetch as jest.Mock) = jest.fn(() => |
| 317 | + new Promise(resolve => { |
| 318 | + setTimeout(() => resolve({ |
| 319 | + ok: true, |
| 320 | + text: () => Promise.resolve('<html></html>'), |
| 321 | + }), 10000); // longer than 5s timeout |
| 322 | + }), |
| 323 | + ); |
| 324 | + |
| 325 | + const healthPromise = checkRgsHealth(); |
| 326 | + |
| 327 | + jest.advanceTimersByTime(6000); |
| 328 | + |
| 329 | + await expect(healthPromise).resolves.toMatchObject({ |
| 330 | + isErr: expect.any(Function), |
| 331 | + }); |
| 332 | + |
| 333 | + const result = await healthPromise; |
| 334 | + expect(result.isErr()).toBe(true); |
| 335 | + |
| 336 | + jest.useRealTimers(); |
| 337 | + }); |
| 338 | + |
| 339 | + it('should parse timestamp with colon delimiter', async () => { |
| 340 | + const testTimestamp = 1762462800; // Nov 6, 2025 |
| 341 | + |
| 342 | + const mockHtml = ` |
| 343 | + <a href="snapshot__calculated-at:${testTimestamp}__range:10800-scope.lngossip">snapshot</a> |
| 344 | + `; |
| 345 | + |
| 346 | + (global.fetch as jest.Mock) = jest.fn(() => |
| 347 | + Promise.resolve({ |
| 348 | + ok: true, |
| 349 | + text: () => Promise.resolve(mockHtml), |
| 350 | + }), |
| 351 | + ); |
| 352 | + |
| 353 | + const result = await checkRgsHealth(); |
| 354 | + |
| 355 | + expect(result.isOk()).toBe(true); |
| 356 | + if (result.isOk()) { |
| 357 | + expect(result.value.timestamp).toBe(testTimestamp); |
| 358 | + } |
| 359 | + }); |
| 360 | + |
| 361 | + it('should parse timestamp with percent-encoded colon', async () => { |
| 362 | + const testTimestamp = 1762462800; // Nov 6, 2025 |
| 363 | + |
| 364 | + const mockHtml = ` |
| 365 | + <a href="snapshot__calculated-at%3A${testTimestamp}__range%3A10800-scope.lngossip">snapshot</a> |
| 366 | + `; |
| 367 | + |
| 368 | + (global.fetch as jest.Mock) = jest.fn(() => |
| 369 | + Promise.resolve({ |
| 370 | + ok: true, |
| 371 | + text: () => Promise.resolve(mockHtml), |
| 372 | + }), |
| 373 | + ); |
| 374 | + |
| 375 | + const result = await checkRgsHealth(); |
| 376 | + |
| 377 | + expect(result.isOk()).toBe(true); |
| 378 | + if (result.isOk()) { |
| 379 | + expect(result.value.timestamp).toBe(testTimestamp); |
| 380 | + } |
| 381 | + }); |
| 382 | +}); |
| 383 | + |
0 commit comments