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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user