diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index bb7f702..63ac0ab 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -73,7 +73,11 @@ in Stage 2. ## Stage 2 — Cover real constructor failures through both integrations -Status — pending. +Status — completed. The focused independent-module test +`GOWORK=off GOTOOLCHAIN=local go test -count=1 -run '^TestPromptConstructorConfigurationOutcomes$' .` +passes with PromptKit v0.23.1. It exercises a real malformed companion through +catalog and configuration commands, preserving constructor and typed error +identity, diagnostic/source evidence, reports, and diff-side context. 1. Read the existing test wiring in ckprompt's `command_test.go`, `configuration_outcomes_test.go`, and diff --git a/integrations/ckprompt/constructor_outcomes_test.go b/integrations/ckprompt/constructor_outcomes_test.go new file mode 100644 index 0000000..9c50c5b --- /dev/null +++ b/integrations/ckprompt/constructor_outcomes_test.go @@ -0,0 +1,75 @@ +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()) + } + }) + } +}