// Routes: POST /agent/live/sessions, POST .../{id}/send, GET .../{id}/events, // POST .../{id}/close (HTTP relay over a loopback WebSocket server) #include #include #include #include "../../../src/google/live/loopback.h" #include "support/GoogleRig.hpp" using namespace molo::example::test; using live_test::LoopbackServer; using live_test::ServerConn; namespace { std::string json_of(const std::string& body, const std::string& key) { std::size_t k = body.find("\"" + key + "\":\""); if (k == std::string::npos) return ""; k += key.size() + 4; return body.substr(k, body.find('"', k) - k); } } // namespace int main() { unsetenv("GOOGLE_ACCESS_TOKEN"); unsetenv("GOOGLE_CLOUD_PROJECT"); setenv("GOOGLE_API_KEY", "test-key", 1); // No credential: every Google-bound route answers 503, nothing connects. { GoogleRig rig(false); Fixture f(rig.client); auto res = f.client->post("/agent/live/sessions", "{}", "application/json"); check(res.status == 503, "create without a credential is 503"); check(contains(res.body, "google_not_configured"), "error code google_not_configured"); check(f.client->post("/agent/live/sessions/s0123456789ab/send", "{\"kind\":\"text\",\"text\":\"x\"}", "application/json").status == 503, "send without a credential is 503"); check(f.client->post("/agent/live/sessions/s0123456789ab/close", "{}", "application/json").status == 503, "close without a credential is 503"); } std::string text_seen, audio_seen; LoopbackServer server([&](ServerConn& c) { c.read_message(); // setup c.send_text("{\"setupComplete\":{}}"); text_seen = c.read_message().value_or(""); c.send_text("{\"serverContent\":{\"modelTurn\":{\"parts\":[{\"text\":\"hello from loopback\"}]}}}"); c.send_text("{\"serverContent\":{\"turnComplete\":true}}"); audio_seen = c.read_message().value_or(""); while (c.read_frame()) { } }); setenv("GOOGLE_LIVE_URL", server.url().c_str(), 1); GoogleRig rig; Fixture f(rig.client); // Validation: rejected before any socket is opened. check(f.client->post("/agent/live/sessions", "{\"modality\":\"VIDEO\"}", "application/json").status == 422, "bad modality is 422"); check(f.client->post("/agent/live/sessions", "{\"model\":\"../x\"}", "application/json").status == 422, "bad model is 422"); check(f.client->post("/agent/live/sessions", "{\"voice\":\"a b;c\"}", "application/json").status == 422, "bad voice is 422"); check(f.client->get("/agent/live/sessions").status == 405, "get on create is 405"); auto created = f.client->post("/agent/live/sessions", "{\"system\":\"be brief\",\"transcribe\":\"1\"}", "application/json"); check(created.status == 201, "create answers 201"); std::string id = json_of(created.body, "id"); check(id.size() == 13 && id[0] == 's', "session id shape"); check(!contains(created.body, "test-key"), "credential never returned"); std::string base = "/agent/live/sessions/" + id; check(f.client->post(base + "/send", "{\"kind\":\"smoke\"}", "application/json").status == 422, "unknown kind is 422"); check(f.client->post(base + "/send", "{\"kind\":\"text\",\"text\":\"\"}", "application/json").status == 422, "empty text is 422"); check(f.client->post(base + "/send", "{\"kind\":\"text\",\"text\":\"" + std::string(32001, 'a') + "\"}", "application/json").status == 422, "text over the cap is 422"); check(f.client->post(base + "/send", "{\"kind\":\"audio\",\"data_b64\":\"!!\"}", "application/json").status == 422, "bad base64 is 422"); check(f.client->post("/agent/live/sessions/s000000000000/send", "{\"kind\":\"text\",\"text\":\"x\"}", "application/json").status == 404, "unknown session is 404"); check(f.client->get(base + "/send").status == 405, "get on send is 405"); auto sent = f.client->post(base + "/send", "{\"kind\":\"text\",\"text\":\"hi there\"}", "application/json"); check(sent.status == 200 && contains(sent.body, "\"sent\":true"), "text sent"); // Poll by cursor until the model text arrives. std::string cursor = "0", all; for (int i = 0; i < 40 && !contains(all, "turn_complete"); ++i) { auto page = f.client->get(base + "/events?cursor=" + cursor); check(page.status == 200, "events answers 200"); all += page.body; std::string next = json_of(page.body, "next_cursor"); std::size_t k = page.body.find("\"next_cursor\":"); if (k != std::string::npos) cursor = page.body.substr(k + 14, page.body.find_first_of(",}", k) - k - 14); std::this_thread::sleep_for(std::chrono::milliseconds(50)); } check(contains(all, "hello from loopback"), "model text relayed"); check(contains(all, "\"type\":\"turn_complete\""), "turn_complete relayed"); check(contains(f.client->get(base + "/events?cursor=" + cursor).body, "\"events\":[]"), "cursor: nothing new after the last event"); check(f.client->get(base + "/events?cursor=abc").status == 422, "bad cursor is 422"); check(f.client->get("/agent/live/sessions/s000000000000/events").status == 404, "unknown session events is 404"); auto audio = f.client->post(base + "/send", "{\"kind\":\"audio\",\"data_b64\":\"AQID\",\"mime\":\"audio/pcm;rate=16000\"}", "application/json"); check(audio.status == 200, "audio chunk sent"); check(f.client->post(base + "/send", "{\"kind\":\"audio\",\"data_b64\":\"AQID\",\"mime\":\"image/png\"}", "application/json").status == 422, "audio with an image mime is 422"); auto closed = f.client->post(base + "/close", "{}", "application/json"); check(closed.status == 200 && contains(closed.body, "\"closed\":true"), "close answers 200"); check(f.client->post(base + "/close", "{}", "application/json").status == 404, "closing twice is 404"); check(f.client->get(base + "/events").status == 404, "closed session events is 404"); check(contains(text_seen, "\"realtimeInput\"") && contains(text_seen, "hi there"), "side effect: the loopback server got the text turn"); check(contains(audio_seen, "audio/pcm;rate=16000") && contains(audio_seen, "AQID"), "side effect: the loopback server got the audio chunk"); check(contains(server.request(), "Sec-WebSocket-Key"), "side effect: a real WebSocket upgrade happened"); check(rig.mock->send_count() == 0, "no HTTP traffic to Google"); return finish("agent/live/sessions"); }