Files
archie/internal/config/config_test.go
T
sam c66c0a2a38 refactor(config): move loading into infrastructure/configuration
internal/config both defined the types every package imports and performed
the file I/O to produce them. Importing a settings struct therefore pulled
in a TOML decoder and a YAML decoder, and the shared model owned discovery,
decoding, overlays, defaulting and validation all at once. See
docs/architecture/configuration.md, which requires loading to move out and
internal/config to be dissolved.

internal/config is now types only -- no os, no filepath, no toml. The new
package owns loading, one concern per file:

  features.go    the closed set of recognised feature files
  discovery.go   directory scan and classification
  decode.go      TOML/YAML decoding, format dispatch
  defaults.go    filling absent values, no errors
  validate.go    judging supplied values, no mutation
  provenance.go  which file contributed what
  loader.go      Loader wiring the above; File/Overlay/Dir

Beyond relocating, this fixes what the old code conflated:

  - Defaulting and validation were interleaved. applyAgentDefaults and
    applyDispatchDefaults returned validation errors; validateContainers and
    validateSingleIdentity quietly mutated the config. There was no point at
    which you could see what the operator actually supplied. They are now
    separate passes: applyDefaults never fails, validate never writes.
  - Errors were fmt.Errorf strings prefixed "config: ", so a caller could not
    distinguish operator-fixable input from an unreadable file without
    matching message text. Five sentinels now carry that distinction.
  - dataHome() read XDG_DATA_HOME directly and discarded the error from
    os.UserHomeDir, so defaulting could not be tested without the
    environment. It is injectable on Loader and no longer swallows the error.
  - Nothing recorded where a value came from. With overlays, feature files
    and conf.d/ all able to set the same field, "which file set this?" was
    answerable only by re-deriving precedence by hand. Provenance records it.
  - Feature files were applied in map iteration order, making load order and
    any resulting error arbitrary. Discovery now returns them sorted.
  - loadMainConfig reported "no config.yaml or config.toml found in %s" with
    an empty string: the directory was lost when it was extracted from
    loadDirInto, so the error named no directory.
  - Unreadable or malformed conf.d/ extras were skipped silently, which
    presents as a setting the operator wrote simply not taking effect.
  - Decode errors from the main config were returned bare, without naming
    the file that failed to parse.

The 40 loader tests moved with the code and their assertions are unchanged,
so the defaulting and validation rewrite is behaviour-preserving.

Document.Config is the temporary seam: still internal/config.Config, named
as a field so the compromise is visible at each use site, with its deletion
condition recorded in doc.go. Building typed per-domain sections now, while
every consumer still reads config.Config, would mean two shapes kept in sync
by hand with no consumer for the new one; that belongs with the dissolution
work, not ahead of it.

No consumer outside cmd/archied changed.

Verified: go build, go vet, gofmt clean; go test ./internal/config/... and
./internal/infrastructure/configuration/... pass. Pre-existing gateway and
telegram failures are unchanged.
2026-07-29 11:07:41 +10:00

420 lines
12 KiB
Go

