61 lines
2.9 KiB
Go
61 lines
2.9 KiB
Go
|
|
package promptkit_test
|
||
|
|
|
||
|
|
import (
|
||
|
|
"io/fs"
|
||
|
|
"testing"
|
||
|
|
"testing/fstest"
|
||
|
|
|
||
|
|
"go.maximumdirect.net/configkit"
|
||
|
|
"go.maximumdirect.net/promptkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
type mountApplication struct {
|
||
|
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type countingMountFS struct {
|
||
|
|
fs.FS
|
||
|
|
opens int
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *countingMountFS) Open(name string) (fs.File, error) { c.opens++; return c.FS.Open(name) }
|
||
|
|
|
||
|
|
func TestSettingsAndCollectionMounts(t *testing.T) {
|
||
|
|
files := &countingMountFS{FS: fstest.MapFS{
|
||
|
|
"fragments/root.yaml": {Data: []byte("profiles:\n - id: main\n model: model\n")},
|
||
|
|
"fragments/collection.yaml": {Data: []byte("prompts:\n - id: child\n messages: [{role: user, content_file: body.txt}]\n output: {format: text, validation_mode: basic}\n")},
|
||
|
|
"fragments/item.yaml": {Data: []byte("id: item\nmessages: [{role: user, content: hello}]\noutput: {format: text, validation_mode: basic}\n")},
|
||
|
|
"fragments/body.txt": {Data: []byte("message")},
|
||
|
|
}}
|
||
|
|
namespace := configkit.NewFilesystem("mounts", files)
|
||
|
|
root := promptkit.SettingsInput(namespace.File("fragments/root.yaml"))
|
||
|
|
collection, err := promptkit.CollectionInput("docs", namespace.File("fragments/collection.yaml"))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
item := configkit.Input{Source: namespace.File("fragments/item.yaml"), Destination: configkit.Keys("promptkit", "prompts"), PlacementMode: configkit.PlacementModeSequenceItem}
|
||
|
|
for _, input := range []*configkit.Input{&root, &collection, &item} {
|
||
|
|
input.Filesystem = &configkit.FilesystemContext{FS: &namespace, Root: "."}
|
||
|
|
}
|
||
|
|
bundled := promptkit.BundledOpenRouterUnderlay()
|
||
|
|
if files.opens != 0 || root.Imports || !collection.Imports || root.Destination.String() != "/promptkit" || collection.Destination.String() != "/promptkit/collections/docs" {
|
||
|
|
t.Fatalf("mount construction read files or chose wrong defaults: opens=%d", files.opens)
|
||
|
|
}
|
||
|
|
definition := promptkit.Configure(configkit.Definition[mountApplication]{}, func(value *mountApplication) *promptkit.Settings { return &value.PromptKit })
|
||
|
|
result, err := configkit.LoadLayered(configkit.Input{Source: configkit.NoSource()}, configkit.LayeredOptions[mountApplication]{Definition: definition, Underlays: []configkit.Underlay{bundled, {Inputs: []configkit.Input{root, collection, item}}}})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(result.Config.PromptKit.Profiles) < 1 || len(result.Config.PromptKit.Prompts) != 1 || len(result.Config.PromptKit.Collections["docs"].Prompts) != 1 {
|
||
|
|
t.Fatalf("mounted settings = %+v", result.Config.PromptKit)
|
||
|
|
}
|
||
|
|
info, ok := result.Metadata().Lookup(configkit.Keys("promptkit", "collections", "docs", "prompts").Index(0).Key("messages").Index(0).Key("content_file"))
|
||
|
|
if !ok || len(info.Origin.Locations) != 1 {
|
||
|
|
t.Fatalf("origin = %+v", info)
|
||
|
|
}
|
||
|
|
context, ok := result.SourceContext(info.Origin.Locations[0].SourceID)
|
||
|
|
if !ok || context.FS == nil || context.BaseDir != "fragments" {
|
||
|
|
t.Fatalf("context = %+v", context)
|
||
|
|
}
|
||
|
|
}
|