aboutsummaryrefslogtreecommitdiff
path: root/internal/config/config_load_test.go
blob: 516dd93edb536b2de3f40b078e08f2928642188a (plain) (blame)
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
package config

import (
	"os"
	"path/filepath"
	"testing"
	"time"
)

func TestLoadMissingConfigFileFallsBackToDefaults(t *testing.T) {
	cfg, err := Load(filepath.Join(t.TempDir(), "missing.yaml"))
	if err != nil {
		t.Fatalf("load missing config: %v", err)
	}

	def := Default()
	if cfg.Mode != def.Mode {
		t.Fatalf("mode mismatch: got %q want %q", cfg.Mode, def.Mode)
	}
	if cfg.VerifyPolicy != def.VerifyPolicy {
		t.Fatalf("verify policy mismatch: got %q want %q", cfg.VerifyPolicy, def.VerifyPolicy)
	}
	if cfg.CheckpointInterval != def.CheckpointInterval {
		t.Fatalf("checkpoint interval mismatch: got %d want %d", cfg.CheckpointInterval, def.CheckpointInterval)
	}
	if cfg.VerifyWorkers != def.VerifyWorkers {
		t.Fatalf("verify workers mismatch: got %d want %d", cfg.VerifyWorkers, def.VerifyWorkers)
	}
}

func TestLoadPartialConfigRetainsDefaultValues(t *testing.T) {
	configPath := filepath.Join(t.TempDir(), "partial.yaml")
	content := []byte("mode: thin\nverify: full\n")

	if err := os.WriteFile(configPath, content, 0o644); err != nil {
		t.Fatalf("write partial config: %v", err)
	}

	cfg, err := Load(configPath)
	if err != nil {
		t.Fatalf("load partial config: %v", err)
	}

	if cfg.Mode != ModeThin {
		t.Fatalf("mode mismatch: got %q want %q", cfg.Mode, ModeThin)
	}
	if cfg.VerifyPolicy != VerifyFull {
		t.Fatalf("verify policy mismatch: got %q want %q", cfg.VerifyPolicy, VerifyFull)
	}

	def := Default()
	if cfg.PLCSource != def.PLCSource {
		t.Fatalf("plc_source mismatch: got %q want %q", cfg.PLCSource, def.PLCSource)
	}
	if cfg.RequestTimeout != 10*time.Second {
		t.Fatalf("request_timeout mismatch: got %s want 10s", cfg.RequestTimeout)
	}
	if cfg.CommitBatchSize != def.CommitBatchSize {
		t.Fatalf("commit_batch_size mismatch: got %d want %d", cfg.CommitBatchSize, def.CommitBatchSize)
	}
}

func TestLoadMissingConfigFileAppliesEnvOverrides(t *testing.T) {
	t.Setenv("PLUTIA_MODE", ModeThin)
	t.Setenv("PLUTIA_VERIFY", VerifyFull)
	t.Setenv("PLUTIA_REQUEST_TIMEOUT", "15s")

	cfg, err := Load(filepath.Join(t.TempDir(), "missing.yaml"))
	if err != nil {
		t.Fatalf("load missing config with env overrides: %v", err)
	}

	if cfg.Mode != ModeThin {
		t.Fatalf("mode mismatch: got %q want %q", cfg.Mode, ModeThin)
	}
	if cfg.VerifyPolicy != VerifyFull {
		t.Fatalf("verify policy mismatch: got %q want %q", cfg.VerifyPolicy, VerifyFull)
	}
	if cfg.RequestTimeout != 15*time.Second {
		t.Fatalf("request_timeout mismatch: got %s want 15s", cfg.RequestTimeout)
	}
}