51 lines
1.8 KiB
Go
51 lines
1.8 KiB
Go
|
|
package ckprompt
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
commandkit "gitea.maximumdirect.net/eric/CommandKit"
|
||
|
|
promptkit "gitea.maximumdirect.net/eric/promptkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
type cancelDuringYAML struct{ cancel context.CancelFunc }
|
||
|
|
|
||
|
|
func (value cancelDuringYAML) MarshalYAML() (any, error) {
|
||
|
|
value.cancel()
|
||
|
|
return "prepared", nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestInspectionCancellationDuringPreparationPreventsDelivery(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()
|
||
|
|
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",
|
||
|
|
EffectiveModelParams: promptkit.ExecutionTarget{ExtraParams: map[string]any{
|
||
|
|
"controlled": cancelDuringYAML{cancel},
|
||
|
|
}},
|
||
|
|
}}, 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.OutcomeOperational || got.Status != 1 || !errors.Is(got.Cause, context.Canceled) || 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)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|