Preserve configuration diagnostics from request builders

This commit is contained in:
2026-11-16 19:02:40 +00:00
parent 9b673d0485
commit 21dac4f89e
5 changed files with 115 additions and 25 deletions

View File

@@ -90,21 +90,96 @@ func TestPromptNativeConfigurationFailures(t *testing.T) {
}
})
}
engine := adapterEngine(t)
good := func(context.Context, ckconfig.Selection, struct{}, struct{}) (*promptkit.Engine, error) {
return engine, nil
}
validation := WithRequestValidation(Suite[struct{}, struct{}]{}, good, func(context.Context, ValidationRequest, struct{}, struct{}) (promptkit.RunRequest, error) {
return promptkit.RunRequest{}, loadErr
}
func TestBuilderConfigurationFailureDiagnostics(t *testing.T) {
_, malformed := configkit.Load[map[string]any](configkit.Bytes("builder-config", []byte("broken: [")))
path := configkit.Keys("version")
_, rejected := configkit.Load(configkit.Bytes("builder-config", []byte("version: 1\n")), configkit.Options[map[string]any]{
Definition: configkit.Definition[map[string]any]{Version: &configkit.VersionRule{
Path: path,
Check: func(_ configkit.Version, validation *configkit.Validation) error {
validation.Warning(path, "builder warning")
validation.Error(path, "builder rejection")
return nil
},
}},
})
preparation := WithRequestPreparation(validation, good, func(context.Context, PreparationRequest, struct{}, struct{}) (promptkit.RunRequest, error) {
return promptkit.RunRequest{}, loadErr
})
app = newPromptTestApp(t, Command(preparation))
for _, args := range [][]string{{"promptkit", "requirements", "validate", "example"}, {"promptkit", "show", "prepared", "example"}} {
result := app.Execute(context.Background(), args, commandkit.Streams{Out: io.Discard, Err: io.Discard})
if result.Status != 2 || !errors.Is(result.Cause, loadErr) {
t.Fatalf("builder %v: %#v", args, result)
for _, fixture := range []struct {
name string
cause error
code string
warning string
status int
}{
{"malformed", malformed, "yaml.invalid", "", 2},
{"rejected", rejected, "version.error", "builder warning", 2},
{"application failure", errors.New("builder unavailable"), "", "", 1},
} {
if fixture.cause == nil {
t.Fatalf("%s fixture succeeded", fixture.name)
}
for _, args := range [][]string{
{"promptkit", "requirements", "validate", "example"},
{"promptkit", "show", "prepared", "example"},
{"promptkit", "test", "reachability"},
{"promptkit", "test", "all"},
} {
for _, canceled := range []bool{false, true} {
name := fmt.Sprintf("%s/%s/canceled=%t", fixture.name, strings.Join(args, "_"), canceled)
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
loaderCalls, builderCalls := 0, 0
load := func(context.Context, ckconfig.Selection, struct{}, struct{}) (*promptkit.Engine, error) {
loaderCalls++
return nil, errors.New("unexpected engine load")
}
wrapped := fmt.Errorf("builder load: %w", fixture.cause)
buildError := func() error {
builderCalls++
if canceled {
cancel()
}
return wrapped
}
suite := WithRequestValidation(Suite[struct{}, struct{}]{}, load, func(context.Context, ValidationRequest, struct{}, struct{}) (promptkit.RunRequest, error) {
return promptkit.RunRequest{}, buildError()
})
suite = WithRequestPreparation(suite, load, func(context.Context, PreparationRequest, struct{}, struct{}) (promptkit.RunRequest, error) {
return promptkit.RunRequest{}, buildError()
})
suite = WithSmokeTesting(suite, load, func(context.Context, SmokeRequest, struct{}, struct{}) (promptkit.SmokeTestRequest, error) {
return promptkit.SmokeTestRequest{}, buildError()
})
app := newPromptTestApp(t, Command(suite))
var out, diagnostic bytes.Buffer
result := app.Execute(ctx, append(append([]string{}, args...), "--verbose"), commandkit.Streams{Out: &out, Err: &diagnostic})
wantStatus, wantCalls := fixture.status, 1
if args[len(args)-1] == "all" && !canceled {
wantCalls = 4
}
if canceled {
wantStatus = 1
if !errors.Is(result.Cause, context.Canceled) {
t.Fatalf("lost cancellation: %#v", result)
}
}
if result.Status != wantStatus || !errors.Is(result.Cause, wrapped) || loaderCalls != 0 || builderCalls != wantCalls {
t.Fatalf("result=%#v loader calls=%d builder calls=%d", result, loaderCalls, builderCalls)
}
if fixture.code != "" {
var expected, retained *configkit.LoadError
if !errors.As(fixture.cause, &expected) || !errors.As(result.Cause, &retained) || retained != expected {
t.Fatalf("lost loading error: %#v", result)
}
if !strings.Contains(diagnostic.String(), fixture.code) || !strings.Contains(diagnostic.String(), "builder-config") || !strings.Contains(diagnostic.String(), fixture.warning) {
t.Fatalf("diagnostics: %q", diagnostic.String())
}
}
})
}
}
}
}