Preserve construction error causes without extra branches

This commit is contained in:
2026-11-16 19:44:09 +00:00
parent fcbff80ab2
commit 4939fdb283
4 changed files with 75 additions and 9 deletions

View File

@@ -215,6 +215,49 @@ func TestPublicConstructionPreservesConfigKitLoadError(t *testing.T) {
}
}
func TestPublicConstructionPreservesMalformedYAMLCause(t *testing.T) {
const sourceLabel = "prompts.yml"
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{
Source: configkit.Bytes(sourceLabel, []byte("profiles: [\n")),
}}})
var loadErr *configkit.LoadError
if engine != nil || !errors.Is(err, promptkit.ErrInvalidConfig) || !errors.As(err, &loadErr) {
t.Fatalf("construction = (%#v, %v), want invalid config preserving LoadError", engine, err)
}
cause := errors.Unwrap(err)
var causeLoadErr *configkit.LoadError
if cause == nil || errors.Is(cause, promptkit.ErrInvalidConfig) || !errors.As(cause, &causeLoadErr) || causeLoadErr != loadErr {
t.Fatalf("construction cause = %v, want the original loading failure without a category branch", cause)
}
for _, diagnostic := range loadErr.Diagnostics() {
if diagnostic.Code != "yaml.invalid" {
continue
}
if diagnostic.Primary == nil {
t.Fatal("YAML diagnostic has no source location")
}
source, ok := loadErr.Source(diagnostic.Primary.SourceID)
if !ok || source.Label != sourceLabel {
t.Fatalf("YAML diagnostic source = %+v, found=%v, want %q", source, ok, sourceLabel)
}
return
}
t.Fatalf("loading diagnostics = %+v, want yaml.invalid", loadErr.Diagnostics())
}
func TestPublicConstructionPreservesOptionFailureCause(t *testing.T) {
path := filepath.Join(t.TempDir(), "missing.json")
engine, err := promptkit.NewEngine(promptkit.Config{}, promptkit.WithSchemaFile(path))
if engine != nil || !errors.Is(err, promptkit.ErrInvalidConfig) || !errors.Is(err, os.ErrNotExist) {
t.Fatalf("construction = (%#v, %v), want invalid config preserving the missing-file cause", engine, err)
}
cause := errors.Unwrap(err)
var pathErr *os.PathError
if cause == nil || errors.Is(cause, promptkit.ErrInvalidConfig) || !errors.As(cause, &pathErr) || pathErr.Path != path {
t.Fatalf("construction cause = %v, want the original path error without a category branch", cause)
}
}
func TestPublicConstructionRejectsSuppliedInvalidMainInput(t *testing.T) {
main := configkit.Input{}
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &main}})