설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

integration_test.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "testing"
  4. "git.x2erp.com/qdy/go-svc-code/internal/opencode"
  5. )
  6. // TestMockClient 测试模拟客户端的基本功能
  7. func TestMockClient(t *testing.T) {
  8. mockClient := opencode.NewMockClient(8080)
  9. // 测试创建会话
  10. session, err := mockClient.CreateSession(nil, "测试会话")
  11. if err != nil {
  12. t.Fatalf("创建会话失败: %v", err)
  13. }
  14. if session.Title != "测试会话" {
  15. t.Errorf("预期会话标题为 '测试会话',得到 %s", session.Title)
  16. }
  17. t.Logf("✓ 创建会话成功: ID=%s", session.ID)
  18. // 测试发送消息
  19. prompt := &opencode.PromptRequest{
  20. Parts: []opencode.TextPart{
  21. {Type: "text", Text: "你好"},
  22. },
  23. }
  24. response, err := mockClient.SendPrompt(nil, session.ID, prompt)
  25. if err != nil {
  26. t.Fatalf("发送消息失败: %v", err)
  27. }
  28. if response.Info.Content == "" {
  29. t.Error("预期响应内容,得到空")
  30. }
  31. t.Logf("✓ 发送消息成功: %s", response.Info.Content)
  32. // 测试获取会话列表
  33. sessions, err := mockClient.ListSessions(nil)
  34. if err != nil {
  35. t.Fatalf("获取会话列表失败: %v", err)
  36. }
  37. if len(sessions) != 1 {
  38. t.Errorf("预期 1 个会话,得到 %d", len(sessions))
  39. }
  40. t.Logf("✓ 获取会话列表成功: 共 %d 个会话", len(sessions))
  41. }
  42. // TestProcessFunctions 测试进程相关功能
  43. func TestProcessFunctions(t *testing.T) {
  44. // 测试获取可用端口
  45. port, err := opencode.GetAvailablePort()
  46. if err != nil {
  47. t.Fatalf("获取可用端口失败: %v", err)
  48. }
  49. if port <= 0 || port > 65535 {
  50. t.Errorf("无效的端口号: %d", port)
  51. }
  52. t.Logf("✓ 获取可用端口成功: %d", port)
  53. }
  54. // // TestClientInterface 测试客户端接口一致性
  55. // func TestClientInterface(t *testing.T) {
  56. // // 验证 MockClient 实现了 OpenCodeClient 接口
  57. // var _ opencode.OpenCodeClient = opencode.NewMockClient(8080)
  58. // t.Log("✓ MockClient 正确实现了 OpenCodeClient 接口")
  59. // }
  60. // TestAPIEndpoints 测试 API 端点定义
  61. func TestAPIEndpoints(t *testing.T) {
  62. endpoints := []struct {
  63. method string
  64. path string
  65. desc string
  66. }{
  67. {"POST", "/api/session/create", "创建会话"},
  68. {"GET", "/api/session/list", "获取会话列表"},
  69. {"POST", "/api/prompt/sync", "同步对话"},
  70. {"POST", "/api/prompt/stream", "流式对话"},
  71. {"GET", "/api/logs/stream", "日志流"},
  72. {"GET", "/api/health", "健康检查"},
  73. }
  74. for _, endpoint := range endpoints {
  75. t.Logf("✓ API 端点: %s %s - %s", endpoint.method, endpoint.path, endpoint.desc)
  76. }
  77. }
  78. // TestServiceComponents 测试服务组件
  79. func TestServiceComponents(t *testing.T) {
  80. components := []string{
  81. "opencode.Process - 进程管理",
  82. "opencode.Client - API 客户端",
  83. "opencode.MockClient - 模拟客户端",
  84. "routes.SessionRoutes - 会话路由",
  85. "routes.PromptSyncRoutes - 同步对话路由",
  86. "routes.PromptStreamHandler - 流式对话处理器",
  87. "routes.LogStreamHandler - 日志流处理器",
  88. "service.SessionManager - 会话管理器",
  89. }
  90. for _, component := range components {
  91. t.Logf("✓ 服务组件: %s", component)
  92. }
  93. }