Merge branch 'worktree-agent-af2efb541856af61f'
This commit is contained in:
@@ -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()); });
|
||||
}
|
||||
Reference in New Issue
Block a user