146 lines
5.1 KiB
Go
146 lines
5.1 KiB
Go
package v1
|
|
|
|
import (
|
|
"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 mockTaskExtHandlerLLM struct {
|
|
response *llm.ChatResponse
|
|
err error
|
|
}
|
|
|
|
func (m *mockTaskExtHandlerLLM) ChatCompletion(_ context.Context, _ llm.ChatRequest) (*llm.ChatResponse, error) {
|
|
return m.response, m.err
|
|
}
|
|
func (m *mockTaskExtHandlerLLM) CreateEmbedding(_ context.Context, _ llm.EmbeddingRequest) (*llm.EmbeddingResponse, error) {
|
|
return nil, nil
|
|
}
|
|
func (m *mockTaskExtHandlerLLM) ChatCompletionStream(_ context.Context, _ llm.ChatRequest, _ func(llm.StreamChunk) error) error {
|
|
return nil
|
|
}
|
|
|
|
func setupTaskExtendedHandlerTest(t *testing.T, mockLLM llm.Provider) (*CaptainTaskExtendedHandler, *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)
|
|
svc := service.NewCaptainTaskExtendedService(convRepo, msgRepo, assistantRepo, prefRepo, mockLLM)
|
|
handler := NewCaptainTaskExtendedHandler(svc)
|
|
return handler, db
|
|
}
|
|
|
|
func TestCaptainTaskExtendedHandler_LabelSuggestion(t *testing.T) {
|
|
mockLLM := &mockTaskExtHandlerLLM{
|
|
response: &llm.ChatResponse{
|
|
Choices: []llm.ChatChoice{
|
|
{Message: llm.ChatMessage{Role: "assistant", Content: `{"labels": ["billing"], "priority": "high", "reason": "billing issue"}`}},
|
|
},
|
|
},
|
|
}
|
|
handler, db := setupTaskExtendedHandlerTest(t, mockLLM)
|
|
|
|
// Seed conversation + message
|
|
inbox := &model.Inbox{AccountID: 1, Name: "Inbox", ChannelType: "web_widget"}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
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: "billing help", ContentType: "text", MessageType: "incoming"}
|
|
require.NoError(t, db.Create(msg).Error)
|
|
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Params = gin.Params{{Key: "id", Value: "1"}}
|
|
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/captain/tasks/label_suggestion?conversation_ids="+fmt.Sprintf("%d", conv.ID), nil)
|
|
|
|
handler.LabelSuggestion(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 TestCaptainTaskExtendedHandler_LabelSuggestion_MissingConversationIDs(t *testing.T) {
|
|
mockLLM := &mockTaskExtHandlerLLM{}
|
|
handler, _ := setupTaskExtendedHandlerTest(t, mockLLM)
|
|
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Params = gin.Params{{Key: "id", Value: "1"}}
|
|
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/captain/tasks/label_suggestion", nil)
|
|
|
|
handler.LabelSuggestion(c)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestCaptainTaskExtendedHandler_FollowUp(t *testing.T) {
|
|
mockLLM := &mockTaskExtHandlerLLM{
|
|
response: &llm.ChatResponse{
|
|
Choices: []llm.ChatChoice{
|
|
{Message: llm.ChatMessage{Role: "assistant", Content: `{"follow_ups": [{"title": "Follow up", "description": "desc", "priority": "medium", "due_date_hint": "24h"}]}`}},
|
|
},
|
|
},
|
|
}
|
|
handler, db := setupTaskExtendedHandlerTest(t, mockLLM)
|
|
|
|
inbox := &model.Inbox{AccountID: 1, Name: "Inbox", ChannelType: "web_widget"}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
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: "need follow up", ContentType: "text", MessageType: "incoming"}
|
|
require.NoError(t, db.Create(msg).Error)
|
|
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Params = gin.Params{{Key: "id", Value: "1"}}
|
|
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/captain/tasks/follow_up?conversation_ids="+fmt.Sprintf("%d", conv.ID), nil)
|
|
|
|
handler.FollowUp(c)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
}
|
|
|
|
func TestCaptainTaskExtendedHandler_FollowUp_MissingConversationIDs(t *testing.T) {
|
|
mockLLM := &mockTaskExtHandlerLLM{}
|
|
handler, _ := setupTaskExtendedHandlerTest(t, mockLLM)
|
|
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Params = gin.Params{{Key: "id", Value: "1"}}
|
|
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/captain/tasks/follow_up", nil)
|
|
|
|
handler.FollowUp(c)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|