Files
archie/internal/infrastructure/configuration/setup/params_test.go
T
sam 027dc9925b feat(configuration): let setup reference a secret without carrying its value
A parameterised install could not configure a cloud provider without a terminal:
the only way a secret value entered was the prompt, so an unattended run was
limited to the keyless self-hosted provider. The missing piece is the reference
form.

A reference is not a secret. An engine name and a key name are not sensitive --
which is exactly why a reference may be a parameter while a value may not -- so
setup gains -forge-secret-ref, -provider-secret-ref and -telegram-secret-ref,
each taking engine:key, parsed by one new setup.ParseSecretRef. When a reference
is given the step writes it to TOML and asks nothing, and setup neither reads nor
stores the value, because the value is not its to hold. That is the difference
from the prompt path, where the operator supplies a value and setup writes it
into the env engine and references it there.

Engine names are identifiers, not paths: the shipped engines are registered as
age, sops and vault, so a reference like vault/openbao:KEY is refused rather than
written into a config that could never resolve it.

Two refusals guard the silent no-op. A reference naming a key for the keyless
self-hosted provider, or for a forge that is disabled, would sit in the config
looking configured while nothing read it -- the failure shape this flow exists to
remove -- so both are refused rather than written.

telegram keeps writing token_env for the prompt path, because that form names an
env var and the prompt path is the env engine; a supplied reference may name any
engine, which token_env cannot express, so the reference path writes the
preferred token = {engine, key} form. Noted as the one asymmetry.

Also folded stepCloudProvider's key handling into cloudKeyEdit and cloudModel.
That was a lint fix for nested blocks, but the split is the honest shape: the two
branches obtain a key edit by different means and then resolve a model the same
way.

Every new test was mutation-verified by reintroducing the absence: ignoring the
forge reference, making ParseSecretRef validate nothing, and dropping the keyless
-provider guard each fail the corresponding test.
2026-09-15 03:23:08 +10:00

251 lines
9.2 KiB
Go

