| 1 | from __future__ import annotations |
| 2 | |
| 3 | import re |
| 4 | from pathlib import Path |
| 5 | from unittest import mock |
| 6 | |
| 7 | from lib import env |
| 8 | |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parents[1] |
| 11 | DOC_PATHS = [ |
| 12 | ROOT / "skills" / "last30days" / "SKILL.md", |
| 13 | ROOT / "README.md", |
| 14 | ] |
| 15 | CONFIG_ENV_KEY_RE = re.compile( |
| 16 | r"(?<![A-Z0-9_])(?:" |
| 17 | r"LAST30DAYS_[A-Z0-9_]+|" |
| 18 | r"(?:XAI|GOOGLE|GEMINI|GOOGLE_GENAI|XIAOHONGSHU|SCRAPECREATORS|APIFY|" |
| 19 | r"BSKY|TRUTHSOCIAL|BRAVE|EXA|SERPER|OPENROUTER|PERPLEXITY|PARALLEL|" |
| 20 | r"XQUIK|GROQ)_[A-Z0-9_]+|" |
| 21 | r"OPENAI_API_KEY|AUTH_TOKEN|CT0|FROM_BROWSER|INCLUDE_SOURCES|" |
| 22 | r"EXCLUDE_SOURCES|SETUP_COMPLETE|FUN_LEVEL" |
| 23 | r")(?![A-Z0-9_])" |
| 24 | ) |
| 25 | DOC_ONLY_KEYS = { |
| 26 | "LAST30DAYS_API_BASE", |
| 27 | "LAST30DAYS_API_KEY", |
| 28 | "LAST30DAYS_CACHE_DIR", |
| 29 | "LAST30DAYS_MCP_TIMEOUT", |
| 30 | "LAST30DAYS_PYTHON", |
| 31 | } |
| 32 | |
| 33 | |
| 34 | def _neutral_secret_sources(): |
| 35 | return ( |
| 36 | mock.patch.object(env, "_load_keychain", return_value={}), |
| 37 | mock.patch.object(env, "_load_pass", return_value={}), |
| 38 | ) |
| 39 | |
| 40 | |
| 41 | def _documented_env_keys() -> set[str]: |
| 42 | keys: set[str] = set() |
| 43 | for path in DOC_PATHS: |
| 44 | keys.update(CONFIG_ENV_KEY_RE.findall(path.read_text(encoding="utf-8"))) |
| 45 | return keys |
| 46 | |
| 47 | |
| 48 | def test_registered_user_config_keys_round_trip_from_env_file(tmp_path, monkeypatch): |
| 49 | config_file = tmp_path / ".env" |
| 50 | config_file.write_text( |
| 51 | "FUN_LEVEL=high\nLAST30DAYS_REPORT_CACHE_TTL_SECONDS=120\n", |
| 52 | encoding="utf-8", |
| 53 | ) |
| 54 | config_file.chmod(0o600) |
| 55 | monkeypatch.setenv("LAST30DAYS_CONFIG_DIR", str(tmp_path)) |
| 56 | monkeypatch.delenv("FUN_LEVEL", raising=False) |
| 57 | monkeypatch.delenv("LAST30DAYS_REPORT_CACHE_TTL_SECONDS", raising=False) |
| 58 | monkeypatch.setattr(env, "CONFIG_DIR", tmp_path) |
| 59 | monkeypatch.setattr(env, "CONFIG_FILE", config_file) |
| 60 | monkeypatch.chdir(tmp_path) |
| 61 | |
| 62 | keychain, pass_store = _neutral_secret_sources() |
| 63 | with keychain, pass_store: |
| 64 | config = env.get_config() |
| 65 | |
| 66 | assert config["FUN_LEVEL"] == "high" |
| 67 | assert config["LAST30DAYS_REPORT_CACHE_TTL_SECONDS"] == "120" |
| 68 | |
| 69 | |
| 70 | def test_documented_env_keys_are_registered_in_get_config(tmp_path, monkeypatch): |
| 71 | monkeypatch.setenv("LAST30DAYS_CONFIG_DIR", str(tmp_path)) |
| 72 | monkeypatch.setattr(env, "CONFIG_DIR", tmp_path) |
| 73 | monkeypatch.setattr(env, "CONFIG_FILE", tmp_path / "does-not-exist.env") |
| 74 | monkeypatch.chdir(tmp_path) |
| 75 | |
| 76 | keychain, pass_store = _neutral_secret_sources() |
| 77 | with keychain, pass_store: |
| 78 | registered_keys = set(env.get_config()) |
| 79 | |
| 80 | missing = sorted(_documented_env_keys() - DOC_ONLY_KEYS - registered_keys) |
| 81 | |
| 82 | assert missing == [] |
| 83 |