|
| 1 | +import { SuspiciousActivityService, SuspiciousActivityType, AlertSeverity } from './suspicious-activity.service'; |
| 2 | +import { Transaction, TxStatus, TxType } from './transaction.entity'; |
| 3 | + |
| 4 | +function makeTx(overrides: Partial<Transaction> = {}): Transaction { |
| 5 | + return Object.assign(new Transaction(), { |
| 6 | + id: crypto.randomUUID(), |
| 7 | + txHash: `0x${Math.random().toString(16).slice(2)}`, |
| 8 | + merchantId: 'merchant_1', |
| 9 | + chainId: 1, |
| 10 | + status: TxStatus.SUCCESS, |
| 11 | + type: TxType.TRANSFER, |
| 12 | + gasUsed: 21000, |
| 13 | + timestamp: new Date(), |
| 14 | + ...overrides, |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +describe('SuspiciousActivityService', () => { |
| 19 | + let service: SuspiciousActivityService; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + service = new SuspiciousActivityService(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('analyze() — no detection below threshold', () => { |
| 26 | + it('returns detected=false when fewer than 5 transactions', () => { |
| 27 | + for (let i = 0; i < 4; i++) { |
| 28 | + const result = service.analyze(makeTx()); |
| 29 | + expect(result.detected).toBe(false); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns detected=false for normal traffic', () => { |
| 34 | + for (let i = 0; i < 10; i++) { |
| 35 | + const tx = makeTx({ timestamp: new Date(Date.now() - i * 5000) }); |
| 36 | + service.analyze(tx); |
| 37 | + } |
| 38 | + const result = service.analyze(makeTx()); |
| 39 | + expect(result.detected).toBe(false); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('High failure rate detection', () => { |
| 44 | + it('detects HIGH_FAILURE_RATE when > 70% of last 10 transactions fail', () => { |
| 45 | + // Seed 10 transactions with 8 failures |
| 46 | + const txs = [ |
| 47 | + ...Array(8).fill(null).map(() => makeTx({ status: TxStatus.FAILURE })), |
| 48 | + ...Array(2).fill(null).map(() => makeTx({ status: TxStatus.SUCCESS })), |
| 49 | + ]; |
| 50 | + let lastResult: any; |
| 51 | + for (const tx of txs) { |
| 52 | + lastResult = service.analyze(tx); |
| 53 | + } |
| 54 | + expect(lastResult.detected).toBe(true); |
| 55 | + expect(lastResult.type).toBe(SuspiciousActivityType.HIGH_FAILURE_RATE); |
| 56 | + expect([AlertSeverity.LOW, AlertSeverity.MEDIUM, AlertSeverity.HIGH]).toContain(lastResult.severity); |
| 57 | + expect(lastResult.detectedAt).toBeDefined(); |
| 58 | + expect(lastResult.metadata.failureRate).toBeGreaterThanOrEqual(70); |
| 59 | + }); |
| 60 | + |
| 61 | + it('does not flag when failure rate is below 70%', () => { |
| 62 | + const txs = [ |
| 63 | + ...Array(6).fill(null).map(() => makeTx({ status: TxStatus.SUCCESS })), |
| 64 | + ...Array(4).fill(null).map(() => makeTx({ status: TxStatus.FAILURE })), |
| 65 | + ]; |
| 66 | + let lastResult: any; |
| 67 | + for (const tx of txs) { |
| 68 | + lastResult = service.analyze(tx); |
| 69 | + } |
| 70 | + // 40% failure — should NOT trigger high failure rate |
| 71 | + if (lastResult.detected) { |
| 72 | + expect(lastResult.type).not.toBe(SuspiciousActivityType.HIGH_FAILURE_RATE); |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('Gas spike detection', () => { |
| 78 | + it('detects GAS_SPIKE when latest tx uses > 4x rolling average', () => { |
| 79 | + // 9 normal transactions |
| 80 | + for (let i = 0; i < 9; i++) { |
| 81 | + service.analyze(makeTx({ gasUsed: 21000 })); |
| 82 | + } |
| 83 | + // Spike transaction |
| 84 | + const result = service.analyze(makeTx({ gasUsed: 210000 })); // 10x |
| 85 | + expect(result.detected).toBe(true); |
| 86 | + expect(result.type).toBe(SuspiciousActivityType.GAS_SPIKE); |
| 87 | + expect(result.severity).toBe(AlertSeverity.HIGH); // 10x => HIGH |
| 88 | + expect(result.metadata?.spikeMultiplier).toBeGreaterThanOrEqual(4); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does not flag a normal gas amount', () => { |
| 92 | + for (let i = 0; i < 9; i++) { |
| 93 | + service.analyze(makeTx({ gasUsed: 21000 })); |
| 94 | + } |
| 95 | + const result = service.analyze(makeTx({ gasUsed: 25000 })); // ~1.2x — normal |
| 96 | + if (result.detected) { |
| 97 | + expect(result.type).not.toBe(SuspiciousActivityType.GAS_SPIKE); |
| 98 | + } |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('Burst detection', () => { |
| 103 | + it('detects BURST_TRANSACTIONS when >= 5 tx occur within 10 seconds', () => { |
| 104 | + const now = Date.now(); |
| 105 | + const txs = Array(6).fill(null).map((_, i) => |
| 106 | + makeTx({ timestamp: new Date(now - i * 1000) }), // 1s apart within 10s |
| 107 | + ); |
| 108 | + let lastResult: any; |
| 109 | + for (const tx of txs) { |
| 110 | + lastResult = service.analyze(tx); |
| 111 | + } |
| 112 | + expect(lastResult.detected).toBe(true); |
| 113 | + expect(lastResult.type).toBe(SuspiciousActivityType.BURST_TRANSACTIONS); |
| 114 | + expect(lastResult.metadata?.burstCount).toBeGreaterThanOrEqual(5); |
| 115 | + }); |
| 116 | + |
| 117 | + it('does not flag transactions spread over time', () => { |
| 118 | + const now = Date.now(); |
| 119 | + // Spread 10 transactions over 2 minutes — never > 5 in 10s |
| 120 | + const txs = Array(10).fill(null).map((_, i) => |
| 121 | + makeTx({ timestamp: new Date(now - i * 15000) }), |
| 122 | + ); |
| 123 | + let lastResult: any; |
| 124 | + for (const tx of txs) { |
| 125 | + lastResult = service.analyze(tx); |
| 126 | + } |
| 127 | + if (lastResult.detected) { |
| 128 | + expect(lastResult.type).not.toBe(SuspiciousActivityType.BURST_TRANSACTIONS); |
| 129 | + } |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments