|
| 1 | +"""Tests to verify API keys are correctly passed through the system and sent to the API.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from unittest.mock import patch, MagicMock |
| 5 | +from rich.console import Console |
| 6 | + |
| 7 | +from weco.api import start_optimization_run, evaluate_feedback_then_suggest_next_solution |
| 8 | + |
| 9 | + |
| 10 | +class TestApiKeysInStartOptimizationRun: |
| 11 | + """Test that api_keys are correctly included in start_optimization_run requests.""" |
| 12 | + |
| 13 | + @pytest.fixture |
| 14 | + def mock_console(self): |
| 15 | + """Create a mock console for testing.""" |
| 16 | + return MagicMock(spec=Console) |
| 17 | + |
| 18 | + @pytest.fixture |
| 19 | + def base_params(self, mock_console): |
| 20 | + """Base parameters for start_optimization_run.""" |
| 21 | + return { |
| 22 | + "console": mock_console, |
| 23 | + "source_code": "print('hello')", |
| 24 | + "source_path": "test.py", |
| 25 | + "evaluation_command": "python test.py", |
| 26 | + "metric_name": "accuracy", |
| 27 | + "maximize": True, |
| 28 | + "steps": 10, |
| 29 | + "code_generator_config": {"model": "o4-mini"}, |
| 30 | + "evaluator_config": {"model": "o4-mini"}, |
| 31 | + "search_policy_config": {"num_drafts": 2}, |
| 32 | + } |
| 33 | + |
| 34 | + @patch("weco.api.requests.post") |
| 35 | + def test_api_keys_included_in_request(self, mock_post, base_params): |
| 36 | + """Test that api_keys are included in the request JSON when provided.""" |
| 37 | + mock_response = MagicMock() |
| 38 | + mock_response.json.return_value = { |
| 39 | + "run_id": "test-run-id", |
| 40 | + "solution_id": "test-solution-id", |
| 41 | + "code": "print('hello')", |
| 42 | + "plan": "test plan", |
| 43 | + } |
| 44 | + mock_response.raise_for_status = MagicMock() |
| 45 | + mock_post.return_value = mock_response |
| 46 | + |
| 47 | + api_keys = {"openai": "sk-test-key", "anthropic": "sk-ant-test"} |
| 48 | + start_optimization_run(**base_params, api_keys=api_keys) |
| 49 | + |
| 50 | + # Verify the request was made with api_keys in the JSON payload |
| 51 | + mock_post.assert_called_once() |
| 52 | + call_kwargs = mock_post.call_args |
| 53 | + request_json = call_kwargs.kwargs["json"] |
| 54 | + assert "api_keys" in request_json |
| 55 | + assert request_json["api_keys"] == {"openai": "sk-test-key", "anthropic": "sk-ant-test"} |
| 56 | + |
| 57 | + @patch("weco.api.requests.post") |
| 58 | + def test_api_keys_not_included_when_none(self, mock_post, base_params): |
| 59 | + """Test that api_keys field is not included when api_keys is None.""" |
| 60 | + mock_response = MagicMock() |
| 61 | + mock_response.json.return_value = { |
| 62 | + "run_id": "test-run-id", |
| 63 | + "solution_id": "test-solution-id", |
| 64 | + "code": "print('hello')", |
| 65 | + "plan": "test plan", |
| 66 | + } |
| 67 | + mock_response.raise_for_status = MagicMock() |
| 68 | + mock_post.return_value = mock_response |
| 69 | + |
| 70 | + start_optimization_run(**base_params, api_keys=None) |
| 71 | + |
| 72 | + # Verify the request was made without api_keys |
| 73 | + mock_post.assert_called_once() |
| 74 | + call_kwargs = mock_post.call_args |
| 75 | + request_json = call_kwargs.kwargs["json"] |
| 76 | + assert "api_keys" not in request_json |
| 77 | + |
| 78 | + @patch("weco.api.requests.post") |
| 79 | + def test_api_keys_not_included_when_empty_dict(self, mock_post, base_params): |
| 80 | + """Test that api_keys field is not included when api_keys is an empty dict.""" |
| 81 | + mock_response = MagicMock() |
| 82 | + mock_response.json.return_value = { |
| 83 | + "run_id": "test-run-id", |
| 84 | + "solution_id": "test-solution-id", |
| 85 | + "code": "print('hello')", |
| 86 | + "plan": "test plan", |
| 87 | + } |
| 88 | + mock_response.raise_for_status = MagicMock() |
| 89 | + mock_post.return_value = mock_response |
| 90 | + |
| 91 | + # Empty dict is falsy, so api_keys should not be included |
| 92 | + start_optimization_run(**base_params, api_keys={}) |
| 93 | + |
| 94 | + mock_post.assert_called_once() |
| 95 | + call_kwargs = mock_post.call_args |
| 96 | + request_json = call_kwargs.kwargs["json"] |
| 97 | + assert "api_keys" not in request_json |
| 98 | + |
| 99 | + |
| 100 | +class TestApiKeysInEvaluateFeedbackThenSuggest: |
| 101 | + """Test that api_keys are correctly included in evaluate_feedback_then_suggest_next_solution requests.""" |
| 102 | + |
| 103 | + @pytest.fixture |
| 104 | + def mock_console(self): |
| 105 | + """Create a mock console for testing.""" |
| 106 | + return MagicMock(spec=Console) |
| 107 | + |
| 108 | + @patch("weco.api.requests.post") |
| 109 | + def test_api_keys_included_in_suggest_request(self, mock_post, mock_console): |
| 110 | + """Test that api_keys are included in the suggest request JSON when provided.""" |
| 111 | + mock_response = MagicMock() |
| 112 | + mock_response.json.return_value = { |
| 113 | + "run_id": "test-run-id", |
| 114 | + "solution_id": "new-solution-id", |
| 115 | + "code": "print('improved')", |
| 116 | + "plan": "improvement plan", |
| 117 | + "is_done": False, |
| 118 | + } |
| 119 | + mock_response.raise_for_status = MagicMock() |
| 120 | + mock_post.return_value = mock_response |
| 121 | + |
| 122 | + api_keys = {"openai": "sk-test-key"} |
| 123 | + evaluate_feedback_then_suggest_next_solution( |
| 124 | + console=mock_console, |
| 125 | + run_id="test-run-id", |
| 126 | + step=1, |
| 127 | + execution_output="accuracy: 0.95", |
| 128 | + auth_headers={"Authorization": "Bearer test-token"}, |
| 129 | + api_keys=api_keys, |
| 130 | + ) |
| 131 | + |
| 132 | + mock_post.assert_called_once() |
| 133 | + call_kwargs = mock_post.call_args |
| 134 | + request_json = call_kwargs.kwargs["json"] |
| 135 | + assert "api_keys" in request_json |
| 136 | + assert request_json["api_keys"] == {"openai": "sk-test-key"} |
| 137 | + |
| 138 | + @patch("weco.api.requests.post") |
| 139 | + def test_api_keys_not_included_in_suggest_when_none(self, mock_post, mock_console): |
| 140 | + """Test that api_keys field is not included in suggest request when api_keys is None.""" |
| 141 | + mock_response = MagicMock() |
| 142 | + mock_response.json.return_value = { |
| 143 | + "run_id": "test-run-id", |
| 144 | + "solution_id": "new-solution-id", |
| 145 | + "code": "print('improved')", |
| 146 | + "plan": "improvement plan", |
| 147 | + "is_done": False, |
| 148 | + } |
| 149 | + mock_response.raise_for_status = MagicMock() |
| 150 | + mock_post.return_value = mock_response |
| 151 | + |
| 152 | + evaluate_feedback_then_suggest_next_solution( |
| 153 | + console=mock_console, |
| 154 | + run_id="test-run-id", |
| 155 | + step=1, |
| 156 | + execution_output="accuracy: 0.95", |
| 157 | + auth_headers={"Authorization": "Bearer test-token"}, |
| 158 | + api_keys=None, |
| 159 | + ) |
| 160 | + |
| 161 | + mock_post.assert_called_once() |
| 162 | + call_kwargs = mock_post.call_args |
| 163 | + request_json = call_kwargs.kwargs["json"] |
| 164 | + assert "api_keys" not in request_json |
| 165 | + |
| 166 | + @patch("weco.api.requests.post") |
| 167 | + def test_api_keys_not_included_in_suggest_when_empty_dict(self, mock_post, mock_console): |
| 168 | + """Test that api_keys field is not included in suggest request when api_keys is None.""" |
| 169 | + mock_response = MagicMock() |
| 170 | + mock_response.json.return_value = { |
| 171 | + "run_id": "test-run-id", |
| 172 | + "solution_id": "new-solution-id", |
| 173 | + "code": "print('improved')", |
| 174 | + "plan": "improvement plan", |
| 175 | + "is_done": False, |
| 176 | + } |
| 177 | + mock_response.raise_for_status = MagicMock() |
| 178 | + mock_post.return_value = mock_response |
| 179 | + |
| 180 | + evaluate_feedback_then_suggest_next_solution( |
| 181 | + console=mock_console, |
| 182 | + run_id="test-run-id", |
| 183 | + step=1, |
| 184 | + execution_output="accuracy: 0.95", |
| 185 | + auth_headers={"Authorization": "Bearer test-token"}, |
| 186 | + api_keys={}, |
| 187 | + ) |
| 188 | + |
| 189 | + mock_post.assert_called_once() |
| 190 | + call_kwargs = mock_post.call_args |
| 191 | + request_json = call_kwargs.kwargs["json"] |
| 192 | + assert "api_keys" not in request_json |
0 commit comments