-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathissue_dependencies_test.go
More file actions
372 lines (344 loc) · 12.6 KB
/
Copy pathissue_dependencies_test.go
File metadata and controls
372 lines (344 loc) · 12.6 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package github
import (
"context"
"encoding/json"
"net/http"
"strconv"
"testing"
"github.com/github/github-mcp-server/internal/toolsnaps"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/google/jsonschema-go/jsonschema"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
endpointBlockedBy = EndpointPattern("GET /repos/{owner}/{repo}/issues/{issue_number}/dependencies/blocked_by")
endpointBlocking = EndpointPattern("GET /repos/{owner}/{repo}/issues/{issue_number}/dependencies/blocking")
endpointAddBlock = EndpointPattern("POST /repos/{owner}/{repo}/issues/{issue_number}/dependencies/blocked_by")
endpointRemoveBlk = EndpointPattern("DELETE /repos/{owner}/{repo}/issues/{issue_number}/dependencies/blocked_by/{issue_id}")
endpointGetIssue = EndpointPattern("GET /repos/{owner}/{repo}/issues/{issue_number}")
)
// jsonHandler writes the given status code and JSON-encoded body.
func jsonHandler(status int, body any) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(status)
_, _ = w.Write(MustMarshal(body))
}
}
func Test_IssueDependencyRead(t *testing.T) {
// Verify tool definition once (flag-gated variant snap)
serverTool := IssueDependencyRead(translations.NullTranslationHelper)
tool := serverTool.Tool
require.NoError(t, toolsnaps.Test(tool.Name+"_ff_"+FeatureFlagIssueDependencies, tool))
require.Equal(t, FeatureFlagIssueDependencies, serverTool.FeatureFlagEnable)
assert.Equal(t, "issue_dependency_read", tool.Name)
assert.NotEmpty(t, tool.Description)
assert.True(t, tool.Annotations.ReadOnlyHint)
schema := tool.InputSchema.(*jsonschema.Schema)
assert.Contains(t, schema.Properties, "method")
assert.Contains(t, schema.Properties, "owner")
assert.Contains(t, schema.Properties, "repo")
assert.Contains(t, schema.Properties, "issue_number")
assert.Contains(t, schema.Properties, "page")
assert.Contains(t, schema.Properties, "perPage")
assert.ElementsMatch(t, schema.Required, []string{"method", "owner", "repo", "issue_number"})
blockedByIssues := []map[string]any{
{
"number": 7,
"title": "Blocker",
"state": "open",
"html_url": "https://github.com/owner/repo/issues/7",
"repository_url": "https://api.github.com/repos/owner/repo",
},
}
blockingIssues := []map[string]any{
{
"number": 8,
"title": "Blocked A",
"state": "open",
"html_url": "https://github.com/owner/repo/issues/8",
"repository_url": "https://api.github.com/repos/owner/repo",
},
{
"number": 9,
"title": "Blocked B",
"state": "closed",
"html_url": "https://github.com/owner/repo/issues/9",
"repository_url": "https://api.github.com/repos/owner/repo",
},
}
// A handler that also advertises a next page via the Link header.
blockingHandler := func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Link", `<https://api.github.com/repos/owner/repo/issues/123/dependencies/blocking?page=2>; rel="next"`)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(MustMarshal(blockingIssues))
}
tests := []struct {
name string
method string
option MockBackendOption
expectedCount int
expectedFirst int
expectedState string
expectedNext bool
}{
{
name: "get_blocked_by returns blockers",
method: "get_blocked_by",
option: WithRequestMatch(endpointBlockedBy, blockedByIssues),
expectedCount: 1,
expectedFirst: 7,
expectedState: "OPEN",
expectedNext: false,
},
{
name: "get_blocking returns blocked issues",
method: "get_blocking",
option: WithRequestMatchHandler(endpointBlocking, blockingHandler),
expectedCount: 2,
expectedFirst: 8,
expectedState: "OPEN",
expectedNext: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
client := mustNewGHClient(t, NewMockedHTTPClient(tc.option))
deps := BaseDeps{Client: client}
handler := serverTool.Handler(deps)
request := createMCPRequest(map[string]any{
"method": tc.method,
"owner": "owner",
"repo": "repo",
"issue_number": float64(123),
})
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
require.NoError(t, err)
require.False(t, result.IsError, "expected result to not be an error")
text := getTextResult(t, result)
var payload struct {
Issues []MinimalIssueRef `json:"issues"`
PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
NextPage int `json:"nextPage"`
} `json:"pageInfo"`
}
require.NoError(t, json.Unmarshal([]byte(text.Text), &payload))
require.Len(t, payload.Issues, tc.expectedCount)
assert.Equal(t, tc.expectedFirst, payload.Issues[0].Number)
assert.Equal(t, "owner/repo", payload.Issues[0].Repository)
// State is normalized to upper case to match the GraphQL-sourced
// state used by other MinimalIssueRef producers (e.g. get_parent).
assert.Equal(t, tc.expectedState, payload.Issues[0].State)
assert.Equal(t, tc.expectedNext, payload.PageInfo.HasNextPage)
})
}
}
func Test_IssueDependencyRead_Errors(t *testing.T) {
serverTool := IssueDependencyRead(translations.NullTranslationHelper)
t.Run("missing required param", func(t *testing.T) {
client := mustNewGHClient(t, NewMockedHTTPClient())
deps := BaseDeps{Client: client}
handler := serverTool.Handler(deps)
request := createMCPRequest(map[string]any{
"method": "get_blocked_by",
"owner": "owner",
"repo": "repo",
})
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
require.NoError(t, err)
getErrorResult(t, result)
})
t.Run("API error is surfaced", func(t *testing.T) {
client := mustNewGHClient(t, NewMockedHTTPClient(
WithRequestMatchHandler(endpointBlockedBy, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
})),
))
deps := BaseDeps{Client: client}
handler := serverTool.Handler(deps)
request := createMCPRequest(map[string]any{
"method": "get_blocked_by",
"owner": "owner",
"repo": "repo",
"issue_number": float64(123),
})
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
require.NoError(t, err)
getErrorResult(t, result)
})
}
func Test_IssueDependencyWrite(t *testing.T) {
// Verify tool definition once (flag-gated variant snap)
serverTool := IssueDependencyWrite(translations.NullTranslationHelper)
tool := serverTool.Tool
require.NoError(t, toolsnaps.Test(tool.Name+"_ff_"+FeatureFlagIssueDependencies, tool))
require.Equal(t, FeatureFlagIssueDependencies, serverTool.FeatureFlagEnable)
assert.Equal(t, "issue_dependency_write", tool.Name)
assert.NotEmpty(t, tool.Description)
assert.False(t, tool.Annotations.ReadOnlyHint)
schema := tool.InputSchema.(*jsonschema.Schema)
assert.Contains(t, schema.Properties, "method")
assert.Contains(t, schema.Properties, "type")
assert.Contains(t, schema.Properties, "issue_number")
assert.Contains(t, schema.Properties, "related_issue_number")
assert.ElementsMatch(t, schema.Required, []string{"method", "type", "owner", "repo", "issue_number", "related_issue_number"})
// issue returned by the blocking-issue resolve GET; its id is what the
// dependency endpoints operate on.
resolvedIssue := func(number, id int) map[string]any {
return map[string]any{
"id": id,
"number": number,
"title": "Resolved",
"state": "open",
"html_url": "https://github.com/owner/repo/issues/" + strconv.Itoa(number),
"repository_url": "https://api.github.com/repos/owner/repo",
}
}
// issue returned by the add/remove endpoints (the blocked issue).
blockedIssue := func(number int) map[string]any {
return map[string]any{
"number": number,
"title": "Blocked",
"state": "open",
"html_url": "https://github.com/owner/repo/issues/" + strconv.Itoa(number),
"repository_url": "https://api.github.com/repos/owner/repo",
}
}
tests := []struct {
name string
method string
relationship string
options []MockBackendOption
expectedMessage string
expectedBlocked int
expectedBlockng int
}{
{
name: "add blocked_by uses subject as blocked",
method: "add",
relationship: "blocked_by",
// subject(1) is blocked by related(2): resolve related(2), block issue 1.
options: []MockBackendOption{
WithRequestMatch(endpointGetIssue, resolvedIssue(2, 1002)),
WithRequestMatchHandler(endpointAddBlock, jsonHandler(http.StatusCreated, blockedIssue(1))),
},
expectedMessage: "dependency added",
expectedBlocked: 1,
expectedBlockng: 2,
},
{
name: "add blocking swaps roles",
method: "add",
relationship: "blocking",
// subject(1) blocks related(2): resolve subject(1), block issue 2.
options: []MockBackendOption{
WithRequestMatch(endpointGetIssue, resolvedIssue(1, 1001)),
WithRequestMatchHandler(endpointAddBlock, jsonHandler(http.StatusCreated, blockedIssue(2))),
},
expectedMessage: "dependency added",
expectedBlocked: 2,
expectedBlockng: 1,
},
{
name: "remove blocked_by",
method: "remove",
relationship: "blocked_by",
options: []MockBackendOption{
WithRequestMatch(endpointGetIssue, resolvedIssue(2, 1002)),
WithRequestMatch(endpointRemoveBlk, blockedIssue(1)),
},
expectedMessage: "dependency removed",
expectedBlocked: 1,
expectedBlockng: 2,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
client := mustNewGHClient(t, NewMockedHTTPClient(tc.options...))
deps := BaseDeps{Client: client}
handler := serverTool.Handler(deps)
request := createMCPRequest(map[string]any{
"method": tc.method,
"type": tc.relationship,
"owner": "owner",
"repo": "repo",
"issue_number": float64(1),
"related_issue_number": float64(2),
})
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
require.NoError(t, err)
require.False(t, result.IsError, "expected result to not be an error")
text := getTextResult(t, result)
var payload struct {
Message string `json:"message"`
BlockedIssue MinimalIssueRef `json:"blocked_issue"`
BlockingIssue MinimalIssueRef `json:"blocking_issue"`
}
require.NoError(t, json.Unmarshal([]byte(text.Text), &payload))
assert.Equal(t, tc.expectedMessage, payload.Message)
assert.Equal(t, tc.expectedBlocked, payload.BlockedIssue.Number)
assert.Equal(t, tc.expectedBlockng, payload.BlockingIssue.Number)
})
}
t.Run("self dependency fails before any API call", func(t *testing.T) {
// Register no handlers: the handler must return before resolving or mutating.
client := mustNewGHClient(t, NewMockedHTTPClient())
deps := BaseDeps{Client: client}
handler := serverTool.Handler(deps)
request := createMCPRequest(map[string]any{
"method": "add",
"type": "blocked_by",
"owner": "owner",
"repo": "repo",
"issue_number": float64(1),
"related_issue_number": float64(1),
})
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
require.NoError(t, err)
require.True(t, result.IsError, "expected result to be an error")
text := getTextResult(t, result)
assert.Contains(t, text.Text, "itself")
})
}
func Test_IssueDependencyWrite_Validation(t *testing.T) {
serverTool := IssueDependencyWrite(translations.NullTranslationHelper)
cases := []struct {
name string
args map[string]any
}{
{
name: "unknown type",
args: map[string]any{
"method": "add",
"type": "related_to",
"owner": "owner",
"repo": "repo",
"issue_number": float64(1),
"related_issue_number": float64(2),
},
},
{
name: "missing related_issue_number",
args: map[string]any{
"method": "add",
"type": "blocked_by",
"owner": "owner",
"repo": "repo",
"issue_number": float64(1),
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
client := mustNewGHClient(t, NewMockedHTTPClient())
deps := BaseDeps{Client: client}
handler := serverTool.Handler(deps)
request := createMCPRequest(tc.args)
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
require.NoError(t, err)
getErrorResult(t, result)
})
}
}