2026-09-24 02:07:17 +00:00
|
|
|
package validators
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2026-11-10 18:13:12 +00:00
|
|
|
"io"
|
2026-09-24 02:07:17 +00:00
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2026-11-10 18:13:12 +00:00
|
|
|
"strings"
|
2026-09-24 02:07:17 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2026-10-11 01:40:38 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/llm"
|
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
|
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
|
2026-11-10 18:13:12 +00:00
|
|
|
ck "go.maximumdirect.net/configkit"
|
2026-09-24 02:07:17 +00:00
|
|
|
)
|
|
|
|
|
|
2026-11-10 18:13:12 +00:00
|
|
|
type validationTransport func(*http.Request) (*http.Response, error)
|
|
|
|
|
|
|
|
|
|
func (f validationTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
|
|
|
|
|
2026-09-24 02:07:17 +00:00
|
|
|
func TestValidationRepairAndRuntimeFailuresThroughDomainBoundary(t *testing.T) {
|
2026-09-25 12:38:23 +00:00
|
|
|
valid := `{"validations":[{"correction_index":1,"approved":true,"confidence":0.9,"reason":"ok"}]}`
|
2026-09-24 02:07:17 +00:00
|
|
|
for _, tc := range []struct {
|
2026-09-24 17:14:33 +00:00
|
|
|
name string
|
|
|
|
|
responses []string
|
|
|
|
|
wantCalls int
|
|
|
|
|
wantApproved bool
|
|
|
|
|
wantFailure outcomes.ReasonCode
|
2026-09-24 02:07:17 +00:00
|
|
|
}{
|
2026-09-24 17:14:33 +00:00
|
|
|
{"repair reaches decisions", []string{`{"wrong":true}`, valid}, 2, true, ""},
|
|
|
|
|
{"exhausted repair rejects batch", []string{`{"wrong":true}`, `{"wrong":true}`}, 2, false, outcomes.ReasonInvalidOutputExhausted},
|
|
|
|
|
{"malformed envelope excludes batch", []string{"MALFORMED_ENVELOPE"}, 1, false, outcomes.ReasonProviderResponseInvalid},
|
2026-09-24 02:07:17 +00:00
|
|
|
} {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
calls := 0
|
2026-11-10 18:13:12 +00:00
|
|
|
transport := validationTransport(func(r *http.Request) (*http.Response, error) {
|
2026-09-24 02:07:17 +00:00
|
|
|
calls++
|
|
|
|
|
output := tc.responses[len(tc.responses)-1]
|
|
|
|
|
if calls <= len(tc.responses) {
|
|
|
|
|
output = tc.responses[calls-1]
|
|
|
|
|
}
|
2026-11-10 18:13:12 +00:00
|
|
|
body := `{"choices":[]}`
|
|
|
|
|
if output != "MALFORMED_ENVELOPE" {
|
|
|
|
|
content, _ := json.Marshal(output)
|
|
|
|
|
body = fmt.Sprintf(`{"choices":[{"message":{"content":%s}}]}`, content)
|
2026-09-24 02:07:17 +00:00
|
|
|
}
|
2026-11-10 18:13:12 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil
|
|
|
|
|
})
|
2026-09-24 02:07:17 +00:00
|
|
|
dir := t.TempDir()
|
|
|
|
|
main := filepath.Join(dir, "config.yml")
|
2026-11-10 18:13:12 +00:00
|
|
|
if err := os.WriteFile(main, []byte("version: 3\n"), 0600); err != nil {
|
2026-09-24 02:07:17 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-10 18:13:12 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(dir, "prompts.yml"), []byte("profiles:\n - {id: base, endpoint: 'https://example.invalid/v1', model: local-model, api_key_required: false}\n"), 0600); err != nil {
|
2026-09-24 02:07:17 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-10 18:13:12 +00:00
|
|
|
loaded, err := config.Load(ck.Input{Source: ck.File(main)}, "")
|
2026-09-24 02:07:17 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-10 18:13:12 +00:00
|
|
|
runtime, err := llm.NewRuntime(context.Background(), loaded, &http.Client{Transport: transport})
|
2026-09-24 02:07:17 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-10 18:13:12 +00:00
|
|
|
settings := loaded.Config.LLM
|
|
|
|
|
settings.TransientRetries, settings.RepairAttempts = 0, 1
|
|
|
|
|
client, err := runtime.NewRoleClient("validation", "base", settings, llm.CallHooks{})
|
2026-09-24 02:07:17 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
validator, err := NewLLMBackedValidator("editorial_review", LLMValidatorTypeEditorialReview)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
req := makeReq([]proposals.EnrichedCorrectionProposal{mk(0, "gestures", "Jesters")})
|
|
|
|
|
req.LLMClient = client
|
|
|
|
|
result, err := validator.Validate(context.Background(), req)
|
|
|
|
|
if calls != tc.wantCalls {
|
|
|
|
|
t.Fatalf("calls=%d, want %d", calls, tc.wantCalls)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-09-24 17:14:33 +00:00
|
|
|
if len(result.Decisions) != 1 || result.Decisions[0].Approved != tc.wantApproved || len(result.Operations) != 1 || result.Operations[0].FailureReason != tc.wantFailure {
|
2026-09-24 02:07:17 +00:00
|
|
|
t.Fatalf("domain result: %+v", result)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|