package ckprompt import ( "bytes" "context" "errors" "os" "path/filepath" "strings" "testing" "go.maximumdirect.net/ckconfig" "go.maximumdirect.net/commandkit" "go.maximumdirect.net/configkit" "go.maximumdirect.net/promptkit" ) func TestPromptConstructorConfigurationOutcomes(t *testing.T) { dir := t.TempDir() mainPath := filepath.Join(dir, "config.yml") companionPath := filepath.Join(dir, "prompts.yml") for path, data := range map[string]string{mainPath: "companion: prompts.yml\n", companionPath: "profiles: [\n"} { if err := os.WriteFile(path, []byte(data), 0600); err != nil { t.Fatal(err) } } good := adapterEngine(t) var constructorErr error load := func(_ context.Context, selection ckconfig.Selection, _, _ struct{}) (*promptkit.Engine, error) { if selection.Config.Value == "good" { return good, nil } appConfig, err := configkit.Load[struct { Companion string `yaml:"companion"` }](configkit.File(selection.Config.Value)) if err != nil { return nil, err } companion := filepath.Join(filepath.Dir(selection.Config.Value), appConfig.Config.Companion) engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{Source: configkit.File(companion)}}}, promptkit.WithBundledOpenRouter(false)) constructorErr = err return engine, err } app := newPromptTestApp(t, Command(FromEngine[struct{}, struct{}](load)), ckconfig.Command(ckconfig.Suite[struct{}, struct{}]{Scopes: []ckconfig.Scope[struct{}, struct{}]{ConfigScope(load)}})) cases := []struct { name string args []string report, side string }{ {"catalog", []string{"promptkit", "list", "prompts", "--config", mainPath}, "", ""}, {"show", []string{"config", "show", "--scope=promptkit", "--config", mainPath}, "", ""}, {"sources", []string{"config", "sources", "--format=json", "--scope=promptkit", "--config", mainPath}, `"sources"`, ""}, {"validate", []string{"config", "validate", "--details", "--format=json", "--scope=promptkit", "--config", mainPath}, `"code": "yaml.invalid"`, ""}, {"explain", []string{"config", "explain", "--list", "--scope=promptkit", "--config", mainPath}, "", ""}, {"diff left", []string{"config", "diff", "--scope=promptkit", "--config", mainPath, "--against-config=good"}, "", "left"}, {"diff right", []string{"config", "diff", "--scope=promptkit", "--config=good", "--against-config", mainPath}, "", "right"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { constructorErr = nil var out, diag bytes.Buffer result := app.Execute(context.Background(), tc.args, commandkit.Streams{Out: &out, Err: &diag}) var actual, expected *configkit.LoadError if constructorErr == nil || !errors.As(constructorErr, &expected) || !errors.As(result.Cause, &actual) || actual != expected || !errors.Is(result.Cause, constructorErr) || !errors.Is(result.Cause, promptkit.ErrInvalidConfig) || result.Kind != commandkit.OutcomeUsage || result.Status != 2 { t.Fatalf("outcome=%#v constructor=%v diagnostic=%q", result, constructorErr, diag.String()) } if !strings.Contains(diag.String(), "yaml.invalid") || !strings.Contains(diag.String(), "prompts.yml") || (tc.report != "" && !strings.Contains(out.String(), tc.report)) || (tc.side != "" && !strings.Contains(diag.String(), "("+tc.side+")")) { t.Fatalf("output=%q diagnostics=%q", out.String(), diag.String()) } if tc.name == "sources" && !strings.Contains(out.String(), "prompts.yml") { t.Fatalf("source evidence missing companion: %q", out.String()) } }) } }