|
| 1 | +package inventory |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func makeTool(name string) ServerTool { |
| 12 | + return ServerTool{ |
| 13 | + Tool: mcp.Tool{ |
| 14 | + Name: name, |
| 15 | + Description: "Tool " + name, |
| 16 | + }, |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func makeOverride(name, desc string) ServerTool { |
| 21 | + return ServerTool{ |
| 22 | + Tool: mcp.Tool{ |
| 23 | + Name: name, |
| 24 | + Description: desc, |
| 25 | + }, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestToolOverrides_Apply(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + overrides := ToolOverrides{ |
| 33 | + "create_issue": { |
| 34 | + ToolName: "create_issue", |
| 35 | + Condition: func(_ context.Context) (bool, error) { return true, nil }, |
| 36 | + Override: makeOverride("create_issue", "Enterprise variant"), |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + ctx := context.Background() |
| 41 | + |
| 42 | + // Tool with override |
| 43 | + result := overrides.Apply(ctx, "create_issue") |
| 44 | + assert.NotNil(t, result) |
| 45 | + assert.Equal(t, "Enterprise variant", result.Tool.Description) |
| 46 | + |
| 47 | + // Tool without override |
| 48 | + result = overrides.Apply(ctx, "list_repos") |
| 49 | + assert.Nil(t, result) |
| 50 | +} |
| 51 | + |
| 52 | +func TestToolOverrides_Apply_ConditionFalse(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + |
| 55 | + overrides := ToolOverrides{ |
| 56 | + "create_issue": { |
| 57 | + ToolName: "create_issue", |
| 58 | + Condition: func(_ context.Context) (bool, error) { return false, nil }, |
| 59 | + Override: makeOverride("create_issue", "Enterprise variant"), |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + ctx := context.Background() |
| 64 | + |
| 65 | + // Condition doesn't match - no override |
| 66 | + result := overrides.Apply(ctx, "create_issue") |
| 67 | + assert.Nil(t, result) |
| 68 | +} |
| 69 | + |
| 70 | +func TestToolOverrides_Apply_NilCondition(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + |
| 73 | + overrides := ToolOverrides{ |
| 74 | + "create_issue": { |
| 75 | + ToolName: "create_issue", |
| 76 | + // nil Condition - always applies |
| 77 | + Override: makeOverride("create_issue", "Always applied"), |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + ctx := context.Background() |
| 82 | + |
| 83 | + result := overrides.Apply(ctx, "create_issue") |
| 84 | + assert.NotNil(t, result) |
| 85 | + assert.Equal(t, "Always applied", result.Tool.Description) |
| 86 | +} |
| 87 | + |
| 88 | +func TestToolOverrides_ApplyToTools(t *testing.T) { |
| 89 | + t.Parallel() |
| 90 | + |
| 91 | + tools := []*ServerTool{ |
| 92 | + ptr(makeTool("create_issue")), |
| 93 | + ptr(makeTool("list_repos")), |
| 94 | + ptr(makeTool("get_me")), |
| 95 | + } |
| 96 | + |
| 97 | + overrides := ToolOverrides{ |
| 98 | + "create_issue": { |
| 99 | + ToolName: "create_issue", |
| 100 | + Condition: func(_ context.Context) (bool, error) { return true, nil }, |
| 101 | + Override: makeOverride("create_issue", "Enterprise create_issue"), |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + ctx := context.Background() |
| 106 | + result := overrides.ApplyToTools(ctx, tools) |
| 107 | + |
| 108 | + assert.Len(t, result, 3) |
| 109 | + assert.Equal(t, "Enterprise create_issue", result[0].Tool.Description) |
| 110 | + assert.Equal(t, "Tool list_repos", result[1].Tool.Description) |
| 111 | + assert.Equal(t, "Tool get_me", result[2].Tool.Description) |
| 112 | +} |
| 113 | + |
| 114 | +func TestToolOverrides_ApplyToTools_Empty(t *testing.T) { |
| 115 | + t.Parallel() |
| 116 | + |
| 117 | + tools := []*ServerTool{ |
| 118 | + ptr(makeTool("create_issue")), |
| 119 | + } |
| 120 | + |
| 121 | + overrides := ToolOverrides{} |
| 122 | + |
| 123 | + ctx := context.Background() |
| 124 | + result := overrides.ApplyToTools(ctx, tools) |
| 125 | + |
| 126 | + // Empty overrides returns original slice |
| 127 | + assert.Equal(t, tools, result) |
| 128 | +} |
| 129 | + |
| 130 | +func ptr(t ServerTool) *ServerTool { |
| 131 | + return &t |
| 132 | +} |
| 133 | + |
| 134 | +func BenchmarkToolOverrides_ApplyToTools(b *testing.B) { |
| 135 | + // 130 tools, 2 overrides (realistic) |
| 136 | + tools := make([]*ServerTool, 130) |
| 137 | + for i := range tools { |
| 138 | + tools[i] = ptr(makeTool("tool_" + string(rune('a'+i%26)))) |
| 139 | + } |
| 140 | + |
| 141 | + overrides := ToolOverrides{ |
| 142 | + "tool_a": { |
| 143 | + ToolName: "tool_a", |
| 144 | + Condition: func(_ context.Context) (bool, error) { return true, nil }, |
| 145 | + Override: makeOverride("tool_a", "Override A"), |
| 146 | + }, |
| 147 | + "tool_b": { |
| 148 | + ToolName: "tool_b", |
| 149 | + Condition: func(_ context.Context) (bool, error) { return true, nil }, |
| 150 | + Override: makeOverride("tool_b", "Override B"), |
| 151 | + }, |
| 152 | + } |
| 153 | + |
| 154 | + ctx := context.Background() |
| 155 | + |
| 156 | + b.ReportAllocs() // Only count allocs in the hot loop |
| 157 | + b.ResetTimer() |
| 158 | + for i := 0; i < b.N; i++ { |
| 159 | + _ = overrides.ApplyToTools(ctx, tools) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func BenchmarkToolOverrides_ApplyToTools_NoMatch(b *testing.B) { |
| 164 | + // 130 tools, overrides don't match any - should be zero alloc |
| 165 | + tools := make([]*ServerTool, 130) |
| 166 | + for i := range tools { |
| 167 | + tools[i] = ptr(makeTool("tool_" + string(rune('a'+i%26)))) |
| 168 | + } |
| 169 | + |
| 170 | + // Override exists but for a tool not in list |
| 171 | + overrides := ToolOverrides{ |
| 172 | + "nonexistent_tool": { |
| 173 | + ToolName: "nonexistent_tool", |
| 174 | + Override: makeOverride("nonexistent_tool", "Override"), |
| 175 | + }, |
| 176 | + } |
| 177 | + |
| 178 | + ctx := context.Background() |
| 179 | + |
| 180 | + b.ReportAllocs() |
| 181 | + b.ResetTimer() |
| 182 | + for i := 0; i < b.N; i++ { |
| 183 | + _ = overrides.ApplyToTools(ctx, tools) |
| 184 | + } |
| 185 | +} |
0 commit comments