#!/usr/bin/env python3 """Env / .env config overlay + hotmail path resolution.""" from __future__ import annotations import importlib.util import os import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[2] def _load(): # Avoid importing full ttk (tkinter); load module with sys.modules stub if needed. # register_cli imports ttk; for unit test we only need the pure helpers. # Exec just the needed functions by importing the module carefully. name = "grok_register_ttk_envtest" # Prefer import; if tk fails, exec_module may still fail on tk. Skip GUI side. try: import grok_register_ttk as m return m except Exception as e: # Fallback: load source and exec only load_env helpers by compiling subset — skip if hard. print("SKIP full import:", type(e).__name__, e) return None def test_apply_env_config_overrides() -> None: m = _load() if m is None: # lightweight reimplementation check via source contract src = (ROOT / "grok_register_ttk.py").read_text(encoding="utf-8") assert "def apply_env_config_overrides" in src assert "HOTMAIL_ACCOUNTS_FILE" in src print("PASS apply_env_config_overrides (source contract; import skipped)") return old = os.environ.get("HOTMAIL_ACCOUNTS_FILE") old2 = os.environ.get("CPA_REMOTE_LIVE_REQUIRED") try: os.environ["HOTMAIL_ACCOUNTS_FILE"] = "mail_assets/merged.txt" os.environ["CPA_REMOTE_LIVE_REQUIRED"] = "true" os.environ["CPA_AUTH_PRIORITY"] = "42" out = m.apply_env_config_overrides({"hotmail_accounts_file": "mail_credentials.txt", "cpa_auth_priority": 1000}) assert out["hotmail_accounts_file"] == "mail_assets/merged.txt" assert out["cpa_remote_live_required"] is True assert out["cpa_auth_priority"] == 42 # path resolver path = m.get_hotmail_accounts_file() if hasattr(m, "get_hotmail_accounts_file") else None if path: assert path.endswith("mail_assets/merged.txt") or "mail_assets/merged.txt" in path print("PASS apply_env_config_overrides") finally: if old is None: os.environ.pop("HOTMAIL_ACCOUNTS_FILE", None) else: os.environ["HOTMAIL_ACCOUNTS_FILE"] = old if old2 is None: os.environ.pop("CPA_REMOTE_LIVE_REQUIRED", None) else: os.environ["CPA_REMOTE_LIVE_REQUIRED"] = old2 os.environ.pop("CPA_AUTH_PRIORITY", None) def test_env_overlay_advance_on_start_and_cpa_mint() -> None: m = _load() if m is None: src = (ROOT / "grok_register_ttk.py").read_text(encoding="utf-8") assert "PROXY_ROTATE_ADVANCE_ON_START" in src assert "CPA_PREFER_PROTOCOL" in src assert "CPA_MINT_TIMEOUT_SEC" in src print("PASS env overlay advance_on_start/cpa_mint (source contract; import skipped)") return try: os.environ["PROXY_ROTATE_ADVANCE_ON_START"] = "true" os.environ["CPA_PREFER_PROTOCOL"] = "true" os.environ["CPA_MINT_TIMEOUT_SEC"] = "120" out = m.apply_env_config_overrides({}) assert out["proxy_rotate_advance_on_start"] is True assert out["cpa_prefer_protocol"] is True assert out["cpa_mint_timeout_sec"] == 120 print("PASS env overlay advance_on_start/cpa_mint") finally: os.environ.pop("PROXY_ROTATE_ADVANCE_ON_START", None) os.environ.pop("CPA_PREFER_PROTOCOL", None) os.environ.pop("CPA_MINT_TIMEOUT_SEC", None) if __name__ == "__main__": test_apply_env_config_overrides() test_env_overlay_advance_on_start_and_cpa_mint()