Merge xai core package

This commit is contained in:
2026-10-24 08:12:20 +02:00
55 changed files with 8148 additions and 3 deletions
+822
View File
@@ -0,0 +1,822 @@
// xAI core: deterministic local tests. Call table, validation, caps, request
// bodies, parsing of fixtures copied verbatim from docs.x.ai, SSE decoding,
// local polling logic with a fake sleeper, the ChatModel mapping, and the
// no-credential behaviour. No scripted vendor replies: shapes are verified
// against the published OpenAPI document by src/xai/core/tools/check_spec.py
// and, with a key, live (opt-in tests at the end of this file).
#include <cstdlib>
#include <iostream>
#include <memory>
#include "framework.h"
#include "clients/transports/mock_transport.h"
#include "llm/config.h"
#include "xai/core/client.h"
using namespace molo::xai::core;
using molo::clients::Method;
using molo::clients::transports::MockTransport;
namespace {
struct Rig {
MockTransport* mock;
std::shared_ptr<molo::llm::ApiClient> api;
Rig() {
auto t = std::make_unique<MockTransport>();
mock = t.get();
api = std::make_shared<molo::llm::ApiClient>(std::move(t), XaiService::default_config(), "");
}
};
template <typename F>
bool rejects(F&& f) {
try {
f();
} catch (const molo::llm::ApiError& e) {
return e.http_status() == 400 && e.type() == "INVALID_ARGUMENT";
}
return false;
}
template <typename F>
bool not_configured(F&& f) {
try {
f();
} catch (const molo::llm::NotConfiguredError&) {
return true;
}
return false;
}
} // namespace
TEST(xai_core, call_table_is_consistent) {
ASSERT_EQ(call_count(), 43u);
for (std::size_t i = 0; i < call_count(); ++i) {
const CallSpec& s = spec_at(i);
ASSERT_TRUE(&spec_of(s.call) == &s);
ASSERT_TRUE(std::string(s.path).rfind("/v1/", 0) == 0);
std::string m = s.http_method;
ASSERT_TRUE(m == "GET" || m == "POST" || m == "DELETE");
ASSERT_TRUE(std::string(s.source) == "openapi" || std::string(s.source) == "docs");
}
ASSERT_EQ(render(Call::kResponsesCreate), "/v1/responses");
ASSERT_EQ(render(Call::kResponsesGet, {"resp_1"}), "/v1/responses/resp_1");
ASSERT_EQ(render(Call::kResponsesInputItems, {"resp_1"}), "/v1/responses/resp_1/input_items");
ASSERT_EQ(render(Call::kBatchCancel, {"batch_1"}), "/v1/batches/batch_1:cancel");
ASSERT_EQ(render(Call::kChatDeferredGet, {"abc-1"}), "/v1/chat/deferred-completion/abc-1");
ASSERT_TRUE(method_of(Call::kResponsesDelete) == Method::kDelete);
ASSERT_TRUE(method_of(Call::kModelsList) == Method::kGet);
ASSERT_TRUE(method_of(Call::kBatchCreate) == Method::kPost);
}
TEST(xai_core, ids_are_validated_before_a_path_is_built) {
ASSERT_TRUE(rejects([] { render(Call::kResponsesGet, {"../etc"}); }));
ASSERT_TRUE(rejects([] { render(Call::kResponsesGet, {"a/b"}); }));
ASSERT_TRUE(rejects([] { render(Call::kResponsesGet, {""}); }));
ASSERT_TRUE(rejects([] { render(Call::kResponsesGet, {}); }));
ASSERT_TRUE(rejects([] { render(Call::kResponsesGet, {"a", "b"}); }));
ASSERT_TRUE(rejects([] { render(Call::kResponsesCreate, {"x"}); }));
}
TEST(xai_core, query_builder_uses_declared_params_only) {
Query q;
q.add(Call::kBatchList, "limit", 20L).add(Call::kBatchList, "pagination_token", "a b/c");
ASSERT_EQ(q.str(), "?limit=20&pagination_token=a%20b%2Fc");
ASSERT_TRUE(rejects([] { Query().add(Call::kBatchList, "bogus", "1"); }));
ASSERT_TRUE(rejects([] { Query().add(Call::kBatchList, "limit", ""); }));
ASSERT_EQ(Query().str(), "");
}
TEST(xai_core, plan_rejects_undeclared_body_fields) {
Json b = Json::object();
b.set("model", Json("grok-4.3"));
b.set("input", Json("hi"));
Plan p = XaiService::plan(Call::kResponsesCreate, {}, &b);
ASSERT_EQ(p.path, "/v1/responses");
ASSERT_TRUE(p.has_body);
b.set("surprise", Json(1));
ASSERT_TRUE(rejects([&] { XaiService::plan(Call::kResponsesCreate, {}, &b); }));
Json arr = Json::array();
ASSERT_TRUE(rejects([&] { XaiService::plan(Call::kResponsesCreate, {}, &arr); }));
// documented-only fields are allowed
Json g = Json::object();
g.set("prompt", Json("a cat"));
g.set("quality", Json("low"));
XaiService::plan(Call::kImagesGenerate, {}, &g);
}
TEST(xai_core, no_credential_means_no_traffic) {
Rig r;
XaiClient c(r.api);
ASSERT_FALSE(c.configured());
ASSERT_TRUE(not_configured([&] { c.models.list(); }));
ASSERT_TRUE(not_configured([&] { c.account.api_key(); }));
ASSERT_TRUE(not_configured([&] { c.responses.create(ResponseRequest("grok-4.3").input_text("hi")); }));
ASSERT_TRUE(not_configured([&] { c.responses.remove("resp_1"); }));
ASSERT_TRUE(not_configured([&] { c.batches.get("batch_1"); }));
ASSERT_TRUE(not_configured([&] { c.tokenizer.tokenize("grok-4.3", "hi"); }));
ASSERT_EQ(r.mock->send_count(), 0u);
// validation runs before the credential check
ASSERT_TRUE(rejects([&] { c.responses.create(ResponseRequest("")); }));
ASSERT_TRUE(rejects([&] { c.responses.retrieve("a/b"); }));
}
TEST(xai_core, responses_request_body) {
ResponseRequest r("grok-4.3");
r.input_text("hello").instructions("be brief").store(false).max_output_tokens(100).temperature(0.5)
.top_p(0.9).reasoning_effort(Effort::kLow).reasoning_summary("auto")
.add_tool(WebSearchTool().allow_domains({"x.ai"})).add_tool(XSearchTool().from_date("2026-01-01"))
.tool_choice(ToolChoice::automatic()).parallel_tool_calls(true)
.include(Include::kEncryptedReasoning).service_tier("priority");
Json b = r.to_json(true);
ASSERT_EQ(b.find("model")->as_string(), "grok-4.3");
ASSERT_EQ(b.find("input")->as_string(), "hello");
ASSERT_TRUE(b.find("stream")->as_bool());
ASSERT_FALSE(b.find("store")->as_bool());
ASSERT_EQ(b.find("reasoning")->find("effort")->as_string(), "low");
ASSERT_EQ(b.find("reasoning")->find("summary")->as_string(), "auto");
ASSERT_EQ(b.find("tools")->as_array().size(), 2u);
ASSERT_EQ(b.find("tools")->as_array()[0].find("type")->as_string(), "web_search");
ASSERT_EQ(b.find("tool_choice")->as_string(), "auto");
ASSERT_EQ(b.find("include")->as_array()[0].as_string(), "reasoning.encrypted_content");
XaiService::plan(Call::kResponsesCreate, {}, &b); // every field is in the table
ASSERT_TRUE(b.find("stream") != nullptr);
ASSERT_TRUE(r.to_json(false).find("stream") == nullptr);
}
TEST(xai_core, responses_request_validation) {
auto base = [] { return ResponseRequest("m").input_text("x"); };
ASSERT_TRUE(rejects([&] { base().temperature(2.5).to_json(); }));
ASSERT_TRUE(rejects([&] { base().top_p(-0.1).to_json(); }));
ASSERT_TRUE(rejects([&] { base().max_output_tokens(0).to_json(); }));
ASSERT_TRUE(rejects([&] { base().top_logprobs(3).to_json(); }));
ASSERT_TRUE(rejects([&] { base().top_logprobs(9).logprobs(true).to_json(); }));
ASSERT_TRUE(rejects([&] { base().instructions("a").previous_response_id("r1").to_json(); }));
ASSERT_TRUE(rejects([&] { base().reasoning_summary("loud").to_json(); }));
ASSERT_TRUE(rejects([&] { base().service_tier("turbo").to_json(); }));
ASSERT_TRUE(rejects([&] { base().previous_response_id("a/b").to_json(); }));
ASSERT_TRUE(rejects([&] { ResponseRequest("m").to_json(); })); // no input
ASSERT_TRUE(rejects([&] { base().include("bad value").to_json(); }));
ASSERT_TRUE(rejects([&] { base().guideline({"nsfw"}).guideline({"nsfw"}).to_json(); }));
ResponseRequest many("m");
many.input_text("x");
for (std::size_t i = 0; i < Caps::kMaxIncludes; ++i) many.include("a" + std::to_string(i));
ASSERT_TRUE(rejects([&] { many.include("one-too-many"); }));
}
TEST(xai_core, tools_build_and_validate) {
Json fn = FunctionTool("get_weather", "Weather", Json::parse("{\"type\":\"object\"}")).strict(true).to_json();
ASSERT_EQ(fn.find("type")->as_string(), "function");
ASSERT_EQ(fn.find("name")->as_string(), "get_weather");
Json chat = FunctionTool("get_weather", "", Json::object()).to_chat_json();
ASSERT_EQ(chat.find("function")->find("name")->as_string(), "get_weather");
ASSERT_TRUE(rejects([] { FunctionTool("bad name", "", Json::object()).to_json(); }));
ASSERT_TRUE(rejects([] { FunctionTool("f", "", Json()).to_json(); }));
ASSERT_TRUE(rejects([] { WebSearchTool().allow_domains({"a.com"}).exclude_domains({"b.com"}).to_json(); }));
ASSERT_TRUE(rejects([] { WebSearchTool().allow_domains({"1.a", "2.a", "3.a", "4.a", "5.a", "6.a"}).to_json(); }));
ASSERT_TRUE(rejects([] { WebSearchTool().allow_domains({"https://a.com"}).to_json(); }));
ASSERT_TRUE(rejects([] { XSearchTool().allow_handles({"@elon"}).to_json(); }));
ASSERT_TRUE(rejects([] { XSearchTool().from_date("2026-13-01").to_json(); }));
ASSERT_TRUE(rejects([] { XSearchTool().from_date("2026-02-01").to_date("2026-01-01").to_json(); }));
Json loc = WebSearchTool().location({"US", "Austin", "", "America/Chicago"}).to_json();
ASSERT_EQ(loc.find("user_location")->find("type")->as_string(), "approximate");
Json x = XSearchTool().allow_handles({"xai"}).video_understanding(true).to_json();
ASSERT_EQ(x.find("allowed_x_handles")->as_array().size(), 1u);
ASSERT_EQ(CodeExecutionTool().to_json().find("type")->as_string(), "code_interpreter");
Json fs = CollectionsSearchTool({"col_1"}).max_results(10).to_json();
ASSERT_EQ(fs.find("type")->as_string(), "file_search");
ASSERT_EQ(fs.find("vector_store_ids")->as_array()[0].as_string(), "col_1");
ASSERT_TRUE(rejects([] { CollectionsSearchTool({}).to_json(); }));
Json mcp = McpTool("deepwiki", "https://mcp.deepwiki.com/mcp").allow_tools({"ask"}).header("X-A", "1").to_json();
ASSERT_EQ(mcp.find("type")->as_string(), "mcp");
ASSERT_EQ(mcp.find("headers")->find("X-A")->as_string(), "1");
ASSERT_TRUE(rejects([] { McpTool("l", "ftp://x").to_json(); }));
ASSERT_EQ(ImageGenerationTool().action("edit").to_json().find("action")->as_string(), "edit");
ASSERT_TRUE(rejects([] { ImageGenerationTool().action("draw").to_json(); }));
ASSERT_EQ(ShellTool().to_json().find("environment")->find("type")->as_string(), "local");
ASSERT_EQ(ToolSearchTool().to_json().find("execution")->as_string(), "server");
ASSERT_EQ(ToolChoice::function("f").to_responses().find("name")->as_string(), "f");
ASSERT_EQ(ToolChoice::function("f").to_chat().find("function")->find("name")->as_string(), "f");
ASSERT_EQ(ToolChoice::required().to_chat().as_string(), "required");
ASSERT_FALSE(ToolChoice::from_neutral("").has_value());
// the TOOLS table lists every builder's type
std::size_t found = 0;
for (std::size_t i = 0; i < tool_field_count(); ++i) {
std::string t = tool_fields_at(i).type;
if (t == "function" || t == "web_search" || t == "x_search" || t == "code_interpreter" ||
t == "file_search" || t == "mcp" || t == "image_generation" || t == "shell" || t == "tool_search")
++found;
}
ASSERT_EQ(found, 9u);
}
TEST(xai_core, structured_output_formats_and_schema_lint) {
Json schema = Json::parse("{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"string\"}},\"required\":[\"a\"]}");
ResponseFormat f = ResponseFormat::json_schema("answer", schema);
Json c = f.to_chat();
ASSERT_EQ(c.find("type")->as_string(), "json_schema");
ASSERT_EQ(c.find("json_schema")->find("name")->as_string(), "answer");
Json r = f.to_responses_text();
ASSERT_EQ(r.find("format")->find("type")->as_string(), "json_schema");
ASSERT_TRUE(r.find("format")->find("schema") != nullptr);
ASSERT_EQ(ResponseFormat::json_object().to_chat().find("type")->as_string(), "json_object");
ASSERT_EQ(ResponseFormat::text().to_responses_text().find("format")->find("type")->as_string(), "text");
ASSERT_TRUE(rejects([] { ResponseFormat::json_schema("n", Json::parse("{\"enum\":[]}")); }));
ASSERT_TRUE(rejects([] { ResponseFormat::json_schema("n", Json::parse("{\"anyOf\":[]}")); }));
ASSERT_TRUE(rejects([] { ResponseFormat::json_schema("n", Json::parse("{\"type\":\"array\",\"items\":[{}]}")); }));
ASSERT_TRUE(rejects([] { ResponseFormat::json_schema("n", Json::parse("{\"properties\":{\"a\":true}}")); }));
ASSERT_TRUE(rejects([] { ResponseFormat::json_schema("n", Json::parse("{\"minContains\":1}")); }));
ASSERT_TRUE(rejects([] { ResponseFormat::json_schema("bad name!", Json::object()); }));
ASSERT_TRUE(rejects([] { ResponseFormat::json_schema("n", Json("not an object")); }));
}
TEST(xai_core, input_items) {
Input in;
in.add(InputItem::message("user", {InputPart::text("look"), InputPart::image("https://x.ai/a.png")}));
in.add(InputItem::function_output("call_1", "42"));
in.add(InputItem::shell_output("call_2", "out", "", 0));
Json j = in.to_json();
ASSERT_EQ(j.as_array().size(), 3u);
ASSERT_EQ(j.as_array()[0].find("content")->as_array()[1].find("type")->as_string(), "input_image");
ASSERT_EQ(j.as_array()[1].find("type")->as_string(), "function_call_output");
ASSERT_EQ(j.as_array()[2].find("output")->as_array()[0].find("outcome")->find("type")->as_string(), "exit");
ASSERT_TRUE(rejects([] { InputItem::message("robot", "hi"); }));
ASSERT_TRUE(rejects([] { InputPart::image("ftp://x"); }));
ASSERT_TRUE(rejects([] { InputPart::file_id("a/b"); }));
ASSERT_TRUE(rejects([] { Input().to_json(); }));
ASSERT_TRUE(rejects([] { Input("t").add(InputItem::message("user", "x")); }));
ASSERT_TRUE(rejects([] { InputItem::replay(Json::parse("{\"type\":\"nonsense\"}")); }));
Json enc = Json::parse("{\"type\":\"reasoning\",\"id\":\"rs_1\",\"summary\":[],\"encrypted_content\":\"abc\"}");
ASSERT_EQ(InputItem::replay(enc).type(), "reasoning");
ASSERT_EQ(InputPart::file_data("a.pdf", "QUJD", "application/pdf").json().find("filename")->as_string(), "a.pdf");
}
TEST(xai_core, chat_request_body) {
ChatRequest r("grok-4.3");
r.add(ChatMessage::system("sys")).add(ChatMessage::user("hi"))
.add(ChatMessage::user({ChatPart::text("see"), ChatPart::image("https://x.ai/i.png")}))
.max_completion_tokens(50).temperature(0.2).n(2).seed(7).stop({"END"}).reasoning_effort("high")
.add_tool(FunctionTool("f", "d", Json::object())).tool_choice(ToolChoice::none())
.response_format(ResponseFormat::json_object()).include_usage(true);
Json b = r.to_json(true);
ASSERT_TRUE(b.find("stream")->as_bool());
ASSERT_TRUE(b.find("stream_options")->find("include_usage")->as_bool());
ASSERT_EQ(b.find("messages")->as_array().size(), 3u);
ASSERT_EQ(b.find("tools")->as_array()[0].find("function")->find("name")->as_string(), "f");
ASSERT_EQ(b.find("tool_choice")->as_string(), "none");
XaiService::plan(Call::kChatCreate, {}, &b);
ASSERT_TRUE(ChatRequest("m").add(ChatMessage::user("x")).to_json(false, true).find("deferred")->as_bool());
ASSERT_TRUE(rejects([&] { r.to_json(false, false); })); // include_usage without stream
ASSERT_TRUE(rejects([&] { ChatRequest("m").to_json(); })); // no messages
ASSERT_TRUE(rejects([&] { ChatRequest("m").add(ChatMessage::user("x")).stop({"a", "b", "c", "d", "e"}).to_json(); }));
ASSERT_TRUE(rejects([&] { ChatRequest("m").add(ChatMessage::user("x")).max_tokens_deprecated(1).max_completion_tokens(1).to_json(); }));
ASSERT_TRUE(rejects([&] { ChatRequest("m").add(ChatMessage::user("x")).n(99).to_json(); }));
ASSERT_TRUE(rejects([&] { ChatRequest("m").add(ChatMessage::user("x")).to_json(true, true); }));
ChatToolCall tc{"call_1", "function", "f", "{\"a\":1}", -1};
Json am = ChatMessage::assistant_calls("", {tc}).json();
ASSERT_EQ(am.find("tool_calls")->as_array()[0].find("function")->find("name")->as_string(), "f");
ASSERT_EQ(ChatMessage::tool_result("call_1", "ok").json().find("tool_call_id")->as_string(), "call_1");
}
TEST(xai_core, anthropic_messages_and_legacy_bodies) {
MessagesRequest r("grok-4.3", 256);
r.system("s").user("hi").add("assistant", {MessageBlock::tool_use("tu_1", "f", Json::object())})
.add("user", {MessageBlock::tool_result("tu_1", "ok")}).add_tool("f", "d", Json::object())
.tool_choice_tool("f").user_id("u1").temperature(0.1);
Json b = r.to_json(true);
ASSERT_EQ(b.find("max_tokens")->as_int(), 256);
ASSERT_EQ(b.find("tool_choice")->find("name")->as_string(), "f");
ASSERT_EQ(b.find("metadata")->find("user_id")->as_string(), "u1");
XaiService::plan(Call::kMessagesCreate, {}, &b);
ASSERT_TRUE(rejects([] { MessagesRequest("m", 0).user("x").to_json(); }));
ASSERT_TRUE(rejects([] { MessagesRequest("m", 1).to_json(); }));
ASSERT_TRUE(rejects([] { MessageBlock::image_base64("image/gif", "x"); }));
AnthropicCompleteRequest c;
c.model = "m";
c.prompt = "\n\nHuman: hi\n\nAssistant:";
c.max_tokens_to_sample = 10;
Json cj = c.to_json();
XaiService::plan(Call::kCompleteLegacy, {}, &cj);
TextCompletionRequest t;
t.model = "m";
t.prompt = {"hello"};
t.max_tokens = 5;
Json tb = t.to_json();
ASSERT_EQ(tb.find("prompt")->as_string(), "hello");
XaiService::plan(Call::kCompletionsLegacy, {}, &tb);
}
TEST(xai_core, image_requests) {
ImageGenerationRequest g;
g.prompt = "a cat";
g.options.n = 3;
g.options.aspect_ratio = "16:9";
g.options.resolution = "2k";
g.options.response_format = "b64_json";
g.options.quality = "low";
StorageOptions st;
st.filename = "cat.jpg";
st.expires_after = 3600;
st.public_url = true;
g.options.storage = st;
Json b = g.to_json();
ASSERT_EQ(b.find("n")->as_int(), 3);
ASSERT_EQ(b.find("storage_options")->find("filename")->as_string(), "cat.jpg");
ASSERT_TRUE(b.find("storage_options")->find("public_url")->as_bool());
XaiService::plan(Call::kImagesGenerate, {}, &b);
ASSERT_TRUE(rejects([&] { g.to_json(true); })); // deferred supports url only
g.options.response_format = "url";
ASSERT_TRUE(g.to_json(true).find("deferred")->as_bool());
ImageGenerationRequest bad;
bad.prompt = "x";
bad.options.n = 11;
ASSERT_TRUE(rejects([&] { bad.to_json(); }));
bad.options.n = 1;
bad.options.aspect_ratio = "7:5";
ASSERT_TRUE(rejects([&] { bad.to_json(); }));
bad.options.aspect_ratio = "";
bad.options.upload_urls = {"https://u/1", "https://u/2"};
ASSERT_TRUE(rejects([&] { bad.to_json(); })); // 2 urls for n=1
ImageGenerationRequest empty;
ASSERT_TRUE(rejects([&] { empty.to_json(); }));
ImageEditRequest e;
e.prompt = "add a rainbow";
ASSERT_TRUE(rejects([&] { e.to_json(); })); // no input image
e.image = MediaSource::image_url("https://x.ai/a.png");
e.images = {MediaSource::image_file("file_1")};
ASSERT_TRUE(rejects([&] { e.to_json(); })); // image + images
e.images.clear();
Json eb = e.to_json();
ASSERT_EQ(eb.find("image")->find("url")->as_string(), "https://x.ai/a.png");
XaiService::plan(Call::kImagesEdit, {}, &eb);
e.options.quality = "low"; // documented for generation only
Json eq = e.to_json();
ASSERT_TRUE(rejects([&] { XaiService::plan(Call::kImagesEdit, {}, &eq); }));
}
TEST(xai_core, video_requests) {
VideoGenerationRequest v;
v.prompt = "a rocket";
v.duration = 8;
v.resolution = "720p";
v.aspect_ratio = "16:9";
Json b = v.to_json();
ASSERT_EQ(b.find("duration")->as_int(), 8);
XaiService::plan(Call::kVideosGenerate, {}, &b);
v.duration = 16;
ASSERT_TRUE(rejects([&] { v.to_json(); }));
v.duration = 0;
ASSERT_TRUE(rejects([&] { v.to_json(); }));
v.duration = 10;
v.keyframes = {{MediaSource::image_url("https://x.ai/k.png"), 10.0}};
ASSERT_TRUE(rejects([&] { v.to_json(); })); // timestamp must be inside the clip
v.keyframes = {{MediaSource::image_url("https://x.ai/k.png"), 4.5}};
ASSERT_TRUE(v.to_json().find("keyframes") != nullptr);
v.last_frame = MediaSource::image_url("https://x.ai/l.png");
Json lf = v.to_json();
XaiService::plan(Call::kVideosGenerate, {}, &lf); // last_frame is a documented-only field
VideoGenerationRequest refs;
refs.reference_images.assign(15, MediaSource::image_url("https://x.ai/r.png"));
ASSERT_TRUE(rejects([&] { refs.to_json(); }));
VideoGenerationRequest none;
ASSERT_TRUE(rejects([&] { none.to_json(); }));
VideoEditRequest ed;
ed.prompt = "slow motion";
ASSERT_TRUE(rejects([&] { ed.to_json(); })); // no video
ed.video = MediaSource::video_url("https://x.ai/a.mp4");
Json eb = ed.to_json();
XaiService::plan(Call::kVideosEdit, {}, &eb);
ASSERT_TRUE(rejects([] { MediaSource::video_url("https://x.ai/a.avi"); }));
VideoExtendRequest ex;
ex.prompt = "pan";
ex.video = MediaSource::video_file("file_1");
ex.duration = 6;
Json xb = ex.to_json();
XaiService::plan(Call::kVideosExtend, {}, &xb);
ex.duration = 1;
ASSERT_TRUE(rejects([&] { ex.to_json(); }));
ex.duration = 11;
ASSERT_TRUE(rejects([&] { ex.to_json(); }));
}
TEST(xai_core, embeddings_tokenize_and_base64_floats) {
EmbeddingRequest e;
e.model = "grok-embed";
e.texts = {"query: hi"};
Json b = e.to_json();
ASSERT_EQ(b.find("input")->as_string(), "query: hi");
e.texts = {"a", "b"};
e.dimensions = 256;
e.encoding_format = "base64";
Json b2 = e.to_json();
ASSERT_EQ(b2.find("input")->as_array().size(), 2u);
XaiService::plan(Call::kEmbeddingsCreate, {}, &b2);
e.token_arrays = {{1, 2}};
ASSERT_TRUE(rejects([&] { e.to_json(); }));
e.token_arrays.clear();
e.texts.assign(Caps::kMaxEmbeddingInputs + 1, "x");
ASSERT_TRUE(rejects([&] { e.to_json(); }));
e.texts = {"x"};
e.encoding_format = "hex";
ASSERT_TRUE(rejects([&] { e.to_json(); }));
// 1.0f and 2.0f little-endian
std::vector<float> f = floats_from_base64("AACAPwAAAEA=");
ASSERT_EQ(f.size(), 2u);
ASSERT_TRUE(f[0] == 1.0f && f[1] == 2.0f);
ASSERT_TRUE(floats_from_base64("!!").empty());
Rig r;
TokenizeService t(r.api);
ASSERT_TRUE(rejects([&] { t.tokenize("m", ""); }));
ASSERT_TRUE(rejects([&] { t.tokenize("m", std::string(Caps::kMaxTokenizeBytes + 1, 'x')); }));
}
TEST(xai_core, batch_entries_and_limits) {
Rig r;
BatchService svc(r.api);
ChatRequest cr("grok-4.3");
cr.add(ChatMessage::user("What is 101*3?"));
BatchEntry e = BatchEntry::chat("test_request_0", cr);
ASSERT_EQ(e.json().find("batch_request_id")->as_string(), "test_request_0");
ASSERT_TRUE(e.json().find("batch_request")->find("chat_get_completion") != nullptr);
ResponseRequest rr("grok-4.3");
rr.input_text("hi");
ASSERT_TRUE(BatchEntry::responses("r1", rr).json().find("batch_request")->find("responses") != nullptr);
ASSERT_TRUE(rejects([&] { BatchEntry::chat("bad id", cr); }));
ASSERT_TRUE(rejects([&] { svc.add_requests("batch_1", {}); }));
ASSERT_TRUE(rejects([&] { svc.add_requests("batch_1", {e, e}); })); // duplicate id
ASSERT_TRUE(rejects([&] { svc.add_requests("batch_1:cancel", {e}); }));
ASSERT_TRUE(rejects([&] { svc.create(""); }));
ASSERT_TRUE(rejects([&] { svc.list(0); }));
ASSERT_TRUE(rejects([&] { svc.list(1001); }));
ASSERT_TRUE(not_configured([&] { svc.add_requests("batch_1", {e}); }));
ASSERT_TRUE(not_configured([&] { svc.cancel("batch_1"); }));
ASSERT_EQ(r.mock->send_count(), 0u);
}
// Fixtures below are copied verbatim from docs.x.ai (REST reference: Batches).
TEST(xai_core, fixture_batch_documented_examples) {
Json created = Json::parse(R"({
"batch_id": "batch_1934e8b5-f3dc-45f1-8329-9841b0aee9d8",
"name": "My New Batch",
"create_time": "2025-11-11",
"expire_time": "2025-11-12",
"create_api_key_id": "********-****-****-****-************",
"cancel_time": null,
"cancel_by_xai_message": null,
"state": {"num_requests": 0, "num_pending": 0, "num_success": 0, "num_error": 0, "num_cancelled": 0}
})");
Batch b = Batch::parse(created);
ASSERT_EQ(b.batch_id, "batch_1934e8b5-f3dc-45f1-8329-9841b0aee9d8");
ASSERT_EQ(b.name, "My New Batch");
ASSERT_FALSE(b.finished()); // zero requests is not finished
Json results = Json::parse(R"({
"results": [{"batch_request_id": "test_request_0", "batch_result": {"response": {"chat_get_completion": {
"id": "e7c2162b-ca73-c181-2364-1feabef778fe_us-east-1", "object": "chat.completion", "created": 1762801725,
"model": "grok-4",
"choices": [{"index": 0, "message": {"role": "assistant",
"content": "101 multiplied by 3 is 303. If you have more calculations or questions, feel free to ask!",
"refusal": null}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": 706, "completion_tokens": 22, "total_tokens": 827,
"prompt_tokens_details": {"text_tokens": 706, "audio_tokens": 0, "image_tokens": 0, "cached_tokens": 679},
"completion_tokens_details": {"reasoning_tokens": 99, "audio_tokens": 0, "accepted_prediction_tokens": 0, "rejected_prediction_tokens": 0},
"num_sources_used": 0},
"system_fingerprint": "fp_1944a19e1f"}}}}],
"pagination_token": null
})");
const Json& chat = *results.find("results")->as_array()[0].find("batch_result")->find("response")->find("chat_get_completion");
ChatCompletion c = ChatCompletion::parse(chat);
ASSERT_EQ(c.text(), "101 multiplied by 3 is 303. If you have more calculations or questions, feel free to ask!");
ASSERT_EQ(c.finish_reason(), "stop");
ASSERT_EQ(c.usage.input_tokens, 706);
ASSERT_EQ(c.usage.output_tokens, 22);
ASSERT_EQ(c.usage.cached_tokens, 679);
ASSERT_EQ(c.usage.reasoning_tokens, 99);
ASSERT_EQ(c.usage.total_tokens, 827);
ASSERT_EQ(c.system_fingerprint, "fp_1944a19e1f");
Json meta = Json::parse(R"({"batch_request_metadata": [{"batch_request_id": "test_request_0",
"endpoint": "xai_api.Chat/GetCompletion", "model": "grok-4", "state": "succeeded",
"create_time": "2025-11-11", "finish_time": "2025-11-12"}], "pagination_token": null})");
ASSERT_EQ(meta.find("batch_request_metadata")->as_array()[0].find("state")->as_string(), "succeeded");
}
TEST(xai_core, fixture_usage_and_cost_ticks) {
// docs: "Cost is returned in ticks (1e-10 USD)"
Json u = Json::parse("{\"prompt_tokens\":10,\"completion_tokens\":5,\"total_tokens\":15,\"cost_in_usd_ticks\":250000000,"
"\"server_side_tool_usage_details\":{\"web_search_calls\":2,\"x_search_calls\":1,\"x_posts_fetched\":9}}");
TokenUsage t = TokenUsage::from_chat(u);
ASSERT_EQ(t.cost_usd_ticks, 250000000);
ASSERT_TRUE(t.cost_usd() > 0.0249 && t.cost_usd() < 0.0251);
ASSERT_EQ(t.tools.web_search_calls, 2);
ASSERT_EQ(t.tools.total_calls(), 3);
ASSERT_EQ(t.tools.x_posts_fetched, 9);
TokenUsage m = TokenUsage::from_responses(Json::parse(
"{\"input_tokens\":4,\"output_tokens\":6,\"input_tokens_details\":{\"cached_tokens\":2},"
"\"output_tokens_details\":{\"reasoning_tokens\":3},\"context_details\":{\"input_tokens\":4,\"output_tokens\":6}}"));
ASSERT_EQ(m.total_tokens, 10);
ASSERT_EQ(m.cached_tokens, 2);
ASSERT_EQ(m.reasoning_tokens, 3);
ASSERT_TRUE(m.context_input_tokens.has_value());
ASSERT_FALSE(TokenUsage::from_chat(Json()).present);
}
TEST(xai_core, response_document_parsing) {
Json doc = Json::parse(R"({"id":"resp_1","object":"response","model":"grok-4.3","status":"completed","created_at":1,"store":true,
"output":[
{"type":"reasoning","id":"rs_1","summary":[{"type":"summary_text","text":"thought"}],"encrypted_content":"enc"},
{"type":"web_search_call","id":"ws_1","status":"completed","action":{"type":"search","query":"spacex","sources":[{"type":"url","url":"https://a.com"}]}},
{"type":"custom_tool_call","id":"ct_1","call_id":"xs_123","name":"x_keyword_search","input":"{}"},
{"type":"function_call","id":"fc_1","call_id":"call_9","name":"get_weather","arguments":"{\"city\":\"Oslo\"}","status":"completed"},
{"type":"message","id":"m_1","role":"assistant","status":"completed","content":[
{"type":"output_text","text":"Hello","annotations":[{"type":"url_citation","url":"https://a.com","title":"A","start_index":0,"end_index":5}]}]}],
"usage":{"input_tokens":3,"output_tokens":4,"total_tokens":7,"cost_in_usd_ticks":1000}})");
ModelResponse r = ModelResponse::parse(doc);
ASSERT_TRUE(r.completed());
ASSERT_EQ(r.text(), "Hello");
ASSERT_EQ(r.summary_text(), "thought");
ASSERT_EQ(r.citations().size(), 1u);
ASSERT_EQ(r.citations()[0].url, "https://a.com");
ASSERT_EQ(r.function_calls().size(), 1u);
ASSERT_EQ(r.function_calls()[0].arguments().find("city")->as_string(), "Oslo");
ASSERT_EQ(r.server_tool_calls().size(), 2u);
ASSERT_TRUE(r.server_tool_calls()[0].server_tool() == ServerTool::kWebSearch);
ASSERT_TRUE(r.server_tool_calls()[1].server_tool() == ServerTool::kXSearch);
ASSERT_EQ(r.output()[1].web_search_query(), "spacex");
ASSERT_EQ(r.output()[1].web_search_sources()[0], "https://a.com");
ASSERT_EQ(r.usage().total_tokens, 7);
ASSERT_EQ(r.replay_items().size(), 5u); // reasoning with encrypted content, calls, message
ASSERT_EQ(item_kind_of("mcp_call") == ItemKind::kMcpCall, true);
Json failed = Json::parse("{\"id\":\"r\",\"status\":\"failed\",\"error\":{\"code\":\"x\",\"message\":\"boom\"}}");
ASSERT_TRUE(ModelResponse::parse(failed).failed());
ASSERT_EQ(ModelResponse::parse(failed).error()->message, "boom");
Json inc = Json::parse("{\"id\":\"r\",\"status\":\"incomplete\",\"incomplete_details\":{\"reason\":\"max_output_tokens\"}}");
ASSERT_EQ(ModelResponse::parse(inc).incomplete_reason(), "max_output_tokens");
}
TEST(xai_core, sse_transcript_into_response_events) {
// A stream shaped like the guides' loops: created, text deltas, item done, completed.
std::string wire =
"data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_1\",\"status\":\"in_progress\"}}\n\n"
"data: {\"type\":\"response.output_item.added\",\"output_index\":0,\"item\":{\"type\":\"function_call\",\"id\":\"fc_1\",\"call_id\":\"call_1\",\"name\":\"f\"}}\n\n"
"data: {\"type\":\"response.function_call_arguments.delta\",\"item_id\":\"fc_1\",\"delta\":\"{\\\"a\\\":\"}\n\n"
"data: {\"type\":\"response.function_call_arguments.delta\",\"item_id\":\"fc_1\",\"delta\":\"1}\"}\n\n"
"data: {\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"think\"}\n\n"
"data: {\"type\":\"response.web_search_call.searching\",\"item_id\":\"ws_1\"}\n\n"
"data: {\"type\":\"response.output_text.delta\",\"delta\":\"Hel\"}\n\n"
"data: {\"type\":\"response.output_text.delta\",\"delta\":\"lo\"}\n\n"
"data: {\"type\":\"response.image_generation_call.generating\",\"item_id\":\"ig_1\"}\n\n"
"data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"image_generation_call\",\"id\":\"ig_1\",\"result\":\"QUJD\",\"status\":\"completed\"}}\n\n"
"data: {\"type\":\"response.some.future_event\",\"x\":1}\n\n"
"data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"status\":\"completed\",\"output\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":2,\"total_tokens\":3}}}\n\n"
"data: [DONE]\n\n";
ResponseAccumulator acc;
long unknown = 0;
molo::llm::SseDecoder dec("xai", [&](const molo::llm::SseEvent& ev) {
ResponseEvent e = ResponseEvent::parse(ev);
if (e.kind == EventKind::kUnknown) ++unknown;
return acc.feed(e);
});
dec.feed(wire);
dec.finish();
ASSERT_EQ(acc.text(), "Hello");
ASSERT_EQ(acc.summary(), "think");
ASSERT_EQ(acc.arguments().at("fc_1"), "{\"a\":1}");
ASSERT_EQ(acc.tool_status().at("ws_1"), "searching");
ASSERT_EQ(acc.tool_status().at("ig_1"), "generating");
ASSERT_EQ(acc.images().size(), 1u);
ASSERT_EQ(unknown, 1);
ASSERT_TRUE(acc.final_response().has_value());
ASSERT_EQ(acc.final_response()->usage().total_tokens, 3);
ASSERT_FALSE(acc.failed());
ASSERT_EQ(std::string(event_type_of(EventKind::kOutputTextDelta)), "response.output_text.delta");
ASSERT_TRUE(event_kind_of("response.text.delta") == EventKind::kOutputTextDelta);
}
TEST(xai_core, stream_failure_and_caps) {
ResponseAccumulator acc;
molo::llm::SseEvent err;
err.data = Json::parse("{\"type\":\"error\",\"message\":\"overloaded\"}");
ASSERT_FALSE(acc.feed(ResponseEvent::parse(err)));
ASSERT_TRUE(acc.failed());
ASSERT_EQ(acc.error_message(), "overloaded");
ResponseAccumulator big;
molo::llm::SseEvent d;
d.data = Json::parse("{\"type\":\"response.output_text.delta\",\"delta\":\"" + std::string(1024 * 1024, 'x') + "\"}");
bool stopped = false;
for (int i = 0; i < 40 && !stopped; ++i) stopped = !big.feed(ResponseEvent::parse(d));
ASSERT_TRUE(stopped);
ASSERT_TRUE(big.stopped());
}
TEST(xai_core, chat_chunks_fold_into_a_completion) {
ChatStreamAccumulator acc;
acc.feed(CompletionChunk::parse(Json::parse("{\"id\":\"c1\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"reasoning_content\":\"hm\"}}]}")));
acc.feed(CompletionChunk::parse(Json::parse("{\"id\":\"c1\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hi \"}}]}")));
acc.feed(CompletionChunk::parse(Json::parse("{\"id\":\"c1\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"there\"}}]}")));
acc.feed(CompletionChunk::parse(Json::parse("{\"id\":\"c1\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"f\",\"arguments\":\"{\\\"a\\\"\"}}]}}]}")));
acc.feed(CompletionChunk::parse(Json::parse("{\"id\":\"c1\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\":1}\"}}]},\"finish_reason\":\"tool_calls\"}]}")));
acc.feed(CompletionChunk::parse(Json::parse("{\"id\":\"c1\",\"choices\":[],\"usage\":{\"prompt_tokens\":2,\"completion_tokens\":3,\"total_tokens\":5,\"cost_in_usd_ticks\":9}}")));
ChatCompletion c = acc.result();
ASSERT_EQ(c.text(), "Hi there");
ASSERT_EQ(c.reasoning(), "hm");
ASSERT_EQ(c.finish_reason(), "tool_calls");
ASSERT_EQ(c.function_calls().size(), 1u);
ASSERT_EQ(c.function_calls()[0].arguments_json().find("a")->as_int(), 1);
ASSERT_EQ(c.usage.total_tokens, 5);
ASSERT_EQ(c.usage.cost_usd_ticks, 9);
}
TEST(xai_core, polling_is_bounded_and_deterministic) {
WaitPolicy p;
p.interval_ms = 1000;
p.timeout_ms = 5000;
ASSERT_EQ(p.max_polls(), 6);
int calls = 0, slept = 0;
std::function<std::optional<int>()> never = [&]() -> std::optional<int> { ++calls; return std::nullopt; };
auto r = poll_until<int>(p, never, [&](int ms) { slept += ms; });
ASSERT_FALSE(r.has_value());
ASSERT_EQ(calls, 6);
ASSERT_EQ(slept, 5000);
calls = 0;
std::function<std::optional<int>()> third = [&]() -> std::optional<int> { return ++calls == 3 ? std::optional<int>(7) : std::nullopt; };
auto ok = poll_until<int>(p, third, [](int) {});
ASSERT_TRUE(ok && *ok == 7);
WaitPolicy fast;
fast.interval_ms = 10;
ASSERT_TRUE(rejects([&] { fast.validate(); }));
WaitPolicy slow;
slow.timeout_ms = Caps::kMaxWaitMs + 1;
ASSERT_TRUE(rejects([&] { slow.validate(); }));
Rig rig;
VideoService v(rig.api);
ASSERT_TRUE(rejects([&] { v.wait("a/b"); }));
ASSERT_TRUE(not_configured([&] { v.poll("req_1"); }));
}
TEST(xai_core, deferred_documents_parse) {
// docs: GET /v1/images/{id}: 202 while pending, 200 with data when done or error when failed.
DeferredImage pending = DeferredImage::parse(Json::object(), "r1", 202);
ASSERT_TRUE(pending.pending());
DeferredImage done = DeferredImage::parse(Json::parse("{\"request_id\":\"r1\",\"status\":\"done\",\"data\":[{\"url\":\"https://u/1.jpg\",\"mime_type\":\"image/jpeg\"}],\"usage\":{\"cost_in_usd_ticks\":400000000}}"), "r1", 200);
ASSERT_TRUE(done.done());
ASSERT_EQ(done.result->data[0].url, "https://u/1.jpg");
ASSERT_EQ(done.result->usage.cost_usd_ticks, 400000000);
DeferredImage failed = DeferredImage::parse(Json::parse("{\"request_id\":\"r1\",\"status\":\"failed\",\"error\":{\"code\":\"invalid_argument\",\"message\":\"no\"}}"), "r1", 200);
ASSERT_TRUE(failed.failed());
ASSERT_EQ(failed.error->code, "invalid_argument");
VideoJob j = VideoJob::parse(Json::parse("{\"status\":\"done\",\"model\":\"grok-imagine-video\",\"progress\":100,\"video\":{\"url\":\"https://u/v.mp4\",\"duration\":8,\"respect_moderation\":true}}"), "r2");
ASSERT_TRUE(j.done());
ASSERT_EQ(j.video->duration, 8);
ASSERT_TRUE(j.video->respect_moderation);
ASSERT_TRUE(VideoJob::parse(Json::parse("{\"status\":\"failed\",\"error\":{\"code\":\"internal_error\",\"message\":\"x\"}}"), "r3").failed());
ASSERT_TRUE(VideoJob::parse(Json::object(), "r4").pending());
}
TEST(xai_core, models_parse_pricing_and_capabilities) {
Json lang = Json::parse("{\"id\":\"grok-4.7\",\"object\":\"model\",\"owned_by\":\"xai\",\"created\":1,\"version\":\"1\",\"fingerprint\":\"fp\","
"\"aliases\":[\"grok-4.7-latest\"],\"input_modalities\":[\"text\",\"image\"],\"output_modalities\":[\"text\"],"
"\"prompt_text_token_price\":200000000,\"completion_text_token_price\":600000000,\"search_price\":5,"
"\"capabilities\":{\"reasoning_effort\":[\"low\",\"high\"],\"default_reasoning_effort\":\"high\"}}");
ModelInfo m = ModelInfo::parse(ModelKind::kLanguage, lang);
ASSERT_EQ(m.id, "grok-4.7");
ASSERT_EQ(m.prices.at("prompt_text_token_price"), 200000000);
ASSERT_TRUE(m.accepts_effort("low"));
ASSERT_FALSE(m.accepts_effort("xhigh"));
ASSERT_EQ(m.default_reasoning_effort, "high");
ModelInfo img = ModelInfo::parse(ModelKind::kImage, Json::parse("{\"id\":\"grok-imagine-image\",\"max_prompt_length\":4000,\"pricing\":[{\"quality\":\"low\",\"resolution\":\"1k\",\"price_per_image\":2}]}"));
ASSERT_EQ(img.image_pricing.size(), 1u);
ASSERT_EQ(*img.max_prompt_length, 4000);
ApiKeyInfo k = ApiKeyInfo::parse(Json::parse("{\"api_key_id\":\"k1\",\"redacted_api_key\":\"xai-****\",\"api_key_blocked\":true,\"acls\":[\"api-key:endpoint:*\"]}"));
ASSERT_FALSE(k.usable());
ASSERT_EQ(k.acls.size(), 1u);
MeInfo me = MeInfo::parse(Json::parse("{\"team_id\":\"t\",\"user_id\":\"u\",\"zdr_status\":\"no_zdr\",\"api_key\":{\"api_key_id\":\"k\"},\"team_blocked\":false}"));
ASSERT_TRUE(me.has_api_key);
ASSERT_FALSE(me.has_oauth);
}
TEST(xai_core, chat_model_wire_mapping) {
Rig r;
XaiChatModel responses(r.api, "grok-4.3");
molo::llm::ChatRequest q;
q.system = "be brief";
q.messages.push_back(molo::llm::Message::user("hi"));
molo::llm::Message a;
a.role = molo::llm::Role::kAssistant;
molo::llm::ToolCall tc;
tc.id = "call_1";
tc.name = "f";
tc.arguments = Json::parse("{\"x\":1}");
a.parts.push_back(molo::llm::ContentPart::of_call(tc));
q.messages.push_back(a);
molo::llm::Message tr;
tr.role = molo::llm::Role::kTool;
tr.parts.push_back(molo::llm::ContentPart::of_result({"call_1", "done", false}));
q.messages.push_back(tr);
q.tools.push_back({"f", "d", Json::parse("{\"type\":\"object\"}")});
q.tool_choice = "required";
q.max_tokens = 64;
q.reasoning_effort = "low";
q.response_schema = Json::parse("{\"type\":\"object\"}");
Json b = responses.responses_body(q, false);
ASSERT_EQ(b.find("model")->as_string(), "grok-4.3");
ASSERT_FALSE(b.find("store")->as_bool());
ASSERT_EQ(b.find("instructions")->as_string(), "be brief");
ASSERT_EQ(b.find("max_output_tokens")->as_int(), 64);
ASSERT_EQ(b.find("tool_choice")->as_string(), "required");
ASSERT_EQ(b.find("input")->as_array().size(), 3u);
ASSERT_EQ(b.find("input")->as_array()[1].find("type")->as_string(), "function_call");
ASSERT_EQ(b.find("input")->as_array()[2].find("type")->as_string(), "function_call_output");
ASSERT_EQ(b.find("text")->find("format")->find("type")->as_string(), "json_schema");
XaiService::plan(Call::kResponsesCreate, {}, &b);
q.stop = {"END"};
ASSERT_TRUE(rejects([&] { responses.responses_body(q, false); })); // loud, not silent
XaiChatModel chat(r.api, "grok-4.3", XaiChatModel::Wire::kChatCompletions);
Json cb = chat.chat_body(q, true);
ASSERT_EQ(cb.find("messages")->as_array()[0].find("role")->as_string(), "system");
ASSERT_EQ(cb.find("messages")->as_array()[2].find("tool_calls")->as_array()[0].find("id")->as_string(), "call_1");
ASSERT_EQ(cb.find("messages")->as_array()[3].find("role")->as_string(), "tool");
ASSERT_TRUE(cb.find("stream_options")->find("include_usage")->as_bool());
ASSERT_EQ(cb.find("stop")->as_array()[0].as_string(), "END");
XaiService::plan(Call::kChatCreate, {}, &cb);
q.extra = Json::parse("{\"surprise\":1}");
q.stop.clear();
Json eb = responses.responses_body(q, false);
ASSERT_TRUE(rejects([&] { XaiService::plan(Call::kResponsesCreate, {}, &eb); }));
ASSERT_EQ(responses.vendor(), "xai");
ASSERT_TRUE(not_configured([&] {
molo::llm::ChatRequest ok;
ok.messages.push_back(molo::llm::Message::user("hi"));
responses.generate(ok);
}));
ASSERT_EQ(r.mock->send_count(), 0u);
}
TEST(xai_core, skills_and_account_ids) {
Rig r;
SkillsService s(r.api);
ASSERT_TRUE(rejects([&] { s.get("a/b"); }));
ASSERT_TRUE(rejects([&] { s.list(0); }));
ASSERT_TRUE(rejects([&] { s.list(std::nullopt, "", "sideways"); }));
ASSERT_TRUE(rejects([&] { s.upload_zip("", "zip"); }));
ASSERT_TRUE(not_configured([&] { s.remove("skill_1"); }));
ASSERT_EQ(r.mock->send_count(), 0u);
}
// ---- Opt-in live tests (MOLO_LIVE_NET=1 and XAI_API_KEY) ---------------------
namespace {
bool live() {
const char* net = std::getenv("MOLO_LIVE_NET");
return net && std::string(net) == "1" && !molo::llm::Config::get("XAI_API_KEY").empty();
}
bool paid() {
const char* p = std::getenv("MOLO_LIVE_PAID");
return live() && p && std::string(p) == "1";
}
} // namespace
TEST(xai_core_live, read_only_account_and_models) {
if (!live()) {
std::cout << "SKIP xai_core_live.read_only_account_and_models (set MOLO_LIVE_NET=1 and XAI_API_KEY)\n";
return;
}
auto c = XaiClient::from_env();
ApiKeyInfo k = c->account.api_key();
std::cout << " key " << k.redacted_api_key << " usable=" << k.usable() << "\n";
ASSERT_TRUE(k.usable());
ModelList all = c->models.list();
ModelList lang = c->models.list(ModelKind::kLanguage);
std::cout << " models: " << all.models.size() << ", language: " << lang.models.size() << "\n";
ASSERT_TRUE(!all.models.empty());
ASSERT_TRUE(!lang.models.empty());
c->models.list(ModelKind::kImage);
c->models.list(ModelKind::kVideo);
c->models.list(ModelKind::kEmbedding);
c->batches.list(5);
}
TEST(xai_core_live, one_smallest_completion) {
if (!live()) {
std::cout << "SKIP xai_core_live.one_smallest_completion (set MOLO_LIVE_NET=1 and XAI_API_KEY)\n";
return;
}
auto c = XaiClient::from_env();
ModelList lang = c->models.list(ModelKind::kLanguage);
ASSERT_TRUE(!lang.models.empty());
ResponseRequest req(lang.models.front().id);
req.input_text("Reply with the single word: ok").max_output_tokens(64).store(false);
ModelResponse r = c->responses.create(req);
std::cout << " status " << r.status() << " ticks " << r.usage().cost_usd_ticks << "\n";
ASSERT_TRUE(r.completed() || r.incomplete());
}
TEST(xai_core_live, paid_image_generation) {
if (!paid()) {
std::cout << "SKIP xai_core_live.paid_image_generation (set MOLO_LIVE_NET=1, XAI_API_KEY and MOLO_LIVE_PAID=1)\n";
return;
}
auto c = XaiClient::from_env();
ImageGenerationRequest g;
g.prompt = "a small red circle on white";
ImageResult r = c->images.generate(g);
std::cout << " images " << r.data.size() << " ticks " << r.usage.cost_usd_ticks << "\n";
ASSERT_EQ(r.data.size(), 1u);
}