147 lines
4.9 KiB
Go
147 lines
4.9 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gochat/gochat/internal/llm"
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
"github.com/gochat/gochat/internal/service"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type mockBulkHandlerLLM struct {
|
|
response *llm.ChatResponse
|
|
err error
|
|
}
|
|
|
|
func (m *mockBulkHandlerLLM) ChatCompletion(_ context.Context, _ llm.ChatRequest) (*llm.ChatResponse, error) {
|
|
return m.response, m.err
|
|
}
|
|
func (m *mockBulkHandlerLLM) CreateEmbedding(_ context.Context, _ llm.EmbeddingRequest) (*llm.EmbeddingResponse, error) {
|
|
return nil, nil
|
|
}
|
|
func (m *mockBulkHandlerLLM) ChatCompletionStream(_ context.Context, _ llm.ChatRequest, _ func(llm.StreamChunk) error) error {
|
|
return nil
|
|
}
|
|
|
|
func setupBulkActionHandlerTest(t *testing.T, mockLLM llm.Provider) (*CaptainBulkActionHandler, *gorm.DB) {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
dbName := fmt.Sprintf("file:%s?mode=memory&cache=private", t.Name())
|
|
db, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{})
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.AutoMigrate(
|
|
&model.CaptainPreference{},
|
|
&model.CaptainAssistant{},
|
|
&model.Conversation{},
|
|
&model.Message{},
|
|
&model.Inbox{},
|
|
&model.Contact{},
|
|
))
|
|
convRepo := repository.NewConversationRepo(db)
|
|
msgRepo := repository.NewMessageRepo(db)
|
|
assistantRepo := repository.NewCaptainAssistantRepo(db)
|
|
prefRepo := repository.NewCaptainPreferenceRepo(db)
|
|
responseRepo := repository.NewCaptainAssistantResponseRepo(db)
|
|
taskSvc := service.NewCaptainTaskExtendedService(convRepo, msgRepo, assistantRepo, prefRepo, mockLLM)
|
|
responseSvc := service.NewCaptainAssistantResponseService(assistantRepo, responseRepo, convRepo, msgRepo, prefRepo, mockLLM)
|
|
bulkSvc := service.NewCaptainBulkActionService(convRepo, msgRepo, assistantRepo, prefRepo, mockLLM, taskSvc, responseSvc)
|
|
handler := NewCaptainBulkActionHandler(bulkSvc)
|
|
return handler, db
|
|
}
|
|
|
|
func seedBulkHandlerData(t *testing.T, db *gorm.DB) []*model.Conversation {
|
|
t.Helper()
|
|
inbox := &model.Inbox{AccountID: 1, Name: "Inbox", ChannelType: "web_widget"}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
var convs []*model.Conversation
|
|
for i := 0; i < 2; i++ {
|
|
conv := &model.Conversation{AccountID: 1, InboxID: inbox.ID, Status: "open"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
msg := &model.Message{ConversationID: conv.ID, AccountID: 1, InboxID: inbox.ID, SenderType: "contact", Content: fmt.Sprintf("msg %d", i), ContentType: "text", MessageType: "incoming"}
|
|
require.NoError(t, db.Create(msg).Error)
|
|
convs = append(convs, conv)
|
|
}
|
|
return convs
|
|
}
|
|
|
|
func TestCaptainBulkActionHandler_Execute_LabelSuggestion(t *testing.T) {
|
|
mockLLM := &mockBulkHandlerLLM{
|
|
response: &llm.ChatResponse{
|
|
Choices: []llm.ChatChoice{
|
|
{Message: llm.ChatMessage{Role: "assistant", Content: `{"labels": ["support"], "priority": "medium", "reason": "routine"}`}},
|
|
},
|
|
},
|
|
}
|
|
handler, db := setupBulkActionHandlerTest(t, mockLLM)
|
|
convs := seedBulkHandlerData(t, db)
|
|
|
|
body := map[string]interface{}{
|
|
"action": "label_suggestion",
|
|
"conversation_ids": []uint{convs[0].ID, convs[1].ID},
|
|
}
|
|
bodyBytes, _ := json.Marshal(body)
|
|
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Params = gin.Params{{Key: "id", Value: "1"}}
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/captain/bulk_actions", bytes.NewReader(bodyBytes))
|
|
c.Request.Header.Set("Content-Type", "application/json")
|
|
|
|
handler.Execute(c)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
var resp handlerTestResponse
|
|
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
|
|
assert.True(t, resp.Success)
|
|
}
|
|
|
|
func TestCaptainBulkActionHandler_Execute_InvalidAction(t *testing.T) {
|
|
mockLLM := &mockBulkHandlerLLM{}
|
|
handler, _ := setupBulkActionHandlerTest(t, mockLLM)
|
|
|
|
body := map[string]interface{}{
|
|
"action": "invalid_action",
|
|
"conversation_ids": []uint{1},
|
|
}
|
|
bodyBytes, _ := json.Marshal(body)
|
|
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Params = gin.Params{{Key: "id", Value: "1"}}
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/captain/bulk_actions", bytes.NewReader(bodyBytes))
|
|
c.Request.Header.Set("Content-Type", "application/json")
|
|
|
|
handler.Execute(c)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestCaptainBulkActionHandler_Execute_InvalidAccountID(t *testing.T) {
|
|
mockLLM := &mockBulkHandlerLLM{}
|
|
handler, _ := setupBulkActionHandlerTest(t, mockLLM)
|
|
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Params = gin.Params{{Key: "id", Value: "abc"}}
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/accounts/abc/captain/bulk_actions", nil)
|
|
c.Request.Header.Set("Content-Type", "application/json")
|
|
|
|
handler.Execute(c)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|