(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func Test_GetCodeScanningAlert(t *testing.T) { |
| 18 | // Verify tool definition once |
| 19 | toolDef := GetCodeScanningAlert(translations.NullTranslationHelper) |
| 20 | require.NoError(t, toolsnaps.Test(toolDef.Tool.Name, toolDef.Tool)) |
| 21 | |
| 22 | assert.Equal(t, "get_code_scanning_alert", toolDef.Tool.Name) |
| 23 | assert.NotEmpty(t, toolDef.Tool.Description) |
| 24 | |
| 25 | // InputSchema is of type any, need to cast to *jsonschema.Schema |
| 26 | schema, ok := toolDef.Tool.InputSchema.(*jsonschema.Schema) |
| 27 | require.True(t, ok, "InputSchema should be *jsonschema.Schema") |
| 28 | assert.Contains(t, schema.Properties, "owner") |
| 29 | assert.Contains(t, schema.Properties, "repo") |
| 30 | assert.Contains(t, schema.Properties, "alertNumber") |
| 31 | assert.ElementsMatch(t, schema.Required, []string{"owner", "repo", "alertNumber"}) |
| 32 | |
| 33 | // Setup mock alert for success case |
| 34 | mockAlert := &github.Alert{ |
| 35 | Number: github.Ptr(42), |
| 36 | State: github.Ptr("open"), |
| 37 | Rule: &github.Rule{ID: github.Ptr("test-rule"), Description: github.Ptr("Test Rule Description")}, |
| 38 | HTMLURL: github.Ptr("https://github.com/owner/repo/security/code-scanning/42"), |
| 39 | } |
| 40 | |
| 41 | tests := []struct { |
| 42 | name string |
| 43 | mockedClient *http.Client |
| 44 | requestArgs map[string]any |
| 45 | expectError bool |
| 46 | expectedAlert *github.Alert |
| 47 | expectedErrMsg string |
| 48 | }{ |
| 49 | { |
| 50 | name: "successful alert fetch", |
| 51 | mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 52 | GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber: mockResponse(t, http.StatusOK, mockAlert), |
| 53 | }), |
| 54 | requestArgs: map[string]any{ |
| 55 | "owner": "owner", |
| 56 | "repo": "repo", |
| 57 | "alertNumber": float64(42), |
| 58 | }, |
| 59 | expectError: false, |
| 60 | expectedAlert: mockAlert, |
| 61 | }, |
| 62 | { |
| 63 | name: "alert fetch fails", |
| 64 | mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 65 | GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 66 | w.WriteHeader(http.StatusNotFound) |
| 67 | _, _ = w.Write([]byte(`{"message": "Not Found"}`)) |
| 68 | }), |
| 69 | }), |
| 70 | requestArgs: map[string]any{ |
| 71 | "owner": "owner", |
| 72 | "repo": "repo", |
| 73 | "alertNumber": float64(9999), |
| 74 | }, |
nothing calls this directly
no test coverage detected