File size: 4,551 Bytes
ea8c728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Tests for template_dir path resolution
"""

import unittest
import tempfile
import yaml
from pathlib import Path

from openevolve.config import Config


class TestTemplateDirResolution(unittest.TestCase):
    """Test that template_dir paths are resolved relative to config file location"""

    def test_relative_template_dir_resolved_to_config_location(self):
        """Relative paths in template_dir should resolve relative to config file"""
        # Create a temporary directory structure
        with tempfile.TemporaryDirectory() as tmpdir:
            config_dir = Path(tmpdir) / "config_subdir"
            config_dir.mkdir()
            config_file = config_dir / "test_config.yaml"

            # Write config with relative template_dir
            config_data = {
                "prompt": {"template_dir": "templates"},
                "llm": {"models": [{"name": "gpt-4", "weight": 1.0}]},
            }
            with open(config_file, "w") as f:
                yaml.dump(config_data, f)

            # Load config
            config = Config.from_yaml(config_file)

            # Template_dir should be resolved relative to config file location
            expected_path = str((config_dir / "templates").resolve())
            self.assertEqual(config.prompt.template_dir, expected_path)

    def test_absolute_template_dir_unchanged(self):
        """Absolute paths in template_dir should remain unchanged"""
        with tempfile.TemporaryDirectory() as tmpdir:
            config_file = Path(tmpdir) / "test_config.yaml"
            absolute_template_path = "/absolute/path/to/templates"

            # Write config with absolute template_dir
            config_data = {
                "prompt": {"template_dir": absolute_template_path},
                "llm": {"models": [{"name": "gpt-4", "weight": 1.0}]},
            }
            with open(config_file, "w") as f:
                yaml.dump(config_data, f)

            # Load config
            config = Config.from_yaml(config_file)

            # Absolute path should remain unchanged
            self.assertEqual(config.prompt.template_dir, absolute_template_path)

    def test_null_template_dir_unchanged(self):
        """Null template_dir should remain None"""
        with tempfile.TemporaryDirectory() as tmpdir:
            config_file = Path(tmpdir) / "test_config.yaml"

            # Write config with null template_dir
            config_data = {
                "prompt": {"template_dir": None},
                "llm": {"models": [{"name": "gpt-4", "weight": 1.0}]},
            }
            with open(config_file, "w") as f:
                yaml.dump(config_data, f)

            # Load config
            config = Config.from_yaml(config_file)

            # None should remain None
            self.assertIsNone(config.prompt.template_dir)

    def test_nested_relative_template_dir(self):
        """Nested relative paths should resolve correctly"""
        with tempfile.TemporaryDirectory() as tmpdir:
            config_dir = Path(tmpdir) / "configs"
            config_dir.mkdir()
            config_file = config_dir / "test_config.yaml"

            # Write config with nested relative path
            config_data = {
                "prompt": {"template_dir": "../templates/custom"},
                "llm": {"models": [{"name": "gpt-4", "weight": 1.0}]},
            }
            with open(config_file, "w") as f:
                yaml.dump(config_data, f)

            # Load config
            config = Config.from_yaml(config_file)

            # Should resolve to <tmpdir>/templates/custom
            expected_path = str((config_dir / "../templates/custom").resolve())
            self.assertEqual(config.prompt.template_dir, expected_path)

    def test_real_example_config(self):
        """Test with real example config file"""
        # This test uses the actual llm_prompt_optimization example
        config_path = "examples/llm_prompt_optimization/config.yaml"
        if not Path(config_path).exists():
            self.skipTest(f"Example config not found: {config_path}")

        config = Config.from_yaml(config_path)

        # Should resolve to examples/llm_prompt_optimization/templates
        expected_dir = Path("examples/llm_prompt_optimization/templates").resolve()
        actual_dir = Path(config.prompt.template_dir)

        self.assertEqual(actual_dir, expected_dir)
        # Verify the resolved path is absolute
        self.assertTrue(actual_dir.is_absolute())


if __name__ == "__main__":
    unittest.main()