MCPcopy
hub / github.com/github/github-mcp-server / Test_GetFileContents

Function Test_GetFileContents

pkg/github/repositories_test.go:26–481  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

24)
25
26func Test_GetFileContents(t *testing.T) {
27 // Verify tool definition once
28 serverTool := GetFileContents(translations.NullTranslationHelper)
29 tool := serverTool.Tool
30 require.NoError(t, toolsnaps.Test(tool.Name, tool))
31
32 schema, ok := tool.InputSchema.(*jsonschema.Schema)
33 require.True(t, ok, "InputSchema should be *jsonschema.Schema")
34
35 assert.Equal(t, "get_file_contents", tool.Name)
36 assert.NotEmpty(t, tool.Description)
37 assert.Contains(t, schema.Properties, "owner")
38 assert.Contains(t, schema.Properties, "repo")
39 assert.Contains(t, schema.Properties, "path")
40 assert.Contains(t, schema.Properties, "ref")
41 assert.Contains(t, schema.Properties, "sha")
42 assert.ElementsMatch(t, schema.Required, []string{"owner", "repo"})
43
44 // Mock response for raw content
45 mockRawContent := []byte("# Test Repository\n\nThis is a test repository.")
46
47 // Setup mock directory content for success case
48 mockDirContent := []*github.RepositoryContent{
49 {
50 Type: github.Ptr("file"),
51 Name: github.Ptr("README.md"),
52 Path: github.Ptr("README.md"),
53 SHA: github.Ptr("abc123"),
54 Size: github.Ptr(42),
55 HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/README.md"),
56 },
57 {
58 Type: github.Ptr("dir"),
59 Name: github.Ptr("src"),
60 Path: github.Ptr("src"),
61 SHA: github.Ptr("def456"),
62 HTMLURL: github.Ptr("https://github.com/owner/repo/tree/main/src"),
63 },
64 }
65
66 tests := []struct {
67 name string
68 mockedClient *http.Client
69 requestArgs map[string]any
70 expectError bool
71 expectedResult any
72 expectedErrMsg string
73 expectStatus int
74 expectedMsg string // optional: expected message text to verify in result
75 }{
76 {
77 name: "successful text content fetch",
78 mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
79 GetReposGitRefByOwnerByRepoByRef: mockResponse(t, http.StatusOK, "{\"ref\": \"refs/heads/main\", \"object\": {\"sha\": \"\"}}"),
80 GetReposByOwnerByRepo: mockResponse(t, http.StatusOK, "{\"name\": \"repo\", \"default_branch\": \"main\"}"),
81 GetReposContentsByOwnerByRepoByPath: func(w http.ResponseWriter, _ *http.Request) {
82 w.WriteHeader(http.StatusOK)
83 // Base64 encode the content as GitHub API does

Callers

nothing calls this directly

Calls 15

TestFunction · 0.92
NewToolResultErrorFunction · 0.92
NewClientFunction · 0.92
GetFileContentsFunction · 0.85
mockResponseFunction · 0.85
expectQueryParamsFunction · 0.85
mustNewGHClientFunction · 0.85
createMCPRequestFunction · 0.85
ContextWithDepsFunction · 0.85
getErrorResultFunction · 0.85
getResourceResultFunction · 0.85

Tested by

no test coverage detected