test(google-workspace): type and shrink empty Gmail search regression test

Replace the hand-rolled fake service with MagicMock, patch through
monkeypatch, and annotate the test and gmail_search().
This commit is contained in:
unsupportedpastels
2026-10-04 22:04:36 +00:00
committed by Mark S.
parent 78f1cd1394
commit af8839df10
2 changed files with 11 additions and 21 deletions

View File

@@ -275,7 +275,7 @@ def build_service(api, version):
# =========================================================================
def gmail_search(args):
def gmail_search(args: argparse.Namespace) -> None:
if _gws_binary():
results = _run_gws(
["gmail", "users", "messages", "list"],

View File

@@ -197,26 +197,16 @@ def test_api_get_credentials_refresh_persists_authorized_user_type(api_module, m
assert saved["type"] == "authorized_user"
def test_gmail_search_empty_result_prints_json_array(api_module, capsys):
"""Regression for #131711: an empty Gmail search through the Python
fallback printed "No messages found." instead of the JSON array the gws
backend emits, breaking consumers that parse search output as JSON."""
class EmptyGmail:
def users(self):
return self
def messages(self):
return self
def list(self, **_kwargs):
return self
def execute(self):
return {"resultSizeEstimate": 0}
api_module._gws_binary = lambda: None
api_module.build_service = lambda *_args, **_kwargs: EmptyGmail()
def test_gmail_search_empty_result_prints_json_array(
api_module: types.ModuleType,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Python-fallback empty search prints [] like the gws path (#131711)."""
service = MagicMock()
service.users().messages().list().execute.return_value = {"resultSizeEstimate": 0}
monkeypatch.setattr(api_module, "_gws_binary", lambda: None)
monkeypatch.setattr(api_module, "build_service", lambda *_args: service)
api_module.gmail_search(api_module.argparse.Namespace(query="is:unread", max=10))