Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
414 lines
12 KiB
Plaintext
414 lines
12 KiB
Plaintext
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/llm"
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// ========== CreateThread ==========
|
|
|
|
func TestCopilotService_CreateThread_成功(t *testing.T) {
|
|
db, mockLLM, svc := setupCopilotService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
req := &CreateThreadRequest{Title: "测试线程"}
|
|
thread, err := svc.CreateThread(context.Background(), account.ID, 1, req)
|
|
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, thread.ID)
|
|
assert.Equal(t, account.ID, thread.AccountID)
|
|
assert.Equal(t, uint(1), thread.UserID)
|
|
assert.Equal(t, "测试线程", thread.Title)
|
|
assert.Nil(t, thread.AssistantID)
|
|
// CreateThread 不调用 LLM
|
|
assert.Nil(t, mockLLM.lastChatRequest)
|
|
}
|
|
|
|
func TestCopilotService_CreateThread_带AssistantID(t *testing.T) {
|
|
db, _, svc := setupCopilotService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
assistantID := uint(42)
|
|
req := &CreateThreadRequest{Title: "带助手线程", AssistantID: &assistantID}
|
|
thread, err := svc.CreateThread(context.Background(), account.ID, 1, req)
|
|
|
|
require.NoError(t, err)
|
|
require.NotNil(t, thread.AssistantID)
|
|
assert.Equal(t, assistantID, *thread.AssistantID)
|
|
}
|
|
|
|
// ========== GetThread ==========
|
|
|
|
func TestCopilotService_GetThread_成功(t *testing.T) {
|
|
db, _, svc := setupCopilotService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
// 先创建一个线程
|
|
req := &CreateThreadRequest{Title: "获取测试线程"}
|
|
thread, err := svc.CreateThread(context.Background(), account.ID, 1, req)
|
|
require.NoError(t, err)
|
|
|
|
// 获取线程
|
|
result, err := svc.GetThread(context.Background(), thread.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, thread.ID, result.ID)
|
|
assert.Equal(t, "获取测试线程", result.Title)
|
|
}
|
|
|
|
func TestCopilotService_GetThread_不存在(t *testing.T) {
|
|
_, _, svc := setupCopilotService(t)
|
|
|
|
result, err := svc.GetThread(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestCopilotService_GetThread_带消息(t *testing.T) {
|
|
db, _, svc := setupCopilotService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
// 创建线程
|
|
req := &CreateThreadRequest{Title: "带消息线程"}
|
|
thread, err := svc.CreateThread(context.Background(), account.ID, 1, req)
|
|
require.NoError(t, err)
|
|
|
|
// 直接插入消息到数据库
|
|
msg1 := &model.CopilotMessage{
|
|
AccountID: account.ID,
|
|
CopilotThreadID: thread.ID,
|
|
MessageType: model.CopilotMessageTypeUser,
|
|
Message: json.RawMessage(`{"content": "你好"}`),
|
|
}
|
|
require.NoError(t, db.Create(msg1).Error)
|
|
msg2 := &model.CopilotMessage{
|
|
AccountID: account.ID,
|
|
CopilotThreadID: thread.ID,
|
|
MessageType: model.CopilotMessageTypeAssistant,
|
|
Message: json.RawMessage(`{"content": "你好,有什么可以帮您?"}`),
|
|
}
|
|
require.NoError(t, db.Create(msg2).Error)
|
|
|
|
// 获取线程并验证包含消息
|
|
result, err := svc.GetThread(context.Background(), thread.ID)
|
|
require.NoError(t, err)
|
|
assert.Len(t, result.Messages, 2)
|
|
}
|
|
|
|
// ========== ListThreads ==========
|
|
|
|
func TestCopilotService_ListThreads_成功(t *testing.T) {
|
|
db, _, svc := setupCopilotService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
// 创建多个线程
|
|
for i := 0; i < 3; i++ {
|
|
req := &CreateThreadRequest{Title: fmt.Sprintf("线程%d", i)}
|
|
_, err := svc.CreateThread(context.Background(), account.ID, 1, req)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
threads, count, err := svc.ListThreads(context.Background(), account.ID, 1, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(3), count)
|
|
assert.Len(t, threads, 3)
|
|
}
|
|
|
|
func TestCopilotService_ListThreads_分页(t *testing.T) {
|
|
db, _, svc := setupCopilotService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
// 创建5个线程
|
|
for i := 0; i < 5; i++ {
|
|
req := &CreateThreadRequest{Title: fmt.Sprintf("分页线程%d", i)}
|
|
_, err := svc.CreateThread(context.Background(), account.ID, 1, req)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// 第一页:2条
|
|
threads, count, err := svc.ListThreads(context.Background(), account.ID, 1, 0, 2)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(5), count)
|
|
assert.Len(t, threads, 2)
|
|
|
|
// 第二页:3条
|
|
threads2, count2, err := svc.ListThreads(context.Background(), account.ID, 1, 2, 3)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(5), count2)
|
|
assert.Len(t, threads2, 3)
|
|
}
|
|
|
|
func TestCopilotService_ListThreads_空列表(t *testing.T) {
|
|
_, _, svc := setupCopilotService(t)
|
|
|
|
threads, count, err := svc.ListThreads(context.Background(), 999, 999, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(0), count)
|
|
assert.Len(t, threads, 0)
|
|
}
|
|
|
|
// ========== SendMessage ==========
|
|
|
|
func TestCopilotService_SendMessage_成功(t *testing.T) {
|
|
db, mockLLM, svc := setupCopilotService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
// 创建线程
|
|
req := &CreateThreadRequest{Title: "发消息线程"}
|
|
thread, err := svc.CreateThread(context.Background(), account.ID, 1, req)
|
|
require.NoError(t, err)
|
|
|
|
// 配置 mock LLM 返回
|
|
mockLLM.chatResponse = &llm.ChatResponse{
|
|
ID: "chat-123",
|
|
Choices: []llm.ChatChoice{
|
|
{
|
|
Message: llm.ChatMessage{Role: "assistant", Content: "这是AI回复"},
|
|
FinishReason: "stop",
|
|
},
|
|
},
|
|
}
|
|
|
|
// 发送消息
|
|
result, err := svc.SendMessage(context.Background(), thread.ID, account.ID, &SendMessageRequest{Content: "你好"})
|
|
require.NoError(t, err)
|
|
require.NotNil(t, result.UserMessage)
|
|
assert.Equal(t, model.CopilotMessageTypeUser, result.UserMessage.MessageType)
|
|
require.NotNil(t, result.AssistantMessage)
|
|
assert.Equal(t, model.CopilotMessageTypeAssistant, result.AssistantMessage.MessageType)
|
|
// 验证 LLM 被调用
|
|
require.NotNil(t, mockLLM.lastChatRequest)
|
|
assert.Equal(t, "gpt-4", mockLLM.lastChatRequest.Model)
|
|
}
|
|
|
|
func TestCopilotService_SendMessage_LLM调用失败(t *testing.T) {
|
|
db, mockLLM, svc := setupCopilotService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
// 创建线程
|
|
req := &CreateThreadRequest{Title: "LLM失败线程"}
|
|
thread, err := svc.CreateThread(context.Background(), account.ID, 1, req)
|
|
require.NoError(t, err)
|
|
|
|
// 配置 mock LLM 返回错误
|
|
mockLLM.chatError = fmt.Errorf("LLM API不可用")
|
|
|
|
// 发送消息 — 应返回部分结果(只有用户消息)
|
|
result, err := svc.SendMessage(context.Background(), thread.ID, account.ID, &SendMessageRequest{Content: "你好"})
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "llm generation failed")
|
|
require.NotNil(t, result)
|
|
require.NotNil(t, result.UserMessage)
|
|
assert.Nil(t, result.AssistantMessage)
|
|
}
|
|
|
|
func TestCopilotService_SendMessage_线程不存在(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
mockLLM.chatResponse = &llm.ChatResponse{
|
|
ID: "chat-999",
|
|
Choices: []llm.ChatChoice{
|
|
{Message: llm.ChatMessage{Role: "assistant", Content: "回复"}},
|
|
},
|
|
}
|
|
|
|
result, err := svc.SendMessage(context.Background(), 9999, 1, &SendMessageRequest{Content: "你好"})
|
|
assert.Error(t, err)
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
// ========== GetSuggestedReplies ==========
|
|
|
|
func TestCopilotService_GetSuggestedReplies_成功JSON数组(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
// Mock LLM 返回 JSON 数组格式
|
|
mockLLM.chatResponse = &llm.ChatResponse{
|
|
ID: "suggest-1",
|
|
Choices: []llm.ChatChoice{
|
|
{
|
|
Message: llm.ChatMessage{Role: "assistant", Content: `["回复选项1", "回复选项2", "回复选项3"]`},
|
|
FinishReason: "stop",
|
|
},
|
|
},
|
|
}
|
|
|
|
result, err := svc.GetSuggestedReplies(context.Background(), 1, "客户问了一个问题")
|
|
require.NoError(t, err)
|
|
assert.Len(t, result.Replies, 3)
|
|
assert.Equal(t, "回复选项1", result.Replies[0])
|
|
}
|
|
|
|
func TestCopilotService_GetSuggestedReplies_LLM失败(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
mockLLM.chatError = fmt.Errorf("LLM 服务暂时不可用")
|
|
|
|
result, err := svc.GetSuggestedReplies(context.Background(), 1, "对话内容")
|
|
assert.Error(t, err)
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestCopilotService_GetSuggestedReplies_空Choices(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
// Mock LLM 返回空 Choices
|
|
mockLLM.chatResponse = &llm.ChatResponse{
|
|
ID: "suggest-empty",
|
|
Choices: []llm.ChatChoice{},
|
|
}
|
|
|
|
result, err := svc.GetSuggestedReplies(context.Background(), 1, "对话内容")
|
|
require.NoError(t, err)
|
|
assert.Empty(t, result.Replies)
|
|
}
|
|
|
|
func TestCopilotService_GetSuggestedReplies_非JSON格式回退(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
// Mock LLM 返回非 JSON 数组格式,应该 fallback 到 splitReplies
|
|
mockLLM.chatResponse = &llm.ChatResponse{
|
|
ID: "suggest-raw",
|
|
Choices: []llm.ChatChoice{
|
|
{
|
|
Message: llm.ChatMessage{Role: "assistant", Content: "1. 第一条回复\n2. 第二条回复\n3. 第三条回复"},
|
|
FinishReason: "stop",
|
|
},
|
|
},
|
|
}
|
|
|
|
result, err := svc.GetSuggestedReplies(context.Background(), 1, "对话内容")
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, result.Replies)
|
|
}
|
|
|
|
// ========== SummarizeConversation ==========
|
|
|
|
func TestCopilotService_SummarizeConversation_成功(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
mockLLM.chatResponse = &llm.ChatResponse{
|
|
ID: "summary-1",
|
|
Choices: []llm.ChatChoice{
|
|
{
|
|
Message: llm.ChatMessage{Role: "assistant", Content: "客户报告了网络连接问题,目前尚未解决。"},
|
|
FinishReason: "stop",
|
|
},
|
|
},
|
|
}
|
|
|
|
result, err := svc.SummarizeConversation(context.Background(), 1, "客户:我的网络断了\n客服:让我帮您检查")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "客户报告了网络连接问题,目前尚未解决。", result.Summary)
|
|
}
|
|
|
|
func TestCopilotService_SummarizeConversation_LLM失败(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
mockLLM.chatError = fmt.Errorf("LLM 服务错误")
|
|
|
|
result, err := svc.SummarizeConversation(context.Background(), 1, "对话内容")
|
|
assert.Error(t, err)
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestCopilotService_SummarizeConversation_空Choices(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
mockLLM.chatResponse = &llm.ChatResponse{
|
|
ID: "summary-empty",
|
|
Choices: []llm.ChatChoice{},
|
|
}
|
|
|
|
result, err := svc.SummarizeConversation(context.Background(), 1, "对话内容")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "", result.Summary)
|
|
}
|
|
|
|
// ========== DeleteThread ==========
|
|
|
|
func TestCopilotService_DeleteThread_成功(t *testing.T) {
|
|
db, _, svc := setupCopilotService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
// 创建线程
|
|
req := &CreateThreadRequest{Title: "待删除线程"}
|
|
thread, err := svc.CreateThread(context.Background(), account.ID, 1, req)
|
|
require.NoError(t, err)
|
|
|
|
// 删除线程
|
|
err = svc.DeleteThread(context.Background(), thread.ID)
|
|
require.NoError(t, err)
|
|
|
|
// 验证线程已被删除
|
|
_, err = svc.GetThread(context.Background(), thread.ID)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestCopilotService_DeleteThread_不存在(t *testing.T) {
|
|
_, _, svc := setupCopilotService(t)
|
|
|
|
// 删除不存在的线程 — GORM soft delete 不报错
|
|
err := svc.DeleteThread(context.Background(), 9999)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
// ========== TranslateMessage ==========
|
|
|
|
func TestCopilotService_TranslateMessage_成功(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
mockLLM.chatResponse = &llm.ChatResponse{
|
|
ID: "translate-1",
|
|
Choices: []llm.ChatChoice{
|
|
{
|
|
Message: llm.ChatMessage{Role: "assistant", Content: "Bonjour"},
|
|
FinishReason: "stop",
|
|
},
|
|
},
|
|
}
|
|
|
|
result, err := svc.TranslateMessage(context.Background(), 1, &TranslateRequest{
|
|
Content: "Hello",
|
|
TargetLanguage: "French",
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "Bonjour", result.TranslatedContent)
|
|
assert.Equal(t, "French", result.TargetLanguage)
|
|
}
|
|
|
|
func TestCopilotService_TranslateMessage_LLM失败(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
mockLLM.chatError = fmt.Errorf("翻译API故障")
|
|
|
|
result, err := svc.TranslateMessage(context.Background(), 1, &TranslateRequest{
|
|
Content: "Hello",
|
|
TargetLanguage: "French",
|
|
})
|
|
assert.Error(t, err)
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestCopilotService_TranslateMessage_空Choices(t *testing.T) {
|
|
_, mockLLM, svc := setupCopilotService(t)
|
|
|
|
mockLLM.chatResponse = &llm.ChatResponse{
|
|
ID: "translate-empty",
|
|
Choices: []llm.ChatChoice{},
|
|
}
|
|
|
|
result, err := svc.TranslateMessage(context.Background(), 1, &TranslateRequest{
|
|
Content: "Hello",
|
|
TargetLanguage: "French",
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "", result.TranslatedContent)
|
|
assert.Equal(t, "French", result.TargetLanguage)
|
|
} |