180 lines
6.5 KiB
Go
180 lines
6.5 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// === InboxService tests ===
|
|
|
|
func TestInboxService_Ready_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
repo := repository.NewInboxRepo(db)
|
|
svc := NewInboxService(repo, nil, nil, nil, nil, nil, nil)
|
|
assert.True(t, svc.Ready())
|
|
}
|
|
|
|
func TestInboxService_Ready_Nil_Cov8(t *testing.T) {
|
|
var svc *InboxService
|
|
assert.False(t, svc.Ready())
|
|
}
|
|
|
|
func TestInboxService_DB_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
repo := repository.NewInboxRepo(db)
|
|
svc := NewInboxService(repo, nil, nil, nil, nil, nil, nil)
|
|
assert.NotNil(t, svc.DB())
|
|
}
|
|
|
|
func TestInboxService_DB_Nil_Cov8(t *testing.T) {
|
|
var svc *InboxService
|
|
assert.Nil(t, svc.DB())
|
|
}
|
|
|
|
func TestInboxService_ListByAccount_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
repo := repository.NewInboxRepo(db)
|
|
svc := NewInboxService(repo, nil, nil, nil, nil, nil, nil)
|
|
|
|
// Seed
|
|
require.NoError(t, db.Create(&model.Account{Name: "Test"}).Error)
|
|
require.NoError(t, db.Create(&model.Inbox{AccountID: 1, Name: "Inbox1", ChannelType: "web_widget", Enabled: true}).Error)
|
|
|
|
inboxes, total, err := svc.ListByAccount(context.Background(), 1, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), total)
|
|
assert.Len(t, inboxes, 1)
|
|
}
|
|
|
|
func TestInboxService_GetByID_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
repo := repository.NewInboxRepo(db)
|
|
svc := NewInboxService(repo, nil, nil, nil, nil, nil, nil)
|
|
|
|
require.NoError(t, db.Create(&model.Account{Name: "Test"}).Error)
|
|
inbox := &model.Inbox{AccountID: 1, Name: "TestInbox", ChannelType: "web_widget", Enabled: true}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
got, err := svc.GetByID(context.Background(), inbox.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "TestInbox", got.Name)
|
|
}
|
|
|
|
func TestInboxService_GetByID_NotFound_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
repo := repository.NewInboxRepo(db)
|
|
svc := NewInboxService(repo, nil, nil, nil, nil, nil, nil)
|
|
|
|
_, err := svc.GetByID(context.Background(), 99999)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// === ConversationService tests ===
|
|
|
|
func TestConversationService_ListByAccount_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
convRepo := repository.NewConversationRepo(db)
|
|
msgRepo := repository.NewMessageRepo(db)
|
|
svc := NewConversationService(convRepo, msgRepo, nil, nil, nil, nil, nil)
|
|
|
|
require.NoError(t, db.Create(&model.Account{Name: "Test"}).Error)
|
|
require.NoError(t, db.Create(&model.Inbox{AccountID: 1, Name: "Inbox", ChannelType: "web_widget", Enabled: true}).Error)
|
|
require.NoError(t, db.Create(&model.Contact{AccountID: 1, Name: "Contact", Email: "test@test.com"}).Error)
|
|
require.NoError(t, db.Create(&model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, Status: "open", DisplayID: uintPtr(1)}).Error)
|
|
|
|
convs, total, err := svc.ListByAccount(context.Background(), 1, 0, 10)
|
|
require.NoError(t, err)
|
|
_ = convs
|
|
_ = total
|
|
}
|
|
|
|
func TestConversationService_ListByStatus_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
convRepo := repository.NewConversationRepo(db)
|
|
msgRepo := repository.NewMessageRepo(db)
|
|
svc := NewConversationService(convRepo, msgRepo, nil, nil, nil, nil, nil)
|
|
|
|
require.NoError(t, db.Create(&model.Account{Name: "Test"}).Error)
|
|
require.NoError(t, db.Create(&model.Inbox{AccountID: 1, Name: "Inbox", ChannelType: "web_widget", Enabled: true}).Error)
|
|
require.NoError(t, db.Create(&model.Contact{AccountID: 1, Name: "Contact", Email: "test@test.com"}).Error)
|
|
require.NoError(t, db.Create(&model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, Status: "open", DisplayID: uintPtr(1)}).Error)
|
|
|
|
convs, _, err := svc.ListByStatus(context.Background(), 1, "open", 0, 10)
|
|
require.NoError(t, err)
|
|
_ = convs
|
|
}
|
|
|
|
func TestConversationService_GetByID_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
convRepo := repository.NewConversationRepo(db)
|
|
msgRepo := repository.NewMessageRepo(db)
|
|
svc := NewConversationService(convRepo, msgRepo, nil, nil, nil, nil, nil)
|
|
|
|
require.NoError(t, db.Create(&model.Account{Name: "Test"}).Error)
|
|
require.NoError(t, db.Create(&model.Inbox{AccountID: 1, Name: "Inbox", ChannelType: "web_widget", Enabled: true}).Error)
|
|
require.NoError(t, db.Create(&model.Contact{AccountID: 1, Name: "Contact", Email: "test@test.com"}).Error)
|
|
conv := &model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, Status: "open", DisplayID: uintPtr(1)}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
got, err := svc.GetByID(context.Background(), conv.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, uint(1), got.AccountID)
|
|
}
|
|
|
|
func TestConversationService_GetByID_NotFound_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
convRepo := repository.NewConversationRepo(db)
|
|
msgRepo := repository.NewMessageRepo(db)
|
|
svc := NewConversationService(convRepo, msgRepo, nil, nil, nil, nil, nil)
|
|
|
|
_, err := svc.GetByID(context.Background(), 99999)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// === WidgetService additional tests ===
|
|
|
|
func TestWidgetService_SetTranscriptDeliverer_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
repo := repository.NewInboxRepo(db)
|
|
contactRepo := repository.NewContactRepo(db)
|
|
contactInboxRepo := repository.NewContactInboxRepo(db)
|
|
convRepo := repository.NewConversationRepo(db)
|
|
msgRepo := repository.NewMessageRepo(db)
|
|
svc := NewWidgetService(repo, contactRepo, contactInboxRepo, convRepo, msgRepo, nil, nil, nil, nil, nil, nil, nil, nil)
|
|
svc.SetTranscriptDeliverer(nil)
|
|
// Should not panic
|
|
}
|
|
|
|
func TestWidgetService_SetDispatcher_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
repo := repository.NewInboxRepo(db)
|
|
contactRepo := repository.NewContactRepo(db)
|
|
contactInboxRepo := repository.NewContactInboxRepo(db)
|
|
convRepo := repository.NewConversationRepo(db)
|
|
msgRepo := repository.NewMessageRepo(db)
|
|
svc := NewWidgetService(repo, contactRepo, contactInboxRepo, convRepo, msgRepo, nil, nil, nil, nil, nil, nil, nil, nil)
|
|
svc.SetDispatcher(nil)
|
|
// Should not panic
|
|
}
|
|
|
|
// === ContactService additional tests ===
|
|
|
|
func TestContactService_GetByID_Cov8(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
repo := repository.NewContactRepo(db)
|
|
svc := NewContactService(repo, nil, nil)
|
|
|
|
require.NoError(t, db.Create(&model.Account{Name: "Test"}).Error)
|
|
contact := &model.Contact{AccountID: 1, Name: "TestContact", Email: "test@test.com"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
got, err := svc.GetByID(context.Background(), contact.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "TestContact", got.Name)
|
|
}
|