Files
archie/internal/infrastructure/configuration/apply_test.go
T
sam da2d93a830 feat(config): runtime config overlay store and PATCH /api/config
The second half of #348's storage layer: dashboard-edited overrides
live in their own SQLite file, separate from the task and conversation
stores so each file owns its user_version and migrator with no
contention (configDBPath -> cfg.DBPath + "-config.sqlite", matching
the taskDBPath / conversationDBPath convention).

Store (internal/infrastructure/configuration/overlay):
- config_overlay(key TEXT PRIMARY KEY, value TEXT NOT NULL,
  updated_at, updated_by), own migration, WAL.
- Set refuses DeniedKeys (db_path, work_dir) at write time as well as
  at the API layer; the map is exported so the dashboard can show the
  reason instead of silently omitting the edit affordance.
- Snapshot JSON-decodes each value and nests dotted keys
  ("budgets.max_steps" -> map[b][m]) ready to decode into Config.

Loader (ApplyOverlay / ApplyOverlayValues):
- Decodes a nested overlay map with the same field-level precedence as
  a file overlay, records the runtime-overlay origin, then applies
  defaults and validates. Used at boot, on SIGHUP reload (ReloadController
  gained an optional WithOverlay source) and by the PATCH path.

Boot and reload:
- A broken overlay (unopenable store, unreadable snapshot, values that
  fail validation) degrades to file config alone -- it must never brick
  the daemon. --no-config-overlay / ARCHIE_SKIP_CONFIG_OVERLAY=1 skips
  the store entirely; the flag is now a real gate, not a log-only stub.

PATCH /api/config (webui):
- handleConfigUpdate decodes {"updates": {dotted.key: value}}, calls
  the composition-wired UpdateConfig seam, and classifies errors via
  webui sentinels: ErrConfigUpdateInvalid -> 400 (denylisted key,
  failed validation, unparseable value), ErrConfigUpdateUnavailable ->
  503 (overlay skipped by recovery flag), anything else -> 500.
- The seam (main.go) applies updates to a COPY of the published
  config, validates the materialised result, persists to the overlay,
  then publishes through the same path as reload -- it never calls
  Set directly from the handler, since web.Cfg is the daemon's own
  Holder.
- ConfigView gains Locked (dotted key -> reason) so the UI disables
  denylisted rows explicitly.

Verified: task check clean; new tests cover the store round-trip,
denied keys, loader overlay layering/validation/provenance, reload
with and without overlay, and the PATCH handler's 200/400/503 paths.
2026-08-10 00:54:23 +10:00

89 lines
2.6 KiB
Go

package configuration
import (
"log/slog"
"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)
}
// Unrelated nested fields keep their value too.
if cfg.Budgets.MaxTokens != 0 {
t.Errorf("MaxTokens = %d, want 0 (untouched)", cfg.Budgets.MaxTokens)
}
}
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; the base document is
// untouched when a copy is passed.
next := *doc
if _, err := l.ApplyOverlay(&next, map[string]any{"agent": map[string]any{"mode": "bogus"}}); err == nil {
t.Fatal("invalid overlay accepted")
}
if doc.Config.Agent.Mode != "inprocess" {
t.Errorf("base Agent.Mode = %q, want inprocess (defaulted, untouched)", doc.Config.Agent.Mode)
}
}
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)
}
}