Files
archie/internal/app/archied/setup_test.go
T
sam 6bdbd5e31e feat(config): key services by name through a registry
Services was a struct with one field per service, so each service's name was
stated three times: the field, the loader's defaulting branch, and every
consumer's field access. Adding one meant editing all three, and nothing
stopped them disagreeing. The drift was already there: stateStoreResolvedToken
and gatewayResolvedToken were identical apart from the env var each hard-coded.

Services becomes map[string]ServiceConnection, and RegisterService declares a
service once with the five fields it has. Everything that differs between
services is now an argument rather than a branch elsewhere: an empty registered
target means the operator must supply one, which is how services.state.target
stays required while the gateway's is defaulted, and the registered token env
var replaces the two copied resolvers with Services.ResolvedToken.

Defaulting iterates the registry, so a new service needs no loader edit.
Consumers read Services.Get(name).

Keeping a typo'd section reported took explicit work. [services.gatway] used to
land in toml.MetaData.Undecoded() because the struct had no such field; a map
decodes any section, so it would have loaded silently and left the daemon
dialing the default. unregisteredServiceKeys asks the registry which names are
real, and the test for it fails with an empty UnknownKeys when that check is
removed.

The TOML shape is unchanged, so no deployment profile, config.example.toml or
install.sh edit is needed.

Refs: archie-core-u23g
2026-09-18 23:32:36 +10:00

285 lines
10 KiB
Go

