|
1 | | -# ratios: loc_comments=143:4 imports_exports=9:3 calls_definitions=86:21 |
| 1 | +# ratios: loc_comments=184:4 imports_exports=10:4 calls_definitions=112:26 |
2 | 2 | """Stdlib-only tests for the a0min harness and CLI. |
3 | 3 |
|
4 | 4 | Run from the a0min project root: |
|
11 | 11 | import json |
12 | 12 | import subprocess |
13 | 13 | import sys |
| 14 | +import tempfile |
14 | 15 | import unittest |
15 | 16 | from pathlib import Path |
16 | 17 |
|
|
19 | 20 | from a0min import ( # noqa: E402 (project root on sys.path via test runner) |
20 | 21 | Harness, |
21 | 22 | SpawnCapExceeded, |
| 23 | + available_providers, |
22 | 24 | candidate_platonic_agent, |
| 25 | + load_provider_keys, |
| 26 | + presence, |
| 27 | + provider_key, |
23 | 28 | ) |
24 | 29 |
|
25 | 30 |
|
@@ -120,6 +125,37 @@ def test_save_load_roundtrip(self) -> None: |
120 | 125 | self.assertEqual(restored._index, self.harness._index) |
121 | 126 |
|
122 | 127 |
|
| 128 | +class EnvLoaderTests(unittest.TestCase): |
| 129 | + def test_loads_provider_keys_without_exposing_them(self) -> None: |
| 130 | + with tempfile.TemporaryDirectory() as tmp: |
| 131 | + env_path = Path(tmp) / ".env" |
| 132 | + env_path.write_text( |
| 133 | + "OPENAI_API_KEY=sk-test-openai\n" |
| 134 | + "DEEPSEEK_API_KEY=sk-test-deepseek\n" |
| 135 | + "XAI_API_KEY=xai-test\n", |
| 136 | + encoding="utf-8", |
| 137 | + ) |
| 138 | + keys = load_provider_keys(explicit=env_path) |
| 139 | + self.assertEqual(keys["openai"], "sk-test-openai") |
| 140 | + self.assertEqual(keys["deepseek"], "sk-test-deepseek") |
| 141 | + self.assertEqual(keys["xai"], "xai-test") |
| 142 | + |
| 143 | + def test_presence_never_contains_values(self) -> None: |
| 144 | + with tempfile.TemporaryDirectory() as tmp: |
| 145 | + env_path = Path(tmp) / ".env" |
| 146 | + env_path.write_text("OPENAI_API_KEY=sk-secret\n", encoding="utf-8") |
| 147 | + report = presence(explicit=env_path) |
| 148 | + self.assertEqual(report, {"openai": True, "deepseek": False, "xai": False}) |
| 149 | + self.assertNotIn("sk-secret", json.dumps(report)) |
| 150 | + |
| 151 | + def test_provider_key_missing_returns_none(self) -> None: |
| 152 | + with tempfile.TemporaryDirectory() as tmp: |
| 153 | + env_path = Path(tmp) / ".env" |
| 154 | + env_path.write_text("", encoding="utf-8") |
| 155 | + self.assertIsNone(provider_key("openai", explicit=env_path)) |
| 156 | + self.assertEqual(available_providers(explicit=env_path), ()) |
| 157 | + |
| 158 | + |
123 | 159 | class CliSmokeTests(unittest.TestCase): |
124 | 160 | def run_cli(self, *args: str) -> subprocess.CompletedProcess[str]: |
125 | 161 | return subprocess.run( |
@@ -176,7 +212,17 @@ def test_superpotential_json(self) -> None: |
176 | 212 | self.assertEqual(len(payload["dimensions"]), 13) |
177 | 213 | self.assertEqual(len(payload["regions"]), 11) |
178 | 214 |
|
| 215 | + def test_env_json_shows_presence_only(self) -> None: |
| 216 | + with tempfile.TemporaryDirectory() as tmp: |
| 217 | + env_path = Path(tmp) / ".env" |
| 218 | + env_path.write_text("OPENAI_API_KEY=sk-secret\n", encoding="utf-8") |
| 219 | + result = self.run_cli("env", "--env-file", str(env_path), "--json") |
| 220 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 221 | + payload = json.loads(result.stdout) |
| 222 | + self.assertEqual(payload, {"openai": True, "deepseek": False, "xai": False}) |
| 223 | + self.assertNotIn("sk-secret", result.stdout) |
| 224 | + |
179 | 225 |
|
180 | 226 | if __name__ == "__main__": |
181 | 227 | unittest.main() |
182 | | -# ratios: loc_comments=143:4 imports_exports=9:3 calls_definitions=86:21 |
| 228 | +# ratios: loc_comments=184:4 imports_exports=10:4 calls_definitions=112:26 |
0 commit comments