Files
commandkit/integrations/ckprompt/adapters_test.go

229 lines
8.7 KiB
Go

package ckprompt
import (
"bytes"
"context"
"errors"
"io"
"strings"
"testing"
"testing/fstest"
commandkit "gitea.maximumdirect.net/eric/CommandKit"
"gitea.maximumdirect.net/eric/CommandKit/integrations/ckconfig"
configkit "gitea.maximumdirect.net/eric/ConfigKit"
promptkit "gitea.maximumdirect.net/eric/promptkit"
)
const adapterPrompt = `prompts:
briefing:
"1":
default_profile: default
inputs:
- name: topic
required: true
content_type: text/plain
description: "Topic"
- name: audience
required: false
content_type: text/plain
description: ""
messages:
- role: user
content: "Hello"
output:
format: text
validation_mode: none
empty:
"1":
messages:
- role: user
content: "Empty inputs"
output:
format: text
validation_mode: none
backends:
local:
endpoint: "https://example.invalid/v1"
profiles:
default:
backend: local
model: example
temperature: 0
max_tokens: 0
api_key_env: PROMPTKIT_ADAPTER_TEST_KEY
extra_params:
token: "caller-owned-value"
nested:
visible: true
null_value: null
sequence: [first, null, 3]
requires-key:
backend: local
model: example
api_key_required: true
`
func adapterEngine(t *testing.T) *promptkit.Engine {
t.Helper()
inputs, err := promptkit.FilesystemInputs("adapter fixture", fstest.MapFS{
"config/prompt.yml": &fstest.MapFile{Data: []byte(adapterPrompt)},
}, "config")
if err != nil {
t.Fatal(err)
}
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{
Underlays: []configkit.Underlay{{Inputs: inputs}},
}})
if err != nil {
t.Fatalf("construct PromptKit fixture: %v", err)
}
return engine
}
func TestConfigScopeUsesNativeInspectionFacetsAndInvocationValues(t *testing.T) {
type input struct{ token string }
type state struct{ token string }
type contextKey struct{}
engine := adapterEngine(t)
wantContext := context.WithValue(context.Background(), contextKey{}, "invocation")
wantInput := input{token: "input"}
wantState := state{token: "state"}
loaderCalls := 0
scope := ConfigScope(func(ctx context.Context, selection ckconfig.Selection, gotInput input, gotState state) (*promptkit.Engine, error) {
loaderCalls++
if ctx.Value(contextKey{}) != "invocation" || !selection.Config.Set || selection.Config.Value != "prompt-config.yml" || gotInput != wantInput || gotState != wantState {
t.Errorf("loader args = ctx=%v selection=%#v input=%#v state=%#v", ctx.Value(contextKey{}), selection, gotInput, gotState)
}
return engine, nil
})
if scope.Name != "promptkit" || len(scope.Operations) != 4 || scope.Acquire == nil {
t.Fatalf("configuration scope = %#v", scope)
}
acquired, err := scope.Acquire(wantContext, ckconfig.Request{
Operation: ckconfig.Show,
Selection: ckconfig.Selection{Config: commandkit.Supplied[string]{Value: "prompt-config.yml", Set: true}},
}, wantInput, wantState)
if err != nil || loaderCalls != 1 {
t.Fatalf("scope acquisition = (%#v, %v), calls=%d", acquired, err, loaderCalls)
}
var effective bytes.Buffer
if err := acquired.Value.Effective.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), "briefing:") {
t.Fatalf("native effective report = %q, %v", effective.String(), err)
}
if sources := acquired.Value.Sources.Sources(); len(sources) == 0 || sources[0].ID == "" {
t.Fatalf("native sources = %#v", sources)
}
if explanation, ok := acquired.Value.Explanation.Explain(configkit.Path{}); !ok || !explanation.Effective.Present {
t.Fatalf("native root explanation = %#v, found=%t", explanation, ok)
}
}
func TestConfigScopeRetainsNilLoaderIssueAfterCapabilitiesAreCleared(t *testing.T) {
scope := ConfigScope[int, int](nil)
if scope.Acquire != nil {
t.Fatal("nil loader installed an acquisition callback")
}
scope.Operations = nil
app, err := commandkit.New(commandkit.Identity{Name: "example"})
if err != nil {
t.Fatal(err)
}
declaration := ckconfig.Command(ckconfig.Suite[int, int]{Scopes: []ckconfig.Scope[int, int]{scope}})
if err := app.Register(declaration); err == nil || !strings.Contains(err.Error(), "PromptKit configuration loader is nil") {
t.Fatalf("narrowed nil-loader scope registered: %v", err)
}
}
func TestFromEnginePassesRequestsAndRetainsNilLoaderIssue(t *testing.T) {
type input struct{ value string }
type state struct{ value string }
type contextKey struct{}
engine := adapterEngine(t)
wantContext := context.WithValue(context.Background(), contextKey{}, "prompt invocation")
wantInput := input{value: "typed input"}
wantState := state{value: "shared state"}
loaderCalls := 0
suite := FromEngine(func(ctx context.Context, selection ckconfig.Selection, gotInput input, gotState state) (*promptkit.Engine, error) {
loaderCalls++
if ctx.Value(contextKey{}) != "prompt invocation" || !selection.Overlay.Set || selection.Overlay.Value != "overlay.yml" || gotInput != wantInput || gotState != wantState {
t.Errorf("loader args = ctx=%v selection=%#v input=%#v state=%#v", ctx.Value(contextKey{}), selection, gotInput, gotState)
}
return engine, nil
})
selection := ckconfig.Selection{Overlay: commandkit.Supplied[string]{Value: "overlay.yml", Set: true}}
version := commandkit.Supplied[string]{Value: "1", Set: true}
prompt, err := suite.InspectPrompt(wantContext, PromptRequest{Selection: selection, ID: "briefing", Version: version}, wantInput, wantState)
if err != nil || prompt.Value == nil || prompt.Value.PromptID != "briefing" || prompt.Value.PromptVersion != "1" || loaderCalls != 1 {
t.Fatalf("prompt inspection = (%#v, %v), calls=%d", prompt, err, loaderCalls)
}
defaultPrompt, err := suite.InspectPrompt(wantContext, PromptRequest{Selection: selection, ID: "briefing"}, wantInput, wantState)
if err != nil || defaultPrompt.Value == nil || defaultPrompt.Value.PromptVersion != "1" || loaderCalls != 2 {
t.Fatalf("empty version default = (%#v, %v), calls=%d", defaultPrompt, err, loaderCalls)
}
profile, err := suite.InspectProfile(wantContext, ProfileRequest{Selection: selection, ID: "default"}, wantInput, wantState)
if err != nil || profile.Value == nil || profile.Value.ProfileID != "default" || loaderCalls != 3 {
t.Fatalf("profile inspection = (%#v, %v), calls=%d", profile, err, loaderCalls)
}
if version.Set != true || version.Value != "1" {
t.Fatalf("custom callback request lost version presence: %#v", version)
}
empty := FromEngine[int, int](nil)
empty.InspectPrompt = nil
empty.InspectProfile = nil
if len(empty.issues) != 1 || !strings.Contains(empty.issues[0].Error(), "PromptKit engine loader is nil") {
t.Fatalf("nil-loader issue was lost when callbacks were cleared: %#v", empty.issues)
}
}
func TestAdapterAcquisitionPreservesLoadAndContextErrors(t *testing.T) {
loadErr := errors.New("loader failure")
suite := FromEngine[int, int](func(context.Context, ckconfig.Selection, int, int) (*promptkit.Engine, error) {
return nil, loadErr
})
acquired, err := suite.InspectProfile(context.Background(), ProfileRequest{ID: "default"}, 0, 0)
if err != loadErr || len(acquired.Diagnostics) != 0 {
t.Fatalf("loader failure = (%#v, %v), want original error", acquired, err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
engine := adapterEngine(t)
called := false
suite = FromEngine[int, int](func(context.Context, ckconfig.Selection, int, int) (*promptkit.Engine, error) {
called = true
return engine, nil
})
acquired, err = suite.InspectProfile(ctx, ProfileRequest{ID: "default"}, 0, 0)
if !errors.Is(err, context.Canceled) || acquired.Value != nil || !called {
t.Fatalf("cancelled profile acquisition = (%#v, %v), loader called=%t", acquired, err, called)
}
}
func TestPromptKitDiagnosticsRemainDeferredAndPropagateWriterErrors(t *testing.T) {
writeErr := errors.New("diagnostic writer failed")
diagnostics := promptKitDiagnostics("promptkit", []configkit.SourceInfo{{ID: "source"}}, []configkit.Diagnostic{{
Severity: configkit.SeverityWarning,
Message: "safe warning",
}})
if len(diagnostics) != 1 || diagnostics[0].Severity != commandkit.SeverityWarning {
t.Fatalf("diagnostics = %#v", diagnostics)
}
if err := diagnostics[0].Write(failingDiagnosticWriter{err: writeErr}); !errors.Is(err, writeErr) {
t.Fatalf("diagnostic write error = %v", err)
}
var output bytes.Buffer
if err := diagnostics[0].Write(&output); err != nil || !strings.Contains(output.String(), "safe warning") || !strings.Contains(output.String(), `"source"`) {
t.Fatalf("diagnostic text = %q, %v", output.String(), err)
}
}
type failingDiagnosticWriter struct{ err error }
func (writer failingDiagnosticWriter) Write([]byte) (int, error) { return 0, writer.err }
var _ io.Writer = failingDiagnosticWriter{}