package cli import ( "bytes" "context" "errors" "os" "path/filepath" "strings" "testing" "gitea.maximumdirect.net/eric/audita/v4/internal/appconfig" "go.maximumdirect.net/ckconfig" "go.maximumdirect.net/ckprompt" commandkit "go.maximumdirect.net/commandkit" ck "go.maximumdirect.net/configkit" pk "go.maximumdirect.net/promptkit" ) func TestBootstrapConfigurationOutcomes(t *testing.T) { dir := t.TempDir() if err := os.Mkdir(filepath.Join(dir, "secrets"), 0700); err != nil { t.Fatal(err) } t.Setenv("AUDITA_BOOTSTRAP_OUTCOME", "previous") if err := os.WriteFile(filepath.Join(dir, "secrets", "AUDITA_BOOTSTRAP_OUTCOME"), []byte("private-value"), 0600); err != nil { t.Fatal(err) } main := filepath.Join(dir, "config.yml") writeMain := func(directory string) { t.Helper() if err := os.WriteFile(main, []byte("version: 3\nconfig:\n secrets_dir: "+directory+"\n secrets_overwrite: true\n"), 0600); err != nil { t.Fatal(err) } } writeMain("secrets") loads := 0 load := func(ctx context.Context, selection ckconfig.Selection, _, _ struct{}) (*ck.Result[appconfig.Config], error) { loads++ result, report, err := appconfig.Load(ctx, ck.Input{Source: ck.File(selection.Config.Value)}, "") return result, reportBootstrap(ctx, report, err) } scope := withBootstrapContext(ckconfig.FromConfigKit("app", "Application settings", load)) command := ckconfig.Command(ckconfig.Suite[struct{}, struct{}]{Scopes: []ckconfig.Scope[struct{}, struct{}]{scope}}) app, err := commandkit.New(commandkit.Identity{Name: "test"}, commandkit.WithInvocationFeature(command, bootstrapFeature())) if err != nil { t.Fatal(err) } var stdout, stderr bytes.Buffer for _, args := range [][]string{{"--help"}, {"version"}, {"config", "show", "--help"}, {"config", "explain"}} { outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &stdout, Err: &stderr}) if outcome.Status != 0 || loads != 0 || os.Getenv("AUDITA_BOOTSTRAP_OUTCOME") != "previous" { t.Fatalf("help %v: outcome=%+v loads=%d", args, outcome, loads) } stdout.Reset() stderr.Reset() } result := app.Execute(context.Background(), []string{"config", "show", "--scope=app", "--config", main, "--verbose"}, commandkit.Streams{Out: &stdout, Err: &stderr}) if result.Status != 0 || os.Getenv("AUDITA_BOOTSTRAP_OUTCOME") != "private-value" || !strings.Contains(stderr.String(), "AUDITA_BOOTSTRAP_OUTCOME") || strings.Contains(stderr.String(), "private-value") { t.Fatalf("success: status=%d stderr=%q", result.Status, stderr.String()) } stdout.Reset() stderr.Reset() before := loads result = app.Execute(context.Background(), []string{"config", "diff", "--scope=app", "--config", main, "--against-config", main}, commandkit.Streams{Out: &stdout, Err: &stderr}) if result.Status != 0 || loads-before != 2 { t.Fatalf("comparison: status=%d acquisitions=%d stderr=%q", result.Status, loads-before, stderr.String()) } failedWrite := errors.New("diagnostic destination failed") stdout.Reset() result = app.Execute(context.Background(), []string{"config", "show", "--scope=app", "--config", main, "--verbose"}, commandkit.Streams{Out: &stdout, Err: bootstrapFailWriter{err: failedWrite}}) if result.Status != 1 || !errors.Is(result.Presentation.Diagnostics.WriteError, failedWrite) { t.Fatalf("diagnostic write failure: %+v", result) } writeMain("missing") stdout.Reset() stderr.Reset() result = app.Execute(context.Background(), []string{"config", "show", "--scope=app", "--config", main, "--quiet"}, commandkit.Streams{Out: &stdout, Err: &stderr}) var native *ck.SecretsError if result.Status != 1 || !errors.As(result.Cause, &native) || !strings.Contains(stderr.String(), "secrets.") || strings.Contains(stderr.String(), "private-value") { t.Fatalf("failure: status=%d cause=%v stderr=%q", result.Status, result.Cause, stderr.String()) } } func TestBootstrapPreservesJoinedCancellation(t *testing.T) { main := filepath.Join(t.TempDir(), "config.yml") if err := os.WriteFile(main, []byte("version: 3\nconfig:\n secrets_dir: missing\n"), 0600); err != nil { t.Fatal(err) } _, report, err := appconfig.Load(context.Background(), ck.Input{Source: ck.File(main)}, "") joined := errors.Join(err, context.Canceled) wrapped := reportBootstrap(context.Background(), report, joined) var native *ck.SecretsError if !errors.As(wrapped, &native) || !errors.Is(wrapped, context.Canceled) { t.Fatalf("cause = %v", wrapped) } explanation := bootstrapExplanation(commandkit.Explanation{}, wrapped) var detail bytes.Buffer if len(explanation.Details) != 1 { t.Fatalf("details = %d", len(explanation.Details)) } if err := explanation.Details[0](&detail); err != nil || !strings.Contains(detail.String(), "secrets.") { t.Fatalf("detail = %q error = %v", detail.String(), err) } } func TestPromptBootstrapFailureReportsTerminalDetails(t *testing.T) { main := filepath.Join(t.TempDir(), "config.yml") if err := os.WriteFile(main, []byte("version: 3\nconfig:\n secrets_dir: missing\n"), 0600); err != nil { t.Fatal(err) } load := func(ctx context.Context, selection ckconfig.Selection, _, _ struct{}) (*pk.Engine, error) { _, report, err := appconfig.Load(ctx, ck.Input{Source: ck.File(selection.Config.Value)}, "") return nil, reportBootstrap(ctx, report, err) } suite := withPromptBootstrapContext(ckprompt.FromEngine(load)) commands := ckprompt.Declarations(suite) commands = append(commands, commandkit.WithInvocationFeature(commandkit.Group("auxiliary", ""), bootstrapFeature())) app, err := commandkit.New(commandkit.Identity{Name: "test"}, commands...) if err != nil { t.Fatal(err) } var stdout, stderr bytes.Buffer outcome := app.Execute(context.Background(), []string{"promptkit", "list", "prompts", "--config", main, "--quiet"}, commandkit.Streams{Out: &stdout, Err: &stderr}) var native *ck.SecretsError if outcome.Status != 1 || !errors.As(outcome.Cause, &native) || !strings.Contains(stderr.String(), "secrets.") { t.Fatalf("outcome=%+v stderr=%q", outcome, stderr.String()) } } type bootstrapFailWriter struct{ err error } func (w bootstrapFailWriter) Write([]byte) (int, error) { return 0, w.err }