Merge branch 'worktree-agent-af2efb541856af61f'

This commit is contained in:
2026-10-25 15:24:27 +01:00
31 changed files with 3947 additions and 3 deletions
@@ -0,0 +1,60 @@
// Routes: POST /google/cloudai/{video/annotate,documentai/*,speech/*,tts/*,operation,call}
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
GoogleRig rig(false);
Fixture f(rig.client);
const char* j = "application/json";
// video
std::string v = "{\"inputUri\":\"gs://b/v.mp4\",\"features\":[\"LABEL_DETECTION\"]";
check(f.client->post("/google/cloudai/video/annotate", v + "}", j).status == 200, "video dry run");
check(f.client->post("/google/cloudai/video/annotate", v + ",\"confirm\":true}", j).status == 503, "video confirmed is 503");
check(f.client->post("/google/cloudai/video/annotate", "{\"inputUri\":\"gs://b/v\",\"features\":[\"X\"]}", j).status == 422, "bad feature");
check(f.client->post("/google/cloudai/video/annotate", "{}", j).status == 422, "features required");
// document ai
std::string d = "{\"processorId\":\"abc\",\"gcsUri\":\"gs://b/d.pdf\",\"mimeType\":\"application/pdf\"";
auto dd = f.client->post("/google/cloudai/documentai/process", d + "}", j);
check(dd.status == 200 && contains(dd.body, "gcsDocument"), "process dry run");
check(f.client->post("/google/cloudai/documentai/process", d + ",\"confirm\":true}", j).status == 503, "process confirmed is 503");
auto inline_doc = f.client->post("/google/cloudai/documentai/process",
"{\"processorId\":\"abc\",\"content\":\"" + std::string(1000, 'A') + "\",\"mimeType\":\"application/pdf\"}", j);
check(inline_doc.status == 200 && contains(inline_doc.body, "1000 characters") && !contains(inline_doc.body, "AAAAAAAAAA"), "long content is not echoed");
check(f.client->post("/google/cloudai/documentai/process", "{\"processorId\":\"a/b\",\"gcsUri\":\"gs://b/d\",\"mimeType\":\"application/pdf\"}", j).status == 422, "bad processor id");
check(f.client->post("/google/cloudai/documentai/process", "{\"processorId\":\"a\",\"gcsUri\":\"gs://b/d\",\"mimeType\":\"x/y\"}", j).status == 422, "bad mime");
check(f.client->post("/google/cloudai/documentai/list", "{\"what\":\"processors\",\"location\":\"eu\"}", j).status == 503, "list is 503");
check(f.client->post("/google/cloudai/documentai/list", "{\"what\":\"versions\"}", j).status == 422, "versions need processorId");
// speech
std::string s = "{\"languages\":[\"nl-NL\"],\"uri\":\"gs://b/a.wav\"";
check(f.client->post("/google/cloudai/speech/recognize", s + "}", j).status == 200, "recognize dry run");
check(f.client->post("/google/cloudai/speech/recognize", s + ",\"confirm\":true}", j).status == 503, "recognize confirmed is 503");
check(f.client->post("/google/cloudai/speech/recognize", "{\"uri\":\"gs://b/a\"}", j).status == 422, "languages required");
check(f.client->post("/google/cloudai/speech/recognize", "{\"languages\":[\"nl\"],\"uri\":\"gs://b/a\",\"model\":\"Bad Model\"}", j).status == 422, "bad model");
check(f.client->post("/google/cloudai/speech/list", "{\"what\":\"recognizers\"}", j).status == 503, "speech list is 503");
// long audio
std::string t = "{\"text\":\"Hello\",\"language\":\"en-US\",\"outputUri\":\"gs://b/o.wav\"";
auto td = f.client->post("/google/cloudai/tts/long", t + "}", j);
check(td.status == 200 && contains(td.body, "synthesizeLongAudio"), "long audio dry run");
check(f.client->post("/google/cloudai/tts/long", t + ",\"confirm\":true}", j).status == 503, "long audio confirmed is 503");
check(f.client->post("/google/cloudai/tts/long", "{\"text\":\"x\",\"language\":\"en-US\",\"outputUri\":\"o.wav\"}", j).status == 422, "bad outputUri");
check(f.client->post("/google/cloudai/tts/voices", "{}", j).status == 503, "voices is 503");
check(f.client->post("/google/cloudai/tts/voices", "{\"language\":\"n l\"}", j).status == 422, "bad language");
// operation
check(f.client->post("/google/cloudai/operation", "{\"api\":\"video\",\"name\":\"projects/1/locations/us-east1/operations/2\"}", j).status == 503, "operation get is 503");
check(f.client->post("/google/cloudai/operation", "{\"api\":\"video\",\"name\":\"operations/2\"}", j).status == 422, "bad operation name");
check(f.client->post("/google/cloudai/operation", "{\"api\":\"other\",\"name\":\"x\"}", j).status == 422, "bad api");
// generic call
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.glossaries.list\",\"name\":\"projects/p/locations/global\"}", j).status == 503, "GET row runs, 503");
auto cd = f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.glossaries.delete\",\"name\":\"projects/p/locations/global/glossaries/g\"}", j);
check(cd.status == 200 && contains(cd.body, "\"dry_run\":true") && contains(cd.body, "\"http\":\"DELETE\""), "DELETE row is a dry run");
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.glossaries.delete\",\"name\":\"projects/p/locations/global/glossaries/g\",\"confirm\":true}", j).status == 503, "confirmed DELETE is 503");
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"nope\"}", j).status == 422, "unknown rpc");
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.translateText\",\"name\":\"projects/p/locations/global\",\"body\":{\"bogus\":1}}", j).status == 422, "unknown request field");
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.glossaries.list\",\"name\":\"projects/p\"}", j).status == 422, "name not matching the pattern");
check(f.client->post("/google/cloudai/call", "{\"rpc\":\"translate.projects.locations.glossaries.list\",\"name\":\"projects/p/locations/global\",\"query\":{\"bogus\":\"1\"}}", j).status == 422, "unknown query parameter");
check(f.client->get("/google/cloudai/call").status == 405, "get is 405");
check(rig.mock->send_count() == 0, "nothing reached Google");
return finish("google/cloudai/media");
}
@@ -0,0 +1,25 @@
// Routes: GET /google/cloudai, /google/cloudai/status, /google/cloudai/calls
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
GoogleRig rig(false);
Fixture f(rig.client);
auto page = f.client->get("/google/cloudai");
check(page.status == 200, "page 200 without a credential");
check(contains(page.headers.get("Content-Type"), "text/html"), "html");
check(contains(page.body, "Cloud AI") && contains(page.body, "viewport"), "page and mobile viewport");
check(contains(page.body, "/embedded/google_cloudai.js"), "script wired");
auto status = f.client->get("/google/cloudai/status");
check(status.status == 200 && contains(status.body, "\"configured\":false"), "status without credential");
check(contains(status.body, "documentai v1") && contains(status.body, "mediatranslation"), "lists apis and the gap");
auto calls = f.client->get("/google/cloudai/calls");
check(calls.status == 200 && contains(calls.body, "translate.projects.locations.translateText"), "call table");
check(contains(calls.body, "documentai.projects.locations.processors.process"), "document ai rows");
check(f.client->post("/google/cloudai", "{}", "application/json").status == 405, "post page is 405");
check(f.client->post("/google/cloudai/status", "{}", "application/json").status == 405, "post status is 405");
check(rig.mock->send_count() == 0, "no network");
return finish("google/cloudai/page");
}
@@ -0,0 +1,41 @@
// Routes: POST /google/cloudai/translate{,/detect,/list,/glossary}
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
GoogleRig rig(false);
Fixture f(rig.client);
const char* j = "application/json";
auto dry = f.client->post("/google/cloudai/translate", "{\"texts\":[\"hi\"],\"target\":\"nl\"}", j);
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "targetLanguageCode"), "default is a dry run");
auto real = f.client->post("/google/cloudai/translate", "{\"text\":\"hi\",\"target\":\"nl\",\"confirm\":true}", j);
check(real.status == 503 && contains(real.body, "google_not_configured"), "confirmed without credential is 503");
check(f.client->post("/google/cloudai/translate", "{}", j).status == 422, "missing target is 422");
check(f.client->post("/google/cloudai/translate", "{\"text\":\"hi\",\"target\":\"n l\"}", j).status == 422, "bad language is 422");
check(f.client->post("/google/cloudai/translate", "{\"texts\":[],\"target\":\"nl\"}", j).status == 422, "no text is 422");
check(f.client->post("/google/cloudai/translate", "{\"text\":\"hi\",\"target\":\"nl\",\"confirm\":\"yes\"}", j).status == 422, "non-bool confirm is 422");
check(f.client->post("/google/cloudai/translate", "{\"text\":\"hi\",\"target\":\"nl\",\"location\":\"../x\"}", j).status == 422, "bad location is 422");
check(f.client->post("/google/cloudai/translate", "not json", j).status == 422, "bad json is 422");
check(f.client->get("/google/cloudai/translate").status == 405, "get is 405");
check(f.client->post("/google/cloudai/translate/detect", "{\"text\":\"bonjour\"}", j).status == 200, "detect dry run");
auto det = f.client->post("/google/cloudai/translate/detect", "{\"text\":\"bonjour\",\"confirm\":true}", j);
check(det.status == 503, "detect confirmed is 503");
check(f.client->post("/google/cloudai/translate/detect", "{}", j).status == 422, "detect needs text");
auto lang = f.client->post("/google/cloudai/translate/list", "{\"what\":\"languages\"}", j);
check(lang.status == 503 && contains(lang.body, "google_not_configured"), "read without credential is 503");
check(f.client->post("/google/cloudai/translate/list", "{\"what\":\"nope\"}", j).status == 422, "bad what is 422");
check(f.client->post("/google/cloudai/translate/list", "{\"what\":\"glossaries\",\"location\":\"bad loc\"}", j).status == 422, "bad location");
auto gdry = f.client->post("/google/cloudai/translate/glossary", "{\"action\":\"delete\",\"id\":\"g1\"}", j);
check(gdry.status == 200 && contains(gdry.body, "\"dry_run\":true"), "glossary delete is a dry run");
auto gmake = f.client->post("/google/cloudai/translate/glossary",
"{\"action\":\"create\",\"id\":\"g1\",\"inputUri\":\"gs://b/g.csv\",\"source\":\"en\",\"target\":\"nl\"}", j);
check(gmake.status == 200 && contains(gmake.body, "languagePair"), "glossary create is a dry run");
auto greal = f.client->post("/google/cloudai/translate/glossary", "{\"action\":\"delete\",\"id\":\"g1\",\"confirm\":true}", j);
check(greal.status == 503, "confirmed delete without credential is 503");
check(f.client->post("/google/cloudai/translate/glossary", "{\"action\":\"create\",\"id\":\"g1\",\"inputUri\":\"http://x\",\"source\":\"en\",\"target\":\"nl\"}", j).status == 422, "bad inputUri is 422");
check(f.client->post("/google/cloudai/translate/glossary", "{\"action\":\"get\",\"id\":\"a/b\"}", j).status == 422, "bad id is 422");
check(rig.mock->send_count() == 0, "nothing reached Google");
return finish("google/cloudai/translate");
}
@@ -0,0 +1,37 @@
// Routes: POST /google/cloudai/vision/{annotate,list}, /google/cloudai/language/analyze
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
GoogleRig rig(false);
Fixture f(rig.client);
const char* j = "application/json";
std::string ok = "{\"images\":[{\"uri\":\"gs://b/i.png\"}],\"features\":[{\"type\":\"LABEL_DETECTION\",\"maxResults\":3}]";
auto dry = f.client->post("/google/cloudai/vision/annotate", ok + "}", j);
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "gcsImageUri"), "annotate dry run");
check(f.client->post("/google/cloudai/vision/annotate", ok + ",\"confirm\":true}", j).status == 503, "confirmed is 503");
check(f.client->post("/google/cloudai/vision/annotate", "{}", j).status == 422, "images required");
check(f.client->post("/google/cloudai/vision/annotate", "{\"images\":[{\"uri\":\"gs://b/i\"}],\"features\":[\"NOPE\"]}", j).status == 422, "unknown feature is 422");
check(f.client->post("/google/cloudai/vision/annotate", "{\"images\":[{}],\"features\":[\"LABEL_DETECTION\"]}", j).status == 422, "empty image is 422");
check(f.client->post("/google/cloudai/vision/annotate", "{\"images\":[{\"uri\":\"gs://b/i\"}],\"features\":[{\"type\":\"LABEL_DETECTION\",\"maxResults\":500}]}", j).status == 422, "maxResults over cap is 422");
auto small = f.client->post("/google/cloudai/vision/annotate", "{\"images\":[{\"content\":\"" + std::string(100, 'A') + "\"}],\"features\":[\"LABEL_DETECTION\"]}", j);
check(small.status == 200, "small inline image dry run");
auto sets = f.client->post("/google/cloudai/vision/list", "{\"what\":\"productSets\"}", j);
check(sets.status == 503, "list without credential is 503");
check(f.client->post("/google/cloudai/vision/list", "{\"what\":\"referenceImages\"}", j).status == 422, "productId required");
check(f.client->get("/google/cloudai/vision/list").status == 405, "get is 405");
auto nl = f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"sentiment\",\"content\":\"great\"}", j);
check(nl.status == 200 && contains(nl.body, "\"dry_run\":true"), "analyze dry run");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"sentiment\",\"content\":\"great\",\"confirm\":true}", j).status == 503, "confirmed is 503");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"syntax\",\"content\":\"great\",\"confirm\":true}", j).status == 503, "v1 syntax is 503");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"sentiment\"}", j).status == 422, "content required");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"other\",\"content\":\"x\"}", j).status == 422, "unknown kind");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"annotate\",\"content\":\"x\"}", j).status == 422, "annotate needs a feature");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"annotate\",\"content\":\"x\",\"classifyText\":true}", j).status == 200, "annotate with a feature");
check(f.client->post("/google/cloudai/language/analyze", "{\"kind\":\"sentiment\",\"content\":\"x\",\"type\":\"MD\"}", j).status == 422, "bad type");
check(rig.mock->send_count() == 0, "nothing reached Google");
return finish("google/cloudai/vision_language");
}
+496
View File
@@ -0,0 +1,496 @@
// cloudai: deterministic local tests. The call table is generated from and
// checked against Google's Discovery documents (tools/check_discovery.py);
// here: table integrity, path rendering and validation, endpoint selection,
// request bodies as the client really sends them (a configured client over a
// MockTransport with no scripted reply: the request is recorded, the missing
// reply surfaces as UNAVAILABLE), caps, and the no-credential behavior.
// No Google reply is ever scripted.
#include <memory>
#include <set>
#include "framework.h"
#include "clients/transports/mock_transport.h"
#include "google/cloudai/calls.h"
#include "google/cloudai/caps.h"
#include "google/cloudai/document_ai.h"
#include "google/cloudai/long_audio.h"
#include "google/cloudai/natural_language.h"
#include "google/cloudai/speech_v2.h"
#include "google/cloudai/translation.h"
#include "google/cloudai/video_intelligence.h"
#include "google/cloudai/vision.h"
#include "google/credentials.h"
using namespace molo::google;
using namespace molo::google::cloudai;
using molo::clients::body::Json;
using molo::clients::transports::MockTransport;
namespace {
struct Rig {
MockTransport* mock;
std::shared_ptr<GoogleClient> client;
CloudContext ctx;
explicit Rig(bool with_key = true, const std::string& location = "global") {
auto t = std::make_unique<MockTransport>();
mock = t.get();
client = std::make_shared<GoogleClient>(
std::move(t), with_key ? Credentials::api_key("AIzaTESTKEY") : nullptr);
ctx.project = "p1";
ctx.location = location;
}
MockTransport::SeenRequest last() const { return mock->seen().back(); }
};
// Runs f; returns the GoogleError it threw (status kept) or null.
template <typename F>
std::unique_ptr<GoogleError> thrown(F&& f) {
try {
f();
} catch (const GoogleError& e) {
return std::make_unique<GoogleError>(e);
}
return nullptr;
}
template <typename F>
bool invalid(F&& f) {
auto e = thrown(f);
return e && e->http_status() == 400 && e->status() == "INVALID_ARGUMENT";
}
// Passed validation, died at the missing reply (the request was recorded).
template <typename F>
bool sent(Rig& rig, F&& f) {
std::size_t before = rig.mock->send_count();
auto e = thrown(f);
return e && e->status() == "UNAVAILABLE" && rig.mock->send_count() == before + 1;
}
} // namespace
TEST(cloudai, table_is_indexed_by_enum) {
ASSERT_TRUE(call_count() > 150);
for (std::size_t i = 0; i < call_count(); ++i) ASSERT_EQ(static_cast<std::size_t>(spec_at(i).call), i);
std::set<std::string> ids;
for (std::size_t i = 0; i < call_count(); ++i) ids.insert(spec_at(i).rpc_id);
ASSERT_EQ(ids.size(), call_count());
ASSERT_TRUE(schema_count() > 40);
}
TEST(cloudai, every_row_renders_with_a_matching_name) {
// Build a name that satisfies the row's own pattern and render it.
for (std::size_t i = 0; i < call_count(); ++i) {
const CallSpec& s = spec_at(i);
std::string name;
if (*s.path_var) {
std::string p = s.pattern;
p = p.substr(1, p.size() - 2); // strip ^ $
for (std::size_t at = p.find("[^/]+"); at != std::string::npos; at = p.find("[^/]+"))
p.replace(at, 5, "x1");
for (std::size_t at = p.find(".*"); at != std::string::npos; at = p.find(".*")) p.replace(at, 2, "x1");
std::string out = p;
name = out;
}
std::string path = render(s.call, name);
ASSERT_TRUE(path.size() > 1 && path[0] == '/');
ASSERT_TRUE(path.find('{') == std::string::npos);
ASSERT_TRUE(path.find("v1/") != 1 && path.find("v2/") != 1 && path.find("v3/") != 1);
}
}
TEST(cloudai, render_rejects_bad_names) {
Call c = Call::kTranslateLocationsGlossariesGet;
ASSERT_EQ(render(c, "projects/p/locations/global/glossaries/g"),
"/projects/p/locations/global/glossaries/g");
for (const char* bad : {"", "projects/p/locations/global", "projects/p/locations/global/glossaries/g/x",
"projects/../locations/global/glossaries/g", "projects/p/locations/global/glossaries/g?x=1",
"projects/p/locations/global/glossaries/g#f", "projects/p/locations/global/glossaries/a b",
"projects//locations/global/glossaries/g", "locations/global/glossaries/g"})
ASSERT_TRUE(invalid([&] { render(c, bad); }));
ASSERT_TRUE(invalid([&] { render(c, std::string(600, 'a')); }));
ASSERT_TRUE(invalid([&] { render(Call::kVisionImagesAnnotate, "projects/p"); })); // takes no name
}
TEST(cloudai, query_and_body_are_checked_against_the_row) {
ASSERT_EQ(query_of(Call::kTranslateLocationsGlossariesList, {{"pageSize", "5"}, {"filter", "a b"}}),
"?pageSize=5&filter=a%20b");
ASSERT_TRUE(invalid([&] { query_of(Call::kTranslateLocationsGlossariesList, {{"bogus", "1"}}); }));
ASSERT_TRUE(invalid([&] { query_of(Call::kTranslateLocationsGlossariesList, {{"pageSize", std::string(3000, '1')}}); }));
Json ok = Json::object();
ok.set("contents", Json::array());
check_body(Call::kTranslateLocationsTranslateText, &ok);
Json bad = Json::object();
bad.set("nope", Json(1));
ASSERT_TRUE(invalid([&] { check_body(Call::kTranslateLocationsTranslateText, &bad); }));
ASSERT_TRUE(invalid([&] { check_body(Call::kTranslateLocationsGlossariesGet, &ok); })); // GET takes no body
Json arr = Json::array();
ASSERT_TRUE(invalid([&] { check_body(Call::kTranslateLocationsTranslateText, &arr); }));
Json big = Json::object();
big.set("contents", Json(std::string(CloudAiCaps::kMaxBodyBytes + 10, 'a')));
ASSERT_TRUE(invalid([&] { check_body(Call::kTranslateLocationsTranslateText, &big); }));
}
TEST(cloudai, endpoints_are_regional_where_discovery_lists_them) {
ASSERT_EQ(endpoint_of(Call::kTranslateLocationsTranslateText, "").base_url,
"https://translation.googleapis.com/v3");
ASSERT_EQ(endpoint_of(Call::kVisionImagesAnnotate, "").base_url, "https://vision.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kLanguageDocumentsAnalyzeSentiment, "").base_url,
"https://language.googleapis.com/v2");
ASSERT_EQ(endpoint_of(Call::kLanguageV1DocumentsAnalyzeSyntax, "").base_url,
"https://language.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kVideoVideosAnnotate, "").base_url, "https://videointelligence.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kTtsVoicesList, "").base_url, "https://texttospeech.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kDocumentaiLocationsProcessorsGet, "projects/p/locations/eu/processors/x").base_url,
"https://documentai.eu.rep.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kDocumentaiLocationsProcessorsGet, "projects/p/locations/mars/processors/x").base_url,
"https://documentai.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kSpeechLocationsRecognizersGet, "projects/p/locations/europe-west4/recognizers/_").base_url,
"https://speech.europe-west4.rep.googleapis.com/v2");
ASSERT_EQ(endpoint_of(Call::kSpeechLocationsRecognizersGet, "projects/p/locations/global/recognizers/_").base_url,
"https://speech.googleapis.com/v2");
ASSERT_EQ(endpoint_of(Call::kSpeechLocationsRecognizersGet, "projects/p/locations/eu/recognizers/_").base_url,
"https://eu-speech.googleapis.com/v2");
}
TEST(cloudai, no_credential_is_503_after_validation) {
Rig rig(false);
Translation t(rig.client, rig.ctx);
TranslateRequest r;
r.texts = {"hello"};
r.target = "nl";
auto e = thrown([&] { t.translate(r); });
ASSERT_TRUE(e != nullptr);
ASSERT_EQ(e->http_status(), 503);
ASSERT_EQ(e->status(), "NOT_CONFIGURED");
r.target = "";
ASSERT_TRUE(invalid([&] { t.translate(r); })); // validation wins over the credential check
ASSERT_EQ(rig.mock->send_count(), 0u);
}
TEST(cloudai, translate_request_as_sent) {
Rig rig;
Translation t(rig.client, rig.ctx);
TranslateRequest r;
r.texts = {"hello", "world"};
r.target = "nl";
r.source = "en";
r.glossary = "projects/p1/locations/global/glossaries/g1";
r.glossary_ignore_case = true;
ASSERT_TRUE(sent(rig, [&] { t.translate(r); }));
auto seen = rig.last();
ASSERT_EQ(seen.method, "POST");
ASSERT_TRUE(seen.target.find("/v3/projects/p1/locations/global:translateText") != std::string::npos);
Json body = Json::parse(seen.body);
ASSERT_EQ(body.find("targetLanguageCode")->as_string(), "nl");
ASSERT_EQ(body.find("sourceLanguageCode")->as_string(), "en");
ASSERT_EQ(body.find("contents")->as_array().size(), 2u);
ASSERT_TRUE(body.find("glossaryConfig")->find("ignoreCase")->as_bool());
ASSERT_TRUE(seen.headers.get("x-goog-api-key") == "AIzaTESTKEY");
ASSERT_TRUE(seen.target.find("AIzaTESTKEY") == std::string::npos);
}
TEST(cloudai, translate_validation) {
Rig rig;
Translation t(rig.client, rig.ctx);
TranslateRequest r;
r.target = "nl";
ASSERT_TRUE(invalid([&] { t.translate(r); })); // no texts
r.texts = {""};
ASSERT_TRUE(invalid([&] { t.translate(r); })); // empty text
r.texts = {std::string(CloudAiCaps::kMaxTextChars + 1, 'a')};
ASSERT_TRUE(invalid([&] { t.translate(r); }));
r.texts = std::vector<std::string>(CloudAiCaps::kMaxTexts + 1, "a");
ASSERT_TRUE(invalid([&] { t.translate(r); }));
r.texts = {"a"};
r.target = "n l";
ASSERT_TRUE(invalid([&] { t.translate(r); }));
r.target = "nl";
r.mime_type = "application/pdf";
ASSERT_TRUE(invalid([&] { t.translate(r); }));
r.mime_type = "";
r.glossary = "../x";
ASSERT_TRUE(invalid([&] { t.translate(r); }));
r.glossary = "";
r.model = "base";
ASSERT_TRUE(invalid([&] { t.translate(r); }));
ASSERT_EQ(rig.mock->send_count(), 0u);
// exactly the cap passes
r.model = "";
r.texts = {std::string(30000, 'a')};
ASSERT_TRUE(sent(rig, [&] { t.translate(r); }));
}
TEST(cloudai, translation_reads_and_glossary_writes) {
Rig rig;
Translation t(rig.client, rig.ctx);
ASSERT_TRUE(sent(rig, [&] { t.supported_languages("nl"); }));
ASSERT_TRUE(rig.last().target.find("/v3/projects/p1/locations/global/supportedLanguages?displayLanguageCode=nl") !=
std::string::npos);
ASSERT_TRUE(sent(rig, [&] { t.list_glossaries(); }));
ASSERT_EQ(rig.last().method, "GET");
ASSERT_TRUE(rig.last().target.find("/glossaries?pageSize=50") != std::string::npos);
ASSERT_TRUE(invalid([&] { t.get_glossary("a/b"); }));
ASSERT_TRUE(invalid([&] { t.supported_languages("n l"); }));
GlossaryDraft d;
d.id = "g1";
d.input_uri = "gs://bucket/g.csv";
d.source_language = "en";
d.target_language = "nl";
Json b = t.glossary_body(d);
ASSERT_EQ(b.find("name")->as_string(), "projects/p1/locations/global/glossaries/g1");
ASSERT_EQ(b.find("languagePair")->find("targetLanguageCode")->as_string(), "nl");
ASSERT_TRUE(sent(rig, [&] { t.create_glossary_start(d); }));
ASSERT_EQ(rig.last().method, "POST");
d.input_uri = "http://x/g.csv";
ASSERT_TRUE(invalid([&] { t.create_glossary_start(d); }));
d.input_uri = "gs://bucket/g.csv";
d.language_codes = {"en", "nl"};
ASSERT_TRUE(invalid([&] { t.glossary_body(d); })); // pair and set together
d.source_language = d.target_language = "";
ASSERT_TRUE(t.glossary_body(d).find("languageCodesSet") != nullptr);
ASSERT_TRUE(sent(rig, [&] { t.delete_glossary_start("g1"); }));
ASSERT_EQ(rig.last().method, "DELETE");
ASSERT_TRUE(invalid([&] { t.call(Call::kVisionImagesAnnotate, ""); })); // wrong family
}
TEST(cloudai, vision_bodies_and_validation) {
Rig rig(true, "us");
Vision v(rig.client, rig.ctx);
VisionImage im;
im.uri = "gs://b/o.png";
VisionFeature f;
f.type = "LABEL_DETECTION";
f.max_results = 5;
Json b = v.annotate_body({im}, {f});
const Json& req = b.find("requests")->as_array()[0];
ASSERT_EQ(req.find("image")->find("source")->find("gcsImageUri")->as_string(), "gs://b/o.png");
ASSERT_EQ(req.find("features")->as_array()[0].find("maxResults")->as_number(), 5.0);
im.uri = "https://example.com/a.png";
ASSERT_TRUE(v.annotate_body({im}, {f}).find("requests")->as_array()[0].find("image")->find("source")->find("imageUri") !=
nullptr);
ASSERT_TRUE(sent(rig, [&] { v.annotate({im}, {f}); }));
ASSERT_TRUE(rig.last().target.find("/v1/images:annotate") != std::string::npos);
f.type = "NOT_A_FEATURE";
ASSERT_TRUE(invalid([&] { v.annotate({im}, {f}); }));
f.type = "LABEL_DETECTION";
f.max_results = 101;
ASSERT_TRUE(invalid([&] { v.annotate({im}, {f}); }));
f.max_results = 1;
ASSERT_TRUE(invalid([&] { v.annotate({}, {f}); }));
ASSERT_TRUE(invalid([&] { v.annotate({im}, {}); }));
ASSERT_TRUE(invalid([&] { v.annotate(std::vector<VisionImage>(CloudAiCaps::kMaxImages + 1, im), {f}); }));
VisionImage both;
both.uri = "gs://b/o";
both.content_base64 = "QUJD";
ASSERT_TRUE(invalid([&] { v.annotate({both}, {f}); }));
VisionImage junk;
junk.content_base64 = "not base64!";
ASSERT_TRUE(invalid([&] { v.annotate({junk}, {f}); }));
VisionImage huge;
huge.content_base64 = std::string(CloudAiCaps::kMaxAudioBytes * 2, 'A');
ASSERT_TRUE(invalid([&] { v.annotate({huge}, {f}); }));
VisionImage badu;
badu.uri = "ftp://x";
ASSERT_TRUE(invalid([&] { v.annotate({badu}, {f}); }));
// files
FileAnnotation file;
file.gcs_uri = "gs://b/doc.pdf";
file.mime_type = "application/pdf";
file.pages = {1, 2};
ASSERT_TRUE(sent(rig, [&] { v.annotate_file(file, {f}); }));
ASSERT_TRUE(rig.last().target.find("/v1/files:annotate") != std::string::npos);
file.mime_type = "text/plain";
ASSERT_TRUE(invalid([&] { v.annotate_file(file, {f}); }));
file.mime_type = "application/pdf";
file.pages = {1, 2, 3, 4, 5, 6};
ASSERT_TRUE(invalid([&] { v.annotate_file(file, {f}); }));
// product search
ASSERT_TRUE(sent(rig, [&] { v.list_product_sets(); }));
ASSERT_TRUE(rig.last().target.find("/v1/projects/p1/locations/us/productSets") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { v.create_product_set("ps1", "My set"); }));
ASSERT_TRUE(rig.last().target.find("productSetId=ps1") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { v.create_product("p9", "Shoe", "apparel-v2"); }));
ASSERT_TRUE(rig.last().target.find("productId=p9") != std::string::npos);
ASSERT_TRUE(invalid([&] { v.create_product("p9", "Shoe", "cars"); }));
ASSERT_TRUE(sent(rig, [&] { v.delete_product("p9"); }));
ASSERT_EQ(rig.last().method, "DELETE");
ASSERT_TRUE(invalid([&] { v.get_product("../x"); }));
}
TEST(cloudai, natural_language_bodies) {
Rig rig;
NaturalLanguage nl(rig.client, rig.ctx);
NlDocument d;
d.content = "I love this.";
d.language = "en";
ASSERT_TRUE(sent(rig, [&] { nl.analyze_sentiment(d, "UTF8"); }));
ASSERT_TRUE(rig.last().target.find("/v2/documents:analyzeSentiment") != std::string::npos);
Json body = Json::parse(rig.last().body);
ASSERT_EQ(body.find("document")->find("type")->as_string(), "PLAIN_TEXT");
ASSERT_EQ(body.find("encodingType")->as_string(), "UTF8");
ASSERT_TRUE(sent(rig, [&] { nl.analyze_syntax(d); }));
ASSERT_TRUE(rig.last().target.find("/v1/documents:analyzeSyntax") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { nl.classify_text(d); }));
ASSERT_TRUE(sent(rig, [&] { nl.moderate_text(d); }));
NlFeatures f;
f.sentiment = f.entities = true;
ASSERT_TRUE(sent(rig, [&] { nl.annotate_text(d, f); }));
ASSERT_TRUE(Json::parse(rig.last().body).find("features")->find("extractEntities") != nullptr);
ASSERT_TRUE(invalid([&] { nl.annotate_text(d, NlFeatures{}); }));
ASSERT_TRUE(invalid([&] { nl.analyze_sentiment(d, "UTF99"); }));
ASSERT_TRUE(invalid([&] { nl.classify_text(NlDocument{}); })); // empty
NlDocument html = d;
html.type = "MARKDOWN";
ASSERT_TRUE(invalid([&] { nl.analyze_entities(html); }));
NlDocument big = d;
big.content = std::string(CloudAiCaps::kMaxNlTextChars + 1, 'a');
ASSERT_TRUE(invalid([&] { nl.analyze_entities(big); }));
NlDocument both = d;
both.gcs_uri = "gs://b/o.txt";
ASSERT_TRUE(invalid([&] { nl.analyze_entities(both); }));
// classify and moderate have no encodingType
ASSERT_TRUE(Json::parse(rig.mock->seen()[2].body).find("encodingType") == nullptr);
}
TEST(cloudai, video_intelligence) {
Rig rig(true, "us-east1");
VideoIntelligence vi(rig.client, rig.ctx);
VideoRequest r;
r.input_uri = "gs://b/v.mp4";
r.features = {"LABEL_DETECTION", "SHOT_CHANGE_DETECTION"};
r.location_id = "us-east1";
ASSERT_TRUE(sent(rig, [&] { vi.annotate_start(r); }));
ASSERT_TRUE(rig.last().target.find("/v1/videos:annotate") != std::string::npos);
Json body = Json::parse(rig.last().body);
ASSERT_EQ(body.find("features")->as_array().size(), 2u);
ASSERT_EQ(body.find("locationId")->as_string(), "us-east1");
r.features = {"FLY"};
ASSERT_TRUE(invalid([&] { vi.annotate_start(r); }));
r.features = {};
ASSERT_TRUE(invalid([&] { vi.annotate_start(r); }));
r.features = {"LABEL_DETECTION"};
r.input_uri = "http://x";
ASSERT_TRUE(invalid([&] { vi.annotate_start(r); }));
r.input_uri = "gs://b/v.mp4";
r.location_id = "mars-1";
ASSERT_TRUE(invalid([&] { vi.annotate_start(r); }));
ASSERT_TRUE(sent(rig, [&] { vi.get_operation("projects/1/locations/us-east1/operations/77"); }));
ASSERT_TRUE(invalid([&] { vi.get_operation("operations/77"); }));
ASSERT_TRUE(sent(rig, [&] { vi.list_operations(); }));
ASSERT_TRUE(sent(rig, [&] { vi.cancel_operation("projects/1/locations/us-east1/operations/77"); }));
ASSERT_TRUE(rig.last().target.find(":cancel") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { vi.delete_operation("projects/1/locations/us-east1/operations/77"); }));
ASSERT_EQ(rig.last().method, "DELETE");
}
TEST(cloudai, document_ai) {
Rig rig(true, "eu");
DocumentAi dai(rig.client, rig.ctx);
ProcessRequest r;
r.processor_id = "abc123";
r.content_base64 = "JVBERi0=";
r.mime_type = "application/pdf";
ASSERT_TRUE(sent(rig, [&] { dai.process(r); }));
ASSERT_TRUE(rig.last().target.find("/v1/projects/p1/locations/eu/processors/abc123:process") != std::string::npos);
Json body = Json::parse(rig.last().body);
ASSERT_EQ(body.find("rawDocument")->find("mimeType")->as_string(), "application/pdf");
ASSERT_TRUE(body.find("skipHumanReview")->as_bool());
r.version_id = "pretrained-ocr-v2.0";
ASSERT_TRUE(sent(rig, [&] { dai.process(r); }));
ASSERT_TRUE(rig.last().target.find("/processorVersions/pretrained-ocr-v2.0:process") != std::string::npos);
r.version_id = "";
r.gcs_uri = "gs://b/o.pdf";
ASSERT_TRUE(invalid([&] { dai.process(r); })); // content and uri
r.content_base64 = "";
ASSERT_TRUE(dai.process_body(r).find("gcsDocument") != nullptr);
r.mime_type = "application/zip";
ASSERT_TRUE(invalid([&] { dai.process(r); }));
r.mime_type = "application/pdf";
r.processor_id = "a/b";
ASSERT_TRUE(invalid([&] { dai.process(r); }));
ASSERT_TRUE(sent(rig, [&] { dai.list_processors(); }));
ASSERT_TRUE(sent(rig, [&] { dai.fetch_processor_types(); }));
ASSERT_TRUE(rig.last().target.find(":fetchProcessorTypes") != std::string::npos);
ASSERT_TRUE(invalid([&] { dai.create_processor("x", "form parser"); }));
ASSERT_TRUE(sent(rig, [&] { dai.create_processor("x", "FORM_PARSER_PROCESSOR"); }));
ASSERT_TRUE(sent(rig, [&] { dai.delete_processor_start("abc123"); }));
ASSERT_EQ(rig.last().method, "DELETE");
ASSERT_TRUE(invalid([&] { dai.batch_process_start("abc123", Json::parse("{\"bogus\":1}")); }));
}
TEST(cloudai, speech_v2) {
Rig rig(true, "europe-west4");
SpeechV2 sp(rig.client, rig.ctx);
SpeechRequest r;
r.content_base64 = "UklGRg==";
r.languages = {"nl-NL"};
ASSERT_TRUE(sent(rig, [&] { sp.recognize(r); }));
ASSERT_TRUE(rig.last().target.find("/v2/projects/p1/locations/europe-west4/recognizers/_:recognize") !=
std::string::npos);
Json body = Json::parse(rig.last().body);
ASSERT_EQ(body.find("config")->find("model")->as_string(), "chirp_2");
ASSERT_TRUE(body.find("config")->find("autoDecodingConfig") != nullptr);
r.languages = {};
ASSERT_TRUE(invalid([&] { sp.recognize(r); }));
r.languages = {"a", "b", "c", "d"};
ASSERT_TRUE(invalid([&] { sp.recognize(r); }));
r.languages = {"nl-NL"};
r.model = "Chirp 2";
ASSERT_TRUE(invalid([&] { sp.recognize(r); }));
r.model = "chirp_2";
r.uri = "gs://b/a.wav";
ASSERT_TRUE(invalid([&] { sp.recognize(r); })); // content and uri
r.content_base64 = "";
ASSERT_TRUE(sent(rig, [&] { sp.recognize(r); }));
r.uri = "";
ASSERT_TRUE(invalid([&] { sp.recognize(r); }));
ASSERT_TRUE(sent(rig, [&] { sp.list_recognizers(); }));
ASSERT_TRUE(sent(rig, [&] { sp.get_config(); }));
ASSERT_TRUE(rig.last().target.find("/locations/europe-west4/config") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { sp.create_recognizer_start("r1", "chirp_2", {"en-US"}); }));
ASSERT_TRUE(rig.last().target.find("recognizerId=r1") != std::string::npos);
ASSERT_TRUE(sent(rig, [&] { sp.delete_recognizer_start("r1"); }));
ASSERT_EQ(rig.last().method, "DELETE");
}
TEST(cloudai, long_audio) {
Rig rig(true, "global");
LongAudio la(rig.client, rig.ctx);
LongAudioRequest r;
r.text = "Hello there.";
r.language = "en-US";
r.output_uri = "gs://b/out.wav";
ASSERT_TRUE(sent(rig, [&] { la.synthesize_start(r); }));
ASSERT_TRUE(rig.last().target.find("/v1/projects/p1/locations/global:synthesizeLongAudio") != std::string::npos);
Json body = Json::parse(rig.last().body);
ASSERT_EQ(body.find("audioConfig")->find("audioEncoding")->as_string(), "LINEAR16");
ASSERT_EQ(body.find("outputGcsUri")->as_string(), "gs://b/out.wav");
r.output_uri = "out.wav";
ASSERT_TRUE(invalid([&] { la.synthesize_start(r); }));
r.output_uri = "gs://b/out.wav";
r.text = "";
ASSERT_TRUE(invalid([&] { la.synthesize_start(r); }));
r.text = std::string(CloudAiCaps::kMaxLongAudioChars + 1, 'a');
ASSERT_TRUE(invalid([&] { la.synthesize_start(r); }));
r.text = "x";
r.encoding = "WAVE";
ASSERT_TRUE(invalid([&] { la.synthesize_start(r); }));
r.encoding = "MP3";
r.ssml = true;
ASSERT_TRUE(la.request_body(r).find("input")->find("ssml") != nullptr);
ASSERT_TRUE(sent(rig, [&] { la.voices("nl-NL"); }));
ASSERT_TRUE(rig.last().target.find("/v1/voices?languageCode=nl-NL") != std::string::npos);
ASSERT_TRUE(invalid([&] { la.voices("n l"); }));
}
TEST(cloudai, invoke_is_generic_over_the_table) {
Rig rig;
Translation t(rig.client, rig.ctx);
Json body = Json::parse("{\"bogus\":\"x\"}");
ASSERT_TRUE(invalid([&] { t.invoke(Call::kTranslateLocationsAdaptiveMtTranslate, "projects/p1/locations/global", &body); }));
// a field of the request passes validation and the call goes out
Json ok = Json::parse("{\"dataset\":\"projects/p1/locations/global/adaptiveMtDatasets/d\",\"content\":[\"hi\"]}");
ASSERT_TRUE(sent(rig, [&] { t.invoke(Call::kTranslateLocationsAdaptiveMtTranslate, "projects/p1/locations/global", &ok); }));
}
@@ -0,0 +1,116 @@
// Opt-in live checks (MOLO_LIVE_NET=1, GOOGLE_ACCESS_TOKEN, GOOGLE_CLOUD_PROJECT).
// Read-only: lists and gets only. A disabled API is reported loudly as
// "API NOT ENABLED" and is not a failure. The single paid request (one short
// translation) additionally needs MOLO_LIVE_PAID=1. Nothing is created,
// changed or deleted.
#include <cstdlib>
#include <iostream>
#include "framework.h"
#include "google/cloudai/document_ai.h"
#include "google/cloudai/long_audio.h"
#include "google/cloudai/speech_v2.h"
#include "google/cloudai/translation.h"
#include "google/cloudai/video_intelligence.h"
#include "google/cloudai/vision.h"
#include "google/credentials.h"
using namespace molo::google;
using namespace molo::google::cloudai;
namespace {
bool ready() {
return std::getenv("MOLO_LIVE_NET") && !Credentials::env("GOOGLE_ACCESS_TOKEN").empty() &&
!Credentials::env("GOOGLE_CLOUD_PROJECT").empty();
}
CloudContext context(const char* location) {
CloudContext c = CloudContext::from_env();
c.location = location;
return c;
}
// Runs a read; prints the outcome. Returns false for a disabled API.
template <typename F>
void probe(const char* what, F&& f) {
try {
std::size_t n = f();
std::cout << " " << what << ": 200, " << n << " item(s)\n";
} catch (const GoogleError& e) {
std::string msg = e.message();
bool disabled = e.http_status() == 403 && msg.find("permission") == std::string::npos &&
(msg.find("has not been used") != std::string::npos || msg.find("is disabled") != std::string::npos ||
e.status() == "SERVICE_DISABLED");
if (disabled) {
std::cout << " API NOT ENABLED: " << what << " (" << e.http_status() << " " << e.status() << ": " << msg.substr(0, 90) << ")\n";
return;
}
std::cout << " " << what << ": " << e.http_status() << " " << e.status() << "\n";
FAIL(std::string(what) + " failed: " + e.status());
}
}
std::size_t count(const molo::clients::body::Json& j) { return j.is_array() ? j.as_array().size() : 0; }
#define LIVE_GUARD(name) \
if (!ready()) { \
std::cout << "SKIP " name " (set MOLO_LIVE_NET=1, GOOGLE_ACCESS_TOKEN and GOOGLE_CLOUD_PROJECT)\n"; \
return; \
}
} // namespace
TEST(cloudai_live, translation_reads) {
LIVE_GUARD("cloudai_live.translation_reads");
auto client = GoogleClient::from_env();
Translation t(client, context("global"));
probe("translation supportedLanguages", [&] {
auto j = t.supported_languages("en");
const auto* langs = j.find("languages");
return langs && langs->is_array() ? langs->as_array().size() : 0u;
});
probe("translation glossaries.list", [&] { return count(t.list_glossaries()); });
probe("translation adaptiveMtDatasets.list", [&] { return count(t.list_adaptive_datasets()); });
probe("translation models.list", [&] { return count(t.list_models()); });
}
TEST(cloudai_live, translation_one_paid_request) {
LIVE_GUARD("cloudai_live.translation_one_paid_request");
if (!std::getenv("MOLO_LIVE_PAID")) {
std::cout << "SKIP cloudai_live.translation_one_paid_request (set MOLO_LIVE_PAID=1: one short translateText)\n";
return;
}
auto client = GoogleClient::from_env();
Translation t(client, context("global"));
TranslateRequest r;
r.texts = {"hello"};
r.target = "nl";
r.source = "en";
probe("translation translateText (paid, 5 characters)", [&] {
auto out = t.translate(r);
std::cout << " -> " << (out.empty() ? "" : out[0].text) << "\n";
ASSERT_EQ(out.size(), 1u);
return out.size();
});
}
TEST(cloudai_live, other_apis_reads) {
LIVE_GUARD("cloudai_live.other_apis_reads");
auto client = GoogleClient::from_env();
Vision v(client, context("us-west1"));
probe("vision productSets.list", [&] { return count(v.list_product_sets()); });
DocumentAi d(client, context("us"));
probe("documentai processors.list", [&] { return count(d.list_processors()); });
probe("documentai processorTypes.list", [&] { return count(d.list_processor_types()); });
SpeechV2 s(client, context("us-central1"));
probe("speech recognizers.list", [&] { return count(s.list_recognizers()); });
LongAudio la(client, context("global"));
probe("texttospeech voices.list", [&] {
auto j = la.voices("nl-NL");
const auto* voices = j.find("voices");
return voices && voices->is_array() ? voices->as_array().size() : 0u;
});
VideoIntelligence vi(client, context("us-east1"));
probe("videointelligence operations.list", [&] { return count(vi.list_operations()); });
}