|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/github/github-mcp-server/internal/toolsnaps" |
| 10 | + "github.com/github/github-mcp-server/pkg/translations" |
| 11 | + gogithub "github.com/google/go-github/v82/github" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_ListRepoSkills(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + serverTool := ListRepoSkills(translations.NullTranslationHelper) |
| 20 | + tool := serverTool.Tool |
| 21 | + require.NoError(t, toolsnaps.Test(tool.Name, tool)) |
| 22 | + |
| 23 | + assert.Equal(t, "list_repo_skills", tool.Name) |
| 24 | + assert.NotEmpty(t, tool.Description) |
| 25 | + assert.True(t, tool.Annotations.ReadOnlyHint, "list_repo_skills must be read-only") |
| 26 | + |
| 27 | + treeMock := func(entries ...*gogithub.TreeEntry) http.HandlerFunc { |
| 28 | + return func(w http.ResponseWriter, _ *http.Request) { |
| 29 | + data, _ := json.Marshal(&gogithub.Tree{Entries: entries}) |
| 30 | + w.Header().Set("Content-Type", "application/json") |
| 31 | + _, _ = w.Write(data) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + args map[string]any |
| 38 | + handlers map[string]http.HandlerFunc |
| 39 | + expectToolError bool |
| 40 | + expectErrText string |
| 41 | + expectSkills []string // names; URLs are checked structurally |
| 42 | + }{ |
| 43 | + { |
| 44 | + name: "missing owner", |
| 45 | + args: map[string]any{"repo": "hello-world"}, |
| 46 | + handlers: map[string]http.HandlerFunc{ |
| 47 | + GetReposGitTreesByOwnerByRepoByTree: treeMock(), |
| 48 | + }, |
| 49 | + expectToolError: true, |
| 50 | + expectErrText: "owner", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "missing repo", |
| 54 | + args: map[string]any{"owner": "octocat"}, |
| 55 | + handlers: map[string]http.HandlerFunc{ |
| 56 | + GetReposGitTreesByOwnerByRepoByTree: treeMock(), |
| 57 | + }, |
| 58 | + expectToolError: true, |
| 59 | + expectErrText: "repo", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "empty repo returns no skills", |
| 63 | + args: map[string]any{"owner": "octocat", "repo": "hello-world"}, |
| 64 | + handlers: map[string]http.HandlerFunc{ |
| 65 | + GetReposGitTreesByOwnerByRepoByTree: treeMock( |
| 66 | + &gogithub.TreeEntry{Path: gogithub.Ptr("README.md"), Type: gogithub.Ptr("blob")}, |
| 67 | + ), |
| 68 | + }, |
| 69 | + expectSkills: []string{}, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "discovers across all four conventions", |
| 73 | + args: map[string]any{"owner": "octocat", "repo": "hello-world"}, |
| 74 | + handlers: map[string]http.HandlerFunc{ |
| 75 | + GetReposGitTreesByOwnerByRepoByTree: treeMock( |
| 76 | + &gogithub.TreeEntry{Path: gogithub.Ptr("skills/code-review/SKILL.md"), Type: gogithub.Ptr("blob")}, |
| 77 | + &gogithub.TreeEntry{Path: gogithub.Ptr("skills/acme/data-tool/SKILL.md"), Type: gogithub.Ptr("blob")}, |
| 78 | + &gogithub.TreeEntry{Path: gogithub.Ptr("plugins/my-plugin/skills/lint/SKILL.md"), Type: gogithub.Ptr("blob")}, |
| 79 | + &gogithub.TreeEntry{Path: gogithub.Ptr("root-level-skill/SKILL.md"), Type: gogithub.Ptr("blob")}, |
| 80 | + ), |
| 81 | + }, |
| 82 | + expectSkills: []string{"code-review", "data-tool", "lint", "root-level-skill"}, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tc := range tests { |
| 87 | + t.Run(tc.name, func(t *testing.T) { |
| 88 | + client := gogithub.NewClient(MockHTTPClientWithHandlers(tc.handlers)) |
| 89 | + deps := BaseDeps{Client: client} |
| 90 | + handler := serverTool.Handler(deps) |
| 91 | + |
| 92 | + request := createMCPRequest(tc.args) |
| 93 | + result, err := handler(ContextWithDeps(context.Background(), deps), &request) |
| 94 | + require.NoError(t, err) |
| 95 | + require.NotNil(t, result) |
| 96 | + |
| 97 | + if tc.expectToolError { |
| 98 | + assert.True(t, result.IsError, "expected tool error result") |
| 99 | + if tc.expectErrText != "" { |
| 100 | + textContent := getErrorResult(t, result) |
| 101 | + assert.Contains(t, textContent.Text, tc.expectErrText) |
| 102 | + } |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + assert.False(t, result.IsError, "unexpected tool error: %+v", result) |
| 107 | + |
| 108 | + textContent := getTextResult(t, result) |
| 109 | + var payload struct { |
| 110 | + Owner string `json:"owner"` |
| 111 | + Repo string `json:"repo"` |
| 112 | + Skills []struct { |
| 113 | + Name string `json:"name"` |
| 114 | + URL string `json:"url"` |
| 115 | + } `json:"skills"` |
| 116 | + TotalCount int `json:"totalCount"` |
| 117 | + } |
| 118 | + require.NoError(t, json.Unmarshal([]byte(textContent.Text), &payload)) |
| 119 | + |
| 120 | + assert.Equal(t, tc.args["owner"], payload.Owner) |
| 121 | + assert.Equal(t, tc.args["repo"], payload.Repo) |
| 122 | + assert.Equal(t, len(tc.expectSkills), payload.TotalCount) |
| 123 | + require.Len(t, payload.Skills, len(tc.expectSkills)) |
| 124 | + |
| 125 | + gotNames := make([]string, 0, len(payload.Skills)) |
| 126 | + for _, s := range payload.Skills { |
| 127 | + gotNames = append(gotNames, s.Name) |
| 128 | + // Each URL must match the canonical SkillFileURI shape so the |
| 129 | + // model can pass it straight to resources/read. |
| 130 | + expectedURL := SkillFileURI(payload.Owner, payload.Repo, s.Name, "SKILL.md") |
| 131 | + assert.Equal(t, expectedURL, s.URL, "URL must match SkillFileURI(owner, repo, name, SKILL.md)") |
| 132 | + } |
| 133 | + assert.ElementsMatch(t, tc.expectSkills, gotNames) |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
0 commit comments