"""Per-user Google Calendar OAuth helpers + push handler. Network-free; does NOT import the google libs (they're excluded from requirements-ci).""" import os os.environ.setdefault("USE_STUB_EXTRACTOR", "1") import pytest # noqa: E402 from calendar_out import gcal # safe: google libs are imported lazily, inside functions from ui import blocks # noqa: E402 def test_client_config_requires_env(monkeypatch): monkeypatch.delenv("GOOGLE_OAUTH_CLIENT_ID", raising=False) monkeypatch.delenv("GOOGLE_OAUTH_CLIENT_SECRET", raising=False) with pytest.raises(RuntimeError): gcal._client_config() def test_client_config_with_env(monkeypatch): monkeypatch.setenv("GOOGLE_OAUTH_CLIENT_ID", "cid.apps.googleusercontent.com") monkeypatch.setenv("GOOGLE_OAUTH_CLIENT_SECRET", "secret") cfg = gcal._client_config() assert cfg["web"]["client_id"] == "cid.apps.googleusercontent.com" assert cfg["web"]["token_uri"].startswith("https://") def test_push_handler_without_token_prompts_connect(): msg = blocks._on_push_gcal([["Meet", "2026-06-12T09:00", "", "", 30]], "", 0) assert "Connect Google Calendar" in msg def test_push_handler_with_token_pushes(monkeypatch): calls = {} def fake_push(tok, events): calls["n"] = len(events) return ["https://cal/evt"] monkeypatch.setattr(gcal, "push_events_with_token", fake_push) monkeypatch.setattr(blocks.impact, "record_capture", lambda *a, **k: calls.setdefault("rec", True)) msg = blocks._on_push_gcal([["Meet", "2026-06-12T09:00", "", "", 30]], "TOKEN_JSON", 0) assert calls["n"] == 1 and calls.get("rec") is True assert "Google Calendar" in msg and "cal/evt" in msg