Files
promptkit/context_identity_contract_test.go

98 lines
3.3 KiB
Go
Raw Normal View History

package promptkit_test
import (
"context"
"errors"
"net/http"
"net/url"
"strings"
"testing"
"testing/synctest"
"time"
"go.maximumdirect.net/promptkit"
)
type contextIdentityResponseBody struct {
ctx context.Context
cause error
}
func (b contextIdentityResponseBody) Read([]byte) (int, error) { <-b.ctx.Done(); return 0, b.cause }
func (contextIdentityResponseBody) Close() error { return nil }
func TestExecutionContextAndOriginalCauseIdentities(t *testing.T) {
for _, split := range []bool{false, true} {
for _, kind := range []string{"caller cancel", "caller deadline", "body operation deadline"} {
t.Run(map[bool]string{false: "run/", true: "prepared/"}[split]+kind, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
cause := errors.New("private original cause")
ctx, cancel := context.WithCancelCause(context.Background())
defer cancel(nil)
want := error(context.Canceled)
failure := promptkit.ExecutionFailureCanceled
if kind == "caller deadline" {
var stop context.CancelFunc
ctx, stop = context.WithTimeoutCause(context.Background(), time.Second, cause)
defer stop()
}
if kind != "caller cancel" {
want = context.DeadlineExceeded
failure = promptkit.ExecutionFailureDeadline
}
cfg := simpleConfig("message")
cfg.HTTPClient = &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
if kind == "body operation deadline" {
return &http.Response{StatusCode: http.StatusOK, Body: contextIdentityResponseBody{ctx: r.Context(), cause: cause}}, nil
}
if kind == "caller cancel" {
cancel(cause)
} else {
<-ctx.Done()
}
return nil, context.Cause(ctx)
})}
engine, err := promptkit.NewEngine(cfg)
if err != nil {
t.Fatal(err)
}
var events []promptkit.ExecutionEvent
req := simpleGenerationRequest()
req.OperationTimeout = time.Minute
if kind == "body operation deadline" {
req.OperationTimeout = time.Second
}
req.Observer = func(e promptkit.ExecutionEvent) { events = append(events, e) }
var result *promptkit.RunResult
if split {
handle, prepareErr := engine.PrepareExecution(ctx, req)
if prepareErr != nil {
t.Fatal(prepareErr)
}
result, err = engine.RunPrepared(ctx, handle)
} else {
result, err = engine.Run(ctx, req)
}
for _, identity := range []error{want, cause, promptkit.ErrLLMGenerate, promptkit.ErrTransport} {
if !errors.Is(err, identity) {
t.Errorf("error=%v, want identity %v", err, identity)
}
}
if kind != "body operation deadline" {
var transportErr *url.Error
if !errors.As(err, &transportErr) {
t.Errorf("lost HTTP transport error: %v", err)
}
}
if result != nil || errors.Is(err, promptkit.ErrOperationTimeout) != (kind == "body operation deadline") || strings.Contains(err.Error(), cause.Error()) {
t.Fatalf("incorrect outcome or disclosure: result=%v error=%v", result, err)
}
if len(events) != 3 || events[1].Failure != failure || events[2].Kind != promptkit.ExecutionFinished || events[2].Failure != failure {
t.Fatalf("attempt/terminal classification = %+v", events)
}
})
})
}
}
}