updated tests to work without .env file

This commit is contained in:
2026-03-04 23:07:53 -07:00
parent e34f82b181
commit e743167ba5
6 changed files with 48 additions and 15 deletions
+7 -2
View File
@@ -6,12 +6,17 @@ PROJECT_ROOT = Path(__file__).parent.parent
class Settings(BaseSettings): class Settings(BaseSettings):
anthropic_api_key: str # required in production and has safe defaults for testing
anthropic_api_key: str = ""
model: str = "claude-sonnet-4-5-20250929" model: str = "claude-sonnet-4-5-20250929"
safedir: str = "./workspace" safedir: str = "./workspace"
max_tokens: int = 8096 max_tokens: int = 8096
use_sandbox: bool = True
model_config = {"env_file": PROJECT_ROOT / ".env"} model_config = {
"env_file": PROJECT_ROOT / ".env" if (PROJECT_ROOT / ".env").exists() else None,
"extra": "ignore",
}
# create singleton instance, causes validation # create singleton instance, causes validation
+2
View File
@@ -2,4 +2,6 @@
ANTHROPIC_API_KEY=sk-ant-... ANTHROPIC_API_KEY=sk-ant-...
MODEL=claude-sonnet-4-5-20250929 MODEL=claude-sonnet-4-5-20250929
MAX_TOKENS=8096 #anthropic limit is 8192
SAFEDIR=./workspace SAFEDIR=./workspace
USE_SANDBOX=true
+10
View File
@@ -1,5 +1,6 @@
import asyncio import asyncio
from agent.config import settings
from agent.loop import run_session from agent.loop import run_session
from sandbox.session import PodmanSandbox from sandbox.session import PodmanSandbox
@@ -15,6 +16,15 @@ async def run_tui():
def main(): def main():
print("Hello from mycode!") print("Hello from mycode!")
# Validate required settings
if not settings.anthropic_api_key:
print("Error: ANTHROPIC_API_KEY not set")
print("\nPlease create a .env file with:")
print(" ANTHROPIC_API_KEY=sk-ant-...")
print("\nSee .env.example for template")
exit(1)
asyncio.run(run_tui()) asyncio.run(run_tui())
+10
View File
@@ -16,6 +16,7 @@ dependencies = [
dev = [ dev = [
"pytest>=9.0.2", "pytest>=9.0.2",
"pytest-asyncio>=1.3.0", "pytest-asyncio>=1.3.0",
"pytest-env>=1.5.0",
"pytest-mock>=3.15.1", "pytest-mock>=3.15.1",
] ]
@@ -36,3 +37,12 @@ markers = [
"integration: marks tests as integration tests (require external services)", "integration: marks tests as integration tests (require external services)",
"unit: marks tests as unit tests (no external dependencies)", "unit: marks tests as unit tests (no external dependencies)",
] ]
# Environment variables for tests
env = [
"ANTHROPIC_API_KEY=test-key-12345",
"MODEL=claude-test-model",
"MAX_TOKENS=100",
"SAFEDIR=./test-workspace",
"USE_SANDBOX=false",
]
+4 -13
View File
@@ -25,22 +25,13 @@ def test_settings_with_all_values():
def test_settings_defaults(): def test_settings_defaults():
"""Test Settings uses defaults for optional values.""" """Test Settings uses defaults for optional values."""
settings = Settings( settings = Settings(
anthropic_api_key="sk-ant-test" # Only required field anthropic_api_key="sk-ant-test", # Only required field
_env_file=None,
) )
# Should use defaults # Should use defaults
assert settings.model == "claude-sonnet-4-5-20250929" assert settings.model == "claude-test-model"
assert settings.max_tokens == 8096 assert settings.max_tokens == 100
@pytest.mark.unit
def test_settings_missing_required_field():
"""Test Settings raises error when required field is missing."""
with pytest.raises(ValidationError) as exc_info:
Settings(_env_file=None) # Missing anthropic_api_key
# Verify the error mentions the missing field
assert "anthropic_api_key" in str(exc_info.value)
@pytest.mark.unit @pytest.mark.unit
Generated
+15
View File
@@ -207,6 +207,7 @@ dependencies = [
dev = [ dev = [
{ name = "pytest" }, { name = "pytest" },
{ name = "pytest-asyncio" }, { name = "pytest-asyncio" },
{ name = "pytest-env" },
{ name = "pytest-mock" }, { name = "pytest-mock" },
] ]
@@ -223,6 +224,7 @@ requires-dist = [
dev = [ dev = [
{ name = "pytest", specifier = ">=9.0.2" }, { name = "pytest", specifier = ">=9.0.2" },
{ name = "pytest-asyncio", specifier = ">=1.3.0" }, { name = "pytest-asyncio", specifier = ">=1.3.0" },
{ name = "pytest-env", specifier = ">=1.5.0" },
{ name = "pytest-mock", specifier = ">=3.15.1" }, { name = "pytest-mock", specifier = ">=3.15.1" },
] ]
@@ -362,6 +364,19 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/e5/35/f8b19922b6a25bc0880171a2f1a003eaeb93657475193ab516fd87cac9da/pytest_asyncio-1.3.0-py3-none-any.whl", hash = "sha256:611e26147c7f77640e6d0a92a38ed17c3e9848063698d5c93d5aa7aa11cebff5", size = 15075, upload-time = "2025-11-10T16:07:45.537Z" }, { url = "https://files.pythonhosted.org/packages/e5/35/f8b19922b6a25bc0880171a2f1a003eaeb93657475193ab516fd87cac9da/pytest_asyncio-1.3.0-py3-none-any.whl", hash = "sha256:611e26147c7f77640e6d0a92a38ed17c3e9848063698d5c93d5aa7aa11cebff5", size = 15075, upload-time = "2025-11-10T16:07:45.537Z" },
] ]
[[package]]
name = "pytest-env"
version = "1.5.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "pytest" },
{ name = "python-dotenv" },
]
sdist = { url = "https://files.pythonhosted.org/packages/e6/56/a931c6f6194917ff44be41b8586e2ffd13a18fa70fb28d9800a4695befa5/pytest_env-1.5.0.tar.gz", hash = "sha256:db8994b9ce170f135a37acc09ac753a6fc697d15e691b576ed8d8ca261c40246", size = 15271, upload-time = "2026-02-17T18:31:39.095Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/61/af/99b52a8524983bfece35e51e65a0b517b22920c023e57855c95e744e19e4/pytest_env-1.5.0-py3-none-any.whl", hash = "sha256:89a15686ac837c9cd009a8a2d52bd55865e2f23c82094247915dae4540c87161", size = 10122, upload-time = "2026-02-17T18:31:37.496Z" },
]
[[package]] [[package]]
name = "pytest-mock" name = "pytest-mock"
version = "3.15.1" version = "3.15.1"