Merge openai core package
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
// Routes: POST /ai/openai/{responses,responses/get,responses/input-items,responses/tokens,
|
||||
// chat,chat/stored,completions,embeddings,moderations,models,videos/get}
|
||||
// Well formed input without a credential is 503 not_configured; bad input is 422
|
||||
// (validated first, never reaching OpenAI).
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
namespace {
|
||||
struct Route {
|
||||
const char* path;
|
||||
const char* valid;
|
||||
const char* bad;
|
||||
};
|
||||
const Route kRoutes[] = {
|
||||
{"/ai/openai/responses", R"({"input":"hi"})", R"({})"},
|
||||
{"/ai/openai/responses/get", R"({"id":"resp_1"})", R"({"id":"../x"})"},
|
||||
{"/ai/openai/responses/input-items", R"({"id":"resp_1"})", R"({})"},
|
||||
{"/ai/openai/responses/tokens", R"({"input":"hi"})", R"({"input":5})"},
|
||||
{"/ai/openai/chat", R"({"prompt":"hi"})", R"({"prompt":"hi","max_tokens":0})"},
|
||||
{"/ai/openai/chat/stored", R"({"action":"list"})", R"({"action":"nuke"})"},
|
||||
{"/ai/openai/completions", R"({"prompt":"hi"})", R"({"prompt":"hi","max_tokens":99999})"},
|
||||
{"/ai/openai/embeddings", R"({"input":"hi"})", R"({"input":"hi","dimensions":0})"},
|
||||
{"/ai/openai/moderations", R"({"input":"hi"})", R"({})"},
|
||||
{"/ai/openai/models", R"({})", R"({"id":"a/b"})"},
|
||||
{"/ai/openai/videos/get", R"({"id":"video_1"})", R"({"id":"a b"})"},
|
||||
};
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
unsetenv("OPENAI_API_KEY");
|
||||
setenv("MOLO_ENV_FILES", "off", 1);
|
||||
Fixture f;
|
||||
for (const Route& r : kRoutes) {
|
||||
auto ok = f.client->post(r.path, r.valid, "application/json");
|
||||
check(ok.status == 503 && contains(ok.body, "not_configured"), std::string("valid is 503: ") + r.path);
|
||||
auto bad = f.client->post(r.path, r.bad, "application/json");
|
||||
check(bad.status == 422 && contains(bad.body, "validation_error"), std::string("bad input is 422: ") + r.path);
|
||||
check(f.client->post(r.path, "[1]", "application/json").status == 422, std::string("array body is 422: ") + r.path);
|
||||
check(f.client->post(r.path, "{nope", "application/json").status == 422, std::string("broken json is 422: ") + r.path);
|
||||
}
|
||||
check(f.client->post("/ai/openai/responses", R"({"input":"hi","reasoning_effort":"extreme"})", "application/json").status == 422,
|
||||
"unknown reasoning effort is 422");
|
||||
check(f.client->post("/ai/openai/responses", R"({"input":"hi","max_output_tokens":4})", "application/json").status == 422,
|
||||
"max_output_tokens under 16 is 422");
|
||||
check(f.client->post("/ai/openai/responses", R"({"input":"hi","json_schema":{"type":"object","properties":{"a":{"type":"string"}}}})",
|
||||
"application/json").status == 422,
|
||||
"non-strict schema is 422");
|
||||
check(f.client->post("/ai/openai/responses", std::string("{\"input\":\"") + std::string(10485761, 'x') + "\"}", "application/json").status == 413,
|
||||
"body over the cap is 413");
|
||||
return finish("ai/openai/calls");
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Routes: POST /ai/openai/{images,speech,transcribe,videos,responses/cancel,responses/delete,
|
||||
// chat/delete,models/delete,videos/delete} (paid or destructive: dry run unless confirm:true)
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
namespace {
|
||||
struct Route {
|
||||
const char* path;
|
||||
const char* valid;
|
||||
const char* bad;
|
||||
};
|
||||
const Route kRoutes[] = {
|
||||
{"/ai/openai/images", R"({"prompt":"a cat"})", R"({"prompt":"a cat","n":99})"},
|
||||
{"/ai/openai/speech", R"({"input":"hello"})", R"({"input":"hello","voice":"nobody"})"},
|
||||
{"/ai/openai/transcribe", R"({"audio_b64":"QVVESU8="})", R"({"audio_b64":"!!!"})"},
|
||||
{"/ai/openai/videos", R"({"prompt":"a fox"})", R"({"prompt":"a fox","seconds":"5"})"},
|
||||
{"/ai/openai/responses/cancel", R"({"id":"resp_1"})", R"({})"},
|
||||
{"/ai/openai/responses/delete", R"({"id":"resp_1"})", R"({"id":"a/b"})"},
|
||||
{"/ai/openai/chat/delete", R"({"id":"chatcmpl_1"})", R"({})"},
|
||||
{"/ai/openai/models/delete", R"({"id":"ft:gpt-4o:org::abc"})", R"({"id":"gpt-4o"})"},
|
||||
{"/ai/openai/videos/delete", R"({"id":"video_1"})", R"({})"},
|
||||
};
|
||||
|
||||
std::string with_confirm(const std::string& body) {
|
||||
return body.substr(0, body.size() - 1) + ",\"confirm\":true}";
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
unsetenv("OPENAI_API_KEY");
|
||||
setenv("MOLO_ENV_FILES", "off", 1);
|
||||
Fixture f;
|
||||
for (const Route& r : kRoutes) {
|
||||
auto dry = f.client->post(r.path, r.valid, "application/json");
|
||||
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true"), std::string("default is a dry run: ") + r.path);
|
||||
auto real = f.client->post(r.path, with_confirm(r.valid), "application/json");
|
||||
check(real.status == 503 && contains(real.body, "not_configured"), std::string("confirmed without key is 503: ") + r.path);
|
||||
auto bad = f.client->post(r.path, r.bad, "application/json");
|
||||
check(bad.status == 422, std::string("bad input is 422 even for a dry run: ") + r.path);
|
||||
std::string v = r.valid;
|
||||
check(f.client->post(r.path, v.substr(0, v.size() - 1) + ",\"confirm\":\"yes\"}", "application/json").status == 422,
|
||||
std::string("non-bool confirm is 422: ") + r.path);
|
||||
}
|
||||
auto dry = f.client->post("/ai/openai/images", R"({"prompt":"a cat","n":2,"size":"1024x1024"})", "application/json");
|
||||
check(contains(dry.body, "\"n\":2") && contains(dry.body, "gpt-image-1-mini"), "dry run shows the request that would be sent");
|
||||
return finish("ai/openai/gated");
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Routes: GET /ai/openai, /ai/openai/status, /ai/openai/calls (no credential needed)
|
||||
// Verb checks: every POST route answers 405 to GET, GET pages answer 405 to POST.
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
unsetenv("OPENAI_API_KEY");
|
||||
setenv("MOLO_ENV_FILES", "off", 1);
|
||||
Fixture f;
|
||||
auto page = f.client->get("/ai/openai");
|
||||
check(page.status == 200 && contains(page.headers.get("Content-Type"), "text/html"), "page is html 200");
|
||||
check(contains(page.body, "ai_openai_core.js"), "page loads its script");
|
||||
auto status = f.client->get("/ai/openai/status");
|
||||
check(status.status == 200 && contains(status.body, "\"configured\":false"), "status without key");
|
||||
check(contains(status.body, "https://api.openai.com/v1") && contains(status.body, "\"calls\":44"),
|
||||
"status shows base url and call count");
|
||||
auto calls = f.client->get("/ai/openai/calls");
|
||||
check(calls.status == 200 && contains(calls.body, "createResponse") && contains(calls.body, "/responses/{response_id}/cancel"),
|
||||
"call table listed");
|
||||
check(f.client->post("/ai/openai").status == 405, "post to page is 405");
|
||||
check(f.client->post("/ai/openai/status").status == 405, "post to status is 405");
|
||||
const char* posts[] = {"responses", "responses/get", "responses/input-items", "responses/tokens",
|
||||
"responses/cancel", "responses/delete", "chat", "chat/stored", "chat/delete",
|
||||
"completions", "embeddings", "moderations", "models", "models/delete", "images",
|
||||
"speech", "transcribe", "videos", "videos/get", "videos/delete"};
|
||||
for (const char* p : posts)
|
||||
check(f.client->get(std::string("/ai/openai/") + p).status == 405, std::string("get is 405: ") + p);
|
||||
return finish("ai/openai/pages");
|
||||
}
|
||||
Reference in New Issue
Block a user