-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
469 lines (391 loc) · 12.3 KB
/
Copy pathconfig_test.go
File metadata and controls
469 lines (391 loc) · 12.3 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package config
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// newTestCommand creates a cobra.Command with all config-related flags registered.
func newTestCommand() *cobra.Command {
cmd := &cobra.Command{}
cmd.Flags().String("config", "", "")
cmd.Flags().String("database-url", "", "")
cmd.Flags().String("api-address", "", "")
cmd.Flags().String("log-level", "", "")
return cmd
}
func TestLoad_Defaults(t *testing.T) {
cmd := newTestCommand()
cfg, err := Load(cmd)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Database.URL != "postgres://mantle:mantle@localhost:5432/mantle?sslmode=prefer" {
t.Errorf("Database.URL = %q, want default", cfg.Database.URL)
}
if cfg.API.Address != ":8080" {
t.Errorf("API.Address = %q, want :8080", cfg.API.Address)
}
if cfg.Log.Level != "info" {
t.Errorf("Log.Level = %q, want info", cfg.Log.Level)
}
}
func TestLoad_ConfigFile(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
err := os.WriteFile(configFile, []byte(`
database:
url: "postgres://custom:5432/mydb"
api:
address: ":9090"
log:
level: "debug"
`), 0644)
if err != nil {
t.Fatalf("WriteFile error = %v", err)
}
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
cfg, err := Load(cmd)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Database.URL != "postgres://custom:5432/mydb" {
t.Errorf("Database.URL = %q, want postgres://custom:5432/mydb", cfg.Database.URL)
}
if cfg.API.Address != ":9090" {
t.Errorf("API.Address = %q, want :9090", cfg.API.Address)
}
if cfg.Log.Level != "debug" {
t.Errorf("Log.Level = %q, want debug", cfg.Log.Level)
}
}
func TestLoad_ExplicitConfigNotFound(t *testing.T) {
cmd := newTestCommand()
_ = cmd.Flags().Set("config", "/nonexistent/mantle.yaml")
_, err := Load(cmd)
if err == nil {
t.Fatal("Load() expected error for missing explicit config, got nil")
}
}
func TestLoad_ImplicitConfigMissing_UsesDefaults(t *testing.T) {
origDir, _ := os.Getwd()
dir := t.TempDir()
_ = os.Chdir(dir)
defer func() { _ = os.Chdir(origDir) }()
cmd := newTestCommand()
cfg, err := Load(cmd)
if err != nil {
t.Fatalf("Load() error = %v, want nil (silent fallback)", err)
}
if cfg.Database.URL != "postgres://mantle:mantle@localhost:5432/mantle?sslmode=prefer" {
t.Errorf("Database.URL = %q, want default", cfg.Database.URL)
}
}
func TestLoad_EnvVarOverridesDefault(t *testing.T) {
t.Setenv("MANTLE_DATABASE_URL", "postgres://envhost:5432/envdb")
t.Setenv("MANTLE_LOG_LEVEL", "warn")
cmd := newTestCommand()
cfg, err := Load(cmd)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Database.URL != "postgres://envhost:5432/envdb" {
t.Errorf("Database.URL = %q, want env override", cfg.Database.URL)
}
if cfg.Log.Level != "warn" {
t.Errorf("Log.Level = %q, want warn", cfg.Log.Level)
}
if cfg.API.Address != ":8080" {
t.Errorf("API.Address = %q, want default :8080", cfg.API.Address)
}
}
func TestLoad_EnvVarOverridesConfigFile(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
_ = os.WriteFile(configFile, []byte(`
database:
url: "postgres://filehost:5432/filedb"
`), 0644)
t.Setenv("MANTLE_DATABASE_URL", "postgres://envhost:5432/envdb")
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
cfg, err := Load(cmd)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Database.URL != "postgres://envhost:5432/envdb" {
t.Errorf("Database.URL = %q, want env override over file", cfg.Database.URL)
}
}
func TestConfig_EngineDefaults(t *testing.T) {
cmd := newTestCommand()
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, 200*time.Millisecond, cfg.Engine.WorkerPollInterval)
assert.Equal(t, 5*time.Second, cfg.Engine.WorkerMaxBackoff)
assert.Equal(t, 500*time.Millisecond, cfg.Engine.OrchestratorPollInterval)
assert.Equal(t, 60*time.Second, cfg.Engine.StepLeaseDuration)
assert.Equal(t, 120*time.Second, cfg.Engine.OrchestrationLeaseDuration)
assert.Equal(t, 300*time.Second, cfg.Engine.AIStepLeaseDuration)
assert.Equal(t, 30*time.Second, cfg.Engine.ReaperInterval)
assert.Equal(t, 1048576, cfg.Engine.StepOutputMaxBytes)
assert.Equal(t, 10, cfg.Engine.DefaultMaxToolRounds)
assert.Equal(t, 10, cfg.Engine.DefaultMaxToolCallsPerRound)
assert.NotEmpty(t, cfg.Engine.NodeID)
}
func TestLoad_FlagOverridesAll(t *testing.T) {
t.Setenv("MANTLE_DATABASE_URL", "postgres://envhost:5432/envdb")
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
_ = os.WriteFile(configFile, []byte(`
database:
url: "postgres://filehost:5432/filedb"
log:
level: "debug"
`), 0644)
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
_ = cmd.Flags().Set("database-url", "postgres://flaghost:5432/flagdb")
_ = cmd.Flags().Set("log-level", "error")
cfg, err := Load(cmd)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Database.URL != "postgres://flaghost:5432/flagdb" {
t.Errorf("Database.URL = %q, want flag override", cfg.Database.URL)
}
if cfg.Log.Level != "error" {
t.Errorf("Log.Level = %q, want error (flag override)", cfg.Log.Level)
}
}
func TestLoad_BudgetDefaults(t *testing.T) {
cmd := newTestCommand()
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, "calendar", cfg.Engine.Budget.ResetMode)
assert.Equal(t, 1, cfg.Engine.Budget.ResetDay)
assert.Equal(t, int64(0), cfg.Engine.Budget.GlobalMonthlyTokenLimit)
assert.Equal(t, int64(0), cfg.Engine.Budget.DefaultTeamMonthlyTokenLimit)
}
func TestLoad_BudgetResetDay_RollingInvalid(t *testing.T) {
// ResetDay 0 with rolling mode should error
t.Setenv("MANTLE_ENGINE_BUDGET_RESET_MODE", "rolling")
t.Setenv("MANTLE_ENGINE_BUDGET_RESET_DAY", "0")
cmd := newTestCommand()
_, err := Load(cmd)
require.Error(t, err)
assert.Contains(t, err.Error(), "reset_day must be between 1 and 28")
// ResetDay 29 with rolling mode should error
t.Setenv("MANTLE_ENGINE_BUDGET_RESET_DAY", "29")
cmd = newTestCommand()
_, err = Load(cmd)
require.Error(t, err)
assert.Contains(t, err.Error(), "reset_day must be between 1 and 28")
}
func TestLoad_BudgetResetDay_CalendarClamps(t *testing.T) {
// Invalid ResetDay with calendar mode should silently clamp to 1 (lower bound)
t.Setenv("MANTLE_ENGINE_BUDGET_RESET_MODE", "calendar")
t.Setenv("MANTLE_ENGINE_BUDGET_RESET_DAY", "0")
cmd := newTestCommand()
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, 1, cfg.Engine.Budget.ResetDay)
}
func TestLoad_BudgetResetDay_CalendarClampsUpperBound(t *testing.T) {
// ResetDay >28 with calendar mode should silently clamp to 1
// (calendar mode ignores reset_day entirely, so any invalid value is safe to clamp)
t.Setenv("MANTLE_ENGINE_BUDGET_RESET_MODE", "calendar")
t.Setenv("MANTLE_ENGINE_BUDGET_RESET_DAY", "100")
cmd := newTestCommand()
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, 1, cfg.Engine.Budget.ResetDay)
}
func TestLoad_StorageConfigFromEnvVars(t *testing.T) {
t.Setenv("MANTLE_STORAGE_TYPE", "s3")
t.Setenv("MANTLE_STORAGE_BUCKET", "my-artifacts")
t.Setenv("MANTLE_STORAGE_PREFIX", "workflows/")
t.Setenv("MANTLE_STORAGE_RETENTION", "24h")
cmd := newTestCommand()
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, "s3", cfg.Storage.Type)
assert.Equal(t, "my-artifacts", cfg.Storage.Bucket)
assert.Equal(t, "workflows/", cfg.Storage.Prefix)
assert.Equal(t, "24h", cfg.Storage.Retention)
}
func TestLoad_StorageConfigFromConfigFile(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
err := os.WriteFile(configFile, []byte(`
storage:
type: "filesystem"
path: "/tmp/mantle-artifacts"
retention: "48h"
`), 0644)
require.NoError(t, err)
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, "filesystem", cfg.Storage.Type)
assert.Equal(t, "/tmp/mantle-artifacts", cfg.Storage.Path)
assert.Equal(t, "48h", cfg.Storage.Retention)
}
func TestLoad_VersionValidation(t *testing.T) {
tests := []struct {
name string
yaml string
wantErr string // empty means no error expected
}{
{
name: "version 1 is valid",
yaml: "version: 1\n",
wantErr: "",
},
{
name: "missing version defaults to 1",
yaml: "log:\n level: info\n",
wantErr: "",
},
{
name: "version 0 is rejected",
yaml: "version: 0\n",
wantErr: "unsupported config version 0; this version of mantle supports config version 1",
},
{
name: "version 2 is rejected",
yaml: "version: 2\n",
wantErr: "unsupported config version 2; this version of mantle supports config version 1",
},
{
name: "version 99 is rejected",
yaml: "version: 99\n",
wantErr: "unsupported config version 99; this version of mantle supports config version 1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
err := os.WriteFile(configFile, []byte(tt.yaml), 0644)
require.NoError(t, err)
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
cfg, err := Load(cmd)
if tt.wantErr != "" {
require.Error(t, err)
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("error %q does not contain %q", err.Error(), tt.wantErr)
}
} else {
require.NoError(t, err)
assert.Equal(t, 1, cfg.Version)
}
})
}
}
func TestLoad_StorageConfigEnvVarOverridesFile(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
_ = os.WriteFile(configFile, []byte(`
storage:
type: "filesystem"
path: "/tmp/file-path"
`), 0644)
t.Setenv("MANTLE_STORAGE_TYPE", "s3")
t.Setenv("MANTLE_STORAGE_BUCKET", "env-bucket")
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, "s3", cfg.Storage.Type)
assert.Equal(t, "env-bucket", cfg.Storage.Bucket)
// path from file should still be present since env var didn't override it
assert.Equal(t, "/tmp/file-path", cfg.Storage.Path)
}
func TestLoad_StorageSectionUsedDirectly(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
_ = os.WriteFile(configFile, []byte(`
storage:
type: "s3"
bucket: "my-bucket"
`), 0644)
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, "s3", cfg.Storage.Type)
assert.Equal(t, "my-bucket", cfg.Storage.Bucket)
}
func TestLoad_TmpFallbackToStorageWithWarning(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
_ = os.WriteFile(configFile, []byte(`
tmp:
type: "filesystem"
path: "/tmp/legacy-artifacts"
`), 0644)
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, "filesystem", cfg.Storage.Type)
assert.Equal(t, "/tmp/legacy-artifacts", cfg.Storage.Path)
}
func TestLoad_StorageTakesPrecedenceOverTmp(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
_ = os.WriteFile(configFile, []byte(`
storage:
type: "s3"
bucket: "new-bucket"
tmp:
type: "filesystem"
path: "/tmp/old-path"
`), 0644)
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, "s3", cfg.Storage.Type)
assert.Equal(t, "new-bucket", cfg.Storage.Bucket)
}
func TestLoad_EnvMap(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
err := os.WriteFile(configFile, []byte(`
env:
APP_NAME: "my-app"
REGION: "us-east-1"
DEBUG: "true"
`), 0644)
require.NoError(t, err)
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Equal(t, "my-app", cfg.Env["APP_NAME"])
assert.Equal(t, "us-east-1", cfg.Env["REGION"])
assert.Equal(t, "true", cfg.Env["DEBUG"])
}
func TestLoad_EnvMapEmpty(t *testing.T) {
dir := t.TempDir()
configFile := filepath.Join(dir, "mantle.yaml")
err := os.WriteFile(configFile, []byte(`
log:
level: "debug"
`), 0644)
require.NoError(t, err)
cmd := newTestCommand()
_ = cmd.Flags().Set("config", configFile)
cfg, err := Load(cmd)
require.NoError(t, err)
assert.Empty(t, cfg.Env)
}