Files

70 lines
2.5 KiB
C++
Raw Permalink Normal View History

#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