Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tests/xllm_service/common/call_data_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
#include <google/protobuf/util/json_util.h>
#include <gtest/gtest.h>

#include <nlohmann/json.hpp>
#include <string>
#include <vector>

Expand Down Expand Up @@ -96,6 +97,59 @@ TEST(CallDataTest, NonStreamFinishWithErrorMarksControllerFailed) {
EXPECT_TRUE(done.ran);
}

TEST(CallDataTest, NativeErrorsPreserveHttpStatusAndEscapeMessages) {
for (const auto& [code, expected_http] :
{std::pair{llm::StatusCode::INVALID_ARGUMENT, 400},
std::pair{llm::StatusCode::RESOURCE_EXHAUSTED, 429},
std::pair{llm::StatusCode::UNAVAILABLE, 503}}) {
brpc::Controller controller;
xllm::proto::ChatRequest request;
xllm::proto::ChatResponse response;
TestClosure done;
const std::string message = "invalid \"参数\"\nretry later";
{
ChatCallData call_data(&controller,
/*stream=*/false,
&done,
&request,
&response);
ASSERT_TRUE(call_data.finish_with_error(llm::Status(code, message)));
EXPECT_FALSE(controller.Failed());
EXPECT_EQ(controller.http_response().status_code(), expected_http);
EXPECT_EQ(controller.http_response().content_type(), "application/json");
const auto json =
nlohmann::json::parse(controller.response_attachment().to_string());
EXPECT_EQ(json["error"]["message"], message);
EXPECT_EQ(json["error"]["code"], expected_http);
}
EXPECT_TRUE(done.ran);
}
}

TEST(CallDataTest, NativeAnthropicErrorUsesMessagesEnvelope) {
brpc::Controller controller;
xllm::proto::ChatRequest request;
xllm::proto::AnthropicMessagesResponse response;
TestClosure done;
{
AnthropicCallData call_data(&controller,
/*stream=*/false,
&done,
&request,
&response);
ASSERT_TRUE(call_data.finish_with_error(
llm::Status(llm::StatusCode::INVALID_ARGUMENT, "invalid input")));
EXPECT_FALSE(controller.Failed());
EXPECT_EQ(controller.http_response().status_code(), 400);
const auto json =
nlohmann::json::parse(controller.response_attachment().to_string());
EXPECT_EQ(json["type"], "error");
EXPECT_EQ(json["error"]["type"], "invalid_request_error");
EXPECT_EQ(json["error"]["message"], "invalid input");
}
EXPECT_TRUE(done.ran);
}

TEST(CallDataTest, ChatResponseOmitsEmptyToolCalls) {
brpc::Controller controller;
xllm::proto::ChatRequest request;
Expand Down
58 changes: 48 additions & 10 deletions tests/xllm_service/http_service/anthropic_adapter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ xllm::proto::AnthropicMessagesRequest parse_request(const std::string& json) {
return request;
}

bool rejects_unrepresentable_thinking(const std::string& json) {
if (xllm::proto::AnthropicMessagesRequest::descriptor()->FindFieldByName(
"thinking") != nullptr) {
return false;
}
xllm::proto::AnthropicMessagesRequest request;
const auto result = parse_anthropic_json(json, &request);
EXPECT_FALSE(result.ok);
EXPECT_NE(result.error.find("thinking is not supported"), std::string::npos);
return true;
}

const std::string& text_content(const Message& message) {
return std::get<std::string>(message.content);
}
Expand Down Expand Up @@ -134,20 +146,20 @@ TEST(AnthropicAdapterTest, DefaultsMissingTemperatureToOne) {
}

TEST(AnthropicAdapterTest, MapsAdaptiveThinkingAndEffortToTemplateKwargs) {
auto request = parse_request(R"({
const std::string json = R"({
"model": "test-model",
"max_tokens": 8,
"thinking": {"type": "adaptive", "budget_tokens": 1024},
"output_config": {"effort": "high"},
"messages": [
{"role": "user", "content": "hello"}
]
})");
})";
if (rejects_unrepresentable_thinking(json)) {
return;
}
auto request = parse_request(json);

ASSERT_TRUE(request.has_thinking());
EXPECT_EQ(request.thinking().type(), "adaptive");
ASSERT_TRUE(request.thinking().has_budget_tokens());
EXPECT_EQ(request.thinking().budget_tokens(), 1024);
ASSERT_TRUE(request.has_output_config());
ASSERT_TRUE(request.output_config().has_effort());
EXPECT_EQ(request.output_config().effort(), "high");
Expand All @@ -166,14 +178,18 @@ TEST(AnthropicAdapterTest, MapsAdaptiveThinkingAndEffortToTemplateKwargs) {
}

TEST(AnthropicAdapterTest, MapsEnabledThinkingToTemplateKwargs) {
auto request = parse_request(R"({
const std::string json = R"({
"model": "test-model",
"max_tokens": 8,
"thinking": {"type": "enabled"},
"messages": [
{"role": "user", "content": "hello"}
]
})");
})";
if (rejects_unrepresentable_thinking(json)) {
return;
}
auto request = parse_request(json);

