ara-cli 0.1.9.86__py3-none-any.whl → 0.1.9.89__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ara_cli/ara_config.py +69 -114
- ara_cli/prompt_handler.py +2 -5
- ara_cli/version.py +1 -1
- {ara_cli-0.1.9.86.dist-info → ara_cli-0.1.9.89.dist-info}/METADATA +1 -1
- {ara_cli-0.1.9.86.dist-info → ara_cli-0.1.9.89.dist-info}/RECORD +10 -9
- tests/test_ara_config.py +174 -281
- tests/test_prompt_handler.py +306 -0
- {ara_cli-0.1.9.86.dist-info → ara_cli-0.1.9.89.dist-info}/WHEEL +0 -0
- {ara_cli-0.1.9.86.dist-info → ara_cli-0.1.9.89.dist-info}/entry_points.txt +0 -0
- {ara_cli-0.1.9.86.dist-info → ara_cli-0.1.9.89.dist-info}/top_level.txt +0 -0
tests/test_ara_config.py
CHANGED
|
@@ -1,169 +1,130 @@
|
|
|
1
1
|
import os
|
|
2
2
|
import json
|
|
3
3
|
import pytest
|
|
4
|
-
from unittest.mock import patch, mock_open, MagicMock
|
|
5
|
-
from tempfile import TemporaryDirectory
|
|
6
|
-
from pydantic import ValidationError
|
|
4
|
+
from unittest.mock import patch, mock_open, MagicMock
|
|
7
5
|
import sys
|
|
8
6
|
from io import StringIO
|
|
7
|
+
from pydantic import ValidationError
|
|
9
8
|
|
|
9
|
+
# Assuming the test file is structured to import from the production code module
|
|
10
10
|
from ara_cli.ara_config import (
|
|
11
|
-
ensure_directory_exists,
|
|
12
|
-
read_data,
|
|
11
|
+
ensure_directory_exists,
|
|
12
|
+
read_data,
|
|
13
13
|
save_data,
|
|
14
|
-
ARAconfig,
|
|
15
|
-
ConfigManager,
|
|
14
|
+
ARAconfig,
|
|
15
|
+
ConfigManager,
|
|
16
16
|
DEFAULT_CONFIG_LOCATION,
|
|
17
17
|
LLMConfigItem,
|
|
18
|
-
ExtCodeDirItem,
|
|
19
18
|
handle_unrecognized_keys,
|
|
20
|
-
fix_llm_temperatures,
|
|
21
|
-
validate_and_fix_config_data
|
|
22
19
|
)
|
|
23
20
|
|
|
24
21
|
|
|
25
22
|
@pytest.fixture
|
|
26
23
|
def default_config_data():
|
|
24
|
+
"""Provides the default configuration as a dictionary."""
|
|
27
25
|
return ARAconfig().model_dump()
|
|
28
26
|
|
|
29
27
|
|
|
30
28
|
@pytest.fixture
|
|
31
29
|
def valid_config_dict():
|
|
30
|
+
"""A valid, non-default configuration dictionary for testing."""
|
|
32
31
|
return {
|
|
33
|
-
"ext_code_dirs": [
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"custom_prompt_templates_subdir": "custom-prompt-modules",
|
|
41
|
-
"local_ara_templates_dir": "./ara/.araconfig/templates/",
|
|
42
|
-
"ara_prompt_given_list_includes": ["*.py", "*.md"],
|
|
32
|
+
"ext_code_dirs": [{"source_dir": "./app"}],
|
|
33
|
+
"glossary_dir": "./custom_glossary",
|
|
34
|
+
"doc_dir": "./custom_docs",
|
|
35
|
+
"local_prompt_templates_dir": "./custom_prompts",
|
|
36
|
+
"custom_prompt_templates_subdir": "custom_subdir",
|
|
37
|
+
"local_ara_templates_dir": "./custom_templates/",
|
|
38
|
+
"ara_prompt_given_list_includes": ["*.py", "*.md", "*.json"],
|
|
43
39
|
"llm_config": {
|
|
44
|
-
"gpt-4o": {
|
|
40
|
+
"gpt-4o-custom": {
|
|
45
41
|
"provider": "openai",
|
|
46
42
|
"model": "openai/gpt-4o",
|
|
47
|
-
"temperature": 0.
|
|
48
|
-
"max_tokens":
|
|
43
|
+
"temperature": 0.5,
|
|
44
|
+
"max_tokens": 4096
|
|
49
45
|
}
|
|
50
46
|
},
|
|
51
|
-
"default_llm": "gpt-4o"
|
|
47
|
+
"default_llm": "gpt-4o-custom"
|
|
52
48
|
}
|
|
53
49
|
|
|
54
50
|
|
|
55
51
|
@pytest.fixture
|
|
56
52
|
def corrupted_config_dict():
|
|
53
|
+
"""A config dictionary with various type errors to test validation and fixing."""
|
|
57
54
|
return {
|
|
58
|
-
"ext_code_dirs": "should_be_a_list",
|
|
59
|
-
"glossary_dir": 123,
|
|
55
|
+
"ext_code_dirs": "should_be_a_list",
|
|
56
|
+
"glossary_dir": 123,
|
|
60
57
|
"llm_config": {
|
|
61
|
-
"
|
|
62
|
-
"provider": "
|
|
63
|
-
"model": "
|
|
64
|
-
"temperature": "
|
|
65
|
-
"max_tokens": "16384" # Should be int
|
|
58
|
+
"bad-model": {
|
|
59
|
+
"provider": "test",
|
|
60
|
+
"model": "test/model",
|
|
61
|
+
"temperature": "not_a_float"
|
|
66
62
|
}
|
|
67
|
-
}
|
|
63
|
+
},
|
|
64
|
+
"default_llm": 999
|
|
68
65
|
}
|
|
69
66
|
|
|
70
67
|
|
|
71
68
|
@pytest.fixture(autouse=True)
|
|
72
69
|
def reset_config_manager():
|
|
73
|
-
"""
|
|
70
|
+
"""Ensures a clean state for each test by resetting the singleton and caches."""
|
|
74
71
|
ConfigManager.reset()
|
|
75
72
|
yield
|
|
76
73
|
ConfigManager.reset()
|
|
77
74
|
|
|
75
|
+
# --- Test Pydantic Models ---
|
|
78
76
|
|
|
79
77
|
class TestLLMConfigItem:
|
|
80
78
|
def test_valid_temperature(self):
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
model="gpt-4",
|
|
84
|
-
temperature=0.7
|
|
85
|
-
)
|
|
79
|
+
"""Tests that a valid temperature is accepted."""
|
|
80
|
+
config = LLMConfigItem(provider="test", model="test/model", temperature=0.7)
|
|
86
81
|
assert config.temperature == 0.7
|
|
87
82
|
|
|
88
|
-
def
|
|
89
|
-
|
|
90
|
-
with pytest.raises(ValidationError
|
|
91
|
-
LLMConfigItem(
|
|
92
|
-
provider="openai",
|
|
93
|
-
model="gpt-4",
|
|
94
|
-
temperature=1.5
|
|
95
|
-
)
|
|
96
|
-
assert "less than or equal to 1" in str(exc_info.value)
|
|
97
|
-
|
|
98
|
-
def test_negative_temperature_raises_validation_error(self):
|
|
99
|
-
# The Field constraint prevents negative temperatures
|
|
100
|
-
with pytest.raises(ValidationError) as exc_info:
|
|
101
|
-
LLMConfigItem(
|
|
102
|
-
provider="openai",
|
|
103
|
-
model="gpt-4",
|
|
104
|
-
temperature=-0.5
|
|
105
|
-
)
|
|
106
|
-
assert "greater than or equal to 0" in str(exc_info.value)
|
|
107
|
-
|
|
108
|
-
def test_temperature_validator_with_dict_input(self):
|
|
109
|
-
# Test the validator through dict input (simulating JSON load)
|
|
110
|
-
# This tests the fix_llm_temperatures function behavior
|
|
111
|
-
data = {
|
|
112
|
-
"provider": "openai",
|
|
113
|
-
"model": "gpt-4",
|
|
114
|
-
"temperature": 0.8
|
|
115
|
-
}
|
|
116
|
-
config = LLMConfigItem(**data)
|
|
117
|
-
assert config.temperature == 0.8
|
|
118
|
-
|
|
83
|
+
def test_invalid_temperature_too_high_raises_error(self):
|
|
84
|
+
"""Tests that temperature > 1.0 raises a ValidationError."""
|
|
85
|
+
with pytest.raises(ValidationError, match="Input should be less than or equal to 1"):
|
|
86
|
+
LLMConfigItem(provider="test", model="test/model", temperature=1.5)
|
|
119
87
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
88
|
+
def test_invalid_temperature_too_low_raises_error(self):
|
|
89
|
+
"""Tests that temperature < 0.0 raises a ValidationError."""
|
|
90
|
+
with pytest.raises(ValidationError, match="Input should be greater than or equal to 0"):
|
|
91
|
+
LLMConfigItem(provider="test", model="test/model", temperature=-0.5)
|
|
124
92
|
|
|
125
93
|
|
|
126
94
|
class TestARAconfig:
|
|
127
|
-
def
|
|
95
|
+
def test_default_values_are_correct(self):
|
|
96
|
+
"""Tests that the model initializes with correct default values."""
|
|
128
97
|
config = ARAconfig()
|
|
129
|
-
assert
|
|
130
|
-
assert config.ext_code_dirs[0].source_dir == "./src"
|
|
131
|
-
assert config.ext_code_dirs[1].source_dir == "./tests"
|
|
98
|
+
assert config.ext_code_dirs == [{"source_dir": "./src"}, {"source_dir": "./tests"}]
|
|
132
99
|
assert config.glossary_dir == "./glossary"
|
|
133
|
-
assert config.default_llm == "gpt-
|
|
134
|
-
|
|
135
|
-
def test_forbid_extra_fields(self):
|
|
136
|
-
with pytest.raises(ValidationError) as exc_info:
|
|
137
|
-
ARAconfig(unknown_field="value")
|
|
138
|
-
assert "Extra inputs are not permitted" in str(exc_info.value)
|
|
100
|
+
assert config.default_llm == "gpt-5"
|
|
101
|
+
assert "gpt-5" in config.llm_config
|
|
139
102
|
|
|
140
103
|
@patch('sys.stdout', new_callable=StringIO)
|
|
141
|
-
def
|
|
104
|
+
def test_check_critical_fields_with_empty_list_reverts_to_default(self, mock_stdout):
|
|
105
|
+
"""Tests that an empty list for a critical field is reverted to its default."""
|
|
142
106
|
config = ARAconfig(ext_code_dirs=[])
|
|
143
107
|
assert len(config.ext_code_dirs) == 2
|
|
144
|
-
assert
|
|
108
|
+
assert config.ext_code_dirs[0] == {"source_dir": "./src"}
|
|
109
|
+
assert "Warning: Value for 'ext_code_dirs' is missing or empty. Using default." in mock_stdout.getvalue()
|
|
145
110
|
|
|
146
111
|
@patch('sys.stdout', new_callable=StringIO)
|
|
147
|
-
def
|
|
112
|
+
def test_check_critical_fields_with_empty_string_reverts_to_default(self, mock_stdout):
|
|
113
|
+
"""Tests that an empty string for a critical field is reverted to its default."""
|
|
148
114
|
config = ARAconfig(glossary_dir="")
|
|
149
115
|
assert config.glossary_dir == "./glossary"
|
|
150
|
-
assert "Warning: Value for 'glossary_dir' is missing or empty." in mock_stdout.getvalue()
|
|
151
|
-
|
|
152
|
-
@patch('sys.stdout', new_callable=StringIO)
|
|
153
|
-
def test_check_critical_fields_whitespace_string(self, mock_stdout):
|
|
154
|
-
config = ARAconfig(local_prompt_templates_dir=" ")
|
|
155
|
-
assert config.local_prompt_templates_dir == "./ara/.araconfig"
|
|
156
|
-
assert "Warning: Value for 'local_prompt_templates_dir' is missing or empty." in mock_stdout.getvalue()
|
|
116
|
+
assert "Warning: Value for 'glossary_dir' is missing or empty. Using default." in mock_stdout.getvalue()
|
|
157
117
|
|
|
118
|
+
# --- Test Helper Functions ---
|
|
158
119
|
|
|
159
120
|
class TestEnsureDirectoryExists:
|
|
160
121
|
@patch('sys.stdout', new_callable=StringIO)
|
|
161
122
|
@patch("os.makedirs")
|
|
162
123
|
@patch("ara_cli.ara_config.exists", return_value=False)
|
|
163
|
-
def
|
|
164
|
-
directory
|
|
165
|
-
# Clear the cache before test
|
|
124
|
+
def test_directory_creation_when_not_exists(self, mock_exists, mock_makedirs, mock_stdout):
|
|
125
|
+
"""Tests that a directory is created if it doesn't exist."""
|
|
166
126
|
ensure_directory_exists.cache_clear()
|
|
127
|
+
directory = "/tmp/new/dir"
|
|
167
128
|
result = ensure_directory_exists(directory)
|
|
168
129
|
|
|
169
130
|
mock_exists.assert_called_once_with(directory)
|
|
@@ -173,10 +134,10 @@ class TestEnsureDirectoryExists:
|
|
|
173
134
|
|
|
174
135
|
@patch("os.makedirs")
|
|
175
136
|
@patch("ara_cli.ara_config.exists", return_value=True)
|
|
176
|
-
def
|
|
177
|
-
directory
|
|
178
|
-
# Clear the cache before test
|
|
137
|
+
def test_directory_no_creation_when_exists(self, mock_exists, mock_makedirs):
|
|
138
|
+
"""Tests that a directory is not created if it already exists."""
|
|
179
139
|
ensure_directory_exists.cache_clear()
|
|
140
|
+
directory = "/tmp/existing/dir"
|
|
180
141
|
result = ensure_directory_exists(directory)
|
|
181
142
|
|
|
182
143
|
mock_exists.assert_called_once_with(directory)
|
|
@@ -186,134 +147,37 @@ class TestEnsureDirectoryExists:
|
|
|
186
147
|
|
|
187
148
|
class TestHandleUnrecognizedKeys:
|
|
188
149
|
@patch('sys.stdout', new_callable=StringIO)
|
|
189
|
-
def
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
"unknown_key": "value"
|
|
194
|
-
}
|
|
195
|
-
known_fields = {"ext_code_dirs", "glossary_dir"}
|
|
196
|
-
|
|
197
|
-
result = handle_unrecognized_keys(data, known_fields)
|
|
198
|
-
|
|
199
|
-
assert "unknown_key" not in result
|
|
200
|
-
assert "ext_code_dirs" in result
|
|
201
|
-
assert "glossary_dir" in result
|
|
202
|
-
assert "Warning: unknown_key is not recognized as a valid configuration option." in mock_stdout.getvalue()
|
|
203
|
-
|
|
204
|
-
def test_handle_no_unrecognized_keys(self):
|
|
205
|
-
data = {
|
|
206
|
-
"ext_code_dirs": [],
|
|
207
|
-
"glossary_dir": "./glossary"
|
|
208
|
-
}
|
|
209
|
-
known_fields = {"ext_code_dirs", "glossary_dir"}
|
|
210
|
-
|
|
211
|
-
result = handle_unrecognized_keys(data, known_fields)
|
|
212
|
-
assert result == data
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
class TestFixLLMTemperatures:
|
|
216
|
-
@patch('sys.stdout', new_callable=StringIO)
|
|
217
|
-
def test_fix_invalid_temperature_too_high(self, mock_stdout):
|
|
218
|
-
data = {
|
|
219
|
-
"llm_config": {
|
|
220
|
-
"gpt-4o": {
|
|
221
|
-
"temperature": 1.5
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
result = fix_llm_temperatures(data)
|
|
227
|
-
|
|
228
|
-
assert result["llm_config"]["gpt-4o"]["temperature"] == 0.8
|
|
229
|
-
assert "Warning: Temperature for model 'gpt-4o' is outside the 0.0 to 1.0 range" in mock_stdout.getvalue()
|
|
230
|
-
|
|
231
|
-
@patch('sys.stdout', new_callable=StringIO)
|
|
232
|
-
def test_fix_invalid_temperature_too_low(self, mock_stdout):
|
|
233
|
-
data = {
|
|
234
|
-
"llm_config": {
|
|
235
|
-
"gpt-4o": {
|
|
236
|
-
"temperature": -0.5
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
result = fix_llm_temperatures(data)
|
|
150
|
+
def test_removes_unrecognized_keys_and_warns(self, mock_stdout):
|
|
151
|
+
"""Tests that unknown keys are removed and a warning is printed."""
|
|
152
|
+
data = {"glossary_dir": "./glossary", "unknown_key": "some_value"}
|
|
153
|
+
cleaned_data = handle_unrecognized_keys(data)
|
|
242
154
|
|
|
243
|
-
assert
|
|
244
|
-
assert "
|
|
245
|
-
|
|
246
|
-
def test_valid_temperature_not_changed(self):
|
|
247
|
-
data = {
|
|
248
|
-
"llm_config": {
|
|
249
|
-
"gpt-4o": {
|
|
250
|
-
"temperature": 0.7
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
result = fix_llm_temperatures(data)
|
|
256
|
-
assert result["llm_config"]["gpt-4o"]["temperature"] == 0.7
|
|
155
|
+
assert "unknown_key" not in cleaned_data
|
|
156
|
+
assert "glossary_dir" in cleaned_data
|
|
157
|
+
assert "Warning: Unrecognized configuration key 'unknown_key' will be ignored." in mock_stdout.getvalue()
|
|
257
158
|
|
|
258
|
-
def test_no_llm_config(self):
|
|
259
|
-
data = {"other_field": "value"}
|
|
260
|
-
result = fix_llm_temperatures(data)
|
|
261
|
-
assert result == data
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
class TestValidateAndFixConfigData:
|
|
265
159
|
@patch('sys.stdout', new_callable=StringIO)
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
result = validate_and_fix_config_data("config.json")
|
|
160
|
+
def test_no_action_for_valid_data(self, mock_stdout):
|
|
161
|
+
"""Tests that no changes are made when there are no unrecognized keys."""
|
|
162
|
+
data = {"glossary_dir": "./glossary", "doc_dir": "./docs"}
|
|
163
|
+
cleaned_data = handle_unrecognized_keys(data)
|
|
272
164
|
|
|
273
|
-
assert
|
|
274
|
-
assert "
|
|
275
|
-
assert "Warning: unknown_key is not recognized as a valid configuration option." in mock_stdout.getvalue()
|
|
276
|
-
|
|
277
|
-
@patch('sys.stdout', new_callable=StringIO)
|
|
278
|
-
@patch("builtins.open", mock_open(read_data="invalid json"))
|
|
279
|
-
def test_invalid_json(self, mock_stdout):
|
|
280
|
-
result = validate_and_fix_config_data("config.json")
|
|
281
|
-
|
|
282
|
-
assert result == {}
|
|
283
|
-
assert "Error: Invalid JSON in configuration file:" in mock_stdout.getvalue()
|
|
284
|
-
assert "Creating new configuration with defaults..." in mock_stdout.getvalue()
|
|
285
|
-
|
|
286
|
-
@patch('sys.stdout', new_callable=StringIO)
|
|
287
|
-
@patch("builtins.open", side_effect=IOError("File not found"))
|
|
288
|
-
def test_file_read_error(self, mock_file, mock_stdout):
|
|
289
|
-
result = validate_and_fix_config_data("config.json")
|
|
290
|
-
|
|
291
|
-
assert result == {}
|
|
292
|
-
assert "Error reading configuration file: File not found" in mock_stdout.getvalue()
|
|
293
|
-
|
|
294
|
-
@patch('sys.stdout', new_callable=StringIO)
|
|
295
|
-
@patch("builtins.open")
|
|
296
|
-
def test_fix_invalid_temperatures(self, mock_file, mock_stdout, valid_config_dict):
|
|
297
|
-
valid_config_dict["llm_config"]["gpt-4o"]["temperature"] = 2.0
|
|
298
|
-
mock_file.return_value = mock_open(read_data=json.dumps(valid_config_dict))()
|
|
299
|
-
|
|
300
|
-
result = validate_and_fix_config_data("config.json")
|
|
301
|
-
|
|
302
|
-
assert result["llm_config"]["gpt-4o"]["temperature"] == 0.8
|
|
303
|
-
assert "Warning: Temperature for model 'gpt-4o' is outside the 0.0 to 1.0 range" in mock_stdout.getvalue()
|
|
165
|
+
assert cleaned_data == data
|
|
166
|
+
assert mock_stdout.getvalue() == ""
|
|
304
167
|
|
|
168
|
+
# --- Test Core I/O and Logic ---
|
|
305
169
|
|
|
306
170
|
class TestSaveData:
|
|
307
171
|
@patch("builtins.open", new_callable=mock_open)
|
|
308
|
-
def
|
|
172
|
+
def test_save_data_writes_correct_json(self, mock_file, default_config_data):
|
|
173
|
+
"""Tests that the config is correctly serialized to a JSON file."""
|
|
309
174
|
config = ARAconfig()
|
|
310
|
-
|
|
311
175
|
save_data("config.json", config)
|
|
312
|
-
|
|
176
|
+
|
|
313
177
|
mock_file.assert_called_once_with("config.json", "w", encoding="utf-8")
|
|
314
|
-
# Check that json.dump was called with correct data
|
|
315
178
|
handle = mock_file()
|
|
316
179
|
written_data = ''.join(call.args[0] for call in handle.write.call_args_list)
|
|
180
|
+
|
|
317
181
|
assert json.loads(written_data) == default_config_data
|
|
318
182
|
|
|
319
183
|
|
|
@@ -322,26 +186,53 @@ class TestReadData:
|
|
|
322
186
|
@patch('ara_cli.ara_config.save_data')
|
|
323
187
|
@patch('ara_cli.ara_config.ensure_directory_exists')
|
|
324
188
|
@patch('ara_cli.ara_config.exists', return_value=False)
|
|
325
|
-
def
|
|
189
|
+
def test_file_not_found_creates_default_and_exits(self, mock_exists, mock_ensure_dir, mock_save, mock_stdout):
|
|
190
|
+
"""Tests that a default config is created and the program exits if no config file is found."""
|
|
326
191
|
with pytest.raises(SystemExit) as exc_info:
|
|
327
|
-
read_data.cache_clear()
|
|
192
|
+
read_data.cache_clear()
|
|
328
193
|
read_data("config.json")
|
|
329
|
-
|
|
194
|
+
|
|
330
195
|
assert exc_info.value.code == 0
|
|
196
|
+
mock_ensure_dir.assert_called_once_with(os.path.dirname("config.json"))
|
|
331
197
|
mock_save.assert_called_once()
|
|
332
|
-
|
|
198
|
+
|
|
199
|
+
output = mock_stdout.getvalue()
|
|
200
|
+
assert "Configuration file not found. Creating a default one at 'config.json'." in output
|
|
201
|
+
assert "Please review the default configuration and re-run your command." in output
|
|
333
202
|
|
|
334
203
|
@patch('ara_cli.ara_config.save_data')
|
|
335
204
|
@patch('builtins.open')
|
|
336
205
|
@patch('ara_cli.ara_config.ensure_directory_exists')
|
|
337
206
|
@patch('ara_cli.ara_config.exists', return_value=True)
|
|
338
|
-
def
|
|
339
|
-
|
|
340
|
-
read_data.
|
|
207
|
+
def test_valid_config_is_loaded_and_resaved(self, mock_exists, mock_ensure_dir, mock_open_func, mock_save, valid_config_dict):
|
|
208
|
+
"""Tests that a valid config is loaded correctly and re-saved (to clean it)."""
|
|
209
|
+
m = mock_open(read_data=json.dumps(valid_config_dict))
|
|
210
|
+
mock_open_func.return_value = m()
|
|
211
|
+
read_data.cache_clear()
|
|
212
|
+
|
|
213
|
+
result = read_data("config.json")
|
|
214
|
+
|
|
215
|
+
assert isinstance(result, ARAconfig)
|
|
216
|
+
assert result.default_llm == "gpt-4o-custom"
|
|
217
|
+
mock_save.assert_called_once()
|
|
218
|
+
|
|
219
|
+
@patch('sys.stdout', new_callable=StringIO)
|
|
220
|
+
@patch('ara_cli.ara_config.save_data')
|
|
221
|
+
@patch('builtins.open', new_callable=mock_open, read_data="this is not json")
|
|
222
|
+
@patch('ara_cli.ara_config.ensure_directory_exists')
|
|
223
|
+
@patch('ara_cli.ara_config.exists', return_value=True)
|
|
224
|
+
def test_invalid_json_creates_default_config(self, mock_exists, mock_ensure_dir, mock_open_func, mock_save, mock_stdout):
|
|
225
|
+
"""Tests that a JSON decoding error results in a new default configuration."""
|
|
226
|
+
read_data.cache_clear()
|
|
341
227
|
|
|
342
228
|
result = read_data("config.json")
|
|
343
229
|
|
|
344
230
|
assert isinstance(result, ARAconfig)
|
|
231
|
+
assert result.default_llm == "gpt-5" # Should be the default config
|
|
232
|
+
|
|
233
|
+
output = mock_stdout.getvalue()
|
|
234
|
+
assert "Error: Invalid JSON in configuration file" in output
|
|
235
|
+
assert "Creating a new configuration with defaults..." in output
|
|
345
236
|
mock_save.assert_called_once()
|
|
346
237
|
|
|
347
238
|
@patch('sys.stdout', new_callable=StringIO)
|
|
@@ -349,95 +240,97 @@ class TestReadData:
|
|
|
349
240
|
@patch('builtins.open')
|
|
350
241
|
@patch('ara_cli.ara_config.ensure_directory_exists')
|
|
351
242
|
@patch('ara_cli.ara_config.exists', return_value=True)
|
|
352
|
-
def
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
243
|
+
def test_config_with_validation_errors_is_fixed(self, mock_exists, mock_ensure_dir, mock_open_func, mock_save, mock_stdout, corrupted_config_dict):
|
|
244
|
+
"""Tests that a config with invalid fields is automatically corrected to defaults."""
|
|
245
|
+
m = mock_open(read_data=json.dumps(corrupted_config_dict))
|
|
246
|
+
mock_open_func.return_value = m()
|
|
247
|
+
read_data.cache_clear()
|
|
356
248
|
|
|
249
|
+
defaults = ARAconfig()
|
|
357
250
|
result = read_data("config.json")
|
|
358
|
-
|
|
251
|
+
|
|
359
252
|
assert isinstance(result, ARAconfig)
|
|
253
|
+
assert result.ext_code_dirs == defaults.ext_code_dirs
|
|
254
|
+
assert result.glossary_dir == defaults.glossary_dir
|
|
255
|
+
assert result.llm_config == defaults.llm_config
|
|
256
|
+
assert result.default_llm == defaults.default_llm
|
|
257
|
+
|
|
360
258
|
output = mock_stdout.getvalue()
|
|
361
|
-
|
|
362
|
-
assert
|
|
363
|
-
|
|
364
|
-
|
|
259
|
+
assert "--- Configuration Error Detected ---" in output
|
|
260
|
+
assert "-> Field 'ext_code_dirs' is invalid and will be reverted to its default value." in output
|
|
261
|
+
assert "-> Field 'glossary_dir' is invalid and will be reverted to its default value." in output
|
|
262
|
+
assert "-> Field 'llm_config' is invalid and will be reverted to its default value." in output
|
|
263
|
+
assert "Configuration has been corrected and saved" in output
|
|
264
|
+
|
|
265
|
+
mock_save.assert_called_once_with("config.json", result)
|
|
365
266
|
|
|
366
267
|
@patch('sys.stdout', new_callable=StringIO)
|
|
367
268
|
@patch('ara_cli.ara_config.save_data')
|
|
368
269
|
@patch('builtins.open')
|
|
369
270
|
@patch('ara_cli.ara_config.ensure_directory_exists')
|
|
370
271
|
@patch('ara_cli.ara_config.exists', return_value=True)
|
|
371
|
-
def
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
"glossary_dir": "./custom
|
|
375
|
-
"
|
|
376
|
-
"
|
|
272
|
+
def test_preserves_valid_fields_when_fixing_errors(self, mock_exists, mock_ensure_dir, mock_open_func, mock_save, mock_stdout):
|
|
273
|
+
"""Tests that valid, non-default values are preserved during a fix."""
|
|
274
|
+
mixed_config = {
|
|
275
|
+
"glossary_dir": "./my-custom-glossary", # Valid, non-default
|
|
276
|
+
"default_llm": 12345, # Invalid type
|
|
277
|
+
"unrecognized_key": "will_be_ignored" # Unrecognized
|
|
377
278
|
}
|
|
279
|
+
m = mock_open(read_data=json.dumps(mixed_config))
|
|
280
|
+
mock_open_func.return_value = m()
|
|
281
|
+
read_data.cache_clear()
|
|
378
282
|
|
|
379
|
-
|
|
380
|
-
read_data.cache_clear() # Clear cache
|
|
381
|
-
|
|
283
|
+
defaults = ARAconfig()
|
|
382
284
|
result = read_data("config.json")
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
assert result.ext_code_dirs == "invalid" # The invalid value is preserved
|
|
388
|
-
assert result.glossary_dir == "./custom/glossary"
|
|
389
|
-
assert result.doc_dir == "./custom/docs"
|
|
390
|
-
|
|
285
|
+
|
|
286
|
+
assert result.glossary_dir == "./my-custom-glossary"
|
|
287
|
+
assert result.default_llm == defaults.default_llm
|
|
288
|
+
|
|
391
289
|
output = mock_stdout.getvalue()
|
|
392
|
-
assert "
|
|
393
|
-
assert "
|
|
290
|
+
assert "Warning: Unrecognized configuration key 'unrecognized_key' will be ignored." in output
|
|
291
|
+
assert "-> Field 'default_llm' is invalid" in output
|
|
292
|
+
assert "-> Field 'glossary_dir' is invalid" not in output
|
|
394
293
|
|
|
294
|
+
mock_save.assert_called_once()
|
|
295
|
+
saved_config = mock_save.call_args[0][1]
|
|
296
|
+
assert saved_config.glossary_dir == "./my-custom-glossary"
|
|
297
|
+
assert saved_config.default_llm == defaults.default_llm
|
|
298
|
+
|
|
299
|
+
# --- Test Singleton Manager ---
|
|
395
300
|
|
|
396
301
|
class TestConfigManager:
|
|
397
302
|
@patch('ara_cli.ara_config.read_data')
|
|
398
|
-
def
|
|
399
|
-
|
|
400
|
-
mock_read.return_value =
|
|
303
|
+
def test_get_config_is_singleton(self, mock_read):
|
|
304
|
+
"""Tests that get_config returns the same instance on subsequent calls."""
|
|
305
|
+
mock_read.return_value = MagicMock(spec=ARAconfig)
|
|
401
306
|
|
|
402
|
-
# First call
|
|
403
307
|
config1 = ConfigManager.get_config()
|
|
404
|
-
assert config1 == mock_config
|
|
405
|
-
mock_read.assert_called_once()
|
|
406
|
-
|
|
407
|
-
# Second call should return cached instance
|
|
408
308
|
config2 = ConfigManager.get_config()
|
|
409
|
-
|
|
410
|
-
|
|
309
|
+
|
|
310
|
+
assert config1 is config2
|
|
311
|
+
mock_read.assert_called_once()
|
|
411
312
|
|
|
412
313
|
@patch('ara_cli.ara_config.read_data')
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
def test_get_config_creates_directory_if_not_exists(self, mock_exists, mock_makedirs, mock_read):
|
|
314
|
+
def test_reset_clears_instance_and_caches(self, mock_read):
|
|
315
|
+
"""Tests that the reset method clears the instance and underlying caches."""
|
|
416
316
|
mock_read.return_value = MagicMock(spec=ARAconfig)
|
|
417
|
-
|
|
418
|
-
ConfigManager.get_config("./custom/config.json")
|
|
419
|
-
mock_makedirs.assert_called_once_with("./custom")
|
|
420
317
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
mock_config = MagicMock(spec=ARAconfig)
|
|
424
|
-
mock_read.return_value = mock_config
|
|
425
|
-
|
|
426
|
-
# Get config
|
|
427
|
-
config1 = ConfigManager.get_config()
|
|
428
|
-
assert ConfigManager._config_instance is not None
|
|
318
|
+
ConfigManager.get_config()
|
|
319
|
+
mock_read.assert_called_once()
|
|
429
320
|
|
|
430
|
-
# Reset
|
|
431
321
|
ConfigManager.reset()
|
|
432
322
|
assert ConfigManager._config_instance is None
|
|
433
323
|
mock_read.cache_clear.assert_called_once()
|
|
434
324
|
|
|
325
|
+
ConfigManager.get_config()
|
|
326
|
+
assert mock_read.call_count == 2 # Called again after reset
|
|
327
|
+
|
|
435
328
|
@patch('ara_cli.ara_config.read_data')
|
|
436
|
-
def
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
329
|
+
def test_get_config_with_custom_filepath(self, mock_read):
|
|
330
|
+
"""Tests that get_config can be called with a custom file path."""
|
|
331
|
+
mock_read.return_value = MagicMock(spec=ARAconfig)
|
|
332
|
+
custom_path = "/custom/path/config.json"
|
|
333
|
+
|
|
334
|
+
ConfigManager.get_config(custom_path)
|
|
440
335
|
|
|
441
|
-
|
|
442
|
-
mock_read.assert_called_once_with(custom_path)
|
|
443
|
-
assert config == mock_config
|
|
336
|
+
mock_read.assert_called_once_with(custom_path)
|