|
1 | | -import { warnIfIndexerCursorStale } from '../indexer-cursor-staleness.utils'; |
| 1 | +import { |
| 2 | + checkIndexerCursorStalenessFromStore, |
| 3 | + warnIfIndexerCursorStale, |
| 4 | +} from '../indexer-cursor-staleness.utils'; |
2 | 5 | import { logger } from '../logger.utils'; |
| 6 | +import { prisma } from '../prisma.utils'; |
| 7 | +import { envConfig } from '../../config'; |
3 | 8 |
|
4 | 9 | jest.mock('../logger.utils', () => ({ |
5 | | - logger: { warn: jest.fn() }, |
| 10 | + logger: { warn: jest.fn() }, |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('../prisma.utils', () => ({ |
| 14 | + prisma: { |
| 15 | + indexedLedger: { |
| 16 | + findFirst: jest.fn(), |
| 17 | + }, |
| 18 | + }, |
| 19 | +})); |
| 20 | + |
| 21 | +jest.mock('../../config', () => ({ |
| 22 | + envConfig: { |
| 23 | + ENABLE_INDEXER_CURSOR_STALENESS_WARNING: true, |
| 24 | + INDEXER_CURSOR_STALE_AGE_WARNING_MS: 300_000, |
| 25 | + }, |
6 | 26 | })); |
7 | 27 |
|
8 | 28 | const warnMock = logger.warn as jest.Mock; |
| 29 | +const findFirstMock = prisma.indexedLedger.findFirst as jest.Mock; |
9 | 30 |
|
10 | 31 | beforeEach(() => { |
11 | | - warnMock.mockClear(); |
| 32 | + warnMock.mockClear(); |
| 33 | + findFirstMock.mockReset(); |
| 34 | + (envConfig as { ENABLE_INDEXER_CURSOR_STALENESS_WARNING: boolean }).ENABLE_INDEXER_CURSOR_STALENESS_WARNING = true; |
12 | 35 | }); |
13 | 36 |
|
14 | 37 | describe('warnIfIndexerCursorStale()', () => { |
15 | | - it('emits a warning when cursor age exceeds the threshold', () => { |
16 | | - const sixMinutesAgo = new Date(Date.now() - 360_000); |
17 | | - warnIfIndexerCursorStale(sixMinutesAgo, 300_000); |
18 | | - expect(warnMock).toHaveBeenCalledTimes(1); |
19 | | - expect(warnMock).toHaveBeenCalledWith( |
20 | | - expect.objectContaining({ |
21 | | - msg: 'Indexer cursor is stale', |
22 | | - thresholdMs: 300_000, |
23 | | - }) |
24 | | - ); |
25 | | - }); |
26 | | - |
27 | | - it('does not emit a warning when cursor age is within the threshold', () => { |
28 | | - const oneMinuteAgo = new Date(Date.now() - 60_000); |
29 | | - warnIfIndexerCursorStale(oneMinuteAgo, 300_000); |
30 | | - expect(warnMock).not.toHaveBeenCalled(); |
31 | | - }); |
32 | | - |
33 | | - it('does not emit a warning when cursor age exactly equals the threshold', () => { |
34 | | - const exactly = new Date(Date.now() - 300_000); |
35 | | - warnIfIndexerCursorStale(exactly, 300_000); |
36 | | - expect(warnMock).not.toHaveBeenCalled(); |
37 | | - }); |
38 | | - |
39 | | - it('includes lastUpdatedAt, ageMs and thresholdMs in the warning payload', () => { |
40 | | - const ts = new Date(Date.now() - 400_000); |
41 | | - warnIfIndexerCursorStale(ts, 300_000); |
42 | | - const call = warnMock.mock.calls[0][0]; |
43 | | - expect(call.lastUpdatedAt).toBe(ts.toISOString()); |
44 | | - expect(typeof call.ageMs).toBe('number'); |
45 | | - expect(call.ageMs).toBeGreaterThan(300_000); |
46 | | - expect(call.thresholdMs).toBe(300_000); |
47 | | - }); |
48 | | - |
49 | | - it('respects a custom threshold override', () => { |
50 | | - const twoSecondsAgo = new Date(Date.now() - 2_000); |
51 | | - warnIfIndexerCursorStale(twoSecondsAgo, 1_000); |
52 | | - expect(warnMock).toHaveBeenCalledTimes(1); |
53 | | - }); |
| 38 | + it('emits a warning when cursor lag exceeds the threshold', () => { |
| 39 | + const sixMinutesAgo = new Date(Date.now() - 360_000); |
| 40 | + warnIfIndexerCursorStale(sixMinutesAgo, 300_000, { |
| 41 | + job: 'indexer', |
| 42 | + cursor: 'cursor-1', |
| 43 | + ledger: 42, |
| 44 | + }); |
| 45 | + expect(warnMock).toHaveBeenCalledTimes(1); |
| 46 | + expect(warnMock).toHaveBeenCalledWith( |
| 47 | + expect.objectContaining({ |
| 48 | + msg: 'Indexer cursor lag exceeded threshold', |
| 49 | + job: 'indexer', |
| 50 | + cursor: 'cursor-1', |
| 51 | + ledger: 42, |
| 52 | + lagMs: expect.any(Number), |
| 53 | + thresholdMs: 300_000, |
| 54 | + }) |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('does not emit a warning when cursor lag is within the threshold', () => { |
| 59 | + const oneMinuteAgo = new Date(Date.now() - 60_000); |
| 60 | + warnIfIndexerCursorStale(oneMinuteAgo, 300_000); |
| 61 | + expect(warnMock).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('does not emit a warning when cursor lag exactly equals the threshold', () => { |
| 65 | + const exactly = new Date(Date.now() - 300_000); |
| 66 | + warnIfIndexerCursorStale(exactly, 300_000); |
| 67 | + expect(warnMock).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('includes lastUpdatedAt, lagMs and thresholdMs in the warning payload', () => { |
| 71 | + const ts = new Date(Date.now() - 400_000); |
| 72 | + warnIfIndexerCursorStale(ts, 300_000); |
| 73 | + const call = warnMock.mock.calls[0][0]; |
| 74 | + expect(call.lastUpdatedAt).toBe(ts.toISOString()); |
| 75 | + expect(typeof call.lagMs).toBe('number'); |
| 76 | + expect(call.lagMs).toBeGreaterThan(300_000); |
| 77 | + expect(call.thresholdMs).toBe(300_000); |
| 78 | + }); |
| 79 | + |
| 80 | + it('respects a custom threshold override', () => { |
| 81 | + const twoSecondsAgo = new Date(Date.now() - 2_000); |
| 82 | + warnIfIndexerCursorStale(twoSecondsAgo, 1_000); |
| 83 | + expect(warnMock).toHaveBeenCalledTimes(1); |
| 84 | + }); |
| 85 | + |
| 86 | + it('does not emit a warning when ENABLE_INDEXER_CURSOR_STALENESS_WARNING is false', () => { |
| 87 | + (envConfig as { ENABLE_INDEXER_CURSOR_STALENESS_WARNING: boolean }).ENABLE_INDEXER_CURSOR_STALENESS_WARNING = false; |
| 88 | + const sixMinutesAgo = new Date(Date.now() - 360_000); |
| 89 | + warnIfIndexerCursorStale(sixMinutesAgo, 300_000); |
| 90 | + expect(warnMock).not.toHaveBeenCalled(); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe('checkIndexerCursorStalenessFromStore()', () => { |
| 95 | + it('emits a warning when the stored cursor is stale', async () => { |
| 96 | + findFirstMock.mockResolvedValue({ |
| 97 | + ledger: 99, |
| 98 | + cursor: 'abc', |
| 99 | + updatedAt: new Date(Date.now() - 400_000), |
| 100 | + }); |
| 101 | + |
| 102 | + await checkIndexerCursorStalenessFromStore({ job: 'indexer' }); |
| 103 | + |
| 104 | + expect(warnMock).toHaveBeenCalledWith( |
| 105 | + expect.objectContaining({ |
| 106 | + job: 'indexer', |
| 107 | + cursor: 'abc', |
| 108 | + ledger: 99, |
| 109 | + lagMs: expect.any(Number), |
| 110 | + thresholdMs: 300_000, |
| 111 | + }) |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it('does not emit a warning when no indexed ledger row exists', async () => { |
| 116 | + findFirstMock.mockResolvedValue(null); |
| 117 | + |
| 118 | + await checkIndexerCursorStalenessFromStore(); |
| 119 | + |
| 120 | + expect(warnMock).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('does not emit a warning when the stored cursor is fresh', async () => { |
| 124 | + findFirstMock.mockResolvedValue({ |
| 125 | + ledger: 1, |
| 126 | + cursor: 'fresh', |
| 127 | + updatedAt: new Date(), |
| 128 | + }); |
| 129 | + |
| 130 | + await checkIndexerCursorStalenessFromStore(); |
| 131 | + |
| 132 | + expect(warnMock).not.toHaveBeenCalled(); |
| 133 | + }); |
54 | 134 | }); |
0 commit comments