// Live API: typed setup, client messages, server event decoding, session, // relay. The wire names are checked against live_spec.h (which the // tools/check_discovery.py script checks against Google's Discovery // documents). No Google traffic: the loopback server only carries bytes. #include #include #include #include #include "framework.h" #include "google/errors.h" #include "google/live/live_config.h" #include "google/live/live_relay.h" #include "google/live/live_session.h" #include "google/live/live_spec.h" #include "loopback.h" #include "security/crypto/Base64.h" using namespace molo::google; using namespace molo::google::live; using molo::clients::body::Json; using molo::security::crypto::Base64; using live_test::LoopbackServer; using live_test::ServerConn; namespace { void collect_keys(const Json& j, std::set& out) { if (j.is_object()) { for (const auto& [k, v] : j.as_object()) { out.insert(k); collect_keys(v, out); } } else if (j.is_array()) { for (const auto& v : j.as_array()) collect_keys(v, out); } } std::set spec_fields() { std::set s; for (std::size_t i = 0; i < kLiveSpecRows; ++i) s.insert(kLiveSpec[i].field); return s; } LiveEndpoint local_endpoint(const LoopbackServer& s, bool vertex = false) { LiveEndpoint e; if (vertex) { CloudContext ctx; ctx.project = "test-project"; e = LiveEndpoint::vertex(ctx, "tok"); } else { e = LiveEndpoint::api_key("test-key"); } e.url_override = s.url(); return e; } // Reads events until `kind` (inclusive) or timeout; returns everything seen. std::vector until(LiveSession& s, LiveEventKind kind, int ms = 3000) { std::vector seen; auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(ms); while (std::chrono::steady_clock::now() < deadline) { auto e = s.next_event(100); if (!e) { if (!s.open() && s.pending() == 0) break; continue; } seen.push_back(*e); if (e->kind == kind) break; } return seen; } } // namespace TEST(live_endpoint, urls_headers_models) { CloudContext ctx; ctx.project = "p1"; ctx.location = "europe-west4"; auto v = LiveEndpoint::vertex(ctx, "tok"); ASSERT_EQ(v.url(), "wss://europe-west4-aiplatform.googleapis.com/ws/" "google.cloud.aiplatform.v1.LlmBidiService/BidiGenerateContent"); ASSERT_EQ(v.headers().at(0).first, "Authorization"); ASSERT_EQ(v.headers().at(0).second, "Bearer tok"); ASSERT_EQ(v.model_name("m1"), "projects/p1/locations/europe-west4/publishers/google/models/m1"); ctx.location = "global"; ASSERT_TRUE(LiveEndpoint::vertex(ctx, "t").url().find("wss://aiplatform.googleapis.com/ws/") == 0); auto x = LiveEndpoint::vertex_express(ctx, "AQ.k"); ASSERT_EQ(x.headers().at(0).first, "x-goog-api-key"); ASSERT_TRUE(x.url().find("AQ.k") == std::string::npos); // express key never in the URL auto a = LiveEndpoint::api_key("K1"); ASSERT_EQ(a.url(), std::string(kLiveApiKeyUrl) + "?key=K1"); ASSERT_TRUE(a.headers().empty()); ASSERT_EQ(a.model_name("m1"), "models/m1"); ASSERT_THROW(a.model_name("../x"), GoogleError); ASSERT_THROW(a.model_name(""), GoogleError); } TEST(live_endpoint, validation_before_traffic) { CloudContext ctx; ctx.project = "p1"; ASSERT_THROW(LiveEndpoint::api_key("").validate(), NotConfiguredError); ASSERT_THROW(LiveEndpoint::vertex(ctx, "").validate(), NotConfiguredError); ASSERT_THROW(LiveEndpoint::api_key("a&b").validate(), GoogleError); ASSERT_THROW(LiveEndpoint::api_key("a b").validate(), GoogleError); ASSERT_THROW(LiveEndpoint::vertex(ctx, "t\r\nX: y").validate(), GoogleError); ctx.project = "Bad Project"; ASSERT_THROW(LiveEndpoint::vertex(ctx, "t").validate(), GoogleError); ctx.project = "p1"; ctx.location = "us central"; ASSERT_THROW(LiveEndpoint::vertex(ctx, "t").validate(), GoogleError); // connect() without a credential throws before any socket is opened. LiveSession s(LiveEndpoint::api_key(""), LiveConfig{}); ASSERT_THROW(s.connect(), NotConfiguredError); } TEST(live_config, setup_matches_spec_names) { LiveConfig c; c.system_instruction = "be brief"; c.voice_name = "Puck"; c.language_code = "nl-NL"; c.temperature = 0.5; c.max_output_tokens = 100; c.input_transcription = c.output_transcription = true; c.session_resumption = true; c.context_compression = true; LiveFunctionDeclaration f; f.name = "get_weather"; f.description = "weather"; f.parameters = Json::parse("{\"type\":\"OBJECT\",\"properties\":{\"city\":{\"type\":\"STRING\"}}}"); c.functions.push_back(f); CloudContext ctx; ctx.project = "p"; Json j = c.to_setup(LiveEndpoint::vertex(ctx, "t")); ASSERT_TRUE(j.has("setup")); const Json& s = *j.find("setup"); ASSERT_EQ(s.find("model")->as_string(), "projects/p/locations/us-central1/publishers/google/models/" + c.model); ASSERT_EQ(s.find("generationConfig")->find("responseModalities")->as_array().at(0).as_string(), "AUDIO"); ASSERT_EQ(s.find("generationConfig")->find("speechConfig")->find("voiceConfig") ->find("prebuiltVoiceConfig")->find("voiceName")->as_string(), "Puck"); ASSERT_EQ(s.find("systemInstruction")->find("parts")->as_array().at(0).find("text")->as_string(), "be brief"); ASSERT_TRUE(s.has("inputAudioTranscription") && s.has("outputAudioTranscription")); ASSERT_TRUE(s.find("sessionResumption")->is_object()); ASSERT_TRUE(s.find("contextWindowCompression")->find("slidingWindow")->is_object()); // Every key anywhere in the message, except user-supplied schema content // under "parameters", is a declared wire name. auto fields = spec_fields(); fields.insert("setup"); std::set keys; Json copy = c.to_setup(LiveEndpoint::api_key("k")); Json& decls = copy.as_object().at("setup").as_object().at("tools").as_array().at(0) .as_object().at("functionDeclarations").as_array().at(0); decls.as_object().erase("parameters"); collect_keys(copy, keys); for (const auto& k : keys) ASSERT_TRUE(fields.count(k) == 1 || k == "setup"); ASSERT_EQ(copy.find("setup")->find("model")->as_string(), "models/" + c.model); } TEST(live_config, validation) { auto ep = LiveEndpoint::api_key("k"); auto bad = [&](auto mutate) { LiveConfig c; mutate(c); ASSERT_THROW(c.to_setup(ep), GoogleError); }; bad([](LiveConfig& c) { c.model = "a/b"; }); bad([](LiveConfig& c) { c.response_modalities = {}; }); bad([](LiveConfig& c) { c.response_modalities = {"VIDEO"}; }); bad([](LiveConfig& c) { c.temperature = 3.0; }); bad([](LiveConfig& c) { c.max_output_tokens = 0; }); bad([](LiveConfig& c) { c.voice_name = "bad voice!"; }); bad([](LiveConfig& c) { c.language_code = "nl_NL"; }); bad([](LiveConfig& c) { c.system_instruction = std::string(LiveCaps::kMaxSystemChars + 1, 'x'); }); bad([](LiveConfig& c) { c.resumption_handle = "has space"; }); bad([](LiveConfig& c) { c.functions.resize(LiveCaps::kMaxFunctionDecls + 1); for (std::size_t i = 0; i < c.functions.size(); ++i) c.functions[i].name = "f" + std::to_string(i); }); bad([](LiveConfig& c) { c.functions.push_back({"1bad", "", Json()}); }); bad([](LiveConfig& c) { c.functions.push_back({"ok", "", Json("not an object")}); }); } TEST(live_requests, shapes_and_caps) { Json t = Json::parse(LiveRequests::text("hi")); ASSERT_EQ(t.find("realtimeInput")->find("text")->as_string(), "hi"); Json turn = Json::parse(LiveRequests::turn("hello", false)); ASSERT_EQ(turn.find("clientContent")->find("turns")->as_array().at(0).find("role")->as_string(), "user"); ASSERT_FALSE(turn.find("clientContent")->find("turnComplete")->as_bool()); Json a = Json::parse(LiveRequests::audio(std::string("\x01\x02\x03", 3), "audio/pcm;rate=16000")); const Json* blob = a.find("realtimeInput")->find("audio"); ASSERT_EQ(blob->find("mimeType")->as_string(), "audio/pcm;rate=16000"); std::string raw; ASSERT_TRUE(Base64::decode(blob->find("data")->as_string(), raw)); ASSERT_EQ(raw.size(), (std::size_t)3); Json v = Json::parse(LiveRequests::video("jpegbytes", "image/jpeg")); ASSERT_TRUE(v.find("realtimeInput")->has("video")); ASSERT_TRUE(Json::parse(LiveRequests::audio_stream_end()).find("realtimeInput")->find("audioStreamEnd")->as_bool()); Json resp = Json::object(); resp.set("temp", 21); Json tr = Json::parse(LiveRequests::tool_responses({{"call-1", "get_weather", resp}})); const Json& fr = tr.find("toolResponse")->find("functionResponses")->as_array().at(0); ASSERT_EQ(fr.find("id")->as_string(), "call-1"); ASSERT_EQ(fr.find("name")->as_string(), "get_weather"); ASSERT_THROW(LiveRequests::text(""), GoogleError); ASSERT_THROW(LiveRequests::text(std::string(LiveCaps::kMaxTextChars + 1, 'x')), GoogleError); ASSERT_THROW(LiveRequests::audio("", "audio/pcm"), GoogleError); ASSERT_THROW(LiveRequests::audio("x", "image/png"), GoogleError); ASSERT_THROW(LiveRequests::audio("x", "audio/pcm\r\n"), GoogleError); ASSERT_THROW(LiveRequests::audio(std::string(LiveCaps::kMaxAudioChunkBytes + 1, 'x'), "audio/pcm"), GoogleError); ASSERT_THROW(LiveRequests::video(std::string(LiveCaps::kMaxVideoChunkBytes + 1, 'x'), "image/jpeg"), GoogleError); ASSERT_THROW(LiveRequests::video("x", "text/plain"), GoogleError); ASSERT_THROW(LiveRequests::tool_responses({}), GoogleError); ASSERT_THROW(LiveRequests::tool_responses({{"", "n", resp}}), GoogleError); ASSERT_THROW(LiveRequests::tool_responses({{"i", "n", Json("str")}}), GoogleError); std::vector many(LiveCaps::kMaxFunctionResponses + 1, LiveToolResponse{"i", "n", resp}); ASSERT_THROW(LiveRequests::tool_responses(many), GoogleError); // Client message envelopes are declared names. auto fields = spec_fields(); for (const std::string& m : {LiveRequests::text("x"), LiveRequests::turn("x", true), LiveRequests::audio("x", "audio/pcm"), LiveRequests::video("x", "image/jpeg"), LiveRequests::audio_stream_end(), LiveRequests::tool_responses({{"i", "n", resp}})}) { std::set keys; Json j = Json::parse(m); // "response" content is user data. collect_keys(j, keys); for (const auto& k : keys) ASSERT_TRUE(fields.count(k) == 1 || k == "temp"); } } TEST(live_events, decode_server_messages) { auto one = [](const std::string& json) { return LiveParser::parse(json); }; auto ev = one("{\"setupComplete\":{}}"); ASSERT_EQ(ev.size(), (std::size_t)1); ASSERT_TRUE(ev[0].kind == LiveEventKind::SetupComplete); std::string pcm("\x10\x00\x20\x00", 4); ev = one("{\"serverContent\":{\"modelTurn\":{\"parts\":[{\"text\":\"hi\"},{\"inlineData\":{\"mimeType\":" "\"audio/pcm;rate=24000\",\"data\":\"" + Base64::encode(pcm) + "\"}}]},\"outputTranscription\":" "{\"text\":\"hi\"},\"generationComplete\":true,\"turnComplete\":true}}"); ASSERT_EQ(ev.size(), (std::size_t)5); ASSERT_TRUE(ev[0].kind == LiveEventKind::Text); ASSERT_TRUE(ev[1].kind == LiveEventKind::Audio); ASSERT_EQ(ev[1].data, pcm); ASSERT_EQ(ev[1].mime, "audio/pcm;rate=24000"); ASSERT_TRUE(ev[2].kind == LiveEventKind::OutputTranscription); ASSERT_TRUE(ev[3].kind == LiveEventKind::GenerationComplete); ASSERT_TRUE(ev[4].kind == LiveEventKind::TurnComplete); // always last ev = one("{\"serverContent\":{\"inputTranscription\":{\"text\":\"you\"},\"interrupted\":true}}"); ASSERT_TRUE(ev[0].kind == LiveEventKind::InputTranscription); ASSERT_TRUE(ev[1].kind == LiveEventKind::Interrupted); ev = one("{\"toolCall\":{\"functionCalls\":[{\"id\":\"c1\",\"name\":\"f\",\"args\":{\"x\":1}}]}}"); ASSERT_TRUE(ev[0].kind == LiveEventKind::ToolCall); ASSERT_EQ(ev[0].calls.at(0).id, "c1"); ASSERT_EQ(ev[0].calls.at(0).args.find("x")->as_int(), 1L); ev = one("{\"toolCallCancellation\":{\"ids\":[\"c1\",\"c2\"]}}"); ASSERT_EQ(ev[0].ids.size(), (std::size_t)2); ev = one("{\"goAway\":{\"timeLeft\":\"12.5s\"}}"); ASSERT_TRUE(ev[0].seconds == 12.5); ev = one("{\"sessionResumptionUpdate\":{\"newHandle\":\"h1\",\"resumable\":true}}"); ASSERT_EQ(ev[0].handle, "h1"); ASSERT_TRUE(ev[0].resumable); ev = one("{\"usageMetadata\":{\"totalTokenCount\":42}}"); ASSERT_EQ(ev[0].total_tokens, 42L); // Loud on the unexpected. ASSERT_TRUE(one("not json")[0].kind == LiveEventKind::Error); ASSERT_TRUE(one("[1]")[0].kind == LiveEventKind::Error); ASSERT_TRUE(one("{\"somethingNew\":{}}")[0].kind == LiveEventKind::Unknown); ev = one("{\"serverContent\":{\"modelTurn\":{\"parts\":[{\"inlineData\":{\"mimeType\":\"audio/pcm\",\"data\":\"!!\"}}]}}}"); ASSERT_TRUE(ev[0].kind == LiveEventKind::Error); ASSERT_TRUE(LiveParser::parse_duration("0.5s") == 0.5); ASSERT_TRUE(LiveParser::parse_duration("5") < 0); ASSERT_TRUE(LiveParser::parse_duration("-1s") < 0); ASSERT_TRUE(LiveParser::parse_duration("xs") < 0); } TEST(live_session, full_conversation_over_loopback) { std::string setup_seen, text_seen, audio_seen, tool_seen; LoopbackServer server([&](ServerConn& c) { setup_seen = c.read_message().value_or(""); c.send_text("{\"setupComplete\":{}}"); text_seen = c.read_message().value_or(""); // Binary frames carry JSON too (as Gemini does). c.send_binary("{\"serverContent\":{\"modelTurn\":{\"parts\":[{\"text\":\"hello\"}]}}}"); c.send_text("{\"serverContent\":{\"turnComplete\":true}}"); audio_seen = c.read_message().value_or(""); c.send_text("{\"toolCall\":{\"functionCalls\":[{\"id\":\"c1\",\"name\":\"f\",\"args\":{}}]}}"); tool_seen = c.read_message().value_or(""); c.send_text("{\"sessionResumptionUpdate\":{\"newHandle\":\"h-123\",\"resumable\":true}}"); c.send_text("{\"goAway\":{\"timeLeft\":\"5s\"}}"); c.read_frame(); // the client's close c.send_close(1000); }); LiveConfig cfg; cfg.system_instruction = "hi"; LiveSession s(local_endpoint(server), cfg); s.connect(); ASSERT_TRUE(s.open()); ASSERT_TRUE(Json::parse(setup_seen).has("setup") || setup_seen.empty() == false); s.send_text("ping"); auto evs = until(s, LiveEventKind::TurnComplete); ASSERT_TRUE(!evs.empty()); ASSERT_TRUE(evs.front().kind == LiveEventKind::SetupComplete); // queued during connect bool saw_text = false; for (auto& e : evs) saw_text |= e.kind == LiveEventKind::Text && e.text == "hello"; ASSERT_TRUE(saw_text); for (std::size_t i = 1; i < evs.size(); ++i) ASSERT_TRUE(evs[i].seq > evs[i - 1].seq); s.send_audio("pcm-bytes"); auto tc = until(s, LiveEventKind::ToolCall); ASSERT_TRUE(tc.back().kind == LiveEventKind::ToolCall); Json resp = Json::object(); resp.set("ok", true); s.send_tool_responses({{tc.back().calls.at(0).id, "f", resp}}); auto rest = until(s, LiveEventKind::GoAway); ASSERT_TRUE(rest.back().kind == LiveEventKind::GoAway); ASSERT_TRUE(rest.back().seconds == 5.0); ASSERT_EQ(s.resumption_handle(), "h-123"); s.close(); ASSERT_FALSE(s.open()); auto closing = until(s, LiveEventKind::Closed, 500); ASSERT_TRUE(!closing.empty() && closing.back().kind == LiveEventKind::Closed); ASSERT_THROW(s.send_text("late"), GoogleError); s.close(); // idempotent ASSERT_TRUE(setup_seen.find("\"setup\"") != std::string::npos); ASSERT_TRUE(setup_seen.find("systemInstruction") != std::string::npos); ASSERT_EQ(text_seen, "{\"realtimeInput\":{\"text\":\"ping\"}}"); ASSERT_TRUE(audio_seen.find("\"audio\"") != std::string::npos); ASSERT_TRUE(audio_seen.find("audio/pcm;rate=16000") != std::string::npos); ASSERT_TRUE(tool_seen.find("functionResponses") != std::string::npos); auto st = s.stats(); ASSERT_EQ(st.sent, (std::uint64_t)3); } TEST(live_session, vertex_headers_reach_the_server) { LoopbackServer server([](ServerConn& c) { c.read_message(); c.send_text("{\"setupComplete\":{}}"); c.read_frame(); }); LiveSession s(local_endpoint(server, true), LiveConfig{}); s.connect(); s.close(); ASSERT_TRUE(server.request().find("Authorization: Bearer tok") != std::string::npos); } TEST(live_session, server_close_before_setup_is_an_error_with_reason) { LoopbackServer server([](ServerConn& c) { c.read_message(); c.send_close(1008, "Publisher model was not found"); }); LiveSession s(local_endpoint(server), LiveConfig{}); try { s.connect(); FAIL("expected failure"); } catch (const GoogleError& e) { ASSERT_EQ(e.status(), "FAILED_PRECONDITION"); ASSERT_TRUE(e.message().find("1008") != std::string::npos); ASSERT_TRUE(e.message().find("Publisher model was not found") != std::string::npos); } ASSERT_FALSE(s.open()); } TEST(live_session, handshake_errors_map_to_google_statuses) { struct C { LoopbackServer::Handshake mode; int http; const char* status; }; for (C c : {C{LoopbackServer::Handshake::Status403, 403, "PERMISSION_DENIED"}, C{LoopbackServer::Handshake::BadAccept, 502, "UNAVAILABLE"}, C{LoopbackServer::Handshake::CloseImmediately, 502, "UNAVAILABLE"}}) { LoopbackServer server([](ServerConn&) {}, c.mode); LiveSession s(local_endpoint(server), LiveConfig{}); try { s.connect(); FAIL("expected failure"); } catch (const GoogleError& e) { ASSERT_EQ(e.http_status(), c.http); ASSERT_EQ(e.status(), c.status); ASSERT_TRUE(e.message().find("test-key") == std::string::npos); } } } TEST(live_session, setup_timeout_is_bounded) { LoopbackServer server([](ServerConn& c) { c.read_message(); std::this_thread::sleep_for(std::chrono::milliseconds(1200)); // never sends setupComplete }); LiveOptions opt; opt.setup_timeout_ms = 400; LiveSession s(local_endpoint(server), LiveConfig{}, opt); auto t0 = std::chrono::steady_clock::now(); try { s.connect(); FAIL("expected timeout"); } catch (const GoogleError& e) { ASSERT_EQ(e.http_status(), 504); } ASSERT_TRUE(std::chrono::steady_clock::now() - t0 < std::chrono::milliseconds(1100)); } TEST(live_session, event_queue_is_bounded_and_counts_drops) { const int flood = (int)LiveCaps::kMaxQueuedEvents + 300; LoopbackServer server([&](ServerConn& c) { c.read_message(); c.send_text("{\"setupComplete\":{}}"); for (int i = 0; i < flood; ++i) c.send_text("{\"serverContent\":{\"modelTurn\":{\"parts\":[{\"text\":\"t" + std::to_string(i) + "\"}]}}}"); c.send_text("{\"serverContent\":{\"turnComplete\":true}}"); c.read_frame(); }); LiveSession s(local_endpoint(server), LiveConfig{}); s.connect(); auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5); while (s.stats().received < (std::uint64_t)flood + 1 && std::chrono::steady_clock::now() < deadline) std::this_thread::sleep_for(std::chrono::milliseconds(20)); ASSERT_TRUE(s.pending() <= LiveCaps::kMaxQueuedEvents); ASSERT_TRUE(s.stats().dropped > 0); // Newest survives: the last event is the turnComplete. auto evs = until(s, LiveEventKind::TurnComplete); ASSERT_TRUE(evs.back().kind == LiveEventKind::TurnComplete); s.close(); } TEST(live_session, abnormal_disconnect_surfaces_closed_event) { LoopbackServer server([](ServerConn& c) { c.read_message(); c.send_text("{\"setupComplete\":{}}"); }); LiveSession s(local_endpoint(server), LiveConfig{}); s.connect(); auto evs = until(s, LiveEventKind::Closed); ASSERT_TRUE(evs.back().kind == LiveEventKind::Closed); ASSERT_EQ(evs.back().code, 1006); ASSERT_FALSE(s.open()); } TEST(live_session, lifetime_cap_closes_session) { LoopbackServer server([](ServerConn& c) { c.read_message(); c.send_text("{\"setupComplete\":{}}"); while (c.read_frame()) { } }); LiveOptions opt; opt.max_session_seconds = 1; LiveSession s(local_endpoint(server), LiveConfig{}, opt); s.connect(); auto evs = until(s, LiveEventKind::Closed, 4000); ASSERT_TRUE(evs.back().kind == LiveEventKind::Closed); ASSERT_TRUE(evs.back().text.find("lifetime") != std::string::npos); } TEST(live_session, send_validates_before_the_wire) { LoopbackServer server([](ServerConn& c) { c.read_message(); c.send_text("{\"setupComplete\":{}}"); while (c.read_frame()) { } }); LiveSession s(local_endpoint(server), LiveConfig{}); s.connect(); ASSERT_THROW(s.send_text(""), GoogleError); ASSERT_THROW(s.send_audio("x", "video/mp4"), GoogleError); ASSERT_THROW(s.send_video(std::string(LiveCaps::kMaxVideoChunkBytes + 1, 'x')), GoogleError); ASSERT_EQ(s.stats().sent, (std::uint64_t)0); s.close(); } TEST(live_relay, create_poll_cursor_close_and_caps) { LoopbackServer server([](ServerConn& c) { c.read_message(); c.send_text("{\"setupComplete\":{}}"); auto m = c.read_message(); c.send_text("{\"serverContent\":{\"modelTurn\":{\"parts\":[{\"text\":\"echo\"}]}}}"); c.send_text("{\"serverContent\":{\"turnComplete\":true}}"); (void)m; while (c.read_frame()) { } }); LiveRelay relay(1); std::string id = relay.create(local_endpoint(server), LiveConfig{}); ASSERT_TRUE(LiveRelay::valid_id(id)); ASSERT_EQ(relay.size(), (std::size_t)1); // Second concurrent session is refused (cap 1). try { relay.create(local_endpoint(server), LiveConfig{}); FAIL("expected cap"); } catch (const GoogleError& e) { ASSERT_EQ(e.http_status(), 429); } relay.get(id)->send_text("hi"); std::vector all; std::uint64_t cursor = 0; for (int i = 0; i < 40 && (all.empty() || all.back().kind != LiveEventKind::TurnComplete); ++i) { auto page = relay.poll(id, cursor, 100); for (auto& e : page.events) all.push_back(e); cursor = page.next_cursor; std::this_thread::sleep_for(std::chrono::milliseconds(50)); } ASSERT_TRUE(all.back().kind == LiveEventKind::TurnComplete); // Cursor semantics: polling again from the end returns nothing new. ASSERT_TRUE(relay.poll(id, cursor, 100).events.empty()); // Page size is capped. ASSERT_TRUE(relay.poll(id, 0, 100000).events.size() <= LiveCaps::kMaxRelayPage); Json j = event_to_json(all.front()); ASSERT_TRUE(j.has("seq") && j.has("type")); relay.close(id); ASSERT_EQ(relay.size(), (std::size_t)0); ASSERT_THROW(relay.poll(id, 0, 10), GoogleError); ASSERT_THROW(relay.close(id), GoogleError); ASSERT_THROW(relay.get("../etc"), GoogleError); } TEST(live_relay, json_never_leaks_resumption_handle) { LiveEvent e; e.kind = LiveEventKind::SessionResumption; e.handle = "SECRET-HANDLE"; e.resumable = true; ASSERT_TRUE(event_to_json(e).dump().find("SECRET-HANDLE") == std::string::npos); } // Opt-in real network checks: MOLO_LIVE_NET=1 plus credentials from the // environment (never printed): GOOGLE_CLOUD_PROJECT and either // GOOGLE_API_KEY (express key, x-goog-api-key) or GOOGLE_ACCESS_TOKEN (OAuth // bearer). One handshake, one short text turn, close. Loud SKIP otherwise. namespace { bool live_net_enabled(const char* test, const char* credential_var) { const char* on = std::getenv("MOLO_LIVE_NET"); if (!on || std::string(on) != "1" || !std::getenv(credential_var) || !std::getenv("GOOGLE_CLOUD_PROJECT")) { std::printf(" SKIP %s: set MOLO_LIVE_NET=1, %s and GOOGLE_CLOUD_PROJECT to run against " "the real Vertex Live endpoint\n", test, credential_var); return false; } return true; } void live_text_turn(const LiveEndpoint& endpoint) { LiveConfig cfg; cfg.output_transcription = true; LiveSession s(endpoint, cfg); s.connect(); s.send_text("Say hello in one short sentence."); auto evs = until(s, LiveEventKind::TurnComplete, 30000); bool audio = false, transcript = false; for (auto& e : evs) { audio |= e.kind == LiveEventKind::Audio && !e.data.empty(); transcript |= e.kind == LiveEventKind::OutputTranscription && !e.text.empty(); } s.close(); ASSERT_TRUE(evs.back().kind == LiveEventKind::TurnComplete); ASSERT_TRUE(audio); ASSERT_TRUE(transcript); } } // namespace TEST(live_net, vertex_express_text_turn) { if (!live_net_enabled("live_net.vertex_express_text_turn", "GOOGLE_API_KEY")) return; CloudContext ctx = CloudContext::from_env(); live_text_turn(LiveEndpoint::vertex_express(ctx, std::getenv("GOOGLE_API_KEY"))); } TEST(live_net, vertex_oauth_text_turn) { if (!live_net_enabled("live_net.vertex_oauth_text_turn", "GOOGLE_ACCESS_TOKEN")) return; CloudContext ctx = CloudContext::from_env(); live_text_turn(LiveEndpoint::vertex(ctx, std::getenv("GOOGLE_ACCESS_TOKEN"))); }