Files
archie/internal/infrastructure/configuration/defaults_test.go
T
sam be03844df8 feat(config): config surface for memory engine selection
Part of archie-core-1786637490043 (config surface for memory engine
selection and settings).

config.MemoryConfig gets an Engine field selecting among the
domain/memory engine family, plus toml tags on every field -- Provider,
ProviderConfig, and SessionTTL had none before, yaml/json only, which
didn't match repo convention.

Engine and Provider are kept as two separate fields rather than one:
they select between two different mechanisms (the new engine family vs.
the legacy external-provider path, archie-core-1786637499161-356-e424e40d.1),
and conflating them would mean a value meant for one silently getting
read by the other once both are wired.

applyMemoryDefaults resolves an absent engine to "builtin" so
unconfigured and explicitly-builtin are the same state everywhere after
defaulting. validateMemory rejects an unrecognised engine name in the
same style as forge.type's existing validation -- named constant list,
"%w: field %q (want %s)" shape, wired into the same validate() chain.
config.example.toml documents the new field.

Not yet wired to anything: nothing reads cfg.Memory.Engine at daemon
boot yet to actually select and start an engine through
domain/memory.Registry. That's bootstrap wiring, tracked as the
remaining half of archie-core-1786637499480 (ship the builtin store as
the default engine).
2026-09-02 02:40:10 +10:00

150 lines
5.2 KiB
Go

