feat(clients): xai platform: voice, files, collections, management API, key info
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,449 @@
|
||||
// xAI platform REST: call table, ids, caps, request bodies (what would go on
|
||||
// the wire), destructive guards, host and key separation. No scripted vendor
|
||||
// replies: the rig records each request and refuses it.
|
||||
#include <set>
|
||||
|
||||
#include "rig.h"
|
||||
#include "xai/platform/account.h"
|
||||
#include "xai/platform/api_keys.h"
|
||||
#include "xai/platform/audit.h"
|
||||
#include "xai/platform/billing.h"
|
||||
#include "xai/platform/calls.h"
|
||||
#include "xai/platform/collections.h"
|
||||
#include "xai/platform/documents_search.h"
|
||||
#include "xai/platform/files.h"
|
||||
|
||||
using namespace molo::xai::platform;
|
||||
using namespace xai_test;
|
||||
using molo::llm::ApiError;
|
||||
using molo::llm::NotConfiguredError;
|
||||
using molo::clients::body::Json;
|
||||
|
||||
TEST(xai_platform_calls, table_is_ordered_unique_and_renders) {
|
||||
std::set<std::string> seen;
|
||||
for (std::size_t i = 0; i < call_count(); ++i) {
|
||||
const CallSpec& s = spec_at(i);
|
||||
ASSERT_EQ(static_cast<std::size_t>(s.call), i);
|
||||
ASSERT_TRUE(seen.insert(std::string(s.http_method) + " " + s.path).second);
|
||||
ASSERT_TRUE(std::string(s.host) == "api" || std::string(s.host) == "management");
|
||||
// Fill every placeholder with a valid id; no brace may survive.
|
||||
std::string path = s.path;
|
||||
std::size_t holes = 0;
|
||||
for (char c : path) holes += c == '{';
|
||||
std::string out;
|
||||
switch (holes) {
|
||||
case 0: out = render(s.call); break;
|
||||
case 1: out = render(s.call, {"a1"}); break;
|
||||
case 2: out = render(s.call, {"a1", "b2"}); break;
|
||||
default: FAIL("unexpected placeholder count");
|
||||
}
|
||||
ASSERT_TRUE(out.find('{') == std::string::npos);
|
||||
ASSERT_TRUE(out.front() == '/');
|
||||
}
|
||||
ASSERT_EQ(call_count(), static_cast<std::size_t>(59));
|
||||
}
|
||||
|
||||
TEST(xai_platform_calls, render_rejects_bad_segments) {
|
||||
ASSERT_EQ(render(Call::kFileGet, {"file_a128090d-f0c9-4873-bd84-e499777e7417"}),
|
||||
std::string("/v1/files/file_a128090d-f0c9-4873-bd84-e499777e7417"));
|
||||
ASSERT_EQ(render(Call::kDocBatchGet, {"collection_x"}),
|
||||
std::string("/v1/collections/collection_x/documents:batchGet"));
|
||||
ASSERT_THROW(render(Call::kFileGet, {"../x"}), ApiError);
|
||||
ASSERT_THROW(render(Call::kFileGet, {"a/b"}), ApiError);
|
||||
ASSERT_THROW(render(Call::kFileGet, {""}), ApiError);
|
||||
ASSERT_THROW(render(Call::kFileGet, {}), ApiError);
|
||||
ASSERT_THROW(render(Call::kFileGet, {"a", "b"}), ApiError);
|
||||
ASSERT_THROW(render(Call::kFileList, {"a"}), ApiError);
|
||||
}
|
||||
|
||||
TEST(xai_platform_calls, methods_and_hosts) {
|
||||
ASSERT_TRUE(method_of(Call::kKeyUpdate) == molo::clients::Method::kPut);
|
||||
ASSERT_TRUE(method_of(Call::kDocReindex) == molo::clients::Method::kPatch);
|
||||
ASSERT_TRUE(host_of(Call::kCollectionList) == Host::kManagement);
|
||||
ASSERT_TRUE(host_of(Call::kDocumentsSearch) == Host::kApi);
|
||||
}
|
||||
|
||||
TEST(xai_platform_account, key_info_and_me_use_the_api_key_only) {
|
||||
Rig r;
|
||||
AccountService svc(r.api);
|
||||
auto s = sent(r.api_mock, [&] { svc.api_key_info(); });
|
||||
ASSERT_EQ(s.method, std::string("GET"));
|
||||
ASSERT_TRUE(has(s.target, "/v1/api-key"));
|
||||
ASSERT_EQ(s.headers.get_or("Authorization", ""), std::string("Bearer API-SECRET"));
|
||||
auto m = sent(r.api_mock, [&] { svc.me(); });
|
||||
ASSERT_TRUE(has(m.target, "/v1/me"));
|
||||
// A management client must be refused for an api call.
|
||||
AccountService wrong(r.mgmt);
|
||||
refused(r.mgmt_mock, [&] { wrong.me(); });
|
||||
}
|
||||
|
||||
TEST(xai_platform_account, not_configured_before_network) {
|
||||
Rig r("", "");
|
||||
AccountService svc(r.api);
|
||||
try {
|
||||
svc.me();
|
||||
FAIL("expected NotConfiguredError");
|
||||
} catch (const NotConfiguredError& e) {
|
||||
ASSERT_EQ(e.http_status(), 503);
|
||||
ASSERT_TRUE(has(e.message(), "XAI_API_KEY"));
|
||||
}
|
||||
CollectionsService col(r.mgmt);
|
||||
try {
|
||||
col.list();
|
||||
FAIL("expected NotConfiguredError");
|
||||
} catch (const NotConfiguredError& e) {
|
||||
ASSERT_TRUE(has(e.message(), "XAI_MANAGEMENT_KEY"));
|
||||
}
|
||||
ASSERT_EQ(r.api_mock->send_count(), static_cast<std::size_t>(0));
|
||||
ASSERT_EQ(r.mgmt_mock->send_count(), static_cast<std::size_t>(0));
|
||||
}
|
||||
|
||||
TEST(xai_platform_files, upload_orders_expiry_before_file) {
|
||||
Rig r;
|
||||
FilesService svc(r.api);
|
||||
auto s = sent(r.api_mock, [&] { svc.upload("report.pdf", "application/pdf", "PDFDATA", 3600, "assistants"); });
|
||||
ASSERT_EQ(s.method, std::string("POST"));
|
||||
ASSERT_TRUE(has(s.headers.get_or("Content-Type", ""), "multipart/form-data"));
|
||||
std::size_t exp = s.body.find("name=\"expires_after\"");
|
||||
std::size_t file = s.body.find("name=\"file\"");
|
||||
ASSERT_TRUE(exp != std::string::npos && file != std::string::npos && exp < file);
|
||||
ASSERT_TRUE(has(s.body, "filename=\"report.pdf\""));
|
||||
ASSERT_TRUE(has(s.body, "name=\"purpose\""));
|
||||
}
|
||||
|
||||
TEST(xai_platform_files, upload_caps_and_name_checks) {
|
||||
Rig r;
|
||||
FilesService svc(r.api);
|
||||
refused(r.api_mock, [&] { svc.upload("", "text/plain", "x"); });
|
||||
refused(r.api_mock, [&] { svc.upload("a/b.txt", "text/plain", "x"); });
|
||||
refused(r.api_mock, [&] { svc.upload("a\"b.txt", "text/plain", "x"); });
|
||||
refused(r.api_mock, [&] { svc.upload("a.txt", "text/plain", ""); });
|
||||
refused(r.api_mock, [&] { svc.upload("a.txt", "text/plain", "x", 3599); });
|
||||
refused(r.api_mock, [&] { svc.upload("a.txt", "text/plain", "x", 2592001); });
|
||||
std::string big(PlatformCaps::kMaxFileBytes + 1, 'x');
|
||||
refused(r.api_mock, [&] { svc.upload("big.bin", "application/octet-stream", big); });
|
||||
}
|
||||
|
||||
TEST(xai_platform_files, list_query_is_validated_and_encoded) {
|
||||
Rig r;
|
||||
FilesService svc(r.api);
|
||||
FileListOptions o;
|
||||
o.limit = 50;
|
||||
o.order = "desc";
|
||||
o.sort_by = "size";
|
||||
o.filter = "name:\"quarterly report\" AND size_bytes > 1000";
|
||||
auto s = sent(r.api_mock, [&] { svc.list(o); });
|
||||
ASSERT_TRUE(has(s.target, "limit=50"));
|
||||
ASSERT_TRUE(has(s.target, "order=desc"));
|
||||
ASSERT_TRUE(has(s.target, "sort_by=size"));
|
||||
ASSERT_TRUE(has(s.target, "filter=name%3A%22quarterly%20report%22"));
|
||||
FileListOptions bad;
|
||||
bad.order = "sideways";
|
||||
refused(r.api_mock, [&] { svc.list(bad); });
|
||||
FileListOptions big;
|
||||
big.limit = PlatformCaps::kMaxLimit + 1;
|
||||
refused(r.api_mock, [&] { svc.list(big); });
|
||||
refused(r.api_mock, [&] { svc.list_all(0); });
|
||||
refused(r.api_mock, [&] { svc.list_all(101); });
|
||||
}
|
||||
|
||||
TEST(xai_platform_files, get_content_public_url_and_guards) {
|
||||
Rig r;
|
||||
FilesService svc(r.api);
|
||||
auto g = sent(r.api_mock, [&] { svc.get("file_abc"); });
|
||||
ASSERT_TRUE(has(g.target, "/v1/files/file_abc"));
|
||||
auto c = sent(r.api_mock, [&] { svc.content("file_abc", "text"); });
|
||||
ASSERT_TRUE(has(c.target, "/v1/files/file_abc/content?format=text"));
|
||||
refused(r.api_mock, [&] { svc.content("file_abc", "pdf"); });
|
||||
auto p = sent(r.api_mock, [&] { svc.create_public_url("file_abc", 7200); });
|
||||
ASSERT_EQ(p.body, std::string("{\"expires_after\":7200}"));
|
||||
ASSERT_TRUE(has(p.target, "/public-url"));
|
||||
refused(r.api_mock, [&] { svc.create_public_url("file_abc", 10); });
|
||||
// Destructive: no confirm, no request.
|
||||
refused(r.api_mock, [&] { svc.remove("file_abc", false); });
|
||||
refused(r.api_mock, [&] { svc.revoke_public_url("file_abc", false); });
|
||||
auto d = sent(r.api_mock, [&] { svc.remove("file_abc", true); });
|
||||
ASSERT_EQ(d.method, std::string("DELETE"));
|
||||
auto v = sent(r.api_mock, [&] { svc.revoke_public_url("file_abc", true); });
|
||||
ASSERT_TRUE(has(v.target, "/public-url/revoke"));
|
||||
refused(r.api_mock, [&] { svc.get("../etc/passwd"); });
|
||||
}
|
||||
|
||||
TEST(xai_platform_collections, create_update_list_bodies) {
|
||||
Rig r;
|
||||
CollectionsService svc(r.mgmt);
|
||||
CollectionSpec spec;
|
||||
spec.name = "SEC Filings";
|
||||
spec.description = "10-K and 10-Q";
|
||||
spec.embedding_model = "grok-embedding-small";
|
||||
spec.metric_space = "HNSW_METRIC_COSINE";
|
||||
Json defs = Json::array();
|
||||
Json d = Json::object();
|
||||
d.set("key", "author");
|
||||
d.set("required", Json(true));
|
||||
defs.push(d);
|
||||
spec.field_definitions = defs;
|
||||
auto s = sent(r.mgmt_mock, [&] { svc.create(spec); });
|
||||
ASSERT_EQ(s.method, std::string("POST"));
|
||||
ASSERT_TRUE(has(s.target, "/v1/collections"));
|
||||
ASSERT_EQ(s.headers.get_or("Authorization", ""), std::string("Bearer MGMT-SECRET"));
|
||||
Json body = Json::parse(s.body);
|
||||
ASSERT_EQ(body.find("collection_name")->as_string(), std::string("SEC Filings"));
|
||||
ASSERT_EQ(body.find("index_configuration")->find("model_name")->as_string(), std::string("grok-embedding-small"));
|
||||
ASSERT_EQ(body.find("metric_space")->as_string(), std::string("HNSW_METRIC_COSINE"));
|
||||
ASSERT_EQ(body.find("field_definitions")->as_array().size(), static_cast<std::size_t>(1));
|
||||
|
||||
CollectionSpec bad;
|
||||
refused(r.mgmt_mock, [&] { svc.create(bad); });
|
||||
bad.name = "x";
|
||||
bad.metric_space = "COSINE";
|
||||
refused(r.mgmt_mock, [&] { svc.create(bad); });
|
||||
bad.metric_space = "";
|
||||
bad.team_id = "not-a-uuid";
|
||||
refused(r.mgmt_mock, [&] { svc.create(bad); });
|
||||
|
||||
CollectionSpec ren;
|
||||
ren.name = "SEC Filings (New)";
|
||||
auto u = sent(r.mgmt_mock, [&] { svc.update("collection_x", ren); });
|
||||
ASSERT_EQ(u.method, std::string("PUT"));
|
||||
ASSERT_EQ(u.body, std::string("{\"collection_name\":\"SEC Filings (New)\"}"));
|
||||
refused(r.mgmt_mock, [&] { svc.update("collection_x", CollectionSpec()); });
|
||||
Json ups = Json::array();
|
||||
Json one = Json::object();
|
||||
Json def = Json::object();
|
||||
def.set("key", "year");
|
||||
one.set("field_definition", def);
|
||||
one.set("operation", "FIELD_DEFINITION_ADD");
|
||||
ups.push(one);
|
||||
auto u2 = sent(r.mgmt_mock, [&] { svc.update("collection_x", CollectionSpec(), ups); });
|
||||
ASSERT_TRUE(has(u2.body, "field_definition_updates"));
|
||||
}
|
||||
|
||||
TEST(xai_platform_collections, list_and_document_queries) {
|
||||
Rig r;
|
||||
CollectionsService svc(r.mgmt);
|
||||
ListOptions o;
|
||||
o.limit = 10;
|
||||
o.order = "ORDERING_DESCENDING";
|
||||
o.sort_by = "COLLECTIONS_SORT_BY_AGE";
|
||||
o.team_id = kTeam;
|
||||
auto s = sent(r.mgmt_mock, [&] { svc.list(o); });
|
||||
ASSERT_TRUE(has(s.target, "team_id=" + std::string(kTeam)));
|
||||
ASSERT_TRUE(has(s.target, "sort_by=COLLECTIONS_SORT_BY_AGE"));
|
||||
ListOptions wrong = o;
|
||||
wrong.sort_by = "DOCUMENTS_SORT_BY_NAME";
|
||||
refused(r.mgmt_mock, [&] { svc.list(wrong); });
|
||||
ListOptions docs;
|
||||
docs.sort_by = "DOCUMENTS_SORT_BY_SIZE";
|
||||
docs.name = "paper.pdf";
|
||||
auto d = sent(r.mgmt_mock, [&] { svc.list_documents("collection_x", docs); });
|
||||
ASSERT_TRUE(has(d.target, "/v1/collections/collection_x/documents?"));
|
||||
ASSERT_TRUE(has(d.target, "name=paper.pdf"));
|
||||
auto b = sent(r.mgmt_mock, [&] { svc.batch_get("collection_x", {"file_1", "file_2"}); });
|
||||
ASSERT_TRUE(has(b.target, "documents:batchGet?file_ids=file_1&file_ids=file_2"));
|
||||
refused(r.mgmt_mock, [&] { svc.batch_get("collection_x", {}); });
|
||||
std::vector<std::string> many(PlatformCaps::kMaxBatchFileIds + 1, "f");
|
||||
refused(r.mgmt_mock, [&] { svc.batch_get("collection_x", many); });
|
||||
}
|
||||
|
||||
TEST(xai_platform_collections, documents_add_upload_and_guards) {
|
||||
Rig r;
|
||||
CollectionsService svc(r.mgmt);
|
||||
Json fields = Json::object();
|
||||
fields.set("author", "Sandra Kim");
|
||||
auto a = sent(r.mgmt_mock, [&] { svc.add_document("collection_x", "file_y", fields); });
|
||||
ASSERT_EQ(a.method, std::string("POST"));
|
||||
ASSERT_TRUE(has(a.target, "/v1/collections/collection_x/documents/file_y"));
|
||||
ASSERT_EQ(a.body, std::string("{\"fields\":{\"author\":\"Sandra Kim\"}}"));
|
||||
auto u = sent(r.mgmt_mock, [&] { svc.upload_document("collection_x", "paper.pdf", "application/pdf", "PDF", fields); });
|
||||
ASSERT_TRUE(has(u.target, "/v1/collections/collection_x/documents"));
|
||||
ASSERT_TRUE(has(u.body, "name=\"data\"; filename=\"paper.pdf\""));
|
||||
ASSERT_TRUE(has(u.body, "name=\"fields\""));
|
||||
Json notobj = Json::array();
|
||||
refused(r.mgmt_mock, [&] { svc.add_document("collection_x", "file_y", notobj); });
|
||||
auto re = sent(r.mgmt_mock, [&] { svc.reindex_document("collection_x", "file_y"); });
|
||||
ASSERT_EQ(re.method, std::string("PATCH"));
|
||||
refused(r.mgmt_mock, [&] { svc.remove("collection_x", false); });
|
||||
refused(r.mgmt_mock, [&] { svc.remove_document("collection_x", "file_y", false); });
|
||||
auto d = sent(r.mgmt_mock, [&] { svc.remove_document("collection_x", "file_y", true); });
|
||||
ASSERT_EQ(d.method, std::string("DELETE"));
|
||||
auto c = sent(r.mgmt_mock, [&] { svc.remove("collection_x", true); });
|
||||
ASSERT_EQ(c.method, std::string("DELETE"));
|
||||
std::string big(PlatformCaps::kMaxCollectionDocBytes + 1, 'x');
|
||||
refused(r.mgmt_mock, [&] { svc.upload_document("collection_x", "big.txt", "text/plain", big); });
|
||||
// Collection management is the management host: the api client is refused.
|
||||
CollectionsService wrong(r.api);
|
||||
refused(r.api_mock, [&] { wrong.list(); });
|
||||
}
|
||||
|
||||
TEST(xai_platform_search, body_matches_the_documented_example) {
|
||||
Rig r;
|
||||
DocumentSearch svc(r.api);
|
||||
DocumentSearchRequest q;
|
||||
q.query = "What were the key revenue drivers based on the SEC filings?";
|
||||
q.collection_ids = {"collection_dbc087b1-6c99-493d-86c6-b401fee34a9d"};
|
||||
q.retrieval_mode = "hybrid";
|
||||
auto s = sent(r.api_mock, [&] { svc.search(q); });
|
||||
ASSERT_TRUE(has(s.target, "/v1/documents/search"));
|
||||
Json b = Json::parse(s.body);
|
||||
ASSERT_EQ(b.find("source")->find("collection_ids")->as_array()[0].as_string(),
|
||||
std::string("collection_dbc087b1-6c99-493d-86c6-b401fee34a9d"));
|
||||
ASSERT_EQ(b.find("retrieval_mode")->find("type")->as_string(), std::string("hybrid"));
|
||||
q.group_by = Json::object();
|
||||
refused(r.api_mock, [&] { svc.search(q); });
|
||||
DocumentSearchRequest none;
|
||||
refused(r.api_mock, [&] { svc.search(none); });
|
||||
none.query = "x";
|
||||
refused(r.api_mock, [&] { svc.search(none); });
|
||||
none.collection_ids = {"c"};
|
||||
none.retrieval_mode = "fuzzy";
|
||||
refused(r.api_mock, [&] { svc.search(none); });
|
||||
none.retrieval_mode = "";
|
||||
none.ranking_metric = "RANKING_METRIC_NONE";
|
||||
refused(r.api_mock, [&] { svc.search(none); });
|
||||
none.ranking_metric = "";
|
||||
none.rag_pipeline = "solr";
|
||||
refused(r.api_mock, [&] { svc.search(none); });
|
||||
}
|
||||
|
||||
TEST(xai_platform_keys, create_matches_the_documented_example) {
|
||||
Rig r;
|
||||
ApiKeysService svc(r.mgmt);
|
||||
ApiKeySpec spec;
|
||||
spec.name = "My API key";
|
||||
spec.acls = {"api-key:model:*", "api-key:endpoint:*"};
|
||||
spec.qps = 5;
|
||||
spec.qpm = 100;
|
||||
spec.tpm = 100000;
|
||||
auto s = sent(r.mgmt_mock, [&] { svc.create(kTeam, spec); });
|
||||
ASSERT_TRUE(has(s.target, std::string("/auth/teams/") + kTeam + "/api-keys"));
|
||||
Json b = Json::parse(s.body);
|
||||
ASSERT_EQ(b.find("qps")->as_int(), 5L);
|
||||
ASSERT_TRUE(b.find("tpm")->is_string()); // spec: tpm is a string
|
||||
ASSERT_EQ(b.find("acls")->as_array().size(), static_cast<std::size_t>(2));
|
||||
ApiKeySpec bad;
|
||||
refused(r.mgmt_mock, [&] { svc.create(kTeam, bad); });
|
||||
bad.name = "k";
|
||||
bad.acls = {"admin:everything"};
|
||||
refused(r.mgmt_mock, [&] { svc.create(kTeam, bad); });
|
||||
bad.acls = {};
|
||||
bad.qps = -1;
|
||||
refused(r.mgmt_mock, [&] { svc.create(kTeam, bad); });
|
||||
refused(r.mgmt_mock, [&] { svc.create("team-1", spec); });
|
||||
}
|
||||
|
||||
TEST(xai_platform_keys, list_update_rotate_delete) {
|
||||
Rig r;
|
||||
ApiKeysService svc(r.mgmt);
|
||||
auto l = sent(r.mgmt_mock, [&] { svc.list(kTeam, 10); });
|
||||
ASSERT_TRUE(has(l.target, "pageSize=10"));
|
||||
ASSERT_FALSE(has(l.target, "paginationToken")); // empty token is rejected by the server: never sent
|
||||
auto l2 = sent(r.mgmt_mock, [&] { svc.list(kTeam, 10, "tok", true, {"api-key:model:grok-4"}); });
|
||||
ASSERT_TRUE(has(l2.target, "paginationToken=tok"));
|
||||
ASSERT_TRUE(has(l2.target, "activeOnly=true"));
|
||||
ASSERT_TRUE(has(l2.target, "aclFilters=api-key%3Amodel%3Agrok-4"));
|
||||
refused(r.mgmt_mock, [&] { svc.list_all(kTeam, 0); });
|
||||
|
||||
Json ch = Json::object();
|
||||
Json inner = Json::object();
|
||||
inner.set("qpm", Json(200));
|
||||
ch = inner;
|
||||
auto u = sent(r.mgmt_mock, [&] { svc.update(kKeyId, ch); });
|
||||
ASSERT_EQ(u.method, std::string("PUT"));
|
||||
ASSERT_EQ(u.body, std::string("{\"apiKey\":{\"qpm\":200},\"fieldMask\":\"qpm\"}"));
|
||||
Json unknown = Json::object();
|
||||
unknown.set("userId", "x");
|
||||
refused(r.mgmt_mock, [&] { svc.update(kKeyId, unknown); });
|
||||
refused(r.mgmt_mock, [&] { svc.update(kKeyId, Json::object()); });
|
||||
|
||||
refused(r.mgmt_mock, [&] { svc.rotate(kKeyId, false); });
|
||||
auto ro = sent(r.mgmt_mock, [&] { svc.rotate(kKeyId, true, "2027-01-01T00:00:00Z", "2026-12-01T00:00:00Z"); });
|
||||
ASSERT_TRUE(has(ro.target, "/rotate"));
|
||||
ASSERT_TRUE(has(ro.body, "oldSecretExpireTime"));
|
||||
refused(r.mgmt_mock, [&] { svc.remove(kKeyId, false); });
|
||||
auto d = sent(r.mgmt_mock, [&] { svc.remove(kKeyId, true); });
|
||||
ASSERT_EQ(d.method, std::string("DELETE"));
|
||||
auto p = sent(r.mgmt_mock, [&] { svc.propagation(kKeyId); });
|
||||
ASSERT_TRUE(has(p.target, "/propagation"));
|
||||
auto m = sent(r.mgmt_mock, [&] { svc.team_models(kTeam); });
|
||||
ASSERT_TRUE(has(m.target, "/models"));
|
||||
auto e = sent(r.mgmt_mock, [&] { svc.team_endpoints(kTeam); });
|
||||
ASSERT_TRUE(has(e.target, "/endpoints"));
|
||||
auto v = sent(r.mgmt_mock, [&] { svc.validate_management_key(); });
|
||||
ASSERT_TRUE(has(v.target, "/auth/management-keys/validation"));
|
||||
}
|
||||
|
||||
TEST(xai_platform_billing, guards_amounts_and_bodies) {
|
||||
Rig r;
|
||||
BillingService svc(r.mgmt);
|
||||
ASSERT_TRUE(BillingService::valid_amount("10"));
|
||||
ASSERT_TRUE(BillingService::valid_amount("10.50"));
|
||||
ASSERT_FALSE(BillingService::valid_amount("-1"));
|
||||
ASSERT_FALSE(BillingService::valid_amount("1.234"));
|
||||
ASSERT_FALSE(BillingService::valid_amount("1e3"));
|
||||
ASSERT_FALSE(BillingService::valid_amount(""));
|
||||
refused(r.mgmt_mock, [&] { svc.top_up(kTeam, "10", false); });
|
||||
refused(r.mgmt_mock, [&] { svc.top_up(kTeam, "ten", true); });
|
||||
auto t = sent(r.mgmt_mock, [&] { svc.top_up(kTeam, "10", true); });
|
||||
ASSERT_EQ(t.body, std::string("{\"amount\":{\"val\":\"10\"}}"));
|
||||
ASSERT_TRUE(has(t.target, "/prepaid/top-up"));
|
||||
refused(r.mgmt_mock, [&] { svc.set_soft_spending_limit(kTeam, "100", false); });
|
||||
auto sl = sent(r.mgmt_mock, [&] { svc.set_soft_spending_limit(kTeam, "100", true); });
|
||||
ASSERT_EQ(sl.body, std::string("{\"desiredSoftSpendingLimit\":{\"val\":\"100\"}}"));
|
||||
refused(r.mgmt_mock, [&] { svc.set_default_payment_method(kTeam, "pm_1", false); });
|
||||
refused(r.mgmt_mock, [&] { svc.set_billing_info(kTeam, Json::object(), true); });
|
||||
Json info = Json::object();
|
||||
info.set("name", "Acme Inc.");
|
||||
refused(r.mgmt_mock, [&] { svc.set_billing_info(kTeam, info, false); });
|
||||
auto bi = sent(r.mgmt_mock, [&] { svc.set_billing_info(kTeam, info, true); });
|
||||
ASSERT_EQ(bi.body, std::string("{\"billingInfo\":{\"name\":\"Acme Inc.\"}}"));
|
||||
refused(r.mgmt_mock, [&] { svc.prepaid_balance("team"); });
|
||||
auto pb = sent(r.mgmt_mock, [&] { svc.prepaid_balance(kTeam); });
|
||||
ASSERT_TRUE(has(pb.target, "/prepaid/balance"));
|
||||
}
|
||||
|
||||
TEST(xai_platform_billing, usage_invoices_and_audit) {
|
||||
Rig r;
|
||||
BillingService svc(r.mgmt);
|
||||
// The documented usage request example.
|
||||
Json range = Json::object();
|
||||
range.set("startTime", "2025-10-01 00:00:00");
|
||||
range.set("endTime", "2025-10-07 23:59:59");
|
||||
range.set("timezone", "Etc/GMT");
|
||||
Json val = Json::object();
|
||||
val.set("name", "usd");
|
||||
val.set("aggregation", "AGGREGATION_SUM");
|
||||
Json vals = Json::array();
|
||||
vals.push(val);
|
||||
Json req = Json::object();
|
||||
req.set("timeRange", range);
|
||||
req.set("timeUnit", "TIME_UNIT_DAY");
|
||||
req.set("values", vals);
|
||||
auto u = sent(r.mgmt_mock, [&] { svc.usage(kTeam, req); });
|
||||
ASSERT_TRUE(has(u.target, "/usage"));
|
||||
ASSERT_TRUE(has(u.body, "\"analyticsRequest\""));
|
||||
refused(r.mgmt_mock, [&] { svc.usage(kTeam, Json::object()); });
|
||||
InvoiceQuery iq;
|
||||
iq.cycle_year = 2026;
|
||||
iq.cycle_month = 9;
|
||||
auto inv = sent(r.mgmt_mock, [&] { svc.invoices(kTeam, iq); });
|
||||
ASSERT_TRUE(has(inv.target, "billingCycle.year=2026&billingCycle.month=9"));
|
||||
InvoiceQuery half;
|
||||
half.since_year = 2026;
|
||||
refused(r.mgmt_mock, [&] { svc.invoices(kTeam, half); });
|
||||
|
||||
AuditService audit(r.mgmt);
|
||||
AuditQuery a;
|
||||
a.page_size = 50;
|
||||
a.time_from = "2025-01-01T00:00:00Z";
|
||||
a.order_by = "TIME_DESCENDING";
|
||||
a.user_id = "user-123";
|
||||
auto e = sent(r.mgmt_mock, [&] { audit.events(kTeam, a); });
|
||||
ASSERT_TRUE(has(e.target, "/audit/teams/" + std::string(kTeam) + "/events?"));
|
||||
ASSERT_TRUE(has(e.target, "eventFilter.userId=user-123"));
|
||||
ASSERT_TRUE(has(e.target, "eventTimeFrom=2025-01-01T00%3A00%3A00Z"));
|
||||
AuditQuery bad;
|
||||
bad.order_by = "RANDOM";
|
||||
refused(r.mgmt_mock, [&] { audit.events(kTeam, bad); });
|
||||
refused(r.mgmt_mock, [&] { audit.events_all(kTeam, 0); });
|
||||
}
|
||||
Reference in New Issue
Block a user