Skip to content

HarnessRouter Community Edition: the self-hosted, Apache-2.0 edition of the unified interface for agent harnesses. Run Codex, Claude Code, Hermes, PI, DSH, and more through one API, with sessions, streaming, files, cancellation, and failure handling. Implements the Unified Harness Protocol (UHP), an open standard. Your keys, your infrastructure.

3.8 KB
Raw Blame
"""The gemini backend (Gemini CLI, Google's native API) on the gateway side: wired into the ONE google
provider, refused in broker mode with the reason, the served model kept on the record."""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import app as gw  # noqa: E402


def test_the_gemini_backend_is_wired_into_the_one_google_provider():
    assert gw._INTEGRATION_WIRING[("google", "gemini")] == "google"
    assert gw._PROVIDER_CATALOG["google"]["base_url"] == "https://generativelanguage.googleapis.com/v1beta/openai"
    # every Gemini CLI model runs on a Google key, except one Google answers with another model;
    # that one runs where it is served as itself (TokenRouter, the hosted service)
    assert set(gw._MODEL_CATALOG["gemini"]["models"]) - gw._GOOGLE_ANSWERS_AS_ANOTHER <= set(gw._VENDOR_MODELS["google"])
    assert gw._GOOGLE_ANSWERS_AS_ANOTHER <= set(gw._VENDOR_MODELS["tokenrouter"])
    assert gw._MODEL_CATALOG["gemini"]["default"] == "gemini-3.8-flash"
    assert gw._MODEL_CATALOG["gemini"]["default"] in gw._MODEL_CATALOG["gemini"]["models"]
    assert gw._BASE_CATALOG["gemini"]["backend"] == "gemini"


def test_a_native_api_backend_is_refused_in_broker_mode_and_served_raw_in_owner_trust(monkeypatch):
    conn = {"name": "integration:Google AI Studio", "backend": "gemini", "provider": "google", "api_key": "AIza-t"}
    monkeypatch.setattr(gw, "SANDBOX_TRUST", "broker")
    monkeypatch.setattr(gw, "PUBLIC_BASE_URL", "https://hr.example")
    assert gw._auth_from_conn(conn, "sid1") is None
    # the same connection brokers fine for an OpenAI-shape backend
    assert gw._auth_from_conn({**conn, "backend": "pi", "provider": "openai-api"}, "sid1") is not None
    monkeypatch.setattr(gw, "SANDBOX_TRUST", "owner")
    out = gw._auth_from_conn(conn, "sid1")
    assert out and out.get("api_key") == "AIza-t"


def test_the_served_model_rides_the_result_onto_the_record():
    ev = {"type": "result", "result": "PONG", "usage": {"input_tokens": 1, "output_tokens": 1}, "model": "gemini-3.5-flash"}
    assert ("result", {"text": "PONG", "usage": ev["usage"], "is_error": False, "model": "gemini-3.5-flash"}) in gw._blocks_from_canonical(ev)
    plain = {"type": "result", "result": "ok", "usage": None}
    assert ("result", {"text": "ok", "usage": None, "is_error": False, "model": ""}) in gw._blocks_from_canonical(plain)
    tr = gw._RespTranslator("resp_1", "gemini-3.6-flash", None, True, 0.0)
    tr._handle("result", {"text": "PONG", "usage": {"input_tokens": 1, "output_tokens": 1}, "is_error": False, "model": "gemini-3.5-flash"})
    assert tr.served_model == "gemini-3.5-flash"
    assert tr.record()["served_model"] == "gemini-3.5-flash" if hasattr(tr, "record") else True


def test_tokenrouter_drives_the_gemini_backend_for_the_ids_it_serves():
    assert gw._INTEGRATION_WIRING[("tokenrouter", "gemini")] == "google"
    tr = {"name": "My TokenRouter", "provider": "tokenrouter", "config": {"api_key": "t", "base_url": "https://api.tokenrouter.com/v1"}}
    google = {"name": "Google AI Studio", "provider": "google", "config": {"api_key": "g"}}
    assert gw._integration_driving([tr, google], "gemini", "gemini-3.8-flash", "My TokenRouter") is tr
    assert "gemini-3.8-flash" in gw._integration_models(tr) and "gemini-3.1-flash-lite" not in gw._integration_models(tr)
    # an id TokenRouter has no channel for falls through to the Google integration when the map named TokenRouter for it
    assert gw._integration_driving([tr, google], "gemini", "gemini-3.1-flash-lite", "Google AI Studio") is google


def test_the_failed_turn_reason_is_the_runners_error_before_its_result():
    src = Path(gw.__file__).read_text()
    assert '"error": (s.get("error") or s.get("result") or "")[:2000]' in src
    assert '"native_model": ((conn.get("model") if conn.get("_model_resolved") else _map_model(conn, model_req))' in src