Files
notarius/internal/preflight/smoke_test.go

101 lines
4.6 KiB
Go

package preflight
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
)
type cancelingClient struct{ cancel context.CancelFunc }
func (client cancelingClient) Generate(ctx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
client.cancel()
<-ctx.Done()
return nil, ctx.Err()
}
func TestSmokeCancellationRetainsPartialEvidence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
input := configkit.Input{Destination: configkit.Keys("promptkit"), Source: configkit.Mapping("smoke", map[string]any{
"profiles": []any{map[string]any{"id": "profile", "endpoint": "http://example.invalid/v1", "model": "fixture"}},
})}
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &input}}, promptkit.WithBundledOpenRouter(false), promptkit.WithLLMClient(cancelingClient{cancel}))
if err != nil {
t.Fatal(err)
}
observations, err := RunSmokeChecks(ctx, engine, Targets{Access: []AccessTarget{{ProfileID: "profile"}}, Profiles: []ProfileTarget{{ID: "profile"}}}, nil, nil)
if !errors.Is(err, context.Canceled) || len(observations) != 3 || observations[0].Applicable || observations[1].Applicable || observations[2].Report == nil || observations[2].Report.Completed {
t.Fatalf("canceled checks = %+v, %v", observations, err)
}
}
func TestSmokeChecksUseSelectedProfileForAccessAndGeneration(t *testing.T) {
var gets, posts atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
gets.Add(1)
fmt.Fprint(w, `{"data":{"label":"member","is_free_tier":false,"usage":0}}`)
return
}
posts.Add(1)
fmt.Fprint(w, `{"choices":[{"message":{"role":"assistant","content":"{\"status\":\"ok\"}"},"finish_reason":"stop"}]}`)
}))
defer server.Close()
input := configkit.Input{Destination: configkit.Keys("promptkit"), Source: configkit.Mapping("smoke", map[string]any{
"backends": []any{map[string]any{"id": "backend", "provider": "openrouter", "endpoint": "http://127.0.0.1:1/v1"}},
"profiles": []any{map[string]any{"id": "first", "backend": "backend", "endpoint": server.URL + "/v1", "model": "one"}, map[string]any{"id": "second", "backend": "backend", "endpoint": server.URL + "/v1", "model": "two"}},
})}
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &input}}, promptkit.WithBundledOpenRouter(false))
if err != nil {
t.Fatal(err)
}
targets := Targets{Access: []AccessTarget{{BackendID: "backend", ProfileID: "first"}}, Profiles: []ProfileTarget{{ID: "first"}, {ID: "second"}}}
model := "override"
retries := 0
observations, err := RunSmokeChecks(context.Background(), engine, targets, &promptkit.ExecutionTargetOverride{Model: model}, &retries)
if err != nil {
t.Fatalf("checks: %v; observations: %+v", err, observations)
}
if gets.Load() != 2 || posts.Load() != 2 || len(observations) != 4 {
t.Fatalf("gets=%d posts=%d observations=%+v", gets.Load(), posts.Load(), observations)
}
for _, observation := range observations {
if observation.Report.Target.EffectiveSettings.Model != model {
t.Fatalf("override missing: %+v", observation.Report.Target)
}
}
}
func TestSmokeVerdictsRequireAffirmativeFacts(t *testing.T) {
yes, no := true, false
for _, test := range []struct {
name string
report *promptkit.SmokeTestReport
err error
applicable, failed bool
}{
{"passed", &promptkit.SmokeTestReport{Supported: true, Completed: true, Facts: promptkit.SmokeTestFacts{GenerationCompleted: &yes, ValidJSON: &yes, SchemaMatched: &yes}}, nil, true, false},
{"negative", &promptkit.SmokeTestReport{Supported: true, Completed: true, Facts: promptkit.SmokeTestFacts{GenerationCompleted: &yes, ValidJSON: &yes, SchemaMatched: &no}}, nil, true, true},
{"unknown", &promptkit.SmokeTestReport{Supported: true, Completed: true}, nil, true, true},
{"unfinished", &promptkit.SmokeTestReport{Supported: true}, nil, true, true},
{"unsupported", &promptkit.SmokeTestReport{}, nil, false, false},
{"inapplicable", &promptkit.SmokeTestReport{Supported: true, Applicable: &no}, nil, false, false},
{"canceled", &promptkit.SmokeTestReport{}, context.Canceled, true, true},
} {
t.Run(test.name, func(t *testing.T) {
applicable, err := evaluateSmoke(promptkit.SmokeTestStructuredOutput, test.report, test.err)
if applicable != test.applicable || (err != nil) != test.failed || test.err != nil && !errors.Is(err, test.err) {
t.Fatalf("verdict = %v, %v", applicable, err)
}
})
}
}