test: fix test_list_models_with_direct_resource_patch to mock correct resource loading

This commit is contained in:
Paul Gauthier (aider) 2025-03-25 14:46:47 -10:00
parent 899972e22f
commit a6cbaad5a2

View file

@ -1164,16 +1164,26 @@ class TestMain(TestCase):
def test_list_models_with_direct_resource_patch(self):
# Test that models from resources/model-metadata.json are included in list-models output
with GitTemporaryDirectory():
# Mock the importlib.resources.open_text to return a custom model-metadata.json
# Create a temporary file with test model metadata
test_file = Path(self.tempdir) / "test-model-metadata.json"
test_resource_models = {
"special-model": {"max_input_tokens": 8192, "litellm_provider": "resource-provider"}
"special-model": {
"max_input_tokens": 8192,
"litellm_provider": "resource-provider",
"mode": "chat"
}
}
test_file.write_text(json.dumps(test_resource_models))
mock_file = MagicMock()
mock_file.read.return_value = json.dumps(test_resource_models)
mock_file.__enter__.return_value = mock_file
# Create a mock for the resource file path
mock_resource_path = MagicMock()
mock_resource_path.__str__.return_value = str(test_file)
with patch("importlib.resources.open_text", return_value=mock_file):
# Create a mock for the files function that returns an object with joinpath
mock_files = MagicMock()
mock_files.joinpath.return_value = mock_resource_path
with patch("aider.main.importlib_resources.files", return_value=mock_files):
# Capture stdout to check the output
with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
main(