|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { TransactionMetricsService } from './transaction-metrics.service'; |
| 3 | + |
| 4 | +describe('TransactionMetricsService', () => { |
| 5 | + let service: TransactionMetricsService; |
| 6 | + |
| 7 | + beforeEach(async () => { |
| 8 | + const module: TestingModule = await Test.createTestingModule({ |
| 9 | + providers: [TransactionMetricsService], |
| 10 | + }).compile(); |
| 11 | + |
| 12 | + service = module.get<TransactionMetricsService>(TransactionMetricsService); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should be defined', () => { |
| 16 | + expect(service).toBeDefined(); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('getSnapshot initial state', () => { |
| 20 | + it('returns all-zero counters when no events have been recorded', () => { |
| 21 | + const snap = service.getSnapshot(); |
| 22 | + expect(snap.transactionsCreatedTotal).toBe(0); |
| 23 | + expect(snap.transactionsStatusUpdatedTotal).toBe(0); |
| 24 | + expect(snap.transactionsFailedTotal).toBe(0); |
| 25 | + expect(snap.idempotencyHitsTotal).toBe(0); |
| 26 | + expect(snap.cacheHitsTotal).toBe(0); |
| 27 | + expect(snap.cacheMissesTotal).toBe(0); |
| 28 | + expect(snap.transactionsCreatedByAsset).toEqual({}); |
| 29 | + expect(snap.transactionsStatusUpdatedByTransition).toEqual({}); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('incrementTransactionCreated', () => { |
| 34 | + it('increments total counter', () => { |
| 35 | + service.incrementTransactionCreated('NATIVE'); |
| 36 | + expect(service.getSnapshot().transactionsCreatedTotal).toBe(1); |
| 37 | + }); |
| 38 | + |
| 39 | + it('tracks per-asset-type counts', () => { |
| 40 | + service.incrementTransactionCreated('NATIVE'); |
| 41 | + service.incrementTransactionCreated('NATIVE'); |
| 42 | + service.incrementTransactionCreated('TOKEN'); |
| 43 | + |
| 44 | + const snap = service.getSnapshot(); |
| 45 | + expect(snap.transactionsCreatedTotal).toBe(3); |
| 46 | + expect(snap.transactionsCreatedByAsset).toEqual({ |
| 47 | + NATIVE: 2, |
| 48 | + TOKEN: 1, |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns a copy of the asset map so the snapshot is immutable', () => { |
| 53 | + service.incrementTransactionCreated('NATIVE'); |
| 54 | + const snap = service.getSnapshot(); |
| 55 | + snap.transactionsCreatedByAsset['NATIVE'] = 999; |
| 56 | + expect(service.getSnapshot().transactionsCreatedByAsset['NATIVE']).toBe( |
| 57 | + 1, |
| 58 | + ); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('incrementStatusUpdated', () => { |
| 63 | + it('increments total and records the transition key', () => { |
| 64 | + service.incrementStatusUpdated('PENDING', 'SUBMITTED'); |
| 65 | + |
| 66 | + const snap = service.getSnapshot(); |
| 67 | + expect(snap.transactionsStatusUpdatedTotal).toBe(1); |
| 68 | + expect(snap.transactionsStatusUpdatedByTransition).toEqual({ |
| 69 | + PENDING_to_SUBMITTED: 1, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('increments transactionsFailedTotal when toStatus is FAILED', () => { |
| 74 | + service.incrementStatusUpdated('PENDING', 'FAILED'); |
| 75 | + |
| 76 | + const snap = service.getSnapshot(); |
| 77 | + expect(snap.transactionsFailedTotal).toBe(1); |
| 78 | + }); |
| 79 | + |
| 80 | + it('does not increment transactionsFailedTotal for non-FAILED transitions', () => { |
| 81 | + service.incrementStatusUpdated('PENDING', 'SUBMITTED'); |
| 82 | + expect(service.getSnapshot().transactionsFailedTotal).toBe(0); |
| 83 | + }); |
| 84 | + |
| 85 | + it('accumulates multiple different transitions', () => { |
| 86 | + service.incrementStatusUpdated('PENDING', 'SUBMITTED'); |
| 87 | + service.incrementStatusUpdated('SUBMITTED', 'CONFIRMED'); |
| 88 | + service.incrementStatusUpdated('PENDING', 'SUBMITTED'); |
| 89 | + |
| 90 | + const snap = service.getSnapshot(); |
| 91 | + expect(snap.transactionsStatusUpdatedTotal).toBe(3); |
| 92 | + expect(snap.transactionsStatusUpdatedByTransition).toEqual({ |
| 93 | + PENDING_to_SUBMITTED: 2, |
| 94 | + SUBMITTED_to_CONFIRMED: 1, |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('incrementIdempotencyHit', () => { |
| 100 | + it('increments idempotencyHitsTotal', () => { |
| 101 | + service.incrementIdempotencyHit(); |
| 102 | + service.incrementIdempotencyHit(); |
| 103 | + expect(service.getSnapshot().idempotencyHitsTotal).toBe(2); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('incrementCacheHit', () => { |
| 108 | + it('increments cacheHitsTotal', () => { |
| 109 | + service.incrementCacheHit(); |
| 110 | + expect(service.getSnapshot().cacheHitsTotal).toBe(1); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('incrementCacheMiss', () => { |
| 115 | + it('increments cacheMissesTotal', () => { |
| 116 | + service.incrementCacheMiss(); |
| 117 | + service.incrementCacheMiss(); |
| 118 | + expect(service.getSnapshot().cacheMissesTotal).toBe(2); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('getSnapshot', () => { |
| 123 | + it('returns independent copies so mutations do not affect internal state', () => { |
| 124 | + service.incrementStatusUpdated('PENDING', 'SUBMITTED'); |
| 125 | + const snap = service.getSnapshot(); |
| 126 | + snap.transactionsStatusUpdatedByTransition['PENDING_to_SUBMITTED'] = 999; |
| 127 | + expect( |
| 128 | + service.getSnapshot().transactionsStatusUpdatedByTransition[ |
| 129 | + 'PENDING_to_SUBMITTED' |
| 130 | + ], |
| 131 | + ).toBe(1); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments