Add scoped PromptKit flag aliases

This commit is contained in:
2026-11-07 04:27:28 +00:00
parent 644e6c365b
commit 0183022a28
13 changed files with 237 additions and 38 deletions

View File

@@ -0,0 +1,123 @@
package ckprompt_test
import (
"bytes"
"context"
"io"
"strings"
"testing"
"go.maximumdirect.net/ckprompt"
commandkit "go.maximumdirect.net/commandkit"
promptkit "go.maximumdirect.net/promptkit"
)
func TestDeclarationsInstallOnlySelectedScopedAliases(t *testing.T) {
var got ckprompt.PreparationRequest
calls := 0
suite := ckprompt.WithNamespaces(ckprompt.Suite[struct{}, struct{}]{PrepareRequest: func(_ context.Context, request ckprompt.PreparationRequest, _, _ struct{}) (commandkit.Acquired[*promptkit.PreparedRun], error) {
calls++
got = request
return commandkit.Acquired[*promptkit.PreparedRun]{Value: &promptkit.PreparedRun{}}, nil
}}, promptkit.Namespaces{Prompt: "default"})
declarations := append(ckprompt.Declarations(suite), commandkit.Alias("preview", "promptkit show prepared"), commandkit.Completion("app"))
app, err := commandkit.New(commandkit.Identity{Name: "app"}, declarations...)
if err != nil {
t.Fatal(err)
}
var help bytes.Buffer
for _, args := range [][]string{{"promptkit", "show", "prepared", "--help"}, {"__complete", "promptkit", "show", "prepared", ""}} {
result := app.Execute(context.Background(), args, commandkit.Streams{Out: &help, Err: io.Discard})
if result.Kind != commandkit.OutcomeSuccess || calls != 0 {
t.Fatalf("metadata %v = %#v calls=%d", args, result, calls)
}
}
if !strings.Contains(help.String(), "--llm-input-text") || !strings.Contains(help.String(), "--input") {
t.Fatalf("alias help = %q", help.String())
}
result := app.Execute(context.Background(), []string{"preview", "p", "--profile=", "--prompt-namespace=", "--input-file=x=path", "--llm-input=x=literal", "--input-text=y=first", "--llm-input-file=y=last", "--input=z=", "--model=fixture"}, commandkit.Streams{Out: io.Discard, Err: io.Discard})
if result.Kind != commandkit.OutcomeSuccess || calls != 1 || got.Namespaces == nil || got.Namespaces.Prompt != "" || got.Request.Execution == nil || got.Request.Execution.Model != "fixture" || got.Request.Inputs["x"] != promptkit.Inline("literal") || got.Request.Inputs["y"] != promptkit.File("last") || got.Request.Inputs["z"] != promptkit.Inline("") {
t.Fatalf("result=%#v request=%#v calls=%d", result, got, calls)
}
for _, flag := range []string{"--backend=unavailable", "--unknown=value"} {
result = app.Execute(context.Background(), []string{"preview", "p", flag}, commandkit.Streams{Out: io.Discard, Err: io.Discard})
if result.Kind != commandkit.OutcomeUsage || calls != 1 {
t.Fatalf("inapplicable %s = %#v", flag, result)
}
}
canonical, err := commandkit.New(commandkit.Identity{Name: "app"}, ckprompt.Command(suite))
if err != nil {
t.Fatal(err)
}
help.Reset()
result = canonical.Execute(context.Background(), []string{"promptkit", "show", "prepared", "--help"}, commandkit.Streams{Out: &help, Err: io.Discard})
if result.Kind != commandkit.OutcomeSuccess || strings.Contains(help.String(), "--input ") {
t.Fatalf("canonical help = %q", help.String())
}
result = canonical.Execute(context.Background(), []string{"promptkit", "show", "prepared", "p", "--input=x=text"}, commandkit.Streams{Out: io.Discard, Err: io.Discard})
if result.Kind != commandkit.OutcomeUsage {
t.Fatalf("canonical alias accepted: %#v", result)
}
}
func TestDeclarationsDetectAliasConflicts(t *testing.T) {
type input struct{ profile string }
selector := commandkit.ValueFlag(commandkit.FlagMeta{Name: "profile"}, (*string)(nil), func(v string) (string, error) { return v, nil }, func(i *input, v commandkit.Supplied[string]) { i.profile = v.Value })
suite := ckprompt.Suite[input, struct{}]{Selectors: []commandkit.Flag[input]{selector}, PrepareRequest: func(context.Context, ckprompt.PreparationRequest, input, struct{}) (commandkit.Acquired[*promptkit.PreparedRun], error) {
return commandkit.Acquired[*promptkit.PreparedRun]{Value: &promptkit.PreparedRun{}}, nil
}}
if _, err := commandkit.New(commandkit.Identity{Name: "app"}, ckprompt.Declarations(suite)...); err == nil {
t.Fatal("selector conflicting with scoped alias was accepted")
}
if _, err := commandkit.New(commandkit.Identity{Name: "app"}, ckprompt.Command(suite)); err != nil {
t.Fatalf("canonical selector unexpectedly rejected: %v", err)
}
}
func TestDeclarationsCoverEnabledCommandsAndNamespaces(t *testing.T) {
var requirements ckprompt.RequirementsRequest
var smoke ckprompt.SmokeRequest
suite := ckprompt.WithNamespaces(ckprompt.Suite[struct{}, struct{}]{
InspectPrompt: func(context.Context, ckprompt.PromptRequest, struct{}, struct{}) (commandkit.Acquired[*promptkit.PromptInspection], error) {
return commandkit.Acquired[*promptkit.PromptInspection]{Value: &promptkit.PromptInspection{}}, nil
},
InspectProfile: func(context.Context, ckprompt.ProfileRequest, struct{}, struct{}) (commandkit.Acquired[*promptkit.ProfileInspection], error) {
return commandkit.Acquired[*promptkit.ProfileInspection]{Value: &promptkit.ProfileInspection{}}, nil
},
InspectBackend: func(context.Context, ckprompt.BackendRequest, struct{}, struct{}) (commandkit.Acquired[*promptkit.BackendInspection], error) {
return commandkit.Acquired[*promptkit.BackendInspection]{Value: &promptkit.BackendInspection{}}, nil
},
InspectRequirements: func(_ context.Context, req ckprompt.RequirementsRequest, _, _ struct{}) (commandkit.Acquired[*promptkit.RequirementInspection], error) {
requirements = req
return commandkit.Acquired[*promptkit.RequirementInspection]{Value: &promptkit.RequirementInspection{}}, nil
},
ValidateRequest: func(context.Context, ckprompt.ValidationRequest, struct{}, struct{}) (commandkit.Acquired[struct{}], error) {
return commandkit.Acquired[struct{}]{}, nil
},
PrepareRequest: func(context.Context, ckprompt.PreparationRequest, struct{}, struct{}) (commandkit.Acquired[*promptkit.PreparedRun], error) {
return commandkit.Acquired[*promptkit.PreparedRun]{Value: &promptkit.PreparedRun{}}, nil
},
SmokeTest: func(_ context.Context, req ckprompt.SmokeRequest, _, _ struct{}) (commandkit.Acquired[*promptkit.SmokeTestReport], error) {
smoke = req
return commandkit.Acquired[*promptkit.SmokeTestReport]{Value: &promptkit.SmokeTestReport{}}, nil
},
}, promptkit.Namespaces{Prompt: "original", Profile: "profiles", Backend: "backends"})
app, err := commandkit.New(commandkit.Identity{Name: "app"}, ckprompt.Declarations(suite)...)
if err != nil {
t.Fatal(err)
}
run := func(args ...string) commandkit.Outcome {
return app.Execute(context.Background(), args, commandkit.Streams{Out: io.Discard, Err: io.Discard})
}
if result := run("promptkit", "requirements", "show", "p", "--profile=first", "--llm-profile=last", "--var=x=y", "--profile-namespace=custom"); result.Kind != commandkit.OutcomeSuccess || requirements.Inspection.ProfileID != "last" || requirements.Inspection.Vars["x"] != "y" || requirements.Namespaces == nil || requirements.Namespaces.Profile != "custom" {
t.Fatalf("requirements = %#v %#v", result, requirements)
}
if result := run("promptkit", "test", "authentication", "--backend=local", "--prompt=p", "--temperature=0", "--extra-params={}", "--backend-namespace="); result.Kind != commandkit.OutcomeSuccess || smoke.Request.BackendID != "local" || smoke.Request.PromptID != "p" || smoke.Request.Execution == nil || smoke.Request.Execution.Temperature == nil || *smoke.Request.Execution.Temperature != 0 || smoke.Request.Execution.ExtraParams == nil || smoke.Namespaces == nil || smoke.Namespaces.Backend != "" {
t.Fatalf("smoke = %#v %#v", result, smoke)
}
for _, args := range [][]string{{"promptkit", "show", "prompt", "p", "--prompt-namespace=x"}, {"promptkit", "show", "profile", "p", "--profile-namespace=x"}, {"promptkit", "show", "backend", "p", "--backend-namespace=x"}, {"promptkit", "requirements", "validate", "p", "--input=x=value"}} {
if result := run(args...); result.Kind != commandkit.OutcomeSuccess {
t.Fatalf("%v = %#v", args, result)
}
}
}