100 lines
4.2 KiB
Go
100 lines
4.2 KiB
Go
package promptkit_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"gitea.maximumdirect.net/eric/ConfigKit"
|
|
"gitea.maximumdirect.net/eric/promptkit"
|
|
)
|
|
|
|
func TestInspectProfileUsesLiteralFileKeyForPreparation(t *testing.T) {
|
|
profiles, err := promptkit.FilesystemInputs("literal profile", fstest.MapFS{
|
|
"config/profile.yaml": &fstest.MapFile{Data: []byte("profiles:\n ' literal ':\n endpoint: https://example.test/v1\n model: model\n")},
|
|
}, "config")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{
|
|
Underlays: []configkit.Underlay{{Inputs: profiles}},
|
|
Main: &configkit.Input{Source: configkit.Mapping("prompt", map[string]any{
|
|
"prompts": map[string]any{"prompt": map[string]any{"1": map[string]any{
|
|
"default_profile": " literal ",
|
|
"messages": []any{map[string]any{"role": "user", "content": "hello"}},
|
|
"output": map[string]any{"format": "text", "validation_mode": "none"},
|
|
}}},
|
|
})},
|
|
}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
inspected, err := engine.InspectProfile(context.Background(), " literal ")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{PromptID: "prompt"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if inspected.ProfileID != " literal " || prepared.SelectedProfileID != inspected.ProfileID || prepared.EffectiveModelParams.Model != inspected.EffectiveModelParams.Model {
|
|
t.Fatalf("inspection=%+v preparation=%+v", inspected, prepared)
|
|
}
|
|
if _, err := engine.InspectProfile(context.Background(), "literal"); !errors.Is(err, promptkit.ErrProfileNotFound) {
|
|
t.Fatalf("trimmed ID resolved unexpectedly: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestInspectProfileIsIndependentAndDoesNotInvokeProvider(t *testing.T) {
|
|
client := &fakeLLMClient{response: &promptkit.GenerateResponse{Content: "unexpected"}}
|
|
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{
|
|
Source: configkit.Mapping("inspection", map[string]any{"profiles": map[string]any{
|
|
"profile": map[string]any{
|
|
"endpoint": "https://example.test/v1", "model": "model", "api_key_required": true,
|
|
"extra_params": map[string]any{"nested": map[string]any{"value": "original"}},
|
|
},
|
|
}}),
|
|
}}}, promptkit.WithLLMClient(client))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
first, err := engine.InspectProfile(context.Background(), "profile")
|
|
if err != nil || first == nil || !first.APIKeyRequired {
|
|
t.Fatalf("first inspection=(%+v, %v)", first, err)
|
|
}
|
|
first.EffectiveModelParams.ExtraParams["nested"].(map[string]any)["value"] = "changed"
|
|
second, err := engine.InspectProfile(context.Background(), "profile")
|
|
if err != nil || second == nil || second.EffectiveModelParams.ExtraParams["nested"].(map[string]any)["value"] != "original" || len(client.requests) != 0 {
|
|
t.Fatalf("inspection changed snapshot or invoked provider: (%+v, %v), calls=%d", second, err, len(client.requests))
|
|
}
|
|
}
|
|
|
|
func TestInspectProfilePublicErrorsAndPartialTarget(t *testing.T) {
|
|
var nilEngine *promptkit.Engine
|
|
if _, err := nilEngine.InspectProfile(context.Background(), "profile"); !errors.Is(err, promptkit.ErrInvalidConfig) {
|
|
t.Fatalf("nil engine: %v", err)
|
|
}
|
|
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{
|
|
Source: configkit.Mapping("partial", map[string]any{"profiles": map[string]any{"profile": map[string]any{"model": "model"}}}),
|
|
}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := engine.InspectProfile(context.Background(), " "); !errors.Is(err, promptkit.ErrInvalidRequest) {
|
|
t.Fatalf("blank selection: %v", err)
|
|
}
|
|
if _, err := engine.InspectProfile(context.Background(), "missing"); !errors.Is(err, promptkit.ErrProfileNotFound) {
|
|
t.Fatalf("missing selection: %v", err)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
if _, err := engine.InspectProfile(ctx, "profile"); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("canceled inspection: %v", err)
|
|
}
|
|
partial, err := engine.InspectProfile(context.Background(), "profile")
|
|
if err != nil || partial == nil || partial.EffectiveModelParams.Endpoint != "" || partial.EffectiveModelParams.Model != "model" {
|
|
t.Fatalf("partial inspection=(%+v, %v)", partial, err)
|
|
}
|
|
}
|