Use ConfigKit mapping values directly
This commit is contained in:
@@ -3,6 +3,7 @@ package promptkit_test
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -73,3 +74,51 @@ func TestProgrammaticProfileVariantInherits(t *testing.T) {
|
||||
t.Fatalf("inherited profile = %+v, %v", profile, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProgrammaticSourcesCaptureNestedValuesAndDeferInvalidValues(t *testing.T) {
|
||||
endpoint := "https://example.invalid/v1"
|
||||
type namedString string
|
||||
cyclic := map[string]any{}
|
||||
cyclic["self"] = cyclic
|
||||
for _, kind := range []string{"backend", "profile"} {
|
||||
makeSource := func(params map[string]any) configkit.Source {
|
||||
if kind == "backend" {
|
||||
return promptkit.BackendSource("backend", []promptkit.BackendConfig{{ID: "local", Endpoint: &endpoint, ExtraParams: params}})
|
||||
}
|
||||
return promptkit.ProfileSource("profile", []promptkit.ProfileConfig{{ID: "local", ExtraParams: params}})
|
||||
}
|
||||
load := func(source configkit.Source) (*promptkit.Engine, error) {
|
||||
return promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{Source: source}}}, promptkit.WithBundledOpenRouter(false))
|
||||
}
|
||||
t.Run(kind+"/capture", func(t *testing.T) {
|
||||
values := map[string]any{"nested": map[string]any{"items": []any{"original", int64(42), true, nil}}}
|
||||
source := makeSource(values)
|
||||
values["nested"].(map[string]any)["items"].([]any)[0] = "mutated"
|
||||
engine, err := load(source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
view, err := engine.InspectConfiguration()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
if err := view.WriteEffective(&output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "original") || strings.Contains(output.String(), "mutated") {
|
||||
t.Fatalf("captured configuration = %s", output.String())
|
||||
}
|
||||
})
|
||||
for name, value := range map[string]any{"number": json.Number("1.25"), "typed slice": []string{"value"}, "named scalar": namedString("value"), "pointer": &endpoint, "cycle": cyclic} {
|
||||
t.Run(kind+"/"+name, func(t *testing.T) {
|
||||
source := makeSource(map[string]any{"value": value})
|
||||
_, err := load(source)
|
||||
var loadErr *configkit.LoadError
|
||||
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) == 0 {
|
||||
t.Fatalf("source error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user