|
| 1 | +package runner |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/go-logr/logr" |
| 9 | + "github.com/kagent-dev/kagent/go/api/adk" |
| 10 | + "google.golang.org/adk/agent" |
| 11 | + "google.golang.org/adk/session" |
| 12 | + "google.golang.org/genai" |
| 13 | +) |
| 14 | + |
| 15 | +func TestBuildReliabilityPlugins(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + config *adk.ReliabilityConfig |
| 19 | + wantNames []string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "nil config", |
| 23 | + config: nil, |
| 24 | + wantNames: nil, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "empty config", |
| 28 | + config: &adk.ReliabilityConfig{}, |
| 29 | + wantNames: nil, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "debug logging only", |
| 33 | + config: &adk.ReliabilityConfig{DebugLogging: new(true)}, |
| 34 | + wantNames: []string{"kagent_debug_logging"}, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "debug logging false", |
| 38 | + config: &adk.ReliabilityConfig{DebugLogging: new(false)}, |
| 39 | + wantNames: nil, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "tool retries only", |
| 43 | + config: &adk.ReliabilityConfig{ToolRetries: new(3)}, |
| 44 | + wantNames: []string{"RetryAndReflectPlugin"}, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "max llm calls only", |
| 48 | + config: &adk.ReliabilityConfig{MaxLLMCalls: new(10)}, |
| 49 | + wantNames: []string{"kagent_max_llm_calls"}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "all enabled", |
| 53 | + config: &adk.ReliabilityConfig{ |
| 54 | + ToolRetries: new(2), |
| 55 | + MaxLLMCalls: new(50), |
| 56 | + DebugLogging: new(true), |
| 57 | + }, |
| 58 | + wantNames: []string{"kagent_debug_logging", "RetryAndReflectPlugin", "kagent_max_llm_calls"}, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + for _, tt := range tests { |
| 63 | + t.Run(tt.name, func(t *testing.T) { |
| 64 | + plugins, err := buildReliabilityPlugins(tt.config, logr.Discard()) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("buildReliabilityPlugins() error = %v", err) |
| 67 | + } |
| 68 | + if len(plugins) != len(tt.wantNames) { |
| 69 | + t.Fatalf("got %d plugins, want %d", len(plugins), len(tt.wantNames)) |
| 70 | + } |
| 71 | + for i, want := range tt.wantNames { |
| 72 | + if got := plugins[i].Name(); got != want { |
| 73 | + t.Errorf("plugin[%d].Name() = %q, want %q", i, got, want) |
| 74 | + } |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// fakeCallbackContext is a minimal agent.CallbackContext for testing. |
| 81 | +type fakeCallbackContext struct { |
| 82 | + context.Context |
| 83 | + invocationID string |
| 84 | +} |
| 85 | + |
| 86 | +func (f *fakeCallbackContext) UserContent() *genai.Content { return nil } |
| 87 | +func (f *fakeCallbackContext) InvocationID() string { return f.invocationID } |
| 88 | +func (f *fakeCallbackContext) AgentName() string { return "test-agent" } |
| 89 | +func (f *fakeCallbackContext) ReadonlyState() session.ReadonlyState { return nil } |
| 90 | +func (f *fakeCallbackContext) UserID() string { return "user" } |
| 91 | +func (f *fakeCallbackContext) AppName() string { return "app" } |
| 92 | +func (f *fakeCallbackContext) SessionID() string { return "session" } |
| 93 | +func (f *fakeCallbackContext) Branch() string { return "" } |
| 94 | +func (f *fakeCallbackContext) Artifacts() agent.Artifacts { return nil } |
| 95 | +func (f *fakeCallbackContext) State() session.State { return nil } |
| 96 | + |
| 97 | +func TestMaxLLMCallsPlugin(t *testing.T) { |
| 98 | + p, err := newMaxLLMCallsPlugin(2) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("newMaxLLMCallsPlugin() error = %v", err) |
| 101 | + } |
| 102 | + cb := p.BeforeModelCallback() |
| 103 | + if cb == nil { |
| 104 | + t.Fatal("BeforeModelCallback is nil") |
| 105 | + } |
| 106 | + |
| 107 | + ctxA := &fakeCallbackContext{Context: context.Background(), invocationID: "inv-a"} |
| 108 | + ctxB := &fakeCallbackContext{Context: context.Background(), invocationID: "inv-b"} |
| 109 | + |
| 110 | + // First two calls within the limit succeed. |
| 111 | + for i := range 2 { |
| 112 | + if _, err := cb(ctxA, nil); err != nil { |
| 113 | + t.Fatalf("call %d: unexpected error: %v", i+1, err) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Third call exceeds the limit. |
| 118 | + if _, err := cb(ctxA, nil); err == nil { |
| 119 | + t.Fatal("expected error after exceeding limit, got nil") |
| 120 | + } else if !strings.Contains(err.Error(), "limit of 2 model calls") { |
| 121 | + t.Errorf("unexpected error message: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + // A different invocation has its own counter. |
| 125 | + if _, err := cb(ctxB, nil); err != nil { |
| 126 | + t.Fatalf("different invocation should not be limited: %v", err) |
| 127 | + } |
| 128 | +} |
0 commit comments