-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintent_test.go
More file actions
451 lines (413 loc) · 12.1 KB
/
intent_test.go
File metadata and controls
451 lines (413 loc) · 12.1 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package seiconfig
import (
"testing"
)
func TestValidateIntent_ValidModes(t *testing.T) {
modes := []NodeMode{ModeValidator, ModeFull, ModeSeed, ModeArchive}
for _, mode := range modes {
result := ValidateIntent(ConfigIntent{Mode: mode})
if !result.Valid {
t.Errorf("mode %q: expected valid, got diagnostics: %v", mode, result.Diagnostics)
}
if result.Mode != mode {
t.Errorf("mode %q: expected mode in result, got %q", mode, result.Mode)
}
if result.Version != CurrentVersion {
t.Errorf("mode %q: expected version %d, got %d", mode, CurrentVersion, result.Version)
}
}
}
func TestValidateIntent_InvalidMode(t *testing.T) {
result := ValidateIntent(ConfigIntent{Mode: "bogus"})
if result.Valid {
t.Fatal("expected invalid result for bogus mode")
}
if len(result.Diagnostics) == 0 {
t.Fatal("expected diagnostics")
}
found := false
for _, d := range result.Diagnostics {
if d.Field == "mode" && d.Severity == SeverityError {
found = true
break
}
}
if !found {
t.Error("expected error-level diagnostic on 'mode' field")
}
}
func TestValidateIntent_EmptyModeNonIncremental(t *testing.T) {
result := ValidateIntent(ConfigIntent{Mode: ""})
if result.Valid {
t.Fatal("expected invalid result for empty mode on non-incremental intent")
}
}
func TestValidateIntent_EmptyModeIncremental(t *testing.T) {
result := ValidateIntent(ConfigIntent{Mode: "", Incremental: true})
if !result.Valid {
t.Errorf("incremental with empty mode should be valid, got: %v", result.Diagnostics)
}
}
func TestValidateIntent_TargetVersionTooHigh(t *testing.T) {
result := ValidateIntent(ConfigIntent{
Mode: ModeFull,
TargetVersion: CurrentVersion + 10,
})
if result.Valid {
t.Fatal("expected invalid for version exceeding CurrentVersion")
}
found := false
for _, d := range result.Diagnostics {
if d.Field == "targetVersion" && d.Severity == SeverityError {
found = true
break
}
}
if !found {
t.Error("expected error on targetVersion field")
}
}
func TestValidateIntent_ExplicitTargetVersion(t *testing.T) {
result := ValidateIntent(ConfigIntent{
Mode: ModeFull,
TargetVersion: 1,
})
if !result.Valid {
t.Errorf("expected valid for explicit version 1, got: %v", result.Diagnostics)
}
if result.Version != 1 {
t.Errorf("expected version 1 in result, got %d", result.Version)
}
}
func TestValidateIntent_ZeroVersionDefaultsToCurrent(t *testing.T) {
result := ValidateIntent(ConfigIntent{
Mode: ModeFull,
TargetVersion: 0,
})
if result.Version != CurrentVersion {
t.Errorf("expected CurrentVersion (%d), got %d", CurrentVersion, result.Version)
}
}
func TestValidateIntent_UnknownOverrideKey(t *testing.T) {
result := ValidateIntent(ConfigIntent{
Mode: ModeFull,
Overrides: map[string]string{
"totally.fake.key": "value",
},
})
if result.Valid {
t.Fatal("expected invalid for unknown override key")
}
found := false
for _, d := range result.Diagnostics {
if d.Field == "overrides.totally.fake.key" {
found = true
break
}
}
if !found {
t.Error("expected diagnostic referencing the unknown override key")
}
}
func TestValidateIntent_ValidOverrideKey(t *testing.T) {
result := ValidateIntent(ConfigIntent{
Mode: ModeFull,
Overrides: map[string]string{
"evm.http_port": "9545",
},
})
if !result.Valid {
t.Errorf("expected valid with known override key, got: %v", result.Diagnostics)
}
}
// ---------------------------------------------------------------------------
// ResolveIntent tests
// ---------------------------------------------------------------------------
func TestResolveIntent_AllModes(t *testing.T) {
modes := []NodeMode{ModeValidator, ModeFull, ModeSeed, ModeArchive}
for _, mode := range modes {
result, err := ResolveIntent(ConfigIntent{Mode: mode})
if err != nil {
t.Errorf("mode %q: unexpected error: %v", mode, err)
continue
}
if !result.Valid {
t.Errorf("mode %q: expected valid, got diagnostics: %v", mode, result.Diagnostics)
continue
}
if result.Config == nil {
t.Errorf("mode %q: expected non-nil config", mode)
continue
}
if result.Config.Mode != mode {
t.Errorf("mode %q: config.Mode = %q", mode, result.Config.Mode)
}
if result.Version != CurrentVersion {
t.Errorf("mode %q: expected version %d, got %d", mode, CurrentVersion, result.Version)
}
}
}
func TestResolveIntent_EmptyModeError(t *testing.T) {
_, err := ResolveIntent(ConfigIntent{Mode: ""})
if err == nil {
t.Fatal("expected error for empty mode")
}
}
func TestResolveIntent_InvalidModeError(t *testing.T) {
_, err := ResolveIntent(ConfigIntent{Mode: "bogus"})
if err == nil {
t.Fatal("expected error for invalid mode")
}
}
func TestResolveIntent_WithOverrides(t *testing.T) {
result, err := ResolveIntent(ConfigIntent{
Mode: ModeFull,
Overrides: map[string]string{
"evm.http_port": "9545",
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !result.Valid {
t.Fatalf("expected valid, got: %v", result.Diagnostics)
}
if result.Config.EVM.HTTPPort != 9545 {
t.Errorf("expected HTTPPort 9545, got %d", result.Config.EVM.HTTPPort)
}
}
func TestResolveIntent_OverrideThatCausesValidationError(t *testing.T) {
result, err := ResolveIntent(ConfigIntent{
Mode: ModeFull,
Overrides: map[string]string{
"chain.min_gas_prices": "",
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Valid {
t.Fatal("expected invalid when override clears a required field")
}
if result.Config != nil {
t.Error("expected nil config when result is invalid")
}
}
func TestResolveIntent_ExplicitTargetVersion(t *testing.T) {
result, err := ResolveIntent(ConfigIntent{
Mode: ModeValidator,
TargetVersion: 1,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Version != 1 {
t.Errorf("expected version 1, got %d", result.Version)
}
}
func TestResolveIntent_ConfigVersionMatchesResultVersion(t *testing.T) {
result, err := ResolveIntent(ConfigIntent{Mode: ModeFull})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Config.Version != result.Version {
t.Errorf("result.Config.Version (%d) != result.Version (%d); "+
"config written to disk would carry the wrong schema version",
result.Config.Version, result.Version)
}
}
func TestResolveIntent_ModeDefaultsApplied(t *testing.T) {
archiveResult, err := ResolveIntent(ConfigIntent{Mode: ModeArchive})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
seedResult, err := ResolveIntent(ConfigIntent{Mode: ModeSeed})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if archiveResult.Config.Storage.PruningStrategy == seedResult.Config.Storage.PruningStrategy {
t.Error("expected different pruning strategies for archive (nothing) vs seed (everything)")
}
}
// ---------------------------------------------------------------------------
// ResolveIncrementalIntent tests
// ---------------------------------------------------------------------------
func TestResolveIncrementalIntent_NilCurrentError(t *testing.T) {
_, err := ResolveIncrementalIntent(ConfigIntent{}, nil)
if err == nil {
t.Fatal("expected error for nil current config")
}
}
func TestResolveIncrementalIntent_PatchesExistingConfig(t *testing.T) {
current := DefaultForMode(ModeFull)
originalPort := current.EVM.HTTPPort
result, err := ResolveIncrementalIntent(
ConfigIntent{
Overrides: map[string]string{
"evm.http_port": "9999",
},
},
current,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !result.Valid {
t.Fatalf("expected valid, got: %v", result.Diagnostics)
}
if result.Config.EVM.HTTPPort != 9999 {
t.Errorf("expected patched HTTPPort 9999, got %d", result.Config.EVM.HTTPPort)
}
if originalPort == 9999 {
t.Error("test setup error: default port should not be 9999")
}
}
func TestResolveIncrementalIntent_ModeOverride(t *testing.T) {
current := DefaultForMode(ModeFull)
result, err := ResolveIncrementalIntent(
ConfigIntent{Mode: ModeArchive},
current,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Config.Mode != ModeArchive {
t.Errorf("expected mode archive, got %q", result.Config.Mode)
}
if result.Mode != ModeArchive {
t.Errorf("expected result.Mode archive, got %q", result.Mode)
}
}
func TestResolveIncrementalIntent_PreservesExistingValues(t *testing.T) {
current := DefaultForMode(ModeValidator)
current.Chain.Moniker = "my-validator"
result, err := ResolveIncrementalIntent(
ConfigIntent{
Overrides: map[string]string{
"evm.http_port": "7777",
},
},
current,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Config.Chain.Moniker != "my-validator" {
t.Errorf("expected moniker preserved, got %q", result.Config.Chain.Moniker)
}
if result.Config.EVM.HTTPPort != 7777 {
t.Errorf("expected patched port, got %d", result.Config.EVM.HTTPPort)
}
}
func TestResolveIncrementalIntent_DoesNotMutateCaller(t *testing.T) {
current := DefaultForMode(ModeFull)
originalMode := current.Mode
originalPort := current.EVM.HTTPPort
_, err := ResolveIncrementalIntent(
ConfigIntent{
Mode: ModeArchive,
Overrides: map[string]string{
"evm.http_port": "9999",
},
},
current,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if current.Mode != originalMode {
t.Errorf("caller's Mode was mutated: got %q, want %q", current.Mode, originalMode)
}
if current.EVM.HTTPPort != originalPort {
t.Errorf("caller's HTTPPort was mutated: got %d, want %d", current.EVM.HTTPPort, originalPort)
}
}
func TestResolveIncrementalIntent_InvalidPatchReturnsInvalid(t *testing.T) {
current := DefaultForMode(ModeFull)
result, err := ResolveIncrementalIntent(
ConfigIntent{
Overrides: map[string]string{
"chain.min_gas_prices": "",
},
},
current,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Valid {
t.Fatal("expected invalid after clearing required field")
}
}
// ---------------------------------------------------------------------------
// ConfigResult helpers
// ---------------------------------------------------------------------------
func TestConfigResult_AddError(t *testing.T) {
r := &ConfigResult{Valid: true}
r.addError("test.field", "something went wrong")
if r.Valid {
t.Error("expected Valid to be false after addError")
}
if len(r.Diagnostics) != 1 {
t.Fatalf("expected 1 diagnostic, got %d", len(r.Diagnostics))
}
if r.Diagnostics[0].Severity != SeverityError {
t.Error("expected error severity")
}
}
func TestConfigResult_AddWarning(t *testing.T) {
r := &ConfigResult{Valid: true}
r.addWarning("test.field", "just a heads up")
if !r.Valid {
t.Error("warnings should not flip Valid to false")
}
if len(r.Diagnostics) != 1 {
t.Fatalf("expected 1 diagnostic, got %d", len(r.Diagnostics))
}
}
// ---------------------------------------------------------------------------
// isZeroValue
// ---------------------------------------------------------------------------
func TestIsZeroValue(t *testing.T) {
tests := []struct {
name string
val any
want bool
}{
{"nil", nil, true},
{"empty string", "", true},
{"non-empty string", "hello", false},
{"zero int", 0, true},
{"non-zero int", 42, false},
{"zero int64", int64(0), true},
{"non-zero int64", int64(1), false},
{"zero uint", uint(0), true},
{"non-zero uint", uint(1), false},
{"zero uint64", uint64(0), true},
{"non-zero uint64", uint64(1), false},
{"zero float64", 0.0, true},
{"non-zero float64", 3.14, false},
{"false bool", false, true},
{"true bool", true, false},
{"unknown type", struct{}{}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isZeroValue(tt.val)
if got != tt.want {
t.Errorf("isZeroValue(%v) = %v, want %v", tt.val, got, tt.want)
}
})
}
}
// ---------------------------------------------------------------------------
// resolveTargetVersion
// ---------------------------------------------------------------------------
func TestResolveTargetVersion(t *testing.T) {
if got := resolveTargetVersion(0); got != CurrentVersion {
t.Errorf("zero should default to CurrentVersion (%d), got %d", CurrentVersion, got)
}
if got := resolveTargetVersion(3); got != 3 {
t.Errorf("explicit 3 should return 3, got %d", got)
}
}