File size: 9,881 Bytes
dbb04e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3a3710
dbb04e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

HAIM Test Suite — Configuration Tests

"""

import os
import tempfile
from pathlib import Path

import pytest
import yaml

from mnemocore.core.config import (
    HAIMConfig,
    load_config,
    get_config,
    reset_config,
    TierConfig,
    LTPConfig,
)
from mnemocore.core.exceptions import ConfigurationError


@pytest.fixture(autouse=True)
def clean_config():
    """Reset global config singleton between tests."""
    reset_config()
    yield
    reset_config()


@pytest.fixture
def sample_config_path(tmp_path):
    """Create a temporary config.yaml."""
    config_data = {
        "haim": {
            "version": "3.0-test",
            "dimensionality": 1024,  # Small for tests
            "encoding": {"mode": "binary", "token_method": "bundle"},
            "tiers": {
                "hot": {"max_memories": 100, "ltp_threshold_min": 0.7},
                "warm": {
                    "max_memories": 1000,
                    "ltp_threshold_min": 0.3,
                    "consolidation_interval_hours": 1,
                    "storage_backend": "mmap",
                },
                "cold": {
                    "max_memories": 0,
                    "ltp_threshold_min": 0.0,
                    "storage_backend": "filesystem",
                },
            },
            "ltp": {
                "initial_importance": 0.5,
                "decay_lambda": 0.01,
                "permanence_threshold": 0.95,
                "half_life_days": 30.0,
            },
            "hysteresis": {"promote_delta": 0.15, "demote_delta": 0.10},
            "redis": {"url": "redis://localhost:6379/0"},
            "qdrant": {"url": "http://localhost:6333"},
            "gpu": {"enabled": False},
            "observability": {"log_level": "DEBUG"},
            "paths": {"data_dir": str(tmp_path / "data")},
        }
    }
    config_path = tmp_path / "config.yaml"
    with open(config_path, "w") as f:
        yaml.dump(config_data, f)
    return config_path


class TestLoadConfig:
    def test_load_from_yaml(self, sample_config_path):
        config = load_config(sample_config_path)
        assert config.version == "3.0-test"
        assert config.dimensionality == 1024

    def test_default_values_when_no_file(self, tmp_path):
        missing_path = tmp_path / "nonexistent.yaml"
        config = load_config(missing_path)
        assert config.dimensionality == 16384
        assert config.version == "4.5"

    def test_dimensionality_must_be_multiple_of_64(self, tmp_path):
        bad_config = {"haim": {"dimensionality": 100}}
        path = tmp_path / "bad.yaml"
        with open(path, "w") as f:
            yaml.dump(bad_config, f)

        with pytest.raises(ConfigurationError, match="multiple of 64"):
            load_config(path)

    def test_encoding_mode(self, sample_config_path):
        config = load_config(sample_config_path)
        assert config.encoding.mode == "binary"
        assert config.encoding.token_method == "bundle"

    def test_tier_config(self, sample_config_path):
        config = load_config(sample_config_path)
        assert config.tiers_hot.max_memories == 100
        assert config.tiers_hot.ltp_threshold_min == 0.7
        assert config.tiers_warm.storage_backend == "mmap"
        assert config.tiers_warm.consolidation_interval_hours == 1

    def test_ltp_config(self, sample_config_path):
        config = load_config(sample_config_path)
        assert config.ltp.decay_lambda == 0.01
        assert config.ltp.permanence_threshold == 0.95

    def test_hysteresis_config(self, sample_config_path):
        config = load_config(sample_config_path)
        assert config.hysteresis.promote_delta == 0.15
        assert config.hysteresis.demote_delta == 0.10


class TestEnvironmentOverrides:
    def test_dimensionality_override(self, sample_config_path):
        os.environ["HAIM_DIMENSIONALITY"] = "2048"
        try:
            config = load_config(sample_config_path)
            assert config.dimensionality == 2048
        finally:
            del os.environ["HAIM_DIMENSIONALITY"]

    def test_redis_url_override(self, sample_config_path):
        os.environ["HAIM_REDIS_URL"] = "redis://custom:6380/1"
        try:
            config = load_config(sample_config_path)
            assert config.redis.url == "redis://custom:6380/1"
        finally:
            del os.environ["HAIM_REDIS_URL"]

    def test_gpu_enabled_override(self, sample_config_path):
        os.environ["HAIM_GPU_ENABLED"] = "true"
        try:
            config = load_config(sample_config_path)
            assert config.gpu.enabled is True
        finally:
            del os.environ["HAIM_GPU_ENABLED"]

    def test_log_level_override(self, sample_config_path):
        os.environ["HAIM_LOG_LEVEL"] = "WARNING"
        try:
            config = load_config(sample_config_path)
            assert config.observability.log_level == "WARNING"
        finally:
            del os.environ["HAIM_LOG_LEVEL"]

    def test_mcp_enabled_override(self, sample_config_path):
        os.environ["HAIM_MCP_ENABLED"] = "true"
        try:
            config = load_config(sample_config_path)
            assert config.mcp.enabled is True
        finally:
            del os.environ["HAIM_MCP_ENABLED"]

    def test_mcp_api_base_url_override(self, sample_config_path):
        os.environ["HAIM_MCP_API_BASE_URL"] = "http://localhost:8200"
        try:
            config = load_config(sample_config_path)
            assert config.mcp.api_base_url == "http://localhost:8200"
        finally:
            del os.environ["HAIM_MCP_API_BASE_URL"]


class TestConfigSingleton:
    def test_get_config_returns_same_instance(self):
        config_a = get_config()
        config_b = get_config()
        assert config_a is config_b

    def test_reset_clears_singleton(self):
        config_a = get_config()
        reset_config()
        config_b = get_config()
        # New instance after reset (they're equal but not same object)
        assert config_a is not config_b

    def test_config_is_frozen(self):
        config = get_config()
        with pytest.raises(AttributeError):
            config.dimensionality = 9999


class TestConfigValidation:
    def test_valid_dimensionalities(self, tmp_path):
        for dim in [64, 128, 1024, 16384]:
            data = {"haim": {"dimensionality": dim}}
            path = tmp_path / f"config_{dim}.yaml"
            with open(path, "w") as f:
                yaml.dump(data, f)
            config = load_config(path)
            assert config.dimensionality == dim

    def test_invalid_dimensionalities(self, tmp_path):
        for dim in [100, 1000, 10000, 15000]:
            data = {"haim": {"dimensionality": dim}}
            path = tmp_path / f"config_{dim}.yaml"
            with open(path, "w") as f:
                yaml.dump(data, f)
            with pytest.raises(ConfigurationError):
                load_config(path)

class TestSecurityOverrides:
    def test_redis_password_override(self, sample_config_path):
        os.environ["HAIM_REDIS_PASSWORD"] = "secret_password"
        try:
            config = load_config(sample_config_path)
            assert config.redis.password == "secret_password"
        finally:
            del os.environ["HAIM_REDIS_PASSWORD"]

    def test_qdrant_api_key_override(self, sample_config_path):
        os.environ["HAIM_QDRANT_API_KEY"] = "secret_api_key"
        try:
            config = load_config(sample_config_path)
            assert config.qdrant.api_key == "secret_api_key"
        finally:
            del os.environ["HAIM_QDRANT_API_KEY"]

    def test_config_file_values(self, tmp_path):
        """Test that values can also be loaded from yaml directly."""
        config_data = {
            "haim": {
                "dimensionality": 1024,
                "redis": {
                    "url": "redis://localhost:6379/0",
                    "password": "yaml_password"
                },
                "qdrant": {
                    "url": "http://localhost:6333",
                    "api_key": "yaml_api_key"
                }
            }
        }
        config_path = tmp_path / "config_security.yaml"
        with open(config_path, "w") as f:
            yaml.dump(config_data, f)

        config = load_config(config_path)
        assert config.redis.password == "yaml_password"
        assert config.qdrant.api_key == "yaml_api_key"


class TestMCPConfig:
    def test_mcp_defaults(self, sample_config_path):
        config = load_config(sample_config_path)
        assert config.mcp.enabled is False
        assert config.mcp.transport == "stdio"
        assert "memory_health" in config.mcp.allow_tools

    def test_mcp_config_file_values(self, tmp_path):
        config_data = {
            "haim": {
                "dimensionality": 1024,
                "mcp": {
                    "enabled": True,
                    "transport": "sse",
                    "host": "0.0.0.0",
                    "port": 8123,
                    "api_base_url": "http://localhost:8100",
                    "timeout_seconds": 20,
                    "allow_tools": ["memory_health", "memory_stats"],
                },
            }
        }
        config_path = tmp_path / "config_mcp.yaml"
        with open(config_path, "w") as f:
            yaml.dump(config_data, f)

        config = load_config(config_path)
        assert config.mcp.enabled is True
        assert config.mcp.transport == "sse"
        assert config.mcp.port == 8123
        assert config.mcp.allow_tools == ["memory_health", "memory_stats"]