Extend composed consumer with request validation
This commit is contained in:
@@ -4,11 +4,14 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
commandkit "gitea.maximumdirect.net/eric/CommandKit"
|
||||
"gitea.maximumdirect.net/eric/CommandKit/integrations/ckconfig"
|
||||
"gitea.maximumdirect.net/eric/CommandKit/integrations/ckprompt"
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
promptkit "gitea.maximumdirect.net/eric/promptkit"
|
||||
"go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
@@ -211,6 +214,106 @@ func TestComposedRequirementsDiscoveryUsesSelectedCampaign(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposedRequestValidationUsesFileBindings(t *testing.T) {
|
||||
app, err := newApp()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
args []string
|
||||
}{
|
||||
{
|
||||
name: "default mode",
|
||||
args: []string{"promptkit", "requirements", "validate", "briefing", "--campaign", "spring", "--input", "topic=fixtures/topic.txt"},
|
||||
},
|
||||
{
|
||||
name: "conditional notes",
|
||||
args: []string{"promptkit", "requirements", "validate", "briefing", "--campaign", "spring", "--input", "topic=fixtures/topic.txt", "--var", "mode=full", "--input", "notes=fixtures/notes.txt", "--format", "yaml"},
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), test.args, commandkit.Streams{Out: &output, Err: io.Discard})
|
||||
if outcome.Kind != commandkit.OutcomeSuccess || outcome.Status != 0 {
|
||||
t.Fatalf("request validation = %#v, report %q", outcome, output.String())
|
||||
}
|
||||
var report map[string]any
|
||||
if err := yaml.Unmarshal(output.Bytes(), &report); err != nil {
|
||||
t.Fatalf("decode validation confirmation %q: %v", output.String(), err)
|
||||
}
|
||||
if len(report) != 2 || report["prompt_id"] != "briefing" || report["valid"] != true {
|
||||
t.Fatalf("validation confirmation = %#v", report)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposedRequestValidationRequiresTopicArtifact(t *testing.T) {
|
||||
app, err := newApp()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"promptkit", "requirements", "validate", "briefing", "--campaign", "spring"}, commandkit.Streams{Out: &output, Err: io.Discard})
|
||||
if outcome.Kind != commandkit.OutcomeOperational || outcome.Status != 1 || outcome.Cause == nil || output.Len() != 0 {
|
||||
t.Fatalf("missing topic artifact outcome = %#v, report %q", outcome, output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildValidationRequestDefaultsOnlyAbsentVariables(t *testing.T) {
|
||||
input := &inspectionInput{Campaign: commandkit.Supplied[string]{Value: "autumn", Set: true}}
|
||||
cache := &runCache{}
|
||||
inputs := map[string]promptkit.ArtifactRef{"topic": promptkit.File("fixtures/topic.txt")}
|
||||
explicit := map[string]string{"topic": "", "mode": "", "audience": ""}
|
||||
seed := ckprompt.ValidationRequest{Request: promptkit.RunRequest{
|
||||
PromptID: "briefing",
|
||||
PromptVersion: "2",
|
||||
ProfileID: "custom-profile",
|
||||
SessionID: "session-9",
|
||||
Vars: explicit,
|
||||
Inputs: inputs,
|
||||
}}
|
||||
|
||||
got, err := buildValidationRequest(context.Background(), seed, input, cache)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.PromptID != seed.Request.PromptID || got.PromptVersion != seed.Request.PromptVersion || got.ProfileID != seed.Request.ProfileID || got.SessionID != seed.Request.SessionID {
|
||||
t.Fatalf("request identity/context changed: %#v", got)
|
||||
}
|
||||
if got.Vars["topic"] != "" || got.Vars["mode"] != "" || got.Vars["audience"] != "" {
|
||||
t.Fatalf("explicit empty values were replaced: %#v", got.Vars)
|
||||
}
|
||||
if !reflect.DeepEqual(got.Inputs, inputs) || !reflect.DeepEqual(seed.Request.Vars, explicit) {
|
||||
t.Fatalf("seed bindings changed: built=%#v seed=%#v", got.Inputs, explicit)
|
||||
}
|
||||
key := applicationCacheKey{selection: seed.Selection, campaign: input.Campaign}
|
||||
cached := cache.applications[key]
|
||||
if cached == nil {
|
||||
t.Fatal("builder did not load application configuration into invocation state")
|
||||
}
|
||||
if _, err := loadPromptEngine(context.Background(), seed.Selection, input, cache); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cache.applications[key] != cached {
|
||||
t.Fatal("engine loader did not reuse the builder's application configuration")
|
||||
}
|
||||
|
||||
missing := ckprompt.ValidationRequest{Request: promptkit.RunRequest{PromptID: "briefing", Vars: map[string]string{"audience": "internal"}}}
|
||||
defaulted, err := buildValidationRequest(context.Background(), missing, input, cache)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if defaulted.Vars["topic"] != "autumn campaign planning" || defaulted.Vars["mode"] != "" || defaulted.Vars["audience"] != "internal" {
|
||||
t.Fatalf("missing variables were not defaulted while retaining supplied values: %#v", defaulted.Vars)
|
||||
}
|
||||
if _, ok := missing.Request.Vars["topic"]; ok {
|
||||
t.Fatalf("builder modified the seed variable map: %#v", missing.Request.Vars)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptKitScopeDiffAndCatalogsUseSelectedCampaign(t *testing.T) {
|
||||
app, err := newApp()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user