-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_config_manual.py
More file actions
187 lines (141 loc) · 5.2 KB
/
test_config_manual.py
File metadata and controls
187 lines (141 loc) · 5.2 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
#!/usr/bin/env python3
"""Manual test script for configuration (no pytest required)."""
import os
import sys
import tempfile
from pathlib import Path
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))
from codeframe.core.config import Config, GlobalConfig, load_environment
def test_default_values():
"""Test default values."""
print("✓ Testing default values...")
config = GlobalConfig()
assert config.database_path == ".codeframe/state.db"
assert config.api_host == "0.0.0.0"
assert config.api_port == 8080
assert config.log_level == "INFO"
print(" ✓ All defaults correct")
def test_cors_origins():
"""Test CORS origins parsing."""
print("✓ Testing CORS origins parsing...")
config = GlobalConfig(cors_origins="http://localhost:3000, http://localhost:5173")
origins = config.get_cors_origins_list()
assert len(origins) == 2
assert "http://localhost:3000" in origins
print(" ✓ CORS parsing works")
def test_log_level_validation():
"""Test log level validation."""
print("✓ Testing log level validation...")
# Valid
config = GlobalConfig(log_level="DEBUG")
assert config.log_level == "DEBUG"
# Case insensitive
config = GlobalConfig(log_level="info")
assert config.log_level == "INFO"
# Invalid should raise
try:
GlobalConfig(log_level="INVALID")
assert False, "Should have raised ValueError"
except ValueError as e:
assert "LOG_LEVEL must be one of" in str(e)
print(" ✓ Log level validation works")
def test_port_validation():
"""Test port validation."""
print("✓ Testing port validation...")
# Valid
config = GlobalConfig(api_port=3000)
assert config.api_port == 3000
# Invalid (too low)
try:
GlobalConfig(api_port=0)
assert False, "Should have raised ValueError"
except ValueError as e:
assert "API_PORT must be between" in str(e)
print(" ✓ Port validation works")
def test_sprint_validation():
"""Test sprint validation."""
print("✓ Testing sprint validation...")
# With API key - should pass
config = GlobalConfig(anthropic_api_key="sk-test-key")
config.validate_required_for_sprint(sprint=1)
print(" ✓ Validation passes with API key")
# Without API key - should fail
config = GlobalConfig()
try:
config.validate_required_for_sprint(sprint=1)
assert False, "Should have raised ValueError"
except ValueError as e:
assert "ANTHROPIC_API_KEY is required" in str(e)
print(" ✓ Validation fails without API key")
def test_ensure_directories():
"""Test directory creation."""
print("✓ Testing directory creation...")
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_path = Path(tmp_dir)
db_path = tmp_path / "test_db" / "state.db"
log_path = tmp_path / "logs" / "test.log"
config = GlobalConfig(database_path=str(db_path), log_file=str(log_path))
config.ensure_directories()
assert db_path.parent.exists()
assert log_path.parent.exists()
print(" ✓ Directories created successfully")
def test_config_manager():
"""Test Config manager."""
print("✓ Testing Config manager...")
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_path = Path(tmp_dir)
config = Config(tmp_path)
assert config.project_dir == tmp_path
assert config.config_dir == tmp_path / ".codeframe"
print(" ✓ Config manager initialized")
def test_environment_loading():
"""Test environment file loading."""
print("✓ Testing environment file loading...")
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_path = Path(tmp_dir)
env_file = tmp_path / ".env"
env_file.write_text("ANTHROPIC_API_KEY=sk-test-env-file")
# Save current directory and change to temp
original_cwd = Path.cwd()
original_key = os.getenv("ANTHROPIC_API_KEY")
try:
os.chdir(tmp_path)
load_environment()
config = GlobalConfig()
assert config.anthropic_api_key == "sk-test-env-file"
print(" ✓ Environment loaded from .env file")
finally:
os.chdir(original_cwd)
# Restore or remove environment variable
if original_key:
os.environ["ANTHROPIC_API_KEY"] = original_key
else:
os.environ.pop("ANTHROPIC_API_KEY", None)
def main():
"""Run all tests."""
print("\n" + "=" * 70)
print("CONFIGURATION TESTS")
print("=" * 70 + "\n")
try:
test_default_values()
test_cors_origins()
test_log_level_validation()
test_port_validation()
test_sprint_validation()
test_ensure_directories()
test_config_manager()
test_environment_loading()
print("\n" + "=" * 70)
print("✅ ALL TESTS PASSED")
print("=" * 70 + "\n")
return 0
except Exception as e:
print("\n" + "=" * 70)
print(f"❌ TEST FAILED: {e}")
print("=" * 70 + "\n")
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
sys.exit(main())