// Routes: POST /ai/compat/* (405, 422, dry runs, 503 without a credential) #include #include "support/Assert.hpp" #include "vendors/compat/registry.h" using namespace molo::example::test; namespace cp = molo::compat; int main() { setenv("MOLO_ENV_FILES", "off", 1); for (std::size_t i = 0; i < cp::Registry::count(); ++i) { const cp::ProviderRow& r = cp::Registry::at(i); unsetenv(r.key_env); if (r.base_env[0]) unsetenv(r.base_env); if (r.extra_env[0]) unsetenv(r.extra_env); } Fixture f; auto post = [&](const std::string& route, const std::string& body) { return f.client->post("/ai/compat/" + route, body, "application/json"); }; const char* all[] = {"chat", "models", "embeddings", "images", "transcribe", "speech"}; for (const char* r : all) { std::string route = std::string("/ai/compat/") + r; check(f.client->get(route).status == 405, route + " get is 405"); check(post(r, "not json").status == 422, route + " bad json is 422"); check(post(r, "[1]").status == 422, route + " non-object is 422"); check(post(r, "{}").status == 422, route + " without provider is 422"); check(post(r, "{\"provider\":\"nope\"}").status == 422, route + " unknown provider is 422"); check(post(r, "{\"provider\":5}").status == 422, route + " non-string provider is 422"); } // 422: input rules run before anything else check(post("chat", "{\"provider\":\"groq\"}").status == 422, "chat needs prompt"); check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\"}").status == 422, "chat needs a model"); check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"m\",\"max_tokens\":-1}").status == 422, "max_tokens -1"); check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"m\",\"extra\":{\"logprobs\":true}}").status == 422, "groq quirk logprobs is 422"); check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"m\",\"extra\":{\"n\":2}}").status == 422, "groq quirk n is 422"); check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"m\",\"temperature\":3}").status == 422, "temperature 3"); check(post("models", "{\"provider\":\"dashscope\"}").status == 422, "row without a list endpoint is 422"); check(post("embeddings", "{\"provider\":\"cerebras\",\"model\":\"m\",\"input\":\"x\"}").status == 422, "cerebras has no embeddings"); check(post("embeddings", "{\"provider\":\"nvidia\",\"model\":\"m\"}").status == 422, "embeddings need input"); check(post("embeddings", "{\"provider\":\"nvidia\",\"model\":\"m\",\"input\":[1]}").status == 422, "input must be strings"); check(post("images", "{\"provider\":\"together\",\"model\":\"m\",\"prompt\":\"x\",\"n\":11}").status == 422, "n over 10"); check(post("images", "{\"provider\":\"groq\",\"model\":\"m\",\"prompt\":\"x\"}").status == 422, "groq has no images"); check(post("transcribe", "{\"provider\":\"groq\",\"model\":\"m\",\"filename\":\"a.wav\",\"data_base64\":\"@@\"}").status == 422, "bad base64"); check(post("transcribe", "{\"provider\":\"cerebras\",\"model\":\"m\",\"filename\":\"a.wav\",\"data_base64\":\"aGk=\"}").status == 422, "cerebras has no transcription"); check(post("speech", "{\"provider\":\"together\",\"model\":\"m\",\"input\":\"x\"}").status == 422, "speech needs a voice"); check(post("speech", "{\"provider\":\"together\",\"model\":\"m\",\"input\":\"x\",\"voice\":\"v\",\"format\":\"ogg\"}").status == 422, "bad format"); // dry runs: paid routes show the exact request and the URL auto dry = post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"llama\",\"max_tokens\":8}"); check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "\"model\":\"llama\"") && contains(dry.body, "https://api.groq.com/openai/v1/chat/completions"), "chat is a dry run without confirm"); check(contains(post("embeddings", "{\"provider\":\"nvidia\",\"model\":\"m\",\"input\":\"x\"}").body, "/embeddings"), "embeddings dry run"); check(contains(post("images", "{\"provider\":\"together\",\"model\":\"m\",\"prompt\":\"x\"}").body, "/images/generations"), "images dry run"); check(contains(post("transcribe", "{\"provider\":\"groq\",\"model\":\"m\",\"filename\":\"a.wav\",\"data_base64\":\"aGk=\"}").body, "\"bytes\":2"), "transcribe dry run"); check(contains(post("speech", "{\"provider\":\"together\",\"model\":\"m\",\"input\":\"x\",\"voice\":\"v\"}").body, "/audio/speech"), "speech dry run"); check(contains(post("chat", "{\"provider\":\"ollama\",\"prompt\":\"hi\",\"model\":\"m\"}").body, "http://localhost:11434/v1/chat/completions"), "local provider dry run shows its default URL"); // 503: the free route and confirmed routes need the provider's credential auto models = post("models", "{\"provider\":\"groq\"}"); check(models.status == 503 && contains(models.body, "compat_not_configured") && contains(models.body, "GROQ_API_KEY"), "models without key is 503 and names the env"); check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"m\",\"confirm\":true}").status == 503, "confirmed chat without key is 503"); check(post("embeddings", "{\"provider\":\"nvidia\",\"model\":\"m\",\"input\":\"x\",\"confirm\":true}").status == 503, "confirmed embeddings without key is 503"); check(post("images", "{\"provider\":\"together\",\"model\":\"m\",\"prompt\":\"x\",\"confirm\":true}").status == 503, "confirmed images without key is 503"); check(post("transcribe", "{\"provider\":\"groq\",\"model\":\"m\",\"filename\":\"a.wav\",\"data_base64\":\"aGk=\",\"confirm\":true}").status == 503, "confirmed transcribe without key is 503"); check(post("speech", "{\"provider\":\"together\",\"model\":\"m\",\"input\":\"x\",\"voice\":\"v\",\"confirm\":true}").status == 503, "confirmed speech without key is 503"); auto local = post("models", "{\"provider\":\"ollama\"}"); check(local.status == 503 && contains(local.body, "OLLAMA_BASE_URL"), "local provider without base env is 503"); auto cf = post("chat", "{\"provider\":\"cloudflare\",\"prompt\":\"hi\",\"model\":\"m\",\"confirm\":true}"); check(cf.status == 503, "cloudflare without account is 503"); return finish("ai/compat/routes"); }