-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathinstructions_test.go
More file actions
265 lines (243 loc) · 6.99 KB
/
instructions_test.go
File metadata and controls
265 lines (243 loc) · 6.99 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package inventory
import (
"os"
"strings"
"testing"
)
// createTestInventory creates an inventory with the specified toolsets for testing.
// All toolsets are enabled by default using WithToolsets([]string{"all"}).
func createTestInventory(toolsets []ToolsetMetadata) *Inventory {
// Create tools for each toolset so they show up in AvailableToolsets()
var tools []ServerTool
for _, ts := range toolsets {
tools = append(tools, ServerTool{
Toolset: ts,
})
}
inv, _ := NewBuilder().
SetTools(tools).
WithToolsets([]string{"all"}).
Build()
return inv
}
func TestGenerateInstructions(t *testing.T) {
tests := []struct {
name string
toolsets []ToolsetMetadata
expectedEmpty bool
}{
{
name: "empty toolsets",
toolsets: []ToolsetMetadata{},
expectedEmpty: false, // base instructions are always included
},
{
name: "toolset with instructions",
toolsets: []ToolsetMetadata{
{
ID: "test",
Description: "Test toolset",
InstructionsFunc: func(_ *Inventory) string {
return "Test instructions"
},
},
},
expectedEmpty: false,
},
{
name: "toolset without instructions",
toolsets: []ToolsetMetadata{
{
ID: "test",
Description: "Test toolset",
},
},
expectedEmpty: false, // base instructions still included
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inv := createTestInventory(tt.toolsets)
result := generateInstructions(inv)
if tt.expectedEmpty {
if result != "" {
t.Errorf("Expected empty instructions but got: %s", result)
}
} else {
if result == "" {
t.Errorf("Expected non-empty instructions but got empty result")
}
}
})
}
}
func TestGenerateInstructionsWithDisableFlag(t *testing.T) {
tests := []struct {
name string
disableEnvValue string
expectedEmpty bool
}{
{
name: "DISABLE_INSTRUCTIONS=true returns empty",
disableEnvValue: "true",
expectedEmpty: true,
},
{
name: "DISABLE_INSTRUCTIONS=false returns normal instructions",
disableEnvValue: "false",
expectedEmpty: false,
},
{
name: "DISABLE_INSTRUCTIONS unset returns normal instructions",
disableEnvValue: "",
expectedEmpty: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Save original env value
originalValue := os.Getenv("DISABLE_INSTRUCTIONS")
defer func() {
if originalValue == "" {
os.Unsetenv("DISABLE_INSTRUCTIONS")
} else {
os.Setenv("DISABLE_INSTRUCTIONS", originalValue)
}
}()
// Set test env value
if tt.disableEnvValue == "" {
os.Unsetenv("DISABLE_INSTRUCTIONS")
} else {
os.Setenv("DISABLE_INSTRUCTIONS", tt.disableEnvValue)
}
inv := createTestInventory([]ToolsetMetadata{
{ID: "test", Description: "Test"},
})
result := generateInstructions(inv)
if tt.expectedEmpty {
if result != "" {
t.Errorf("Expected empty instructions but got: %s", result)
}
} else {
if result == "" {
t.Errorf("Expected non-empty instructions but got empty result")
}
}
})
}
}
func TestToolsetInstructionsFunc(t *testing.T) {
tests := []struct {
name string
toolsets []ToolsetMetadata
expectedToContain string
notExpectedToContain string
}{
{
name: "toolset with context-aware instructions includes extra text when dependency present",
toolsets: []ToolsetMetadata{
{ID: "repos", Description: "Repos"},
{
ID: "pull_requests",
Description: "PRs",
InstructionsFunc: func(inv *Inventory) string {
instructions := "PR base instructions"
if inv.HasToolset("repos") {
instructions += " PR template instructions"
}
return instructions
},
},
},
expectedToContain: "PR template instructions",
},
{
name: "toolset with context-aware instructions excludes extra text when dependency missing",
toolsets: []ToolsetMetadata{
{
ID: "pull_requests",
Description: "PRs",
InstructionsFunc: func(inv *Inventory) string {
instructions := "PR base instructions"
if inv.HasToolset("repos") {
instructions += " PR template instructions"
}
return instructions
},
},
},
notExpectedToContain: "PR template instructions",
},
{
name: "toolset without InstructionsFunc returns no toolset-specific instructions",
toolsets: []ToolsetMetadata{
{ID: "test", Description: "Test without instructions"},
},
notExpectedToContain: "## Test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inv := createTestInventory(tt.toolsets)
result := generateInstructions(inv)
if tt.expectedToContain != "" && !strings.Contains(result, tt.expectedToContain) {
t.Errorf("Expected result to contain '%s', but it did not. Result: %s", tt.expectedToContain, result)
}
if tt.notExpectedToContain != "" && strings.Contains(result, tt.notExpectedToContain) {
t.Errorf("Did not expect result to contain '%s', but it did. Result: %s", tt.notExpectedToContain, result)
}
})
}
}
// TestGenerateInstructionsOnlyEnabledToolsets verifies that generateInstructions
// only includes instructions from enabled toolsets, not all available toolsets.
// This is a regression test for https://github.com/github/github-mcp-server/issues/1897
func TestGenerateInstructionsOnlyEnabledToolsets(t *testing.T) {
// Create tools for multiple toolsets
reposToolset := ToolsetMetadata{
ID: "repos",
Description: "Repository tools",
InstructionsFunc: func(_ *Inventory) string {
return "REPOS_INSTRUCTIONS"
},
}
issuesToolset := ToolsetMetadata{
ID: "issues",
Description: "Issue tools",
InstructionsFunc: func(_ *Inventory) string {
return "ISSUES_INSTRUCTIONS"
},
}
prsToolset := ToolsetMetadata{
ID: "pull_requests",
Description: "PR tools",
InstructionsFunc: func(_ *Inventory) string {
return "PRS_INSTRUCTIONS"
},
}
tools := []ServerTool{
{Toolset: reposToolset},
{Toolset: issuesToolset},
{Toolset: prsToolset},
}
// Build inventory with only "repos" toolset enabled
inv, err := NewBuilder().
SetTools(tools).
WithToolsets([]string{"repos"}).
Build()
if err != nil {
t.Fatalf("Failed to build inventory: %v", err)
}
result := generateInstructions(inv)
// Should contain instructions from enabled toolset
if !strings.Contains(result, "REPOS_INSTRUCTIONS") {
t.Errorf("Expected instructions to contain 'REPOS_INSTRUCTIONS' for enabled toolset, but it did not. Result: %s", result)
}
// Should NOT contain instructions from non-enabled toolsets
if strings.Contains(result, "ISSUES_INSTRUCTIONS") {
t.Errorf("Did not expect instructions to contain 'ISSUES_INSTRUCTIONS' for disabled toolset, but it did. Result: %s", result)
}
if strings.Contains(result, "PRS_INSTRUCTIONS") {
t.Errorf("Did not expect instructions to contain 'PRS_INSTRUCTIONS' for disabled toolset, but it did. Result: %s", result)
}
}