119 lines
5.1 KiB
Go
119 lines
5.1 KiB
Go
|
|
package ckprompt
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"go.maximumdirect.net/ckconfig"
|
||
|
|
commandkit "go.maximumdirect.net/commandkit"
|
||
|
|
configkit "go.maximumdirect.net/configkit"
|
||
|
|
promptkit "go.maximumdirect.net/promptkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestConfigScopeUsesNativeSharedLoadPaths(t *testing.T) {
|
||
|
|
type application struct {
|
||
|
|
Name string `yaml:"app_name"`
|
||
|
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||
|
|
}
|
||
|
|
definition := promptkit.Configure(configkit.Definition[application]{Validate: func(_ *application, v *configkit.Validation) {
|
||
|
|
v.Warning(configkit.Keys("app_name"), "application warning")
|
||
|
|
}}, func(a *application) *promptkit.Settings { return &a.PromptKit })
|
||
|
|
source := "app_name: example\npromptkit:\n " + strings.ReplaceAll(strings.TrimSpace(adapterPrompt), "\n", "\n ") + "\n"
|
||
|
|
result, err := configkit.Load(configkit.Bytes("shared application", []byte(source)), configkit.Options[application]{Definition: definition})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
engine, err := promptkit.NewFromConfig(result, promptkit.RuntimeConfig{})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
native, err := engine.InspectConfiguration()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
scope := ConfigScope(func(context.Context, ckconfig.Selection, struct{}, struct{}) (*promptkit.Engine, error) {
|
||
|
|
return engine, nil
|
||
|
|
})
|
||
|
|
app := newPromptTestApp(t, ckconfig.Command(ckconfig.Suite[struct{}, struct{}]{Scopes: []ckconfig.Scope[struct{}, struct{}]{scope}}))
|
||
|
|
run := func(args ...string) ([]string, string, string) {
|
||
|
|
t.Helper()
|
||
|
|
var out, diag bytes.Buffer
|
||
|
|
argv := append([]string{"config", "explain", "--scope=promptkit", "--format=json", "--verbose"}, args...)
|
||
|
|
outcome := app.Execute(context.Background(), argv, commandkit.Streams{Out: &out, Err: &diag})
|
||
|
|
if outcome.Kind != commandkit.OutcomeSuccess {
|
||
|
|
t.Fatalf("%v: %#v %s", args, outcome, diag.String())
|
||
|
|
}
|
||
|
|
var report struct {
|
||
|
|
Paths []struct {
|
||
|
|
Path string `json:"path"`
|
||
|
|
} `json:"paths"`
|
||
|
|
}
|
||
|
|
if args[0] == "--list" {
|
||
|
|
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
var paths []string
|
||
|
|
for _, entry := range report.Paths {
|
||
|
|
paths = append(paths, entry.Path)
|
||
|
|
}
|
||
|
|
return paths, out.String(), diag.String()
|
||
|
|
}
|
||
|
|
metadata := native.Metadata()
|
||
|
|
expected := func(prefix string, explicit *bool) []string {
|
||
|
|
var paths []string
|
||
|
|
for _, path := range native.HistoricalPaths() {
|
||
|
|
info, present := metadata.Lookup(path)
|
||
|
|
if !present || (prefix != "" && !strings.HasPrefix(path.String(), prefix)) || (explicit != nil && info.Explicit != *explicit) {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
paths = append(paths, path.String())
|
||
|
|
}
|
||
|
|
return paths
|
||
|
|
}
|
||
|
|
yes := true
|
||
|
|
for _, tc := range []struct {
|
||
|
|
args []string
|
||
|
|
prefix string
|
||
|
|
explicit *bool
|
||
|
|
}{
|
||
|
|
{[]string{"--list"}, "", nil}, {[]string{"--list", "--historical"}, "", nil}, {[]string{"--list", "--under=/promptkit/prompts"}, "/promptkit/prompts", nil}, {[]string{"--list", "--explicit=true"}, "", &yes},
|
||
|
|
} {
|
||
|
|
paths, _, diag := run(tc.args...)
|
||
|
|
if !reflect.DeepEqual(paths, expected(tc.prefix, tc.explicit)) || !strings.Contains(diag, "application warning") {
|
||
|
|
t.Fatalf("paths=%v want=%v diagnostics=%q", paths, expected(tc.prefix, tc.explicit), diag)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
_, output, diag := run("--path=/app_name")
|
||
|
|
if !strings.Contains(output, "app_name") || !strings.Contains(output, "shared application") || !strings.Contains(diag, "application warning") {
|
||
|
|
t.Fatalf("output=%q diagnostics=%q", output, diag)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRequirementsReportsApplicationInputDefaults(t *testing.T) {
|
||
|
|
type application struct {
|
||
|
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||
|
|
}
|
||
|
|
settings := promptkit.Settings{
|
||
|
|
Profiles: []promptkit.ProfileSettings{{ID: "default", Endpoint: new("https://example.invalid/v1"), Model: new("example")}},
|
||
|
|
Prompts: []promptkit.PromptSettings{{ID: "briefing", DefaultProfile: new("default"), Inputs: []promptkit.PromptInputSettings{{Name: "doc", Required: true, ContentType: new("text/plain"), Description: new("application default")}}, Messages: []promptkit.PromptMessageSettings{{Role: new("user"), Content: new("hello")}}, Output: &promptkit.PromptOutputSettings{Format: new("text"), ValidationMode: new("none")}}},
|
||
|
|
}
|
||
|
|
definition := promptkit.Configure(configkit.Definition[application]{Defaults: func() application { return application{PromptKit: settings} }}, func(value *application) *promptkit.Settings { return &value.PromptKit })
|
||
|
|
result, err := configkit.Load(configkit.NoSource(), configkit.Options[application]{Definition: definition})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
engine, err := promptkit.NewFromConfig(result, promptkit.RuntimeConfig{})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
native, _, report, _ := executeRequirementsAgainstNative(t, engine, promptkit.RequirementInspectionRequest{PromptID: "briefing"}, []string{"promptkit", "requirements", "show", "briefing"})
|
||
|
|
if len(native.Inputs) != 1 || native.Inputs[0].ContentType == nil || *native.Inputs[0].ContentType != "text/plain" || native.Inputs[0].Description == nil || *native.Inputs[0].Description != "application default" || len(report) == 0 {
|
||
|
|
t.Fatalf("native requirements = %#v, report = %#v", native, report)
|
||
|
|
}
|
||
|
|
}
|