docs/architecture/configuration.md decides that "install.sh must stop generating
config.toml; archied setup owns it", and gives the reason: the installer
hand-rolling TOML already drifted from the schema once, and "the durable fix is
that the code writing the config is the code that reads it". setup.Run,
tomlwrite, configtemplate.Example and terminalprompt were all built for that and
had no production caller between them. This adds the caller.
setup.Run's own doc comment states the obligations, and they are the shape of
installValidatedConfig: render the edits, prove the result loads through a real
configuration.Loader, and only then install the file and commit the secrets. A
validation failure returns before either write, so a bad config leaves neither a
config nor a secret on disk. The rendered bytes are proven from a temp file, so
a load failure cannot have already installed anything. The config is written
0600 via same-directory temp and rename.
Parameterisation is typed, not matched against prompt text. A flag-to-prompt
mapping would have to match strings, which is the brittleness removed in
1f3f54b, so setup.Params is consulted before each question instead and the
prompts are only reached for answers nothing supplied. Run stays a wrapper
passing the zero value, so the package's existing tests keep passing untouched.
No environment variable is consulted anywhere: flags and prompts are the only
two ways an answer arrives.
--defaults is a known-valid unattended baseline, not "each prompt's own default".
That cannot work, and the test proves it: the provider step's first option is
OpenAI, whose model prompt hard-errors on an empty answer. The baseline therefore
covers every required answer and leaves optional ones to answer themselves as
"skip" -- ollama is the only keyless provider, so an unattended install boots
without needing a secret.
Two gaps closed after review rather than left as accepted work:
- A cloud provider was not configurable without a terminal, because the provider
API key had no typed parameter: unattended setup could only ever pick the
keyless self-hosted provider. Params.ProviderAPIKey and -provider-api-key fix
that. The key goes to the env file and TOML carries only the secret reference.
- The write order was unpinned. Inverting installValidatedConfig's two writes --
committing secrets before the config -- left every test green, so nothing held
the invariant the SecretSink contract states. The new
TestInstallValidatedConfig_ConfigWriteFailureLeavesNoSecret fails on that
inversion; the existing validation-failure test does not cover it.
Also fixed three lint failures in the first pass (gocyclo on RunSetup, and two
ineffectual `choice` initialisers): RunSetup's flag surface, the flag-to-Params
mapping, the prompter choice and the existing-config read are now separate
functions, which is the real fix for the complexity rather than a suppression.
The setup flow no longer hand-rolls its own answer format: -telegram-user-ids
reuses setup.ParseTelegramUserIDs, the same parser the prompt path uses, so the
allowlist cannot be parsed two ways.
Found by an adversarial package sweep (temp tracker: config-1,
repo-root-deployment-2, .local/issue-tracker/). Plan of record:
.local/issue-tracker/design-archied-setup.md. install.sh still generates its own
config; that is phase 2 and is not claimed here.
167 lines
5.4 KiB
Go
167 lines
5.4 KiB
Go
package archied
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"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)
|
|
}
|
|
}
|
|
|
|
func TestRunSetup_DefaultsWithForgeTokenWritesEnv(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, "-forge-token", "ghp-secret"}, stdin, &stdout, &stderr)
|
|
if code != 0 {
|
|
t.Fatalf("RunSetup exit = %d, stderr:\n%s", code, stderr.String())
|
|
}
|
|
|
|
doc, err := configuration.New(nil).File(cfgPath)
|
|
if err != nil {
|
|
t.Fatalf("generated config does not load: %v", err)
|
|
}
|
|
if doc.Config.Forge.Token.Key != "ARCHIE_GITHUB_TOKEN" {
|
|
t.Errorf("forge.token.key = %q, want ARCHIE_GITHUB_TOKEN", doc.Config.Forge.Token.Key)
|
|
}
|
|
b, err := os.ReadFile(filepath.Join(dir, "env"))
|
|
if err != nil {
|
|
t.Fatalf("env file not written: %v", err)
|
|
}
|
|
if got, want := string(b), "ARCHIE_GITHUB_TOKEN='ghp-secret'\n"; got != want {
|
|
t.Errorf("env file = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
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")
|
|
err := installValidatedConfig(configuration.New(nil), cfgPath, sink, rendered)
|
|
if err == nil {
|
|
t.Fatal("installValidatedConfig = nil error, want a write failure")
|
|
}
|
|
if strings.Contains(err.Error(), "does not load") {
|
|
t.Fatalf("fixture failed validation rather than the write, so this test never exercised the write path: %v", err)
|
|
}
|
|
if _, statErr := os.Stat(envPath); !os.IsNotExist(statErr) {
|
|
t.Errorf("secret committed even though the config was never written: %v", statErr)
|
|
}
|
|
}
|