|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/Jetlum/WalletAlertService/mock" |
| 10 | + "github.com/Jetlum/WalletAlertService/models" |
| 11 | + "github.com/ethereum/go-ethereum/core/types" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestIntegration(t *testing.T) { |
| 16 | + if testing.Short() { |
| 17 | + t.Skip("Skipping integration test in short mode") |
| 18 | + } |
| 19 | + |
| 20 | + // Setup test context with timeout |
| 21 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 22 | + defer cancel() |
| 23 | + |
| 24 | + // Create test event |
| 25 | + event := &models.Event{ |
| 26 | + TxHash: "0xtest", |
| 27 | + FromAddress: "0xfrom", |
| 28 | + ToAddress: "0xto", |
| 29 | + Value: "1000000000000000000", // 1 ETH |
| 30 | + EventType: "LARGE_TRANSFER", |
| 31 | + } |
| 32 | + |
| 33 | + // Setup mocks |
| 34 | + mockEventRepo := &mock.MockEventRepository{ |
| 35 | + CreateFunc: func(e *models.Event) error { |
| 36 | + assert.Equal(t, event.TxHash, e.TxHash) |
| 37 | + return nil |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + userPref := models.UserPreference{ |
| 42 | + UserID: "test@example.com", |
| 43 | + WalletAddress: "0xto", |
| 44 | + MinEtherValue: "500000000000000000", // 0.5 ETH |
| 45 | + EmailNotification: true, |
| 46 | + } |
| 47 | + |
| 48 | + mockUserPrefRepo := &mock.MockUserPreferenceRepository{ |
| 49 | + GetMatchingPreferencesFunc: func(e *models.Event) ([]models.UserPreference, error) { |
| 50 | + assert.Equal(t, event.ToAddress, e.ToAddress) |
| 51 | + return []models.UserPreference{userPref}, nil |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + // Setup mock email notification |
| 56 | + emailSent := false |
| 57 | + mockEmailNotification := &mock.MockEmailNotification{ |
| 58 | + SendFunc: func(e *models.Event, up *models.UserPreference) error { |
| 59 | + emailSent = true |
| 60 | + return nil |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + // Create mock block and header |
| 65 | + mockHeader := &types.Header{ |
| 66 | + Number: big.NewInt(1), |
| 67 | + } |
| 68 | + |
| 69 | + // Test full workflow |
| 70 | + done := make(chan bool) |
| 71 | + go func() { |
| 72 | + processBlock( |
| 73 | + nil, |
| 74 | + mockHeader, |
| 75 | + mock.NewMockNFTDetector(), |
| 76 | + mockEmailNotification, |
| 77 | + mockEventRepo, |
| 78 | + mockUserPrefRepo, |
| 79 | + ) |
| 80 | + done <- true |
| 81 | + }() |
| 82 | + |
| 83 | + // Wait for completion or timeout |
| 84 | + select { |
| 85 | + case <-ctx.Done(): |
| 86 | + t.Fatal("Test timed out") |
| 87 | + case <-done: |
| 88 | + assert.True(t, emailSent, "Email notification should have been sent") |
| 89 | + } |
| 90 | +} |
0 commit comments