3
from pathlib import Path
4
from unittest.mock import patch
8
from git import InvalidGitRepositoryError
10
import autogpt.app.utils
11
from autogpt.app.utils import (
12
get_bulletin_from_web,
13
get_current_git_branch,
17
from autogpt.core.utils.json_utils import extract_dict_from_json
18
from autogpt.utils import validate_yaml_file
19
from tests.utils import skip_in_ci
23
def valid_json_response() -> dict:
26
"text": "My task is complete. I will use the 'task_complete' command "
28
"reasoning": "I will use the 'task_complete' command because it allows me "
29
"to shut down and signal that my task is complete.",
30
"plan": "I will use the 'task_complete' command with the reason "
31
"'Task complete: retrieved Tesla's revenue in 2022.' to shut down.",
32
"criticism": "I need to ensure that I have completed all necessary tasks "
33
"before shutting down.",
37
"name": "task_complete",
38
"args": {"reason": "Task complete: retrieved Tesla's revenue in 2022."},
44
def invalid_json_response() -> dict:
47
"text": "My task is complete. I will use the 'task_complete' command "
49
"reasoning": "I will use the 'task_complete' command because it allows me "
50
"to shut down and signal that my task is complete.",
51
"plan": "I will use the 'task_complete' command with the reason "
52
"'Task complete: retrieved Tesla's revenue in 2022.' to shut down.",
53
"criticism": "I need to ensure that I have completed all necessary tasks "
54
"before shutting down.",
57
"command": {"name": "", "args": {}},
61
def test_validate_yaml_file_valid():
62
with open("valid_test_file.yaml", "w") as f:
63
f.write("setting: value")
64
result, message = validate_yaml_file("valid_test_file.yaml")
65
os.remove("valid_test_file.yaml")
68
assert "Successfully validated" in message
71
def test_validate_yaml_file_not_found():
72
result, message = validate_yaml_file("non_existent_file.yaml")
74
assert result is False
75
assert "wasn't found" in message
78
def test_validate_yaml_file_invalid():
79
with open("invalid_test_file.yaml", "w") as f:
82
" first_setting: value\n"
83
" second_setting: value\n"
84
" nested_setting: value\n"
85
" third_setting: value\n"
86
"unindented_setting: value"
88
result, message = validate_yaml_file("invalid_test_file.yaml")
89
os.remove("invalid_test_file.yaml")
92
assert result is False
93
assert "There was an issue while trying to read" in message
97
def test_get_bulletin_from_web_success(mock_get):
98
expected_content = "Test bulletin from web"
100
mock_get.return_value.status_code = 200
101
mock_get.return_value.text = expected_content
102
bulletin = get_bulletin_from_web()
104
assert expected_content in bulletin
105
mock_get.assert_called_with(
106
"https://raw.githubusercontent.com/Significant-Gravitas/AutoGPT/master/autogpts/autogpt/BULLETIN.md" # noqa: E501
110
@patch("requests.get")
111
def test_get_bulletin_from_web_failure(mock_get):
112
mock_get.return_value.status_code = 404
113
bulletin = get_bulletin_from_web()
115
assert bulletin == ""
118
@patch("requests.get")
119
def test_get_bulletin_from_web_exception(mock_get):
120
mock_get.side_effect = requests.exceptions.RequestException()
121
bulletin = get_bulletin_from_web()
123
assert bulletin == ""
126
def test_get_latest_bulletin_no_file():
127
if os.path.exists("data/CURRENT_BULLETIN.md"):
128
os.remove("data/CURRENT_BULLETIN.md")
130
bulletin, is_new = get_latest_bulletin()
134
def test_get_latest_bulletin_with_file():
135
expected_content = "Test bulletin"
136
with open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8") as f:
137
f.write(expected_content)
139
with patch("autogpt.app.utils.get_bulletin_from_web", return_value=""):
140
bulletin, is_new = get_latest_bulletin()
141
assert expected_content in bulletin
142
assert is_new is False
144
os.remove("data/CURRENT_BULLETIN.md")
147
def test_get_latest_bulletin_with_new_bulletin():
148
with open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8") as f:
149
f.write("Old bulletin")
151
expected_content = "New bulletin from web"
153
"autogpt.app.utils.get_bulletin_from_web", return_value=expected_content
155
bulletin, is_new = get_latest_bulletin()
156
assert "::NEW BULLETIN::" in bulletin
157
assert expected_content in bulletin
160
os.remove("data/CURRENT_BULLETIN.md")
163
def test_get_latest_bulletin_new_bulletin_same_as_old_bulletin():
164
expected_content = "Current bulletin"
165
with open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8") as f:
166
f.write(expected_content)
169
"autogpt.app.utils.get_bulletin_from_web", return_value=expected_content
171
bulletin, is_new = get_latest_bulletin()
172
assert expected_content in bulletin
173
assert is_new is False
175
os.remove("data/CURRENT_BULLETIN.md")
179
def test_get_current_git_branch():
180
branch_name = get_current_git_branch()
181
assert branch_name != ""
184
@patch("autogpt.app.utils.Repo")
185
def test_get_current_git_branch_success(mock_repo):
186
mock_repo.return_value.active_branch.name = "test-branch"
187
branch_name = get_current_git_branch()
189
assert branch_name == "test-branch"
192
@patch("autogpt.app.utils.Repo")
193
def test_get_current_git_branch_failure(mock_repo):
194
mock_repo.side_effect = InvalidGitRepositoryError()
195
branch_name = get_current_git_branch()
197
assert branch_name == ""
200
def test_extract_json_from_response(valid_json_response: dict):
201
emulated_response_from_openai = json.dumps(valid_json_response)
202
assert extract_dict_from_json(emulated_response_from_openai) == valid_json_response
205
def test_extract_json_from_response_wrapped_in_code_block(valid_json_response: dict):
206
emulated_response_from_openai = "```" + json.dumps(valid_json_response) + "```"
207
assert extract_dict_from_json(emulated_response_from_openai) == valid_json_response
210
def test_extract_json_from_response_wrapped_in_code_block_with_language(
211
valid_json_response: dict,
213
emulated_response_from_openai = "```json" + json.dumps(valid_json_response) + "```"
214
assert extract_dict_from_json(emulated_response_from_openai) == valid_json_response
217
def test_extract_json_from_response_json_contained_in_string(valid_json_response: dict):
218
emulated_response_from_openai = (
219
"sentence1" + json.dumps(valid_json_response) + "sentence2"
221
assert extract_dict_from_json(emulated_response_from_openai) == valid_json_response
225
def mock_env_file_path(tmp_path):
226
return tmp_path / ".env"
229
env_file_initial_content = """
231
EXISTING_KEY=EXISTING_VALUE
233
## This is also a comment
234
# DISABLED_KEY=DISABLED_VALUE
237
UNUSED_KEY=UNUSED_VALUE
242
def mock_env_file(mock_env_file_path: Path, monkeypatch: pytest.MonkeyPatch):
243
mock_env_file_path.write_text(env_file_initial_content)
244
monkeypatch.setattr(autogpt.app.utils, "ENV_FILE_PATH", mock_env_file_path)
245
return mock_env_file_path
249
def mock_environ(monkeypatch: pytest.MonkeyPatch):
251
monkeypatch.setattr(os, "environ", env)
255
def test_set_env_config_value_updates_existing_key(
256
mock_env_file: Path, mock_environ: dict
258
# Before updating, ensure the original content is as expected
259
with mock_env_file.open("r") as file:
260
assert file.readlines() == env_file_initial_content.splitlines(True)
262
set_env_config_value("EXISTING_KEY", "NEW_VALUE")
263
with mock_env_file.open("r") as file:
264
content = file.readlines()
266
# Ensure only the relevant line is altered
267
expected_content_lines = [
269
"# This is a comment\n",
270
"EXISTING_KEY=NEW_VALUE\n", # existing key + new value
272
"## This is also a comment\n",
273
"# DISABLED_KEY=DISABLED_VALUE\n",
275
"# Another comment\n",
276
"UNUSED_KEY=UNUSED_VALUE\n",
278
assert content == expected_content_lines
279
assert mock_environ["EXISTING_KEY"] == "NEW_VALUE"
282
def test_set_env_config_value_uncomments_and_updates_disabled_key(
283
mock_env_file: Path, mock_environ: dict
285
# Before adding, ensure the original content is as expected
286
with mock_env_file.open("r") as file:
287
assert file.readlines() == env_file_initial_content.splitlines(True)
289
set_env_config_value("DISABLED_KEY", "ENABLED_NEW_VALUE")
290
with mock_env_file.open("r") as file:
291
content = file.readlines()
293
# Ensure only the relevant line is altered
294
expected_content_lines = [
296
"# This is a comment\n",
297
"EXISTING_KEY=EXISTING_VALUE\n",
299
"## This is also a comment\n",
300
"DISABLED_KEY=ENABLED_NEW_VALUE\n", # disabled -> enabled + new value
302
"# Another comment\n",
303
"UNUSED_KEY=UNUSED_VALUE\n",
305
assert content == expected_content_lines
306
assert mock_environ["DISABLED_KEY"] == "ENABLED_NEW_VALUE"
309
def test_set_env_config_value_adds_new_key(mock_env_file: Path, mock_environ: dict):
310
# Before adding, ensure the original content is as expected
311
with mock_env_file.open("r") as file:
312
assert file.readlines() == env_file_initial_content.splitlines(True)
314
set_env_config_value("NEW_KEY", "NEW_VALUE")
315
with mock_env_file.open("r") as file:
316
content = file.readlines()
318
# Ensure the new key-value pair is added without altering the rest
319
expected_content_lines = [
321
"# This is a comment\n",
322
"EXISTING_KEY=EXISTING_VALUE\n",
324
"## This is also a comment\n",
325
"# DISABLED_KEY=DISABLED_VALUE\n",
327
"# Another comment\n",
328
"UNUSED_KEY=UNUSED_VALUE\n",
329
"NEW_KEY=NEW_VALUE\n", # New key-value pair added at the end
331
assert content == expected_content_lines
332
assert mock_environ["NEW_KEY"] == "NEW_VALUE"