Files

82 lines
5.4 KiB
C++
Raw Permalink Normal View History

// Local routes (no key, no network): POST /ai/xai/platform/realtime/session/preview
// and /ai/xai/platform/webhook/verify, plus body and limit cases.
#include <cstdlib>
#include "support/Assert.hpp"
#include "xai/platform/webhook.h"
using namespace molo::example::test;
int main() {
unsetenv("XAI_API_KEY");
unsetenv("XAI_MANAGEMENT_KEY");
Fixture f;
auto pre = f.client->post("/ai/xai/platform/realtime/session/preview",
"{\"voice\":\"eve\",\"turn_detection\":\"server_vad\",\"instructions\":\"Be brief.\","
"\"input_type\":\"audio/pcm\",\"input_rate\":16000,"
"\"tools\":[{\"type\":\"web_search\"}]}",
"application/json");
check(pre.status == 200 && contains(pre.body, "\"type\":\"session.update\""), "preview builds session.update");
check(contains(pre.body, "\"rate\":16000") && contains(pre.body, "\"type\":\"server_vad\""), "preview carries audio and turns");
auto manual = f.client->post("/ai/xai/platform/realtime/session/preview", "{\"turn_detection\":\"manual\"}", "application/json");
check(manual.status == 200 && contains(manual.body, "\"type\":null"), "manual turns send a null type");
for (const char* bad : {"{\"model\":\"gpt-realtime\"}", "{\"input_type\":\"audio/mp3\"}", "{\"output_speed\":3}",
"{\"turn_detection\":\"loud\"}", "{\"language_hint\":\"es\"}", "{\"tools\":[{\"type\":\"nope\"}]}",
"{\"keyterms\":[1]}", "{\"idle_timeout_ms\":\"soon\"}"})
check(f.client->post("/ai/xai/platform/realtime/session/preview", bad, "application/json").status == 422,
std::string("preview refuses ") + bad);
// Webhook verification with a locally signed SIP event (shape from the docs).
const std::string secret = "whsec_MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY=";
const std::string event =
"{\"object\":\"event\",\"id\":\"evt_123\",\"type\":\"realtime.call.incoming\",\"created_at\":1750000000,"
"\"data\":{\"call_id\":\"00000000-0000-0000-0000-000000000000\",\"metadata\":{}}}";
molo::xai::platform::WebhookVerifier v(secret);
molo::xai::platform::WebhookHeaders h{"msg_1", "1750000000", ""};
h.signature = v.sign(h, event);
auto esc = [](const std::string& s) {
std::string o;
for (char c : s) {
if (c == '"' || c == '\\') o.push_back('\\');
o.push_back(c);
}
return o;
};
auto req = [&](const std::string& sig, const std::string& body, long now) {
return "{\"secret\":\"" + secret + "\",\"webhook_id\":\"msg_1\",\"webhook_timestamp\":\"1750000000\","
"\"webhook_signature\":\"" + sig + "\",\"body\":\"" + esc(body) + "\",\"now\":" + std::to_string(now) + "}";
};
auto ok = f.client->post("/ai/xai/platform/webhook/verify", req(h.signature, event, 1750000010), "application/json");
check(ok.status == 200 && contains(ok.body, "\"valid\":true") &&
contains(ok.body, "call_id=00000000-0000-0000-0000-000000000000"),
"valid signature opens the call");
auto tamper = f.client->post("/ai/xai/platform/webhook/verify", req(h.signature, event + " ", 1750000010), "application/json");
check(tamper.status == 502 || tamper.status == 422, "tampered body is refused (loud)");
auto old = f.client->post("/ai/xai/platform/webhook/verify", req(h.signature, event, 1750001000), "application/json");
check(old.status != 200, "stale timestamp is refused");
check(f.client->post("/ai/xai/platform/webhook/verify", "{}", "application/json").status == 422, "missing fields is 422");
// Limits and bodies.
std::string big(5 * 1024 * 1024, 'a');
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"upload\",\"filename\":\"a\",\"data_base64\":\"" + big + "\"}",
"application/json").status == 413, "body over the cap is 413");
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"upload\",\"filename\":\"a.txt\",\"data_base64\":\"!!!\"}",
"application/json").status == 422, "bad base64 is 422");
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"upload\",\"filename\":\"a/b\",\"data_base64\":\"QUJD\"}",
"application/json").status == 422, "path in filename is 422");
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"list\",\"limit\":5000}", "application/json").status == 422,
"limit over the cap is 422");
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"list_all\",\"max_pages\":1000}", "application/json").status == 422,
"page walk over the cap is 422");
check(f.client->post("/ai/xai/platform/tts", "{\"text\":123}", "application/json").status == 422, "wrong type is 422");
check(f.client->post("/ai/xai/platform/tts", "{\"text\":\"" + std::string(60001, 'a') + "\"}", "application/json").status == 422,
"tts text over 60000 characters is 422");
check(f.client->post("/ai/xai/platform/stt", "{\"keyterms\":[\"a\"],\"url\":\"ftp://x\"}", "application/json").status == 422,
"stt url scheme is checked");
check(f.client->post("/ai/xai/platform/collections", "{\"action\":\"batch_get\",\"collection_id\":\"c\",\"file_ids\":[]}",
"application/json").status == 422, "empty batch is 422");
check(f.client->post("/ai/xai/platform/search", "{\"query\":\"q\",\"collection_ids\":[\"c\"],\"retrieval_mode\":\"fuzzy\"}",
"application/json").status == 422, "unknown retrieval mode is 422");
return finish("ai/xai/platform/local");
}