package archied
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/setup"
)
func TestSetupSubcommandIsRecognised(t *testing.T) {
if !IsSetupArgs([]string{setupCommand, "--defaults"}) {
t.Errorf("IsSetupArgs() did not recognise %q", setupCommand)
}
if IsSetupArgs([]string{"-once"}) {
t.Error("IsSetupArgs() claimed a normal daemon flag")
}
if IsSetupArgs(nil) {
t.Error("IsSetupArgs() claimed an empty argument list")
}
}
func TestRunSetup_DefaultsWritesLoadableConfig(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.toml")
stdin, err := os.Open(os.DevNull)
if err != nil {
t.Fatal(err)
}
defer stdin.Close()
var stdout, stderr strings.Builder
code := RunSetup([]string{"--defaults", "-config", cfgPath}, stdin, &stdout, &stderr)
if code != 0 {
t.Fatalf("RunSetup exit = %d, stderr:\n%s", code, stderr.String())
}
info, err := os.Stat(cfgPath)
if err != nil {
t.Fatalf("config not written: %v", err)
}
if info.Mode().Perm() != 0o600 {
t.Errorf("config mode = %v, want 0600", info.Mode().Perm())
}
doc, err := configuration.New(nil).File(cfgPath)
if err != nil {
t.Fatalf("generated config does not load: %v", err)
}
if doc.Config.BotUser != "archie-bot" {
t.Errorf("bot_user = %q, want archie-bot", doc.Config.BotUser)
}
// The defaults baseline is keyless (ollama), so nothing may be written
// to the env file.
if _, err := os.Stat(filepath.Join(dir, "env")); !os.IsNotExist(err) {
t.Errorf("env file should not exist after a defaults run, got %v", err)
}
}
// TestRunSetupRejectsSecretValueFlags pins that no secret value can be passed
// on the command line. Secrets are set in a secret engine; this flow only ever
// writes a reference to one. A flag that carried a value would be a second,
// silent way to set a secret -- and would put it in shell history and in every
// process listing, which is why install.sh prompts with read_secret instead.
func TestRunSetupRejectsSecretValueFlags(t *testing.T) {
for _, flagName := range []string{"-forge-token", "-provider-api-key", "-telegram-token"} {
t.Run(flagName, func(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.toml")
devNull, err := os.Open(os.DevNull)
if err != nil {
t.Fatal(err)
}
defer func() { _ = devNull.Close() }()
var stdout, stderr strings.Builder
code := RunSetup([]string{"--defaults", "-config", cfgPath, flagName, "leaked-value"}, devNull, &stdout, &stderr)
if code != 2 {
t.Fatalf("RunSetup with %s = exit %d, want 2 (unknown flag); stderr: %s", flagName, code, stderr.String())
}
if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
t.Errorf("config written despite a rejected flag: %v", err)
}
if _, err := os.Stat(filepath.Join(dir, "env")); !os.IsNotExist(err) {
t.Errorf("env file written despite a rejected flag: %v", err)
}
})
}
}
func TestRunSetup_GuidedRequiresTTY(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.toml")
stdin, err := os.Open(os.DevNull)
if err != nil {
t.Fatal(err)
}
defer stdin.Close()
var stdout, stderr strings.Builder
code := RunSetup([]string{"-config", cfgPath}, stdin, &stdout, &stderr)
if code == 0 {
t.Fatalf("RunSetup exit = 0, want non-zero for a guided run without a TTY")
}
if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
t.Errorf("config should not be written when a guided non-TTY run is refused, got %v", err)
}
}
func TestInstallValidatedConfig_ValidationFailureWritesNothing(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.toml")
sink := setup.NewEnvFileSink(filepath.Join(dir, "env"))
if err := sink.Put("env", "ARCHIE_GITHUB_TOKEN", "secret"); err != nil {
t.Fatal(err)
}
err := installValidatedConfig(configuration.New(nil), cfgPath, sink, []byte("this is not toml"))
if err == nil {
t.Fatal("installValidatedConfig = nil error, want a load failure")
}
if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
t.Errorf("config written despite validation failure: %v", err)
}
if _, err := os.Stat(filepath.Join(dir, "env")); !os.IsNotExist(err) {
t.Errorf("secret written despite validation failure: %v", err)
}
}
// TestInstallValidatedConfig_ConfigWriteFailureLeavesNoSecret pins the write
// order the setup package's SecretSink contract depends on: the config is
// installed before any secret is committed, so a secret is never left on disk
// pointing at a config that was never written.
//
// This is not covered by the validation-failure test: inverting the two writes
// leaves every other test in the package green, so this assertion is what
// actually holds the invariant.
func TestInstallValidatedConfig_ConfigWriteFailureLeavesNoSecret(t *testing.T) {
dir := t.TempDir()
// The parent directory does not exist, so validation succeeds and the
// config write fails.
cfgPath := filepath.Join(dir, "missing", "config.toml")
envPath := filepath.Join(dir, "env")
sink := setup.NewEnvFileSink(envPath)
if err := sink.Put("env", "ARCHIE_GITHUB_TOKEN", "secret"); err != nil {
t.Fatal(err)
}
rendered := []byte("bot_user = \"widget\"\nwork_dir = \"/tmp/archie-setup-work\"\n")
// Prove the fixture is loadable BEFORE relying on the write failing.
// Asserted by loading it rather than by matching the error text below: a
// reworded error must not silently disarm this guard and let the test pass
// for the wrong reason.
fixture := filepath.Join(dir, "fixture.toml")
if err := os.WriteFile(fixture, rendered, 0o600); err != nil {
t.Fatal(err)
}
if _, err := configuration.New(nil).File(fixture); err != nil {
t.Fatalf("fixture config does not load, so this test would not exercise the write path: %v", err)
}
if err := installValidatedConfig(configuration.New(nil), cfgPath, sink, rendered); err == nil {
t.Fatal("installValidatedConfig = nil error, want a write failure")
}
if _, statErr := os.Stat(envPath); !os.IsNotExist(statErr) {
t.Errorf("secret committed even though the config was never written: %v", statErr)
}
}
// TestRunSetup_SecretRefFlagsWriteReferencesWithoutEnvFile pins the reference
// form end to end: naming where a secret is stored writes the reference, and
// setup stores nothing, so no env file appears. The engine here (bws) is one
// setup has no writer for -- which is the point, the value is not setup's.
func TestRunSetup_SecretRefFlagsWriteReferencesWithoutEnvFile(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.toml")
devNull, err := os.Open(os.DevNull)
if err != nil {
t.Fatal(err)
}
defer func() { _ = devNull.Close() }()
var stdout, stderr strings.Builder
code := RunSetup([]string{
"--defaults", "-config", cfgPath,
"-provider", "openai", "-model", "gpt-5.4",
"-provider-secret-ref", "bws:OPENAI_API_KEY",
"-forge-secret-ref", "bws:ARCHIE_GITHUB_TOKEN",
}, devNull, &stdout, &stderr)
if code != 0 {
t.Fatalf("RunSetup exit = %d, stderr: %s", code, stderr.String())
}
doc, err := configuration.New(nil).File(cfgPath)
if err != nil {
t.Fatalf("generated config does not load: %v", err)
}
if got, want := doc.Config.Providers["openai"].APIKey, (config.SecretRef{Engine: "bws", Key: "OPENAI_API_KEY"}); got != want {
t.Errorf("providers.openai.api_key = %+v, want %+v", got, want)
}
if got, want := doc.Config.Forge.Token, (config.SecretRef{Engine: "bws", Key: "ARCHIE_GITHUB_TOKEN"}); got != want {
t.Errorf("forge.token = %+v, want %+v", got, want)
}
if _, err := os.Stat(filepath.Join(dir, "env")); !os.IsNotExist(err) {
t.Errorf("env file written: a reference means setup stores no value (stat err: %v)", err)
}
}
// TestRunSetup_RejectsReferencesNothingWouldUse pins the no-silent-no-op rule.
// A reference for a keyless provider, or for a disabled forge, would sit in the
// config looking configured while nothing read it -- the failure shape this
// whole flow exists to remove -- so it is refused instead.
func TestRunSetup_RejectsReferencesNothingWouldUse(t *testing.T) {
tests := []struct {
name string
args []string
}{
{name: "provider ref with the keyless self-hosted provider", args: []string{"-provider-secret-ref", "bws:OPENAI_API_KEY"}},
{name: "forge ref with the forge disabled", args: []string{"-forge-type", "none", "-forge-secret-ref", "bws:ARCHIE_GITHUB_TOKEN"}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.toml")
devNull, err := os.Open(os.DevNull)
if err != nil {
t.Fatal(err)
}
defer func() { _ = devNull.Close() }()
args := append([]string{"--defaults", "-config", cfgPath}, tc.args...)
var stdout, stderr strings.Builder
if code := RunSetup(args, devNull, &stdout, &stderr); code == 0 {
t.Fatalf("RunSetup = exit 0, want a refusal; stderr: %s", stderr.String())
}
if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
t.Errorf("config written despite the refusal: %v", err)
}
})
}
}
// TestRunSetup_GeneratedConfigSatisfiesDaemonRequirements pins the properties
// archied needs of whatever setup emits, asserted on the GENERATED config.
//
// It replaces a test that read config.example.toml and reconstructed install.sh's
// shell heredoc. Testing hand-written files is testing an artifact a human or an
// agent may legitimately rewrite at any time -- and that test in fact broke the
// moment install.sh stopped generating config, because the heredoc it parsed no
// longer existed. The generated output is the contract; the template is an input.
func TestRunSetup_GeneratedConfigSatisfiesDaemonRequirements(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.toml")
devNull, err := os.Open(os.DevNull)
if err != nil {
t.Fatal(err)
}
defer func() { _ = devNull.Close() }()
var stdout, stderr strings.Builder
if code := RunSetup([]string{"--defaults", "-config", cfgPath}, devNull, &stdout, &stderr); code != 0 {
t.Fatalf("RunSetup exit = %d, stderr: %s", code, stderr.String())
}
doc, err := configuration.New(nil).File(cfgPath)
if err != nil {
t.Fatalf("generated config does not load: %v", err)
}
// openStateStoreAdapter refuses an empty target, so this is the difference
// between a config that boots and one that does not.
if doc.Config.Services.Get(config.ServiceNameState).Target == "" {
t.Error("generated config leaves services.state.target empty; the daemon would not boot")
}
if doc.Config.BotUser == "" {
t.Error("generated config leaves bot_user empty")
}
}