Skip to content
1 change: 1 addition & 0 deletions tests/xllm_service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_subdirectory(chat_template)
add_subdirectory(common)
add_subdirectory(http_service)
add_subdirectory(scheduler)
add_subdirectory(tokenizer)
30 changes: 30 additions & 0 deletions tests/xllm_service/chat_template/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,33 @@ cc_test(
:message_projection
GTest::gtest_main
)

cc_test(
NAME
model_type_test
SRCS
model_type_test.cpp
DEPS
:model_type
GTest::gtest_main
)

cc_test(
NAME
chat_template_test
SRCS
jinja_chat_template_test.cpp
DEPS
:chat_template
GTest::gtest_main
)

cc_test(
NAME
deepseek_v4_cpp_chat_template_test
SRCS
deepseek_v4_cpp_chat_template_test.cpp
DEPS
:deepseek_v4_cpp_chat_template
GTest::gtest_main
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/* Copyright 2025-2026 The xLLM Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://github.com/jd-opensource/xllm-service/blob/main/LICENSE

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "chat_template/deepseek_v4_cpp_chat_template.h"

#include <gtest/gtest.h>

#include <memory>

#include "chat_template/chat_template.h"
#include "chat_template/jinja_chat_template.h"
#include "tokenizer/tokenizer_args.h"

namespace xllm_service {
namespace {

TokenizerArgs make_v4_args() {
TokenizerArgs args;
args.bos_token("<|begin▁of▁sentence|>");
return args;
}

TEST(DeepseekV4CppChatTemplate, EncodeDoesNotAddSpecialTokens) {
std::unique_ptr<ChatTemplate> tmpl =
std::make_unique<DeepseekV4CppChatTemplate>(make_v4_args());
EXPECT_FALSE(tmpl->encode_add_special_tokens());
}

TEST(DeepseekV4CppChatTemplate, PlainTextMatchesUpstreamGolden) {
DeepseekV4CppChatTemplate tmpl(make_v4_args());

ChatMessages messages;
messages.emplace_back("system", "You are a helpful assistant.");
messages.emplace_back("user", "Hello");

auto prompt = tmpl.apply(messages, {}, nlohmann::ordered_json::object());
ASSERT_TRUE(prompt.has_value());
EXPECT_EQ(*prompt,
"<|begin▁of▁sentence|>You are a helpful assistant."
"<|User|>Hello<|Assistant|></think>");
}

TEST(DeepseekV4CppChatTemplate, MMContentVecIsFlattened) {
DeepseekV4CppChatTemplate tmpl(make_v4_args());

// Adjacent text blocks are flattened (joined by '\n') for the upstream
// template.
Message::MMContentVec blocks{Message::MMContent("text", "first"),
Message::MMContent("text", "second")};
ChatMessages messages{Message("user", blocks)};

auto prompt = tmpl.apply(messages, {}, nlohmann::ordered_json::object());
ASSERT_TRUE(prompt.has_value());
EXPECT_EQ(*prompt,
"<|begin▁of▁sentence|><|User|>first\nsecond"
"<|Assistant|></think>");
}

TEST(DeepseekV4CppChatTemplate, ToolCallsBlockAndReasoningAreProjected) {
DeepseekV4CppChatTemplate tmpl(make_v4_args());

ChatMessages messages;
messages.emplace_back("user", "weather?");

Message assistant("assistant", "");
Message::ToolCall call;
call.id = "call_001";
call.type = "function";
call.function.name = "get_weather";
call.function.arguments = R"({"location":"Beijing"})";
assistant.tool_calls = Message::ToolCallVec{call};
assistant.reasoning_content = "Need weather data.";
messages.push_back(assistant);

nlohmann::ordered_json kwargs = nlohmann::ordered_json::object();
kwargs["thinking"] = true;
auto prompt = tmpl.apply(messages, {}, kwargs);
ASSERT_TRUE(prompt.has_value());

EXPECT_NE(prompt->find("<|DSML|tool_calls>"), std::string::npos);
EXPECT_NE(prompt->find("Need weather data.</think>"), std::string::npos);
}

TEST(DeepseekV4CppChatTemplate, ToolResultMergesViaToolCallId) {
DeepseekV4CppChatTemplate tmpl(make_v4_args());

ChatMessages messages;
messages.emplace_back("user", "weather?");

Message assistant("assistant", "");
Message::ToolCall call;
call.id = "call_001";
call.type = "function";
call.function.name = "get_weather";
call.function.arguments = R"({"location":"Beijing"})";
assistant.tool_calls = Message::ToolCallVec{call};
messages.push_back(assistant);

// Non-empty tool_call_id drives tool-result merging.
Message tool_msg("tool", R"({"temperature":22})");
tool_msg.tool_call_id = "call_001";
messages.push_back(tool_msg);

auto prompt = tmpl.apply(messages, {}, nlohmann::ordered_json::object());
ASSERT_TRUE(prompt.has_value());

EXPECT_NE(prompt->find("<|User|><tool_result>{\"temperature\":22}"
"</tool_result><|Assistant|></think>"),
std::string::npos);
}

TEST(DeepseekV4CppChatTemplate, ToolsInjectionRendersV4Schema) {
DeepseekV4CppChatTemplate tmpl(make_v4_args());

ChatMessages messages;
messages.emplace_back("user", "weather in beijing");

std::vector<JsonTool> tools;
JsonTool tool;
tool.type = "function";
tool.function.name = "get_weather";
tool.function.description = "query weather";
tool.function.parameters = nlohmann::json{
{"type", "object"}, {"properties", {{"city", {{"type", "string"}}}}}};
tools.push_back(tool);

auto prompt = tmpl.apply(messages, tools, nlohmann::ordered_json::object());
ASSERT_TRUE(prompt.has_value());

EXPECT_NE(prompt->find("### Available Tool Schemas"), std::string::npos);
EXPECT_NE(prompt->find("get_weather"), std::string::npos);
}

TEST(DeepseekV4CppChatTemplate, ThinkingKwargTogglesThinkBlock) {
DeepseekV4CppChatTemplate tmpl(make_v4_args());

ChatMessages messages{Message("user", "Hello")};

nlohmann::ordered_json kwargs = nlohmann::ordered_json::object();
kwargs["thinking"] = true;
auto thinking_on = tmpl.apply(messages, {}, kwargs);
ASSERT_TRUE(thinking_on.has_value());
EXPECT_EQ(*thinking_on,
"<|begin▁of▁sentence|><|User|>Hello<|Assistant|><think>");

// Default (no kwarg) closes thinking immediately.
auto thinking_off =
tmpl.apply(messages, {}, nlohmann::ordered_json::object());
ASSERT_TRUE(thinking_off.has_value());
EXPECT_EQ(*thinking_off,
"<|begin▁of▁sentence|><|User|>Hello<|Assistant|></think>");
}

} // namespace
} // namespace xllm_service
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,27 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "jinja_chat_template.h"
#include "chat_template/jinja_chat_template.h"

#include <gtest/gtest.h>

#include <memory>

#include "chat_template/chat_template.h"

namespace xllm_service {

TEST(JinjaChatTemplate, EncodeAddsSpecialTokensViaBase) {
TokenizerArgs args;
args.chat_template("{{ messages[0]['content'] }}");
args.bos_token("");
args.eos_token("");

std::unique_ptr<ChatTemplate> tmpl =
std::make_unique<JinjaChatTemplate>(args);
EXPECT_TRUE(tmpl->encode_add_special_tokens());
}

TEST(JinjaChatTemplate, OpenChatModel) {
// clang-format off
const std::string template_str =
Expand Down
69 changes: 69 additions & 0 deletions tests/xllm_service/chat_template/model_type_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright 2025-2026 The xLLM Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://github.com/jd-opensource/xllm-service/blob/main/LICENSE

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "chat_template/model_type.h"

#include <gtest/gtest.h>

#include <filesystem>
#include <fstream>

namespace xllm_service {
namespace {

// Writes `content` to config.json in a fresh temp dir and returns that dir.
std::string make_model_dir_with_config(const std::string& content) {
const auto dir = std::filesystem::temp_directory_path() /
("model_type_test_" +
std::to_string(reinterpret_cast<uintptr_t>(&content)) +
std::to_string(std::rand()));
std::filesystem::create_directories(dir);
std::ofstream(dir / "config.json") << content;
return dir.string();
}

TEST(SelectChatTemplateKind, DeepseekV4SelectsCppTemplate) {
EXPECT_EQ(select_chat_template_kind("deepseek_v4"),
ChatTemplateKind::kDeepseekV4Cpp);
}

TEST(SelectChatTemplateKind, UnknownModelTypeFallsBackToJinja) {
EXPECT_EQ(select_chat_template_kind("qwen3"), ChatTemplateKind::kJinja);
}

TEST(SelectChatTemplateKind, MissingModelTypeFallsBackToJinja) {
EXPECT_EQ(select_chat_template_kind(std::nullopt), ChatTemplateKind::kJinja);
}

TEST(LoadModelType, ReadsModelTypeFromConfigJson) {
const auto dir =
make_model_dir_with_config(R"({"model_type": "deepseek_v4"})");
EXPECT_EQ(load_model_type(dir), "deepseek_v4");
}

TEST(LoadModelType, MissingConfigJsonReturnsNullopt) {
const auto dir = std::filesystem::temp_directory_path() /
("model_type_test_missing_" + std::to_string(std::rand()));
std::filesystem::create_directories(dir);
EXPECT_FALSE(load_model_type(dir.string()).has_value());
}

TEST(LoadModelType, ConfigWithoutModelTypeReturnsNullopt) {
const auto dir = make_model_dir_with_config(R"({"hidden_size": 4096})");
EXPECT_FALSE(load_model_type(dir).has_value());
}

} // namespace
} // namespace xllm_service
11 changes: 11 additions & 0 deletions tests/xllm_service/tokenizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include(cc_test)

cc_test(
NAME
tokenizer_test
SRCS
tokenizer_test.cpp
DEPS
:tokenizer
GTest::gtest_main
)
59 changes: 59 additions & 0 deletions tests/xllm_service/tokenizer/tokenizer_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright 2025-2026 The xLLM Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://github.com/jd-opensource/xllm-service/blob/main/LICENSE

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tokenizer/tokenizer.h"

#include <gtest/gtest.h>

namespace xllm_service {
namespace {

// Records the add_special_tokens flag passed to encode.
class RecordingTokenizer : public Tokenizer {
public:
bool encode(const std::string_view& text,
std::vector<int32_t>* ids,
bool add_special_tokens = true) const override {
last_add_special_tokens = add_special_tokens;
return true;
}
std::string decode(const Slice<int32_t>&, bool) const override { return ""; }
std::optional<int32_t> token_to_id(const std::string_view&) const override {
return std::nullopt;
}
std::string id_to_token(int32_t) const override { return ""; }
size_t vocab_size() const override { return 0; }
std::unique_ptr<Tokenizer> clone() const override { return nullptr; }

mutable bool last_add_special_tokens = false;
};

} // namespace

TEST(Tokenizer, EncodeDefaultsToAddingSpecialTokens) {
RecordingTokenizer tokenizer;
std::vector<int32_t> ids;
tokenizer.encode("hi", &ids);
EXPECT_TRUE(tokenizer.last_add_special_tokens);
}

TEST(Tokenizer, EncodeHonorsExplicitFalse) {
RecordingTokenizer tokenizer;
std::vector<int32_t> ids;
tokenizer.encode("hi", &ids, /*add_special_tokens=*/false);
EXPECT_FALSE(tokenizer.last_add_special_tokens);
}

} // namespace xllm_service
Loading
Loading