106 lines
4.2 KiB
Go
106 lines
4.2 KiB
Go
package engine
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
|
|
)
|
|
|
|
func TestEngineSmokeTestUsesInjectedGenerationWithoutHTTPSupport(t *testing.T) {
|
|
calls := 0
|
|
options := Options{}
|
|
options.SetLLMClient(engineSmokeClientFunc(func(_ context.Context, request domain.GenerateRequest) (*domain.GenerateResponse, error) {
|
|
calls++
|
|
if len(request.Prompt.Messages) != 1 || request.Prompt.Messages[0].Role != "user" {
|
|
t.Fatalf("injected generation request = %+v", request)
|
|
}
|
|
return &domain.GenerateResponse{Content: "engine response"}, nil
|
|
}))
|
|
assembled, err := New(Config{}, options)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
report, err := assembled.SmokeTest(context.Background(), domain.SmokeTestRequest{
|
|
Kind: domain.SmokeTestTextInference, BackendID: "rakestrawhome",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if calls != 1 || !report.Completed || !report.Supported || report.Applicable == nil || !*report.Applicable ||
|
|
report.Facts.GenerationCompleted == nil || !*report.Facts.GenerationCompleted ||
|
|
report.Facts.TextReturned == nil || !*report.Facts.TextReturned ||
|
|
report.Facts.ExpectedTextMatched == nil || *report.Facts.ExpectedTextMatched ||
|
|
report.Facts.EndpointReachable != nil || report.Target.BackendID != "rakestrawhome" {
|
|
t.Fatalf("injected smoke report calls=%d: %+v", calls, report)
|
|
}
|
|
if report.Target.Endpoint != "" || report.Target.EffectiveSettings.Endpoint == "" || len(report.Attempts) != 1 ||
|
|
report.Attempts[0].InitialEndpoint != "" || report.Attempts[0].Endpoint != "" {
|
|
t.Fatalf("injected report claimed HTTP destinations: %+v", report)
|
|
}
|
|
|
|
authentication, err := assembled.SmokeTest(context.Background(), domain.SmokeTestRequest{
|
|
Kind: domain.SmokeTestAuthentication, BackendID: "rakestrawhome",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !authentication.Supported || authentication.Applicable == nil || *authentication.Applicable ||
|
|
authentication.Reason != domain.SmokeTestReasonAuthenticationNotApplicable || len(authentication.Attempts) != 0 {
|
|
t.Fatalf("injected authentication report = %+v", authentication)
|
|
}
|
|
}
|
|
|
|
func TestEngineSmokeTestUsesBuiltInHTTPClientForInferenceEvidence(t *testing.T) {
|
|
var observedURL string
|
|
var authorization string
|
|
options := Options{}
|
|
assembled, err := New(Config{HTTPClient: &http.Client{Transport: engineRoundTripperFunc(func(request *http.Request) (*http.Response, error) {
|
|
observedURL = request.URL.String()
|
|
authorization = request.Header.Get("Authorization")
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: make(http.Header),
|
|
Body: io.NopCloser(strings.NewReader(`{"choices":[{"message":{"content":"engine response"}}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)),
|
|
}, nil
|
|
})}}, options)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
report, err := assembled.SmokeTest(context.Background(), domain.SmokeTestRequest{
|
|
Kind: domain.SmokeTestTextInference, BackendID: "rakestrawhome",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if observedURL == "" || authorization != "" {
|
|
t.Fatalf("built-in request URL=%q authorization=%q", observedURL, authorization)
|
|
}
|
|
if !report.Completed || report.Facts.EndpointReachable == nil || !*report.Facts.EndpointReachable ||
|
|
report.Facts.ResponseReceived == nil || !*report.Facts.ResponseReceived ||
|
|
report.Facts.GenerationCompleted == nil || !*report.Facts.GenerationCompleted ||
|
|
report.Facts.CredentialsSupplied == nil || *report.Facts.CredentialsSupplied ||
|
|
report.Facts.TextReturned == nil || !*report.Facts.TextReturned ||
|
|
report.Facts.ExpectedTextMatched == nil || *report.Facts.ExpectedTextMatched || len(report.Attempts) != 1 ||
|
|
report.Attempts[0].Endpoint != observedURL {
|
|
t.Fatalf("built-in smoke report = %+v", report)
|
|
}
|
|
}
|
|
|
|
type engineSmokeClientFunc func(context.Context, domain.GenerateRequest) (*domain.GenerateResponse, error)
|
|
|
|
func (f engineSmokeClientFunc) Generate(ctx context.Context, request domain.GenerateRequest) (*domain.GenerateResponse, error) {
|
|
return f(ctx, request)
|
|
}
|
|
|
|
type engineRoundTripperFunc func(*http.Request) (*http.Response, error)
|
|
|
|
func (f engineRoundTripperFunc) RoundTrip(request *http.Request) (*http.Response, error) {
|
|
return f(request)
|
|
}
|