Skip to content

Commit 0c660ae

Browse files
committed
fix: add vault drift anomaly tests to existing abuse-monitor test file
Add tests for logVaultDriftAnomaly to the existing abuse-monitor.test.ts file instead of creating a separate test file. This avoids complex mocking issues while still providing test coverage for the vault drift anomaly logging functionality.
1 parent cbbbad8 commit 0c660ae

1 file changed

Lines changed: 52 additions & 1 deletion

File tree

src/tests/abuse-monitor.test.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { jest, describe, it, expect, beforeEach } from '@jest/globals'
22
import { AbuseMonitor } from '../services/abuse-monitor.js'
33
import { logger } from '../middleware/logger.js'
4-
import { emitTestSuspiciousEvent, __resetSecurityMonitorForTests, getAbuseCategoryCounts } from '../security/abuse-monitor.js'
4+
import { emitTestSuspiciousEvent, __resetSecurityMonitorForTests, getAbuseCategoryCounts, logVaultDriftAnomaly } from '../security/abuse-monitor.js'
55
import type { AbuseCategory } from '../types/security.js'
66

77
describe('AbuseMonitor Heuristics', () => {
@@ -191,4 +191,55 @@ describe('security/abuse-monitor structured events (pino integration)', () => {
191191
const counts = getAbuseCategoryCounts()
192192
expect(counts['brute-force']).toBe(2)
193193
})
194+
195+
it('logVaultDriftAnomaly emits structured vault_missing_onchain event', () => {
196+
const spy = jest.spyOn(console, 'log').mockImplementation(() => {})
197+
198+
logVaultDriftAnomaly('vault_missing_onchain', {
199+
vaultId: 'vault-123',
200+
persistedStatus: 'active',
201+
})
202+
203+
const logCall = spy.mock.calls[0][0] as string
204+
expect(logCall).toContain('vault.vault_missing_onchain')
205+
expect(logCall).toContain('vault-123')
206+
expect(logCall).toContain('active')
207+
208+
spy.mockRestore()
209+
})
210+
211+
it('logVaultDriftAnomaly emits structured vault_state_drift event', () => {
212+
const spy = jest.spyOn(console, 'log').mockImplementation(() => {})
213+
214+
logVaultDriftAnomaly('vault_state_drift', {
215+
vaultId: 'vault-456',
216+
driftedFields: ['status', 'amount'],
217+
persisted: { status: 'active', amount: '1000' },
218+
onChain: { status: 'completed', amount: '2000' },
219+
})
220+
221+
const logCall = spy.mock.calls[0][0] as string
222+
expect(logCall).toContain('vault.vault_state_drift')
223+
expect(logCall).toContain('vault-456')
224+
expect(logCall).toContain('status')
225+
expect(logCall).toContain('amount')
226+
227+
spy.mockRestore()
228+
})
229+
230+
it('logVaultDriftAnomaly emits structured vault_reconciliation_error event', () => {
231+
const spy = jest.spyOn(console, 'log').mockImplementation(() => {})
232+
233+
logVaultDriftAnomaly('vault_reconciliation_error', {
234+
vaultId: 'vault-789',
235+
error: 'RPC timeout',
236+
})
237+
238+
const logCall = spy.mock.calls[0][0] as string
239+
expect(logCall).toContain('vault.vault_reconciliation_error')
240+
expect(logCall).toContain('vault-789')
241+
expect(logCall).toContain('RPC timeout')
242+
243+
spy.mockRestore()
244+
})
194245
})

0 commit comments

Comments
 (0)