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()) } }) } } func TestPromptConstructorFailurePrecedence(t *testing.T) { dir := t.TempDir() malformed := filepath.Join(dir, "prompts.yml") if err := os.WriteFile(malformed, []byte("profiles: [\n"), 0600); err != nil { t.Fatal(err) } _, invalid := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{Source: configkit.File(malformed)}}}, promptkit.WithBundledOpenRouter(false)) _, missing := promptkit.NewEngine(promptkit.Config{}, promptkit.WithBundledOpenRouter(false), promptkit.WithSchemaFile(filepath.Join(dir, "missing.json"))) if invalid == nil || missing == nil || !errors.Is(invalid, promptkit.ErrInvalidConfig) || !errors.Is(missing, promptkit.ErrInvalidConfig) || !errors.Is(missing, os.ErrNotExist) { t.Fatalf("constructor fixtures invalid=%v missing=%v", invalid, missing) } operational := errors.New("independent failure") for _, tc := range []struct { name string cause error extra error canceled bool }{ {"missing schema", missing, os.ErrNotExist, false}, {"joined operational", errors.Join(invalid, operational), operational, false}, {"joined cancellation", errors.Join(invalid, context.Canceled), context.Canceled, false}, {"already canceled", nil, context.Canceled, true}, } { for _, args := range [][]string{{"promptkit", "list", "prompts"}, {"config", "show", "--scope=promptkit"}} { t.Run(tc.name+"/"+strings.Join(args, "_"), func(t *testing.T) { calls := 0 load := func(context.Context, ckconfig.Selection, struct{}, struct{}) (*promptkit.Engine, error) { calls++ if tc.cause != nil { return nil, tc.cause } return adapterEngine(t), nil } app := newPromptTestApp(t, Command(FromEngine[struct{}, struct{}](load)), ckconfig.Command(ckconfig.Suite[struct{}, struct{}]{Scopes: []ckconfig.Scope[struct{}, struct{}]{ConfigScope(load)}})) ctx, cancel := context.WithCancel(context.Background()) defer cancel() if tc.canceled { cancel() } var out bytes.Buffer result := app.Execute(ctx, args, commandkit.Streams{Out: &out}) if result.Kind != commandkit.OutcomeOperational || result.Status != 1 || !errors.Is(result.Cause, tc.extra) { t.Fatalf("outcome=%#v output=%q", result, out.String()) } if tc.canceled { if calls != 0 || out.Len() != 0 { t.Fatalf("calls=%d output=%q", calls, out.String()) } } else if calls != 1 || !errors.Is(result.Cause, tc.cause) || !errors.Is(result.Cause, promptkit.ErrInvalidConfig) { t.Fatalf("outcome=%#v calls=%d", result, calls) } }) } } }