|
1 | 1 | import os |
2 | 2 | import ssl |
3 | 3 | import sys |
| 4 | +import random |
4 | 5 | from unittest.mock import MagicMock, patch |
5 | 6 |
|
6 | 7 | import pytest |
@@ -148,19 +149,36 @@ def test_validator_type_label(self, mock_contract): |
148 | 149 |
|
149 | 150 |
|
150 | 151 | class TestGetLlmTee: |
151 | | - def test_returns_first_active_tee(self, mock_contract): |
| 152 | + def test_returns_active_tee_from_pool(self, mock_contract): |
| 153 | + """get_llm_tee uses random.choice; patch it to make the test deterministic.""" |
152 | 154 | registry, contract = mock_contract |
153 | 155 |
|
154 | | - contract.functions.getActiveTEEs.return_value.call.return_value = [ |
| 156 | + tee_infos = [ |
155 | 157 | _make_tee_info(endpoint="https://tee-1.example.com"), |
156 | 158 | _make_tee_info(endpoint="https://tee-2.example.com"), |
157 | 159 | ] |
| 160 | + contract.functions.getActiveTEEs.return_value.call.return_value = tee_infos |
158 | 161 |
|
159 | | - result = registry.get_llm_tee() |
| 162 | + with patch("src.opengradient.client.tee_registry.random.choice", side_effect=lambda seq: seq[0]): |
| 163 | + result = registry.get_llm_tee() |
160 | 164 |
|
161 | 165 | assert result is not None |
162 | 166 | assert result.endpoint == "https://tee-1.example.com" |
163 | 167 |
|
| 168 | + def test_returns_any_active_tee(self, mock_contract): |
| 169 | + """Without patching random.choice, result must still be one of the active TEEs.""" |
| 170 | + registry, contract = mock_contract |
| 171 | + |
| 172 | + endpoints = {"https://tee-1.example.com", "https://tee-2.example.com"} |
| 173 | + contract.functions.getActiveTEEs.return_value.call.return_value = [ |
| 174 | + _make_tee_info(endpoint=ep) for ep in sorted(endpoints) |
| 175 | + ] |
| 176 | + |
| 177 | + result = registry.get_llm_tee() |
| 178 | + |
| 179 | + assert result is not None |
| 180 | + assert result.endpoint in endpoints |
| 181 | + |
164 | 182 | def test_returns_none_when_no_tees(self, mock_contract): |
165 | 183 | registry, contract = mock_contract |
166 | 184 |
|
|
0 commit comments