Skip to content

Commit 06beba2

Browse files
committed
Fix NFT mock detector test and ensure proper initialization
1 parent 21153b9 commit 06beba2

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

mock/nft_mock.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ type MockNFTDetector struct {
1414
}
1515

1616
func NewMockNFTDetector() *MockNFTDetector {
17-
detector := &MockNFTDetector{
18-
IsNFTTransactionFunc: func(tx *types.Transaction) bool {
19-
return false
20-
},
21-
}
17+
detector := &MockNFTDetector{}
18+
detector.knownContracts = sync.Map{}
2219

23-
// Initialize with some test NFT contract addresses
20+
// Initialize with test contracts
2421
testContracts := map[string]bool{
25-
"0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D": true, // Test BAYC
26-
"0x23581767a106ae21c074b2276D25e5C3e136a68b": true, // Test Moonbirds
22+
"0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D": true,
23+
"0x23581767a106ae21c074b2276D25e5C3e136a68b": true,
2724
}
2825

2926
for addr := range testContracts {
@@ -36,6 +33,7 @@ func NewMockNFTDetector() *MockNFTDetector {
3633
func (m *MockNFTDetector) IsNFTTransaction(tx *types.Transaction) bool {
3734
m.callCount++
3835

36+
// Custom function takes precedence
3937
if m.IsNFTTransactionFunc != nil {
4038
return m.IsNFTTransactionFunc(tx)
4139
}
@@ -44,11 +42,13 @@ func (m *MockNFTDetector) IsNFTTransaction(tx *types.Transaction) bool {
4442
return false
4543
}
4644

47-
if val, exists := m.knownContracts.Load(*tx.To()); exists {
48-
return val.(bool)
45+
toAddr := tx.To()
46+
// Check known contracts
47+
val, exists := m.knownContracts.Load(*toAddr)
48+
if !exists {
49+
return false
4950
}
50-
51-
return false
51+
return val.(bool)
5252
}
5353

5454
// Helper methods for testing

mock/nft_mock_test.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,28 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12+
// mock/nft_mock_test.go
1213
func TestMockNFTDetector(t *testing.T) {
13-
detector := NewMockNFTDetector()
14-
1514
t.Run("Known contract", func(t *testing.T) {
16-
detector.Reset()
17-
address := common.HexToAddress("0xBAYC")
18-
detector.AddKnownContract(address)
15+
detector := NewMockNFTDetector() // Create fresh instance
16+
17+
contractAddr := common.HexToAddress("0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D")
18+
19+
// Verify initial state
20+
val, exists := detector.knownContracts.Load(contractAddr)
21+
assert.True(t, exists, "Contract should be in initial known contracts")
22+
assert.True(t, val.(bool), "Contract should be marked as valid")
1923

20-
// Create transaction to the known contract address
2124
tx := types.NewTransaction(
2225
0,
23-
address,
26+
contractAddr,
2427
big.NewInt(0),
2528
21000,
2629
big.NewInt(1),
2730
nil,
2831
)
2932

30-
// Add debug assertions
31-
toAddr := tx.To()
32-
if toAddr == nil {
33-
t.Fatal("Transaction To address is nil")
34-
}
35-
36-
// Verify the addresses match
37-
assert.Equal(t, address, *toAddr, "Transaction address should match known contract")
38-
39-
// Check if transaction is detected as NFT
4033
result := detector.IsNFTTransaction(tx)
41-
assert.True(t, result, "Transaction to known NFT contract address should return true")
34+
assert.True(t, result, "Transaction to known NFT contract should be detected")
4235
})
4336
}

0 commit comments

Comments
 (0)