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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package env
import (
"os"
"path/filepath"
"testing"
"github.com/ethereum-optimism/optimism/devnet-sdk/descriptors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoadDevnetEnv(t *testing.T) {
// Create a temporary test file
content := `{
"l1": {
"name": "l1",
"nodes": [{
"services": {
"el": {
"endpoints": {
"rpc": {
"host": "localhost",
"port": 8545
}
}
}
}
}],
"jwt": "0x1234567890abcdef",
"addresses": {
"deployer": "0x123"
}
},
"l2": [{
"name": "op",
"nodes": [{
"services": {
"el": {
"endpoints": {
"rpc": {
"host": "localhost",
"port": 9545
}
}
}
}
}],
"jwt": "0xdeadbeef",
"addresses": {
"deployer": "0x456"
}
}]
}`
tmpfile, err := os.CreateTemp("", "devnet-*.json")
require.NoError(t, err)
defer os.Remove(tmpfile.Name())
_, err = tmpfile.Write([]byte(content))
require.NoError(t, err)
err = tmpfile.Close()
require.NoError(t, err)
// Test successful load
t.Run("successful load", func(t *testing.T) {
env, err := LoadDevnetEnv(tmpfile.Name())
require.NoError(t, err)
assert.Equal(t, "l1", env.config.L1.Name)
assert.Equal(t, "op", env.config.L2[0].Name)
})
// Test loading non-existent file
t.Run("non-existent file", func(t *testing.T) {
_, err := LoadDevnetEnv("non-existent.json")
assert.Error(t, err)
})
// Test loading invalid JSON
t.Run("invalid JSON", func(t *testing.T) {
invalidFile := filepath.Join(t.TempDir(), "invalid.json")
err := os.WriteFile(invalidFile, []byte("{invalid json}"), 0644)
require.NoError(t, err)
_, err = LoadDevnetEnv(invalidFile)
assert.Error(t, err)
})
}
func TestGetChain(t *testing.T) {
devnet := &DevnetEnv{
config: descriptors.DevnetEnvironment{
L1: &descriptors.Chain{
Name: "l1",
Nodes: []descriptors.Node{
{
Services: descriptors.ServiceMap{
"el": {
Endpoints: descriptors.EndpointMap{
"rpc": {
Host: "localhost",
Port: 8545,
},
},
},
},
},
},
JWT: "0x1234",
},
L2: []*descriptors.Chain{
{
Name: "op",
Nodes: []descriptors.Node{
{
Services: descriptors.ServiceMap{
"el": {
Endpoints: descriptors.EndpointMap{
"rpc": {
Host: "localhost",
Port: 9545,
},
},
},
},
},
},
JWT: "0x5678",
},
},
},
fname: "test.json",
}
// Test getting L1 chain
t.Run("get L1 chain", func(t *testing.T) {
chain, err := devnet.GetChain("l1")
require.NoError(t, err)
assert.Equal(t, "l1", chain.name)
assert.Equal(t, "0x1234", chain.chain.JWT)
})
// Test getting L2 chain
t.Run("get L2 chain", func(t *testing.T) {
chain, err := devnet.GetChain("op")
require.NoError(t, err)
assert.Equal(t, "op", chain.name)
assert.Equal(t, "0x5678", chain.chain.JWT)
})
// Test getting non-existent chain
t.Run("get non-existent chain", func(t *testing.T) {
_, err := devnet.GetChain("invalid")
assert.Error(t, err)
})
}
func TestChainConfig(t *testing.T) {
chain := &ChainConfig{
chain: &descriptors.Chain{
Name: "test",
Nodes: []descriptors.Node{
{
Services: descriptors.ServiceMap{
"el": {
Endpoints: descriptors.EndpointMap{
"rpc": {
Host: "localhost",
Port: 8545,
},
},
},
},
},
},
JWT: "0x1234",
Addresses: map[string]string{
"deployer": "0x123",
},
},
devnetFile: "test.json",
name: "test",
}
// Test getting environment variables
t.Run("get environment variables", func(t *testing.T) {
env, err := chain.GetEnv()
require.NoError(t, err)
assert.Equal(t, "http://localhost:8545", env.EnvVars["ETH_RPC_URL"])
assert.Equal(t, "1234", env.EnvVars["ETH_RPC_JWT_SECRET"])
assert.Equal(t, "test.json", env.EnvVars[EnvFileVar])
assert.Equal(t, "test", env.EnvVars[ChainNameVar])
assert.Contains(t, env.Motd, "deployer")
assert.Contains(t, env.Motd, "0x123")
})
// Test chain with no nodes
t.Run("chain with no nodes", func(t *testing.T) {
noNodesChain := &ChainConfig{
chain: &descriptors.Chain{
Name: "test",
Nodes: []descriptors.Node{},
},
}
_, err := noNodesChain.GetEnv()
assert.Error(t, err)
})
// Test chain with missing service
t.Run("chain with missing service", func(t *testing.T) {
missingServiceChain := &ChainConfig{
chain: &descriptors.Chain{
Name: "test",
Nodes: []descriptors.Node{
{
Services: descriptors.ServiceMap{},
},
},
},
}
_, err := missingServiceChain.GetEnv()
assert.Error(t, err)
})
// Test chain with missing endpoint
t.Run("chain with missing endpoint", func(t *testing.T) {
missingEndpointChain := &ChainConfig{
chain: &descriptors.Chain{
Name: "test",
Nodes: []descriptors.Node{
{
Services: descriptors.ServiceMap{
"el": {
Endpoints: descriptors.EndpointMap{},
},
},
},
},
},
}
_, err := missingEndpointChain.GetEnv()
assert.Error(t, err)
})
}