package config
import (
"encoding/json"
"reflect"
"strings"
"testing"
"time"
"github.com/BurntSushi/toml"
"github.com/samcharles93/archie-core/internal/secret"
)
func TestTaskConfigToConfigRoundTrip(t *testing.T) {
cfg := Config{
DiffCapLines: 321,
Models: map[string]string{"builder": "anthropic/claude"},
Budgets: Budgets{MaxSteps: 12, MaxTokens: 34_000, WallClock: Duration(45 * time.Minute), GateMaxFailures: 3},
Dispatch: Dispatch{Trigger: "label", AckReaction: "eyes", Labels: map[string]string{"working": "bot:working"}},
Notify: Notify{Webhook: "https://notify.example.test/hook"},
Forge: Forge{Type: "github", Host: "https://forge.example.test", Token: secret.SecretRef{Engine: "env", Key: "TOP_SECRET"}},
}
got := cfg.ForTask().ToConfig()
if !reflect.DeepEqual(got.Models, cfg.Models) {
t.Fatalf("Models = %#v, want %#v", got.Models, cfg.Models)
}
if got.Budgets != cfg.Budgets {
t.Fatalf("Budgets = %#v, want %#v", got.Budgets, cfg.Budgets)
}
if !reflect.DeepEqual(got.Dispatch, cfg.Dispatch) {
t.Fatalf("Dispatch = %#v, want %#v", got.Dispatch, cfg.Dispatch)
}
if got.DiffCapLines != cfg.DiffCapLines {
t.Fatalf("DiffCapLines = %d, want %d", got.DiffCapLines, cfg.DiffCapLines)
}
if got.Notify != cfg.Notify {
t.Fatalf("Notify = %#v, want %#v", got.Notify, cfg.Notify)
}
if got.Forge.Host != cfg.Forge.Host {
t.Fatalf("Forge.Host = %q, want %q", got.Forge.Host, cfg.Forge.Host)
}
if got.Forge.Token != (secret.SecretRef{}) {
t.Fatalf("Forge.Token = %#v, want zero value (never carried by TaskConfig)", got.Forge.Token)
}
}
func TestMCPServerTOMLIncludesClientWorkingDirectory(t *testing.T) {
var cfg Config
if _, err := toml.Decode(`
[[tools.mcp_servers]]
name = "desktop-commander"
transport = "stdio"
command = "npx"
args = ["-y", "@wonderwhy-er/desktop-commander@0.2.46", "--no-onboarding"]
work_dir = "/workspace"
`, &cfg); err != nil {
t.Fatal(err)
}
if len(cfg.Tools.MCPServers) != 1 {
t.Fatalf("MCPServers = %#v, want one server", cfg.Tools.MCPServers)
}
server := cfg.Tools.MCPServers[0]
if server.Name != "desktop-commander" || server.Command != "npx" || server.WorkDir != "/workspace" {
t.Fatalf("MCP server = %#v", server)
}
if len(server.Args) != 3 || server.Args[1] != "@wonderwhy-er/desktop-commander@0.2.46" {
t.Fatalf("MCP args = %v", server.Args)
}
}
func TestChatModelCatalogDecodesFromTOML(t *testing.T) {
var cfg Config
if _, err := toml.Decode(`
[chat]
models = ["openai/gpt-5.6-sol", "openrouter/openai/gpt-5.6-sol"]
`, &cfg); err != nil {
t.Fatal(err)
}
if len(cfg.Chat.Models) != 2 || cfg.Chat.Models[1] != "openrouter/openai/gpt-5.6-sol" {
t.Fatalf("chat models = %v", cfg.Chat.Models)
}
}
func TestConfigForTaskJSONRoundTrip(t *testing.T) {
cfg := Config{
DiffCapLines: 321,
Models: map[string]string{
"builder": "anthropic/claude",
"planner": "openai/gpt",
},
Budgets: Budgets{
MaxSteps: 12,
MaxTokens: 34_000,
WallClock: Duration(45 * time.Minute),
GateMaxFailures: 3,
},
Dispatch: Dispatch{
Trigger: "label",
AckReaction: "eyes",
Labels: map[string]string{"working": "bot:working"},
},
Notify: Notify{Webhook: "https://notify.example.test/hook"},
Forge: Forge{
Type: "github",
Host: "https://forge.example.test",
Token: secret.SecretRef{Engine: "env", Key: "TOP_SECRET_FORGE_TOKEN"},
},
Providers: map[string]Provider{
"secret": {APIKeyEnv: "TOP_SECRET_PROVIDER_TOKEN"},
},
NATS: NATSConfig{TokenEnv: "TOP_SECRET_NATS_TOKEN"},
}
want := TaskConfig{
Models: cfg.Models,
Budgets: cfg.Budgets,
Dispatch: cfg.Dispatch,
DiffCapLines: cfg.DiffCapLines,
Notify: cfg.Notify,
Forge: TaskForge{Host: cfg.Forge.Host},
}
got := cfg.ForTask()
if !reflect.DeepEqual(got, want) {
t.Fatalf("ForTask() = %#v, want %#v", got, want)
}
cfg.Models["source-only"] = "source/model"
if _, ok := got.Models["source-only"]; ok {
t.Fatal("ForTask().Models aliases Config.Models")
}
got.Models["task-only"] = "task/model"
if _, ok := cfg.Models["task-only"]; ok {
t.Fatal("Config.Models aliases ForTask().Models")
}
cfg.Dispatch.Labels["source-only"] = "source:label"
if _, ok := got.Dispatch.Labels["source-only"]; ok {
t.Fatal("ForTask().Dispatch.Labels aliases Config.Dispatch.Labels")
}
got.Dispatch.Labels["task-only"] = "task:label"
if _, ok := cfg.Dispatch.Labels["task-only"]; ok {
t.Fatal("Config.Dispatch.Labels aliases ForTask().Dispatch.Labels")
}
data, err := json.Marshal(got)
if err != nil {
t.Fatal(err)
}
assertJSONKeys(t, data, "models", "budgets", "dispatch", "diff_cap_lines", "notify", "forge")
var forgePayload map[string]json.RawMessage
var payload map[string]json.RawMessage
if err := json.Unmarshal(data, &payload); err != nil {
t.Fatal(err)
}
var budgetsPayload map[string]json.RawMessage
if err := json.Unmarshal(payload["budgets"], &budgetsPayload); err != nil {
t.Fatal(err)
}
if string(budgetsPayload["wall_clock"]) != `"45m0s"` {
t.Fatalf("wall_clock = %s, want JSON duration string", budgetsPayload["wall_clock"])
}
if err := json.Unmarshal(payload["forge"], &forgePayload); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(forgePayload, map[string]json.RawMessage{
"host": json.RawMessage(`"https://forge.example.test"`),
}) {
t.Fatalf("forge payload = %s, want only host", payload["forge"])
}
for _, ref := range []string{
cfg.Forge.Token.Key,
cfg.Providers["secret"].APIKeyEnv,
cfg.NATS.TokenEnv,
} {
if strings.Contains(string(data), ref) {
t.Fatalf("task config JSON contains credential reference %q: %s", ref, data)
}
}
var roundTripped TaskConfig
if err := json.Unmarshal(data, &roundTripped); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(roundTripped, got) {
t.Fatalf("JSON round trip = %#v, want %#v", roundTripped, got)
}
}
func TestRepoJSONRoundTrip(t *testing.T) {
repo := Repo{
Owner: "acme",
Name: "widget",
Base: "develop",
Gate: [][]string{{"task", "check"}, {"go", "test", "./..."}},
Protect: []string{"_templ.go", ".generated"},
Ecosystem: "go",
Preflight: [][]string{{"go", "version"}},
TestGlob: "*_test.go",
PersistentStorage: true,
MaxRetries: 4,
AllowConcurrent: true,
}
data, err := json.Marshal(repo)
if err != nil {
t.Fatal(err)
}
assertJSONKeys(t, data,
"owner",
"name",
"base",
"gate",
"protect",
"ecosystem",
"preflight",
"test_glob",
"persistent_storage",
"max_retries",
"allow_concurrent",
)
var got Repo
if err := json.Unmarshal(data, &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, repo) {
t.Fatalf("JSON round trip = %#v, want %#v", got, repo)
}
}
func assertJSONKeys(t *testing.T, data []byte, want ...string) {
t.Helper()
var payload map[string]json.RawMessage
if err := json.Unmarshal(data, &payload); err != nil {
t.Fatal(err)
}
got := make(map[string]bool, len(payload))
for key := range payload {
got[key] = true
}
wantSet := make(map[string]bool, len(want))
for _, key := range want {
wantSet[key] = true
}
if !reflect.DeepEqual(got, wantSet) {
t.Fatalf("JSON keys = %v, want %v", got, wantSet)
}
}
func TestDispatchDefaults(t *testing.T) {
// Simulate a config with no [dispatch] section -- all fields zero.
var d Dispatch
d.Labels = map[string]string{}
// Per-key defaults (mimicking Load's loop).
for k, v := range dispatchLabelDefaults {
if d.Labels[k] == "" {
d.Labels[k] = v
}
}
if d.Trigger == "" {
d.Trigger = "assignee"
}
if d.AckReaction == "" {
d.AckReaction = "eyes"
}
// StateLabel should return the default agent:* labels.
if got := d.StateLabel("queued"); got != "agent:queued" {
t.Errorf("queued: got %q, want agent:queued", got)
}
if got := d.StateLabel("working"); got != "agent:working" {
t.Errorf("working: got %q, want agent:working", got)
}
if got := d.StateLabel("waiting"); got != "agent:waiting" {
t.Errorf("waiting: got %q, want agent:waiting", got)
}
if got := d.StateLabel("pr"); got != "agent:pr" {
t.Errorf("pr: got %q, want agent:pr", got)
}
if got := d.StateLabel("parked"); got != "agent:parked" {
t.Errorf("parked: got %q, want agent:parked", got)
}
// Unknown state returns empty.
if got := d.StateLabel("nonexistent"); got != "" {
t.Errorf("nonexistent: got %q, want empty", got)
}
// AckReaction default.
if d.AckReaction != "eyes" {
t.Errorf("AckReaction: got %q, want eyes", d.AckReaction)
}
// Trigger default.
if d.Trigger != "assignee" {
t.Errorf("Trigger: got %q, want assignee", d.Trigger)
}
}
func TestDispatchCustomLabels(t *testing.T) {
d := Dispatch{
Trigger: "label",
AckReaction: "",
Labels: map[string]string{
"queued": "bot:queued",
"working": "bot:working",
"parked": "bot:parked",
// "waiting" and "pr" intentionally missing -- should fall back.
},
}
// Custom labels.
if got := d.StateLabel("queued"); got != "bot:queued" {
t.Errorf("queued: got %q, want bot:queued", got)
}
if got := d.StateLabel("parked"); got != "bot:parked" {
t.Errorf("parked: got %q, want bot:parked", got)
}
// Missing keys fall back to defaults.
if got := d.StateLabel("waiting"); got != "agent:waiting" {
t.Errorf("waiting: got %q, want agent:waiting (fallback)", got)
}
if got := d.StateLabel("pr"); got != "agent:pr" {
t.Errorf("pr: got %q, want agent:pr (fallback)", got)
}
// Empty AckReaction.
if d.AckReaction != "" {
t.Errorf("AckReaction: got %q, want empty", d.AckReaction)
}
// LabelValues returns all six state labels.
vals := d.LabelValues()
if len(vals) != 6 {
t.Errorf("LabelValues: got %d values, want 6: %v", len(vals), vals)
}
seen := map[string]bool{}
for _, v := range vals {
seen[v] = true
}
for _, want := range []string{"bot:queued", "bot:working", "bot:parked", "agent:waiting", "agent:pr", "agent:dead"} {
if !seen[want] {
t.Errorf("LabelValues missing %q", want)
}
}
}
func TestDispatchLabelValuesDedup(t *testing.T) {
// When two state keys map to the same label string, LabelValues
// should deduplicate.
d := Dispatch{
Labels: map[string]string{
"queued": "bot:queued",
"working": "bot:queued", // same as queued
"parked": "bot:queued", // same again
},
}
vals := d.LabelValues()
// Should have "bot:queued" plus fallbacks for waiting, pr, and dead.
if len(vals) != 4 {
t.Errorf("LabelValues: got %d values, want 4: %v", len(vals), vals)
}
}
func TestEcosystemDefaults(t *testing.T) {
// Empty ecosystem → "go" (backward compat).
r := Repo{}
if got := r.effectiveEcosystem(); got != "go" {
t.Errorf("effectiveEcosystem empty: got %q, want go", got)
}
if got := r.ResolvedTestGlob(); got != "*_test.go" {
t.Errorf("ResolvedTestGlob empty: got %q, want *_test.go", got)
}
pre := r.ResolvedPreflight()
if len(pre) != 1 || pre[0][0] != "go" || pre[0][1] != "version" {
t.Errorf("ResolvedPreflight empty: got %v, want [[go version]]", pre)
}
// Explicit ecosystem.
r2 := Repo{Ecosystem: "python"}
if got := r2.ResolvedTestGlob(); got != "test_*.py" {
t.Errorf("ResolvedTestGlob python: got %q, want test_*.py", got)
}
pre2 := r2.ResolvedPreflight()
if len(pre2) != 1 || pre2[0][0] != "python" || pre2[0][1] != "--version" {
t.Errorf("ResolvedPreflight python: got %v, want [[python --version]]", pre2)
}
}
func TestEcosystemOverrides(t *testing.T) {
// Explicit Preflight and TestGlob always win over ecosystem.
r := Repo{
Ecosystem: "go",
Preflight: [][]string{{"make", "check"}},
TestGlob: "spec_*.py",
}
if got := r.ResolvedTestGlob(); got != "spec_*.py" {
t.Errorf("ResolvedTestGlob override: got %q, want spec_*.py", got)
}
pre := r.ResolvedPreflight()
if len(pre) != 1 || pre[0][0] != "make" || pre[0][1] != "check" {
t.Errorf("ResolvedPreflight override: got %v, want [[make check]]", pre)
}
}
func TestEcosystemCustom(t *testing.T) {
// "custom" ecosystem has no defaults -- preflight empty, test glob empty.
r := Repo{Ecosystem: "custom"}
if got := r.ResolvedTestGlob(); got != "" {
t.Errorf("ResolvedTestGlob custom: got %q, want empty", got)
}
if got := r.ResolvedPreflight(); got != nil {
t.Errorf("ResolvedPreflight custom: got %v, want nil", got)
}
}