File size: 10,877 Bytes
5e4510c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""
Integration tests for OpenEvolve library API with real LLM inference
Tests the end-to-end flow of using OpenEvolve as a library
"""

import pytest
import tempfile
import shutil
from pathlib import Path

from openevolve import run_evolution, evolve_function, evolve_code, evolve_algorithm
from openevolve.config import Config, LLMModelConfig


def _get_library_test_config(port: int = 8000) -> Config:
    """Get config for library API tests with optillm server"""
    config = Config()
    config.max_iterations = 100
    config.checkpoint_interval = 1
    config.database.in_memory = True
    config.evaluator.cascade_evaluation = False
    config.evaluator.parallel_evaluations = 1
    config.evaluator.timeout = 60
    
    # Configure to use optillm server
    base_url = f"http://localhost:{port}/v1"
    config.llm.api_base = base_url
    config.llm.timeout = 120
    config.llm.retries = 0
    config.llm.models = [
        LLMModelConfig(
            name="google/gemma-3-270m-it",
            api_key="optillm",
            api_base=base_url,
            weight=1.0,
            timeout=120,
            retries=0
        )
    ]
    return config


class TestLibraryAPIIntegration:
    """Test OpenEvolve library API with real LLM integration"""

    @pytest.mark.slow
    def test_evolve_function_real_integration(
        self,
        optillm_server,
        temp_workspace
    ):
        """Test evolve_function with real optillm server - simple optimization task"""
        
        def simple_multiply(x, y):
            """A simple function that can be optimized"""
            # Inefficient implementation that can be improved
            result = 0
            for i in range(x):
                result += y
            return result
        
        # Test cases - the function should return x * y
        test_cases = [
            ((2, 3), 6),
            ((4, 5), 20),
            ((1, 7), 7),
            ((0, 10), 0)
        ]
        
        print("Testing evolve_function with real LLM...")
        
        # Run evolution with minimal iterations for testing
        result = evolve_function(
            simple_multiply,
            test_cases,
            iterations=2,  # Very small number for CI speed
            output_dir=str(temp_workspace / "evolve_function_output"),
            cleanup=False,  # Keep files for inspection
            config=_get_library_test_config(optillm_server['port'])
        )
        
        # Verify the result structure
        assert result is not None
        assert hasattr(result, 'best_score')
        assert hasattr(result, 'best_code')
        assert hasattr(result, 'metrics')
        assert hasattr(result, 'output_dir')
        
        # Basic checks
        assert result.best_score >= 0.0
        assert "def simple_multiply" in result.best_code
        assert result.output_dir == str(temp_workspace / "evolve_function_output")
        
        # Check that output directory was created
        output_path = Path(result.output_dir)
        assert output_path.exists()
        assert (output_path / "best").exists()
        
        print(f"✅ evolve_function completed successfully!")
        print(f"   Best score: {result.best_score}")
        print(f"   Output dir: {result.output_dir}")
        print(f"   Code length: {len(result.best_code)} chars")

    @pytest.mark.slow
    def test_evolve_code_real_integration(
        self,
        optillm_server,
        temp_workspace
    ):
        """Test evolve_code with real optillm server - code string optimization"""
        
        # Initial code that can be optimized
        initial_code = """
# EVOLVE-BLOCK-START
def fibonacci(n):
    # Inefficient recursive implementation
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)
# EVOLVE-BLOCK-END
"""
        
        def fibonacci_evaluator(program_path):
            """Simple evaluator for fibonacci function"""
            try:
                # Import the evolved program
                import importlib.util
                spec = importlib.util.spec_from_file_location("evolved", program_path)
                module = importlib.util.module_from_spec(spec)
                spec.loader.exec_module(module)
                
                # Test the function
                if hasattr(module, 'fibonacci'):
                    fib = module.fibonacci
                    
                    # Test cases
                    test_cases = [
                        (0, 0), (1, 1), (2, 1), (3, 2), (4, 3), (5, 5)
                    ]
                    
                    correct = 0
                    for input_val, expected in test_cases:
                        try:
                            result = fib(input_val)
                            if result == expected:
                                correct += 1
                        except:
                            pass
                    
                    accuracy = correct / len(test_cases)
                    return {
                        "score": accuracy,
                        "correctness": accuracy,
                        "test_cases_passed": correct,
                        "combined_score": accuracy  # Use accuracy as combined score
                    }
                else:
                    return {"score": 0.0, "error": "fibonacci function not found"}
                    
            except Exception as e:
                return {"score": 0.0, "error": str(e)}
        
        print("Testing evolve_code with real LLM...")
        
        # Run evolution
        result = evolve_code(
            initial_code,
            fibonacci_evaluator,
            iterations=1,  # Minimal for CI speed
            output_dir=str(temp_workspace / "evolve_code_output"),
            cleanup=False,  # Keep output directory
            config=_get_library_test_config(optillm_server['port'])
        )
        
        # Verify result structure
        assert result is not None
        assert result.best_score >= 0.0
        assert "fibonacci" in result.best_code.lower()
        assert "# EVOLVE-BLOCK-START" in result.best_code
        assert "# EVOLVE-BLOCK-END" in result.best_code
        
        # Check output directory
        output_path = Path(result.output_dir)
        assert output_path.exists()
        
        print(f"✅ evolve_code completed successfully!")
        print(f"   Best score: {result.best_score}")
        print(f"   Output dir: {result.output_dir}")

    @pytest.mark.slow
    def test_run_evolution_real_integration(
        self,
        optillm_server,
        temp_workspace
    ):
        """Test run_evolution with real optillm server - basic program evolution"""
        
        # Create initial program file
        initial_program = temp_workspace / "initial_program.py"
        initial_program.write_text("""
