Skip to content

Commit ea7b19b

Browse files
committed
More tests
1 parent 3796906 commit ea7b19b

File tree

1 file changed

+263
-0
lines changed

1 file changed

+263
-0
lines changed

pkg/github/actions_test.go

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,3 +1718,266 @@ func Test_ActionsResourceRead_ListWorkflowRuns(t *testing.T) {
17181718
})
17191719
}
17201720
}
1721+
1722+
func Test_ActionsResourceRead_GetWorkflowJob(t *testing.T) {
1723+
tests := []struct {
1724+
name string
1725+
mockedClient *http.Client
1726+
requestArgs map[string]any
1727+
expectError bool
1728+
expectedErrMsg string
1729+
expectedErrMsgRegexp *regexp.Regexp
1730+
}{
1731+
{
1732+
name: "successful workflow job read",
1733+
mockedClient: mock.NewMockedHTTPClient(
1734+
mock.WithRequestMatchHandler(
1735+
mock.GetReposActionsJobsByOwnerByRepoByJobId,
1736+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1737+
job := &github.WorkflowJob{
1738+
ID: github.Ptr(int64(12345)),
1739+
RunID: github.Ptr(int64(1)),
1740+
Status: github.Ptr("completed"),
1741+
Conclusion: github.Ptr("success"),
1742+
}
1743+
w.WriteHeader(http.StatusOK)
1744+
_ = json.NewEncoder(w).Encode(job)
1745+
}),
1746+
),
1747+
),
1748+
requestArgs: map[string]any{
1749+
"action": "get_workflow_job",
1750+
"owner": "owner",
1751+
"repo": "repo",
1752+
"resource_id": float64(12345),
1753+
},
1754+
expectError: false,
1755+
},
1756+
{
1757+
name: "missing workflow job read",
1758+
mockedClient: mock.NewMockedHTTPClient(
1759+
mock.WithRequestMatchHandler(
1760+
mock.GetReposActionsJobsByOwnerByRepoByJobId,
1761+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1762+
w.WriteHeader(http.StatusNotFound)
1763+
}),
1764+
),
1765+
),
1766+
requestArgs: map[string]any{
1767+
"action": "get_workflow_job",
1768+
"owner": "owner",
1769+
"repo": "repo",
1770+
"resource_id": float64(99999),
1771+
},
1772+
expectError: true,
1773+
expectedErrMsgRegexp: regexp.MustCompile(`^failed to get workflow job: GET .*/repos/owner/repo/actions/jobs/99999: 404.*$`),
1774+
},
1775+
}
1776+
1777+
for _, tc := range tests {
1778+
t.Run(tc.name, func(t *testing.T) {
1779+
// Setup client with mock
1780+
client := github.NewClient(tc.mockedClient)
1781+
_, handler := ActionsRead(stubGetClientFn(client), translations.NullTranslationHelper)
1782+
1783+
// Create call request
1784+
request := createMCPRequest(tc.requestArgs)
1785+
1786+
// Call handler
1787+
result, err := handler(context.Background(), request)
1788+
1789+
require.NoError(t, err)
1790+
require.Equal(t, tc.expectError, result.IsError)
1791+
1792+
// Parse the result and get the text content if no error
1793+
textContent := getTextResult(t, result)
1794+
1795+
if tc.expectedErrMsg != "" {
1796+
assert.Contains(t, tc.expectedErrMsg, textContent.Text)
1797+
return
1798+
}
1799+
1800+
if tc.expectedErrMsgRegexp != nil {
1801+
assert.Regexp(t, tc.expectedErrMsgRegexp, textContent.Text)
1802+
return
1803+
}
1804+
})
1805+
}
1806+
}
1807+
1808+
func Test_ActionsResourceRead_ListWorkflowJobs(t *testing.T) {
1809+
tests := []struct {
1810+
name string
1811+
mockedClient *http.Client
1812+
requestArgs map[string]any
1813+
expectError bool
1814+
expectedErrMsg string
1815+
expectedErrMsgRegexp *regexp.Regexp
1816+
}{
1817+
{
1818+
name: "successful workflow jobs read",
1819+
mockedClient: mock.NewMockedHTTPClient(
1820+
mock.WithRequestMatchHandler(
1821+
mock.GetReposActionsRunsJobsByOwnerByRepoByRunId,
1822+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1823+
jobs := &github.Jobs{
1824+
TotalCount: github.Ptr(2),
1825+
Jobs: []*github.WorkflowJob{
1826+
{
1827+
ID: github.Ptr(int64(12345)),
1828+
RunID: github.Ptr(int64(1)),
1829+
Status: github.Ptr("completed"),
1830+
Conclusion: github.Ptr("success"),
1831+
},
1832+
{
1833+
ID: github.Ptr(int64(12346)),
1834+
RunID: github.Ptr(int64(1)),
1835+
Status: github.Ptr("completed"),
1836+
Conclusion: github.Ptr("failure"),
1837+
},
1838+
},
1839+
}
1840+
w.WriteHeader(http.StatusOK)
1841+
_ = json.NewEncoder(w).Encode(jobs)
1842+
}),
1843+
),
1844+
),
1845+
requestArgs: map[string]any{
1846+
"action": "list_workflow_jobs",
1847+
"owner": "owner",
1848+
"repo": "repo",
1849+
"resource_id": float64(1),
1850+
},
1851+
expectError: false,
1852+
},
1853+
{
1854+
name: "missing workflow runs read",
1855+
mockedClient: mock.NewMockedHTTPClient(
1856+
mock.WithRequestMatchHandler(
1857+
mock.GetReposActionsRunsJobsByOwnerByRepoByRunId,
1858+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1859+
w.WriteHeader(http.StatusNotFound)
1860+
}),
1861+
),
1862+
),
1863+
requestArgs: map[string]any{
1864+
"action": "list_workflow_jobs",
1865+
"owner": "owner",
1866+
"repo": "repo",
1867+
"resource_id": float64(99999),
1868+
},
1869+
expectError: true,
1870+
expectedErrMsgRegexp: regexp.MustCompile(`^failed to list workflow jobs: GET .*/repos/owner/repo/actions/runs/99999/jobs.* 404.*$`),
1871+
},
1872+
}
1873+
1874+
for _, tc := range tests {
1875+
t.Run(tc.name, func(t *testing.T) {
1876+
// Setup client with mock
1877+
client := github.NewClient(tc.mockedClient)
1878+
_, handler := ActionsRead(stubGetClientFn(client), translations.NullTranslationHelper)
1879+
1880+
// Create call request
1881+
request := createMCPRequest(tc.requestArgs)
1882+
1883+
// Call handler
1884+
result, err := handler(context.Background(), request)
1885+
1886+
require.NoError(t, err)
1887+
require.Equal(t, tc.expectError, result.IsError)
1888+
1889+
// Parse the result and get the text content if no error
1890+
textContent := getTextResult(t, result)
1891+
1892+
if tc.expectedErrMsg != "" {
1893+
assert.Contains(t, tc.expectedErrMsg, textContent.Text)
1894+
return
1895+
}
1896+
1897+
if tc.expectedErrMsgRegexp != nil {
1898+
assert.Regexp(t, tc.expectedErrMsgRegexp, textContent.Text)
1899+
return
1900+
}
1901+
})
1902+
}
1903+
}
1904+
1905+
func Test_ActionsResourceRead_DownloadWorkflowArtifact(t *testing.T) {
1906+
tests := []struct {
1907+
name string
1908+
mockedClient *http.Client
1909+
requestArgs map[string]any
1910+
expectError bool
1911+
expectedErrMsg string
1912+
expectedErrMsgRegexp *regexp.Regexp
1913+
}{
1914+
{
1915+
name: "successful workflow artifact download",
1916+
mockedClient: mock.NewMockedHTTPClient(
1917+
mock.WithRequestMatchHandler(
1918+
mock.GetReposActionsArtifactsByOwnerByRepoByArtifactIdByArchiveFormat,
1919+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1920+
w.WriteHeader(http.StatusFound)
1921+
w.Header().Set("Location", "https://github.com/artifact/download/url")
1922+
}),
1923+
),
1924+
),
1925+
requestArgs: map[string]any{
1926+
"action": "download_workflow_artifact",
1927+
"owner": "owner",
1928+
"repo": "repo",
1929+
"resource_id": float64(12345),
1930+
},
1931+
expectError: false,
1932+
},
1933+
{
1934+
name: "missing workflow artifact download",
1935+
mockedClient: mock.NewMockedHTTPClient(
1936+
mock.WithRequestMatchHandler(
1937+
mock.GetReposActionsArtifactsByOwnerByRepoByArtifactId,
1938+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1939+
w.WriteHeader(http.StatusNotFound)
1940+
}),
1941+
),
1942+
),
1943+
requestArgs: map[string]any{
1944+
"action": "download_workflow_artifact",
1945+
"owner": "owner",
1946+
"repo": "repo",
1947+
"resource_id": float64(99999),
1948+
},
1949+
expectError: true,
1950+
expectedErrMsg: "failed to get artifact download URL: unexpected status code: 404 Not Found",
1951+
},
1952+
}
1953+
1954+
for _, tc := range tests {
1955+
t.Run(tc.name, func(t *testing.T) {
1956+
// Setup client with mock
1957+
client := github.NewClient(tc.mockedClient)
1958+
_, handler := ActionsRead(stubGetClientFn(client), translations.NullTranslationHelper)
1959+
1960+
// Create call request
1961+
request := createMCPRequest(tc.requestArgs)
1962+
1963+
// Call handler
1964+
result, err := handler(context.Background(), request)
1965+
1966+
require.NoError(t, err)
1967+
require.Equal(t, tc.expectError, result.IsError)
1968+
1969+
// Parse the result and get the text content if no error
1970+
textContent := getTextResult(t, result)
1971+
1972+
if tc.expectedErrMsg != "" {
1973+
assert.Contains(t, tc.expectedErrMsg, textContent.Text)
1974+
return
1975+
}
1976+
1977+
if tc.expectedErrMsgRegexp != nil {
1978+
assert.Regexp(t, tc.expectedErrMsgRegexp, textContent.Text)
1979+
return
1980+
}
1981+
})
1982+
}
1983+
}

0 commit comments

Comments
 (0)