-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathfeature_flags_test.go
More file actions
200 lines (171 loc) Β· 5.68 KB
/
feature_flags_test.go
File metadata and controls
200 lines (171 loc) Β· 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package github
import (
"context"
"encoding/json"
"testing"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/github/github-mcp-server/pkg/inventory"
"github.com/github/github-mcp-server/pkg/scopes"
"github.com/github/github-mcp-server/pkg/utils"
)
// RemoteMCPEnthusiasticGreeting is a dummy test feature flag .
const RemoteMCPEnthusiasticGreeting = "remote_mcp_enthusiastic_greeting"
// FeatureChecker is an interface for checking if a feature flag is enabled.
type FeatureChecker interface {
// IsFeatureEnabled checks if a feature flag is enabled.
IsFeatureEnabled(ctx context.Context, flagName string) bool
}
// HelloWorld returns a simple greeting tool that demonstrates feature flag conditional behavior.
// This tool is for testing and demonstration purposes only.
func HelloWorldTool(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetMetadataContext, // Use existing "context" toolset
mcp.Tool{
Name: "hello_world",
Description: t("TOOL_HELLO_WORLD_DESCRIPTION", "A simple greeting tool that demonstrates feature flag conditional behavior"),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_HELLO_WORLD_TITLE", "Hello World"),
ReadOnlyHint: true,
},
},
[]scopes.Scope{},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, _ map[string]any) (*mcp.CallToolResult, any, error) {
// Check feature flag to determine greeting style
greeting := "Hello, world!"
if deps.IsFeatureEnabled(ctx, RemoteMCPEnthusiasticGreeting) {
greeting += " Welcome to the future of MCP! π"
}
if deps.GetFlags(ctx).InsidersMode {
greeting += " Experimental features are enabled! π"
}
// Build response
response := map[string]any{
"greeting": greeting,
}
jsonBytes, err := json.Marshal(response)
if err != nil {
return utils.NewToolResultError("failed to marshal response"), nil, nil
}
return utils.NewToolResultText(string(jsonBytes)), nil, nil
},
)
}
func TestHelloWorld_ConditionalBehavior_Featureflag(t *testing.T) {
t.Parallel()
tests := []struct {
name string
featureFlagEnabled bool
inputName string
expectedGreeting string
}{
{
name: "Feature flag disabled - default greeting",
featureFlagEnabled: false,
expectedGreeting: "Hello, world!",
},
{
name: "Feature flag enabled - enthusiastic greeting",
featureFlagEnabled: true,
expectedGreeting: "Hello, world! Welcome to the future of MCP! π",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// Create feature checker based on test case
checker := func(_ context.Context, flagName string) (bool, error) {
if flagName == RemoteMCPEnthusiasticGreeting {
return tt.featureFlagEnabled, nil
}
return false, nil
}
// Create deps with the checker
deps := NewBaseDeps(
nil, nil, nil, nil,
translations.NullTranslationHelper,
FeatureFlags{},
0,
checker,
stubExporters(),
)
// Get the tool and its handler
tool := HelloWorldTool(translations.NullTranslationHelper)
handler := tool.Handler(deps)
// Call the handler with deps in context
ctx := ContextWithDeps(context.Background(), deps)
result, err := handler(ctx, &mcp.CallToolRequest{
Params: &mcp.CallToolParamsRaw{
Arguments: json.RawMessage(`{}`),
},
})
require.NoError(t, err)
require.NotNil(t, result)
require.Len(t, result.Content, 1)
// Parse the response - should be TextContent
textContent, ok := result.Content[0].(*mcp.TextContent)
require.True(t, ok, "expected content to be TextContent")
var response map[string]any
err = json.Unmarshal([]byte(textContent.Text), &response)
require.NoError(t, err)
// Verify the greeting matches expected based on feature flag
assert.Equal(t, tt.expectedGreeting, response["greeting"])
})
}
}
func TestHelloWorld_ConditionalBehavior_Config(t *testing.T) {
t.Parallel()
tests := []struct {
name string
insidersMode bool
expectedGreeting string
}{
{
name: "Experimental disabled - default greeting",
insidersMode: false,
expectedGreeting: "Hello, world!",
},
{
name: "Experimental enabled - experimental greeting",
insidersMode: true,
expectedGreeting: "Hello, world! Experimental features are enabled! π",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// Create deps with the checker
deps := NewBaseDeps(
nil, nil, nil, nil,
translations.NullTranslationHelper,
FeatureFlags{InsidersMode: tt.insidersMode},
0,
nil,
stubExporters(),
)
// Get the tool and its handler
tool := HelloWorldTool(translations.NullTranslationHelper)
handler := tool.Handler(deps)
// Call the handler with deps in context
ctx := ContextWithDeps(context.Background(), deps)
result, err := handler(ctx, &mcp.CallToolRequest{
Params: &mcp.CallToolParamsRaw{
Arguments: json.RawMessage(`{}`),
},
})
require.NoError(t, err)
require.NotNil(t, result)
require.Len(t, result.Content, 1)
// Parse the response - should be TextContent
textContent, ok := result.Content[0].(*mcp.TextContent)
require.True(t, ok, "expected content to be TextContent")
var response map[string]any
err = json.Unmarshal([]byte(textContent.Text), &response)
require.NoError(t, err)
// Verify the greeting matches expected based on feature flag
assert.Equal(t, tt.expectedGreeting, response["greeting"])
})
}
}