88
99 "github.com/Jetlum/WalletAlertService/mock"
1010 "github.com/Jetlum/WalletAlertService/models"
11+ "github.com/ethereum/go-ethereum/common"
1112 "github.com/ethereum/go-ethereum/core/types"
13+ "github.com/ethereum/go-ethereum/crypto"
1214 "github.com/stretchr/testify/assert"
1315)
1416
@@ -21,23 +23,6 @@ func TestIntegration(t *testing.T) {
2123 ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
2224 defer cancel ()
2325
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-
4126 userPref := models.UserPreference {
4227 UserID : "test@example.com" ,
4328 WalletAddress : "0xto" ,
@@ -47,40 +32,98 @@ func TestIntegration(t *testing.T) {
4732
4833 mockUserPrefRepo := & mock.MockUserPreferenceRepository {
4934 GetMatchingPreferencesFunc : func (e * models.Event ) ([]models.UserPreference , error ) {
50- assert .Equal (t , event .ToAddress , e .ToAddress )
5135 return []models.UserPreference {userPref }, nil
5236 },
5337 }
5438
55- // Setup mock email notification
39+ // Create mock Ethereum client
40+ mockClient := mock .NewMockEthClient ()
41+
42+ // Create and use fromAddress in assertions
43+ privateKey , err := crypto .GenerateKey ()
44+ if err != nil {
45+ t .Fatalf ("Failed to generate private key: %v" , err )
46+ }
47+ fromAddress := crypto .PubkeyToAddress (privateKey .PublicKey )
48+
49+ mockEventRepo := & mock.MockEventRepository {
50+ CreateFunc : func (e * models.Event ) error {
51+ assert .Equal (t , fromAddress .Hex (), e .FromAddress , "From address should match" )
52+ return nil
53+ },
54+ }
55+ // Create mock transaction (unsigned)
56+ mockTx := types .NewTransaction (
57+ 0 ,
58+ common .HexToAddress ("0xto" ),
59+ big .NewInt (2000000000000000000 ), // 2 ETH (above threshold)
60+ 21000 ,
61+ big .NewInt (1 ),
62+ nil ,
63+ )
64+
65+ // Sign the transaction
66+ chainID := big .NewInt (1 ) // Use the same chain ID in the mock client
67+ signer := types .LatestSignerForChainID (chainID )
68+ signedTx , err := types .SignTx (mockTx , signer , privateKey )
69+ if err != nil {
70+ t .Fatalf ("Failed to sign transaction: %v" , err )
71+ }
72+
73+ // Update NetworkIDFunc to return the correct chain ID
74+ mockClient .NetworkIDFunc = func (ctx context.Context ) (* big.Int , error ) {
75+ return chainID , nil
76+ }
77+
78+ // Fix block.WithBody call
79+ mockClient .BlockByHashFunc = func (ctx context.Context , hash common.Hash ) (* types.Block , error ) {
80+ header := & types.Header {
81+ Number : big .NewInt (1 ),
82+ ParentHash : common .HexToHash ("0x123" ),
83+ Time : uint64 (time .Now ().Unix ()),
84+ }
85+
86+ block := types .NewBlockWithHeader (header )
87+ return block .WithBody (types.Body {
88+ Transactions : []* types.Transaction {signedTx },
89+ Uncles : []* types.Header {},
90+ }), nil
91+ }
92+
93+ // Use mock NFT detector
94+ mockNFTDetector := & mock.MockNFTDetector {
95+ IsNFTTransactionFunc : func (tx * types.Transaction ) bool {
96+ return false
97+ },
98+ }
99+
56100 emailSent := false
57101 mockEmailNotification := & mock.MockEmailNotification {
58- SendFunc : func (e * models.Event , up * models.UserPreference ) error {
102+ SendFunc : func (event * models.Event , userPref * models.UserPreference ) error {
59103 emailSent = true
104+ // Add assertions if needed
60105 return nil
61106 },
62107 }
63108
64- // Create mock block and header
109+ // Create mock header
65110 mockHeader := & types.Header {
66111 Number : big .NewInt (1 ),
67112 }
68113
69- // Test full workflow
70114 done := make (chan bool )
71115 go func () {
72116 processBlock (
73- nil ,
117+ mockClient ,
74118 mockHeader ,
75- mock . NewMockNFTDetector () ,
119+ mockNFTDetector ,
76120 mockEmailNotification ,
77121 mockEventRepo ,
78122 mockUserPrefRepo ,
79123 )
80124 done <- true
81125 }()
82126
83- // Wait for completion or timeout
84127 select {
85128 case <- ctx .Done ():
86129 t .Fatal ("Test timed out" )
0 commit comments