xllm::proto::ChatRequest chat_request;
ChatMessages messages;
Expand Down Expand Up @@ -209,15 +225,37 @@ TEST(AnthropicAdapterTest, PreservesMaxEffortWithoutEnablingThinking) {
EXPECT_FALSE(kwargs.contains("thinking"));
}

TEST(AnthropicAdapterTest, PreservesExplicitThinkingTemplateKwargs) {
for (const bool enabled : {false, true}) {
const nlohmann::json json = {
{"model", "test-model"},
{"max_tokens", 8},
{"chat_template_kwargs", {{"enable_thinking", enabled}}},
{"messages", {{{"role", "user"}, {"content", "hello"}}}}};
const auto request = parse_request(json.dump());
xllm::proto::ChatRequest chat_request;
ChatMessages messages;
const auto result = adapt_request(request, &chat_request, &messages);
ASSERT_TRUE(result.ok) << result.error;
const auto& kwargs = chat_request.chat_template_kwargs().fields();
ASSERT_TRUE(kwargs.contains("enable_thinking"));
EXPECT_EQ(kwargs.at("enable_thinking").bool_value(), enabled);
}
}

TEST(AnthropicAdapterTest, OmitsKwargsForUnsupportedThinkingWithoutEffort) {
auto request = parse_request(R"({
const std::string json = R"({
"model": "test-model",
"max_tokens": 8,
"thinking": {"type": "disabled"},
"messages": [
{"role": "user", "content": "hello"}
]
})");
})";
if (rejects_unrepresentable_thinking(json)) {
return;
}
auto request = parse_request(json);

xllm::proto::ChatRequest chat_request;
ChatMessages messages;
Expand Down
40 changes: 40 additions & 0 deletions tests/xllm_service/rpc_service/rpc_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,45 @@ TEST(DisaggGenerationAdapterTest, ConvertsCompleteValidGeneration) {
EXPECT_FALSE(converted_logprob.top_logprobs->front().finished_token);
}

TEST(DisaggGenerationAdapterTest, DistinguishesBufferedFramesFromLegacyOutput) {
auto generation = make_generation(8, 2, 10, 4);
auto legacy = request_output_from_disagg_generation(generation);
ASSERT_TRUE(legacy.output.has_value());
EXPECT_FALSE(legacy.output->chat_frames.has_value());
generation.mutable_chat_output();
auto buffered = request_output_from_disagg_generation(generation);
ASSERT_TRUE(buffered.output.has_value());
ASSERT_TRUE(buffered.output->chat_frames.has_value());
EXPECT_TRUE(buffered.output->chat_frames->empty());
}

TEST(DisaggGenerationAdapterTest,
PreservesStructuredReasoningToolsAndLogprobs) {
auto generation = make_generation(8, 2, 10, 4);
auto* frame = generation.mutable_chat_output()->add_frames();
frame->set_object("chat.completion.chunk");
auto* choice = frame->add_choices();
choice->set_index(2);
choice->set_finish_reason("tool_calls");
choice->mutable_delta()->set_reasoning_content("reason");
auto* tool = choice->mutable_delta()->add_tool_calls();
tool->set_id("call_engine");
tool->set_index(3);
tool->mutable_function()->set_name("weather");
tool->mutable_function()->set_arguments(R"({"city":"Paris"})");
choice->mutable_logprobs()->add_content()->set_logprob(-0.25F);
auto result = request_output_from_disagg_generation(generation);
ASSERT_TRUE(result.output.has_value());
ASSERT_TRUE(result.output->chat_frames.has_value());
ASSERT_EQ(result.output->chat_frames->size(), 1U);
const auto& parsed = result.output->chat_frames->front().choices(0);
EXPECT_EQ(parsed.index(), 2);
EXPECT_EQ(parsed.delta().reasoning_content(), "reason");
EXPECT_EQ(parsed.delta().tool_calls(0).id(), "call_engine");
EXPECT_EQ(parsed.delta().tool_calls(0).index(), 3);
EXPECT_EQ(parsed.finish_reason(), "tool_calls");
EXPECT_FLOAT_EQ(parsed.logprobs().content(0).logprob(), -0.25F);
}

} // namespace
} // namespace xllm_service
36 changes: 33 additions & 3 deletions tests/xllm_service/scheduler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,40 @@ include(cc_test)

cc_test(
NAME
xllm_chat_parse_bridge_test
request_output_test
SRCS
xllm_chat_parse_bridge_test.cpp
request_output_test.cpp
DEPS
:xllm_chat_parse_runtime
:request
GTest::gtest_main
)

cc_test(
NAME
request_metrics_test
SRCS
request_metrics_test.cpp
DEPS
:request
GTest::gtest_main
)

cc_test(
NAME
response_handler_test
SRCS
response_handler_test.cpp
DEPS
:response_handler
GTest::gtest_main
)

cc_test(
NAME
request_admission_test
SRCS
request_admission_test.cpp
DEPS
:scheduler
GTest::gtest_main
)
Loading