GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test(llm): add end-to-end pipeline tests with Anthropic codec · majorcontext/keep@546e102

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • llm
    • pipeline_integration_test.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎llm/pipeline_integration_test.go‎

Copy file name to clipboard
+159Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package llm_test
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/majorcontext/keep"
8+
"github.com/majorcontext/keep/llm"
9+
"github.com/majorcontext/keep/llm/anthropic"
10+
"github.com/majorcontext/keep/sse"
11+
)
12+
13+
func TestPipeline_Anthropic_DenyToolUse(t *testing.T) {
14+
rules := []byte(`
15+
scope: gateway
16+
mode: enforce
17+
rules:
18+
- name: no-dangerous-tools
19+
match:
20+
operation: "llm.tool_use"
21+
when: "params.name == 'rm_rf'"
22+
action: deny
23+
message: "dangerous tool blocked"
24+
`)
25+
engine, err := keep.LoadFromBytes(rules)
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
defer engine.Close()
30+
31+
codec := anthropic.NewCodec()
32+
respBody, _ := json.Marshal(map[string]any{
33+
"id": "msg_1", "type": "message", "role": "assistant",
34+
"model": "claude-sonnet-4-20250514",
35+
"content": []any{
36+
map[string]any{"type": "tool_use", "id": "tu_1", "name": "rm_rf", "input": map[string]any{"path": "/"}},
37+
},
38+
"stop_reason": "tool_use",
39+
})
40+
41+
result, err := llm.EvaluateResponse(engine, codec, respBody, "gateway", llm.DecomposeConfig{})
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
if result.Decision != keep.Deny {
46+
t.Errorf("got %q, want deny", result.Decision)
47+
}
48+
if result.Rule != "no-dangerous-tools" {
49+
t.Errorf("got rule %q, want no-dangerous-tools", result.Rule)
50+
}
51+
}
52+
53+
func TestPipeline_Anthropic_AllowRequest(t *testing.T) {
54+
rules := []byte(`
55+
scope: gateway
56+
mode: enforce
57+
rules:
58+
- name: log-all
59+
match:
60+
operation: "*"
61+
action: log
62+
`)
63+
engine, err := keep.LoadFromBytes(rules)
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
defer engine.Close()
68+
69+
codec := anthropic.NewCodec()
70+
reqBody, _ := json.Marshal(map[string]any{
71+
"model": "claude-sonnet-4-20250514",
72+
"max_tokens": 1024,
73+
"messages": []any{
74+
map[string]any{"role": "user", "content": "Hello"},
75+
},
76+
})
77+
78+
result, err := llm.EvaluateRequest(engine, codec, reqBody, "gateway", llm.DecomposeConfig{})
79+
if err != nil {
80+
t.Fatal(err)
81+
}
82+
if result.Decision != keep.Allow {
83+
t.Errorf("got %q, want allow", result.Decision)
84+
}
85+
if result.Body == nil {
86+
t.Error("expected non-nil body for allow decision")
87+
}
88+
}
89+
90+
func TestPipeline_Anthropic_StreamAllow(t *testing.T) {
91+
rules := []byte(`
92+
scope: gateway
93+
mode: enforce
94+
rules:
95+
- name: log-all
96+
match:
97+
operation: "*"
98+
action: log
99+
`)
100+
engine, err := keep.LoadFromBytes(rules)
101+
if err != nil {
102+
t.Fatal(err)
103+
}
104+
defer engine.Close()
105+
106+
codec := anthropic.NewCodec()
107+
108+
msgStart, _ := json.Marshal(map[string]any{
109+
"type": "message_start",
110+
"message": map[string]any{
111+
"id": "msg_1", "type": "message", "role": "assistant",
112+
"model": "claude-sonnet-4-20250514", "content": []any{},
113+
"stop_reason": nil,
114+
"usage": map[string]any{"input_tokens": 10, "output_tokens": 0},
115+
},
116+
})
117+
blockStart, _ := json.Marshal(map[string]any{
118+
"type": "content_block_start",
119+
"index": 0,
120+
"content_block": map[string]any{"type": "text", "text": ""},
121+
})
122+
blockDelta, _ := json.Marshal(map[string]any{
123+
"type": "content_block_delta",
124+
"index": 0,
125+
"delta": map[string]any{"type": "text_delta", "text": "Hi!"},
126+
})
127+
blockStop, _ := json.Marshal(map[string]any{
128+
"type": "content_block_stop",
129+
"index": 0,
130+
})
131+
msgDelta, _ := json.Marshal(map[string]any{
132+
"type": "message_delta",
133+
"delta": map[string]any{"stop_reason": "end_turn"},
134+
"usage": map[string]any{"output_tokens": 3},
135+
})
136+
msgStop, _ := json.Marshal(map[string]any{
137+
"type": "message_stop",
138+
})
139+
140+
events := []sse.Event{
141+
{Type: "message_start", Data: string(msgStart)},
142+
{Type: "content_block_start", Data: string(blockStart)},
143+
{Type: "content_block_delta", Data: string(blockDelta)},
144+
{Type: "content_block_stop", Data: string(blockStop)},
145+
{Type: "message_delta", Data: string(msgDelta)},
146+
{Type: "message_stop", Data: string(msgStop)},
147+
}
148+
149+
result, err := llm.EvaluateStream(engine, codec, events, "gateway", llm.DecomposeConfig{})
150+
if err != nil {
151+
t.Fatal(err)
152+
}
153+
if result.Decision != keep.Allow {
154+
t.Errorf("got %q, want allow", result.Decision)
155+
}
156+
if len(result.Events) != len(events) {
157+
t.Errorf("got %d events, want %d (original replayed)", len(result.Events), len(events))
158+
}
159+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.