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) } }) } } func TestSmokeCaptureFailureStopsBeforeFurtherChecks(t *testing.T) { input := configkit.Input{Destination: configkit.Keys("promptkit"), Source: configkit.Mapping("smoke", map[string]any{"profiles": []any{map[string]any{"id": "partial", "endpoint": "http://example.invalid/v1", "model": "fixture"}}})} engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &input}}, promptkit.WithBundledOpenRouter(false), promptkit.WithLLMClient(noGeneration{t})) if err != nil { t.Fatal(err) } cause := errors.New("capture failed") observations, err := RunSmokeChecksObserved(context.Background(), engine, Targets{Access: []AccessTarget{{ProfileID: "partial"}}, Profiles: []ProfileTarget{{ID: "partial"}}}, nil, nil, func(SmokeObservation) error { return cause }) if !errors.Is(err, cause) || len(observations) != 1 { t.Fatalf("observations=%+v err=%v", observations, err) } } type smokeResultClient struct{ err error } func (client smokeResultClient) Generate(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) { return &promptkit.GenerateResponse{Content: `{"status":"ok"}`}, client.err } func TestSmokeRetainsTerminalObserverAndDiagnosticErrors(t *testing.T) { providerErr := errors.New("provider failed") observerErr := errors.New("observer failed") for _, cause := range []error{nil, providerErr} { 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(smokeResultClient{cause})) if err != nil { t.Fatal(err) } observations, err := RunSmokeChecksObserved(context.Background(), engine, Targets{Profiles: []ProfileTarget{{ID: "profile"}}}, nil, nil, func(SmokeObservation) error { return observerErr }) if len(observations) != 1 || !errors.Is(err, observerErr) || cause != nil && !errors.Is(err, observations[0].Err) { t.Fatalf("observations=%+v error=%v", observations, err) } if (observations[0].Err != nil) != (cause != nil) { t.Fatalf("recording failure changed native verdict: %+v", observations[0]) } } } func TestSmokeRetainsCancellationDuringTerminalObservation(t *testing.T) { 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(smokeResultClient{})) if err != nil { t.Fatal(err) } ctx, cancel := context.WithCancel(context.Background()) defer cancel() observations, err := RunSmokeChecksObserved(ctx, engine, Targets{Profiles: []ProfileTarget{{ID: "profile"}}}, nil, nil, func(SmokeObservation) error { cancel(); return nil }) if !errors.Is(err, context.Canceled) || len(observations) != 1 || observations[0].Err != nil { t.Fatalf("observations=%+v error=%v", observations, err) } }