Reject missing prepared requests
This commit is contained in:
@@ -100,6 +100,9 @@ func runRequestPreparation[I, S any](invocation commandkit.Invocation, suite Sui
|
||||
if err := promptKitContextError(invocation.Context); err != nil {
|
||||
return definitionFailure(fmt.Sprintf("Preparation of the PromptKit request for prompt %q was canceled.", input.promptID), acquired.Explanation, err)
|
||||
}
|
||||
if acquired.Value == nil {
|
||||
return definitionFailure("PromptKit request preparation returned no result.", acquired.Explanation, nil)
|
||||
}
|
||||
_ = invocation.Reporter().Report(commandkit.Report{Value: acquired.Value})
|
||||
return commandkit.Success()
|
||||
}
|
||||
|
||||
@@ -105,3 +105,43 @@ func TestNativePreparationBuilderAndErrorIdentity(t *testing.T) {
|
||||
t.Fatalf("native error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationCommandRejectsMissingResultAndRetainsEvidence(t *testing.T) {
|
||||
cause := errors.New("provider failure")
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
providerErr error
|
||||
cancel bool
|
||||
}{
|
||||
{name: "missing result"}, {name: "provider error", providerErr: cause}, {name: "cancellation", cancel: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
app := newPromptTestApp(t, Command(Suite[struct{}, struct{}]{PrepareRequest: func(context.Context, PreparationRequest, struct{}, struct{}) (commandkit.Acquired[*promptkit.PreparedRun], error) {
|
||||
if tc.cancel {
|
||||
cancel()
|
||||
}
|
||||
acquired := commandkit.Acquired[*promptkit.PreparedRun]{
|
||||
Diagnostics: []commandkit.Diagnostic{{Severity: commandkit.SeverityWarning, Write: func(w io.Writer) error { _, err := io.WriteString(w, "provider diagnostic\n"); return err }}},
|
||||
Explanation: commandkit.Explanation{Message: "provider explanation"},
|
||||
}
|
||||
if tc.providerErr != nil {
|
||||
acquired.Value = &promptkit.PreparedRun{PromptID: "briefing"}
|
||||
}
|
||||
return acquired, tc.providerErr
|
||||
}}))
|
||||
var output, diagnostics bytes.Buffer
|
||||
result := app.Execute(ctx, []string{"promptkit", "show", "prepared", "briefing", "--format=json", "--verbose"}, commandkit.Streams{Out: &output, Err: &diagnostics})
|
||||
if result.Kind != commandkit.OutcomeOperational || output.Len() != 0 || !bytes.Contains(diagnostics.Bytes(), []byte("provider diagnostic")) || !bytes.Contains(diagnostics.Bytes(), []byte("provider explanation")) {
|
||||
t.Fatalf("result=%#v output=%q diagnostics=%q", result, output.String(), diagnostics.String())
|
||||
}
|
||||
if tc.providerErr != nil && !errors.Is(result.Cause, tc.providerErr) {
|
||||
t.Fatalf("provider cause = %v", result.Cause)
|
||||
}
|
||||
if tc.cancel && !errors.Is(result.Cause, context.Canceled) {
|
||||
t.Fatalf("cancellation cause = %v", result.Cause)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user