package setup
import (
"context"
"testing"
"github.com/samcharles93/archie-core/internal/config"
)
// rejectingPrompter fails the test if any question is asked, proving that a
// fully-populated Params never touches the prompt surface.
// scriptedSecretPrompter answers only the secret reads. Any other prompt means
// Params failed to supply a non-secret answer; an exhausted script means a
// secret was read that the test did not expect.
type scriptedSecretPrompter struct {
t *testing.T
secrets []string
next int
}
func (s *scriptedSecretPrompter) take(prompt string) string {
if s.next >= len(s.secrets) {
s.t.Fatalf("unexpected secret read %q: Params supplied every non-secret answer and no more were scripted", prompt)
}
v := s.secrets[s.next]
s.next++
return v
}
func (s *scriptedSecretPrompter) Select(context.Context, string, []string) (int, error) {
s.t.Fatal("unexpected Select: Params should have supplied this answer")
return 0, nil
}
func (s *scriptedSecretPrompter) ReadLine(context.Context, string, string) (string, error) {
s.t.Fatal("unexpected ReadLine: Params should have supplied this answer")
return "", nil
}
func (s *scriptedSecretPrompter) ReadSecret(_ context.Context, prompt string) (string, error) {
return s.take(prompt), nil
}
func (s *scriptedSecretPrompter) Confirm(context.Context, string, bool) (bool, error) {
s.t.Fatal("unexpected Confirm: Params should have supplied this answer")
return false, nil
}
// TestRunParams_SecretsComeOnlyFromThePrompter pins the rule that a secret is
// never a parameter. Params supplies every non-secret answer, so the only
// prompts reached are the secret reads, and the values they return are the only
// values that reach the secret sink. TOML gets a reference, never the value.
func TestRunParams_SecretsComeOnlyFromThePrompter(t *testing.T) {
params := Params{
BotUser: "archie-bot",
Operator: "Ada",
ForgeType: "github",
ForgeHost: "https://github.acme.internal",
Provider: "ollama", // keyless, so no provider secret is read
Model: "llama3",
TelegramUserIDs: []int64{111, 222},
}
p := &scriptedSecretPrompter{t: t, secrets: []string{"ghp-token", "tg-token"}}
secrets := newFakeSecrets()
edits, err := RunParams(context.Background(), p, nil, secrets, ExistingValues{}, params)
if err != nil {
t.Fatalf("RunParams: %v", err)
}
if err := secrets.Commit(); err != nil {
t.Fatalf("Commit: %v", err)
}
if p.next != len(p.secrets) {
t.Errorf("read %d secrets, want %d: every secret must come from a prompt", p.next, len(p.secrets))
}
if got := secrets.committed["env:ARCHIE_GITHUB_TOKEN"]; got != "ghp-token" {
t.Errorf("committed env:ARCHIE_GITHUB_TOKEN = %q, want ghp-token", got)
}
if got := secrets.committed["env:ARCHIE_TELEGRAM_TOKEN"]; got != "tg-token" {
t.Errorf("committed env:ARCHIE_TELEGRAM_TOKEN = %q, want tg-token", got)
}
cfg := generateAndLoad(t, edits)
if cfg.BotUser != "archie-bot" {
t.Errorf("bot_user = %q, want archie-bot", cfg.BotUser)
}
if cfg.Forge.Token.Engine != "env" || cfg.Forge.Token.Key != "ARCHIE_GITHUB_TOKEN" {
t.Errorf("forge.token = %+v, want a reference to env/ARCHIE_GITHUB_TOKEN", cfg.Forge.Token)
}
}
// TestRunParams_NonSecretParamsNeverPrompt is the non-secret half: every
// answer Params can carry is supplied, so nothing is asked for except the
// secrets, which Params deliberately cannot supply.
func TestRunParams_NonSecretParamsNeverPrompt(t *testing.T) {
params := Params{
BotUser: "archie-bot",
Operator: "Ada",
ForgeType: "none",
Provider: "ollama",
Model: "llama3",
TelegramUserIDs: []int64{111, 222},
}
// Ollama is keyless and forge "none" stores no token, so this run asks for
// exactly one secret: the Telegram bot token.
p := &scriptedSecretPrompter{t: t, secrets: []string{"tg-token"}}
secrets := newFakeSecrets()
edits, err := RunParams(context.Background(), p, nil, secrets, ExistingValues{}, params)
if err != nil {
t.Fatalf("RunParams: %v", err)
}
cfg := generateAndLoad(t, edits)
if cfg.BotUser != "archie-bot" {
t.Errorf("bot_user = %q, want archie-bot", cfg.BotUser)
}
if cfg.Forge.Type != "none" {
t.Errorf("forge.type = %q, want none", cfg.Forge.Type)
}
}
func TestRunParams_PartialParamsAskOnlyTheRest(t *testing.T) {
// Bot user, provider and model are supplied; operator, forge and
// telegram are left for the prompter. The provider API key is never in
// Params (there is no typed field for it), so it is always prompted.
p := &fakePrompter{
lines: []string{""}, // operator left blank
selects: []int{2}, // forge: none
secrets: []string{"sk-openai-key"},
confirms: []bool{false},
}
secrets := newFakeSecrets()
edits, err := RunParams(context.Background(), p, nil, secrets, ExistingValues{}, Params{
BotUser: "archie-bot",
Provider: "openai",
Model: "gpt-5.4",
})
if err != nil {
t.Fatalf("RunParams: %v", err)
}
if err := secrets.Commit(); err != nil {
t.Fatalf("Commit: %v", err)
}
cfg := generateAndLoad(t, edits)
if cfg.BotUser != "archie-bot" {
t.Errorf("bot_user = %q, want archie-bot", cfg.BotUser)
}
if cfg.Chat.Operator != "" {
t.Errorf("chat.operator = %q, want empty (prompt left blank)", cfg.Chat.Operator)
}
if cfg.Forge.Type != "none" {
t.Errorf("forge.type = %q, want none", cfg.Forge.Type)
}
if cfg.Providers["openai"].APIKey.Key != "OPENAI_API_KEY" {
t.Errorf("providers.openai.api_key.key = %q, want OPENAI_API_KEY", cfg.Providers["openai"].APIKey.Key)
}
for _, role := range []string{"triage", "planner", "builder"} {
if cfg.Models[role] != "openai/gpt-5.4" {
t.Errorf("models[%s] = %q, want openai/gpt-5.4", role, cfg.Models[role])
}
}
if got := secrets.committed["env:OPENAI_API_KEY"]; got != "sk-openai-key" {
t.Errorf("committed openai key = %q, want sk-openai-key", got)
}
if len(secrets.committed) != 1 {
t.Errorf("committed secrets = %v, want only the openai key", secrets.committed)
}
}
// The --defaults baseline from the plan's Decision 2: bot user archie-bot,
// github, https://github.com, ollama, llama3, operator/telegram skipped. It
// must produce a loadable config with no secrets written, because ollama is
// the one keyless path.
func TestRun_DefaultsBaselineLoads(t *testing.T) {
params := Params{
BotUser: "archie-bot",
ForgeType: "github",
ForgeHost: "https://github.com",
Provider: "ollama",
Model: "llama3",
}
secrets := newFakeSecrets()
edits, err := RunParams(context.Background(), DefaultPrompter{}, nil, secrets, ExistingValues{}, params)
if err != nil {
t.Fatalf("RunParams: %v", err)
}
if err := secrets.Commit(); err != nil {
t.Fatalf("Commit: %v", err)
}
cfg := generateAndLoad(t, edits)
if cfg.BotUser != "archie-bot" {
t.Errorf("bot_user = %q, want archie-bot", cfg.BotUser)
}
if cfg.Forge.Type != "github" || cfg.Forge.Host != "https://github.com" {
t.Errorf("forge = %q/%q, want github/https://github.com", cfg.Forge.Type, cfg.Forge.Host)
}
for _, role := range []string{"triage", "planner", "builder"} {
if cfg.Models[role] != "ollama/llama3" {
t.Errorf("models[%s] = %q, want ollama/llama3", role, cfg.Models[role])
}
}
if cfg.Providers["ollama"].Class != "ollama" {
t.Errorf("providers.ollama.class = %q, want ollama", cfg.Providers["ollama"].Class)
}
if len(secrets.committed) != 0 {
t.Errorf("committed secrets = %v, want none: the defaults baseline is keyless", secrets.committed)
}
}
// TestRunParams_SecretRefsWriteReferencesWithoutStoring pins what a reference
// means: the value already lives in a secret engine, so setup writes the
// reference, reads nothing from the prompt surface, and stores nothing of its
// own. `vault/openbao` is deliberate -- an arbitrary engine name must survive
// to TOML, which the legacy token_env form could not express.
func TestRunParams_SecretRefsWriteReferencesWithoutStoring(t *testing.T) {
params := Params{
BotUser: "archie-bot",
Operator: "Ada",
ForgeType: "github",
ForgeHost: "https://github.com",
ForgeTokenRef: config.SecretRef{Engine: "bws", Key: "ARCHIE_GITHUB_TOKEN"},
Provider: "openai",
Model: "gpt-5.4",
ProviderAPIKeyRef: config.SecretRef{Engine: "vault", Key: "OPENAI_API_KEY"},
TelegramTokenRef: config.SecretRef{Engine: "bws", Key: "TELEGRAM_BOT_TOKEN"},
TelegramUserIDs: []int64{111},
}
// No secrets are scripted: any ReadSecret call fails the test, because a
// reference means nothing may be prompted for.
p := &scriptedSecretPrompter{t: t}
secrets := newFakeSecrets()
edits, err := RunParams(context.Background(), p, nil, secrets, ExistingValues{}, params)
if err != nil {
t.Fatalf("RunParams: %v", err)
}
if len(secrets.pending) != 0 {
t.Errorf("pending secrets = %v, want none: setup does not own a referenced value", secrets.pending)
}
cfg := generateAndLoad(t, edits)
if got := cfg.Forge.Token; got != params.ForgeTokenRef {
t.Errorf("forge.token = %+v, want %+v", got, params.ForgeTokenRef)
}
if got := cfg.Providers["openai"].APIKey; got != params.ProviderAPIKeyRef {
t.Errorf("providers.openai.api_key = %+v, want %+v", got, params.ProviderAPIKeyRef)
}
if got := cfg.Chat.Telegram.Token; got != params.TelegramTokenRef {
t.Errorf("chat.telegram.token = %+v, want %+v", got, params.TelegramTokenRef)
}
}