Skip to content

Commit ce0ab9c

Browse files
committed
Tests for artifacts and usage
1 parent ea7b19b commit ce0ab9c

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

pkg/github/actions_test.go

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,3 +1981,191 @@ func Test_ActionsResourceRead_DownloadWorkflowArtifact(t *testing.T) {
19811981
})
19821982
}
19831983
}
1984+
1985+
func Test_ActionsResourceRead_ListWorkflowArtifacts(t *testing.T) {
1986+
tests := []struct {
1987+
name string
1988+
mockedClient *http.Client
1989+
requestArgs map[string]any
1990+
expectError bool
1991+
expectedErrMsg string
1992+
expectedErrMsgRegexp *regexp.Regexp
1993+
}{
1994+
{
1995+
name: "successful workflow artifacts read",
1996+
mockedClient: mock.NewMockedHTTPClient(
1997+
mock.WithRequestMatchHandler(
1998+
mock.GetReposActionsRunsArtifactsByOwnerByRepoByRunId,
1999+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
2000+
artifacts := &github.ArtifactList{
2001+
TotalCount: github.Ptr(int64(2)),
2002+
Artifacts: []*github.Artifact{
2003+
{
2004+
ID: github.Ptr(int64(12345)),
2005+
Name: github.Ptr("artifact-1"),
2006+
},
2007+
{
2008+
ID: github.Ptr(int64(12346)),
2009+
Name: github.Ptr("artifact-2"),
2010+
},
2011+
},
2012+
}
2013+
w.WriteHeader(http.StatusOK)
2014+
_ = json.NewEncoder(w).Encode(artifacts)
2015+
}),
2016+
),
2017+
),
2018+
requestArgs: map[string]any{
2019+
"action": "list_workflow_artifacts",
2020+
"owner": "owner",
2021+
"repo": "repo",
2022+
"resource_id": float64(1),
2023+
},
2024+
expectError: false,
2025+
},
2026+
{
2027+
name: "missing workflow artifacts read",
2028+
mockedClient: mock.NewMockedHTTPClient(
2029+
mock.WithRequestMatchHandler(
2030+
mock.GetReposActionsRunsArtifactsByOwnerByRepoByRunId,
2031+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
2032+
w.WriteHeader(http.StatusNotFound)
2033+
}),
2034+
),
2035+
),
2036+
requestArgs: map[string]any{
2037+
"action": "list_workflow_artifacts",
2038+
"owner": "owner",
2039+
"repo": "repo",
2040+
"resource_id": float64(99999),
2041+
},
2042+
expectError: true,
2043+
expectedErrMsgRegexp: regexp.MustCompile(`^failed to list workflow run artifacts: GET .*/repos/owner/repo/actions/runs/99999/artifacts.* 404.*$`),
2044+
},
2045+
}
2046+
2047+
for _, tc := range tests {
2048+
t.Run(tc.name, func(t *testing.T) {
2049+
// Setup client with mock
2050+
client := github.NewClient(tc.mockedClient)
2051+
_, handler := ActionsRead(stubGetClientFn(client), translations.NullTranslationHelper)
2052+
2053+
// Create call request
2054+
request := createMCPRequest(tc.requestArgs)
2055+
2056+
// Call handler
2057+
result, err := handler(context.Background(), request)
2058+
2059+
require.NoError(t, err)
2060+
require.Equal(t, tc.expectError, result.IsError)
2061+
2062+
// Parse the result and get the text content if no error
2063+
textContent := getTextResult(t, result)
2064+
2065+
if tc.expectedErrMsg != "" {
2066+
assert.Contains(t, tc.expectedErrMsg, textContent.Text)
2067+
return
2068+
}
2069+
2070+
if tc.expectedErrMsgRegexp != nil {
2071+
assert.Regexp(t, tc.expectedErrMsgRegexp, textContent.Text)
2072+
return
2073+
}
2074+
})
2075+
}
2076+
}
2077+
2078+
func Test_ActionsResourceRead_GetWorkflowUsage(t *testing.T) {
2079+
tests := []struct {
2080+
name string
2081+
mockedClient *http.Client
2082+
requestArgs map[string]any
2083+
expectError bool
2084+
expectedErrMsg string
2085+
expectedErrMsgRegexp *regexp.Regexp
2086+
}{
2087+
{
2088+
name: "successful workflow usage read",
2089+
mockedClient: mock.NewMockedHTTPClient(
2090+
mock.WithRequestMatchHandler(
2091+
mock.GetReposActionsRunsTimingByOwnerByRepoByRunId,
2092+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
2093+
workflowUsage := &github.WorkflowRunUsage{
2094+
Billable: &github.WorkflowRunBillMap{
2095+
"UBUNTU": &github.WorkflowRunBill{
2096+
TotalMS: github.Ptr(int64(60000)),
2097+
Jobs: github.Ptr(1),
2098+
JobRuns: []*github.WorkflowRunJobRun{
2099+
&github.WorkflowRunJobRun{
2100+
JobID: github.Ptr(1),
2101+
DurationMS: github.Ptr(int64(600)),
2102+
},
2103+
},
2104+
},
2105+
},
2106+
RunDurationMS: github.Ptr(int64(60000)),
2107+
}
2108+
w.WriteHeader(http.StatusOK)
2109+
_ = json.NewEncoder(w).Encode(workflowUsage)
2110+
}),
2111+
),
2112+
),
2113+
requestArgs: map[string]any{
2114+
"action": "get_workflow_run_usage",
2115+
"owner": "owner",
2116+
"repo": "repo",
2117+
"resource_id": float64(1),
2118+
},
2119+
expectError: false,
2120+
},
2121+
{
2122+
name: "missing workflow usage read",
2123+
mockedClient: mock.NewMockedHTTPClient(
2124+
mock.WithRequestMatchHandler(
2125+
mock.GetReposActionsRunsTimingByOwnerByRepoByRunId,
2126+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
2127+
w.WriteHeader(http.StatusNotFound)
2128+
}),
2129+
),
2130+
),
2131+
requestArgs: map[string]any{
2132+
"action": "get_workflow_run_usage",
2133+
"owner": "owner",
2134+
"repo": "repo",
2135+
"resource_id": float64(99999),
2136+
},
2137+
expectError: true,
2138+
expectedErrMsgRegexp: regexp.MustCompile(`^failed to get workflow run usage: GET .*/repos/owner/repo/actions/runs/99999/timing.* 404.*$`),
2139+
},
2140+
}
2141+
2142+
for _, tc := range tests {
2143+
t.Run(tc.name, func(t *testing.T) {
2144+
// Setup client with mock
2145+
client := github.NewClient(tc.mockedClient)
2146+
_, handler := ActionsRead(stubGetClientFn(client), translations.NullTranslationHelper)
2147+
2148+
// Create call request
2149+
request := createMCPRequest(tc.requestArgs)
2150+
2151+
// Call handler
2152+
result, err := handler(context.Background(), request)
2153+
2154+
require.NoError(t, err)
2155+
require.Equal(t, tc.expectError, result.IsError)
2156+
2157+
// Parse the result and get the text content if no error
2158+
textContent := getTextResult(t, result)
2159+
2160+
if tc.expectedErrMsg != "" {
2161+
assert.Contains(t, tc.expectedErrMsg, textContent.Text)
2162+
return
2163+
}
2164+
2165+
if tc.expectedErrMsgRegexp != nil {
2166+
assert.Regexp(t, tc.expectedErrMsgRegexp, textContent.Text)
2167+
return
2168+
}
2169+
})
2170+
}
2171+
}

0 commit comments

Comments
 (0)