forked from github/github-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiscussions_test.go
More file actions
698 lines (647 loc) · 25.4 KB
/
discussions_test.go
File metadata and controls
698 lines (647 loc) · 25.4 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
package github
import (
"context"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/github/github-mcp-server/internal/githubv4mock"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/google/go-github/v73/github"
"github.com/shurcooL/githubv4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
discussionsGeneral = []map[string]any{
{"number": 1, "title": "Discussion 1 title", "createdAt": "2023-01-01T00:00:00Z", "updatedAt": "2023-01-01T00:00:00Z", "author": map[string]any{"login": "user1"}, "url": "https://github.com/owner/repo/discussions/1", "category": map[string]any{"name": "General"}},
{"number": 3, "title": "Discussion 3 title", "createdAt": "2023-03-01T00:00:00Z", "updatedAt": "2023-02-01T00:00:00Z", "author": map[string]any{"login": "user1"}, "url": "https://github.com/owner/repo/discussions/3", "category": map[string]any{"name": "General"}},
}
discussionsAll = []map[string]any{
{
"number": 1,
"title": "Discussion 1 title",
"createdAt": "2023-01-01T00:00:00Z",
"updatedAt": "2023-01-01T00:00:00Z",
"author": map[string]any{"login": "user1"},
"url": "https://github.com/owner/repo/discussions/1",
"category": map[string]any{"name": "General"},
},
{
"number": 2,
"title": "Discussion 2 title",
"createdAt": "2023-02-01T00:00:00Z",
"updatedAt": "2023-02-01T00:00:00Z",
"author": map[string]any{"login": "user2"},
"url": "https://github.com/owner/repo/discussions/2",
"category": map[string]any{"name": "Questions"},
},
{
"number": 3,
"title": "Discussion 3 title",
"createdAt": "2023-03-01T00:00:00Z",
"updatedAt": "2023-03-01T00:00:00Z",
"author": map[string]any{"login": "user3"},
"url": "https://github.com/owner/repo/discussions/3",
"category": map[string]any{"name": "General"},
},
}
discussionsOrgLevel = []map[string]any{
{
"number": 1,
"title": "Org Discussion 1 - Community Guidelines",
"createdAt": "2023-01-15T00:00:00Z",
"updatedAt": "2023-01-15T00:00:00Z",
"author": map[string]any{"login": "org-admin"},
"url": "https://github.com/owner/.github/discussions/1",
"category": map[string]any{"name": "Announcements"},
},
{
"number": 2,
"title": "Org Discussion 2 - Roadmap 2023",
"createdAt": "2023-02-20T00:00:00Z",
"updatedAt": "2023-02-20T00:00:00Z",
"author": map[string]any{"login": "org-admin"},
"url": "https://github.com/owner/.github/discussions/2",
"category": map[string]any{"name": "General"},
},
{
"number": 3,
"title": "Org Discussion 3 - Roadmap 2024",
"createdAt": "2023-02-20T00:00:00Z",
"updatedAt": "2023-02-20T00:00:00Z",
"author": map[string]any{"login": "org-admin"},
"url": "https://github.com/owner/.github/discussions/3",
"category": map[string]any{"name": "General"},
},
{
"number": 4,
"title": "Org Discussion 4 - Roadmap 2025",
"createdAt": "2023-02-20T00:00:00Z",
"updatedAt": "2023-02-20T00:00:00Z",
"author": map[string]any{"login": "org-admin"},
"url": "https://github.com/owner/.github/discussions/4",
"category": map[string]any{"name": "General"},
},
}
// Ordered mock responses
discussionsOrderedCreatedAsc = []map[string]any{
discussionsAll[0], // Discussion 1 (created 2023-01-01)
discussionsAll[1], // Discussion 2 (created 2023-02-01)
discussionsAll[2], // Discussion 3 (created 2023-03-01)
}
discussionsOrderedUpdatedDesc = []map[string]any{
discussionsAll[2], // Discussion 3 (updated 2023-03-01)
discussionsAll[1], // Discussion 2 (updated 2023-02-01)
discussionsAll[0], // Discussion 1 (updated 2023-01-01)
}
// only 'General' category discussions ordered by created date descending
discussionsGeneralOrderedDesc = []map[string]any{
discussionsGeneral[1], // Discussion 3 (created 2023-03-01)
discussionsGeneral[0], // Discussion 1 (created 2023-01-01)
}
mockResponseListAll = githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"discussions": map[string]any{
"nodes": discussionsAll,
"pageInfo": map[string]any{
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "",
"endCursor": "",
},
"totalCount": 3,
},
},
})
mockResponseListGeneral = githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"discussions": map[string]any{
"nodes": discussionsGeneral,
"pageInfo": map[string]any{
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "",
"endCursor": "",
},
"totalCount": 2,
},
},
})
mockResponseOrderedCreatedAsc = githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"discussions": map[string]any{
"nodes": discussionsOrderedCreatedAsc,
"pageInfo": map[string]any{
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "",
"endCursor": "",
},
"totalCount": 3,
},
},
})
mockResponseOrderedUpdatedDesc = githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"discussions": map[string]any{
"nodes": discussionsOrderedUpdatedDesc,
"pageInfo": map[string]any{
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "",
"endCursor": "",
},
"totalCount": 3,
},
},
})
mockResponseGeneralOrderedDesc = githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"discussions": map[string]any{
"nodes": discussionsGeneralOrderedDesc,
"pageInfo": map[string]any{
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "",
"endCursor": "",
},
"totalCount": 2,
},
},
})
mockResponseOrgLevel = githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"discussions": map[string]any{
"nodes": discussionsOrgLevel,
"pageInfo": map[string]any{
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "",
"endCursor": "",
},
"totalCount": 4,
},
},
})
mockErrorRepoNotFound = githubv4mock.ErrorResponse("repository not found")
)
func Test_ListDiscussions(t *testing.T) {
mockClient := githubv4.NewClient(nil)
toolDef, _ := ListDiscussions(stubGetGQLClientFn(mockClient), translations.NullTranslationHelper)
assert.Equal(t, "list_discussions", toolDef.Name)
assert.NotEmpty(t, toolDef.Description)
assert.Contains(t, toolDef.InputSchema.Properties, "owner")
assert.Contains(t, toolDef.InputSchema.Properties, "repo")
assert.Contains(t, toolDef.InputSchema.Properties, "orderBy")
assert.Contains(t, toolDef.InputSchema.Properties, "direction")
assert.ElementsMatch(t, toolDef.InputSchema.Required, []string{"owner"})
// Variables matching what GraphQL receives after JSON marshaling/unmarshaling
varsListAll := map[string]interface{}{
"owner": "owner",
"repo": "repo",
"first": float64(30),
"after": (*string)(nil),
}
varsRepoNotFound := map[string]interface{}{
"owner": "owner",
"repo": "nonexistent-repo",
"first": float64(30),
"after": (*string)(nil),
}
varsDiscussionsFiltered := map[string]interface{}{
"owner": "owner",
"repo": "repo",
"categoryId": "DIC_kwDOABC123",
"first": float64(30),
"after": (*string)(nil),
}
varsOrderByCreatedAsc := map[string]interface{}{
"owner": "owner",
"repo": "repo",
"orderByField": "CREATED_AT",
"orderByDirection": "ASC",
"first": float64(30),
"after": (*string)(nil),
}
varsOrderByUpdatedDesc := map[string]interface{}{
"owner": "owner",
"repo": "repo",
"orderByField": "UPDATED_AT",
"orderByDirection": "DESC",
"first": float64(30),
"after": (*string)(nil),
}
varsCategoryWithOrder := map[string]interface{}{
"owner": "owner",
"repo": "repo",
"categoryId": "DIC_kwDOABC123",
"orderByField": "CREATED_AT",
"orderByDirection": "DESC",
"first": float64(30),
"after": (*string)(nil),
}
varsOrgLevel := map[string]interface{}{
"owner": "owner",
"repo": ".github", // This is what gets set when repo is not provided
"first": float64(30),
"after": (*string)(nil),
}
tests := []struct {
name string
reqParams map[string]interface{}
expectError bool
errContains string
expectedCount int
verifyOrder func(t *testing.T, discussions []*github.Discussion)
}{
{
name: "list all discussions without category filter",
reqParams: map[string]interface{}{
"owner": "owner",
"repo": "repo",
},
expectError: false,
expectedCount: 3, // All discussions
},
{
name: "filter by category ID",
reqParams: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"category": "DIC_kwDOABC123",
},
expectError: false,
expectedCount: 2, // Only General discussions (matching the category ID)
},
{
name: "order by created at ascending",
reqParams: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"orderBy": "CREATED_AT",
"direction": "ASC",
},
expectError: false,
expectedCount: 3,
verifyOrder: func(t *testing.T, discussions []*github.Discussion) {
// Verify discussions are ordered by created date ascending
require.Len(t, discussions, 3)
assert.Equal(t, 1, *discussions[0].Number, "First should be discussion 1 (created 2023-01-01)")
assert.Equal(t, 2, *discussions[1].Number, "Second should be discussion 2 (created 2023-02-01)")
assert.Equal(t, 3, *discussions[2].Number, "Third should be discussion 3 (created 2023-03-01)")
},
},
{
name: "order by updated at descending",
reqParams: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"orderBy": "UPDATED_AT",
"direction": "DESC",
},
expectError: false,
expectedCount: 3,
verifyOrder: func(t *testing.T, discussions []*github.Discussion) {
// Verify discussions are ordered by updated date descending
require.Len(t, discussions, 3)
assert.Equal(t, 3, *discussions[0].Number, "First should be discussion 3 (updated 2023-03-01)")
assert.Equal(t, 2, *discussions[1].Number, "Second should be discussion 2 (updated 2023-02-01)")
assert.Equal(t, 1, *discussions[2].Number, "Third should be discussion 1 (updated 2023-01-01)")
},
},
{
name: "filter by category with order",
reqParams: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"category": "DIC_kwDOABC123",
"orderBy": "CREATED_AT",
"direction": "DESC",
},
expectError: false,
expectedCount: 2,
verifyOrder: func(t *testing.T, discussions []*github.Discussion) {
// Verify only General discussions, ordered by created date descending
require.Len(t, discussions, 2)
assert.Equal(t, 3, *discussions[0].Number, "First should be discussion 3 (created 2023-03-01)")
assert.Equal(t, 1, *discussions[1].Number, "Second should be discussion 1 (created 2023-01-01)")
},
},
{
name: "order by without direction (should not use ordering)",
reqParams: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"orderBy": "CREATED_AT",
},
expectError: false,
expectedCount: 3,
},
{
name: "direction without order by (should not use ordering)",
reqParams: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"direction": "DESC",
},
expectError: false,
expectedCount: 3,
},
{
name: "repository not found error",
reqParams: map[string]interface{}{
"owner": "owner",
"repo": "nonexistent-repo",
},
expectError: true,
errContains: "repository not found",
},
{
name: "list org-level discussions (no repo provided)",
reqParams: map[string]interface{}{
"owner": "owner",
// repo is not provided, it will default to ".github"
},
expectError: false,
expectedCount: 4,
},
}
// Define the actual query strings that match the implementation
qBasicNoOrder := "query($after:String$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussions(first: $first, after: $after){nodes{number,title,createdAt,updatedAt,author{login},category{name},url},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount}}}"
qWithCategoryNoOrder := "query($after:String$categoryId:ID!$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussions(first: $first, after: $after, categoryId: $categoryId){nodes{number,title,createdAt,updatedAt,author{login},category{name},url},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount}}}"
qBasicWithOrder := "query($after:String$first:Int!$orderByDirection:OrderDirection!$orderByField:DiscussionOrderField!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussions(first: $first, after: $after, orderBy: { field: $orderByField, direction: $orderByDirection }){nodes{number,title,createdAt,updatedAt,author{login},category{name},url},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount}}}"
qWithCategoryAndOrder := "query($after:String$categoryId:ID!$first:Int!$orderByDirection:OrderDirection!$orderByField:DiscussionOrderField!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussions(first: $first, after: $after, categoryId: $categoryId, orderBy: { field: $orderByField, direction: $orderByDirection }){nodes{number,title,createdAt,updatedAt,author{login},category{name},url},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount}}}"
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var httpClient *http.Client
switch tc.name {
case "list all discussions without category filter":
matcher := githubv4mock.NewQueryMatcher(qBasicNoOrder, varsListAll, mockResponseListAll)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
case "filter by category ID":
matcher := githubv4mock.NewQueryMatcher(qWithCategoryNoOrder, varsDiscussionsFiltered, mockResponseListGeneral)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
case "order by created at ascending":
matcher := githubv4mock.NewQueryMatcher(qBasicWithOrder, varsOrderByCreatedAsc, mockResponseOrderedCreatedAsc)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
case "order by updated at descending":
matcher := githubv4mock.NewQueryMatcher(qBasicWithOrder, varsOrderByUpdatedDesc, mockResponseOrderedUpdatedDesc)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
case "filter by category with order":
matcher := githubv4mock.NewQueryMatcher(qWithCategoryAndOrder, varsCategoryWithOrder, mockResponseGeneralOrderedDesc)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
case "order by without direction (should not use ordering)":
matcher := githubv4mock.NewQueryMatcher(qBasicNoOrder, varsListAll, mockResponseListAll)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
case "direction without order by (should not use ordering)":
matcher := githubv4mock.NewQueryMatcher(qBasicNoOrder, varsListAll, mockResponseListAll)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
case "repository not found error":
matcher := githubv4mock.NewQueryMatcher(qBasicNoOrder, varsRepoNotFound, mockErrorRepoNotFound)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
case "list org-level discussions (no repo provided)":
matcher := githubv4mock.NewQueryMatcher(qBasicNoOrder, varsOrgLevel, mockResponseOrgLevel)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
}
gqlClient := githubv4.NewClient(httpClient)
_, handler := ListDiscussions(stubGetGQLClientFn(gqlClient), translations.NullTranslationHelper)
req := createMCPRequest(tc.reqParams)
res, err := handler(context.Background(), req)
text := getTextResult(t, res).Text
if tc.expectError {
require.True(t, res.IsError)
assert.Contains(t, text, tc.errContains)
return
}
require.NoError(t, err)
// Parse the structured response with pagination info
var response struct {
Discussions []*github.Discussion `json:"discussions"`
PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
HasPreviousPage bool `json:"hasPreviousPage"`
StartCursor string `json:"startCursor"`
EndCursor string `json:"endCursor"`
} `json:"pageInfo"`
TotalCount int `json:"totalCount"`
}
err = json.Unmarshal([]byte(text), &response)
require.NoError(t, err)
assert.Len(t, response.Discussions, tc.expectedCount, "Expected %d discussions, got %d", tc.expectedCount, len(response.Discussions))
// Verify order if verifyOrder function is provided
if tc.verifyOrder != nil {
tc.verifyOrder(t, response.Discussions)
}
// Verify that all returned discussions have a category if filtered
if _, hasCategory := tc.reqParams["category"]; hasCategory {
for _, discussion := range response.Discussions {
require.NotNil(t, discussion.DiscussionCategory, "Discussion should have category")
assert.NotEmpty(t, *discussion.DiscussionCategory.Name, "Discussion should have category name")
}
}
})
}
}
func Test_GetDiscussion(t *testing.T) {
// Verify tool definition and schema
toolDef, _ := GetDiscussion(nil, translations.NullTranslationHelper)
assert.Equal(t, "get_discussion", toolDef.Name)
assert.NotEmpty(t, toolDef.Description)
assert.Contains(t, toolDef.InputSchema.Properties, "owner")
assert.Contains(t, toolDef.InputSchema.Properties, "repo")
assert.Contains(t, toolDef.InputSchema.Properties, "discussionNumber")
assert.ElementsMatch(t, toolDef.InputSchema.Required, []string{"owner", "repo", "discussionNumber"})
// Use exact string query that matches implementation output
qGetDiscussion := "query($discussionNumber:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussion(number: $discussionNumber){number,body,createdAt,url,category{name}}}}"
vars := map[string]interface{}{
"owner": "owner",
"repo": "repo",
"discussionNumber": float64(1),
}
tests := []struct {
name string
response githubv4mock.GQLResponse
expectError bool
expected *github.Discussion
errContains string
}{
{
name: "successful retrieval",
response: githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{"discussion": map[string]any{
"number": 1,
"body": "This is a test discussion",
"url": "https://github.com/owner/repo/discussions/1",
"createdAt": "2025-04-25T12:00:00Z",
"category": map[string]any{"name": "General"},
}},
}),
expectError: false,
expected: &github.Discussion{
HTMLURL: github.Ptr("https://github.com/owner/repo/discussions/1"),
Number: github.Ptr(1),
Body: github.Ptr("This is a test discussion"),
CreatedAt: &github.Timestamp{Time: time.Date(2025, 4, 25, 12, 0, 0, 0, time.UTC)},
DiscussionCategory: &github.DiscussionCategory{
Name: github.Ptr("General"),
},
},
},
{
name: "discussion not found",
response: githubv4mock.ErrorResponse("discussion not found"),
expectError: true,
errContains: "discussion not found",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
matcher := githubv4mock.NewQueryMatcher(qGetDiscussion, vars, tc.response)
httpClient := githubv4mock.NewMockedHTTPClient(matcher)
gqlClient := githubv4.NewClient(httpClient)
_, handler := GetDiscussion(stubGetGQLClientFn(gqlClient), translations.NullTranslationHelper)
req := createMCPRequest(map[string]interface{}{"owner": "owner", "repo": "repo", "discussionNumber": int32(1)})
res, err := handler(context.Background(), req)
text := getTextResult(t, res).Text
if tc.expectError {
require.True(t, res.IsError)
assert.Contains(t, text, tc.errContains)
return
}
require.NoError(t, err)
var out github.Discussion
require.NoError(t, json.Unmarshal([]byte(text), &out))
assert.Equal(t, *tc.expected.HTMLURL, *out.HTMLURL)
assert.Equal(t, *tc.expected.Number, *out.Number)
assert.Equal(t, *tc.expected.Body, *out.Body)
// Check category label
assert.Equal(t, *tc.expected.DiscussionCategory.Name, *out.DiscussionCategory.Name)
})
}
}
func Test_GetDiscussionComments(t *testing.T) {
// Verify tool definition and schema
toolDef, _ := GetDiscussionComments(nil, translations.NullTranslationHelper)
assert.Equal(t, "get_discussion_comments", toolDef.Name)
assert.NotEmpty(t, toolDef.Description)
assert.Contains(t, toolDef.InputSchema.Properties, "owner")
assert.Contains(t, toolDef.InputSchema.Properties, "repo")
assert.Contains(t, toolDef.InputSchema.Properties, "discussionNumber")
assert.ElementsMatch(t, toolDef.InputSchema.Required, []string{"owner", "repo", "discussionNumber"})
// Use exact string query that matches implementation output
qGetComments := "query($after:String$discussionNumber:Int!$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussion(number: $discussionNumber){comments(first: $first, after: $after){nodes{body},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount}}}}"
// Variables matching what GraphQL receives after JSON marshaling/unmarshaling
vars := map[string]interface{}{
"owner": "owner",
"repo": "repo",
"discussionNumber": float64(1),
"first": float64(30),
"after": (*string)(nil),
}
mockResponse := githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"discussion": map[string]any{
"comments": map[string]any{
"nodes": []map[string]any{
{"body": "This is the first comment"},
{"body": "This is the second comment"},
},
"pageInfo": map[string]any{
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "",
"endCursor": "",
},
"totalCount": 2,
},
},
},
})
matcher := githubv4mock.NewQueryMatcher(qGetComments, vars, mockResponse)
httpClient := githubv4mock.NewMockedHTTPClient(matcher)
gqlClient := githubv4.NewClient(httpClient)
_, handler := GetDiscussionComments(stubGetGQLClientFn(gqlClient), translations.NullTranslationHelper)
request := createMCPRequest(map[string]interface{}{
"owner": "owner",
"repo": "repo",
"discussionNumber": int32(1),
})
result, err := handler(context.Background(), request)
require.NoError(t, err)
textContent := getTextResult(t, result)
// (Lines removed)
var response struct {
Comments []*github.IssueComment `json:"comments"`
PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
HasPreviousPage bool `json:"hasPreviousPage"`
StartCursor string `json:"startCursor"`
EndCursor string `json:"endCursor"`
} `json:"pageInfo"`
TotalCount int `json:"totalCount"`
}
err = json.Unmarshal([]byte(textContent.Text), &response)
require.NoError(t, err)
assert.Len(t, response.Comments, 2)
expectedBodies := []string{"This is the first comment", "This is the second comment"}
for i, comment := range response.Comments {
assert.Equal(t, expectedBodies[i], *comment.Body)
}
}
func Test_ListDiscussionCategories(t *testing.T) {
// Use exact string query that matches implementation output
qListCategories := "query($first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussionCategories(first: $first){nodes{id,name},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount}}}"
// Variables matching what GraphQL receives after JSON marshaling/unmarshaling
vars := map[string]interface{}{
"owner": "owner",
"repo": "repo",
"first": float64(25),
}
mockResp := githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"discussionCategories": map[string]any{
"nodes": []map[string]any{
{"id": "123", "name": "CategoryOne"},
{"id": "456", "name": "CategoryTwo"},
},
"pageInfo": map[string]any{
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "",
"endCursor": "",
},
"totalCount": 2,
},
},
})
matcher := githubv4mock.NewQueryMatcher(qListCategories, vars, mockResp)
httpClient := githubv4mock.NewMockedHTTPClient(matcher)
gqlClient := githubv4.NewClient(httpClient)
tool, handler := ListDiscussionCategories(stubGetGQLClientFn(gqlClient), translations.NullTranslationHelper)
assert.Equal(t, "list_discussion_categories", tool.Name)
assert.NotEmpty(t, tool.Description)
assert.Contains(t, tool.InputSchema.Properties, "owner")
assert.Contains(t, tool.InputSchema.Properties, "repo")
assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"})
request := createMCPRequest(map[string]interface{}{"owner": "owner", "repo": "repo"})
result, err := handler(context.Background(), request)
require.NoError(t, err)
text := getTextResult(t, result).Text
var response struct {
Categories []map[string]string `json:"categories"`
PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
HasPreviousPage bool `json:"hasPreviousPage"`
StartCursor string `json:"startCursor"`
EndCursor string `json:"endCursor"`
} `json:"pageInfo"`
TotalCount int `json:"totalCount"`
}
require.NoError(t, json.Unmarshal([]byte(text), &response))
assert.Len(t, response.Categories, 2)
assert.Equal(t, "123", response.Categories[0]["id"])
assert.Equal(t, "CategoryOne", response.Categories[0]["name"])
assert.Equal(t, "456", response.Categories[1]["id"])
assert.Equal(t, "CategoryTwo", response.Categories[1]["name"])
}