# Simple sorting program to evolve
# EVOLVE-BLOCK-START
def sort_numbers(numbers):
    # Basic bubble sort implementation
    n = len(numbers)
    for i in range(n):
        for j in range(0, n - i - 1):
            if numbers[j] > numbers[j + 1]:
                numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
    return numbers
# EVOLVE-BLOCK-END
""")
        
        # Create evaluator file
        evaluator_file = temp_workspace / "evaluator.py"
        evaluator_file.write_text("""
def evaluate(program_path):
    \"\"\"Evaluate sorting function performance\"\"\"
    try:
        import importlib.util
        spec = importlib.util.spec_from_file_location("program", program_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        
        if hasattr(module, 'sort_numbers'):
            sort_func = module.sort_numbers
            
            # Test cases
            test_cases = [
                [3, 1, 4, 1, 5],
                [9, 2, 6, 5, 3],
                [1],
                [],
                [2, 1]
            ]
            
            correct = 0
            for test_case in test_cases:
                try:
                    input_copy = test_case.copy()
                    result = sort_func(input_copy)
                    expected = sorted(test_case)
                    if result == expected:
                        correct += 1
                except:
                    pass
            
            accuracy = correct / len(test_cases) if test_cases else 0
            return {
                "score": accuracy,
                "correctness": accuracy,
                "complexity": 10,  # Fixed complexity for simplicity
                "combined_score": accuracy  # Use accuracy as combined score
            }
        else:
            return {"score": 0.0, "error": "sort_numbers function not found"}
            
    except Exception as e:
        return {"score": 0.0, "error": str(e)}
""")
        
        print("Testing run_evolution with real LLM...")
        
        # Run evolution using file paths (most common usage)
        result = run_evolution(
            initial_program=str(initial_program),
            evaluator=str(evaluator_file),
            iterations=1,  # Minimal for CI speed
            output_dir=str(temp_workspace / "run_evolution_output"),
            cleanup=False,  # Keep output directory
            config=_get_library_test_config(optillm_server['port'])
        )
        
        # Verify result
        assert result is not None
        assert result.best_score >= 0.0
        assert "sort_numbers" in result.best_code
        
        # Check that files were created
        output_path = Path(result.output_dir)
        assert output_path.exists()
        assert (output_path / "best").exists()
        assert (output_path / "checkpoints").exists()
        
        print(f"✅ run_evolution completed successfully!")
        print(f"   Best score: {result.best_score}")
        print(f"   Output dir: {result.output_dir}")
        
        # Test string input as well
        print("Testing run_evolution with string inputs...")
        
        result2 = run_evolution(
            initial_program=initial_program.read_text(),
            evaluator=lambda path: {"score": 0.8, "test": "passed"},  # Simple callable evaluator
            iterations=1,
            output_dir=str(temp_workspace / "run_evolution_string_output"),
            cleanup=False,  # Keep output directory
            config=_get_library_test_config(optillm_server['port'])
        )
        
        assert result2 is not None
        assert result2.best_score >= 0.0
        
        print(f"✅ run_evolution with string inputs completed!")


@pytest.fixture
def temp_workspace():
    """Create a temporary workspace for integration tests"""
    temp_dir = tempfile.mkdtemp()
    workspace = Path(temp_dir)
    yield workspace
    shutil.rmtree(temp_dir, ignore_errors=True)