Files
hermes-agent/tests/gateway/test_google_chat.py
Teknium 9bcbe7b5df feat(i18n): pluggable, layered language packs across core, Desktop and TUI (#126296)
* feat(i18n): layered catalogs — plugin packs and user overlay over bundled locales

* feat(tui): i18n layer — en catalog, nanostore runtime, RPC pack loader, _keys.tui.json emitter

ui-tui/src/i18n/: en.ts (facade over topical siblings under en/), types.ts
(Translations + dotted TranslationKey derived from en), runtime.ts ($locale/
$catalog atoms, translateFrom active→en→key, pack merge with string→fn
wrapping for {0}/{1} placeholders), loader.ts (display.language →
i18n.catalog {lang, surface:'tui'}, English when the method is missing),
useT()/useLocale() hooks, t() for non-React code. useConfigSync feeds the
loader from the existing config.get full hydration. `npm run i18n:keys`
writes locales/_keys.tui.json (sorted flat key list) and runs before build.

* feat(plugins): provides_locales manifest field, ctx.register_locale/register_locale_dir, manifest-only language packs

* chore(tui): split en catalog siblings by lane (slash sibling)

* feat(plugins): validate language packs — parse, text-only, key-subset WARN against en / _keys exports

* feat(tui_gateway): i18n.languages / i18n.catalog RPC + regenerated contracts

* feat(config): display.language accepts any supported_languages() id, refuses unknown ids with the list

* docs(i18n): language packs user guide, pluggable display.language, plugin developer section, AGENTS notes

* feat(plugins): report language-pack layers in the mid-run activation summary

* feat(tui): wire status bar, composer placeholders, hotkey help and approval/clarify/confirm prompts through i18n

StatusRule maps compared state values (ready/running…/summoning) to catalog
text at render via displayStatus(); hotkeys()/placeholder() resolve lazily so
a pack that arrives after boot applies. Catalog grows to 81 keys.

* feat(desktop): pluggable app locales — registry, host.i18n.registerAppLocale, backend packs, keys emitter

- Locale widens to string (BundledLocale keeps the union); TRANSLATIONS stays
  the bundled record and every consumer resolves through the registry.
- src/i18n/registry.ts: registerAppLocale(id, {endonym, rtl, translations})
  layers partial packs (nested or flat dotted) over bundled/en via
  mergeTranslations; a string over a function-valued en entry becomes a
  positional {0}/{1} formatter; $appLocaleVersion bumps so translators
  re-render; per-source disposers + replaceAppLocaleSource for atomic swaps.
- Backend packs: i18n.languages + i18n.catalog {surface:'desktop'} feed the
  registry as source 'backend' (method-not-found is silent); re-synced on
  socket open, display.language change and profile switch. A saved pack-only
  language is promoted once its pack registers.
- SDK: host.i18n.registerAppLocale / languageOptions; ctx.i18n.registerAppLocale
  tracked for unload. Docs in the desktop plugin SDK guide + skill reference.
- Language switcher lists bundled ∪ registered ∪ backend, endonym-only; RTL
  from the registry (applyDocumentLocale takes rtl).
- npm run i18n:keys emits locales/_keys.desktop.json (wired into build).

* i18n(cli): route /topup + /subscription copy through t() (cli.billing.*, cli.subscription.*)

Module-level copy tables and modal choice tuples in cli_billing_mixin.py froze
English at import, before display.language was known. They are now key tables /
builder functions evaluated at call time; every user-facing line in the /usage
balance block, /subscription and the five /topup screens reads the catalog.
Choice VALUES stay English identifiers. Fragment-assembled status lines
(Plan: … → cancels · $x left · renews …) become full templates.

* i18n(gateway): exec-approval card contract + base/run/run_busy/run_inbound replies through t()

- base_exec_approval: EA_* English constants stay; add ea_header_text()/ea_reason_label_text()/
  ea_smart_deny_line_text()/ea_default_reason_text()/ea_action_labels()/approval_timed_out_notice()
  accessors; deadline + timed-out notice resolve via gateway.exec_approval.*
- BasePlatformAdapter._EA_HEADER/_EA_REASON_LABEL/_EA_SMART_DENY_LINE/_EA_ACTION_LABELS become
  properties (adapters still shadow them with markup class attrs)
- run.py: provider error replies table holds catalog keys; _CONTEXT_OVERFLOW_REPLY -> _context_overflow_reply()
- run_busy/run_inbound: typed approval + slash-confirm matchers accept English ∪ approval.inputs.* (t())
- locales/en.yaml: gateway.exec_approval/busy/errors/... namespaces

* i18n(cli): wire modal, loops, agent-setup mixins through t() (cli.* keys)

* i18n(platforms): route Slack, Matrix and Feishu user-facing text through t()

Exec-approval markup overrides (_EA_HEADER/_EA_REASON_LABEL/_EA_SMART_DENY_LINE/
_EA_ACTION_LABELS) become per-call properties over the shared
gateway.exec_approval.* contract keys, so Slack's 3000-char section budget
measures the resolved template. Slack _APPROVAL_DECISIONS/_CONFIRM_DECISIONS,
Feishu _APPROVAL_LABEL_MAP and Matrix _EA_LEGEND/_EA_TYPED_HINT turn into
key tables resolved at click time; the Matrix typed hints become whole
sentences per offered tier instead of spliced fragments. Slack button labels
are cut to 75 chars and select placeholders to 150 after translation; the
model-facing clarify fallback answer ('choice N') stays English while the
card copy localizes.

locales/en.yaml gains the gateway.exec_approval.* contract keys plus the
platform.shared.* / platform.slack.* / platform.matrix.* / platform.feishu.*
namespaces (and the keys for the other adapters wired in follow-up commits).

* i18n(gateway): run_turn / run_turn_runner / approval-settle copy through t()

- status hints, proxy errors, background task notices, progress heartbeats, session info lines
- tool progress chrome (tool_head/tool_pending/tool_preview/tool_verbose) shared by base.format_tool_event
- run_turn_runner:1406 Chinese clarify placeholder -> gateway.clarify.native_stream_placeholder (zh text kept in zh.yaml)
- _UNEXPECTED_SILENCE_REPLY/_CLARIFY_EXPIRED_NOTICE -> accessor functions

* i18n(platforms): route Google Chat and Teams user-facing text through t()

Google Chat clarify card, typing placeholder, orphan-card labels and the whole
/setup-files reply set (module constants become platform.google_chat.setup_files.*
keys resolved at reply time). The attachment-fallback notice that shipped
hardcoded in Spanish is keyed with an English en value; es.yaml carries the
original Spanish text for those four keys.

Teams approval card header/reason use the gateway.exec_approval.* contract,
_APPROVAL_LABELS becomes a key table resolved at click time, and the meeting
summary writer resolves its section headings/fallbacks per render.

* i18n(platforms): route LINE, WeCom, email, DingTalk, IRC and Home Assistant text through t()

LINE default copy constants become catalog keys resolved in __init__ (the
LINE_*_TEXT / extra.* operator overrides still win); the busy-ack bypass
matcher keys on the leading emoji marker only, so it keeps firing once the
gateway busy heads are localized. WeCom media size/format notices that shipped
hardcoded in Chinese are keyed with English en values and zh.yaml carries the
original Chinese text. DingTalk emotion bubbles resolve per send.

* i18n(cli): route /model switch output and -q status lines through t() (cli.model.*, cli.single_query.*)

Switch-summary labels shared with the gateway reuse gateway.model.* keys
(provider/context/max-output/capabilities/prompt-caching); CLI-only variants
(glyph or no-backtick forms) live under cli.model.*. The hand-padded /model usage
block becomes a (form, description-key) table padded at render time so the
command syntax stays fixed while descriptions translate. -q 'Error:' reuses
gateway.model.error_prefix.

* i18n(cli): route TUI panel/hint/placeholder copy through t() (cli.tui.*)

_APPROVAL_CHOICE_LABELS and _TUI_MODAL_HINTS become key tables resolved at
render time; vault/sudo panel bodies are one catalog value per panel split on
newline; inline plurals use <key>_one/<key>_other. Adds the cli.* namespace
(shared/tui/voice/render/subagents/dock) to locales/en.yaml.

* i18n(cli): voice/wake-word CLI copy through t() (cli.voice.*)

RuntimeError texts raised in _voice_start_recording are human copy (callers
print {e}) and are keyed; the Termux requirement-check match stays English.
Wake state ids stay internal, only their labels localize.

* i18n(cli): live-work dock, subagent monitor and render copy through t()

cli.subagents.* / cli.dock.* / cli.render.*; count fragments pluralize via
_one/_other keys, verdict table holds keys resolved at paint time so width
clipping measures the translated text.

* i18n(gateway): unauthorized/pairing, voice, topics, shutdown, startup, notifications, kanban pings through t()

* i18n(cli): move tips + composer placeholders into the catalog (tips.tNNN / tips.placeholder.pNN)

get_random_tip()/get_random_composer_placeholder() pick a key from the English catalog
(the parity baseline, probed once per process) and resolve it through t() for the active
language, so language packs translate tips like any other string. Also lands the cli.*
en.yaml namespace consumed by the CLI info/help/error-copy wiring in the next commit.

* i18n(cli): wire chat-turn + session mixins through t(); kanban log trimmer matches t() output

* test(cli): assert TUI/dock/voice copy via t(key); prove labels resolve at render time

Pinned-English assertions in the approval-UI, live-work dock and voice tests now
go through the catalog. New test swaps the catalog after import and checks the
approval panel + hint row follow it (the reason _APPROVAL_CHOICE_LABELS and
_TUI_MODAL_HINTS became key tables).

* i18n(cli): route CLI info/help/error copy through t() (cli.* namespace)

cli_info_mixin: /help consumes CommandDef.describe() (added to commands.py: slash.<name>.description
with fallback to .description), section titles/skill/quick-command headers, /tools, /toolsets,
/usage labels, /context, /whoami, /insights, /gateway status, tool-progress labels, bang-shell
denials, MCP config-watch + /reload-mcp confirm/reload lines, /reload-skills, and the session-store
warning all read the catalog at call time (module-level label tables became functions so the
active language is honoured after startup). cli.py: worktree cleanup, tirith warning, show_config
(labels re-padded at print time), quick/plugin/skill slash-command errors, ambiguous-command hint,
stdin error, gateway start, profile warning. cli_chat_error_copy / cli_unknown_command /
cli_output / cli_init_mixin: chat error panel copy, did-you-mean lines, n-more / yes-no prompt
(localized affirmative initial alongside 'y'), unknown-toolsets warning.

* i18n(w1a): wire agent display/explainers/approval + slash registry/help through t()

- hermes_cli/commands.py: CommandDef.describe() resolves slash.<name>.description
  at call time; category labels via slash.category.*; help/alias/usage suffixes
  via slash.shared.*; gateway_help_lines and commands_platforms/slash_exec use them.
- agent/display.py: display.verb.* resolved at call time via get_tool_verb();
  bridge/spinner/thinking-verb/cute-row/failure/preview/diff text via display.*.
- agent/turn_explainers.py: exit-reason / persistence-cause tables become call-time
  lookups (explainer.exit.*, explainer.persistence.*, explainer.file_mutation.*).
- agent/background_review.py, session_activity.py, context_breakdown.py,
  status_output.py: review summaries, iteration progress, context notices.
- tools/approval.py, approval_context.py: approval.summary.*, approval.noun.*,
  approval.window.* pluralized keys.
- gateway/slash_commands*.py: remaining raw strings (busy, whoami, platform,
  bundles, memory, skills, approvals, set_home, diff, update, debug, profile,
  heartbeat, refine, review, subgoal, loop, retry, compress codex path, save,
  sessions, model guard/errors, agents rows, topup, login). HISTORY_UNREADABLE
  keeps its English constant; callers use history_unreadable() ->
  gateway.shared.history_unreadable.
- locales/en.yaml: new approval/display/explainer/slash blocks + gateway leftovers.

* i18n(telegram): route adapter chat copy through t()

Approval card (header/reason/smart-deny as HTML-escaped properties), inline
button labels, callback toasts (cut at Telegram's 200-char cap), model/choice
pickers, clarify/update/slash-confirm prompts, gmail-triage labels and the
inbound-media failure notice now come from the catalog. _UNAUTHORIZED is a
lazy _unauthorized() so the import no longer binds a language. The command
menu carries a language+payload fingerprint (forum scopes re-register on
change) and BotCommand descriptions are cut at 256.

Adds gateway.exec_approval.* (WAVE2 contract), platform.telegram.*,
platform.discord.* and the slash.*.description keys the Discord table shares
with the CLI registry to locales/en.yaml.

* i18n(gateway/platforms): whatsapp_cloud, yuanbao, weixin, signal, api_server copy through t()

- whatsapp_cloud: clarify list/buttons, approve/deny + slash-confirm labels via platform.whatsapp.* (t()-then-truncate at 20/24/72 caps); _EA_HEADER becomes a property wrapping ea_header_text()
- yuanbao: SLOW_RESPONSE_MESSAGE -> slow_response_message() (platform.yuanbao.slow_response_notice; zh keeps the original text); cron-wrapper markers centralized as module constants for strip_cron_wrapper
- api_server: PROVIDER_AUTH_FAILED_LABEL/PROVIDER_RATE_LIMITED_LABEL stay English for run.py matchers; user_text() renders via t()
- signal/_format_wait, weixin voice caption, openai_routes transformed notice
- run_turn: second _UNEXPECTED_SILENCE_REPLY consumer -> accessor

* i18n(discord): route adapter chat copy through t()

Native slash-command table becomes _NATIVE_SLASH_COMMAND_SPECS holding catalog
keys; _native_slash_commands() resolves descriptions, parameter descriptions
and Choice names for the active language, each cut at Discord's 100-char cap,
and the app-command sync fingerprint now includes get_language() so a
display.language change re-syncs. Exec-approval card (gateway.exec_approval.*
contract), slash-confirm / clarify / update views, model+choice pickers,
thread creation, forum titles, voice acks, the response-truncation notice,
the unauthorized-slash security alert and the media upload-size notices all
read from platform.discord.*. Decorator-declared button labels are relabelled
in __init__ (80-char cap); embed titles cut at 256, select placeholders at
150, option label/description at 100. _UNAUTHORIZED is a lazy _unauthorized().

* i18n(cli): wire status-bar, stream, terminal mixins + terminal_input through t(); rename kwargs that shadow t(key)

* i18n: wire hermes_cli/cli_commands_mixin.py slash-command copy through t()

- 431 new leaves under cli.commands.<cmd>.* in locales/en.yaml; 12 rows reuse
  existing gateway.* keys (rollback, diff, resume, branch, btw, model, reasoning)
  via a _gt() helper so CLI and gateway replies stay identical.
- Module-level English tables (_BUSY_MODE_*, _REASONING_TOGGLES, _HATCH_PROGRESS,
  _DIFF_LABELS, _LOCAL_ENGINE_LINES) become call-time catalog lookups keyed by id.
- Verb tables (Enabling/Disabling, Paused/Resumed/Triggered, planned/done,
  Updating/Generating) are one full template per variant; plurals use
  <key>_one/<key>_other via _tn(); hand-padded column labels (/snapshot list)
  translate the value and re-pad at the call site.
- Multi-line usage blocks are single catalog values split with _lines().
- Model-facing system notes and DB-stored reasons stay English (EXCLUDED).

* tests: assert /handoff, /worktree, /login CLI copy via t(key) instead of pinned English

* test(i18n): pin Telegram/Discord adapter catalog wiring

Lazy unauthorized notice, exec-approval contract keys, HTML escaping before
Telegram <b> wrapping, 200-char toast / 256-char BotCommand caps, Discord
100-char app-command text and 80-char button caps, and language-bearing
command-menu fingerprints on both platforms.

* i18n: reconcile cli.shared on/off vs enabled/disabled after lane merge

* i18n: describe() in TUI-gateway slash listings; localize TUI exit resume hint

* i18n(tr): translate bundled catalog + tui pack

* i18n(ja): translate bundled catalog + tui pack

* i18n(ko): translate bundled catalog + tui pack

* i18n(zh): translate bundled catalog + tui pack

* i18n(fr): translate bundled catalog + tui pack

* i18n(af): translate bundled catalog + tui pack

* i18n(uk): translate bundled catalog + tui pack

* i18n(ar): translate bundled catalog + tui pack

* i18n(pt): translate bundled catalog + tui pack

* i18n(it): translate bundled catalog + tui pack

* i18n(es): translate bundled catalog + tui pack

* i18n(zh-hant): translate bundled catalog + tui pack

* i18n(ru): translate bundled catalog + tui pack

* i18n(hu): translate bundled catalog + tui pack

* i18n(hu): translate pre-existing English-valued leftovers (kanban wake, /context, /status, fast labels)

* i18n(de): translate bundled catalog + tui pack

* i18n(ga): translate bundled catalog + tui pack

* test(i18n): fixture matches _normalize_lang(lang, home) signature

* i18n(tui): scaffold userMessages/slashCmd en siblings

* i18n(tui): wire secure prompts + content tables

* feat(tui): i18n — wire billing, subscription, connection-setup and journey overlays

Adds en siblings billing.ts / subscription.ts / connection.ts (namespaces
billing, subscription, connection, journey) and routes every user-facing
literal in billingOverlay, subscriptionOverlay, connectionSetupOverlay and
journey through useT()/messages(). Module-level label tables became lazy
(scopeStillDeniedResult(), verbOf(T, action)); auto-reload rows dispatch on
stable ids instead of label text. Regenerates locales/_keys.tui.json.

* i18n(tui): wire slash ops/wake replies

* i18n(tui): wire pickers (modelPicker, activeSessionSwitcher, petPicker)

* i18n(tui): wire slash core/debug/setup replies

* i18n(tui): wire hubs (agents overlay/panel/controls, skills, plugins)

* i18n(tui): wire slash session/topup/subscription replies

* i18n(tui): wire chat bits (branding, thinking, messageLine, loaders, todo, queued, banner, entry)

* i18n(tui): register t3 siblings (pickers, hubs, secure, content, chatBits) and regenerate keys

* i18n(tui): wire userMessages copy through the userMessages namespace

* i18n(tui): lazy-copy test for userMessages, regenerate _keys.tui.json

* i18n(tui): wire session/gateway/lib text through the TUI catalog (lane t2)

Adds en siblings session.ts, gatewayMsg.ts, libText.ts (namespaces session,
gatewayMsg, libText) and routes user-facing literals in app/{useMainApp,
useSessionLifecycle,useInputHandlers,turnController,createServerRequestHandler,
setupHandoff,createGatewayEventHandler}.ts, gatewayClient displayed reasons,
lib/*, domain/*, hooks/* through t()/messages(). Status-bar state values that
code compares against, backend-matched strings, log lines, model-bound text,
and machine 'error:' prefixes stay literal. Regenerates locales/_keys.tui.json
(232 keys).

* i18n: serve bundled locales/<lang>.tui.yaml under overlay/packs; TUI pack parity test; regen _keys.tui.json (1250)

* i18n: translate pre-existing English stubs in bundled locales (424 leaves, 14 locales)

* tui: i18n-export-en script (English templates for pack translators)

* docs(i18n): bundled TUI packs are the bottom layer of the tui surface

* i18n(ru): translate TUI pack

* i18n(ar): translate TUI pack

* i18n(es): translate TUI pack

* i18n(pt): translate TUI pack

* i18n(ko): translate TUI pack

* i18n(de): translate TUI pack

* i18n(ja): translate TUI pack

* i18n(fr): translate TUI pack

* i18n(tr): translate TUI pack

* i18n(it): translate TUI pack

* i18n(zh): translate TUI pack

* i18n(zh-hant): translate TUI pack

* i18n(hu): translate TUI pack

* i18n(uk): translate TUI pack

1,169 missing keys translated; 81 pre-existing kept byte-identical. Parity OK missing=0 extra=0 placeholder_mismatch=0 empty=0.

Deliberately identical to en: chatBits.branding.mcpSummary ({0} MCP), chatBits.thinking.agentsHint ((/agents)), session.main.voiceStt (◉ STT), session.main.voiceTtsSuffix ( [tts]), slashCmd.core.help.tuiSection (TUI), slashCmd.core.history.hermesTag (Hermes #{0}), slashCmd.debug.heapdump.heapPath (heapdump: {0}), slashCmd.debug.mem.rss (rss), subscription.stepUp.title (Remote Spending — product feature name, as in core catalog), content.faces.* (glyph-only kaomoji).

* i18n(ga): translate TUI pack

* i18n(af): translate TUI pack

* plugin_guard: locale catalogs in language packs step down the agent-config family

A translated status line such as "Updating AGENTS.md" in locales/<lang>.yaml is UI text the loader
reads as a string leaf; it cannot edit a file. The bundled en.yaml itself tripped agent_config_mod
at critical, making any faithful language pack uninstallable. Injection shapes keep full severity.

* plugin_validate_locales: read key exports with utf-8-sig (Windows footgun lint)

* i18n(relay): route relay adapter prompt copy through t(); drop dead import-bound approval header

Adds platform.relay.* (5 keys) to en and all 16 bundled locales, reusing the sibling platform
translations for the confirm buttons and the Other option.

* ci: fix TUI import order, MDX table pipe, main's overflow-warning wording in all locales; fresh-install fixture carries the i18n kernel

- ui-tui/src/i18n/en.ts: perfectionist/sort-imports (slash before slashCmd)
- docs plugins/index.md: escape the | inside the provides_locales table cell (MDX parsed <id> as JSX)
- display.notice.uncompressed_context_overflow: adopt main's wording (names compression.enabled: false
  and /compact) in en + 16 locales; the guardrail test pins that phrase
- tests/scripts/test_fresh_source_install.py: the installer tail now resolves CLI text through
  agent.i18n, so the fixture tree carries the i18n kernel + en.yaml (not the agent runtime)

* docs(desktop-plugin-sdk): double-backtick the template-literal example (MDX evaluated ${n})

* test(e2e): display.language is validated against the live language set; exclude it from the arbitrary-string set property

* commands: keep the localized COMMANDS/COMMANDS_BY_CATEGORY module __getattr__ after the compat block removal

* build: never write locales/_keys.*.json from the desktop/TUI builds; regenerate the committed desktop key export

The desktop build regenerated locales/_keys.desktop.json in the checkout, so a
hermes update that rebuilt the app left the tree dirty (Desktop update E2E:
'M locales/_keys.desktop.json'). The key exports are committed artifacts pinned
to en.ts by apps/desktop/scripts/i18n-keys.test.mjs and ui-tui i18n:keys:check;
builds read them, never write them. Regenerated after main's new desktop strings.

* test: unbreak two main-red timing tests the PR merge-ref inherits

- test_local_runtime racing fake publishes the modern state record (legacy pid-only
  records are rejected since 65ff3ad353; main has been red on this test since)
- test_run_progress_topics ManyProgressLinesAgent waits for the first bubble instead of
  a fixed 0.35s, which a loaded CI runner does not always meet

* chore(i18n): regenerate desktop key catalog for main's new strings (model pricing, copy changelog)

* test(e2e): torture-chamber fd monitor confirms a deleted sidecar is still held before calling it a leak

SQLite's WAL last-close unlinks -shm before closing its descriptor (unixShmUnmap, then
unixShmPurge), so a healthy close shows a (deleted) -shm for microseconds; the 20ms poll
occasionally caught that window on the short-lived opener and failed the episode.

* chore(i18n): regenerate desktop key catalog for main's telemetry/consent strings

* chore(i18n): regenerate desktop key catalog after main sync

---------

Co-authored-by: Teknium <teknium@nousresearch.com>
2026-09-28 14:16:18 -07:00

1764 lines
71 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Tests for Google Chat platform adapter.
Covers: platform registration, env config loading, adapter init, connect
validation, Pub/Sub callback routing (message / membership / card / error),
outbound send with typing patch-in-place and chunking, attachment send paths,
SSRF guard on attachment download, supervisor reconnect, and authorization
(including the user_id_alt email match for GOOGLE_CHAT_ALLOWED_USERS).
Note: the Google libraries may not be installed in the test environment.
We shim the imports at module load so collection doesn't fail.
"""
import asyncio
import json
import os
import sys
import types
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from agent.i18n import t
from gateway.config import Platform, PlatformConfig, load_gateway_config
# Platform uses _missing_() for dynamic members, so "google_chat" is
# resolvable via Platform("google_chat") even without a static
# GOOGLE_CHAT attribute on the enum class.
_GC = Platform("google_chat")
# ---------------------------------------------------------------------------
# Mock the google-* packages if they are not installed
# ---------------------------------------------------------------------------
class _FakeHttpError(Exception):
"""Stand-in for googleapiclient.errors.HttpError with .resp.status."""
def __init__(self, status=500, content=b"", reason=""):
self.resp = MagicMock()
self.resp.status = status
self.content = content
self.reason = reason
super().__init__(f"HTTP {status}: {reason or 'error'}")
def _ensure_google_mocks():
"""Install mock google-* modules so GoogleChatAdapter can be imported."""
if "google.cloud.pubsub_v1" in sys.modules and hasattr(
sys.modules["google.cloud.pubsub_v1"], "__file__"
):
return # Real libraries installed, use them.
# --- google.cloud.pubsub_v1 ---
google = MagicMock()
google_cloud = MagicMock()
pubsub_v1 = MagicMock()
pubsub_v1.SubscriberClient = MagicMock
pubsub_v1.types.FlowControl = MagicMock
# --- google.api_core.exceptions ---
gax = MagicMock()
gax.NotFound = type("NotFound", (Exception,), {})
gax.PermissionDenied = type("PermissionDenied", (Exception,), {})
gax.Unauthenticated = type("Unauthenticated", (Exception,), {})
# --- google.oauth2.service_account ---
oauth2 = MagicMock()
oauth2.Credentials.from_service_account_info = MagicMock(return_value=MagicMock())
oauth2.Credentials.from_service_account_file = MagicMock(return_value=MagicMock())
# --- google_auth_httplib2 + httplib2 ---
httplib2 = MagicMock()
httplib2.Http = MagicMock()
google_auth_httplib2 = MagicMock()
google_auth_httplib2.AuthorizedHttp = MagicMock()
# --- googleapiclient ---
gapi = MagicMock()
gapi_discovery = MagicMock()
gapi_discovery.build = MagicMock()
gapi_errors = MagicMock()
gapi_errors.HttpError = _FakeHttpError
gapi_http = MagicMock()
gapi_http.MediaFileUpload = MagicMock
modules = {
"google": google,
"google.cloud": google_cloud,
"google.cloud.pubsub_v1": pubsub_v1,
"google.api_core": MagicMock(exceptions=gax),
"google.api_core.exceptions": gax,
"google.oauth2": MagicMock(service_account=oauth2),
"google.oauth2.service_account": oauth2,
"google_auth_httplib2": google_auth_httplib2,
"httplib2": httplib2,
"googleapiclient": gapi,
"googleapiclient.discovery": gapi_discovery,
"googleapiclient.errors": gapi_errors,
"googleapiclient.http": gapi_http,
}
for name, mod in modules.items():
sys.modules.setdefault(name, mod)
_ensure_google_mocks()
# Patch the availability flag before importing, so the adapter doesn't bail
# out at the "missing deps" gate during construction.
#
# Note on imports: Teams' test suite uses
# ``tests.gateway._plugin_adapter_loader.load_plugin_adapter`` to load
# its adapter under a unique ``plugin_adapter_<name>`` module name. That
# helper assumes the plugin is a single ``adapter.py`` file with no
# companion modules — it does not set ``__package__`` on the loaded
# module, so any relative import (e.g. our adapter's ``from .oauth import``)
# raises ``ImportError: attempted relative import with no known parent
# package``.
#
# Our google_chat plugin has a companion ``oauth.py`` module (the
# OAuth helper for native attachment delivery), so we need a real package
# context. The fully-qualified package import below resolves correctly
# because ``plugins/__init__.py`` and ``plugins/platforms/__init__.py``
# exist as regular packages on disk. The conftest anti-pattern guard
# (which targets bare ``import adapter`` / ``from adapter import …`` and
# ``sys.path.insert`` into ``plugins/platforms/``) does not flag this
# fully-qualified form.
import plugins.platforms.google_chat.adapter as _gc_mod # noqa: E402
_gc_mod.GOOGLE_CHAT_AVAILABLE = True
from gateway.platforms.event import MessageEvent, MessageType, ProcessingOutcome # noqa: E402
from plugins.platforms.google_chat.adapter import ( # noqa: E402
GoogleChatAdapter,
_is_google_owned_host,
_mime_for_message_type,
_redact_sensitive,
)
# ---------------------------------------------------------------------------
# Helpers / fixtures
# ---------------------------------------------------------------------------
def _base_config(**extra):
cfg = PlatformConfig(enabled=True)
cfg.extra.update({
"project_id": "test-project",
"subscription_name": "projects/test-project/subscriptions/test-sub",
"service_account_json": "/tmp/fake-sa.json",
})
cfg.extra.update(extra)
return cfg
@pytest.fixture()
def adapter(tmp_path):
"""Build an adapter with its loop captured and Chat client mocked.
Redirects the persistent thread-count store to a tmp file so tests
don't pollute (or read state from) the developer's real
~/.hermes/google_chat_thread_counts.json.
"""
from plugins.platforms.google_chat.adapter import _ThreadCountStore
a = GoogleChatAdapter(_base_config())
a._loop = asyncio.get_event_loop_policy().new_event_loop()
a._chat_api = MagicMock()
a._subscriber = MagicMock()
a._credentials = MagicMock()
a._project_id = "test-project"
a._subscription_path = "projects/test-project/subscriptions/test-sub"
a._new_authed_http = MagicMock(return_value=MagicMock())
a.handle_message = AsyncMock()
# Replace the production store (which would write to ~/.hermes/...)
# with a tmp-path one so tests can roundtrip without side effects.
a._thread_count_store = _ThreadCountStore(
tmp_path / "google_chat_thread_counts.json"
)
yield a
try:
a._loop.close()
except Exception:
pass
def _make_pubsub_message(data: dict, *, attributes=None):
"""Build a Mock Pub/Sub Message with ack/nack trackers."""
msg = MagicMock()
msg.data = json.dumps(data).encode("utf-8")
msg.attributes = attributes or {}
msg.ack = MagicMock()
msg.nack = MagicMock()
return msg
def _make_chat_envelope(text="hello", sender_email="u@example.com", sender_type="HUMAN",
msg_name=None, thread_name=None, attachments=None,
slash_command=None):
"""Build a realistic Google Chat CloudEvents-style envelope body."""
msg = {
"name": msg_name or "spaces/S/messages/M.M",
"sender": {
"name": "users/12345",
"email": sender_email,
"displayName": "User Name",
"type": sender_type,
},
"text": text,
"argumentText": text,
"thread": {"name": thread_name or "spaces/S/threads/T"},
"space": {"name": "spaces/S", "spaceType": "DIRECT_MESSAGE"},
}
if attachments is not None:
msg["attachment"] = attachments
if slash_command is not None:
msg["slashCommand"] = slash_command
return {
"chat": {
"messagePayload": {
"space": msg["space"],
"message": msg,
}
}
}
# ===========================================================================
# Platform registration + requirements
# ===========================================================================
# ===========================================================================
# Env-var config loading
# ===========================================================================
class TestEnvConfigLoading:
_ENV_VARS = (
"GOOGLE_CHAT_PROJECT_ID",
"GOOGLE_CLOUD_PROJECT",
"GOOGLE_CHAT_SUBSCRIPTION_NAME",
"GOOGLE_CHAT_SUBSCRIPTION",
"GOOGLE_CHAT_HTTP_EVENTS_URL",
"GOOGLE_CHAT_HTTP_EVENTS_AUDIENCE",
"GOOGLE_CHAT_HTTP_EVENTS_SERVICE_ACCOUNT_EMAIL",
"GOOGLE_CHAT_SERVICE_ACCOUNT_JSON",
"GOOGLE_APPLICATION_CREDENTIALS",
"GOOGLE_CHAT_HOME_CHANNEL",
"GOOGLE_CHAT_HOME_CHANNEL_NAME",
)
def _clean_env(self, monkeypatch):
for v in self._ENV_VARS:
monkeypatch.delenv(v, raising=False)
def test_missing_subscription_does_not_enable(self, monkeypatch):
self._clean_env(monkeypatch)
monkeypatch.setenv("GOOGLE_CHAT_PROJECT_ID", "p")
# No subscription.
cfg = load_gateway_config()
assert _GC not in cfg.platforms
def test_multiplex_scoped_profile_never_borrows_process_env(
self, monkeypatch, tmp_path
):
"""Under multiplex a scoped profile sees ONLY its own Google Chat
settings, and the ADC branch fails closed instead of authenticating
as the default profile's service account (#73439)."""
from agent.secret_scope import (
build_profile_secret_scope,
set_multiplex_active,
set_secret_scope,
)
self._clean_env(monkeypatch)
monkeypatch.setenv("GOOGLE_CHAT_PROJECT_ID", "default-proj")
monkeypatch.setenv("GOOGLE_CHAT_SUBSCRIPTION_NAME", "default-sub")
monkeypatch.setenv("GOOGLE_APPLICATION_CREDENTIALS", "/secrets/default.json")
monkeypatch.setenv("GOOGLE_CHAT_BOOTSTRAP_SPACES", "spaces/DEFAULT")
profile_home = tmp_path / "beta"
profile_home.mkdir()
(profile_home / ".env").write_text(
"GOOGLE_CHAT_PROJECT_ID=beta-proj\nGOOGLE_CHAT_SUBSCRIPTION_NAME=beta-sub\n"
)
set_multiplex_active(True)
token = set_secret_scope(build_profile_secret_scope(profile_home))
try:
seed = _gc_mod._env_enablement() or {}
beta = GoogleChatAdapter(
PlatformConfig(enabled=True, extra={"project_id": "beta-proj", "subscription_name": "beta-sub"})
)
with pytest.raises(ValueError, match="ADC skipped"):
beta._load_sa_credentials()
finally:
from agent.secret_scope import reset_secret_scope
reset_secret_scope(token)
set_multiplex_active(False)
assert seed["project_id"] == "beta-proj"
assert "service_account_json" not in seed
assert beta._bootstrap_spaces == ""
def test_multiplex_default_profile_constructs_unscoped(self, monkeypatch):
"""The default profile's adapter is built OUTSIDE any scope while
multiplex is active (gateway startup/reconnect); it must keep reading
its own process env instead of raising UnscopedSecretError."""
from agent.secret_scope import set_multiplex_active
self._clean_env(monkeypatch)
monkeypatch.setenv("GOOGLE_CHAT_BOOTSTRAP_SPACES", "spaces/DEFAULT")
set_multiplex_active(True)
try:
default = GoogleChatAdapter(_base_config())
finally:
set_multiplex_active(False)
assert default._bootstrap_spaces == "spaces/DEFAULT"
# ===========================================================================
# Pure helpers
# ===========================================================================
class TestHelpers:
def test_mime_image_maps_to_photo(self):
assert _mime_for_message_type("image/png") == MessageType.PHOTO
def test_mime_audio_maps_to_audio(self):
assert _mime_for_message_type("audio/ogg") == MessageType.AUDIO
class TestRedactSensitive:
def test_redacts_subscription_path(self):
out = _redact_sensitive("error on projects/proj-a/subscriptions/sub-b please")
assert "proj-a" not in out
assert "sub-b" not in out
assert "please" in out # surrounding text preserved
def test_redacts_topic_path(self):
out = _redact_sensitive("publisher on projects/p/topics/t")
assert "projects/p/topics/t" not in out
assert "<redacted>" in out
def test_redacts_service_account_email(self):
out = _redact_sensitive("bot@my-project-123.iam.gserviceaccount.com is the principal")
assert "bot" not in out
assert "my-project-123" not in out
assert "principal" in out
class TestGoogleOwnedHost:
@pytest.mark.parametrize("url", [
"https://chat.googleapis.com/v1/x",
"https://www.googleapis.com/upload/chat/v1/x",
"https://drive.google.com/file/d/abc",
"https://lh3.googleusercontent.com/photo.jpg",
])
def test_accepts_google_hosts(self, url):
assert _is_google_owned_host(url) is True
# ===========================================================================
# Config validation (inside connect())
# ===========================================================================
class TestValidateConfig:
def test_missing_subscription_raises(self):
cfg = PlatformConfig(enabled=True)
cfg.extra["project_id"] = "p"
a = GoogleChatAdapter(cfg)
with pytest.raises(ValueError, match="SUBSCRIPTION"):
a._validate_config()
def test_subscription_project_mismatch_rejected(self):
cfg = _base_config(
subscription_name="projects/other-proj/subscriptions/s",
project_id="my-proj",
)
a = GoogleChatAdapter(cfg)
with pytest.raises(ValueError, match="does not match"):
a._validate_config()
class TestHttpEventIngress:
def test_cached_google_auth_request_reuses_successful_get_response(self, monkeypatch):
now = [100.0]
calls = []
class Response:
status = 200
response = Response()
def raw_request(**kwargs):
calls.append(kwargs)
return response
monkeypatch.setattr(_gc_mod.time, "monotonic", lambda: now[0])
request = _gc_mod._CachedGoogleAuthRequest(raw_request, ttl_seconds=300)
assert request("https://www.googleapis.com/oauth2/v1/certs") is response
assert request("https://www.googleapis.com/oauth2/v1/certs") is response
assert len(calls) == 1
now[0] += 301
assert request("https://www.googleapis.com/oauth2/v1/certs") is response
assert len(calls) == 2
def test_verify_google_id_token_uses_cached_request_and_configured_audience(self, monkeypatch):
request = object()
captured = {}
monkeypatch.setattr(_gc_mod, "_get_google_id_token_request", lambda: request)
class FakeIdToken:
@staticmethod
def verify_oauth2_token(token, req, audience):
captured.update(token=token, request=req, audience=audience)
return {"email": "bot@example.test"}
monkeypatch.setitem(sys.modules, "google.oauth2.id_token", FakeIdToken)
monkeypatch.setattr(sys.modules["google.oauth2"], "id_token", FakeIdToken, raising=False)
assert _gc_mod._verify_google_id_token("signed-token", "https://callback.example/events") == {
"email": "bot@example.test"
}
assert captured == {
"token": "signed-token",
"request": request,
"audience": "https://callback.example/events",
}
@pytest.mark.asyncio
async def test_dispatch_http_event_routes_message_payload(self, adapter):
envelope = _make_chat_envelope(text="hello from http")
result = await adapter.dispatch_http_event(envelope)
assert result == {}
adapter.handle_message.assert_awaited_once()
event = adapter.handle_message.await_args.args[0]
assert event.text == "hello from http"
assert event.source.chat_id == "spaces/S"
class TestConnectModes:
@pytest.mark.asyncio
async def test_connect_http_mode_skips_pubsub_subscriber(self, tmp_path, monkeypatch):
cfg = PlatformConfig(enabled=True)
cfg.extra.update({
"http_events_url": "https://example.test/google-chat/events",
"service_account_json": "{}",
})
a = GoogleChatAdapter(cfg)
a._thread_count_store._path = tmp_path / "google_chat_thread_counts.json"
monkeypatch.setattr(_gc_mod, "_load_google_modules", lambda: True)
monkeypatch.setattr(a, "_load_sa_credentials", MagicMock(return_value=MagicMock()))
monkeypatch.setattr(_gc_mod, "build_service", MagicMock(return_value=MagicMock()))
subscriber_client = MagicMock()
monkeypatch.setattr(_gc_mod, "pubsub_v1", MagicMock(SubscriberClient=subscriber_client))
a._resolve_bot_user_id = AsyncMock(return_value=None)
assert await a.connect() is True
subscriber_client.assert_not_called()
assert a._subscription_path is None
assert a._supervisor_task is None
assert a.is_connected is True
await a.disconnect()
# ===========================================================================
# _chunk_text
# ===========================================================================
# ===========================================================================
# _on_pubsub_message — event routing
# ===========================================================================
class TestOnPubsubMessage:
"""Pub/Sub callback routing. The callback runs in a thread and dispatches
to the asyncio loop; here we assert ack/nack behaviour and that
handle_message is scheduled only for MESSAGE events."""
def test_shutting_down_nacks(self, adapter):
adapter._shutting_down = True
msg = _make_pubsub_message({"whatever": 1})
adapter._on_pubsub_message(msg)
msg.nack.assert_called_once()
msg.ack.assert_not_called()
def test_membership_created_caches_bot_user_id(self, adapter, tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
adapter._bot_user_id = None
envelope = {
"chat": {
"membershipPayload": {
"space": {"name": "spaces/S"},
"membership": {"member": {"name": "users/BOT_ID", "type": "BOT"}},
}
}
}
msg = _make_pubsub_message(
envelope,
attributes={"ce-type": "google.workspace.chat.membership.v1.created"},
)
adapter._on_pubsub_message(msg)
assert adapter._bot_user_id == "users/BOT_ID"
msg.ack.assert_called_once()
def test_relay_flat_bot_sender_is_filtered_end_to_end(self, adapter):
"""Format 3 end-to-end: a relay envelope declaring sender_type=BOT
flows through ``_extract_message_payload`` → ``_on_pubsub_message``
and is dropped by the BOT self-filter without dispatch. This is
the actual security contract (the unit tests on
``_extract_message_payload`` only assert the intermediate dict
shape; this test asserts the dispatch is suppressed).
"""
envelope = {
"event_type": "MESSAGE",
"sender_email": "bot@bots.example.com",
"sender_display_name": "HermesBot",
"sender_type": "BOT",
"text": "reply from bot",
"space_name": "spaces/RELAY",
"message_name": "spaces/RELAY/messages/M.M",
}
msg = _make_pubsub_message(envelope)
with patch.object(adapter, "_submit_on_loop") as submit:
adapter._on_pubsub_message(msg)
submit.assert_not_called()
msg.ack.assert_called_once()
def test_callback_exception_does_not_escape(self, adapter):
env = _make_chat_envelope(text="hola")
msg = _make_pubsub_message(env)
with patch.object(
adapter, "_submit_on_loop", side_effect=RuntimeError("boom")
):
# Must not re-raise (would trigger Pub/Sub infinite redelivery).
adapter._on_pubsub_message(msg)
msg.ack.assert_called_once()
class TestExtractMessagePayload:
"""Three Pub/Sub envelope formats are accepted.
The Workspace Add-ons format (current default) was already exercised
by the rest of TestOnPubsubMessage; these tests pin the contract for
the two alternative formats so the multi-format helper does not
regress when operators have non-standard Chat app configurations.
Patterns adapted from PR #14965 by @ArnarValur.
"""
def test_native_chat_api_format_extracts_msg_and_space(self):
"""Format 2: top-level ``message`` + ``space`` + ``type=MESSAGE``.
Used by Chat apps configured WITHOUT the Workspace Add-ons
wrapper — events arrive directly from the Chat API publisher.
"""
envelope = {
"type": "MESSAGE",
"message": {
"name": "spaces/S/messages/M.M",
"sender": {
"name": "users/12345",
"email": "alice@example.com",
"displayName": "Alice",
"type": "HUMAN",
},
"text": "hello",
"argumentText": "hello",
"thread": {"name": "spaces/S/threads/T"},
},
"space": {"name": "spaces/S", "spaceType": "DIRECT_MESSAGE"},
}
result = GoogleChatAdapter._extract_message_payload(envelope, ce_type="")
assert result is not None
msg, space, fmt = result
assert fmt == "native_chat_api"
assert msg.get("name") == "spaces/S/messages/M.M"
assert msg.get("sender", {}).get("email") == "alice@example.com"
assert space.get("name") == "spaces/S"
assert space.get("spaceType") == "DIRECT_MESSAGE"
def test_relay_flat_format_synthesizes_chat_api_shape(self):
"""Format 3: flat fields from a custom Cloud Run relay.
Some self-hosted setups put a relay in front of Pub/Sub to keep
GCP credentials off the Hermes host. The relay flattens Chat
events into top-level ``sender_email`` / ``text`` / ``space_name``
/ etc. The helper synthesizes a Chat-API-shaped ``message`` dict
so downstream code (``_dispatch_message`` →
``_build_message_event``) consumes it without branching.
"""
envelope = {
"event_type": "MESSAGE",
"sender_email": "bob@example.com",
"sender_display_name": "Bob",
"text": "ping",
"space_name": "spaces/RELAY",
"thread_name": "spaces/RELAY/threads/T1",
"message_name": "spaces/RELAY/messages/M.M",
}
result = GoogleChatAdapter._extract_message_payload(envelope)
assert result is not None
msg, space, fmt = result
assert fmt == "relay_flat"
# Synthesized to look like the canonical Chat API shape so
# _build_message_event reads it the same way as format 1/2.
assert msg["text"] == "ping"
assert msg["argumentText"] == "ping"
assert msg["sender"]["email"] == "bob@example.com"
assert msg["sender"]["displayName"] == "Bob"
assert msg["sender"]["type"] == "HUMAN"
# Resource name is unknown for relay events; helper synthesizes
# a deterministic surrogate so dedup keys stay stable across
# at-least-once redelivery.
assert msg["sender"]["name"].startswith("users/relay-")
assert msg["thread"]["name"] == "spaces/RELAY/threads/T1"
assert msg["name"] == "spaces/RELAY/messages/M.M"
assert space["name"] == "spaces/RELAY"
# ===========================================================================
# _build_message_event — payload parsing
# ===========================================================================
class TestBuildMessageEvent:
@pytest.mark.asyncio
async def test_dm_first_message_in_thread_is_main_flow(self, adapter):
"""Google Chat DMs spawn a fresh thread per top-level user
message in the input box. The FIRST message in any new thread
is treated as 'main flow' — thread_id is NOT propagated to the
source so all top-level messages share one DM session and the
agent retains continuity. The thread is still cached for
outbound reply placement."""
env = _make_chat_envelope(text="hola", thread_name="spaces/S/threads/T1")
msg = env["chat"]["messagePayload"]["message"]
event = await adapter._build_message_event(msg, env)
assert event is not None
assert event.text == "hola"
assert event.source.chat_id == "spaces/S"
# First message in this thread → main-flow → no thread_id on source.
assert event.source.thread_id is None
# Identity convention (post-#14965 absorption): the sender's email
# is the canonical ``user_id``; the Chat resource name moves to
# ``user_id_alt`` for traceability and Chat-API operations.
assert event.source.user_id == "u@example.com"
assert event.source.user_id_alt == "users/12345"
# Cache MUST be empty for main-flow so outbound bot reply lands
# at top-level (Chat creates a separate thread for it). If we
# cached the user's auto-thread name and replied with thread.name
# set, Chat would show the pair as an expandable thread under
# the user's message instead of two adjacent top-level cards.
assert "spaces/S" not in adapter._last_inbound_thread
# Counter populated for next-time decision (persisted store).
assert adapter._thread_count_store.get(
"spaces/S", "spaces/S/threads/T1"
) == 1
@pytest.mark.asyncio
async def test_dm_second_message_in_same_thread_is_side_thread(self, adapter):
"""If we've SEEN a thread before (count > 0), the user explicitly
re-engaged it (clicked 'Reply in thread' on a prior message).
Isolate to its own session so old top-level chatter doesn't
leak in.
Without this isolation the bug Ramón reported reappears: he
opens a new thread, says 'Hola!', asks 'dime los mensajes
anteriores' and the bot answers with messages from OTHER
threads — because all DM threads were sharing one session."""
env1 = _make_chat_envelope(text="primera vez", thread_name="spaces/S/threads/T1")
msg1 = env1["chat"]["messagePayload"]["message"]
event1 = await adapter._build_message_event(msg1, env1)
assert event1.source.thread_id is None # first time = main flow
env2 = _make_chat_envelope(text="segunda vez", thread_name="spaces/S/threads/T1")
msg2 = env2["chat"]["messagePayload"]["message"]
event2 = await adapter._build_message_event(msg2, env2)
# Second time same thread = user re-engaged → isolated session.
assert event2.source.thread_id == "spaces/S/threads/T1"
@pytest.mark.asyncio
async def test_group_keeps_thread_id_on_source(self, adapter):
"""In group spaces, threads are real conversational containers —
keep thread_id on the source from the FIRST message so different
threads get isolated sessions (Telegram forum / Discord thread
parity)."""
env = _make_chat_envelope(text="ping", thread_name="spaces/G/threads/T1")
env["chat"]["messagePayload"]["space"]["spaceType"] = "SPACE"
env["chat"]["messagePayload"]["message"]["space"]["spaceType"] = "SPACE"
msg = env["chat"]["messagePayload"]["message"]
event = await adapter._build_message_event(msg, env)
assert event.source.chat_type == "group"
assert event.source.thread_id == "spaces/G/threads/T1"
# ===========================================================================
# send() — text, patch-in-place, chunking, error handling
# ===========================================================================
class TestSend:
@pytest.mark.asyncio
async def test_text_send_creates_message(self, adapter):
adapter._create_message = AsyncMock(
return_value=type("R", (), {"success": True, "message_id": "m/1",
"error": None})()
)
result = await adapter.send("spaces/S", "hola")
adapter._create_message.assert_called()
assert result.success is True
@pytest.mark.asyncio
async def test_create_message_passes_messageReplyOption_when_thread_set(self, adapter):
"""Critical Google Chat API quirk: when messages.create is called
with body.thread.name set BUT WITHOUT messageReplyOption query
param, Google SILENTLY ignores the thread and creates a new
thread. From official docs: 'Default. Starts a new thread.
Using this option ignores any thread ID or threadKey that's
included.'
This test pins down the messageReplyOption=
REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD parameter so a future
refactor doesn't silently regress threading. (The user-visible
symptom of regression: bot replies land at top-level instead of
inside the user's thread.)"""
# Capture the kwargs handed to .create() — this is what hits
# Google's API. The mock chain is: spaces() -> messages() ->
# create(**kwargs) -> .execute(...).
create_call = MagicMock()
create_call.return_value.execute = MagicMock(
return_value={"name": "spaces/S/messages/M"}
)
adapter._chat_api.spaces.return_value.messages.return_value.create = create_call
body = {
"text": "respuesta",
"thread": {"name": "spaces/S/threads/USER_THREAD"},
}
await adapter._create_message("spaces/S", body)
kwargs = create_call.call_args.kwargs
assert kwargs.get("parent") == "spaces/S"
assert kwargs.get("body") == body
assert kwargs.get("messageReplyOption") == "REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"
@pytest.mark.asyncio
async def test_with_typing_card_patches_instead_of_creating(self, adapter):
adapter._typing_messages["spaces/S"] = "spaces/S/messages/THINK"
adapter._patch_message = AsyncMock(
return_value=type("R", (), {"success": True,
"message_id": "spaces/S/messages/THINK",
"error": None})()
)
adapter._create_message = AsyncMock()
result = await adapter.send(
"spaces/S", "hola",
metadata={"thread_id": "spaces/S/threads/T"},
)
adapter._patch_message.assert_awaited_once()
adapter._create_message.assert_not_called()
assert result.success is True
# After patch, the typing slot holds the consumed sentinel so the
# base class's _keep_typing loop cannot post a fresh marker that
# the cleanup pass would later delete and tombstone.
from plugins.platforms.google_chat.adapter import _TYPING_CONSUMED_SENTINEL
assert adapter._typing_messages["spaces/S"] == _TYPING_CONSUMED_SENTINEL
@pytest.mark.asyncio
async def test_send_clarify_posts_choice_card(self, adapter):
adapter._create_message = AsyncMock(
return_value=type(
"R",
(),
{"success": True, "message_id": "m/1", "error": None, "raw_response": None},
)()
)
result = await adapter.send_clarify(
"spaces/S",
"Pick a demo",
["Simple", "Capability test"],
"clarify123",
"session-key",
)
assert result.success is True
body = adapter._create_message.await_args.args[1]
card = body["cardsV2"][0]
assert card["cardId"] == "clarify-clarify123"
buttons = card["card"]["sections"][0]["widgets"][1]["buttonList"]["buttons"]
assert buttons[0]["text"] == "Simple"
assert buttons[0]["onClick"]["action"]["function"] == "hermes_clarify"
assert {"key": "choice", "value": "Simple"} in buttons[0]["onClick"]["action"]["parameters"]
assert buttons[-1]["text"] == t("platform.google_chat.clarify.other_button")
assert adapter._clarify_state["clarify123"] == "session-key"
# ===========================================================================
# send_typing / stop_typing
# ===========================================================================
class TestTypingLifecycle:
@pytest.mark.asyncio
async def test_send_typing_concurrent_calls_create_only_one_card(self, adapter):
"""When _keep_typing fires send_typing twice in flight (the
first call slow, the second arriving before the first stores
its msg_id), only ONE create should hit the API. Without this
guard the second call would create a duplicate card → orphan
'Hermes is thinking…' stuck in chat. Race fix via
_typing_card_inflight Event.
"""
call_count = 0
first_call_started = asyncio.Event()
release_first_call = asyncio.Event()
async def _slow_create(chat_id, body):
nonlocal call_count
call_count += 1
first_call_started.set()
await release_first_call.wait()
return type("R", (), {"success": True,
"message_id": f"spaces/S/messages/CARD_{call_count}",
"error": None})()
adapter._create_message = _slow_create
# Fire two send_typing tasks concurrently (mimics _keep_typing
# firing while a previous tick is still in-flight).
t1 = asyncio.create_task(adapter.send_typing("spaces/S"))
await first_call_started.wait()
t2 = asyncio.create_task(adapter.send_typing("spaces/S"))
# Give t2 a moment to bail out via the in-flight check.
await asyncio.sleep(0.05)
# Release the first call to complete.
release_first_call.set()
await asyncio.gather(t1, t2)
assert call_count == 1
assert adapter._typing_messages["spaces/S"] == "spaces/S/messages/CARD_1"
@pytest.mark.asyncio
async def test_send_typing_survives_caller_cancellation(self, adapter):
"""base.py's _keep_typing wraps send_typing in
asyncio.wait_for(timeout=1.5). When the create-API call takes
longer than 1.5s, wait_for cancels the awaiter — but the create
itself MUST complete and the msg_id MUST land in the slot,
otherwise the next tick spawns a SECOND card (orphan).
This test simulates that: cancel the awaiter while the create
is in flight. The shielded background task should still
populate the slot.
"""
first_call_started = asyncio.Event()
release_first_call = asyncio.Event()
async def _slow_create(chat_id, body):
first_call_started.set()
await release_first_call.wait()
return type("R", (), {"success": True,
"message_id": "spaces/S/messages/CARD_X",
"error": None})()
adapter._create_message = _slow_create
task = asyncio.create_task(adapter.send_typing("spaces/S"))
await first_call_started.wait()
# Simulate wait_for timeout cancelling the awaiter.
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
# The shielded background create is still running. Release it.
release_first_call.set()
# Give the background task time to complete + record.
for _ in range(20):
await asyncio.sleep(0.05)
if "spaces/S" in adapter._typing_messages:
break
# Slot SHOULD be populated despite the cancellation.
assert adapter._typing_messages.get("spaces/S") == "spaces/S/messages/CARD_X"
@pytest.mark.asyncio
async def test_orphan_typing_cards_reaped_on_completion(self, adapter):
"""If a background send_typing task created a card AFTER send()
already populated the slot (race), the orphan id is tracked in
_orphan_typing_messages. on_processing_complete must patch each
orphan to a benign marker so users don't see stuck
'Hermes is thinking…' messages."""
from plugins.platforms.google_chat.adapter import _TYPING_CONSUMED_SENTINEL
adapter._orphan_typing_messages["spaces/S"] = [
"spaces/S/messages/ORPHAN1",
"spaces/S/messages/ORPHAN2",
]
adapter._typing_messages["spaces/S"] = _TYPING_CONSUMED_SENTINEL
adapter._patch_message = AsyncMock(
return_value=type("R", (), {"success": True,
"message_id": "x",
"error": None})()
)
event = MagicMock()
event.source = MagicMock()
event.source.chat_id = "spaces/S"
await adapter.on_processing_complete(event, ProcessingOutcome.SUCCESS)
# Both orphans patched (typing_messages cleared too).
assert adapter._patch_message.await_count == 2
patched_ids = [
call.args[0] for call in adapter._patch_message.call_args_list
]
assert "spaces/S/messages/ORPHAN1" in patched_ids
assert "spaces/S/messages/ORPHAN2" in patched_ids
assert "spaces/S" not in adapter._orphan_typing_messages
@pytest.mark.asyncio
async def test_stop_typing_is_noop_for_live_card(self, adapter):
"""Anti-tombstone: stop_typing leaves a real msg_id in place so
send() can patch it. Deleting would create a "Message deleted by
its author" tombstone."""
adapter._typing_messages["spaces/S"] = "spaces/S/messages/THINK"
delete_mock = MagicMock()
delete_mock.return_value.execute = MagicMock(return_value={})
adapter._chat_api.spaces.return_value.messages.return_value.delete = delete_mock
await adapter.stop_typing("spaces/S")
# Slot retained, no API delete fired.
assert adapter._typing_messages["spaces/S"] == "spaces/S/messages/THINK"
delete_mock.assert_not_called()
@pytest.mark.asyncio
async def test_on_processing_complete_patches_stranded_card(self, adapter):
"""CANCELLED path: send() never ran. Patch the typing card with a
benign final state instead of deleting (no tombstone)."""
adapter._typing_messages["spaces/S"] = "spaces/S/messages/THINK"
adapter._patch_message = AsyncMock(
return_value=type("R", (), {"success": True,
"message_id": "spaces/S/messages/THINK",
"error": None})()
)
event = MagicMock()
event.source = MagicMock()
event.source.chat_id = "spaces/S"
await adapter.on_processing_complete(event, ProcessingOutcome.CANCELLED)
adapter._patch_message.assert_awaited_once()
# Patched with a final-state label, not deleted.
args, kwargs = adapter._patch_message.call_args
assert "interrupted" in args[1]["text"].lower()
assert "spaces/S" not in adapter._typing_messages
# ===========================================================================
# edit_message / delete_message — required by gateway tool-progress + streaming
# ===========================================================================
class TestEditMessage:
@pytest.mark.asyncio
async def test_edit_message_truncates_overlong_text(self, adapter):
adapter._patch_message = AsyncMock(
return_value=type("R", (), {"success": True, "message_id": "m",
"error": None})()
)
long_text = "x" * 9000
await adapter.edit_message("spaces/S", "spaces/S/messages/M", long_text)
sent = adapter._patch_message.call_args[0][1]["text"]
# Truncated to MAX_MESSAGE_LENGTH (4000) with ellipsis.
assert len(sent) <= 4000
# ===========================================================================
# Native attachment delivery via user OAuth
#
# Google Chat's media.upload endpoint hard-rejects bot/SA auth, so the
# adapter calls it through a SEPARATE user-authed Chat API client built
# from a refresh token the user grants once via /setup-files.
# These tests cover:
# - _send_file falls back to text notice when no user creds present
# - _send_file does the two-step upload + create-with-attachment when
# user creds ARE present
# - the /setup-files slash command intercepts before the agent
# - 401/403 from media.upload triggers a clean fallback (token revoked)
# ===========================================================================
class TestNativeAttachmentDelivery:
@pytest.mark.asyncio
async def test_send_file_two_step_native_upload_when_user_oauth_ready(self, adapter, tmp_path):
"""With user creds, _send_file calls media.upload then
messages.create with the attachmentDataRef — both via the
user-authed Chat client."""
f = tmp_path / "report.pdf"
f.write_bytes(b"%PDF-fake")
upload_call = MagicMock()
upload_call.return_value.execute = MagicMock(
return_value={"attachmentDataRef": {"resourceName": "ref-abc"}}
)
create_call = MagicMock()
create_call.return_value.execute = MagicMock(
return_value={"name": "spaces/S/messages/MID"}
)
adapter._user_chat_api = MagicMock()
adapter._user_chat_api.media.return_value.upload = upload_call
adapter._user_chat_api.spaces.return_value.messages.return_value.create = create_call
adapter._user_credentials = MagicMock(valid=True)
adapter._consume_typing_card_with_text = AsyncMock(return_value=None)
result = await adapter._send_file(
"spaces/S", str(f), caption="caption",
mime_hint="application/pdf",
thread_id="spaces/S/threads/T",
)
assert result.success is True
upload_call.assert_called_once()
create_call.assert_called_once()
# Verify the messages.create body referenced the attachment ref.
body_passed = create_call.call_args.kwargs["body"]
assert body_passed["attachment"][0]["attachmentDataRef"] == {
"resourceName": "ref-abc"
}
class TestSetupFilesSlashCommand:
@pytest.mark.asyncio
async def test_slash_command_intercepted_before_agent(self, adapter):
"""/setup-files is bot-side admin, not agent input. The dispatch
path must short-circuit and not call handle_message."""
adapter._handle_setup_files_command = AsyncMock(return_value=True)
adapter._build_message_event = AsyncMock(
return_value=MessageEvent(
text="/setup-files",
message_type=MessageType.TEXT,
source=adapter.build_source(
chat_id="spaces/S",
chat_name="DM",
chat_type="dm",
user_id="users/1",
user_name="Ramón",
thread_id="spaces/S/threads/T",
),
raw_message={},
message_id="spaces/S/messages/M",
)
)
await adapter._dispatch_message({}, {})
adapter._handle_setup_files_command.assert_awaited_once()
adapter.handle_message.assert_not_called()
class TestUserOAuthHelper:
@staticmethod
def _assert_private_json_file(path, expected):
assert json.loads(path.read_text(encoding="utf-8")) == expected
assert list(path.parent.glob(f"{path.stem}.tmp.*")) == []
if os.name != "nt":
assert (path.stat().st_mode & 0o777) == 0o600
def test_sanitize_email_lowercases_and_replaces_unsafe_chars(self):
"""Path components must be filesystem-safe across users.
``a@B.com`` and ``A@b.com`` must collapse to the same key, and
path-traversal characters must NOT escape into the filename."""
from plugins.platforms.google_chat.oauth import _sanitize_email
assert _sanitize_email("Ramon@NTTData.com") == "ramon@nttdata.com"
assert _sanitize_email("user+tag@x.io") == "user_tag@x.io"
# Slashes are stripped (path separator); dots inside names are
# preserved for the .com / .json suffix UX. The resulting filename
# is harmless when joined onto a directory.
assert _sanitize_email("../etc/passwd") == ".._etc_passwd"
assert _sanitize_email("") == "_unknown_"
def test_load_user_credentials_per_email_returns_none_when_missing(
self, tmp_path, monkeypatch
):
"""A user who has not authorized has no token file; load returns
``None`` and never throws — same contract as the legacy path."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
from plugins.platforms.google_chat.oauth import load_user_credentials
assert load_user_credentials("nobody@example.com") is None
def test_list_authorized_emails_lists_per_user_files(
self, tmp_path, monkeypatch
):
"""``list_authorized_emails`` enumerates the per-user dir; the
legacy file is intentionally excluded (its owner is unknown)."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
users_dir = tmp_path / "google_chat_user_tokens"
users_dir.mkdir(parents=True)
(users_dir / "alice@example.com.json").write_text("{}")
(users_dir / "bob@example.com.json").write_text("{}")
# Legacy file should NOT appear in the list.
(tmp_path / "google_chat_user_token.json").write_text("{}")
from plugins.platforms.google_chat.oauth import list_authorized_emails
assert list_authorized_emails() == [
"alice@example.com", "bob@example.com",
]
def test_store_client_secret_writes_private_json(self, tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
src = tmp_path / "client_secret.json"
payload = {"installed": {"client_id": "cid", "client_secret": "secret"}}
src.write_text(json.dumps(payload), encoding="utf-8")
from plugins.platforms.google_chat.oauth import (
_client_secret_path,
store_client_secret,
)
store_client_secret(str(src))
self._assert_private_json_file(_client_secret_path(), payload)
class TestPerUserAttachmentRouting:
"""The bot must use the *requesting user's* OAuth token when sending
an attachment, not the first user who happened to have one stored.
Backward compat: when no per-user token exists, fall back to a legacy
single-user token; only when both are missing does the user see the
setup-instructions notice."""
@pytest.mark.asyncio
async def test_send_file_uses_per_user_token_when_sender_known(
self, adapter, tmp_path, monkeypatch
):
"""sender_email maps to a per-user file → that user's API client
is built and used for the upload, NOT the legacy fallback."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
users_dir = tmp_path / "google_chat_user_tokens"
users_dir.mkdir(parents=True)
(users_dir / "alice@example.com.json").write_text(json.dumps({
"type": "authorized_user",
"client_id": "cid", "client_secret": "csec",
"refresh_token": "rtok", "token": "atok",
}))
adapter._last_sender_by_chat["spaces/S"] = "alice@example.com"
per_user_api = MagicMock()
per_user_api.media.return_value.upload.return_value.execute.return_value = {
"attachmentDataRef": {"resourceName": "ref-alice"}
}
per_user_api.spaces.return_value.messages.return_value.create.return_value.execute.return_value = {
"name": "spaces/S/messages/MID",
"thread": {"name": "spaces/S/threads/T"},
}
# Force legacy path NOT to be picked even if per-user breaks.
adapter._user_chat_api = MagicMock()
adapter._user_credentials = MagicMock(valid=True)
adapter._consume_typing_card_with_text = AsyncMock(return_value=None)
from plugins.platforms.google_chat import oauth as helper
with patch.object(
helper, "load_user_credentials",
return_value=MagicMock(valid=True),
), patch.object(
helper, "build_user_chat_service", return_value=per_user_api,
):
f = tmp_path / "doc.pdf"
f.write_bytes(b"%PDF")
result = await adapter._send_file(
"spaces/S", str(f), caption=None,
mime_hint="application/pdf",
)
assert result.success is True
# Per-user client was used; legacy was untouched.
per_user_api.media.return_value.upload.assert_called_once()
adapter._user_chat_api.media.assert_not_called()
# Cache populated for next call.
assert "alice@example.com" in adapter._user_chat_api_by_email
@pytest.mark.asyncio
async def test_setup_files_revoke_drops_only_that_user(
self, adapter, tmp_path, monkeypatch
):
"""Per-user revoke clears alice's slot; bob and the legacy
fallback both keep working. Alice's choice to revoke must not
knock out unrelated users."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
adapter._user_chat_api_by_email["alice@example.com"] = MagicMock()
adapter._user_creds_by_email["alice@example.com"] = MagicMock()
adapter._user_chat_api_by_email["bob@example.com"] = MagicMock()
adapter._user_creds_by_email["bob@example.com"] = MagicMock()
legacy_api = MagicMock()
legacy_creds = MagicMock()
adapter._user_chat_api = legacy_api
adapter._user_credentials = legacy_creds
adapter._create_message = AsyncMock(
return_value=type("R", (), {"success": True, "message_id": "m",
"error": None})()
)
from plugins.platforms.google_chat import oauth as helper
with patch.object(helper, "revoke") as rev:
await adapter._handle_setup_files_command(
chat_id="spaces/S",
thread_id=None,
raw_text="/setup-files revoke",
sender_email="alice@example.com",
)
# Helper called with alice's email
assert rev.call_args.args[0] == "alice@example.com"
assert "alice@example.com" not in adapter._user_chat_api_by_email
assert "bob@example.com" in adapter._user_chat_api_by_email
# Legacy fallback survives an unrelated user's revoke.
assert adapter._user_chat_api is legacy_api
assert adapter._user_credentials is legacy_creds
# ===========================================================================
# Persistent thread-count store (restart-safe side-thread heuristic)
# ===========================================================================
class TestThreadCountStore:
def test_missing_file_returns_zero_counts(self, tmp_path):
from plugins.platforms.google_chat.adapter import _ThreadCountStore
store = _ThreadCountStore(tmp_path / "nonexistent.json")
store.load()
assert store.get("spaces/X", "spaces/X/threads/T") == 0
def test_corrupt_json_treated_as_empty(self, tmp_path):
"""A garbage file shouldn't crash the adapter — log warn, treat
as fresh, move on. The next incr() will overwrite."""
from plugins.platforms.google_chat.adapter import _ThreadCountStore
path = tmp_path / "counts.json"
path.write_text("not valid json {")
store = _ThreadCountStore(path)
store.load()
assert store.get("spaces/X", "spaces/X/threads/T") == 0
# Next write should overwrite cleanly.
prev = store.incr("spaces/X", "spaces/X/threads/T")
assert prev == 0
# File now has valid JSON.
import json
data = json.loads(path.read_text())
assert data == {"spaces/X": {"spaces/X/threads/T": 1}}
# ===========================================================================
# Inbound attachment download SSRF guard
# ===========================================================================
class TestAttachmentSSRFGuard:
@pytest.mark.asyncio
async def test_drive_file_with_resource_name_uses_bot_path(self, adapter, tmp_path, monkeypatch):
"""Drag-and-drop chat uploads ALSO carry source=DRIVE_FILE but
come with attachmentDataRef.resourceName — bot media.download_media
works against those. Regression test for the original bug where
we skipped them all (left users with 'I don't see any PDF')."""
attachment = {
"source": "DRIVE_FILE",
"contentType": "application/pdf",
"name": "spaces/S/messages/M/attachments/A",
"attachmentDataRef": {
"resourceName": "spaces/S/messages/M/attachments/A",
},
}
# Patch the inner _fetch_media path by hijacking asyncio.to_thread
# — return some bytes directly, no need to walk the full
# google-api-client mock chain.
async def _fake_to_thread(fn, *args, **kwargs):
return b"%PDF-fake"
monkeypatch.setattr(asyncio, "to_thread", _fake_to_thread)
from plugins.platforms.google_chat import adapter as gc_mod
monkeypatch.setattr(
gc_mod,
"cache_document_from_bytes_async",
AsyncMock(return_value=str(tmp_path / "out.pdf")),
)
path, mime = await adapter._download_attachment(attachment)
assert path == str(tmp_path / "out.pdf")
assert mime == "application/pdf"
# ===========================================================================
# Outbound thread routing (anti-top-level fallback in DMs)
# ===========================================================================
class TestOutboundThreadRouting:
def test_resolve_falls_back_to_cached_thread_for_dm(self, adapter):
"""In DMs the source.thread_id is None, so the metadata passed
to send() lacks a thread. Without the cache fallback, replies
would land at top-level (visually disconnected from the user's
thread)."""
adapter._last_inbound_thread["spaces/X"] = "spaces/X/threads/CACHED"
result = adapter._resolve_thread_id(
reply_to=None,
metadata=None,
chat_id="spaces/X",
)
assert result == "spaces/X/threads/CACHED"
def test_resolve_cron_delivery_does_not_fall_back_to_cached_thread(self, adapter):
"""A cron delivery carries job_id in its metadata and must post as a
new top-level message, never as a reply to the last inbound thread."""
adapter._last_inbound_thread["spaces/X"] = "spaces/X/threads/CACHED"
result = adapter._resolve_thread_id(
reply_to=None,
metadata={"job_id": "cron_123"},
chat_id="spaces/X",
)
assert result is None
def test_resolve_cron_delivery_with_explicit_thread_still_uses_it(self, adapter):
"""An explicit thread in cron metadata wins over the no-fallback rule;
only the implicit _last_inbound_thread cache is bypassed for cron."""
adapter._last_inbound_thread["spaces/X"] = "spaces/X/threads/STALE"
result = adapter._resolve_thread_id(
reply_to=None,
metadata={"job_id": "cron_123", "thread_id": "spaces/X/threads/EXPLICIT"},
chat_id="spaces/X",
)
assert result == "spaces/X/threads/EXPLICIT"
def test_resolve_cron_delivery_with_reply_to_thread_still_uses_it(self, adapter):
"""A reply_to thread resource name still routes a cron delivery there."""
adapter._last_inbound_thread["spaces/X"] = "spaces/X/threads/STALE"
result = adapter._resolve_thread_id(
reply_to="spaces/X/threads/EXPLICIT",
metadata={"job_id": "cron_123"},
chat_id="spaces/X",
)
assert result == "spaces/X/threads/EXPLICIT"
def test_resolve_interactive_dm_with_metadata_still_falls_back(self, adapter):
"""Non-cron metadata (e.g. platform event routing) keeps the DM
fallback — job_id is the only marker that means 'automated delivery'."""
adapter._last_inbound_thread["spaces/X"] = "spaces/X/threads/CACHED"
result = adapter._resolve_thread_id(
reply_to=None,
metadata={"user_id": "u1"},
chat_id="spaces/X",
)
assert result == "spaces/X/threads/CACHED"
# ===========================================================================
# Send file delegation (voice/video/animation route through send_document)
# ===========================================================================
# ===========================================================================
# Outbound retry (transient API failure handling)
# ===========================================================================
class TestOutboundRetry:
"""Outbound message creation retries on transient failures.
Without retry, a single 503/429 from Google's Chat REST API drops the
user-visible reply. The retry wrapper handles 429/5xx/timeout/connection
errors with exponential backoff + jitter; permanent errors (auth,
client errors) bubble up on the first attempt.
Pattern lifted from PR #14965 by @ArnarValur.
"""
@pytest.mark.asyncio
async def test_retries_on_503_then_succeeds(self, adapter, monkeypatch):
"""A 503 from messages.create triggers backoff + retry.
On the second attempt the call succeeds, so the user sees the
reply with no visible failure. The wrapper's sleep is patched
out so the test runs instantly.
"""
from plugins.platforms.google_chat import adapter as gc_mod
async def _no_sleep(*_a, **_kw):
return None
monkeypatch.setattr(gc_mod.asyncio, "sleep", _no_sleep)
# First attempt 503, second attempt OK.
execute = MagicMock()
execute.execute.side_effect = [
_FakeHttpError(status=503, reason="Service unavailable"),
{"name": "spaces/S/messages/M", "thread": {"name": "spaces/S/threads/T"}},
]
adapter._chat_api.spaces.return_value.messages.return_value.create.return_value = execute
result = await adapter._create_message("spaces/S", {"text": "hi"})
assert result.success is True
assert result.message_id == "spaces/S/messages/M"
# Two execute() calls — initial + one retry.
assert execute.execute.call_count == 2
class TestFormatMessage:
"""Markdown→Chat dialect conversion + invisible Unicode stripping.
`format_message` runs on EVERY outbound message, so the regex
behavior is the safety surface. Tests cover happy paths, code-block
protection, edge cases the LLM emits in practice (URLs with parens,
unmatched syntax, mixed bold+italic), and the Unicode strip's
interaction with composite emoji.
Pattern lifted from PR #14965 by @ArnarValur.
"""
def test_bold_double_asterisk_to_single(self):
"""**bold** → *bold* (Chat's bold syntax uses single asterisks)."""
out = GoogleChatAdapter.format_message("hello **world**")
assert out == "hello *world*"
def test_markdown_link_to_chat_anglebracket(self):
"""[text](url) → <url|text> (Slack-style anglebracket links)."""
out = GoogleChatAdapter.format_message("see [docs](https://example.com)")
assert out == "see <https://example.com|docs>"
def test_header_to_bold_at_line_start_only(self):
"""# Title → *Title* but only at line-start; mid-line `#` untouched."""
out = GoogleChatAdapter.format_message("# Heading\nbody with # mid-line hash")
assert out == "*Heading*\nbody with # mid-line hash"
def test_strips_zwj_and_variation_selector(self):
"""ZWJ (U+200D) + Variation Selector 16 (U+FE0F) get stripped.
These appear in composite emoji like 👨‍👩‍👧 (family) — Chat's
restricted font can't render them and shows tofu. Stripping
means the underlying base emoji renders cleanly even if the
composite breaks; better than tofu boxes.
"""
# Family emoji: man + ZWJ + woman + ZWJ + girl.
src = "hello \U0001f468‍\U0001f469‍\U0001f467 world"
out = GoogleChatAdapter.format_message(src)
assert "‍" not in out # ZWJ gone
# Base codepoints survive (man, woman, girl).
assert "\U0001f468" in out
assert "\U0001f469" in out
assert "\U0001f467" in out
class TestADCFallback:
"""When no SA JSON is configured, fall back to Application Default Credentials.
Critical for Cloud Run / GCE / GKE deploys where workload identity
means key files are unnecessary and a security risk to manage.
Pattern lifted from PR #14965.
"""
def test_load_credentials_raises_when_no_sa_and_adc_unavailable(
self, adapter, monkeypatch
):
"""ADC failure surfaces a useful error pointing at the two fixes."""
adapter.config.extra.pop("service_account_json", None)
monkeypatch.delenv("GOOGLE_APPLICATION_CREDENTIALS", raising=False)
monkeypatch.delenv("GOOGLE_CHAT_SERVICE_ACCOUNT_JSON", raising=False)
def _boom(*_a, **_kw):
raise Exception("no credentials")
google_pkg = sys.modules.get("google") or types.SimpleNamespace()
fake_auth_module = types.SimpleNamespace(default=_boom)
monkeypatch.setattr(google_pkg, "auth", fake_auth_module, raising=False)
monkeypatch.setitem(sys.modules, "google", google_pkg)
monkeypatch.setitem(sys.modules, "google.auth", fake_auth_module)
with pytest.raises(ValueError) as ei:
adapter._load_sa_credentials()
msg = str(ei.value).lower()
assert "default credentials" in msg or "adc" in msg
assert "google_chat_service_account_json" in msg
# ===========================================================================
# Supervisor reconnect (backoff + fatal)
# ===========================================================================
class TestSupervisorReconnect:
@pytest.mark.asyncio
async def test_unauthenticated_status_does_not_guess_credential_cause(
self, adapter
):
"""An auth rejection may have causes other than a revoked SA key."""
adapter._subscriber.subscribe.side_effect = (
_gc_mod.gax_exceptions.Unauthenticated("request rejected")
)
await adapter._run_supervisor()
assert adapter.fatal_error_code == "pubsub_auth"
assert adapter.fatal_error_message == (
"Pub/Sub authentication failed; check service-account credentials and gateway logs"
)
@pytest.mark.asyncio
async def test_fatal_after_max_retries(self, adapter, monkeypatch):
"""Simulate 10+ failing subscribe() calls and assert fatal error set."""
# Stub out sleep so the test doesn't actually wait minutes.
async def _instant(*args, **kwargs):
return None
monkeypatch.setattr(
"plugins.platforms.google_chat.adapter.asyncio.sleep", _instant
)
def _fail(*args, **kwargs):
raise RuntimeError("stream died")
adapter._subscriber.subscribe = _fail
# Keep the test fast — run supervisor until it exhausts retries.
await adapter._run_supervisor()
assert adapter.has_fatal_error is True
assert adapter.fatal_error_code == "pubsub_reconnect_exhausted"
# ===========================================================================
# Authorization: email-path check via user_id_alt
# ===========================================================================
class TestAuthorizationEmailMatch:
"""`GOOGLE_CHAT_ALLOWED_USERS=email` matches naturally without a bridge.
Post-#14965 absorption: the adapter sets ``source.user_id =
sender_email`` directly, so the generic allowlist match in
``_is_user_authorized`` finds it without any platform-specific
code path. Pinning here so the bridge can never silently come
back without a test failing.
"""
def test_allowlist_matches_when_user_id_is_email(self, monkeypatch, tmp_path):
"""Email allowlist match — the canonical case.
The adapter assigns ``user_id = sender_email`` so the generic
check_ids path picks it up. No platform-specific bridge needed.
"""
from pathlib import Path
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.setattr(Path, "home", lambda: tmp_path)
from gateway.config import GatewayConfig
from gateway.run import GatewayRunner
from gateway.session import SessionSource
from hermes_cli.plugins import discover_plugins
monkeypatch.setenv("GOOGLE_CHAT_ALLOWED_USERS", "alice@example.com")
# Plugin platforms become available during the normal gateway startup
# discovery pass. This unit test constructs GatewayRunner directly,
# so perform that lifecycle step explicitly before testing auth.
discover_plugins()
cfg = GatewayConfig()
runner = GatewayRunner(cfg)
runner.pairing_store = MagicMock()
runner.pairing_store.is_approved = MagicMock(return_value=False)
source = SessionSource(
platform=_GC,
chat_id="spaces/S",
chat_type="dm",
user_id="alice@example.com", # post-swap: email is canonical
user_name="Alice",
user_id_alt="users/12345", # resource name moves to alt
)
assert runner._is_user_authorized(source) is True
# ===========================================================================
# Cron scheduler registry (regression guard from /review)
#
# After the generic-plugin-interface migration, Google Chat no longer lives in
# the hardcoded ``_KNOWN_DELIVERY_PLATFORMS`` / ``_HOME_TARGET_ENV_VARS`` sets
# in ``cron/scheduler.py``. It earns cron delivery via
# ``PlatformEntry.cron_deliver_env_var``, which the scheduler consults through
# ``_is_known_delivery_platform`` and ``_resolve_home_env_var``. The tests
# below check that public resolver behavior, not the hardcoded sets.
# ===========================================================================
class TestCronSchedulerRegistry:
def _ensure_registered(self):
"""Force the plugin system to register the Google Chat adapter.
The adapter's ``register(ctx)`` is only invoked during plugin
discovery; module-level import alone does not register it. We call
discover + manually invoke the register hook so the resolver sees
``cron_deliver_env_var``.
"""
from gateway.platform_registry import platform_registry
if platform_registry.get("google_chat") is not None:
return
# Discover first so the plugin is loaded at all.
try:
from hermes_cli.plugins import discover_plugins
discover_plugins()
except Exception:
pass
if platform_registry.get("google_chat") is not None:
return
# Fallback: construct a minimal ctx and call register directly.
from plugins.platforms.google_chat.adapter import register as _register
class _Ctx:
class _M:
name = "google_chat-platform"
manifest = _M()
_manager = type("_Mgr", (), {"_plugin_platform_names": set()})()
def register_platform(self, **kwargs):
from gateway.platform_registry import PlatformEntry
entry = PlatformEntry(source="plugin", **kwargs)
platform_registry.register(entry)
_register(_Ctx())
def test_google_chat_is_known_delivery_platform(self):
self._ensure_registered()
from cron.scheduler_delivery import _is_known_delivery_platform
assert _is_known_delivery_platform("google_chat") is True
# ── _standalone_send (out-of-process cron delivery) ──────────────────────
class _FakeAiohttpResponse:
def __init__(self, status: int, payload, text_body: str = ""):
self.status = status
self._payload = payload
self._text = text_body or (str(payload) if payload is not None else "")
async def json(self):
return self._payload
async def text(self):
return self._text
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc, tb):
return None
class _FakeAiohttpSession:
def __init__(self, scripts):
self._scripts = list(scripts)
self.calls: list[tuple[str, dict]] = []
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc, tb):
return None
def post(self, url, **kwargs):
self.calls.append((url, kwargs))
if not self._scripts:
raise AssertionError(f"No scripted response for POST {url}")
return self._scripts.pop(0)
def _install_fake_aiohttp(monkeypatch, session):
fake_aiohttp = types.SimpleNamespace(
ClientSession=lambda timeout=None, **kwargs: session,
ClientTimeout=lambda total=None: None,
)
monkeypatch.setitem(sys.modules, "aiohttp", fake_aiohttp)
def _install_fake_google_auth_transport(monkeypatch):
fake_request_module = types.SimpleNamespace(Request=lambda: object())
monkeypatch.setitem(sys.modules, "google.auth.transport", types.SimpleNamespace(requests=fake_request_module))
monkeypatch.setitem(sys.modules, "google.auth.transport.requests", fake_request_module)
class TestGoogleChatStandaloneSend:
@pytest.mark.asyncio
async def test_standalone_send_refreshes_token_and_posts_message(
self, monkeypatch, tmp_path
):
sa_file = tmp_path / "sa.json"
sa_file.write_text(json.dumps({
"type": "service_account",
"client_email": "bot@example.iam.gserviceaccount.com",
"private_key": "fake",
"token_uri": "https://example/token",
}))
monkeypatch.setenv("GOOGLE_CHAT_SERVICE_ACCOUNT_JSON", str(sa_file))
fake_creds = MagicMock()
fake_creds.token = "the-token"
fake_creds.refresh = MagicMock(return_value=None)
original = _gc_mod.service_account.Credentials.from_service_account_info
_gc_mod.service_account.Credentials.from_service_account_info = MagicMock(
return_value=fake_creds
)
try:
_install_fake_google_auth_transport(monkeypatch)
send_resp = _FakeAiohttpResponse(200, {"name": "spaces/AAA/messages/MMM"})
session = _FakeAiohttpSession([send_resp])
_install_fake_aiohttp(monkeypatch, session)
result = await _gc_mod._standalone_send(
PlatformConfig(enabled=True, extra={}),
"spaces/AAAA-BBBB",
"hello cron",
)
finally:
_gc_mod.service_account.Credentials.from_service_account_info = original
assert result == {
"success": True,
"message_id": "spaces/AAA/messages/MMM",
}
fake_creds.refresh.assert_called_once()
assert len(session.calls) == 1
url, kwargs = session.calls[0]
assert url == "https://chat.googleapis.com/v1/spaces/AAAA-BBBB/messages"
assert kwargs["headers"]["Authorization"] == "Bearer the-token"
assert kwargs["json"] == {"text": "hello cron"}