-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathextra_selectors_test.go
More file actions
231 lines (196 loc) · 6.19 KB
/
extra_selectors_test.go
File metadata and controls
231 lines (196 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package chain_selectors
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func createTempYamlFile(t *testing.T, yamlContent string) string {
tmpFile, err := os.CreateTemp("", "test_*.yaml")
require.NoError(t, err)
_, err = tmpFile.WriteString(yamlContent)
require.NoError(t, err)
tmpFile.Close()
return tmpFile.Name()
}
func setSelectorEnv(_ *testing.T, filePath string) func() {
os.Setenv("EXTRA_SELECTORS_FILE", filePath)
return func() {
os.Unsetenv("EXTRA_SELECTORS_FILE")
}
}
func runTestWithYaml(t *testing.T, testName string, yamlContent string, validate func(*testing.T, ExtraSelectorsData)) {
t.Run(testName, func(t *testing.T) {
filePath := createTempYamlFile(t, yamlContent)
defer os.Remove(filePath)
cleanup := setSelectorEnv(t, filePath)
defer cleanup()
result := loadAndParseExtraSelectors()
validate(t, result)
})
}
const (
yamlSingleFamily = `
evm:
999:
selector: 1234567890123456789
name: "test-evm-chain"
`
yamlMultipleFamilies = `
evm:
999:
selector: 1234567890123456789
name: "test-evm-chain"
solana:
"ASwXBTzJM5evpfrWSHSjZaxPErZRuiGJnFixGUHi4NQT": #Random solana chainID
selector: 1111111111111111111
name: "test-solana-chain"
ton:
-666:
selector: 3333333333333333333
name: "test-ton-chain"
aptos:
888:
selector: 9876543210987654321
name: "test-aptos-chain"
network_type: testnet
sui:
777:
selector: 2222222222222222222
name: "test-sui-chain"
tron:
123:
selector: 123557890123456789
name: "test-tron-chain"
starknet:
"TEST_SN":
selector: 1111111111111111111
name: "test-starknet-chain"
`
)
func TestExtraSelectors(t *testing.T) {
runTestWithYaml(t, "Extra selectors: single family", yamlSingleFamily, func(t *testing.T, result ExtraSelectorsData) {
assert.Len(t, result.Evm, 1)
assert.Empty(t, result.Aptos)
assert.Empty(t, result.Solana)
assert.Empty(t, result.Sui)
assert.Empty(t, result.Ton)
assert.Empty(t, result.Tron)
assert.Empty(t, result.Starknet)
evmChain, exists := result.Evm[999]
assert.True(t, exists)
assert.Equal(t, uint64(1234567890123456789), evmChain.ChainSelector)
assert.Equal(t, "test-evm-chain", evmChain.ChainName)
})
runTestWithYaml(t, "Extra selectors: multiple families", yamlMultipleFamilies, func(t *testing.T, result ExtraSelectorsData) {
assert.Len(t, result.Evm, 1)
assert.Len(t, result.Aptos, 1)
assert.Len(t, result.Sui, 1)
assert.Len(t, result.Ton, 1)
assert.Len(t, result.Tron, 1)
assert.Len(t, result.Starknet, 1)
aptosChain, exists := result.Aptos[888]
assert.True(t, exists)
assert.Equal(t, uint64(9876543210987654321), aptosChain.ChainSelector)
assert.Equal(t, "test-aptos-chain", aptosChain.ChainName)
suiChain, exists := result.Sui[777]
assert.True(t, exists)
assert.Equal(t, uint64(2222222222222222222), suiChain.ChainSelector)
assert.Equal(t, "test-sui-chain", suiChain.ChainName)
})
runTestWithYaml(t, "Extra selectors: empty YAML file", ``, func(t *testing.T, result ExtraSelectorsData) {
assert.Empty(t, result.Evm)
assert.Empty(t, result.Aptos)
assert.Empty(t, result.Solana)
assert.Empty(t, result.Sui)
assert.Empty(t, result.Ton)
assert.Empty(t, result.Tron)
assert.Empty(t, result.Starknet)
})
}
func TestExtraSelectorsInvalidFormat(t *testing.T) {
t.Run("Invalid YAML syntax should panic", func(t *testing.T) {
invalidYaml := `
evm:
999:
selector: 1234567890123456789
name: "test-evm-chain"
invalid: yaml: syntax: [unclosed
`
filePath := createTempYamlFile(t, invalidYaml)
defer os.Remove(filePath)
cleanup := setSelectorEnv(t, filePath)
defer cleanup()
assert.Panics(t, func() {
loadAndParseExtraSelectors()
}, "Expected panic for invalid YAML syntax")
})
t.Run("Invalid chain ID type should panic", func(t *testing.T) {
invalidEvmChainIdYaml := `
evm:
"abc": # string
selector: 1234567890123456789
name: "test-evm-chain"
`
filePath := createTempYamlFile(t, invalidEvmChainIdYaml)
defer os.Remove(filePath)
cleanup := setSelectorEnv(t, filePath)
defer cleanup()
assert.Panics(t, func() {
loadAndParseExtraSelectors()
}, "Expected panic for invalid EVM chain ID type")
})
t.Run("Non-existent file should panic", func(t *testing.T) {
cleanup := setSelectorEnv(t, "/non/existent/file.yaml")
defer cleanup()
assert.Panics(t, func() {
loadAndParseExtraSelectors()
}, "Expected panic for non-existent file")
})
}
func TestExtraSelectorsE2E(t *testing.T) {
var err error
var cwd string
cwd, err = os.Getwd()
require.NoError(t, err)
testFilePath := filepath.Join(cwd, "test_extra_selectors.yml")
envFilePath := filepath.Join(cwd, os.Getenv("EXTRA_SELECTORS_FILE"))
if envFilePath != testFilePath {
t.Skipf("Skipping test because EXTRA_SELECTORS_FILE is not set to %s", testFilePath)
}
var selector uint64
var chainID uint64
//Should return data for valid addition
chainID, err = ChainIdFromSelector(1234567890123456789)
assert.NoError(t, err)
assert.Equal(t, uint64(90909090111), chainID)
selector, err = SelectorFromChainId(90909090111)
assert.NoError(t, err)
assert.Equal(t, uint64(1234567890123456789), selector)
name, err := NameFromChainId(90909090111)
assert.NoError(t, err)
assert.Equal(t, "test-evm-chain", name)
//Should not override chain id that already exists in selectors.yml
selector, err = SelectorFromChainId(999)
assert.NoError(t, err)
chain, ok := ChainBySelector(2442541497099098535)
assert.Equal(t, true, ok)
assert.Equal(t, "hyperliquid-mainnet", chain.Name)
assert.Equal(t, uint64(2442541497099098535), selector)
name, err = NameFromChainId(999)
assert.NoError(t, err)
assert.Equal(t, "hyperliquid-mainnet", name)
}
// Validates a custom provide file for formating errors. This can be used in external CI checks to ensure the file is valid.
func TestExtraSelectorsValidateCustomFile(t *testing.T) {
extraSelectorsFile := os.Getenv("EXTRA_SELECTORS_FILE")
if extraSelectorsFile == "" {
t.Skip("EXTRA_SELECTORS_FILE environment variable is not set")
return
}
// loadAndParseExtraSelectors will panic if the file has errors
assert.NotPanics(t, func() {
loadAndParseExtraSelectors()
}, "Loading extra selectors file should not panic if file is valid")
}