Select prepared execution observers at run time
This commit is contained in:
@@ -3,6 +3,7 @@ package promptkit_test
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -10,6 +11,50 @@ import (
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestPreparedObservationUsesExecutionContext(t *testing.T) {
|
||||
engine := newPreparedContractEngine(t, &preparedRecordingClient{response: &promptkit.GenerateResponse{Content: "ok"}}, "hello")
|
||||
foreign := newPreparedContractEngine(t, &preparedRecordingClient{response: &promptkit.GenerateResponse{Content: "ok"}}, "hello")
|
||||
var order []string
|
||||
observe := func(name string) promptkit.ExecutionObserver {
|
||||
return func(promptkit.ExecutionEvent) { order = append(order, name) }
|
||||
}
|
||||
prepareCtx := promptkit.WithExecutionObserver(context.Background(), observe("preparation"))
|
||||
executeCtx := promptkit.WithExecutionObserver(context.Background(), observe("execution"))
|
||||
handle, err := engine.PrepareExecution(prepareCtx, promptkit.RunRequest{PromptID: "prepared", Observer: observe("explicit")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(order) != 0 {
|
||||
t.Fatalf("preparation emitted events: %v", order)
|
||||
}
|
||||
if _, err := foreign.RunPrepared(executeCtx, handle); !errors.Is(err, promptkit.ErrInvalidRequest) || len(order) != 0 {
|
||||
t.Fatalf("foreign claim: %v, %v", err, order)
|
||||
}
|
||||
if _, err := engine.RunPrepared(executeCtx, handle); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(order, []string{"explicit", "execution", "explicit", "execution", "explicit", "execution"}) {
|
||||
t.Fatalf("observer order: %v", order)
|
||||
}
|
||||
order = nil
|
||||
if _, err := engine.RunPrepared(executeCtx, handle); !errors.Is(err, promptkit.ErrInvalidRequest) || len(order) != 0 {
|
||||
t.Fatalf("reused claim: %v, %v", err, order)
|
||||
}
|
||||
if _, err := engine.RunPrepared(executeCtx, nil); !errors.Is(err, promptkit.ErrInvalidRequest) || len(order) != 0 {
|
||||
t.Fatalf("nil claim: %v, %v", err, order)
|
||||
}
|
||||
handle, err = engine.PrepareExecution(prepareCtx, promptkit.RunRequest{PromptID: "prepared"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := engine.RunPrepared(executeCtx, handle); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(order, []string{"execution", "execution", "execution"}) {
|
||||
t.Fatalf("context-only order: %v", order)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparedObservationStartsOnlyAfterClaimAndKeepsObserver(t *testing.T) {
|
||||
engine := newPreparedContractEngine(t, &preparedRecordingClient{response: &promptkit.GenerateResponse{Content: "ok"}}, "hello")
|
||||
var events []promptkit.ExecutionEvent
|
||||
|
||||
Reference in New Issue
Block a user