| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package main
-
- import (
- "testing"
-
- "git.x2erp.com/qdy/go-svc-code/internal/opencode"
- )
-
- // TestMockClient 测试模拟客户端的基本功能
- func TestMockClient(t *testing.T) {
- mockClient := opencode.NewMockClient(8080)
-
- // 测试创建会话
- session, err := mockClient.CreateSession(nil, "测试会话")
- if err != nil {
- t.Fatalf("创建会话失败: %v", err)
- }
-
- if session.Title != "测试会话" {
- t.Errorf("预期会话标题为 '测试会话',得到 %s", session.Title)
- }
-
- t.Logf("✓ 创建会话成功: ID=%s", session.ID)
-
- // 测试发送消息
- prompt := &opencode.PromptRequest{
- Parts: []opencode.TextPart{
- {Type: "text", Text: "你好"},
- },
- }
-
- response, err := mockClient.SendPrompt(nil, session.ID, prompt)
- if err != nil {
- t.Fatalf("发送消息失败: %v", err)
- }
-
- if response.Info.Content == "" {
- t.Error("预期响应内容,得到空")
- }
-
- t.Logf("✓ 发送消息成功: %s", response.Info.Content)
-
- // 测试获取会话列表
- sessions, err := mockClient.ListSessions(nil)
- if err != nil {
- t.Fatalf("获取会话列表失败: %v", err)
- }
-
- if len(sessions) != 1 {
- t.Errorf("预期 1 个会话,得到 %d", len(sessions))
- }
-
- t.Logf("✓ 获取会话列表成功: 共 %d 个会话", len(sessions))
- }
-
- // TestProcessFunctions 测试进程相关功能
- func TestProcessFunctions(t *testing.T) {
- // 测试获取可用端口
- port, err := opencode.GetAvailablePort()
- if err != nil {
- t.Fatalf("获取可用端口失败: %v", err)
- }
-
- if port <= 0 || port > 65535 {
- t.Errorf("无效的端口号: %d", port)
- }
-
- t.Logf("✓ 获取可用端口成功: %d", port)
- }
-
- // TestClientInterface 测试客户端接口一致性
- func TestClientInterface(t *testing.T) {
- // 验证 MockClient 实现了 OpenCodeClient 接口
- var _ opencode.OpenCodeClient = opencode.NewMockClient(8080)
- t.Log("✓ MockClient 正确实现了 OpenCodeClient 接口")
- }
-
- // TestAPIEndpoints 测试 API 端点定义
- func TestAPIEndpoints(t *testing.T) {
- endpoints := []struct {
- method string
- path string
- desc string
- }{
- {"POST", "/api/session/create", "创建会话"},
- {"GET", "/api/session/list", "获取会话列表"},
- {"POST", "/api/prompt/sync", "同步对话"},
- {"POST", "/api/prompt/stream", "流式对话"},
- {"GET", "/api/logs/stream", "日志流"},
- {"GET", "/api/health", "健康检查"},
- }
-
- for _, endpoint := range endpoints {
- t.Logf("✓ API 端点: %s %s - %s", endpoint.method, endpoint.path, endpoint.desc)
- }
- }
-
- // TestServiceComponents 测试服务组件
- func TestServiceComponents(t *testing.T) {
- components := []string{
- "opencode.Process - 进程管理",
- "opencode.Client - API 客户端",
- "opencode.MockClient - 模拟客户端",
- "routes.SessionRoutes - 会话路由",
- "routes.PromptSyncRoutes - 同步对话路由",
- "routes.PromptStreamHandler - 流式对话处理器",
- "routes.LogStreamHandler - 日志流处理器",
- "service.SessionManager - 会话管理器",
- }
-
- for _, component := range components {
- t.Logf("✓ 服务组件: %s", component)
- }
- }
|