-
Notifications
You must be signed in to change notification settings - Fork 1.9k
repository resource tests #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
383c2d2
1cb52f9
2aa3002
d8b0056
2fdda7c
9680b24
b4e6772
db7a180
02ebdc7
ad58220
ede9f22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/google/go-github/v69/github" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/migueleliasweb/go-github-mock/src/mock" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_RepoContentsResourceHandler(t *testing.T) { | ||
mockDirContent := []*github.RepositoryContent{ | ||
{ | ||
Type: github.Ptr("file"), | ||
Name: github.Ptr("README.md"), | ||
Path: github.Ptr("README.md"), | ||
SHA: github.Ptr("abc123"), | ||
Size: github.Ptr(42), | ||
HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/README.md"), | ||
}, | ||
{ | ||
Type: github.Ptr("dir"), | ||
Name: github.Ptr("src"), | ||
Path: github.Ptr("src"), | ||
SHA: github.Ptr("def456"), | ||
HTMLURL: github.Ptr("https://github.com/owner/repo/tree/main/src"), | ||
}, | ||
} | ||
expectedDirContent := []mcp.TextResourceContents{ | ||
{ | ||
URI: "https://github.com/owner/repo/blob/main/README.md", | ||
MIMEType: "", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. feels like this shouldn't be empty, but that's what the current code returns 🤷 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, at very least I suppose we could special case markdown. I used the stdlib mime type lib, but it's perhaps not for for purpose. |
||
Text: "README.md", | ||
}, | ||
{ | ||
URI: "https://github.com/owner/repo/tree/main/src", | ||
MIMEType: "text/directory", | ||
Text: "src", | ||
}, | ||
} | ||
|
||
mockFileContent := &github.RepositoryContent{ | ||
Type: github.Ptr("file"), | ||
Name: github.Ptr("README.md"), | ||
Path: github.Ptr("README.md"), | ||
Content: github.Ptr("IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku"), // Base64 encoded "# Test Repository\n\nThis is a test repository." | ||
SHA: github.Ptr("abc123"), | ||
Size: github.Ptr(42), | ||
HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/README.md"), | ||
DownloadURL: github.Ptr("https://raw.githubusercontent.com/owner/repo/main/README.md"), | ||
} | ||
|
||
expectedFileContent := []mcp.BlobResourceContents{ | ||
{ | ||
Blob: base64.StdEncoding.EncodeToString([]byte("IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku")), // Base64 encoded "# Test Repository\n\nThis is a test repository." | ||
|
||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
mockedClient *http.Client | ||
requestArgs map[string]any | ||
expectError bool | ||
expectedResult any | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
name: "successful file content fetch", | ||
mockedClient: mock.NewMockedHTTPClient( | ||
mock.WithRequestMatch( | ||
mock.GetReposContentsByOwnerByRepoByPath, | ||
mockFileContent, | ||
), | ||
), | ||
requestArgs: map[string]any{ | ||
"owner": []string{"owner"}, | ||
"repo": []string{"repo"}, | ||
"path": []string{"README.md"}, | ||
"branch": []string{"main"}, | ||
}, | ||
expectError: false, | ||
expectedResult: expectedFileContent, | ||
}, | ||
{ | ||
name: "successful directory content fetch", | ||
mockedClient: mock.NewMockedHTTPClient( | ||
mock.WithRequestMatch( | ||
mock.GetReposContentsByOwnerByRepoByPath, | ||
mockDirContent, | ||
), | ||
), | ||
requestArgs: map[string]any{ | ||
"owner": []string{"owner"}, | ||
"repo": []string{"repo"}, | ||
"path": []string{"src"}, | ||
}, | ||
expectError: false, | ||
expectedResult: expectedDirContent, | ||
}, | ||
{ | ||
name: "empty content fetch", | ||
mockedClient: mock.NewMockedHTTPClient( | ||
mock.WithRequestMatch( | ||
mock.GetReposContentsByOwnerByRepoByPath, | ||
[]*github.RepositoryContent{}, | ||
), | ||
), | ||
requestArgs: map[string]any{ | ||
"owner": []string{"owner"}, | ||
"repo": []string{"repo"}, | ||
"path": []string{"src"}, | ||
}, | ||
expectError: false, | ||
expectedResult: nil, | ||
}, | ||
{ | ||
name: "content fetch fails", | ||
mockedClient: mock.NewMockedHTTPClient( | ||
mock.WithRequestMatchHandler( | ||
mock.GetReposContentsByOwnerByRepoByPath, | ||
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
w.WriteHeader(http.StatusNotFound) | ||
_, _ = w.Write([]byte(`{"message": "Not Found"}`)) | ||
}), | ||
), | ||
), | ||
requestArgs: map[string]any{ | ||
"owner": []string{"owner"}, | ||
"repo": []string{"repo"}, | ||
"path": []string{"nonexistent.md"}, | ||
"branch": []string{"main"}, | ||
}, | ||
expectError: true, | ||
expectedErrMsg: "404 Not Found", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
// Setup client with mock | ||
client := github.NewClient(tc.mockedClient) | ||
handler := repoContentsResourceHandler(client) | ||
|
||
// Create call request | ||
request := mcp.ReadResourceRequest{ | ||
Params: struct { | ||
URI string `json:"uri"` | ||
Arguments map[string]any `json:"arguments,omitempty"` | ||
}{ | ||
Arguments: tc.requestArgs, | ||
}, | ||
} | ||
|
||
resp, err := handler(context.TODO(), request) | ||
|
||
if tc.expectError { | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), tc.expectedErrMsg) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.ElementsMatch(t, resp, tc.expectedResult) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
largely copied from
github-mcp-server/pkg/github/repositories_test.go
Line 18 in d8b0056