108 lines
4.6 KiB
Go
108 lines
4.6 KiB
Go
package capturetest
|
|
|
|
import (
|
|
"context"
|
|
"io/fs"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"go.maximumdirect.net/configkit"
|
|
"go.maximumdirect.net/promptkit"
|
|
"go.maximumdirect.net/promptkit/internal/configuration"
|
|
)
|
|
|
|
type application struct {
|
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
|
Other string `yaml:"other"`
|
|
}
|
|
|
|
func TestCaptureUsesEffectiveSourceContexts(t *testing.T) {
|
|
baseFiles := fstest.MapFS{
|
|
"catalog/base.yaml": {Data: []byte("promptkit:\n prompts:\n - id: base\n messages: [{role: user, content_file: original.txt}]\n output: {format: json, validation_mode: json_schema, schema_file: schema.json}\n")},
|
|
"catalog/original.txt": {Data: []byte("original")},
|
|
"catalog/schema.json": {Data: []byte(`{"type":"object"}`)},
|
|
}
|
|
overrideFiles := fstest.MapFS{
|
|
"operator/main.yaml": {Data: []byte("promptkit:\n prompts:\n - id: child\n extends: {id: base}\n messages: [{role: user, content_file: replacement.txt}]\n")},
|
|
"operator/replacement.txt": {Data: []byte("replacement")},
|
|
}
|
|
base := configkit.NewFilesystem("base", baseFiles)
|
|
override := configkit.NewFilesystem("operator", overrideFiles)
|
|
definition := promptkit.Configure(configkit.Definition[application]{}, func(value *application) *promptkit.Settings { return &value.PromptKit })
|
|
result, err := configkit.LoadLayered(configkit.Input{Source: override.File("operator/main.yaml"), Filesystem: &configkit.FilesystemContext{FS: &override, Root: "."}}, configkit.LayeredOptions[application]{
|
|
Underlays: []configkit.Underlay{{Inputs: []configkit.Input{{Source: base.File("catalog/base.yaml"), Filesystem: &configkit.FilesystemContext{FS: &base, Root: "."}}}}}, Definition: definition,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
view, ok := result.InspectSubtree(configkit.Keys("promptkit"))
|
|
if !ok {
|
|
t.Fatal("inspection unavailable")
|
|
}
|
|
state, err := configuration.Capture(&result.Config.PromptKit, view, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
child, _, err := state.GetPromptSnapshot(context.Background(), "child")
|
|
if err != nil || child.Templates[0].Content != "replacement" || child.SchemaFile == nil || child.SchemaFile.Path != "catalog/schema.json" {
|
|
t.Fatalf("child = %+v, %v", child, err)
|
|
}
|
|
data, err := fs.ReadFile(child.SchemaFile.FS, child.SchemaFile.Path)
|
|
if err != nil || string(data) != `{"type":"object"}` {
|
|
t.Fatalf("schema = %q, %v", data, err)
|
|
}
|
|
}
|
|
|
|
func TestCaptureOwnsEffectiveResources(t *testing.T) {
|
|
definition := promptkit.Configure(configkit.Definition[application]{}, func(value *application) *promptkit.Settings { return &value.PromptKit })
|
|
result, err := configkit.Load(configkit.Bytes("application", []byte(`other: application
|
|
promptkit:
|
|
backends:
|
|
- id: local
|
|
endpoint: https://example.test/v1
|
|
extra_params: {nested: {value: original}}
|
|
profiles:
|
|
- id: profile
|
|
backend: local
|
|
model: original
|
|
prompts:
|
|
- id: prompt
|
|
inputs: [{name: document, required: false, description: text}]
|
|
messages: [{role: user, content: hello}]
|
|
output:
|
|
format: json
|
|
validation_mode: json_schema
|
|
schema: {type: object, properties: {answer: {type: string}}}
|
|
`)), configkit.Options[application]{Definition: definition})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
view, ok := result.InspectSubtree(configkit.Keys("promptkit"))
|
|
if !ok {
|
|
t.Fatal("inspection unavailable")
|
|
}
|
|
state, err := configuration.Capture(&result.Config.PromptKit, view, result.Diagnostics())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
settings := &result.Config.PromptKit
|
|
*settings.Backends[0].Endpoint = "https://changed.test/v1"
|
|
settings.Backends[0].ExtraParams["nested"].(map[string]any)["value"] = "changed"
|
|
*settings.Profiles[0].Model = "changed"
|
|
*settings.Prompts[0].Messages[0].Content = "changed"
|
|
settings.Prompts[0].Inputs[0].Required = true
|
|
settings.Prompts[0].Output.Schema.(map[string]any)["type"] = "changed"
|
|
backend, err := state.InspectBackend(context.Background(), "local")
|
|
if err != nil || backend.Endpoint != "https://example.test/v1" || backend.ExtraParams["nested"].(map[string]any)["value"] != "original" {
|
|
t.Fatalf("backend = %+v, %v", backend, err)
|
|
}
|
|
profile, err := state.GetProfile(context.Background(), "profile")
|
|
if err != nil || profile.ResolvedTarget.Model != "original" {
|
|
t.Fatalf("profile = %+v, %v", profile, err)
|
|
}
|
|
prompt, err := state.InspectPrompt(context.Background(), "prompt")
|
|
if err != nil || prompt.Messages[0].Content != "hello" || prompt.Output.Schema.(map[string]any)["type"] != "object" || prompt.Inputs == nil || (*prompt.Inputs)[0].Required == nil || *(*prompt.Inputs)[0].Required {
|
|
t.Fatalf("prompt = %+v, %v", prompt, err)
|
|
}
|
|
}
|