Add unit tests for OpenAI plugin and establish coding standards

- Introduced comprehensive unit tests for the OpenAI plugin, covering SSE parsing, sentinel matching, delta extraction, request building, and more.
- Created a new markdown file detailing coding and naming conventions for the dstalk project, including guidelines for comments, naming rules, code organization, and memory management practices.
This commit is contained in:
2026-05-31 00:51:59 +08:00
parent f2da0f2ed4
commit f6cb51b40a
21 changed files with 343 additions and 131 deletions

View File

@@ -52,10 +52,10 @@ int main()
{
std::ofstream config(config_path);
config << "[api]\n"
<< "provider = \"deepseek\"\n"
<< "base_url = \"https://api.deepseek.com/v1\"\n"
<< "provider = \"openai\"\n"
<< "base_url = \"https://api.openai.com/v1\"\n"
<< "api_key = \"test-key\"\n"
<< "model = \"deepseek-v4-pro\"\n";
<< "model = \"gpt-4o\"\n";
}
// 初始化主机(会自动扫描 plugins/ 加载插件)/ Init host (auto-scans plugins/ to load plugins)
@@ -186,7 +186,7 @@ int main()
// 测试服务查询: ai可能因为没有真实 API key 而失败,但服务应存在)
// Test service query: ai (may fail without real API key, but service should exist)
const char* ai_provider = dstalk_config_get("ai.provider");
if (!ai_provider) ai_provider = "ai.deepseek";
if (!ai_provider) ai_provider = "ai.openai";
auto* ai = static_cast<const dstalk_ai_service_t*>(
dstalk_service_query(ai_provider, 1));
if (ai) {
@@ -598,12 +598,12 @@ int main()
std::cout << "[OK] R3: http error path, no response body (connection refused)\n";
}
} else {
// 回退:测 AI 服务 (ai.deepseek) 错误路径
// Fallback: test AI service (ai.deepseek) error path
// 回退:测 AI 服务 (ai.openai) 错误路径
// Fallback: test AI service (ai.openai) error path
auto* ai_svc = static_cast<const dstalk_ai_service_t*>(
dstalk_service_query("ai.deepseek", 1));
dstalk_service_query("ai.openai", 1));
if (ai_svc) {
std::cout << "[OK] R3: ai.deepseek service found (http fallback)\n";
std::cout << "[OK] R3: ai.openai service found (http fallback)\n";
dstalk_message_t msg = {"user", "hi", nullptr, nullptr};
dstalk_chat_result_t r = ai_svc->chat(&msg, 1, "", nullptr);
// api_key="test-key" 为无效 key应返回 error result 而非崩溃
@@ -748,10 +748,10 @@ int main()
{
std::ofstream c(config_path);
c << "[api]\n"
<< "provider = \"deepseek\"\n"
<< "base_url = \"https://api.deepseek.com/v1\"\n"
<< "provider = \"openai\"\n"
<< "base_url = \"https://api.openai.com/v1\"\n"
<< "api_key = \"test-key\"\n"
<< "model = \"deepseek-v4-pro\"\n";
<< "model = \"gpt-4o\"\n";
}
std::cout << "[R4] cycle " << (i + 1) << "/" << cycles << "\n";