|
| 1 | +package evm |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/rs/zerolog" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNewEventConfirmer(t *testing.T) { |
| 12 | + t.Run("creates event confirmer with valid params", func(t *testing.T) { |
| 13 | + logger := zerolog.Nop() |
| 14 | + chainID := "eip155:1" |
| 15 | + |
| 16 | + confirmer := NewEventConfirmer(nil, nil, chainID, 5, 5, 12, logger) |
| 17 | + |
| 18 | + require.NotNil(t, confirmer) |
| 19 | + assert.Equal(t, chainID, confirmer.chainID) |
| 20 | + assert.Equal(t, 5, confirmer.pollIntervalSeconds) |
| 21 | + assert.Equal(t, uint64(5), confirmer.fastConfirmations) |
| 22 | + assert.Equal(t, uint64(12), confirmer.standardConfirmations) |
| 23 | + assert.NotNil(t, confirmer.chainStore) |
| 24 | + assert.NotNil(t, confirmer.stopCh) |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("creates event confirmer with different confirmation counts", func(t *testing.T) { |
| 28 | + logger := zerolog.Nop() |
| 29 | + |
| 30 | + testCases := []struct { |
| 31 | + fast uint64 |
| 32 | + standard uint64 |
| 33 | + }{ |
| 34 | + {1, 6}, |
| 35 | + {5, 12}, |
| 36 | + {10, 20}, |
| 37 | + {0, 1}, |
| 38 | + } |
| 39 | + |
| 40 | + for _, tc := range testCases { |
| 41 | + confirmer := NewEventConfirmer(nil, nil, "eip155:1", 5, tc.fast, tc.standard, logger) |
| 42 | + assert.Equal(t, tc.fast, confirmer.fastConfirmations) |
| 43 | + assert.Equal(t, tc.standard, confirmer.standardConfirmations) |
| 44 | + } |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func TestEventConfirmerGetTxHashFromEventID(t *testing.T) { |
| 49 | + logger := zerolog.Nop() |
| 50 | + confirmer := NewEventConfirmer(nil, nil, "eip155:1", 5, 5, 12, logger) |
| 51 | + |
| 52 | + t.Run("extracts tx hash from standard format", func(t *testing.T) { |
| 53 | + eventID := "0x1234567890abcdef:5" |
| 54 | + txHash := confirmer.getTxHashFromEventID(eventID) |
| 55 | + assert.Equal(t, "0x1234567890abcdef", txHash) |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("extracts tx hash with log index 0", func(t *testing.T) { |
| 59 | + eventID := "0xabc123:0" |
| 60 | + txHash := confirmer.getTxHashFromEventID(eventID) |
| 61 | + assert.Equal(t, "0xabc123", txHash) |
| 62 | + }) |
| 63 | + |
| 64 | + t.Run("handles event ID without colon", func(t *testing.T) { |
| 65 | + eventID := "0x1234567890abcdef" |
| 66 | + txHash := confirmer.getTxHashFromEventID(eventID) |
| 67 | + assert.Equal(t, "0x1234567890abcdef", txHash) |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("returns empty string for empty event ID", func(t *testing.T) { |
| 71 | + eventID := "" |
| 72 | + txHash := confirmer.getTxHashFromEventID(eventID) |
| 73 | + assert.Empty(t, txHash) |
| 74 | + }) |
| 75 | + |
| 76 | + t.Run("handles multiple colons", func(t *testing.T) { |
| 77 | + eventID := "0x123:456:789" |
| 78 | + txHash := confirmer.getTxHashFromEventID(eventID) |
| 79 | + assert.Equal(t, "0x123", txHash) |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +func TestEventConfirmerGetRequiredConfirmations(t *testing.T) { |
| 84 | + logger := zerolog.Nop() |
| 85 | + |
| 86 | + t.Run("FAST confirmation type", func(t *testing.T) { |
| 87 | + confirmer := NewEventConfirmer(nil, nil, "eip155:1", 5, 5, 12, logger) |
| 88 | + confirmations := confirmer.getRequiredConfirmations("FAST") |
| 89 | + assert.Equal(t, uint64(5), confirmations) |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("STANDARD confirmation type", func(t *testing.T) { |
| 93 | + confirmer := NewEventConfirmer(nil, nil, "eip155:1", 5, 5, 12, logger) |
| 94 | + confirmations := confirmer.getRequiredConfirmations("STANDARD") |
| 95 | + assert.Equal(t, uint64(12), confirmations) |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run("unknown type defaults to standard", func(t *testing.T) { |
| 99 | + confirmer := NewEventConfirmer(nil, nil, "eip155:1", 5, 5, 12, logger) |
| 100 | + confirmations := confirmer.getRequiredConfirmations("UNKNOWN") |
| 101 | + assert.Equal(t, uint64(12), confirmations) |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("empty type defaults to standard", func(t *testing.T) { |
| 105 | + confirmer := NewEventConfirmer(nil, nil, "eip155:1", 5, 5, 12, logger) |
| 106 | + confirmations := confirmer.getRequiredConfirmations("") |
| 107 | + assert.Equal(t, uint64(12), confirmations) |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("uses custom fast confirmations", func(t *testing.T) { |
| 111 | + confirmer := NewEventConfirmer(nil, nil, "eip155:1", 5, 3, 20, logger) |
| 112 | + confirmations := confirmer.getRequiredConfirmations("FAST") |
| 113 | + assert.Equal(t, uint64(3), confirmations) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("uses custom standard confirmations", func(t *testing.T) { |
| 117 | + confirmer := NewEventConfirmer(nil, nil, "eip155:1", 5, 3, 20, logger) |
| 118 | + confirmations := confirmer.getRequiredConfirmations("STANDARD") |
| 119 | + assert.Equal(t, uint64(20), confirmations) |
| 120 | + }) |
| 121 | +} |
| 122 | + |
| 123 | +func TestEventConfirmerStop(t *testing.T) { |
| 124 | + t.Run("stop waits for goroutine", func(t *testing.T) { |
| 125 | + logger := zerolog.Nop() |
| 126 | + confirmer := NewEventConfirmer(nil, nil, "eip155:1", 5, 5, 12, logger) |
| 127 | + |
| 128 | + // Should not panic or hang |
| 129 | + confirmer.Stop() |
| 130 | + }) |
| 131 | +} |
| 132 | + |
| 133 | +func TestEventConfirmerStruct(t *testing.T) { |
| 134 | + t.Run("struct has expected fields", func(t *testing.T) { |
| 135 | + ec := &EventConfirmer{} |
| 136 | + assert.Nil(t, ec.rpcClient) |
| 137 | + assert.Nil(t, ec.chainStore) |
| 138 | + assert.Empty(t, ec.chainID) |
| 139 | + assert.Equal(t, 0, ec.pollIntervalSeconds) |
| 140 | + assert.Equal(t, uint64(0), ec.fastConfirmations) |
| 141 | + assert.Equal(t, uint64(0), ec.standardConfirmations) |
| 142 | + assert.Nil(t, ec.stopCh) |
| 143 | + }) |
| 144 | +} |
0 commit comments