|
| 1 | +import unittest |
| 2 | + |
| 3 | +import responses |
| 4 | +from openai import OpenAI |
| 5 | + |
| 6 | +from discovery.agent_support.agent import ToolCall |
| 7 | +from discovery.environment import require_env |
| 8 | +from discovery.github_support.github_client import GithubClient |
| 9 | +from discovery.repository_agent.repository_agent import repository_agent_creator |
| 10 | +from tests.slow_test_support import slow |
| 11 | + |
| 12 | + |
| 13 | +class TestRepositoryAgent(unittest.TestCase): |
| 14 | + def setUp(self): |
| 15 | + openai_client = OpenAI(api_key=require_env("OPEN_AI_KEY")) |
| 16 | + self.repo_dict = { |
| 17 | + "name": "pickles_web", |
| 18 | + "full_name": "pickles_company/pickles_web", |
| 19 | + "html_url": "https://example.com/pickles_company/pickles_web", |
| 20 | + "url": "https://api.example.com/pickles_company/pickles_web", |
| 21 | + "description": "Pickles Web", |
| 22 | + "private": False, |
| 23 | + "stargazers_count": 12, |
| 24 | + "watchers_count": 13, |
| 25 | + "forks_count": 14, |
| 26 | + } |
| 27 | + |
| 28 | + self.agent = repository_agent_creator(openai_client)(GithubClient("some-token")) |
| 29 | + |
| 30 | + @slow |
| 31 | + @responses.activate |
| 32 | + def test_list_repositories_for_organization(self): |
| 33 | + responses.add( |
| 34 | + responses.GET, |
| 35 | + "https://api.github.com/orgs/pickles_company/repos", |
| 36 | + json=[self.repo_dict], |
| 37 | + status=200, |
| 38 | + ) |
| 39 | + |
| 40 | + result = self.agent.answer("List repositories for the organization pickles_company.") |
| 41 | + |
| 42 | + self.assertIn("pickles_web", result.response) |
| 43 | + self.assertEqual([ToolCall(name="list_repositories_for_organization", arguments={ |
| 44 | + "organization": "pickles_company" |
| 45 | + })], result.tool_calls) |
| 46 | + |
| 47 | + @slow |
| 48 | + @responses.activate |
| 49 | + def test_list_repositories_for_user(self): |
| 50 | + responses.add( |
| 51 | + responses.GET, |
| 52 | + "https://api.github.com/users/some_user/repos", |
| 53 | + json=[self.repo_dict], |
| 54 | + status=200, |
| 55 | + ) |
| 56 | + |
| 57 | + result = self.agent.answer("List repositories for some_user.") |
| 58 | + |
| 59 | + self.assertIn("pickles_web", result.response) |
| 60 | + self.assertEqual([ToolCall(name="list_repositories_for_user", arguments={ |
| 61 | + "user": "some_user" |
| 62 | + })], result.tool_calls) |
| 63 | + |
| 64 | + @slow |
| 65 | + @responses.activate |
| 66 | + def test_search_repositories(self): |
| 67 | + responses.add( |
| 68 | + responses.GET, |
| 69 | + "https://api.github.com/search/repositories", |
| 70 | + json={ |
| 71 | + "items": [ |
| 72 | + { |
| 73 | + "name": "pickles_web", |
| 74 | + "full_name": "pickles_company/pickles_web", |
| 75 | + "html_url": "https://example.com/pickles_company/pickles_web", |
| 76 | + "url": "https://api.example.com/pickles_company/pickles_web", |
| 77 | + "description": "Pickles Web", |
| 78 | + "private": False, |
| 79 | + "stargazers_count": 12, |
| 80 | + "watchers_count": 13, |
| 81 | + "forks_count": 14, |
| 82 | + }, |
| 83 | + { |
| 84 | + "name": "chickens_web", |
| 85 | + "full_name": "pickles_company/dill_pickles_web", |
| 86 | + "html_url": "https://example.com/pickles_company/dill_pickles_web", |
| 87 | + "url": "https://api.example.com/pickles_company/dill_pickles_web", |
| 88 | + "description": "Dill Pickles Web", |
| 89 | + "private": True, |
| 90 | + "stargazers_count": 12, |
| 91 | + "watchers_count": 13, |
| 92 | + "forks_count": 15 |
| 93 | + } |
| 94 | + ] |
| 95 | + }, |
| 96 | + status=200, |
| 97 | + ) |
| 98 | + |
| 99 | + result = self.agent.answer( |
| 100 | + "Search the repositories that are private for the pickles_company and tell me the name of the repository") |
| 101 | + |
| 102 | + self.assertIn("dill_pickles_web", result.response) |
| 103 | + self.assertEqual( |
| 104 | + [ToolCall( |
| 105 | + name='search_repositories', |
| 106 | + arguments={'owner': 'pickles_company', 'owner_type': 'org', 'query': 'private'} |
| 107 | + )], |
| 108 | + result.tool_calls |
| 109 | + ) |
| 110 | + |
| 111 | + @slow |
| 112 | + @responses.activate |
| 113 | + def test_list_repository_languages(self): |
| 114 | + responses.add( |
| 115 | + responses.GET, |
| 116 | + "https://api.github.com/repos/pickles_org/pickles_repo/languages", |
| 117 | + json={ |
| 118 | + "python": 100, |
| 119 | + "html": 50, |
| 120 | + "css": 50, |
| 121 | + }, |
| 122 | + status=200, |
| 123 | + ) |
| 124 | + |
| 125 | + result = self.agent.answer("What languages does the 'pickles_repo' from the 'pickles_org' use?") |
| 126 | + |
| 127 | + self.assertIn("Python", result.response) |
| 128 | + self.assertIn("HTML", result.response) |
| 129 | + self.assertIn("CSS", result.response) |
| 130 | + self.assertNotIn("java", result.response) |
| 131 | + self.assertEqual( |
| 132 | + [ToolCall( |
| 133 | + name='list_repository_languages', |
| 134 | + arguments={'full_name': 'pickles_org/pickles_repo'} |
| 135 | + )], |
| 136 | + result.tool_calls |
| 137 | + ) |
| 138 | + |
| 139 | + @slow |
| 140 | + @responses.activate |
| 141 | + def test_list_repository_contributors(self): |
| 142 | + responses.add( |
| 143 | + responses.GET, |
| 144 | + "https://api.github.com/repos/pickles_org/pickles_repo/contributors", |
| 145 | + json=[ |
| 146 | + {"login": "fred"}, |
| 147 | + {"login": "mary"}, |
| 148 | + {"login": "kate"}, |
| 149 | + ], |
| 150 | + status=200, |
| 151 | + ) |
| 152 | + |
| 153 | + result = self.agent.answer("Who contributes to the pickles_repo within the pickles_org?") |
| 154 | + |
| 155 | + self.assertIn("fred", result.response) |
| 156 | + self.assertIn("mary", result.response) |
| 157 | + self.assertIn("kate", result.response) |
| 158 | + self.assertNotIn("chuck", result.response) |
| 159 | + self.assertEqual( |
| 160 | + [ToolCall( |
| 161 | + name='list_repository_contributors', |
| 162 | + arguments={'full_name': 'pickles_org/pickles_repo'} |
| 163 | + )], |
| 164 | + result.tool_calls |
| 165 | + ) |
0 commit comments