feat(clients): huggingface: Hub, Inference Providers, Endpoints, TGI, TEI, chat model, demo

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-25 15:01:40 +01:00
co-authored by Claude Sonnet 5.5
parent 3db67fcf1f
commit 0b806eb7e3
36 changed files with 5949 additions and 3 deletions
+63
View File
@@ -0,0 +1,63 @@
#pragma once
// Shared rig for the Hugging Face tests: a Platform over three MockTransports
// (hub, router, endpoints). The mock only records the requests the real client
// builds and answers with canned bodies. Canned bodies are either RECORDED
// (copied from an unauthenticated probe on 2026-10-25, trimmed) or SHAPE
// (written by us to the documented schema, not recorded). Nothing here
// verifies vendor behavior (see src/vendors/huggingface/SPEC.md ## Unverified).
#include <memory>
#include <string>
#include "clients/transports/mock_transport.h"
#include "framework.h"
#include "vendors/huggingface/platform.h"
namespace hf_test {
using molo::clients::ClientResponse;
using molo::clients::HeaderMap;
using molo::clients::Method;
using molo::clients::transports::MockExpectation;
using molo::clients::transports::MockTransport;
using Json = molo::clients::body::Json;
inline ClientResponse reply(const std::string& body, int status = 200,
const std::string& type = "application/json") {
HeaderMap h;
h.set("Content-Type", type);
return ClientResponse(status, h, body);
}
inline ClientResponse sse(const std::string& body) { return reply(body, 200, "text/event-stream"); }
inline const char* kToken = "hf_TESTTOKEN_DO_NOT_LEAK";
struct Rig {
MockTransport* hub = nullptr;
MockTransport* router = nullptr;
MockTransport* endpoints = nullptr;
molo::huggingface::Platform platform;
explicit Rig(const std::string& token = kToken,
std::shared_ptr<molo::huggingface::RequestBudget> budget = nullptr) {
auto a = std::make_unique<MockTransport>();
auto b = std::make_unique<MockTransport>();
auto c = std::make_unique<MockTransport>();
hub = a.get();
router = b.get();
endpoints = c.get();
platform = molo::huggingface::Platform::with(std::move(a), std::move(b), std::move(c), token, budget);
}
static std::string header(const MockTransport::SeenRequest& r, const std::string& name) {
return r.headers.get_or(name, "");
}
};
inline bool has(const std::string& hay, const std::string& needle) {
return hay.find(needle) != std::string::npos;
}
} // namespace hf_test
+212
View File
@@ -0,0 +1,212 @@
// The call table and request planning: structure of the table, id and path
// validation, query and body field checks, caps. Pure local logic.
#include <set>
#include "support.h"
#include "vendors/huggingface/calls.h"
#include "vendors/huggingface/request_plan.h"
using namespace molo::huggingface;
using namespace hf_test;
using molo::llm::ApiError;
TEST(huggingface_calls, table_is_in_enum_order_and_unique) {
std::set<std::string> seen;
for (std::size_t i = 0; i < Calls::count(); ++i) {
const CallSpec& s = Calls::at(i);
ASSERT_EQ(static_cast<std::size_t>(s.call), i);
ASSERT_TRUE(&Calls::spec_of(s.call) == &s);
std::string key = std::string(surface_name(s.surface)) + " " + s.http_method + " " + s.path;
ASSERT_TRUE(seen.insert(key).second);
}
ASSERT_EQ(Calls::count(), static_cast<std::size_t>(132));
}
TEST(huggingface_calls, rows_use_known_vocabulary) {
const std::set<std::string> specs = {"none", "hub", "ie", "tgi", "tei", "src", "task"};
const std::set<std::string> auths = {"none", "optional", "token"};
const std::set<std::string> methods = {"GET", "POST", "PUT", "PATCH", "DELETE"};
for (std::size_t i = 0; i < Calls::count(); ++i) {
const CallSpec& s = Calls::at(i);
ASSERT_TRUE(specs.count(s.spec) == 1);
ASSERT_TRUE(auths.count(s.auth) == 1);
ASSERT_TRUE(methods.count(s.http_method) == 1);
ASSERT_TRUE(s.path[0] == '/' || s.path[0] == '{');
ASSERT_TRUE(std::string(s.ref).size() > 0);
// braces balance
int depth = 0;
for (const char* p = s.path; *p; ++p) {
if (*p == '{') ++depth;
if (*p == '}') --depth;
ASSERT_TRUE(depth == 0 || depth == 1);
}
ASSERT_EQ(depth, 0);
}
}
TEST(huggingface_calls, writes_need_a_token_and_deletes_are_marked_destructive) {
std::size_t deletes = 0;
for (std::size_t i = 0; i < Calls::count(); ++i) {
const CallSpec& s = Calls::at(i);
if (s.has_flag("write")) ASSERT_EQ(std::string(s.auth), std::string("token"));
if (std::string(s.http_method) == "DELETE") {
++deletes;
ASSERT_TRUE(s.has_flag("destructive"));
}
if (s.has_flag("destructive")) ASSERT_TRUE(s.has_flag("write"));
}
ASSERT_EQ(deletes, static_cast<std::size_t>(7)); // 3 Hub (discussion, collection, webhook) + 4 Endpoints
}
TEST(huggingface_calls, per_surface_counts_match_the_published_documents) {
std::size_t counts[5] = {0, 0, 0, 0, 0};
std::size_t unsupported = 0;
for (std::size_t i = 0; i < Calls::count(); ++i) {
++counts[static_cast<int>(Calls::at(i).surface)];
if (Calls::at(i).has_flag("unsupported")) ++unsupported;
}
ASSERT_EQ(counts[static_cast<int>(Surface::kEndpoints)], static_cast<std::size_t>(48)); // IE openapi 2.0.0
ASSERT_EQ(counts[static_cast<int>(Surface::kTgi)], static_cast<std::size_t>(12)); // TGI 3.3.6-dev0
ASSERT_EQ(counts[static_cast<int>(Surface::kTei)], static_cast<std::size_t>(12)); // TEI 1.9.4
ASSERT_EQ(unsupported, static_cast<std::size_t>(3)); // the three GET SSE streams
}
TEST(huggingface_paths, repo_ref_needs_owner_and_name) {
RepoRef r = RepoRef::parse("openai-community/gpt2");
ASSERT_EQ(r.owner(), std::string("openai-community"));
ASSERT_EQ(r.name(), std::string("gpt2"));
ASSERT_THROW(RepoRef::parse("gpt2"), ApiError);
ASSERT_THROW(RepoRef::parse("a/b/c"), ApiError);
ASSERT_THROW(RepoRef::parse("../etc"), ApiError);
ASSERT_THROW(RepoRef::parse("a/.."), ApiError);
ASSERT_THROW(RepoRef::parse("a/b?x=1"), ApiError);
ASSERT_THROW(RepoRef::parse("a/b c"), ApiError);
ASSERT_THROW(RepoRef::parse(std::string("a/") + std::string(257, 'x')), ApiError);
}
TEST(huggingface_paths, renders_typed_repo_paths) {
PathArgs a;
a.repo(RepoRef::parse("squad/x")).repo_type(RepoType::kDataset).set("rev", "main").set("path", "");
Plan p = RequestPlan::make(Call::kRepoTree, a, Query());
ASSERT_EQ(p.target, std::string("/api/datasets/squad/x/tree/main"));
ASSERT_TRUE(p.method == Method::kGet);
a.set("path", "data/train 1.parquet");
p = RequestPlan::make(Call::kRepoTree, a, Query());
ASSERT_EQ(p.target, std::string("/api/datasets/squad/x/tree/main/data/train%201.parquet"));
}
TEST(huggingface_paths, revision_is_one_encoded_segment) {
PathArgs a;
a.repo(RepoRef::parse("o/n")).repo_type(RepoType::kModel).set("rev", "refs/pr/1");
Plan p = RequestPlan::make(Call::kRepoCommits, a, Query());
ASSERT_EQ(p.target, std::string("/api/models/o/n/commits/refs%2Fpr%2F1"));
a.set("rev", "..");
ASSERT_THROW(RequestPlan::make(Call::kRepoCommits, a, Query()), ApiError);
a.set("rev", "");
ASSERT_THROW(RequestPlan::make(Call::kRepoCommits, a, Query()), ApiError);
a.set("rev", "a\nb");
ASSERT_THROW(RequestPlan::make(Call::kRepoCommits, a, Query()), ApiError);
}
TEST(huggingface_paths, resolve_uses_the_type_prefix) {
PathArgs a;
a.repo(RepoRef::parse("o/n")).set("rev", "main").set("path", "config.json");
a.repo_type(RepoType::kModel);
ASSERT_EQ(RequestPlan::make(Call::kFileResolve, a, Query()).target, std::string("/o/n/resolve/main/config.json"));
a.repo_type(RepoType::kDataset);
ASSERT_EQ(RequestPlan::make(Call::kFileResolve, a, Query()).target,
std::string("/datasets/o/n/resolve/main/config.json"));
a.repo_type(RepoType::kSpace);
ASSERT_EQ(RequestPlan::make(Call::kFileResolve, a, Query()).target, std::string("/spaces/o/n/resolve/main/config.json"));
a.set("path", ""); // a file path is required for resolve
ASSERT_THROW(RequestPlan::make(Call::kFileResolve, a, Query()), ApiError);
}
TEST(huggingface_paths, file_path_rules) {
ASSERT_EQ(RequestPlan::encode_file_path("a/b.txt"), std::string("a/b.txt"));
ASSERT_THROW(RequestPlan::encode_file_path("/abs"), ApiError);
ASSERT_THROW(RequestPlan::encode_file_path("a//b"), ApiError);
ASSERT_THROW(RequestPlan::encode_file_path("a/../b"), ApiError);
ASSERT_THROW(RequestPlan::encode_file_path("a/./b"), ApiError);
ASSERT_THROW(RequestPlan::encode_file_path(std::string(1025, 'x')), ApiError);
std::string deep;
for (int i = 0; i < 33; ++i) deep += "d/";
ASSERT_THROW(RequestPlan::encode_file_path(deep + "f"), ApiError);
}
TEST(huggingface_paths, query_and_body_are_checked_against_the_table) {
PathArgs a;
a.repo(RepoRef::parse("o/n")).repo_type(RepoType::kModel);
Query ok;
ok.flag("include_prs", true);
ASSERT_EQ(RequestPlan::make(Call::kRepoRefs, a, ok).target, std::string("/api/models/o/n/refs?include_prs=true"));
Query unknown;
unknown.add("evil", "1");
ASSERT_THROW(RequestPlan::make(Call::kRepoRefs, a, unknown), ApiError);
Json body = Json::object();
ASSERT_THROW(RequestPlan::make(Call::kRepoRefs, a, Query(), &body), ApiError); // GET takes no body
PathArgs b;
b.repo(RepoRef::parse("o/n")).repo_type(RepoType::kModel).set("rev", "main");
Json paths = Json::object();
paths.set("paths", Json::array());
ASSERT_TRUE(!RequestPlan::make(Call::kRepoPathsInfo, b, Query(), &paths).target.empty());
paths.set("sneaky", Json(1));
ASSERT_THROW(RequestPlan::make(Call::kRepoPathsInfo, b, Query(), &paths), ApiError);
}
TEST(huggingface_paths, query_values_are_encoded_and_dots_survive_the_client_guard) {
Query q;
q.add("search", "a b&c=d");
Plan p = RequestPlan::make(Call::kModelsList, PathArgs(), q);
ASSERT_EQ(p.target, std::string("/api/models?search=a%20b%26c%3Dd"));
Query dots;
dots.add("search", "x..y");
p = RequestPlan::make(Call::kModelsList, PathArgs(), dots);
ASSERT_EQ(p.target, std::string("/api/models?search=x%2E%2Ey"));
ASSERT_TRUE(p.target.find("..") == std::string::npos);
}
TEST(huggingface_paths, missing_and_unused_arguments_are_refused) {
PathArgs none;
ASSERT_THROW(RequestPlan::make(Call::kModelInfo, none, Query()), ApiError);
PathArgs extra;
extra.repo(RepoRef::parse("o/n")).set("surplus", "1");
ASSERT_THROW(RequestPlan::make(Call::kModelInfo, extra, Query()), ApiError);
}
TEST(huggingface_paths, caps_on_queries) {
Query q;
for (std::size_t i = 0; i < 32; ++i) q.add("filter", "x");
ASSERT_THROW(q.add("filter", "x"), ApiError);
Query big;
ASSERT_THROW(big.add("search", std::string(2049, 'x')), ApiError);
ASSERT_THROW(big.add("search", std::string("a\r\nb")), ApiError);
}
TEST(huggingface_paths, unsupported_calls_are_refused_with_a_reason) {
PathArgs a;
a.set("namespace", "acme").set("name", "ep");
try {
RequestPlan::make(Call::kIeGetEndpointSse, a, Query());
FAIL("expected a refusal");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 400);
ASSERT_TRUE(molo::huggingface::Calls::spec_of(Call::kIeGetEndpointSse).has_flag("unsupported"));
}
}
TEST(huggingface_paths, special_placeholders) {
PathArgs a;
a.set("webhookId", "abc123").set("action", "enable");
ASSERT_EQ(RequestPlan::make(Call::kWebhookToggle, a, Query()).target, std::string("/api/settings/webhooks/abc123/enable"));
a.set("action", "explode");
ASSERT_THROW(RequestPlan::make(Call::kWebhookToggle, a, Query()), ApiError);
PathArgs d;
d.repo(RepoRef::parse("o/n")).repo_type(RepoType::kSpace).set("num", "12");
ASSERT_EQ(RequestPlan::make(Call::kDiscussionGet, d, Query()).target, std::string("/api/spaces/o/n/discussions/12"));
d.set("num", "12x");
ASSERT_THROW(RequestPlan::make(Call::kDiscussionGet, d, Query()), ApiError);
ASSERT_EQ(RequestPlan::check_model("gpt2"), std::string("gpt2"));
ASSERT_THROW(RequestPlan::check_model("a/b/c"), ApiError);
ASSERT_THROW(RequestPlan::check_model(""), ApiError);
}
+135
View File
@@ -0,0 +1,135 @@
// Discussions, collections, webhooks: validation and the exact requests built.
#include "support.h"
#include "vendors/huggingface/community.h"
using namespace molo::huggingface;
using namespace hf_test;
using molo::llm::ApiError;
namespace {
// RECORDED 2026-10-25: GET /api/collections/nvidia/flora-6ad6a8986320a55467825cf5 (first fields)
const char* kCollection =
"{\"slug\":\"nvidia/flora-6ad6a8986320a55467825cf5\",\"title\":\"Flora\",\"gating\":false,"
"\"owner\":{\"name\":\"nvidia\",\"type\":\"org\"}}";
} // namespace
TEST(huggingface_community, discussion_reads_are_public_and_writes_need_a_token) {
Rig anon("");
Discussions d(anon.platform.hub);
anon.hub->expect_response(Method::kGet, "/api/models/o/n/discussions", reply("{\"discussions\":[]}"));
DiscussionFilter f;
f.status = "open";
f.page = 2;
d.list(RepoType::kModel, RepoRef::parse("o/n"), f);
ASSERT_EQ(anon.hub->seen().back().target, std::string("/api/models/o/n/discussions?p=2&status=open"));
try {
d.create(RepoType::kModel, RepoRef::parse("o/n"), "A title", "body", false);
FAIL("expected NOT_CONFIGURED");
} catch (const ApiError& e) {
ASSERT_EQ(e.type(), std::string("NOT_CONFIGURED"));
}
ASSERT_EQ(anon.hub->send_count(), static_cast<std::size_t>(1));
}
TEST(huggingface_community, discussion_writes_build_the_documented_bodies) {
Rig rig;
Discussions d(rig.platform.hub);
RepoRef r = RepoRef::parse("o/n");
rig.hub->expect_response(Method::kPost, "/api/datasets/o/n/discussions", reply("{\"num\":7}"));
d.create(RepoType::kDataset, r, "Typo in card", "details", true);
ASSERT_EQ(rig.hub->seen().back().body, std::string("{\"description\":\"details\",\"pullRequest\":true,\"title\":\"Typo in card\"}"));
rig.hub->expect_response(Method::kPost, "/api/datasets/o/n/discussions/7/status", reply("{}"));
d.change_status(RepoType::kDataset, r, 7, "closed", "done");
ASSERT_EQ(rig.hub->seen().back().body, std::string("{\"comment\":\"done\",\"status\":\"closed\"}"));
rig.hub->expect_response(Method::kDelete, "/api/datasets/o/n/discussions/7", reply("{}"));
d.remove_discussion(RepoType::kDataset, r, 7);
ASSERT_THROW(d.create(RepoType::kModel, r, "ab", "x", false), ApiError); // title 3..200
ASSERT_THROW(d.change_status(RepoType::kModel, r, 1, "maybe"), ApiError);
ASSERT_THROW(d.comment(RepoType::kModel, r, 0, "x"), ApiError);
ASSERT_THROW(d.comment(RepoType::kModel, r, 1, ""), ApiError);
}
TEST(huggingface_community, collections_read_and_write) {
Rig rig;
Collections c(rig.platform.hub);
rig.hub->expect_response(Method::kGet, "/api/collections/nvidia/flora-6ad6a8986320a55467825cf5", reply(kCollection));
Json got = c.get("nvidia", "flora-6ad6a8986320a55467825cf5");
ASSERT_EQ(got.find("title")->as_string(), std::string("Flora"));
rig.hub->expect_response(Method::kPost, "/api/collections", reply("{\"slug\":\"me/x-1\"}"));
c.create("me", "My list", "d", true);
ASSERT_EQ(rig.hub->seen().back().body,
std::string("{\"description\":\"d\",\"namespace\":\"me\",\"private\":true,\"title\":\"My list\"}"));
ASSERT_THROW(c.create("me", "", "", false), ApiError);
ASSERT_THROW(c.create("me", std::string(61, 'x'), "", false), ApiError);
ASSERT_THROW(c.create("me", "t", std::string(151, 'x'), false), ApiError);
CollectionItem bad_item;
bad_item.type = "video";
bad_item.id = "x";
ASSERT_THROW(c.add_item("me", "x-1", bad_item), ApiError);
CollectionItem item;
item.type = "model";
item.id = "openai-community/gpt2";
rig.hub->expect_response(Method::kPost, "/api/collections/me/x-1/items", reply("{}"));
c.add_item("me", "x-1", item, "note");
ASSERT_EQ(rig.hub->seen().back().body,
std::string("{\"item\":{\"id\":\"openai-community/gpt2\",\"type\":\"model\"},\"note\":\"note\"}"));
ASSERT_THROW(c.update("me", "x-1", Json::object()), ApiError);
}
TEST(huggingface_community, collection_list_walks_with_item_filter) {
Rig rig("");
Collections c(rig.platform.hub);
rig.hub->expect_response(Method::kGet, "/api/collections", reply("[{\"slug\":\"a/b-1\"}]"));
CollectionListParams p;
p.owner = "nvidia";
p.limit = 5;
p.item.type = "model";
p.item.id = "openai-community/gpt2";
Page page = c.list(p, 10, 2);
ASSERT_EQ(page.items.as_array().size(), static_cast<std::size_t>(1));
ASSERT_EQ(rig.hub->seen().back().target, std::string("/api/collections?owner=nvidia&limit=5&item=model%2Fopenai-community%2Fgpt2"));
}
TEST(huggingface_community, webhook_spec_is_validated_before_anything_is_sent) {
Rig rig;
Webhooks w(rig.platform.hub);
WebhookSpec s;
s.url = "https://example.com/hook";
s.watched.push_back({"model", "openai-community/gpt2"});
s.domains = {"repo"};
rig.hub->expect_response(Method::kPost, "/api/settings/webhooks", reply("{\"webhook\":{\"id\":\"w1\"}}"));
w.create(s);
ASSERT_EQ(rig.hub->seen().back().body,
std::string("{\"domains\":[\"repo\"],\"url\":\"https://example.com/hook\",\"watched\":[{\"name\":\"openai-community/gpt2\",\"type\":\"model\"}]}"));
WebhookSpec bad = s;
bad.url = "ftp://x/y/z/w";
ASSERT_THROW(w.create(bad), ApiError);
bad = s;
bad.watched.clear();
ASSERT_THROW(w.create(bad), ApiError);
bad = s;
bad.watched[0].type = "planet";
ASSERT_THROW(w.create(bad), ApiError);
bad = s;
bad.domains = {"everything"};
ASSERT_THROW(w.create(bad), ApiError);
bad = s;
bad.secret = "caf\xc3\xa9";
ASSERT_THROW(w.create(bad), ApiError);
ASSERT_EQ(rig.hub->send_count(), static_cast<std::size_t>(1));
}
TEST(huggingface_community, webhook_lifecycle_paths) {
Rig rig;
Webhooks w(rig.platform.hub);
rig.hub->expect_response(Method::kPost, "/api/settings/webhooks/w1/enable", reply("{}"));
rig.hub->expect_response(Method::kPost, "/api/settings/webhooks/w1/disable", reply("{}"));
rig.hub->expect_response(Method::kPost, "/api/settings/webhooks/w1/replay/l9", reply("{}"));
rig.hub->expect_response(Method::kDelete, "/api/settings/webhooks/w1", reply("{}"));
w.enable("w1");
w.disable("w1");
w.replay("w1", "l9");
w.remove_webhook("w1");
rig.hub->verify();
ASSERT_THROW(w.get("../x"), ApiError);
}
+229
View File
@@ -0,0 +1,229 @@
// Hub reads and the shared service rules (token, budget, paging) through the
// real ApiClient over a mock transport. Replies are RECORDED trims or SHAPE.
#include "support.h"
#include "vendors/huggingface/hub.h"
#include "vendors/huggingface/platform.h"
using namespace molo::huggingface;
using namespace hf_test;
using molo::llm::ApiError;
namespace {
// RECORDED 2026-10-25: GET https://huggingface.co/api/models/openai-community/gpt2/refs (no token)
const char* kRefs =
"{\"tags\":[],\"branches\":[{\"name\":\"main\",\"ref\":\"refs/heads/main\","
"\"targetCommit\":\"607a30d783dfa663caf39e06633721c8d4cfcd7e\"}],\"converts\":[]}";
// RECORDED 2026-10-25: GET /api/models/openai-community/gpt2/tree/main (first 3 entries)
const char* kTree =
"[{\"type\":\"directory\",\"oid\":\"d03ec5ec179df58241d27d55f92a674f5f44197f\",\"size\":0,\"path\":\"onnx\"},"
"{\"type\":\"file\",\"oid\":\"602b71f15d40ed68c5f96330e3f3175a76a32126\",\"size\":445,\"path\":\".gitattributes\"}]";
// RECORDED 2026-10-25: GET /api/whoami-v2 without a token -> 401
const char* kUnauth = "{\"error\":\"Invalid username or password.\"}";
// RECORDED 2026-10-25: GET /api/models?limit=1 (trimmed fields)
const char* kModel =
"{\"_id\":\"621ffdc036468d709f17434d\",\"id\":\"openai-community/gpt2\",\"likes\":3891,\"private\":false,"
"\"downloads\":20867408,\"pipeline_tag\":\"text-generation\",\"library_name\":\"transformers\"}";
ClientResponse paged(const std::string& body, const std::string& next_cursor) {
HeaderMap h;
h.set("Content-Type", "application/json");
if (!next_cursor.empty())
h.set("Link", "<https://huggingface.co/api/models?limit=1&cursor=" + next_cursor + ">; rel=\"next\"");
return ClientResponse(200, h, body);
}
} // namespace
TEST(huggingface_hub, anonymous_reads_send_no_authorization) {
Rig rig("");
rig.hub->expect_response(Method::kGet, "/api/models/openai-community/gpt2/refs", reply(kRefs));
Hub hub = Hub::from_platform(rig.platform);
ASSERT_FALSE(hub.has_token());
Json refs = hub.refs(RepoType::kModel, RepoRef::parse("openai-community/gpt2"));
ASSERT_EQ(refs.find("branches")->as_array().front().find("name")->as_string(), std::string("main"));
auto seen = rig.hub->seen();
ASSERT_EQ(seen.size(), static_cast<std::size_t>(1));
ASSERT_EQ(Rig::header(seen[0], "Authorization"), std::string(""));
ASSERT_EQ(Rig::header(seen[0], "X-Molo-Anonymous"), std::string("1"));
}
TEST(huggingface_hub, token_travels_as_bearer_only) {
Rig rig;
rig.hub->expect_response(Method::kGet, "/api/models/o/n/refs", reply(kRefs));
Hub hub = Hub::from_platform(rig.platform);
hub.refs(RepoType::kModel, RepoRef::parse("o/n"), true);
auto seen = rig.hub->seen();
ASSERT_EQ(Rig::header(seen[0], "Authorization"), std::string("Bearer ") + kToken);
ASSERT_TRUE(seen[0].target.find(kToken) == std::string::npos);
ASSERT_EQ(seen[0].target, std::string("/api/models/o/n/refs?include_prs=true"));
}
TEST(huggingface_hub, whoami_without_token_fails_before_any_traffic) {
Rig rig("");
Hub hub = Hub::from_platform(rig.platform);
try {
hub.whoami();
FAIL("expected NOT_CONFIGURED");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 503);
ASSERT_EQ(e.type(), std::string("NOT_CONFIGURED"));
ASSERT_TRUE(has(e.message(), "HF_TOKEN"));
}
ASSERT_EQ(rig.hub->send_count(), static_cast<std::size_t>(0));
}
TEST(huggingface_hub, hub_error_envelope_is_kept) {
Rig rig;
rig.hub->expect_response(Method::kGet, "/api/whoami-v2", reply(kUnauth, 401));
Hub hub = Hub::from_platform(rig.platform);
try {
hub.whoami();
FAIL("expected 401");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 401);
ASSERT_EQ(e.message(), std::string("Invalid username or password."));
ASSERT_TRUE(!has(e.message(), kToken));
}
}
TEST(huggingface_hub, list_walks_link_cursors_and_reports_page_truncation) {
Rig rig("");
Hub hub = Hub::from_platform(rig.platform);
// Three pages exist; the walk is capped at two pages.
rig.hub->expect_response(Method::kGet, "/api/models", paged(std::string("[") + kModel + "]", "AAA"));
rig.hub->expect_response(Method::kGet, "/api/models", paged(std::string("[") + kModel + "]", "BBB"));
RepoListParams p;
p.limit = 1;
p.sort = "downloads";
Page page = hub.list_repos(RepoType::kModel, p, 100, 2);
ASSERT_EQ(page.items.as_array().size(), static_cast<std::size_t>(2));
ASSERT_TRUE(page.truncated);
ASSERT_EQ(page.truncated_by, std::string("pages"));
ASSERT_EQ(page.next_cursor, std::string("BBB"));
auto seen = rig.hub->seen();
ASSERT_EQ(seen[0].target, std::string("/api/models?sort=downloads&limit=1"));
ASSERT_EQ(seen[1].target, std::string("/api/models?sort=downloads&limit=1&cursor=AAA"));
}
TEST(huggingface_hub, list_reports_item_truncation_and_clean_end) {
Rig rig("");
Hub hub = Hub::from_platform(rig.platform);
rig.hub->expect_response(Method::kGet, "/api/datasets",
paged(std::string("[") + kModel + "," + kModel + "," + kModel + "]", ""));
Page cut = hub.list_repos(RepoType::kDataset, RepoListParams(), 2, 5);
ASSERT_EQ(cut.items.as_array().size(), static_cast<std::size_t>(2));
ASSERT_TRUE(cut.truncated);
ASSERT_EQ(cut.truncated_by, std::string("items"));
rig.hub->expect_response(Method::kGet, "/api/spaces", paged(std::string("[") + kModel + "]", ""));
Page done = hub.list_repos(RepoType::kSpace, RepoListParams(), 10, 5);
ASSERT_FALSE(done.truncated);
ASSERT_EQ(done.pages, static_cast<std::size_t>(1));
}
TEST(huggingface_hub, link_header_to_another_host_is_refused) {
Rig rig("");
Hub hub = Hub::from_platform(rig.platform);
HeaderMap h;
h.set("Content-Type", "application/json");
h.set("Link", "<https://evil.example/api/models?cursor=x>; rel=\"next\"");
rig.hub->expect_response(Method::kGet, "/api/models", ClientResponse(200, h, "[]"));
ASSERT_THROW(hub.list_repos(RepoType::kModel, RepoListParams()), ApiError);
}
TEST(huggingface_hub, link_header_parsing) {
ASSERT_EQ(next_cursor_of("<https://huggingface.co/api/models?limit=2&cursor=abc%3D>; rel=\"next\"", "huggingface.co"),
std::string("abc="));
ASSERT_EQ(next_cursor_of("<https://huggingface.co/api/models?cursor=p>; rel=\"prev\"", "huggingface.co"), std::string(""));
ASSERT_EQ(next_cursor_of("", "huggingface.co"), std::string(""));
ASSERT_EQ(next_cursor_of("<https://huggingface.co/x?cursor=a>; rel=\"prev\", <https://huggingface.co/x?cursor=b>; rel=\"next\"",
"huggingface.co"),
std::string("b"));
}
TEST(huggingface_hub, list_params_are_validated) {
RepoListParams p;
p.limit = 1001;
ASSERT_THROW(p.to_query(), ApiError);
p.limit = -1;
ASSERT_THROW(p.to_query(), ApiError);
RepoListParams q;
q.pipeline_tag = "text-generation";
q.expand = {"downloads", "likes"};
Rig rig("");
Hub hub = Hub::from_platform(rig.platform);
// pipeline_tag is a models-only filter: the datasets row does not list it.
ASSERT_THROW(hub.list_repos(RepoType::kDataset, q), ApiError);
ASSERT_EQ(rig.hub->send_count(), static_cast<std::size_t>(0));
}
TEST(huggingface_hub, tree_paths_info_and_download) {
Rig rig("");
Hub hub = Hub::from_platform(rig.platform);
rig.hub->expect_response(Method::kGet, "/api/models/o/n/tree/main", reply(kTree));
Json t = hub.tree(RepoType::kModel, RepoRef::parse("o/n"), "main");
ASSERT_EQ(t.as_array().size(), static_cast<std::size_t>(2));
rig.hub->expect_response(Method::kPost, "/api/models/o/n/paths-info/main",
reply("[{\"type\":\"file\",\"oid\":\"10c6\",\"size\":665,\"path\":\"config.json\"}]"));
hub.paths_info(RepoType::kModel, RepoRef::parse("o/n"), "main", {"config.json"});
auto seen = rig.hub->seen();
ASSERT_EQ(seen.back().body, std::string("{\"paths\":[\"config.json\"]}"));
ASSERT_THROW(hub.paths_info(RepoType::kModel, RepoRef::parse("o/n"), "main", {}), ApiError);
ASSERT_THROW(hub.paths_info(RepoType::kModel, RepoRef::parse("o/n"), "main", {"../x"}), ApiError);
HeaderMap h;
h.set("Content-Type", "text/plain; charset=utf-8");
h.set("X-Repo-Commit", "607a30d7");
h.set("ETag", "\"abc\"");
rig.hub->expect_response(Method::kGet, "/o/n/resolve/main/config.json", ClientResponse(200, h, "{\"a\":1}"));
Download d = hub.download(RepoType::kModel, RepoRef::parse("o/n"), "main", "config.json");
ASSERT_EQ(d.bytes, std::string("{\"a\":1}"));
ASSERT_EQ(d.commit, std::string("607a30d7"));
ASSERT_EQ(d.etag, std::string("\"abc\""));
}
TEST(huggingface_hub, search_and_catalogs_validate_input) {
Rig rig("");
Hub hub = Hub::from_platform(rig.platform);
ASSERT_THROW(hub.quicksearch(""), ApiError);
ASSERT_THROW(hub.quicksearch("bert", 5000), ApiError);
ASSERT_THROW(hub.tags_by_type(RepoType::kSpace), ApiError);
ASSERT_THROW(hub.paper("../x"), ApiError);
ASSERT_THROW(hub.partner_models("a/b"), ApiError);
rig.hub->expect_response(Method::kGet, "/api/quicksearch", reply("{\"models\":[],\"datasets\":[],\"spaces\":[]}"));
hub.quicksearch("bert", 2, "model");
ASSERT_EQ(rig.hub->seen().back().target, std::string("/api/quicksearch?q=bert&limit=2&type=model"));
}
TEST(huggingface_budget, spends_down_and_resets_with_the_window) {
long now = 1000;
auto budget = std::make_shared<RequestBudget>(2, 300, [&] { return now; });
Rig rig("", budget);
Hub hub = Hub::from_platform(rig.platform);
rig.hub->expect_response(Method::kGet, "/api/models/o/n/refs", reply(kRefs));
rig.hub->expect_response(Method::kGet, "/api/models/o/n/refs", reply(kRefs));
rig.hub->expect_response(Method::kGet, "/api/models/o/n/refs", reply(kRefs));
RepoRef r = RepoRef::parse("o/n");
hub.refs(RepoType::kModel, r);
hub.refs(RepoType::kModel, r);
try {
hub.refs(RepoType::kModel, r);
FAIL("expected the local budget to stop the call");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 429);
ASSERT_EQ(e.type(), std::string("LOCAL_BUDGET"));
}
ASSERT_EQ(rig.hub->send_count(), static_cast<std::size_t>(2));
now += 301;
hub.refs(RepoType::kModel, r);
ASSERT_EQ(rig.hub->send_count(), static_cast<std::size_t>(3));
}
TEST(huggingface_platform, server_urls_are_validated) {
ASSERT_EQ(Platform::check_server_url("http://127.0.0.1:8080/"), std::string("http://127.0.0.1:8080"));
ASSERT_EQ(Platform::check_server_url("https://x.endpoints.huggingface.cloud"), std::string("https://x.endpoints.huggingface.cloud"));
ASSERT_THROW(Platform::check_server_url(""), ApiError);
ASSERT_THROW(Platform::check_server_url("ftp://x"), ApiError);
ASSERT_THROW(Platform::check_server_url("http://user:pw@host"), ApiError);
ASSERT_THROW(Platform::check_server_url("http://host/?a=b"), ApiError);
ASSERT_THROW(Platform::check_server_url("not a url"), ApiError);
}
+136
View File
@@ -0,0 +1,136 @@
// Opt-in live checks. MOLO_LIVE_NET=1 enables the network. Public Hub reads,
// the router model list and the Inference Endpoints vendor list need NO key
// and run under MOLO_LIVE_NET=1 alone. Token-gated reads also need HF_TOKEN
// (via llm::Config); the one tiny chat completion additionally needs
// MOLO_LIVE_PAID=1. Without them each test prints a loud SKIP. Nothing is
// created, changed or deleted.
#include <cstdlib>
#include <iostream>
#include "clients/transports/http_transport.h"
#include "support.h"
#include "vendors/huggingface/chat_model.h"
#include "vendors/huggingface/community.h"
#include "vendors/huggingface/endpoints.h"
#include "vendors/huggingface/hub.h"
#include "vendors/huggingface/providers.h"
using namespace molo::huggingface;
using molo::llm::ApiError;
namespace {
bool net() { return std::getenv("MOLO_LIVE_NET") != nullptr; }
bool paid() { return std::getenv("MOLO_LIVE_PAID") != nullptr; }
Platform anonymous_platform() {
return Platform::with(std::make_unique<molo::clients::transports::HttpTransport>(),
std::make_unique<molo::clients::transports::HttpTransport>(),
std::make_unique<molo::clients::transports::HttpTransport>(), "");
}
} // namespace
TEST(huggingface_live, public_hub_reads_without_a_key) {
if (!net()) {
std::cout << "SKIP huggingface_live.public_hub_reads_without_a_key (set MOLO_LIVE_NET=1; no key needed)\n";
return;
}
Platform p = anonymous_platform();
Hub hub = Hub::from_platform(p);
ASSERT_FALSE(hub.has_token());
RepoRef gpt2 = RepoRef::parse("openai-community/gpt2");
RepoListParams params;
params.limit = 2;
params.sort = "downloads";
Page models = hub.list_repos(RepoType::kModel, params, 4, 2);
std::cout << " models listed: " << models.items.as_array().size() << " pages=" << models.pages
<< " truncated=" << models.truncated << "\n";
ASSERT_TRUE(models.items.as_array().size() >= 2);
Json info = hub.repo_info(RepoType::kModel, gpt2);
ASSERT_EQ(info.find("id")->as_string(), std::string("openai-community/gpt2"));
Json refs = hub.refs(RepoType::kModel, gpt2);
ASSERT_TRUE(refs.find("branches") != nullptr);
Json tree = hub.tree(RepoType::kModel, gpt2, "main");
ASSERT_TRUE(tree.is_array() && !tree.as_array().empty());
Download cfg = hub.download(RepoType::kModel, gpt2, "main", "config.json");
std::cout << " config.json: " << cfg.bytes.size() << " bytes commit=" << cfg.commit << "\n";
ASSERT_TRUE(cfg.bytes.find("GPT2LMHeadModel") != std::string::npos);
Json ds = hub.repo_info(RepoType::kDataset, RepoRef::parse("rajpurkar/squad"));
ASSERT_EQ(ds.find("id")->as_string(), std::string("rajpurkar/squad"));
Json sp = hub.repo_info(RepoType::kSpace, RepoRef::parse("gradio/hello_world"));
ASSERT_EQ(sp.find("id")->as_string(), std::string("gradio/hello_world"));
Json search = hub.quicksearch("bert", 2);
ASSERT_TRUE(search.is_object());
Json hw = hub.spaces_hardware();
ASSERT_TRUE(hw.is_array());
Json trending = hub.trending("model", 3);
ASSERT_TRUE(trending.is_object());
Json mapping = hub.partner_models("groq");
ASSERT_TRUE(mapping.is_object());
Discussions d(p.hub);
Json disc = d.list(RepoType::kModel, gpt2);
ASSERT_TRUE(disc.is_object());
Collections c(p.hub);
Json coll = c.get("nvidia", "flora-6ad6a8986320a55467825cf5");
ASSERT_EQ(coll.find("title")->as_string(), std::string("Flora"));
}
TEST(huggingface_live, router_models_and_endpoint_vendors_without_a_key) {
if (!net()) {
std::cout << "SKIP huggingface_live.router_models_and_endpoint_vendors_without_a_key (set MOLO_LIVE_NET=1; no key needed)\n";
return;
}
Platform p = anonymous_platform();
Json models = RouterChat::from_platform(p).models();
std::cout << " router models: " << models.find("data")->as_array().size() << "\n";
ASSERT_TRUE(models.find("data")->as_array().size() > 10);
Json vendors = Endpoints::from_platform(p).vendors();
ASSERT_TRUE(vendors.find("vendors")->as_array().size() >= 1);
}
TEST(huggingface_live, invalid_token_answers_401_on_whoami_and_endpoints) {
if (!net()) {
std::cout << "SKIP huggingface_live.invalid_token_answers_401_on_whoami_and_endpoints (set MOLO_LIVE_NET=1)\n";
return;
}
Platform p = Platform::with(std::make_unique<molo::clients::transports::HttpTransport>(),
std::make_unique<molo::clients::transports::HttpTransport>(),
std::make_unique<molo::clients::transports::HttpTransport>(), "hf_invalid_molo_probe");
try {
Hub::from_platform(p).whoami();
FAIL("expected 401");
} catch (const ApiError& e) {
std::cout << " GET /api/whoami-v2 with an invalid token: " << e.http_status() << " " << e.message() << "\n";
ASSERT_EQ(e.http_status(), 401);
}
try {
Endpoints::from_platform(p).list("huggingface");
FAIL("expected 401");
} catch (const ApiError& e) {
std::cout << " GET /v2/endpoint/huggingface with an invalid token: " << e.http_status() << " " << e.message() << "\n";
ASSERT_EQ(e.http_status(), 401);
}
}
TEST(huggingface_live, token_reads_and_one_tiny_completion) {
Platform p = Platform::from_env();
if (!net() || !p.configured()) {
std::cout << "SKIP huggingface_live.token_reads_and_one_tiny_completion (set MOLO_LIVE_NET=1 and HF_TOKEN)\n";
return;
}
Json who = Hub::from_platform(p).whoami();
ASSERT_TRUE(who.find("name") != nullptr);
std::cout << " whoami ok\n";
if (!paid()) {
std::cout << "SKIP huggingface_live.token_reads_and_one_tiny_completion chat part (also set MOLO_LIVE_PAID=1)\n";
return;
}
auto model = HuggingfaceChatModel::over_router(p, "openai/gpt-oss-20b");
molo::llm::ChatRequest req;
req.messages.push_back(molo::llm::Message::user("Reply with the single word: ok"));
req.max_tokens = 16;
molo::llm::ChatResponse r = model->generate(req);
std::cout << " completion finish=" << r.finish_reason << " tokens=" << r.usage.total_tokens << "\n";
ASSERT_TRUE(r.usage.total_tokens > 0);
}
+330
View File
@@ -0,0 +1,330 @@
// Inference Providers router, hf-inference tasks, the chat wire and
// HuggingfaceChatModel. Replies are SHAPE (documented schema) or RECORDED.
#include "support.h"
#include "vendors/huggingface/chat_model.h"
#include "vendors/huggingface/providers.h"
using namespace molo::huggingface;
using namespace hf_test;
using molo::llm::ApiError;
namespace {
// SHAPE: chat completion per huggingface.js tasks/chat-completion/spec/output.json
const char* kChat =
"{\"id\":\"cmpl-1\",\"model\":\"openai/gpt-oss-20b\",\"created\":1,\"object\":\"chat.completion\","
"\"choices\":[{\"index\":0,\"finish_reason\":\"stop\",\"message\":{\"role\":\"assistant\",\"content\":\"Hello!\","
"\"reasoning\":\"greeting\"}}],\"usage\":{\"prompt_tokens\":9,\"completion_tokens\":3,\"total_tokens\":12,"
"\"completion_tokens_details\":{\"reasoning_tokens\":2}}}";
const char* kToolReply =
"{\"id\":\"cmpl-2\",\"model\":\"m\",\"choices\":[{\"index\":0,\"finish_reason\":\"tool_calls\",\"message\":{\"role\":\"assistant\","
"\"content\":null,\"tool_calls\":[{\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"get_weather\","
"\"arguments\":\"{\\\"city\\\":\\\"Paris\\\"}\"}},{\"id\":\"call_2\",\"type\":\"function\",\"function\":{\"name\":\"f\","
"\"arguments\":\"{broken\"}}]}}],\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":1,\"total_tokens\":2}}";
// SHAPE: chat.completion.chunk stream, as OpenAI-compatible providers send it
const char* kStream =
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"He\"}}]}\n\n"
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"llo\"}}]}\n\n"
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"function\":{\"name\":\"f\",\"arguments\":\"{\\\"a\\\"\"}}]}}]}\n\n"
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\":1}\"}}]}}]}\n\n"
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n"
"data: {\"id\":\"c\",\"choices\":[],\"usage\":{\"prompt_tokens\":4,\"completion_tokens\":2,\"total_tokens\":6}}\n\n"
"data: [DONE]\n\n";
// RECORDED 2026-10-25: GET https://router.huggingface.co/v1/models (first entry, 2 of its providers)
const char* kRouterModels =
"{\"object\":\"list\",\"data\":[{\"id\":\"deepseek-ai/DeepSeek-V4.1-Flash-1017\",\"object\":\"model\",\"created\":1792204838,"
"\"owned_by\":\"deepseek-ai\",\"providers\":[{\"provider\":\"novita\",\"status\":\"live\",\"context_length\":1048576,"
"\"supports_tools\":true}]}]}";
struct FakeEndpoint : ChatEndpoint {
Json last_body;
std::string reply_json;
std::string stream_text;
Json create(const Json& body) override {
last_body = body;
return Json::parse(reply_json);
}
void stream(const Json& body, const molo::llm::SseDecoder::Sink& sink) override {
last_body = body;
molo::llm::SseDecoder dec("huggingface", sink);
dec.feed(stream_text);
dec.finish();
}
};
} // namespace
TEST(huggingface_router, model_selectors) {
RouterModel m = RouterModel::parse("openai/gpt-oss-20b:fastest");
ASSERT_EQ(m.id, std::string("openai/gpt-oss-20b"));
ASSERT_EQ(m.selector, std::string("fastest"));
ASSERT_EQ(m.str(), std::string("openai/gpt-oss-20b:fastest"));
ASSERT_EQ(RouterModel::parse("a/b").selector, std::string(""));
ASSERT_THROW(RouterModel::parse("a/b:x:y"), ApiError);
ASSERT_THROW(RouterModel::parse("a/b:"), ApiError) ;
ASSERT_THROW(RouterModel::parse("a b"), ApiError);
ASSERT_THROW(RouterModel::parse(""), ApiError);
}
TEST(huggingface_router, chat_request_and_bill_to_header) {
Rig rig;
RouterChat chat = RouterChat::from_platform(rig.platform);
chat.set_bill_to("acme");
rig.router->expect_response(Method::kPost, "/v1/chat/completions", reply(kChat));
Json body = Json::object();
body.set("model", Json("openai/gpt-oss-20b:cheapest"));
Json msgs = Json::array();
Json m = Json::object();
m.set("role", Json("user"));
m.set("content", Json("hi"));
msgs.push(m);
body.set("messages", msgs);
Json out = chat.create(body);
ASSERT_EQ(out.find("id")->as_string(), std::string("cmpl-1"));
auto seen = rig.router->seen();
ASSERT_EQ(Rig::header(seen[0], "X-HF-Bill-To"), std::string("acme"));
ASSERT_EQ(Rig::header(seen[0], "Authorization"), std::string("Bearer ") + kToken);
ASSERT_THROW(chat.set_bill_to("a b"), ApiError);
}
TEST(huggingface_router, chat_refuses_bad_bodies_before_sending) {
Rig rig;
RouterChat chat = RouterChat::from_platform(rig.platform);
Json no_model = Json::object();
ASSERT_THROW(chat.create(no_model), ApiError);
Json unknown = Json::object();
unknown.set("model", Json("a/b"));
unknown.set("provider_secret_sauce", Json(1));
ASSERT_THROW(chat.create(unknown), ApiError);
Json streaming = Json::object();
streaming.set("model", Json("a/b"));
streaming.set("stream", Json(true));
ASSERT_THROW(chat.create(streaming), ApiError);
ASSERT_EQ(rig.router->send_count(), static_cast<std::size_t>(0));
}
TEST(huggingface_router, chat_without_token_is_not_configured) {
Rig rig("");
RouterChat chat = RouterChat::from_platform(rig.platform);
Json b = Json::object();
b.set("model", Json("a/b"));
try {
chat.create(b);
FAIL("expected NOT_CONFIGURED");
} catch (const ApiError& e) {
ASSERT_EQ(e.type(), std::string("NOT_CONFIGURED"));
}
ASSERT_EQ(rig.router->send_count(), static_cast<std::size_t>(0));
}
TEST(huggingface_router, models_list_is_public) {
Rig rig("");
RouterChat chat = RouterChat::from_platform(rig.platform);
rig.router->expect_response(Method::kGet, "/v1/models", reply(kRouterModels));
Json m = chat.models();
ASSERT_EQ(m.find("data")->as_array().front().find("owned_by")->as_string(), std::string("deepseek-ai"));
ASSERT_EQ(Rig::header(rig.router->seen()[0], "Authorization"), std::string(""));
}
TEST(huggingface_tasks, catalog_matches_the_hf_inference_map) {
ASSERT_EQ(TaskCatalog::count(), static_cast<std::size_t>(25));
ASSERT_TRUE(TaskCatalog::find("feature-extraction")->pipeline_path);
ASSERT_TRUE(TaskCatalog::find("sentence-similarity")->pipeline_path);
ASSERT_TRUE(TaskCatalog::find("image-classification")->binary_input);
ASSERT_FALSE(TaskCatalog::find("translation")->binary_input);
ASSERT_TRUE(TaskCatalog::find("conversational") == nullptr); // chat has its own surface
}
TEST(huggingface_tasks, json_task_request) {
Rig rig;
InferenceTasks t = InferenceTasks::from_platform(rig.platform);
rig.router->expect_response(Method::kPost, "/hf-inference/models/distilbert/distilbert-base-uncased-finetuned-sst-2-english",
reply("[[{\"label\":\"POSITIVE\",\"score\":0.99}]]"));
Json params = Json::object();
params.set("top_k", Json(1));
InferenceResult r = t.run_json("text-classification", "distilbert/distilbert-base-uncased-finetuned-sst-2-english",
Json("I like you"), params);
ASSERT_TRUE(r.is_json);
ASSERT_EQ(rig.router->seen()[0].body, std::string("{\"inputs\":\"I like you\",\"parameters\":{\"top_k\":1}}"));
}
TEST(huggingface_tasks, pipeline_tasks_put_parameters_at_the_top_level) {
Rig rig;
InferenceTasks t = InferenceTasks::from_platform(rig.platform);
rig.router->expect_response(Method::kPost, "/hf-inference/models/BAAI/bge-small-en-v1.5/pipeline/feature-extraction",
reply("[[0.1,0.2]]"));
Json e = t.embed("BAAI/bge-small-en-v1.5", {"hello"}, true);
ASSERT_TRUE(e.is_array());
ASSERT_EQ(rig.router->seen()[0].body, std::string("{\"inputs\":\"hello\",\"normalize\":true}"));
ASSERT_THROW(t.embed("BAAI/bge-small-en-v1.5", {}), ApiError);
ASSERT_THROW(t.embed("BAAI/bge-small-en-v1.5", std::vector<std::string>(257, "x")), ApiError);
}
TEST(huggingface_tasks, binary_tasks_send_raw_bytes_or_base64_with_parameters) {
Rig rig;
InferenceTasks t = InferenceTasks::from_platform(rig.platform);
rig.router->expect_response(Method::kPost, "/hf-inference/models/google/vit-base-patch16-224", reply("[{\"label\":\"cat\",\"score\":0.9}]"));
rig.router->expect_response(Method::kPost, "/hf-inference/models/google/vit-base-patch16-224", reply("[]"));
t.run_binary("image-classification", "google/vit-base-patch16-224", "\x89PNG", "image/png");
auto first = rig.router->seen()[0];
ASSERT_EQ(first.body, std::string("\x89PNG"));
ASSERT_EQ(Rig::header(first, "Content-Type"), std::string("image/png"));
Json p = Json::object();
p.set("top_k", Json(2));
t.run_binary("image-classification", "google/vit-base-patch16-224", "abc", "image/png", p);
ASSERT_EQ(rig.router->seen()[1].body, std::string("{\"inputs\":\"YWJj\",\"parameters\":{\"top_k\":2}}"));
ASSERT_THROW(t.run_json("image-classification", "m", Json("x")), ApiError);
ASSERT_THROW(t.run_binary("translation", "m", "x", "text/plain"), ApiError);
ASSERT_THROW(t.run_binary("image-classification", "m", "", "image/png"), ApiError);
ASSERT_THROW(t.run_json("no-such-task", "m", Json("x")), ApiError);
}
TEST(huggingface_tasks, binary_replies_are_returned_as_bytes) {
Rig rig;
InferenceTasks t = InferenceTasks::from_platform(rig.platform);
rig.router->expect_response(Method::kPost, "/hf-inference/models/black-forest-labs/FLUX.1-dev",
reply("\x89PNG\r\n", 200, "image/png"));
InferenceResult r = t.run_json("text-to-image", "black-forest-labs/FLUX.1-dev", Json("a cat"));
ASSERT_FALSE(r.is_json);
ASSERT_EQ(r.content_type, std::string("image/png"));
ASSERT_EQ(r.bytes.size(), static_cast<std::size_t>(6));
}
TEST(huggingface_chat, neutral_request_maps_to_the_wire_body) {
molo::llm::ChatRequest req;
req.model = "openai/gpt-oss-20b:fastest";
req.system = "be brief";
req.messages.push_back(molo::llm::Message::user("hi"));
req.max_tokens = 32;
req.temperature = 0.5;
req.stop = {"END"};
req.json_mode = true;
req.reasoning_effort = "low";
molo::llm::ToolSpec tool;
tool.name = "get_weather";
tool.description = "weather";
req.tools.push_back(tool);
req.tool_choice = "get_weather";
Json b = ChatWire::to_body(req, "ignored", false);
ASSERT_EQ(b.find("model")->as_string(), std::string("openai/gpt-oss-20b:fastest"));
ASSERT_EQ(b.find("messages")->as_array().size(), static_cast<std::size_t>(2));
ASSERT_EQ(b.find("messages")->as_array()[0].find("role")->as_string(), std::string("system"));
ASSERT_EQ(b.find("response_format")->find("type")->as_string(), std::string("json_object"));
ASSERT_EQ(b.find("tool_choice")->find("function")->find("name")->as_string(), std::string("get_weather"));
ASSERT_EQ(b.find("reasoning_effort")->as_string(), std::string("low"));
ASSERT_TRUE(b.find("stream") == nullptr);
Json s = ChatWire::to_body(req, "x", true);
ASSERT_TRUE(s.find("stream")->as_bool());
ASSERT_TRUE(s.find("stream_options")->find("include_usage")->as_bool());
}
TEST(huggingface_chat, requests_are_validated) {
molo::llm::ChatRequest req;
req.messages.push_back(molo::llm::Message::user("hi"));
ASSERT_THROW(ChatWire::to_body(req, "", false), ApiError); // no model anywhere
req.model = "a/b";
req.stop = {"1", "2", "3", "4", "5"};
ASSERT_THROW(ChatWire::to_body(req, "", false), ApiError);
req.stop.clear();
req.json_mode = true;
req.response_schema = Json::object();
ASSERT_THROW(ChatWire::to_body(req, "", false), ApiError);
req.json_mode = false;
req.response_schema = Json();
req.temperature = 2.5;
ASSERT_THROW(ChatWire::to_body(req, "", false), ApiError);
req.temperature.reset();
req.max_tokens = 0;
ASSERT_THROW(ChatWire::to_body(req, "", false), ApiError);
req.max_tokens.reset();
req.extra = Json::object();
req.extra.set("seed", Json(7));
ASSERT_TRUE(ChatWire::to_body(req, "", false).find("seed") != nullptr);
molo::llm::ChatRequest empty;
empty.model = "a/b";
ASSERT_THROW(ChatWire::to_body(empty, "", false), ApiError); // no messages
}
TEST(huggingface_chat, response_parsing_with_reasoning_and_usage) {
molo::llm::ChatResponse r = ChatWire::to_response(Json::parse(kChat));
ASSERT_EQ(r.text(), std::string("Hello!"));
ASSERT_EQ(r.finish_reason, std::string("stop"));
ASSERT_EQ(r.usage.input_tokens, 9);
ASSERT_EQ(r.usage.output_tokens, 3);
ASSERT_EQ(r.usage.reasoning_tokens, 2);
ASSERT_EQ(r.message.parts.front().reasoning, std::string("greeting"));
ASSERT_THROW(ChatWire::to_response(Json::parse("{\"choices\":[]}")), ApiError);
}
TEST(huggingface_chat, tool_calls_keep_invalid_json_visible) {
molo::llm::ChatResponse r = ChatWire::to_response(Json::parse(kToolReply));
std::vector<molo::llm::ToolCall> calls = r.tool_calls();
ASSERT_EQ(calls.size(), static_cast<std::size_t>(2));
ASSERT_EQ(calls[0].name, std::string("get_weather"));
ASSERT_EQ(calls[0].arguments.find("city")->as_string(), std::string("Paris"));
ASSERT_EQ(calls[1].arguments.find("_invalid_json")->as_string(), std::string("{broken"));
ASSERT_EQ(r.finish_reason, std::string("tool_calls"));
}
TEST(huggingface_chat, model_over_a_fake_endpoint) {
auto ep = std::make_shared<FakeEndpoint>();
ep->reply_json = kChat;
HuggingfaceChatModel model(ep, "openai/gpt-oss-20b");
molo::llm::ChatRequest req;
req.messages.push_back(molo::llm::Message::user("hi"));
molo::llm::ChatResponse r = model.generate(req);
ASSERT_EQ(r.text(), std::string("Hello!"));
ASSERT_EQ(ep->last_body.find("model")->as_string(), std::string("openai/gpt-oss-20b"));
ASSERT_EQ(model.vendor(), std::string("huggingface"));
ASSERT_THROW(HuggingfaceChatModel(nullptr, "m"), ApiError);
}
TEST(huggingface_chat, stream_transcript_and_early_stop) {
auto ep = std::make_shared<FakeEndpoint>();
ep->stream_text = kStream;
HuggingfaceChatModel model(ep, "a/b");
molo::llm::ChatRequest req;
req.messages.push_back(molo::llm::Message::user("hi"));
std::string text, args, id, finish;
int total = 0;
model.stream(req, [&](const molo::llm::ChatChunk& c) {
text += c.text_delta;
args += c.tool_args_delta;
if (!c.tool_call_id.empty()) id = c.tool_call_id;
if (!c.finish_reason.empty()) finish = c.finish_reason;
if (c.usage) total = c.usage->total_tokens;
return true;
});
ASSERT_EQ(text, std::string("Hello"));
ASSERT_EQ(args, std::string("{\"a\":1}"));
ASSERT_EQ(id, std::string("call_1"));
ASSERT_EQ(finish, std::string("stop"));
ASSERT_EQ(total, 6);
ASSERT_TRUE(ep->last_body.find("stream")->as_bool());
int seen = 0;
model.stream(req, [&](const molo::llm::ChatChunk&) { return ++seen < 2; });
ASSERT_EQ(seen, 2);
}
TEST(huggingface_chat, error_event_in_stream_throws) {
auto ep = std::make_shared<FakeEndpoint>();
ep->stream_text = "data: {\"error\":{\"message\":\"provider failed\"}}\n\n";
HuggingfaceChatModel model(ep, "a/b");
molo::llm::ChatRequest req;
req.messages.push_back(molo::llm::Message::user("hi"));
ASSERT_THROW(model.stream(req, [](const molo::llm::ChatChunk&) { return true; }), ApiError);
}
TEST(huggingface_chat, router_model_streams_over_the_real_client) {
Rig rig;
rig.router->expect_response(Method::kPost, "/v1/chat/completions", sse(kStream));
auto model = HuggingfaceChatModel::over_router(rig.platform, "a/b");
molo::llm::ChatRequest req;
req.messages.push_back(molo::llm::Message::user("hi"));
std::string text;
model->stream(req, [&](const molo::llm::ChatChunk& c) {
text += c.text_delta;
return true;
});
ASSERT_EQ(text, std::string("Hello"));
ASSERT_TRUE(has(rig.router->seen()[0].body, "\"stream\":true"));
}
+200
View File
@@ -0,0 +1,200 @@
// Inference Endpoints management, TGI and TEI. Requests are checked against
// the call table; replies are SHAPE or RECORDED.
#include "support.h"
#include "vendors/huggingface/endpoints.h"
#include "vendors/huggingface/servers.h"
using namespace molo::huggingface;
using namespace hf_test;
using molo::llm::ApiError;
namespace {
// RECORDED 2026-10-25: GET https://api.endpoints.huggingface.cloud/v2/provider (no token), trimmed
const char* kVendors =
"{\"vendors\":[{\"name\":\"aws\",\"status\":\"available\",\"regions\":[{\"name\":\"eu-west-1\",\"label\":\"Ireland\","
"\"status\":\"available\",\"computes\":[{\"id\":\"aws-eu-west-1-intel-spr-x1\",\"accelerator\":\"cpu\","
"\"instanceType\":\"intel-spr\",\"instanceSize\":\"x1\",\"status\":\"available\",\"pricePerHour\":0.033}]}]}]}";
// RECORDED 2026-10-25: any call without a valid token -> 401
const char* kIeUnauth = "{\"error\":\"401 Unauthorized\",\"code\":\"UNAUTHORIZED\"}";
Json spec() {
return Json::parse(
"{\"name\":\"my-ep\",\"type\":\"protected\",\"provider\":{\"vendor\":\"aws\",\"region\":\"us-east-1\"},"
"\"compute\":{},\"model\":{}}");
}
} // namespace
TEST(huggingface_endpoints, vendors_list_is_public_everything_else_needs_a_token) {
Rig rig("");
Endpoints ie = Endpoints::from_platform(rig.platform);
rig.endpoints->expect_response(Method::kGet, "/v2/provider", reply(kVendors));
Json v = ie.vendors();
ASSERT_EQ(v.find("vendors")->as_array().front().find("name")->as_string(), std::string("aws"));
try {
ie.list("acme");
FAIL("expected NOT_CONFIGURED");
} catch (const ApiError& e) {
ASSERT_EQ(e.type(), std::string("NOT_CONFIGURED"));
}
ASSERT_EQ(rig.endpoints->send_count(), static_cast<std::size_t>(1));
}
TEST(huggingface_endpoints, error_code_is_kept) {
Rig rig;
Endpoints ie = Endpoints::from_platform(rig.platform);
rig.endpoints->expect_response(Method::kGet, "/v2/endpoint/huggingface", reply(kIeUnauth, 401));
try {
ie.list("huggingface");
FAIL("expected 401");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 401);
ASSERT_TRUE(has(e.message(), "401 Unauthorized"));
}
}
TEST(huggingface_endpoints, reads_build_the_documented_paths) {
Rig rig;
Endpoints ie = Endpoints::from_platform(rig.platform);
rig.endpoints->expect_response(Method::kGet, "/v2/endpoint/acme", reply("{\"items\":[]}"));
rig.endpoints->expect_response(Method::kGet, "/v2/endpoint/acme/ep", reply("{}"));
rig.endpoints->expect_response(Method::kGet, "/v2/endpoint/acme/ep/replica", reply("{}"));
rig.endpoints->expect_response(Method::kGet, "/v2/endpoint/acme/ep/logs", reply("line1\nline2\n", 200, "text/plain"));
rig.endpoints->expect_response(Method::kGet, "/v2/search/acme/states", reply("{}"));
EndpointListParams p;
p.limit = 5;
p.state = "running";
ie.list("acme", p);
ASSERT_EQ(rig.endpoints->seen().back().target, std::string("/v2/endpoint/acme?state=running&limit=5"));
ie.get("acme", "ep");
ie.replicas("acme", "ep");
ASSERT_EQ(ie.logs("acme", "ep", 50), std::string("line1\nline2\n"));
ASSERT_EQ(rig.endpoints->seen().back().target, std::string("/v2/endpoint/acme/ep/logs?tail=50"));
ie.state_counts("acme");
ASSERT_THROW(ie.get("acme", "../x"), ApiError);
ASSERT_THROW(ie.list("acme", EndpointListParams{"", "", "", "", "", "", 101}), ApiError);
}
TEST(huggingface_endpoints, metrics_use_post_with_a_time_window) {
Rig rig;
Endpoints ie = Endpoints::from_platform(rig.platform);
rig.endpoints->expect_response(Method::kPost, "/v2/endpoint/acme/ep/metrics/inference/ttft", reply("{}"));
ie.metrics(MetricKind::kTtft, "acme", "ep", "2021-01-01T00:00:00Z", "2021-01-02T00:00:00Z", "r1");
ASSERT_EQ(rig.endpoints->seen().back().body,
std::string("{\"replicaId\":\"r1\",\"start\":\"2021-01-01T00:00:00Z\",\"stop\":\"2021-01-02T00:00:00Z\"}"));
ASSERT_THROW(ie.metrics(MetricKind::kTtft, "acme", "ep", "yesterday", "today"), ApiError);
MetricKind k;
ASSERT_TRUE(parse_metric_kind("kv-cache", k));
ASSERT_FALSE(parse_metric_kind("nope", k));
}
TEST(huggingface_endpoints, writes_are_validated_and_explicit) {
Rig rig;
Endpoints ie = Endpoints::from_platform(rig.platform);
ASSERT_THROW(ie.create("acme", spec()), ApiError); // type "protected" is not a type
Json good = spec();
good.set("type", Json("private"));
rig.endpoints->expect_response(Method::kPost, "/v2/endpoint/acme", reply("{}"));
ie.create("acme", good);
Json bad_name = good;
bad_name.set("name", Json("My_Endpoint"));
ASSERT_THROW(ie.create("acme", bad_name), ApiError);
Json missing = Json::object();
missing.set("name", Json("x"));
ASSERT_THROW(ie.create("acme", missing), ApiError);
rig.endpoints->expect_response(Method::kPost, "/v2/endpoint/acme/ep/pause", reply("{}"));
rig.endpoints->expect_response(Method::kPost, "/v2/endpoint/acme/ep/resume", reply("{}"));
rig.endpoints->expect_response(Method::kPost, "/v2/endpoint/acme/ep/scale-to-zero", reply("{}"));
rig.endpoints->expect_response(Method::kDelete, "/v2/endpoint/acme/ep", reply("{}"));
ie.pause("acme", "ep");
ie.resume("acme", "ep");
ie.scale_to_zero("acme", "ep");
ie.remove_endpoint("acme", "ep");
rig.endpoints->verify();
}
TEST(huggingface_endpoints, sse_streams_are_listed_but_refused) {
Rig rig;
Endpoints ie = Endpoints::from_platform(rig.platform);
ASSERT_EQ(rig.endpoints->send_count(), static_cast<std::size_t>(0));
for (std::size_t i = 0; i < Calls::count(); ++i)
if (Calls::at(i).has_flag("unsupported")) ASSERT_TRUE(std::string(Calls::at(i).path).find("sse") != std::string::npos ||
std::string(Calls::at(i).path).find("/sse") != std::string::npos);
}
TEST(huggingface_tgi, native_and_openai_routes) {
Rig rig; // transports unused: the server has its own
auto mock = std::make_unique<MockTransport>();
MockTransport* t = mock.get();
TgiServer tgi(Platform::server_with(std::move(mock), "http://127.0.0.1:8080/", ""));
ASSERT_FALSE(tgi.has_token());
t->expect_response(Method::kGet, "/health", reply("", 200, "text/plain"));
MockExpectation starting; // the client retries a 503 GET, so allow it to answer more than once
starting.method = Method::kGet;
starting.path = "/health";
starting.response = reply("starting", 503, "text/plain");
starting.times = 5;
t->expect(starting);
ASSERT_TRUE(tgi.ready());
ASSERT_FALSE(tgi.ready());
t->expect_response(Method::kPost, "/generate", reply("{\"generated_text\":\"hi\"}"));
Json params = Json::object();
params.set("max_new_tokens", Json(8));
tgi.generate("Hello", params);
ASSERT_EQ(t->seen().back().body, std::string("{\"inputs\":\"Hello\",\"parameters\":{\"max_new_tokens\":8}}"));
ASSERT_EQ(Rig::header(t->seen().back(), "Authorization"), std::string(""));
ASSERT_THROW(tgi.generate("", params), ApiError);
Json bad = Json::object();
bad.set("max_new_tokens", Json(8));
ASSERT_THROW(tgi.completions(bad), ApiError); // body needs a table field (prompt is optional, max_new_tokens is not a field)
}
TEST(huggingface_tgi, generate_stream_decodes_token_events) {
auto mock = std::make_unique<MockTransport>();
MockTransport* t = mock.get();
TgiServer tgi(Platform::server_with(std::move(mock), "http://tgi.local:8080", "hf_tok"));
ASSERT_TRUE(tgi.has_token());
// SHAPE: TGI streams one StreamResponse per `data:` line
t->expect_response(Method::kPost, "/generate_stream",
sse("data:{\"index\":1,\"token\":{\"id\":1,\"text\":\"a\",\"logprob\":-0.1,\"special\":false},\"generated_text\":null}\n\n"
"data:{\"index\":2,\"token\":{\"id\":2,\"text\":\"b\",\"logprob\":-0.2,\"special\":false},\"generated_text\":\"ab\"}\n\n"));
std::string text;
tgi.generate_stream("x", Json(), [&](const molo::llm::SseEvent& e) {
text += e.data.find("token")->find("text")->as_string();
return true;
});
ASSERT_EQ(text, std::string("ab"));
ASSERT_EQ(Rig::header(t->seen().back(), "Authorization"), std::string("Bearer hf_tok"));
}
TEST(huggingface_tei, embed_rerank_and_validation) {
auto mock = std::make_unique<MockTransport>();
MockTransport* t = mock.get();
TeiServer tei(Platform::server_with(std::move(mock), "http://127.0.0.1:8081", ""));
t->expect_response(Method::kPost, "/embed", reply("[[0.1,0.2]]"));
Json opts = Json::object();
opts.set("normalize", Json(true));
tei.embed(Json("hello"), opts);
ASSERT_EQ(t->seen().back().body, std::string("{\"inputs\":\"hello\",\"normalize\":true}"));
t->expect_response(Method::kPost, "/rerank", reply("[{\"index\":0,\"score\":0.9}]"));
tei.rerank("q", {"a", "b"}, true);
ASSERT_EQ(t->seen().back().body, std::string("{\"query\":\"q\",\"return_text\":true,\"texts\":[\"a\",\"b\"]}"));
ASSERT_THROW(tei.embed(Json(""), Json()), ApiError);
ASSERT_THROW(tei.embed(Json(7), Json()), ApiError);
ASSERT_THROW(tei.rerank("q", {}), ApiError);
Json many = Json::array();
for (int i = 0; i < 257; ++i) many.push(Json("x"));
ASSERT_THROW(tei.embed(many, Json()), ApiError);
Json unknown = Json::object();
unknown.set("hack", Json(1));
ASSERT_THROW(tei.embed(Json("x"), unknown), ApiError);
ASSERT_THROW(tei.decode({}, true), ApiError);
ASSERT_THROW(tei.openai_embeddings(Json("x"), "", -1), ApiError);
}
TEST(huggingface_servers, call_from_the_wrong_surface_is_refused) {
auto mock = std::make_unique<MockTransport>();
MockTransport* t = mock.get();
TeiServer tei(Platform::server_with(std::move(mock), "http://127.0.0.1:8081", ""));
ASSERT_EQ(t->send_count(), static_cast<std::size_t>(0));
ASSERT_TRUE(tei.surface() == Surface::kTei);
}