Verify constructor failure precedence

This commit is contained in:
2026-11-17 00:51:51 +00:00
parent 70b3818351
commit 49fee9f8ee
3 changed files with 65 additions and 2 deletions

View File

@@ -73,3 +73,59 @@ func TestPromptConstructorConfigurationOutcomes(t *testing.T) {
})
}
}
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)
}
})
}
}
}