67 lines
2.8 KiB
Go
67 lines
2.8 KiB
Go
|
|
package ckprompt
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
"io"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
commandkit "go.maximumdirect.net/commandkit"
|
||
|
|
promptkit "go.maximumdirect.net/promptkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestSmokeAggregateJSONRetainsPartialEvidence(t *testing.T) {
|
||
|
|
ctx, cancel := context.WithCancel(context.Background())
|
||
|
|
defer cancel()
|
||
|
|
cause := errors.New("authentication interrupted")
|
||
|
|
calls := 0
|
||
|
|
app := newPromptTestApp(t, Command(Suite[struct{}, struct{}]{SmokeTest: func(_ context.Context, request SmokeRequest, _, _ struct{}) (commandkit.Acquired[*promptkit.SmokeTestReport], error) {
|
||
|
|
calls++
|
||
|
|
report := &promptkit.SmokeTestReport{Kind: request.Request.Kind, Supported: true, Reason: promptkit.SmokeTestReasonAuthenticationResponseLimitExceeded}
|
||
|
|
if calls == 2 {
|
||
|
|
cancel()
|
||
|
|
return commandkit.Acquired[*promptkit.SmokeTestReport]{Value: report}, cause
|
||
|
|
}
|
||
|
|
return commandkit.Acquired[*promptkit.SmokeTestReport]{Value: report}, nil
|
||
|
|
}}))
|
||
|
|
var output bytes.Buffer
|
||
|
|
result := app.Execute(ctx, []string{"promptkit", "test", "all", "--format=json"}, commandkit.Streams{Out: &output, Err: io.Discard})
|
||
|
|
dec := json.NewDecoder(&output)
|
||
|
|
var report struct {
|
||
|
|
Tests []struct {
|
||
|
|
Kind string `json:"kind"`
|
||
|
|
Error *string `json:"error"`
|
||
|
|
Report struct {
|
||
|
|
Reason string `json:"reason"`
|
||
|
|
Facts struct {
|
||
|
|
CredentialsAccepted *bool `json:"credentials_accepted"`
|
||
|
|
EndpointReachable *bool `json:"endpoint_reachable"`
|
||
|
|
} `json:"facts"`
|
||
|
|
} `json:"report"`
|
||
|
|
} `json:"tests"`
|
||
|
|
}
|
||
|
|
if err := dec.Decode(&report); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := dec.Decode(new(any)); err != io.EOF {
|
||
|
|
t.Fatalf("extra JSON: %v", err)
|
||
|
|
}
|
||
|
|
if calls != 2 || !errors.Is(result.Cause, cause) || !errors.Is(result.Cause, context.Canceled) || len(report.Tests) != 2 || report.Tests[1].Error == nil || report.Tests[1].Report.Reason != string(promptkit.SmokeTestReasonAuthenticationResponseLimitExceeded) || report.Tests[1].Report.Facts.CredentialsAccepted != nil || report.Tests[1].Report.Facts.EndpointReachable != nil {
|
||
|
|
t.Fatalf("result=%#v calls=%d report=%#v", result, calls, report)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSmokePreparationErrorDoesNotInventReport(t *testing.T) {
|
||
|
|
cause := errors.New("request preparation failed")
|
||
|
|
app := newPromptTestApp(t, Command(Suite[struct{}, struct{}]{SmokeTest: func(context.Context, SmokeRequest, struct{}, struct{}) (commandkit.Acquired[*promptkit.SmokeTestReport], error) {
|
||
|
|
return commandkit.Acquired[*promptkit.SmokeTestReport]{}, cause
|
||
|
|
}}))
|
||
|
|
var output bytes.Buffer
|
||
|
|
result := app.Execute(t.Context(), []string{"promptkit", "test", "authentication", "--format=json"}, commandkit.Streams{Out: &output, Err: io.Discard})
|
||
|
|
if !errors.Is(result.Cause, cause) || output.Len() != 0 || result.Presentation.Output.State != commandkit.DeliveryNone {
|
||
|
|
t.Fatalf("result=%#v output=%q", result, output.String())
|
||
|
|
}
|
||
|
|
}
|