|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/google/go-github/v69/github" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/migueleliasweb/go-github-mock/src/mock" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_RepoContentsResourceHandler(t *testing.T) { |
| 17 | + mockDirContent := []*github.RepositoryContent{ |
| 18 | + { |
| 19 | + Type: github.Ptr("file"), |
| 20 | + Name: github.Ptr("README.md"), |
| 21 | + Path: github.Ptr("README.md"), |
| 22 | + SHA: github.Ptr("abc123"), |
| 23 | + Size: github.Ptr(42), |
| 24 | + HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/README.md"), |
| 25 | + }, |
| 26 | + { |
| 27 | + Type: github.Ptr("dir"), |
| 28 | + Name: github.Ptr("src"), |
| 29 | + Path: github.Ptr("src"), |
| 30 | + SHA: github.Ptr("def456"), |
| 31 | + HTMLURL: github.Ptr("https://github.com/owner/repo/tree/main/src"), |
| 32 | + }, |
| 33 | + } |
| 34 | + expectedDirContent := []mcp.TextResourceContents{ |
| 35 | + { |
| 36 | + URI: "https://github.com/owner/repo/blob/main/README.md", |
| 37 | + MIMEType: "", |
| 38 | + Text: "README.md", |
| 39 | + }, |
| 40 | + { |
| 41 | + URI: "https://github.com/owner/repo/tree/main/src", |
| 42 | + MIMEType: "text/directory", |
| 43 | + Text: "src", |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + mockFileContent := &github.RepositoryContent{ |
| 48 | + Type: github.Ptr("file"), |
| 49 | + Name: github.Ptr("README.md"), |
| 50 | + Path: github.Ptr("README.md"), |
| 51 | + Content: github.Ptr("IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku"), // Base64 encoded "# Test Repository\n\nThis is a test repository." |
| 52 | + SHA: github.Ptr("abc123"), |
| 53 | + Size: github.Ptr(42), |
| 54 | + HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/README.md"), |
| 55 | + DownloadURL: github.Ptr("https://raw.githubusercontent.com/owner/repo/main/README.md"), |
| 56 | + } |
| 57 | + |
| 58 | + expectedFileContent := []mcp.BlobResourceContents{ |
| 59 | + { |
| 60 | + Blob: base64.StdEncoding.EncodeToString([]byte("IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku")), // Base64 encoded "# Test Repository\n\nThis is a test repository." |
| 61 | + |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + tests := []struct { |
| 66 | + name string |
| 67 | + mockedClient *http.Client |
| 68 | + requestArgs map[string]any |
| 69 | + expectError bool |
| 70 | + expectedResult any |
| 71 | + expectedErrMsg string |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "successful file content fetch", |
| 75 | + mockedClient: mock.NewMockedHTTPClient( |
| 76 | + mock.WithRequestMatch( |
| 77 | + mock.GetReposContentsByOwnerByRepoByPath, |
| 78 | + mockFileContent, |
| 79 | + ), |
| 80 | + ), |
| 81 | + requestArgs: map[string]any{ |
| 82 | + "owner": []string{"owner"}, |
| 83 | + "repo": []string{"repo"}, |
| 84 | + "path": []string{"README.md"}, |
| 85 | + "branch": []string{"main"}, |
| 86 | + }, |
| 87 | + expectError: false, |
| 88 | + expectedResult: expectedFileContent, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "successful directory content fetch", |
| 92 | + mockedClient: mock.NewMockedHTTPClient( |
| 93 | + mock.WithRequestMatch( |
| 94 | + mock.GetReposContentsByOwnerByRepoByPath, |
| 95 | + mockDirContent, |
| 96 | + ), |
| 97 | + ), |
| 98 | + requestArgs: map[string]any{ |
| 99 | + "owner": []string{"owner"}, |
| 100 | + "repo": []string{"repo"}, |
| 101 | + "path": []string{"src"}, |
| 102 | + }, |
| 103 | + expectError: false, |
| 104 | + expectedResult: expectedDirContent, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "empty content fetch", |
| 108 | + mockedClient: mock.NewMockedHTTPClient( |
| 109 | + mock.WithRequestMatch( |
| 110 | + mock.GetReposContentsByOwnerByRepoByPath, |
| 111 | + []*github.RepositoryContent{}, |
| 112 | + ), |
| 113 | + ), |
| 114 | + requestArgs: map[string]any{ |
| 115 | + "owner": []string{"owner"}, |
| 116 | + "repo": []string{"repo"}, |
| 117 | + "path": []string{"src"}, |
| 118 | + }, |
| 119 | + expectError: false, |
| 120 | + expectedResult: nil, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "content fetch fails", |
| 124 | + mockedClient: mock.NewMockedHTTPClient( |
| 125 | + mock.WithRequestMatchHandler( |
| 126 | + mock.GetReposContentsByOwnerByRepoByPath, |
| 127 | + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 128 | + w.WriteHeader(http.StatusNotFound) |
| 129 | + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) |
| 130 | + }), |
| 131 | + ), |
| 132 | + ), |
| 133 | + requestArgs: map[string]any{ |
| 134 | + "owner": []string{"owner"}, |
| 135 | + "repo": []string{"repo"}, |
| 136 | + "path": []string{"nonexistent.md"}, |
| 137 | + "branch": []string{"main"}, |
| 138 | + }, |
| 139 | + expectError: true, |
| 140 | + expectedErrMsg: "404 Not Found", |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + for _, tc := range tests { |
| 145 | + t.Run(tc.name, func(t *testing.T) { |
| 146 | + // Setup client with mock |
| 147 | + client := github.NewClient(tc.mockedClient) |
| 148 | + handler := repoContentsResourceHandler(client) |
| 149 | + |
| 150 | + // Create call request |
| 151 | + request := mcp.ReadResourceRequest{ |
| 152 | + Params: struct { |
| 153 | + URI string `json:"uri"` |
| 154 | + Arguments map[string]any `json:"arguments,omitempty"` |
| 155 | + }{ |
| 156 | + Arguments: tc.requestArgs, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + resp, err := handler(context.TODO(), request) |
| 161 | + |
| 162 | + if tc.expectError { |
| 163 | + require.Error(t, err) |
| 164 | + assert.Contains(t, err.Error(), tc.expectedErrMsg) |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + require.NoError(t, err) |
| 169 | + require.ElementsMatch(t, resp, tc.expectedResult) |
| 170 | + }) |
| 171 | + } |
| 172 | +} |
0 commit comments