feat(clients): google cloud voice: generic client, TTS, STT, Gemini audio, live voice page

src/google: GoogleClient over molo::clients (key in x-goog-api-key, never in
URLs), Service base, TextToSpeech, SpeechToText, Generative (replies, audio
in, audio out), Recognizer/Synthesizer seams with cloud, gemini and vertex
backends, VoicePipeline with bounded history. Example: /voice live
conversation page, /voice/{say,hear,turn,reset}, /google/status. Keys load
from env, .env.json.local, .env.json. Paid routes are excluded from locust.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-24 04:05:41 +02:00
co-authored by Claude Sonnet 5.5
parent dd773d3015
commit 837de58136
54 changed files with 3086 additions and 21 deletions
+32
View File
@@ -0,0 +1,32 @@
// Route: GET /google/status (credential state and known Google services)
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
{
GoogleRig rig;
Fixture f(rig.client);
auto res = f.client->get("/google/status");
check(res.status == 200, "status 200");
check(contains(res.headers.get("Content-Type"), "application/json"), "json");
check(contains(res.body, "\"configured\":true"), "configured with a key");
check(contains(res.body, "\"name\":\"texttospeech\"") &&
contains(res.body, "\"name\":\"speech\"") &&
contains(res.body, "\"name\":\"generativelanguage\""),
"lists the known services");
check(contains(res.body, "https://texttospeech.googleapis.com/v1"),
"base urls shown");
check(!contains(res.body, kTestKey), "credential never shown");
check(rig.mock->send_count() == 0, "side effect: no upstream traffic");
}
{
GoogleRig rig(false);
Fixture f(rig.client);
check(contains(f.client->get("/google/status").body, "\"configured\":false"),
"not configured without a key");
}
Fixture f;
check(f.client->post("/google/status").status == 405, "post is 405");
return finish("google/status");
}
+14 -1
View File
@@ -4,9 +4,11 @@
// test file owns one main() and uses these helpers for loud assertions.
#include <iostream>
#include <optional>
#include <string>
#include "app/App.hpp"
#include "google/client.h"
#include "fw/TestClient.hpp"
namespace molo::example::test {
@@ -40,7 +42,18 @@ struct Fixture {
std::shared_ptr<molo::example::fw::Application> app;
std::shared_ptr<molo::example::fw::TestClient> client;
Fixture() {
Fixture() : Fixture(nullptr) {}
// A scripted Google client can be injected (see support/GoogleRig.hpp).
// Null builds the client from the environment like production. Scripted
// clients default to the classic cloud wire paths; pass a backend to test
// the Gemini ones.
explicit Fixture(std::shared_ptr<molo::google::GoogleClient> google,
std::optional<molo::google::VoiceBackend> backend =
std::nullopt) {
if (google && !backend) backend = molo::google::VoiceBackend::kCloud;
services.voice_backend = backend;
services.google = std::move(google);
app = molo::example::app::build_application(services);
client = std::make_shared<molo::example::fw::TestClient>(app);
}
+69
View File
@@ -0,0 +1,69 @@
#pragma once
// Scripted Google backend for route tests: a GoogleClient over MockTransport
// plus helpers that queue the three upstream replies a voice turn needs.
// The real client, auth, request building and error mapping all run.
#include <memory>
#include <string>
#include "clients/transports/mock_transport.h"
#include "google/credentials.h"
#include "security/crypto/Base64.h"
#include "support/Assert.hpp"
namespace molo::example::test {
inline const char* kTestKey = "AIzaTESTKEY-DO-NOT-LEAK";
struct GoogleRig {
molo::clients::transports::MockTransport* mock = nullptr;
std::shared_ptr<molo::google::GoogleClient> client;
explicit GoogleRig(bool with_key = true) {
auto transport = std::make_unique<molo::clients::transports::MockTransport>();
mock = transport.get();
client = std::make_shared<molo::google::GoogleClient>(
std::move(transport),
with_key ? molo::google::Credentials::api_key(kTestKey) : nullptr);
}
static molo::clients::ClientResponse json(const std::string& body,
int status = 200) {
molo::clients::HeaderMap h;
h.set("Content-Type", "application/json");
return molo::clients::ClientResponse(status, h, body);
}
void tts(const std::string& audio) {
mock->expect_response(
molo::clients::Method::kPost, "/v1/text:synthesize",
json("{\"audioContent\":\"" +
molo::security::crypto::Base64::encode(audio) + "\"}"));
}
void stt(const std::string& text) {
mock->expect_response(
molo::clients::Method::kPost, "/v1/speech:recognize",
text.empty() ? json("{}")
: json("{\"results\":[{\"alternatives\":[{\"transcript\":\"" +
text + "\",\"confidence\":0.95}]}]}"));
}
void gemini(const std::string& text) {
mock->expect_response(
molo::clients::Method::kPost, "/v1beta/models/rig-model:generateContent",
json("{\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"" + text +
"\"}]}}]}"));
}
void upstream_error(const std::string& path, int status,
const std::string& google_status,
const std::string& message) {
mock->expect_response(
molo::clients::Method::kPost, path,
json("{\"error\":{\"code\":" + std::to_string(status) +
",\"message\":\"" + message + "\",\"status\":\"" +
google_status + "\"}}",
status));
}
};
} // namespace molo::example::test
+40
View File
@@ -0,0 +1,40 @@
// Route: POST /voice/hear (audio bytes to transcript through Google STT)
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
GoogleRig rig;
rig.stt("goedemorgen");
Fixture f(rig.client);
auto res = f.client->post("/voice/hear?lang=nl-NL", "WEBM-AUDIO-BYTES",
"audio/webm;codecs=opus");
check(res.status == 200, "status 200");
check(contains(res.headers.get("Content-Type"), "application/json"), "json");
check(contains(res.body, "\"text\":\"goedemorgen\""), "transcript returned");
auto seen = rig.mock->seen();
check(seen.size() == 1 && contains(seen[0].body, "WEBM_OPUS") &&
contains(seen[0].body, "nl-NL"),
"side effect: encoding and language sent upstream");
check(!contains(seen[0].body, "WEBM-AUDIO-BYTES"), "audio is base64, not raw");
check(f.client->post("/voice/hear", "xx", "video/mp4").status == 415,
"unknown media type is 415");
check(f.client->post("/voice/hear", "", "audio/webm").status == 422,
"empty audio is 422");
check(f.client->post("/voice/hear?lang=zz", "xx", "audio/webm").status == 422,
"bad language is 422");
std::string big(1024 * 1024 + 8192, 'a');
check(f.client->post("/voice/hear", big, "audio/webm").status == 413,
"body over the 1 MiB cap is 413");
check(rig.mock->send_count() == 1, "rejected input never reached Google");
check(f.client->get("/voice/hear").status == 405, "get is 405");
GoogleRig failing;
failing.upstream_error("/v1/speech:recognize", 429, "RESOURCE_EXHAUSTED", "quota");
Fixture g(failing.client);
auto bad = g.client->post("/voice/hear", "xx", "audio/webm");
check(bad.status == 502 && contains(bad.body, "RESOURCE_EXHAUSTED"),
"quota error surfaced as 502");
return finish("voice/hear");
}
+25
View File
@@ -0,0 +1,25 @@
// Route: GET /voice (live voice conversation page, script embedded)
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
GoogleRig rig;
Fixture f(rig.client);
auto page = f.client->get("/voice");
check(page.status == 200, "status 200");
check(contains(page.headers.get("Content-Type"), "text/html"), "html");
check(contains(page.body, "id=\"v-toggle\""), "start/stop button");
check(contains(page.body, "id=\"v-lang\""), "language select");
check(contains(page.body, "id=\"v-log\""), "conversation log");
check(contains(page.body, "/embedded/voice.js"), "script linked");
check(contains(page.body, "width=device-width"), "responsive viewport");
auto js = f.client->get("/embedded/voice.js");
check(js.status == 200 && contains(js.body, "/voice/turn"),
"page script served from the bundle");
check(contains(js.body, "getUserMedia") && contains(js.body, "MediaRecorder"),
"script records the microphone");
check(f.client->post("/voice").status == 405, "post is 405");
check(rig.mock->send_count() == 0, "side effect: page makes no Google call");
return finish("voice/page");
}
+25
View File
@@ -0,0 +1,25 @@
// Route: POST /voice/reset (forget one conversation)
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_GENAI_MODEL", "rig-model", 1);
GoogleRig rig;
rig.gemini("Hoi!");
rig.tts("A");
Fixture f(rig.client);
f.client->post("/voice/turn?cid=keep1", "{\"text\":\"hallo\"}", "application/json");
check(f.services.voice->sessions().history_size("keep1") == 2, "arranged: 2 messages");
auto res = f.client->post("/voice/reset?cid=keep1");
check(res.status == 200 && contains(res.body, "\"reset\":true"), "reset answered");
check(f.services.voice->sessions().history_size("keep1") == 0,
"side effect: history gone");
check(f.client->post("/voice/reset?cid=never-seen").status == 200,
"unknown conversation is a no-op");
check(f.client->post("/voice/reset").status == 422, "missing cid is 422");
check(f.client->post("/voice/reset?cid=a%2Fb").status == 422, "bad cid is 422");
check(f.client->get("/voice/reset").status == 405, "get is 405");
unsetenv("GOOGLE_GENAI_MODEL");
return finish("voice/reset");
}
+56
View File
@@ -0,0 +1,56 @@
// Route: POST /voice/say (text to spoken mp3 through Google TTS)
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
GoogleRig rig;
rig.tts("FAKE-MP3-BYTES");
Fixture f(rig.client);
auto res = f.client->post("/voice/say",
"{\"text\":\"hallo wereld\",\"lang\":\"nl-NL\"}",
"application/json");
check(res.status == 200, "status 200");
check(res.headers.get("Content-Type") == "audio/mpeg", "audio/mpeg");
check(res.body == "FAKE-MP3-BYTES", "decoded audio returned");
auto seen = rig.mock->seen();
check(seen.size() == 1 && contains(seen[0].body, "hallo wereld"),
"side effect: text sent upstream");
check(seen[0].headers.get_or("x-goog-api-key", "") == kTestKey,
"key sent in header");
check(seen[0].target.find(kTestKey) == std::string::npos, "key not in url");
check(f.client->post("/voice/say", "{\"text\":\"\"}", "application/json").status == 422,
"empty text is 422");
check(f.client->post("/voice/say", "{\"text\":\"x\",\"lang\":\"dutch\"}",
"application/json").status == 422,
"bad language is 422");
check(f.client->post("/voice/say",
"{\"text\":\"x\",\"voice\":\"a b;c\"}",
"application/json").status == 422,
"bad voice name is 422");
std::string long_text(5001, 'a');
check(f.client->post("/voice/say", "{\"text\":\"" + long_text + "\"}",
"application/json").status == 422,
"text over the 5000 char cap is 422");
check(rig.mock->send_count() == 1, "rejected input never reached Google");
check(f.client->get("/voice/say").status == 405, "get is 405");
GoogleRig failing;
failing.upstream_error("/v1/text:synthesize", 403, "PERMISSION_DENIED",
"API not enabled");
Fixture g(failing.client);
auto bad = g.client->post("/voice/say", "{\"text\":\"x\"}", "application/json");
check(bad.status == 502, "upstream failure is 502");
check(contains(bad.body, "google_error") && contains(bad.body, "PERMISSION_DENIED"),
"google status surfaced");
check(!contains(bad.body, kTestKey), "key never in an error");
GoogleRig none(false);
Fixture h(none.client);
auto nc = h.client->post("/voice/say", "{\"text\":\"x\"}", "application/json");
check(nc.status == 503 && contains(nc.body, "google_not_configured"),
"no key is a loud 503");
check(none.mock->send_count() == 0, "no network without a key");
return finish("voice/say");
}
+94
View File
@@ -0,0 +1,94 @@
// Route: POST /voice/turn (one live-conversation turn: hear, think, speak)
#include "support/GoogleRig.hpp"
#include "security/crypto/Base64.h"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_GENAI_MODEL", "rig-model", 1);
GoogleRig rig;
rig.stt("wat is de hoofdstad van Nederland");
rig.gemini("Amsterdam.");
rig.tts("SPOKEN-ANSWER");
Fixture f(rig.client);
auto res = f.client->post("/voice/turn?cid=abc123&lang=nl-NL", "WEBM-BYTES",
"audio/webm;codecs=opus");
check(res.status == 200, "status 200");
check(contains(res.headers.get("Content-Type"), "application/json"), "json");
check(contains(res.body, "\"transcript\":\"wat is de hoofdstad van Nederland\""),
"transcript");
check(contains(res.body, "\"reply\":\"Amsterdam.\""), "reply text");
check(contains(res.body, "\"mime\":\"audio/mpeg\""), "mime");
check(contains(res.body, "\"audio_b64\":\"" +
molo::security::crypto::Base64::encode("SPOKEN-ANSWER") + "\""),
"spoken answer as base64");
check(rig.mock->send_count() == 3, "stt, gemini, tts called once each");
check(f.services.voice->sessions().history_size("abc123") == 2,
"side effect: user and model turn remembered");
check(contains(rig.mock->seen()[1].body, "hoofdstad"),
"transcript fed to the model");
// Second turn carries the first as history.
rig.stt("en van Duitsland");
rig.gemini("Berlijn.");
rig.tts("SPOKEN-2");
f.client->post("/voice/turn?cid=abc123", "WEBM", "audio/webm");
auto gem2 = rig.mock->seen()[4].body;
check(contains(gem2, "Amsterdam.") && contains(gem2, "Duitsland"),
"history sent with the next turn");
// Typed turn.
rig.gemini("Hallo!");
rig.tts("SPOKEN-3");
auto typed = f.client->post("/voice/turn?cid=abc123", "{\"text\":\"hoi\"}",
"application/json");
check(typed.status == 200 && contains(typed.body, "\"reply\":\"Hallo!\""),
"typed turn answered");
// Silence: one STT call, nothing else.
std::size_t before = rig.mock->send_count();
rig.stt("");
auto quiet = f.client->post("/voice/turn?cid=quiet1", "WEBM", "audio/webm");
check(quiet.status == 200 && contains(quiet.body, "\"transcript\":\"\"") &&
contains(quiet.body, "\"audio_b64\":\"\""),
"silence answers an empty turn");
check(rig.mock->send_count() == before + 1, "silence costs one STT call");
// Validation and limits.
check(f.client->post("/voice/turn", "x", "audio/webm").status == 422,
"missing cid is 422");
check(f.client->post("/voice/turn?cid=bad%20id", "x", "audio/webm").status == 422,
"bad cid is 422");
check(f.client->post("/voice/turn?cid=a1", "x", "video/mp4").status == 415,
"unknown media type is 415");
check(f.client->post("/voice/turn?cid=a1", "", "audio/webm").status == 422,
"empty audio is 422");
check(f.client->post("/voice/turn?cid=a1", "{\"text\":\"\"}",
"application/json").status == 422,
"empty typed text is 422");
check(f.client->post("/voice/turn?cid=a1", std::string(1024 * 1024 + 8192, 'a'),
"audio/webm").status == 413,
"body over the cap is 413");
check(f.client->get("/voice/turn").status == 405, "get is 405");
// A failed model call leaves no half conversation.
GoogleRig failing;
failing.stt("hallo");
failing.upstream_error("/v1beta/models/rig-model:generateContent", 503,
"UNAVAILABLE", "overloaded");
Fixture g(failing.client);
auto bad = g.client->post("/voice/turn?cid=fail1", "x", "audio/webm");
check(bad.status == 502 && contains(bad.body, "UNAVAILABLE"), "502 on model failure");
check(g.services.voice->sessions().history_size("fail1") == 0,
"failed turn rolled back");
GoogleRig none(false);
Fixture h(none.client);
auto nc = h.client->post("/voice/turn?cid=n1", "x", "audio/webm");
check(nc.status == 503 && contains(nc.body, "google_not_configured"),
"no key is a loud 503");
unsetenv("GOOGLE_GENAI_MODEL");
return finish("voice/turn");
}
+44
View File
@@ -0,0 +1,44 @@
// Route: POST /voice/turn (Gemini audio backend: Vertex express key)
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_GENAI_MODEL", "chat-m", 1);
setenv("GOOGLE_STT_MODEL", "stt-m", 1);
setenv("GOOGLE_TTS_MODEL", "tts-m", 1);
GoogleRig rig;
const std::string p = "/v1/publishers/google/models/";
auto text = [&](const std::string& model, const std::string& t) {
rig.mock->expect_response(
molo::clients::Method::kPost, p + model + ":generateContent",
GoogleRig::json("{\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"" +
t + "\"}]}}]}"));
};
text("stt-m", "hallo daar");
text("chat-m", "Hoi!");
rig.mock->expect_response(
molo::clients::Method::kPost, p + "tts-m:generateContent",
GoogleRig::json(
"{\"candidates\":[{\"content\":{\"parts\":[{\"inlineData\":{\"mimeType\":"
"\"audio/L16;codec=pcm;rate=24000\",\"data\":\"" +
molo::security::crypto::Base64::encode("PCM") + "\"}}]}}]}"));
Fixture f(rig.client, molo::google::VoiceBackend::kVertex);
check(contains(f.client->get("/google/status").body, "\"backend\":\"vertex\""),
"status names the backend");
auto res = f.client->post("/voice/turn?cid=v1", "WEBM", "audio/webm;codecs=opus");
check(res.status == 200, "status 200");
check(contains(res.body, "\"transcript\":\"hallo daar\""), "transcript");
check(contains(res.body, "\"reply\":\"Hoi!\""), "reply");
check(contains(res.body, "\"mime\":\"audio/wav\""), "wav for the browser");
check(rig.mock->send_count() == 3, "three Gemini calls");
check(rig.mock->seen()[0].target.rfind(p, 0) == 0, "vertex endpoint used");
check(contains(rig.mock->seen()[0].body, "audio/webm"), "audio mime sent to Gemini");
check(rig.mock->seen()[0].headers.get_or("x-goog-api-key", "") == kTestKey,
"key in header");
unsetenv("GOOGLE_GENAI_MODEL");
unsetenv("GOOGLE_STT_MODEL");
unsetenv("GOOGLE_TTS_MODEL");
return finish("voice/turn vertex");
}