With an existing database_url, setup prefilled the URL, so a blank answer kept it and the bundled socket database was unreachable without typing its URL. The re-run database step now presents a choice menu: keep the existing URL (still the default), switch to the bundled database (socket URL plus a generated PGPASSWORD written to the env file), or enter a different URL. Fresh installs and -database-url are unchanged; a blank answer anywhere still keeps the existing URL. Pinned at the setup API level with fake prompter/sink: TestRunParamsDatabaseChoice.
213 lines
6.1 KiB
Go
213 lines
6.1 KiB
Go
package setup
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite"
|
|
)
|
|
|
|
// fakePrompter answers prompts from maps keyed by the exact prompt text and
|
|
// records what it was asked, so a test can pin both the answers and that the
|
|
// bundled database was actually offered.
|
|
type fakePrompter struct {
|
|
selectAnswers map[string]int
|
|
lineAnswers map[string]string
|
|
confirmAnswer bool
|
|
|
|
selects []recordedSelect
|
|
lines []recordedLine
|
|
}
|
|
|
|
type recordedSelect struct {
|
|
prompt string
|
|
options []string
|
|
}
|
|
|
|
type recordedLine struct {
|
|
prompt string
|
|
defaultValue string
|
|
}
|
|
|
|
func (f *fakePrompter) Select(_ context.Context, prompt string, options []string) (int, error) {
|
|
f.selects = append(f.selects, recordedSelect{prompt: prompt, options: options})
|
|
if ans, ok := f.selectAnswers[prompt]; ok {
|
|
return ans, nil
|
|
}
|
|
return 0, nil
|
|
}
|
|
|
|
func (f *fakePrompter) ReadLine(_ context.Context, prompt, defaultValue string) (string, error) {
|
|
f.lines = append(f.lines, recordedLine{prompt: prompt, defaultValue: defaultValue})
|
|
if ans, ok := f.lineAnswers[prompt]; ok {
|
|
return ans, nil
|
|
}
|
|
return defaultValue, nil
|
|
}
|
|
|
|
func (f *fakePrompter) ReadSecret(context.Context, string) (string, error) { return "", nil }
|
|
|
|
func (f *fakePrompter) Confirm(context.Context, string, bool) (bool, error) {
|
|
return f.confirmAnswer, nil
|
|
}
|
|
|
|
// fakeSink records Put calls by engine/key.
|
|
type fakeSink struct {
|
|
puts map[string]string
|
|
}
|
|
|
|
func (s *fakeSink) Put(engine, key, value string) error {
|
|
if s.puts == nil {
|
|
s.puts = map[string]string{}
|
|
}
|
|
s.puts[engine+"/"+key] = value
|
|
return nil
|
|
}
|
|
|
|
func (s *fakeSink) Commit() error { return nil }
|
|
|
|
// databasePrompt is the re-run database choice; bundledChoice is the index of
|
|
// the bundled database option in that menu.
|
|
const (
|
|
databasePrompt = "Database:"
|
|
bundledChoice = 1
|
|
differentURL = 2
|
|
)
|
|
|
|
func bundledURL() string {
|
|
return "postgres://archie@/archie?host=" + configuration.DefaultPostgresSocketDir()
|
|
}
|
|
|
|
// TestRunParamsDatabaseChoice pins the re-run database step at the setup API
|
|
// level: with an existing database_url the flow must offer the bundled
|
|
// database as an explicit choice, switching to it must generate PGPASSWORD
|
|
// into the sink, and keeping must leave the existing URL untouched.
|
|
func TestRunParamsDatabaseChoice(t *testing.T) {
|
|
const existing = "postgres://operator:secret@db.internal:5432/archie"
|
|
|
|
tests := []struct {
|
|
name string
|
|
// existing is the prefilled database_url; empty means fresh install.
|
|
existing string
|
|
// param is Params.DatabaseURL (the -database-url flag).
|
|
param string
|
|
selectAnswers map[string]int
|
|
lineAnswers map[string]string
|
|
wantURL string
|
|
wantPassword bool
|
|
wantChoice bool // a database Select menu must be presented
|
|
}{
|
|
{
|
|
name: "re-run keep existing is the default choice",
|
|
existing: existing,
|
|
selectAnswers: map[string]int{databasePrompt: 0},
|
|
wantURL: existing,
|
|
wantChoice: true,
|
|
},
|
|
{
|
|
name: "re-run switch to the bundled database",
|
|
existing: existing,
|
|
selectAnswers: map[string]int{databasePrompt: bundledChoice},
|
|
wantURL: bundledURL(),
|
|
wantPassword: true,
|
|
wantChoice: true,
|
|
},
|
|
{
|
|
name: "re-run enter a different URL",
|
|
existing: existing,
|
|
selectAnswers: map[string]int{databasePrompt: differentURL},
|
|
lineAnswers: map[string]string{"PostgreSQL 18 URL: ": "postgres://other.internal/archie"},
|
|
wantURL: "postgres://other.internal/archie",
|
|
wantChoice: true,
|
|
},
|
|
{
|
|
name: "re-run blank answer on the different-URL prompt keeps existing",
|
|
existing: existing,
|
|
selectAnswers: map[string]int{databasePrompt: differentURL},
|
|
lineAnswers: map[string]string{"PostgreSQL 18 URL: ": ""},
|
|
wantURL: existing,
|
|
wantChoice: true,
|
|
},
|
|
{
|
|
name: "fresh install blank answer means the bundled database",
|
|
existing: "",
|
|
lineAnswers: map[string]string{"PostgreSQL 18 URL (blank for the bundled database with a generated password): ": ""},
|
|
wantURL: bundledURL(),
|
|
wantPassword: true,
|
|
wantChoice: false,
|
|
},
|
|
{
|
|
name: "database-url flag wins without prompting",
|
|
existing: existing,
|
|
param: "postgres://flag.internal/archie",
|
|
wantURL: "postgres://flag.internal/archie",
|
|
wantChoice: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
p := &fakePrompter{
|
|
selectAnswers: tt.selectAnswers,
|
|
lineAnswers: tt.lineAnswers,
|
|
confirmAnswer: false,
|
|
}
|
|
sink := &fakeSink{}
|
|
params := Params{
|
|
DatabaseURL: tt.param,
|
|
BotUser: "archie-bot",
|
|
ForgeType: "none",
|
|
Provider: "ollama",
|
|
Model: "llama3",
|
|
}
|
|
edits, err := RunParams(context.Background(), p, nil, sink, ExistingValues{DatabaseURL: tt.existing}, params)
|
|
if err != nil {
|
|
t.Fatalf("RunParams: %v", err)
|
|
}
|
|
|
|
var gotURL string
|
|
for _, e := range edits {
|
|
if e.Table == "" && e.Key == "database_url" {
|
|
gotURL = e.Value
|
|
}
|
|
}
|
|
if gotURL != tomlwrite.String(tt.wantURL) {
|
|
t.Errorf("database_url = %s, want %s", gotURL, tomlwrite.String(tt.wantURL))
|
|
}
|
|
|
|
password, hasPassword := sink.puts["env/PGPASSWORD"]
|
|
if hasPassword != tt.wantPassword {
|
|
t.Fatalf("PGPASSWORD stored = %v, want %v", hasPassword, tt.wantPassword)
|
|
}
|
|
if tt.wantPassword && len(password) != 48 {
|
|
t.Errorf("PGPASSWORD = %q, want a 48-character hex password", password)
|
|
}
|
|
|
|
var offered []string
|
|
for _, sel := range p.selects {
|
|
if sel.prompt == databasePrompt {
|
|
offered = sel.options
|
|
}
|
|
}
|
|
if tt.wantChoice {
|
|
if offered == nil {
|
|
t.Fatalf("no database choice was presented on a re-run; prompts seen: %+v", p.selects)
|
|
}
|
|
bundled := false
|
|
for _, opt := range offered {
|
|
if strings.Contains(strings.ToLower(opt), "bundled") {
|
|
bundled = true
|
|
}
|
|
}
|
|
if !bundled {
|
|
t.Errorf("database choice options %v do not offer the bundled database", offered)
|
|
}
|
|
} else if offered != nil {
|
|
t.Errorf("database choice was presented (%v) where the flow should not ask", offered)
|
|
}
|
|
})
|
|
}
|
|
}
|