Files
archie/internal/infrastructure/configuration/apply_test.go
T
sam 081fac96dc fix(config): stop a partial overlay clearing the fields it omits
An overlay or dashboard PATCH naming only some fields of a map-valued config entry
silently zeroed the rest. For [services.<name>] that meant losing the bearer token,
so changing a State Store endpoint dropped its authentication:

  start   Services{"state": {Target: "127.0.0.1:50051", TargetToken: "secret"}}
  overlay {"services": {"state": {"target": "10.0.0.5:50051"}}}
  result  Services{"state": {Target: "10.0.0.5:50051", TargetToken: ""}}

Cause: ApplyOverlayValues did yaml.Marshal(overrides) then yaml.Unmarshal into cfg.
yaml gives a struct FIELD the field-level treatment the doc comment promises, but a
map VALUE is decoded into a fresh zero value, so an entry the overlay only partly
addresses loses every field it does not name. That contract was written when
Services was a struct; making it map[string]ServiceConnection changed the behaviour
underneath it.

Fix at the producer: fold the fields the overlay omits forward out of the entry cfg
already holds, so the decoder is handed a complete entry. Recursion follows the
destination type, so the promise now holds for every map of structs -- services,
image.hosted, image.local and providers -- in one place instead of at each field's
reader. Struct field-level merge, scalar-valued map-key merge and slice replacement
are unchanged, and overrides is read, never mutated.

The regression test also converts to the map form, which is what unbreaks the build:
apply_test.go still built Services with the struct form after services.go became a
map, and because the two live in different files the breakage merged cleanly. There
is no Go build or test CI, so nothing compiled it.

Red: four cases failed with the token, the target and the sibling fields zeroed.
Green after the fold.
2026-09-19 03:07:14 +10:00

202 lines
7.4 KiB
Go

