package ckprompt import ( "context" "errors" "strings" "testing" commandkit "gitea.maximumdirect.net/eric/CommandKit" "gitea.maximumdirect.net/eric/CommandKit/integrations/ckconfig" promptkit "gitea.maximumdirect.net/eric/promptkit" ) func TestWithRequestValidationPreservesSuiteAndValidatesSeedWithoutBuilder(t *testing.T) { type contextKey struct{} type input struct{ value string } type state struct{ value string } ctx := context.WithValue(context.Background(), contextKey{}, "invocation") engine := adapterEngine(t) selection := ckconfig.Selection{Config: commandkit.Supplied[string]{Value: "config.yml", Set: true}} inspect := func(context.Context, RequirementsRequest, input, state) (commandkit.Acquired[*promptkit.RequirementInspection], error) { return commandkit.Acquired[*promptkit.RequirementInspection]{}, nil } newInput := func() input { return input{value: "factory"} } suite := Suite[input, state]{InspectRequirements: inspect, NewInput: newInput} loaderCalls := 0 suite = WithRequestValidation(suite, func(gotContext context.Context, gotSelection ckconfig.Selection, gotInput input, gotState state) (*promptkit.Engine, error) { loaderCalls++ if gotContext != ctx || gotContext.Value(contextKey{}) != "invocation" || gotSelection != selection || gotInput.value != "input" || gotState.value != "state" { t.Errorf("loader args = (%v, %#v, %#v, %#v)", gotContext, gotSelection, gotInput, gotState) } return engine, nil }, nil) if suite.InspectRequirements == nil || suite.NewInput == nil || suite.ValidateRequest == nil { t.Fatalf("composed suite lost a member or omitted validation: %#v", suite) } acquired, err := suite.ValidateRequest(ctx, ValidationRequest{ Selection: selection, Request: promptkit.RunRequest{PromptID: "empty", ProfileID: "default"}, }, input{value: "input"}, state{value: "state"}) if err != nil { t.Fatalf("validate seed request: %v", err) } if acquired.Value != (struct{}{}) || len(acquired.Diagnostics) != 0 || loaderCalls != 1 { t.Fatalf("acquisition = %#v, loader calls = %d", acquired, loaderCalls) } } func TestWithRequestValidationBuilderFailurePrecedesLoading(t *testing.T) { type input struct{ value string } type state struct{ value string } buildErr := errors.New("cannot build request") loadCalls, buildCalls := 0, 0 suite := WithRequestValidation( Suite[input, state]{}, func(context.Context, ckconfig.Selection, input, state) (*promptkit.Engine, error) { loadCalls++ return nil, errors.New("loader should not run") }, func(ctx context.Context, request ValidationRequest, gotInput input, gotState state) (promptkit.RunRequest, error) { buildCalls++ if ctx == nil || request.Request.PromptID != "seed" || gotInput.value != "input" || gotState.value != "state" { t.Errorf("builder args = (%v, %#v, %#v, %#v)", ctx, request, gotInput, gotState) } return promptkit.RunRequest{}, buildErr }, ) _, err := suite.ValidateRequest(context.Background(), ValidationRequest{Request: promptkit.RunRequest{PromptID: "seed"}}, input{value: "input"}, state{value: "state"}) if !errors.Is(err, buildErr) || buildCalls != 1 || loadCalls != 0 { t.Fatalf("validation = (%v), builder calls = %d, loader calls = %d", err, buildCalls, loadCalls) } } func TestWithRequestValidationRetainsNilLoaderIssueIndependently(t *testing.T) { firstIssue := errors.New("existing adapter issue") backing := make([]error, 1, 3) backing[0] = firstIssue existingCallback := func(context.Context, ValidationRequest, struct{}, struct{}) (commandkit.Acquired[struct{}], error) { return commandkit.Acquired[struct{}]{}, nil } suite := Suite[struct{}, struct{}]{ValidateRequest: existingCallback, issues: backing[:1]} composed := WithRequestValidation(suite, nil, nil) if composed.ValidateRequest == nil || len(composed.issues) != 2 { t.Fatalf("nil loader composition = %#v", composed) } if _, err := composed.ValidateRequest(context.Background(), ValidationRequest{}, struct{}{}, struct{}{}); err != nil { t.Fatalf("nil loader replaced the existing validation callback: %v", err) } if !errors.Is(composed.issues[0], firstIssue) || composed.issues[1] == nil || backing[:cap(backing)][1] != nil { t.Fatalf("issues = %#v, original backing = %#v", composed.issues, backing[:cap(backing)]) } composed.ValidateRequest = nil if err := newPromptTestApp(t).Register(Command(composed)); err == nil || !strings.Contains(err.Error(), "PromptKit engine loader is nil") { t.Fatalf("installation did not report retained issue after capabilities were cleared: %v", err) } }