70 lines
4.8 KiB
C++
70 lines
4.8 KiB
C++
// Routes: POST /ai/deepseek/* (405, 422, dry runs, 503 without a credential)
|
|||
|
|
#include <cstdlib>
|
||
|
|
|
||
|
|
#include "support/Assert.hpp"
|
||
|
|
|
||
|
|
using namespace molo::example::test;
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
setenv("MOLO_ENV_FILES", "off", 1);
|
||
|
|
unsetenv("DEEPSEEK_API_KEY");
|
||
|
|
Fixture f;
|
||
|
|
auto post = [&](const std::string& route, const std::string& body) {
|
||
|
|
return f.client->post("/ai/deepseek/" + route, body, "application/json");
|
||
|
|
};
|
||
|
|
const char* all[] = {"chat", "stream", "prefix", "tools", "fim", "responses", "anthropic",
|
||
|
|
"models", "balance", "files", "search", "fetch"};
|
||
|
|
for (const char* r : all) {
|
||
|
|
std::string route = std::string("/ai/deepseek/") + 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 || std::string(r) == "models" || std::string(r) == "balance",
|
||
|
|
route + " non-object is 422");
|
||
|
|
}
|
||
|
|
// 422: input rules run before anything else
|
||
|
|
check(post("chat", "{}").status == 422, "chat needs prompt");
|
||
|
|
check(post("chat", "{\"prompt\":\"hi\",\"max_tokens\":0}").status == 422, "max_tokens 0");
|
||
|
|
check(post("chat", "{\"prompt\":\"hi\",\"effort\":\"loud\"}").status == 422, "bad effort");
|
||
|
|
check(post("chat", "{\"prompt\":\"hi\",\"json\":true}").status == 422, "json mode needs the word json");
|
||
|
|
check(post("chat", "{\"prompt\":\"hi\",\"thinking\":\"maybe\"}").status == 422, "bad thinking");
|
||
|
|
check(post("prefix", "{\"prompt\":\"hi\"}").status == 422, "prefix needs prefix");
|
||
|
|
check(post("fim", "{\"prompt\":\"x\",\"max_tokens\":5000}").status == 422, "fim max_tokens over 4096");
|
||
|
|
check(post("responses", "{}").status == 422, "responses needs input or instructions");
|
||
|
|
check(post("anthropic", "{\"prompt\":\"hi\",\"effort\":\"turbo\"}").status == 422, "bad anthropic effort");
|
||
|
|
check(post("files", "{\"action\":\"fly\"}").status == 422, "bad files action");
|
||
|
|
check(post("files", "{\"action\":\"get\",\"id\":\"../x\"}").status == 422, "bad file id");
|
||
|
|
check(post("files", "{\"action\":\"upload\",\"filename\":\"a.png\",\"data_base64\":\"@@\"}").status == 422, "bad base64");
|
||
|
|
check(post("files", "{\"action\":\"upload\",\"filename\":\"a.txt\",\"data_base64\":\"aGVsbG8=\"}").status == 422, "not an image");
|
||
|
|
check(post("search", "{\"query\":\" \"}").status == 422, "blank query");
|
||
|
|
check(post("search", "{\"query\":\"x\",\"count\":51}").status == 422, "count over 50");
|
||
|
|
check(post("fetch", "{\"url\":\"ftp://x\"}").status == 422, "fetch needs http(s)");
|
||
|
|
|
||
|
|
// dry runs: paid or resource-changing routes show the exact request
|
||
|
|
auto dry = post("chat", "{\"prompt\":\"hi\",\"thinking\":\"enabled\"}");
|
||
|
|
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "\"model\":\"deepseek-flash\"") &&
|
||
|
|
contains(dry.body, "/chat/completions"),
|
||
|
|
"chat is a dry run without confirm");
|
||
|
|
check(contains(post("prefix", "{\"prompt\":\"hi\",\"prefix\":\"```\"}").body, "/beta/chat/completions"), "prefix targets beta");
|
||
|
|
check(contains(post("tools", "{\"prompt\":\"w\",\"strict\":true}").body, "\"strict\":true"), "strict tools preview");
|
||
|
|
check(contains(post("fim", "{\"prompt\":\"def f():\"}").body, "/beta/completions"), "fim targets beta");
|
||
|
|
check(contains(post("search", "{\"query\":\"x\"}").body, "\"dry_run\":true"), "search is a dry run");
|
||
|
|
check(contains(post("files", "{\"action\":\"delete\",\"id\":\"file-api-1\"}").body, "\"dry_run\":true"), "delete is a dry run");
|
||
|
|
check(contains(post("files", "{\"action\":\"upload\",\"filename\":\"a.png\",\"data_base64\":\"iVBORw0KGgo=\"}").body, "\"format\":\"png\""),
|
||
|
|
"upload is a dry run");
|
||
|
|
|
||
|
|
// 503: free calls and confirmed calls need the credential
|
||
|
|
for (const char* r : {"models", "balance"}) {
|
||
|
|
auto res = post(r, "{}");
|
||
|
|
check(res.status == 503 && contains(res.body, "deepseek_not_configured"), std::string(r) + " without key is 503");
|
||
|
|
}
|
||
|
|
check(post("files", "{\"action\":\"list\"}").status == 503, "files list without key is 503");
|
||
|
|
check(post("files", "{\"action\":\"get\",\"id\":\"file-api-1\"}").status == 503, "files get without key is 503");
|
||
|
|
check(post("chat", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed chat without key is 503");
|
||
|
|
check(post("stream", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed stream without key is 503");
|
||
|
|
check(post("anthropic", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed anthropic without key is 503");
|
||
|
|
check(post("responses", "{\"input\":\"hi\",\"confirm\":true}").status == 503, "confirmed responses without key is 503");
|
||
|
|
check(post("fetch", "{\"url\":\"https://example.com\",\"confirm\":true}").status == 503, "confirmed fetch without key is 503");
|
||
|
|
check(post("files", "{\"action\":\"delete\",\"id\":\"file-api-1\",\"confirm\":true}").status == 503, "confirmed delete without key is 503");
|
||
|
|
return finish("ai/deepseek/routes");
|
||
|
|
}
|