package configuration
import (
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
)
func TestApplyNATSDefaultsResolvesMode(t *testing.T) {
tests := []struct {
name string
mode string
url string
want string
}{
{name: "url set implies external", url: "nats://localhost:4222", want: config.NATSModeExternal},
{name: "empty implies embedded", want: config.NATSModeEmbedded},
{name: "explicit embedded is preserved", mode: config.NATSModeEmbedded, want: config.NATSModeEmbedded},
{name: "explicit external is preserved", mode: config.NATSModeExternal, url: "nats://localhost:4222", want: config.NATSModeExternal},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cfg := &config.Config{NATS: config.NATSConfig{Mode: tc.mode, URL: tc.url}}
applyNATSDefaults(cfg)
if cfg.NATS.Mode != tc.want {
t.Errorf("applyNATSDefaults() mode = %q, want %q", cfg.NATS.Mode, tc.want)
}
})
}
}
func TestApplyContainerDefaultsMakesManagedWorkersReadyByDefault(t *testing.T) {
cfg := &config.Config{}
applyContainerDefaults(cfg)
const wantImage = "ghcr.io/samcharles93/archie-agent:latest"
if cfg.Containers.Image != wantImage {
t.Errorf("Containers.Image = %q, want %q", cfg.Containers.Image, wantImage)
}
if cfg.Containers.MaxUptime.Std() != defaultContainerUptime {
t.Errorf("Containers.MaxUptime = %s, want %s", cfg.Containers.MaxUptime.Std(), defaultContainerUptime)
}
if cfg.Containers.VolumeTTL.Std() != defaultVolumeTTL {
t.Errorf("Containers.VolumeTTL = %s, want %s", cfg.Containers.VolumeTTL.Std(), defaultVolumeTTL)
}
if cfg.Containers.PullPolicy != defaultPullPolicy {
t.Errorf("Containers.PullPolicy = %q, want %q", cfg.Containers.PullPolicy, defaultPullPolicy)
}
}
func TestApplyForgeDefaultsIntake(t *testing.T) {
cfg := &config.Config{}
applyForgeDefaults(cfg)
if cfg.Forge.Intake != config.ForgeIntakePoll {
t.Errorf("applyForgeDefaults() intake = %q, want %q", cfg.Forge.Intake, config.ForgeIntakePoll)
}
explicit := &config.Config{Forge: config.Forge{Intake: config.ForgeIntakeWebhook}}
applyForgeDefaults(explicit)
if explicit.Forge.Intake != config.ForgeIntakeWebhook {
t.Errorf("applyForgeDefaults() intake = %q, want unchanged %q", explicit.Forge.Intake, config.ForgeIntakeWebhook)
}
}
func TestApplyIdentityDefaultsDerivesEmailPerIdentity(t *testing.T) {
cfg := &config.Config{Identities: []config.IdentityConfig{
{BotUser: "github-bot", Forge: config.Forge{Type: forgeTypeGitHub}},
{BotUser: "gitea-bot", Forge: config.Forge{Type: forgeTypeGitea}},
{BotUser: "explicit", BotEmail: "bot@example.test", Forge: config.Forge{Type: forgeTypeGitHub}},
}}
applyIdentityDefaults(cfg)
want := []string{"github-bot@users.noreply.github.com", "gitea-bot@gitea.local", "bot@example.test"}
for i := range want {
if cfg.Identities[i].BotEmail != want[i] {
t.Errorf("identity %d BotEmail = %q, want %q", i, cfg.Identities[i].BotEmail, want[i])
}
}
}
func TestApplyCaptureDefaultsFillsZeroValues(t *testing.T) {
cfg := &config.Config{}
applyCaptureDefaults(cfg)
if got, want := cfg.Capture.Retention.Std(), defaultCaptureRetention; got != want {
t.Errorf("Capture.Retention = %v, want %v", got, want)
}
if cfg.Capture.MaxEvents != defaultCaptureMaxEvents {
t.Errorf("Capture.MaxEvents = %d, want %d", cfg.Capture.MaxEvents, defaultCaptureMaxEvents)
}
if cfg.Capture.MaxBodyBytes != defaultCaptureMaxBodyBytes {
t.Errorf("Capture.MaxBodyBytes = %d, want %d", cfg.Capture.MaxBodyBytes, defaultCaptureMaxBodyBytes)
}
if cfg.Capture.RatePerSecond != defaultCaptureRatePerSecond {
t.Errorf("Capture.RatePerSecond = %v, want %v", cfg.Capture.RatePerSecond, defaultCaptureRatePerSecond)
}
if cfg.Capture.RateBurst != defaultCaptureRateBurst {
t.Errorf("Capture.RateBurst = %d, want %d", cfg.Capture.RateBurst, defaultCaptureRateBurst)
}
}
func TestApplyCaptureDefaultsLeavesExplicitValuesAlone(t *testing.T) {
cfg := &config.Config{
Capture: config.CaptureConfig{
Retention: config.Duration(24 * time.Hour),
MaxEvents: 42,
MaxBodyBytes: 1024,
RatePerSecond: 2.5,
RateBurst: 9,
},
}
applyCaptureDefaults(cfg)
if cfg.Capture.Retention.Std() != 24*time.Hour {
t.Errorf("Capture.Retention = %v, want unchanged 24h", cfg.Capture.Retention.Std())
}
if cfg.Capture.MaxEvents != 42 {
t.Errorf("Capture.MaxEvents = %d, want unchanged 42", cfg.Capture.MaxEvents)
}
if cfg.Capture.MaxBodyBytes != 1024 {
t.Errorf("Capture.MaxBodyBytes = %d, want unchanged 1024", cfg.Capture.MaxBodyBytes)
}
if cfg.Capture.RatePerSecond != 2.5 {
t.Errorf("Capture.RatePerSecond = %v, want unchanged 2.5", cfg.Capture.RatePerSecond)
}
if cfg.Capture.RateBurst != 9 {
t.Errorf("Capture.RateBurst = %d, want unchanged 9", cfg.Capture.RateBurst)
}
}
func TestApplyMemoryDefaultsFillsAbsentEngineWithBuiltin(t *testing.T) {
cfg := &config.Config{}
applyMemoryDefaults(cfg)
if cfg.Memory.Engine != memoryEngineBuiltin {
t.Errorf("Memory.Engine = %q, want %q", cfg.Memory.Engine, memoryEngineBuiltin)
}
}
func TestApplyMemoryDefaultsLeavesExplicitEngineAlone(t *testing.T) {
cfg := &config.Config{Memory: config.MemoryConfig{Engine: "honcho"}}
applyMemoryDefaults(cfg)
if cfg.Memory.Engine != "honcho" {
t.Errorf("Memory.Engine = %q, want unchanged %q", cfg.Memory.Engine, "honcho")
}
}