|
10 | 10 |
|
11 | 11 | from __future__ import annotations |
12 | 12 |
|
| 13 | +import json |
13 | 14 | import os |
14 | 15 | import shutil |
15 | 16 | import subprocess |
| 17 | +from urllib import error as urllib_error |
| 18 | +from urllib import request as urllib_request |
16 | 19 |
|
17 | 20 | import pytest |
18 | 21 |
|
@@ -57,6 +60,39 @@ def _run_agent( |
57 | 60 | ) |
58 | 61 |
|
59 | 62 |
|
| 63 | +def _run_gemini_gateway_smoke(workspace: str, model: str, token: str) -> str: |
| 64 | + """Call the Gemini gateway directly with a text-only prompt. |
| 65 | +
|
| 66 | + This keeps auth recovery coverage focused on the recovered Databricks token |
| 67 | + instead of Gemini CLI's separate tool-calling request shape. |
| 68 | + """ |
| 69 | + url = f"{build_tool_base_url('gemini', workspace)}/v1beta/models/{model}:generateContent" |
| 70 | + payload = { |
| 71 | + "contents": [ |
| 72 | + {"role": "user", "parts": [{"text": "say hi in 5 words or less"}]}, |
| 73 | + ], |
| 74 | + } |
| 75 | + req = urllib_request.Request( |
| 76 | + url, |
| 77 | + data=json.dumps(payload).encode("utf-8"), |
| 78 | + headers={ |
| 79 | + "Authorization": f"Bearer {token}", |
| 80 | + "Content-Type": "application/json", |
| 81 | + "Accept": "application/json", |
| 82 | + }, |
| 83 | + method="POST", |
| 84 | + ) |
| 85 | + try: |
| 86 | + with urllib_request.urlopen(req, timeout=30) as response: |
| 87 | + body = response.read().decode("utf-8") |
| 88 | + except urllib_error.HTTPError as exc: |
| 89 | + body = exc.read().decode("utf-8", errors="replace") if exc.fp else "" |
| 90 | + raise AssertionError(f"Gemini gateway smoke failed: HTTP {exc.code}: {body[:500]}") from exc |
| 91 | + |
| 92 | + data = json.loads(body) |
| 93 | + return data.get("candidates", [{}])[0].get("content", {}).get("parts", [{}])[0].get("text", "") |
| 94 | + |
| 95 | + |
60 | 96 | # --------------------------------------------------------------------------- |
61 | 97 | # Databricks auth / token |
62 | 98 | # --------------------------------------------------------------------------- |
@@ -878,11 +914,4 @@ def test_recovers_when_initial_token_empty( |
878 | 914 | "get_databricks_token may not be retrying after auth login." |
879 | 915 | ) |
880 | 916 |
|
881 | | - env = gemini.build_runtime_env(e2e_workspace, model, recovered_token) |
882 | | - cmd = gemini.validate_cmd("gemini") |
883 | | - result = _run_agent(cmd, env=env, timeout=90) |
884 | | - combined = (result.stdout + result.stderr).strip() |
885 | | - assert result.returncode == 0 and combined, ( |
886 | | - f"Gemini failed after auth recovery: rc={result.returncode} " |
887 | | - f"stdout={result.stdout[:300]!r} stderr={result.stderr[:300]!r}" |
888 | | - ) |
| 917 | + assert _run_gemini_gateway_smoke(e2e_workspace, model, recovered_token).strip() |
0 commit comments