package configuration
import (
"log/slog"
"maps"
"os"
"path/filepath"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
)
func TestApplyOverlayValuesFieldLevelMerge(t *testing.T) {
cfg := config.Config{
BotUser: "a",
Budgets: config.Budgets{MaxSteps: 10},
}
overrides := map[string]any{
"budgets": map[string]any{"max_steps": float64(20)},
}
if err := ApplyOverlayValues(&cfg, overrides); err != nil {
t.Fatal(err)
}
if cfg.Budgets.MaxSteps != 20 {
t.Errorf("MaxSteps = %d, want 20", cfg.Budgets.MaxSteps)
}
// Fields the overlay omits keep their value.
if cfg.BotUser != "a" {
t.Errorf("BotUser = %q, want a (unchanged)", cfg.BotUser)
}
}
// TestApplyOverlayValuesPreservesOmittedNestedFields pins ApplyOverlayValues'
// documented promise -- "only the keys present are replaced, every field the
// overlay omits keeps its existing value" -- at the depth where it matters.
//
// The concrete risk is services.state: an overlay that changes only the target
// must not clear target_token, because dropping the token silently removes
// authentication for a non-loopback State Store. The existing field-level test
// only covers one level (budgets.max_steps); these cases go two levels deep into
// a map of structs, which is the shape Services has.
func TestApplyOverlayValuesPreservesOmittedNestedFields(t *testing.T) {
tests := []struct {
name string
start config.Services
overlay map[string]any
want config.Services
}{
{
name: "target-only overlay keeps the token",
start: config.Services{config.ServiceNameState: {Target: "127.0.0.1:50051", TargetToken: "secret"}},
overlay: map[string]any{"services": map[string]any{config.ServiceNameState: map[string]any{"target": "10.0.0.5:50051"}}},
want: config.Services{config.ServiceNameState: {Target: "10.0.0.5:50051", TargetToken: "secret"}},
},
{
name: "token-only overlay keeps the target",
start: config.Services{config.ServiceNameState: {Target: "10.0.0.5:50051", TargetToken: "old"}},
overlay: map[string]any{"services": map[string]any{config.ServiceNameState: map[string]any{"target_token": "rotated"}}},
want: config.Services{config.ServiceNameState: {Target: "10.0.0.5:50051", TargetToken: "rotated"}},
},
{
name: "sibling service is untouched",
start: config.Services{config.ServiceNameGateway: {Target: "127.0.0.1:50052"}, config.ServiceNameState: {Target: "a", TargetToken: "t"}},
overlay: map[string]any{"services": map[string]any{config.ServiceNameState: map[string]any{"target": "b"}}},
want: config.Services{config.ServiceNameGateway: {Target: "127.0.0.1:50052"}, config.ServiceNameState: {Target: "b", TargetToken: "t"}},
},
{
name: "an entry the overlay introduces decodes in full",
start: config.Services{},
overlay: map[string]any{"services": map[string]any{config.ServiceNameState: map[string]any{"target": "127.0.0.1:50051", "target_token": "fresh"}}},
want: config.Services{config.ServiceNameState: {Target: "127.0.0.1:50051", TargetToken: "fresh"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := config.Config{Services: tt.start}
if err := ApplyOverlayValues(&cfg, tt.overlay); err != nil {
t.Fatalf("ApplyOverlayValues: %v", err)
}
if !maps.Equal(cfg.Services, tt.want) {
t.Errorf("Services = %+v, want %+v", cfg.Services, tt.want)
}
})
}
}
// TestApplyOverlayValuesMergesEveryMapEntry pins that the preservation above is
// a property of ApplyOverlayValues rather than of Services. yaml.v3 decodes a
// map value into a fresh zero value, so every map[string]struct field loses the
// fields an override omits; Services, image.hosted and providers are the three
// the configuration has, so a fix that named one of them would leave the other
// two silently zeroing operator settings.
func TestApplyOverlayValuesMergesEveryMapEntry(t *testing.T) {
tests := []struct {
name string
start config.Config
overlay map[string]any
check func(t *testing.T, cfg config.Config)
}{
{
name: "image.hosted entry keeps the fields the overlay omits",
start: config.Config{Image: config.ImageConfig{Hosted: map[string]config.ImageHostedProvider{
"minimax": {Enabled: true, Class: "minimax", APIKeyEnv: "MINIMAX_API_KEY"},
}}},
overlay: map[string]any{"image": map[string]any{"hosted": map[string]any{
"minimax": map[string]any{"base_url": "https://api.example"},
}}},
check: func(t *testing.T, cfg config.Config) {
t.Helper()
want := map[string]config.ImageHostedProvider{
"minimax": {Enabled: true, Class: "minimax", APIKeyEnv: "MINIMAX_API_KEY", BaseURL: "https://api.example"},
}
if !maps.Equal(cfg.Image.Hosted, want) {
t.Errorf("Image.Hosted = %+v, want %+v", cfg.Image.Hosted, want)
}
},
},
{
name: "providers entry keeps the fields the overlay omits",
start: config.Config{Providers: map[string]config.Provider{
"anthropic": {Class: "anthropic", APIKeyEnv: "ANTHROPIC_API_KEY"},
}},
overlay: map[string]any{"providers": map[string]any{
"anthropic": map[string]any{"base_url": "https://proxy.example"},
}},
check: func(t *testing.T, cfg config.Config) {
t.Helper()
want := map[string]config.Provider{
"anthropic": {Class: "anthropic", APIKeyEnv: "ANTHROPIC_API_KEY", BaseURL: "https://proxy.example"},
}
if !maps.Equal(cfg.Providers, want) {
t.Errorf("Providers = %+v, want %+v", cfg.Providers, want)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := tt.start
if err := ApplyOverlayValues(&cfg, tt.overlay); err != nil {
t.Fatalf("ApplyOverlayValues: %v", err)
}
tt.check(t, cfg)
})
}
}
func TestLoaderApplyOverlayLayersDefaultsValidatesAndRecordsProvenance(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte("bot_user = \"widget\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"), 0o600); err != nil {
t.Fatal(err)
}
l := New(slog.New(slog.DiscardHandler))
doc, err := l.Resolve(path, "")
if err != nil {
t.Fatal(err)
}
applied, err := l.ApplyOverlay(doc, map[string]any{"poll_interval": "90s"})
if err != nil {
t.Fatal(err)
}
if applied.Config.PollInterval != config.Duration(90*time.Second) {
t.Errorf("PollInterval = %v, want 90s", applied.Config.PollInterval)
}
if len(applied.Provenance.Origins) != 2 {
t.Errorf("Origins = %+v, want 2 (file + runtime overlay)", applied.Provenance.Origins)
}
// An overlay that fails validation aborts; ApplyOverlay never mutates
// its input, so the base document is untouched even without a copy.
if _, err := l.ApplyOverlay(doc, map[string]any{"dispatch": map[string]any{"trigger": "bogus"}}); err == nil {
t.Fatal("invalid overlay accepted")
}
if doc.Config.Dispatch.Trigger != "assignee" {
t.Errorf("base Dispatch.Trigger = %q, want assignee (defaulted, untouched)", doc.Config.Dispatch.Trigger)
}
}
func TestLoaderApplyOverlayEmptyIsNoOp(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte("bot_user = \"widget\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"), 0o600); err != nil {
t.Fatal(err)
}
l := New(slog.New(slog.DiscardHandler))
doc, err := l.Resolve(path, "")
if err != nil {
t.Fatal(err)
}
applied, err := l.ApplyOverlay(doc, nil)
if err != nil {
t.Fatal(err)
}
if len(applied.Provenance.Origins) != 1 {
t.Errorf("Origins = %+v, want 1 (no overlay origin for an empty overlay)", applied.Provenance.Origins)
}
}