This repository was archived by the owner on Jan 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config.py
More file actions
573 lines (425 loc) · 21.1 KB
/
Copy pathtest_config.py
File metadata and controls
573 lines (425 loc) · 21.1 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
"""Tests for git_notes_memory.config module.
Tests all configuration constants, path resolution, and environment variable overrides.
"""
from __future__ import annotations
import os
from pathlib import Path
from typing import TYPE_CHECKING
import pytest
from git_notes_memory import config
if TYPE_CHECKING:
from collections.abc import Iterator
# =============================================================================
# Fixtures
# =============================================================================
@pytest.fixture
def clean_env() -> Iterator[None]:
"""Clean up environment variables before and after test."""
env_vars = [
"MEMORY_PLUGIN_DATA_DIR",
"MEMORY_PLUGIN_GIT_NAMESPACE",
"MEMORY_PLUGIN_EMBEDDING_MODEL",
"MEMORY_PLUGIN_AUTO_CAPTURE",
"XDG_DATA_HOME",
]
old_values = {var: os.environ.get(var) for var in env_vars}
# Remove all env vars before test
for var in env_vars:
os.environ.pop(var, None)
yield
# Restore original values after test
for var, value in old_values.items():
if value is None:
os.environ.pop(var, None)
else:
os.environ[var] = value
# =============================================================================
# Namespace Tests
# =============================================================================
class TestNamespaces:
"""Tests for namespace configuration."""
def test_namespaces_count(self) -> None:
"""Test that exactly 10 namespaces are defined."""
assert len(config.NAMESPACES) == 10
def test_namespaces_membership(self) -> None:
"""Test all expected namespaces exist."""
expected = {
"inception",
"elicitation",
"research",
"decisions",
"progress",
"blockers",
"reviews",
"learnings",
"retrospective",
"patterns",
}
assert expected == config.NAMESPACES
def test_namespaces_is_frozenset(self) -> None:
"""Test that NAMESPACES is immutable."""
assert isinstance(config.NAMESPACES, frozenset)
def test_auto_capture_namespaces_is_subset(self) -> None:
"""Test that AUTO_CAPTURE_NAMESPACES is subset of NAMESPACES."""
assert config.AUTO_CAPTURE_NAMESPACES <= config.NAMESPACES
def test_auto_capture_excludes_reviews(self) -> None:
"""Test that auto-capture excludes 'reviews' namespace."""
assert "reviews" not in config.AUTO_CAPTURE_NAMESPACES
# =============================================================================
# Git Configuration Tests
# =============================================================================
class TestGitConfiguration:
"""Tests for git namespace configuration."""
def test_default_git_namespace(self) -> None:
"""Test default git namespace value."""
assert config.DEFAULT_GIT_NAMESPACE == "refs/notes/mem"
def test_get_git_namespace_default(self, clean_env: None) -> None:
"""Test get_git_namespace returns default when no env var."""
assert config.get_git_namespace() == "refs/notes/mem"
def test_get_git_namespace_override(self, clean_env: None) -> None:
"""Test get_git_namespace respects environment override."""
os.environ["MEMORY_PLUGIN_GIT_NAMESPACE"] = "refs/notes/custom"
assert config.get_git_namespace() == "refs/notes/custom"
# =============================================================================
# Path Configuration Tests
# =============================================================================
class TestPathConfiguration:
"""Tests for path configuration and resolution."""
def test_index_db_name(self) -> None:
"""Test index database filename."""
assert config.INDEX_DB_NAME == "index.db"
def test_models_dir_name(self) -> None:
"""Test models directory name."""
assert config.MODELS_DIR_NAME == "models"
def test_lock_file_name(self) -> None:
"""Test lock file name."""
assert config.LOCK_FILE_NAME == ".capture.lock"
def test_get_data_path_default(self, clean_env: None) -> None:
"""Test get_data_path returns XDG-compliant default."""
path = config.get_data_path()
expected = Path.home() / ".local" / "share" / "memory-plugin"
assert path == expected
def test_get_data_path_xdg_override(self, clean_env: None) -> None:
"""Test get_data_path respects XDG_DATA_HOME."""
os.environ["XDG_DATA_HOME"] = "/custom/xdg/data"
path = config.get_data_path()
assert path == Path("/custom/xdg/data/memory-plugin")
def test_get_data_path_env_override(self, clean_env: None) -> None:
"""Test get_data_path respects MEMORY_PLUGIN_DATA_DIR."""
os.environ["MEMORY_PLUGIN_DATA_DIR"] = "/custom/memory/data"
path = config.get_data_path()
assert path == Path("/custom/memory/data")
def test_get_data_path_env_override_priority(self, clean_env: None) -> None:
"""Test MEMORY_PLUGIN_DATA_DIR takes priority over XDG_DATA_HOME."""
os.environ["XDG_DATA_HOME"] = "/xdg/data"
os.environ["MEMORY_PLUGIN_DATA_DIR"] = "/custom/memory"
path = config.get_data_path()
assert path == Path("/custom/memory")
def test_get_data_path_expands_tilde(self, clean_env: None) -> None:
"""Test get_data_path expands ~ in paths."""
os.environ["MEMORY_PLUGIN_DATA_DIR"] = "~/memory-plugin"
path = config.get_data_path()
assert path == Path.home() / "memory-plugin"
def test_get_index_path(self, clean_env: None) -> None:
"""Test get_index_path returns correct path."""
path = config.get_index_path()
expected = config.get_data_path() / "index.db"
assert path == expected
def test_get_models_path(self, clean_env: None) -> None:
"""Test get_models_path returns correct path."""
path = config.get_models_path()
expected = config.get_data_path() / "models"
assert path == expected
def test_get_lock_path(self, clean_env: None) -> None:
"""Test get_lock_path returns correct path."""
path = config.get_lock_path()
expected = config.get_data_path() / ".capture.lock"
assert path == expected
# =============================================================================
# Git Root Detection Tests
# =============================================================================
class TestFindGitRoot:
"""Tests for find_git_root() function."""
def test_finds_git_root_from_root(self, tmp_path: Path) -> None:
"""Test finding git root when at the repo root."""
(tmp_path / ".git").mkdir()
result = config.find_git_root(tmp_path)
assert result == tmp_path
def test_finds_git_root_from_subdir(self, tmp_path: Path) -> None:
"""Test finding git root from a subdirectory."""
(tmp_path / ".git").mkdir()
subdir = tmp_path / "src" / "deep" / "nested"
subdir.mkdir(parents=True)
result = config.find_git_root(subdir)
assert result == tmp_path
def test_uses_cwd_when_no_path(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test using current directory when no path provided."""
(tmp_path / ".git").mkdir()
monkeypatch.chdir(tmp_path)
result = config.find_git_root()
assert result == tmp_path
def test_raises_when_not_in_git_repo(self, tmp_path: Path) -> None:
"""Test raising NotInGitRepositoryError when not in a git repo."""
# tmp_path has no .git directory
with pytest.raises(config.NotInGitRepositoryError) as exc_info:
config.find_git_root(tmp_path)
assert "Not inside a git repository" in str(exc_info.value)
assert ".memory folder must be at the git root" in str(exc_info.value)
def test_error_includes_path(self, tmp_path: Path) -> None:
"""Test that error message includes the path that was searched."""
with pytest.raises(config.NotInGitRepositoryError) as exc_info:
config.find_git_root(tmp_path)
assert str(tmp_path) in str(exc_info.value)
class TestProjectMemoryDirGitRoot:
"""Tests for get_project_memory_dir() git root enforcement."""
def test_returns_memory_dir_at_git_root(self, tmp_path: Path) -> None:
"""Test .memory is placed at git root."""
(tmp_path / ".git").mkdir()
result = config.get_project_memory_dir(tmp_path)
assert result == tmp_path / ".memory"
def test_finds_git_root_from_subdir(self, tmp_path: Path) -> None:
"""Test .memory is placed at git root even when called from subdir."""
(tmp_path / ".git").mkdir()
subdir = tmp_path / "src" / "nested"
subdir.mkdir(parents=True)
result = config.get_project_memory_dir(subdir)
# Should be at git root, not in subdir
assert result == tmp_path / ".memory"
assert result != subdir / ".memory"
def test_raises_when_not_in_git_repo(self, tmp_path: Path) -> None:
"""Test raising error when not in a git repository."""
with pytest.raises(config.NotInGitRepositoryError):
config.get_project_memory_dir(tmp_path)
def test_index_path_at_git_root(self, tmp_path: Path) -> None:
"""Test index.db is placed at git root/.memory/."""
(tmp_path / ".git").mkdir()
subdir = tmp_path / "src"
subdir.mkdir()
result = config.get_project_index_path(subdir)
assert result == tmp_path / ".memory" / "index.db"
class TestProjectIdentifierGitRoot:
"""Tests for get_project_identifier() git root resolution."""
def test_consistent_id_from_any_subdir(self, tmp_path: Path) -> None:
"""Test same ID returned from any subdirectory."""
(tmp_path / ".git").mkdir()
subdir1 = tmp_path / "src"
subdir2 = tmp_path / "tests" / "deep"
subdir1.mkdir()
subdir2.mkdir(parents=True)
id_root = config.get_project_identifier(tmp_path)
id_subdir1 = config.get_project_identifier(subdir1)
id_subdir2 = config.get_project_identifier(subdir2)
assert id_root == id_subdir1 == id_subdir2
def test_raises_when_not_in_git_repo(self, tmp_path: Path) -> None:
"""Test raising error when not in a git repository."""
with pytest.raises(config.NotInGitRepositoryError):
config.get_project_identifier(tmp_path)
# =============================================================================
# Embedding Configuration Tests
# =============================================================================
class TestEmbeddingConfiguration:
"""Tests for embedding model configuration."""
def test_default_embedding_model(self) -> None:
"""Test default embedding model name."""
assert config.DEFAULT_EMBEDDING_MODEL == "all-MiniLM-L6-v2"
def test_embedding_dimensions(self) -> None:
"""Test embedding dimensions for default model."""
assert config.EMBEDDING_DIMENSIONS == 384
def test_get_embedding_model_default(self, clean_env: None) -> None:
"""Test get_embedding_model returns default when no env var."""
assert config.get_embedding_model() == "all-MiniLM-L6-v2"
def test_get_embedding_model_override(self, clean_env: None) -> None:
"""Test get_embedding_model respects environment override."""
os.environ["MEMORY_PLUGIN_EMBEDDING_MODEL"] = "custom-model"
assert config.get_embedding_model() == "custom-model"
# =============================================================================
# Limits and Thresholds Tests
# =============================================================================
class TestLimitsAndThresholds:
"""Tests for limits and threshold constants."""
def test_max_content_bytes(self) -> None:
"""Test MAX_CONTENT_BYTES is 100KB."""
assert config.MAX_CONTENT_BYTES == 102400
def test_max_summary_chars(self) -> None:
"""Test MAX_SUMMARY_CHARS is 100."""
assert config.MAX_SUMMARY_CHARS == 100
def test_max_hydration_files(self) -> None:
"""Test MAX_HYDRATION_FILES is 20."""
assert config.MAX_HYDRATION_FILES == 20
def test_max_file_size(self) -> None:
"""Test MAX_FILE_SIZE is 100KB."""
assert config.MAX_FILE_SIZE == 102400
# =============================================================================
# Performance Timeout Tests
# =============================================================================
class TestPerformanceTimeouts:
"""Tests for performance timeout constants."""
def test_search_timeout_ms(self) -> None:
"""Test SEARCH_TIMEOUT_MS is 500ms."""
assert config.SEARCH_TIMEOUT_MS == 500
def test_capture_timeout_ms(self) -> None:
"""Test CAPTURE_TIMEOUT_MS is 2000ms."""
assert config.CAPTURE_TIMEOUT_MS == 2000
def test_reindex_timeout_ms(self) -> None:
"""Test REINDEX_TIMEOUT_MS is 60000ms."""
assert config.REINDEX_TIMEOUT_MS == 60000
def test_lock_timeout_seconds(self) -> None:
"""Test LOCK_TIMEOUT_SECONDS is 5s."""
assert config.LOCK_TIMEOUT_SECONDS == 5
# =============================================================================
# Cache Settings Tests
# =============================================================================
class TestCacheSettings:
"""Tests for cache configuration constants."""
def test_cache_ttl_seconds(self) -> None:
"""Test CACHE_TTL_SECONDS is 300 (5 minutes)."""
assert config.CACHE_TTL_SECONDS == 300.0
def test_cache_max_entries(self) -> None:
"""Test CACHE_MAX_ENTRIES is 100."""
assert config.CACHE_MAX_ENTRIES == 100
# =============================================================================
# Lifecycle Settings Tests
# =============================================================================
class TestLifecycleSettings:
"""Tests for lifecycle configuration constants."""
def test_decay_half_life_days(self) -> None:
"""Test DECAY_HALF_LIFE_DAYS is 30."""
assert config.DECAY_HALF_LIFE_DAYS == 30
def test_seconds_per_day(self) -> None:
"""Test SECONDS_PER_DAY is correct."""
assert config.SECONDS_PER_DAY == 86400
assert config.SECONDS_PER_DAY == 60 * 60 * 24
# =============================================================================
# Search Defaults Tests
# =============================================================================
class TestSearchDefaults:
"""Tests for search default constants."""
def test_default_recall_limit(self) -> None:
"""Test DEFAULT_RECALL_LIMIT is 10."""
assert config.DEFAULT_RECALL_LIMIT == 10
def test_default_search_limit(self) -> None:
"""Test DEFAULT_SEARCH_LIMIT is 10."""
assert config.DEFAULT_SEARCH_LIMIT == 10
def test_max_recall_limit(self) -> None:
"""Test MAX_RECALL_LIMIT is 100."""
assert config.MAX_RECALL_LIMIT == 100
def test_max_proactive_suggestions(self) -> None:
"""Test MAX_PROACTIVE_SUGGESTIONS is 3."""
assert config.MAX_PROACTIVE_SUGGESTIONS == 3
# =============================================================================
# Note Schema Tests
# =============================================================================
class TestNoteSchema:
"""Tests for note schema configuration."""
def test_note_required_fields(self) -> None:
"""Test NOTE_REQUIRED_FIELDS contains expected fields."""
expected = {"type", "spec", "timestamp", "summary"}
assert expected == config.NOTE_REQUIRED_FIELDS
def test_note_optional_fields(self) -> None:
"""Test NOTE_OPTIONAL_FIELDS contains expected fields."""
expected = {"phase", "tags", "relates_to", "status"}
assert expected == config.NOTE_OPTIONAL_FIELDS
def test_note_fields_are_frozenset(self) -> None:
"""Test note field sets are immutable."""
assert isinstance(config.NOTE_REQUIRED_FIELDS, frozenset)
assert isinstance(config.NOTE_OPTIONAL_FIELDS, frozenset)
# =============================================================================
# Auto-capture Tests
# =============================================================================
class TestAutoCaptureConfiguration:
"""Tests for auto-capture configuration."""
def test_is_auto_capture_disabled_by_default(self, clean_env: None) -> None:
"""Test auto-capture is disabled by default."""
assert config.is_auto_capture_enabled() is False
def test_is_auto_capture_enabled_with_1(self, clean_env: None) -> None:
"""Test auto-capture enabled with '1'."""
os.environ["MEMORY_PLUGIN_AUTO_CAPTURE"] = "1"
assert config.is_auto_capture_enabled() is True
def test_is_auto_capture_enabled_with_true(self, clean_env: None) -> None:
"""Test auto-capture enabled with 'true'."""
os.environ["MEMORY_PLUGIN_AUTO_CAPTURE"] = "true"
assert config.is_auto_capture_enabled() is True
def test_is_auto_capture_enabled_with_TRUE(self, clean_env: None) -> None:
"""Test auto-capture enabled with 'TRUE' (case-insensitive)."""
os.environ["MEMORY_PLUGIN_AUTO_CAPTURE"] = "TRUE"
assert config.is_auto_capture_enabled() is True
def test_is_auto_capture_enabled_with_yes(self, clean_env: None) -> None:
"""Test auto-capture enabled with 'yes'."""
os.environ["MEMORY_PLUGIN_AUTO_CAPTURE"] = "yes"
assert config.is_auto_capture_enabled() is True
def test_is_auto_capture_enabled_with_on(self, clean_env: None) -> None:
"""Test auto-capture enabled with 'on'."""
os.environ["MEMORY_PLUGIN_AUTO_CAPTURE"] = "on"
assert config.is_auto_capture_enabled() is True
def test_is_auto_capture_disabled_with_0(self, clean_env: None) -> None:
"""Test auto-capture disabled with '0'."""
os.environ["MEMORY_PLUGIN_AUTO_CAPTURE"] = "0"
assert config.is_auto_capture_enabled() is False
def test_is_auto_capture_disabled_with_false(self, clean_env: None) -> None:
"""Test auto-capture disabled with 'false'."""
os.environ["MEMORY_PLUGIN_AUTO_CAPTURE"] = "false"
assert config.is_auto_capture_enabled() is False
def test_is_auto_capture_disabled_with_invalid(self, clean_env: None) -> None:
"""Test auto-capture disabled with invalid value."""
os.environ["MEMORY_PLUGIN_AUTO_CAPTURE"] = "invalid"
assert config.is_auto_capture_enabled() is False
# =============================================================================
# Review Categories Tests
# =============================================================================
class TestReviewCategories:
"""Tests for review category configuration."""
def test_review_categories(self) -> None:
"""Test REVIEW_CATEGORIES contains expected values."""
expected = {
"security",
"performance",
"architecture",
"quality",
"tests",
"documentation",
}
assert expected == config.REVIEW_CATEGORIES
def test_review_severities(self) -> None:
"""Test REVIEW_SEVERITIES contains expected values."""
expected = {"critical", "high", "medium", "low"}
assert expected == config.REVIEW_SEVERITIES
def test_review_sets_are_frozenset(self) -> None:
"""Test review sets are immutable."""
assert isinstance(config.REVIEW_CATEGORIES, frozenset)
assert isinstance(config.REVIEW_SEVERITIES, frozenset)
# =============================================================================
# Retrospective Configuration Tests
# =============================================================================
class TestRetrospectiveConfiguration:
"""Tests for retrospective configuration."""
def test_retrospective_outcomes(self) -> None:
"""Test RETROSPECTIVE_OUTCOMES contains expected values."""
expected = {"success", "partial", "failed", "abandoned"}
assert expected == config.RETROSPECTIVE_OUTCOMES
def test_retrospective_outcomes_is_frozenset(self) -> None:
"""Test RETROSPECTIVE_OUTCOMES is immutable."""
assert isinstance(config.RETROSPECTIVE_OUTCOMES, frozenset)
# =============================================================================
# Module Export Tests
# =============================================================================
class TestModuleExports:
"""Tests for module __all__ exports."""
def test_all_exports_exist(self) -> None:
"""Test all items in __all__ are actually defined."""
for name in config.__all__:
assert hasattr(config, name), f"'{name}' in __all__ but not defined"
def test_important_exports_in_all(self) -> None:
"""Test important items are exported in __all__."""
important = [
"NAMESPACES",
"DEFAULT_GIT_NAMESPACE",
"DEFAULT_EMBEDDING_MODEL",
"get_data_path",
"get_index_path",
"get_models_path",
"is_auto_capture_enabled",
]
for name in important:
assert name in config.__all__, f"'{name}' should be in __all__"