package ckprompt import ( "context" "testing" commandkit "go.maximumdirect.net/commandkit" promptkit "go.maximumdirect.net/promptkit" ) type cancelDuringJSON struct{ cancel context.CancelFunc } func (value cancelDuringJSON) MarshalJSON() ([]byte, error) { value.cancel() return []byte(`"prepared"`), nil } func TestInspectionRetainsAcquiredReportWhenRenderingCancels(t *testing.T) { for _, format := range []string{"text", "yaml"} { for _, mode := range []string{"ordinary", "quiet"} { t.Run(format+"/"+mode, func(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() extra := map[string]any{"controlled": cancelDuringJSON{cancel}} app := newPromptTestApp(t, Command(Suite[struct{}, struct{}]{ InspectProfile: func(context.Context, ProfileRequest, struct{}, struct{}) (commandkit.Acquired[*promptkit.ProfileInspection], error) { return commandkit.Acquired[*promptkit.ProfileInspection]{Value: &promptkit.ProfileInspection{ ProfileID: "demo", ExtraParams: &extra, }}, nil }, })) args := []string{"promptkit", "show", "profile", "demo", "--format", format} if mode == "quiet" { args = append(args, "--quiet") } output := &countingWriter{} got := app.Execute(ctx, args, commandkit.Streams{Out: output}) if got.Kind != commandkit.OutcomeSuccess || got.Status != 0 || ctx.Err() != context.Canceled || (mode == "ordinary" && output.calls == 0) || (mode == "quiet" && output.calls != 0) { t.Fatalf("outcome=%#v writes=%d", got, output.calls) } if got.Presentation.Output.RenderError != nil || got.Presentation.Output.WriteError != nil { t.Fatalf("operation cancellation became a presentation failure: %#v", got.Presentation.Output) } }) } } }