Files
llmgw/internal/router/guard_test.go
T
Ilker ManapandClaude Opus 5.5 9b9349183b gofmt the guard hooks
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 12:23:02 +02:00

162 lines
5.6 KiB
Go

package router
import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
"gitea.mynodes.xyz/ilker/llmgw/internal/guard"
)
// wordGuard blocks requests saying "forbidden", rewrites "alice@example.com",
// flags "maybe", and stops answers saying "hi".
type wordGuard struct {
err error
subject guard.Subject
// answers, when set, stops answers saying "hi".
answers bool
}
func (g *wordGuard) Input(_ context.Context, s guard.Subject, body []byte) (guard.Verdict, error) {
g.subject = s
switch {
case g.err != nil:
return guard.Verdict{}, g.err
case bytes.Contains(body, []byte("forbidden")):
return guard.Verdict{Action: guard.Block, Policy: "words", Category: "test"}, nil
case bytes.Contains(body, []byte("alice@example.com")):
return guard.Verdict{Action: guard.Redact, Policy: "pii", Category: "email",
Body: bytes.ReplaceAll(body, []byte("alice@example.com"), []byte("[EMAIL_1]"))}, nil
case bytes.Contains(body, []byte("maybe")):
return guard.Verdict{Action: guard.Flag, Policy: "words"}, nil
}
return guard.Verdict{}, nil
}
func (g *wordGuard) Output(context.Context, guard.Subject) guard.OutputFilter {
if !g.answers {
return nil
}
return &stopHi{}
}
type stopHi struct{ v guard.Verdict }
func (f *stopHi) Buffered(body []byte) []byte {
if !bytes.Contains(body, []byte(`"hi"`)) {
return body
}
f.v = guard.Verdict{Action: guard.Block, Stage: guard.Output, Policy: "answers"}
return []byte(`{"choices":[{"index":0,"message":{"role":"assistant","content":""},"finish_reason":"content_filter"}]}`)
}
func (f *stopHi) OnEvent(d string) ([]string, bool) { return []string{d}, false }
func (f *stopHi) OnEnd() []string { return nil }
func (f *stopHi) Verdict() guard.Verdict { return f.v }
func guardedRouter(t *testing.T, g guard.Guard) (*Router, *fakeEngine) {
e := newFakeEngine(t, "m")
rt := newTestRouter(t, baseConfig(config.Endpoint{ID: "a", URL: e.srv.URL, Models: []string{"m"}}))
rt.SetGuard(g)
return rt, e
}
func say(text string) map[string]any {
return map[string]any{"model": "m", "messages": []any{map[string]any{"role": "user", "content": text}}}
}
func TestABlockedRequestIsRefusedBeforeTheEngine(t *testing.T) {
rt, e := guardedRouter(t, &wordGuard{})
rec := post(t, rt.Handler(), say("something forbidden"), nil)
var body struct {
Error struct{ Type, Code, Message string } `json:"error"`
}
_ = json.Unmarshal(rec.Body.Bytes(), &body)
if rec.Code != http.StatusBadRequest || body.Error.Code != policyViolation ||
rec.Header().Get(guardHeader) != "blocked" || rec.Header().Get(guardPolicyHeader) != "words" {
t.Errorf("%d %v %s", rec.Code, rec.Header(), rec.Body.String())
}
if strings.Contains(rec.Body.String(), "forbidden") {
t.Error("the refusal quotes what it refused")
}
if e.calls.Load() != 0 {
t.Error("the engine was asked anyway")
}
}
func TestARedactedRequestReachesTheEngineRewritten(t *testing.T) {
rt, e := guardedRouter(t, &wordGuard{})
rec := post(t, rt.Handler(), say("write to alice@example.com"), nil)
if rec.Header().Get(guardHeader) != "redacted" {
t.Errorf("headers %v", rec.Header())
}
sent, _ := e.last.Load().([]byte)
if bytes.Contains(sent, []byte("alice@example.com")) || !bytes.Contains(sent, []byte("[EMAIL_1]")) {
t.Errorf("the engine was sent %s", sent)
}
}
func TestAnAnswerAGuardStopsEndsAsContentFilter(t *testing.T) {
rt, _ := guardedRouter(t, &wordGuard{answers: true})
rec := post(t, rt.Handler(), say("hello"), nil)
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"finish_reason":"content_filter"`) ||
strings.Contains(rec.Body.String(), `"hi"`) || rec.Header().Get(guardHeader) != "blocked" {
t.Errorf("%d %v %s", rec.Code, rec.Header(), rec.Body.String())
}
}
func TestAGuardThatCannotJudgeRefuses(t *testing.T) {
g := &wordGuard{err: errors.New("judge down")}
rt, e := guardedRouter(t, g)
rec := post(t, rt.Handler(), say("hello"), nil)
if rec.Code != http.StatusServiceUnavailable || e.calls.Load() != 0 || strings.Contains(rec.Body.String(), "judge down") {
t.Errorf("%d %s, engine calls %d", rec.Code, rec.Body.String(), e.calls.Load())
}
if g.subject.Model != "m" || g.subject.Path != "/v1/chat/completions" {
t.Errorf("judged as %+v", g.subject)
}
}
func TestAFlaggedRequestIsServedAndSaysSo(t *testing.T) {
rt, e := guardedRouter(t, &wordGuard{})
rec := post(t, rt.Handler(), say("maybe"), nil)
if rec.Code != http.StatusOK || rec.Header().Get(guardHeader) != "flagged" || e.calls.Load() != 1 {
t.Errorf("%d %v", rec.Code, rec.Header())
}
}
func TestABlockedRequestIsRecordedWithItsVerdictAndCostsNothing(t *testing.T) {
e := newFakeEngine(t, "qwen2.5-14b-instruct")
rt, secret := pricedRouter(t, e.srv.URL)
rt.SetGuard(&wordGuard{})
rec := post(t, rt.Handler(),
map[string]any{"model": "q14", "messages": []any{map[string]any{"role": "user", "content": "forbidden"}}},
map[string]string{"Authorization": "Bearer " + secret, "X-Request-Id": "blocked-1"})
if rec.Code != http.StatusBadRequest {
t.Fatalf("status %d: %s", rec.Code, rec.Body.String())
}
deadline := time.Now().Add(5 * time.Second)
for {
u, ok, err := rt.store.UsageByRequest(t.Context(), "blocked-1")
if err != nil {
t.Fatal(err)
}
if ok {
if u.GuardAction != "blocked" || u.GuardPolicy != "words" || u.GuardCategory != "test" ||
u.CostMicros != 0 || u.PromptTokens != 0 || u.Status != http.StatusBadRequest {
t.Errorf("recorded %+v", u)
}
return
}
if time.Now().After(deadline) {
t.Fatal("the blocked request was not recorded")
}
time.Sleep(20 * time.Millisecond)
}
}