Add PromptKit catalog providers

This commit is contained in:
2026-10-12 02:10:45 +00:00
parent f7b1d2c583
commit 330017fd75
3 changed files with 164 additions and 10 deletions

View File

@@ -311,6 +311,8 @@ func TestFromEnginePassesRequestsAndRetainsNilLoaderIssue(t *testing.T) {
}
empty := FromEngine[int, int](nil)
empty.ListPrompts = nil
empty.ListProfiles = nil
empty.InspectPrompt = nil
empty.InspectProfile = nil
if len(empty.issues) != 1 || !strings.Contains(empty.issues[0].Error(), "PromptKit engine loader is nil") {
@@ -318,6 +320,116 @@ func TestFromEnginePassesRequestsAndRetainsNilLoaderIssue(t *testing.T) {
}
}
func TestFromEngineListsNativeCatalogsWithInvocationValues(t *testing.T) {
type input struct{ label string }
type state struct{ label string }
type contextKey struct{}
engine := adapterEngine(t)
wantContext := context.WithValue(context.Background(), contextKey{}, "catalog invocation")
wantSelection := ckconfig.Selection{Config: commandkit.Supplied[string]{Value: "selected.yml", Set: true}}
wantInput := input{label: "typed input"}
wantState := state{label: "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{}) != "catalog invocation" || selection != wantSelection || 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 suite.ListPrompts == nil || suite.ListProfiles == nil {
t.Fatalf("FromEngine did not supply both catalog callbacks: %#v", suite)
}
wantPrompts, err := engine.ListPrompts(wantContext)
if err != nil {
t.Fatal(err)
}
wantProfiles, err := engine.ListProfiles(wantContext)
if err != nil {
t.Fatal(err)
}
prompts, err := suite.ListPrompts(wantContext, wantSelection, wantInput, wantState)
if err != nil || !reflect.DeepEqual(prompts.Value, wantPrompts) || len(prompts.Diagnostics) != 0 || loaderCalls != 1 {
t.Fatalf("prompt catalog = (%#v, %v), calls=%d, want %#v", prompts, err, loaderCalls, wantPrompts)
}
profiles, err := suite.ListProfiles(wantContext, wantSelection, wantInput, wantState)
if err != nil || !reflect.DeepEqual(profiles.Value, wantProfiles) || len(profiles.Diagnostics) != 0 || loaderCalls != 2 {
t.Fatalf("profile catalog = (%#v, %v), calls=%d, want %#v", profiles, err, loaderCalls, wantProfiles)
}
}
func TestFromEngineCatalogsRetainLoadDiagnosticsAndErrorIdentity(t *testing.T) {
_, loadErr := configkit.Load[map[string]any](configkit.Bytes("invalid catalog configuration", []byte("broken: [")))
if loadErr == nil {
t.Fatal("invalid configuration unexpectedly loaded")
}
suite := FromEngine[int, int](func(context.Context, ckconfig.Selection, int, int) (*promptkit.Engine, error) {
return nil, loadErr
})
for _, test := range []struct {
name string
call func() (int, error)
}{
{name: "prompts", call: func() (int, error) {
acquired, err := suite.ListPrompts(context.Background(), ckconfig.Selection{}, 0, 0)
var retained *configkit.LoadError
if !errors.As(err, &retained) || retained != loadErr || len(acquired.Diagnostics) == 0 {
t.Errorf("prompt load failure = (%#v, %v)", acquired, err)
}
return len(acquired.Diagnostics), err
}},
{name: "profiles", call: func() (int, error) {
acquired, err := suite.ListProfiles(context.Background(), ckconfig.Selection{}, 0, 0)
var retained *configkit.LoadError
if !errors.As(err, &retained) || retained != loadErr || len(acquired.Diagnostics) == 0 {
t.Errorf("profile load failure = (%#v, %v)", acquired, err)
}
return len(acquired.Diagnostics), err
}},
} {
t.Run(test.name, func(t *testing.T) {
diagnosticCount, err := test.call()
if !errors.Is(err, loadErr) || diagnosticCount == 0 {
t.Fatalf("catalog failure = (%d diagnostics, %v)", diagnosticCount, err)
}
})
}
}
func TestFromEngineCatalogsHonorCancellationAfterLoad(t *testing.T) {
engine := adapterEngine(t)
loaderCalls := 0
for _, test := range []struct {
name string
call func(Suite[int, int], context.Context) error
}{
{name: "prompts", call: func(suite Suite[int, int], ctx context.Context) error {
_, err := suite.ListPrompts(ctx, ckconfig.Selection{}, 0, 0)
return err
}},
{name: "profiles", call: func(suite Suite[int, int], ctx context.Context) error {
_, err := suite.ListProfiles(ctx, ckconfig.Selection{}, 0, 0)
return err
}},
} {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancelSuite := FromEngine(func(_ context.Context, _ ckconfig.Selection, _, _ int) (*promptkit.Engine, error) {
loaderCalls++
cancel()
return engine, nil
})
if err := test.call(cancelSuite, ctx); !errors.Is(err, context.Canceled) {
t.Fatalf("catalog cancellation = %v", err)
}
})
}
if loaderCalls != 2 {
t.Fatalf("catalog loaders called = %d, want one per provider", loaderCalls)
}
}
func TestAdapterAcquisitionPreservesLoadAndContextErrors(t *testing.T) {
loadErr := errors.New("loader failure")
suite := FromEngine[int, int](func(context.Context, ckconfig.Selection, int, int) (*promptkit.Engine, error) {