186 lines
6.0 KiB
Go
186 lines
6.0 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"
|
|
)
|
|
|
|
func newWidgetSvcCov9(t *testing.T) (*WidgetService, *repository.InboxRepo) {
|
|
db := newSimpleServiceTestDB(t, &model.InboxMember{}, &model.Tag{})
|
|
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)
|
|
return svc, repo
|
|
}
|
|
|
|
// --- WidgetService tests ---
|
|
|
|
func TestWidgetService_GetConversation_InvalidToken_Cov9(t *testing.T) {
|
|
svc, _ := newWidgetSvcCov9(t)
|
|
_, err := svc.GetConversation(context.Background(), "invalid-token", 1)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_GetLatestConversationMessages_InvalidToken_Cov9(t *testing.T) {
|
|
svc, _ := newWidgetSvcCov9(t)
|
|
_, _, _, err := svc.GetLatestConversationMessages(context.Background(), "invalid-token", 0, 0)
|
|
// Returns error or empty - either is acceptable
|
|
_ = err
|
|
}
|
|
|
|
func TestWidgetService_GetInboxMembersByWebsiteToken_InvalidToken_Cov9(t *testing.T) {
|
|
svc, _ := newWidgetSvcCov9(t)
|
|
_, err := svc.GetInboxMembersByWebsiteToken(context.Background(), "invalid-token")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_GetCampaignsByWebsiteToken_InvalidToken_Cov9(t *testing.T) {
|
|
svc, _ := newWidgetSvcCov9(t)
|
|
_, err := svc.GetCampaignsByWebsiteToken(context.Background(), "invalid-token")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetAccountFeatureEnabled_Cov9(t *testing.T) {
|
|
assert.False(t, widgetAccountFeatureEnabled("", "feature"))
|
|
assert.False(t, widgetAccountFeatureEnabled(" ", "feature"))
|
|
}
|
|
|
|
func TestWidgetService_TrackEvent_EmptyWebsiteToken_Cov9(t *testing.T) {
|
|
svc, _ := newWidgetSvcCov9(t)
|
|
err := svc.TrackEvent(context.Background(), "", "token", "event", nil)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_AddLabelToLatestConversation_EmptyLabel_Cov9(t *testing.T) {
|
|
svc, _ := newWidgetSvcCov9(t)
|
|
err := svc.AddLabelToLatestConversation(context.Background(), "token", " ")
|
|
assert.NoError(t, err) // empty label returns nil
|
|
}
|
|
|
|
func TestWidgetService_RemoveLabelFromLatestConversation_EmptyLabel_Cov9(t *testing.T) {
|
|
svc, _ := newWidgetSvcCov9(t)
|
|
err := svc.RemoveLabelFromLatestConversation(context.Background(), "token", " ")
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicGetInbox_InvalidIdentifier_Cov9(t *testing.T) {
|
|
svc, _ := newWidgetSvcCov9(t)
|
|
_, _, err := svc.PublicGetInbox(context.Background(), "invalid-identifier")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicCreateContact_InvalidIdentifier_Cov9(t *testing.T) {
|
|
svc, _ := newWidgetSvcCov9(t)
|
|
_, err := svc.PublicCreateContact(context.Background(), "invalid-identifier", PublicContactRequest{})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_SetWorkerPool_Cov9(t *testing.T) {
|
|
svc, _ := newWidgetSvcCov9(t)
|
|
svc.SetWorkerPool(nil)
|
|
}
|
|
|
|
// --- InboxService tests ---
|
|
|
|
func TestInboxService_GetByID_NotFound_Cov9(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.InboxLimit{})
|
|
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_ListByInbox_Cov9(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: uintPtrCov9(1)}).Error)
|
|
|
|
convs, _, err := svc.ListByInbox(context.Background(), 1, 1, 0, 10)
|
|
require.NoError(t, err)
|
|
_ = convs
|
|
}
|
|
|
|
func TestConversationService_ListByAssignee_Cov9(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
convRepo := repository.NewConversationRepo(db)
|
|
msgRepo := repository.NewMessageRepo(db)
|
|
svc := NewConversationService(convRepo, msgRepo, nil, nil, nil, nil, nil)
|
|
|
|
convs, _, err := svc.ListByAssignee(context.Background(), 1, 1, 0, 10)
|
|
require.NoError(t, err)
|
|
_ = convs
|
|
}
|
|
|
|
func TestConversationService_ListUnassigned_Cov9(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
convRepo := repository.NewConversationRepo(db)
|
|
msgRepo := repository.NewMessageRepo(db)
|
|
svc := NewConversationService(convRepo, msgRepo, nil, nil, nil, nil, nil)
|
|
|
|
convs, _, err := svc.ListUnassigned(context.Background(), 1, 0, 10)
|
|
require.NoError(t, err)
|
|
_ = convs
|
|
}
|
|
|
|
func TestConversationService_SetSearchIndexer_Cov9(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
convRepo := repository.NewConversationRepo(db)
|
|
msgRepo := repository.NewMessageRepo(db)
|
|
svc := NewConversationService(convRepo, msgRepo, nil, nil, nil, nil, nil)
|
|
svc.SetSearchIndexer(nil)
|
|
}
|
|
|
|
// --- ContactService tests ---
|
|
|
|
func TestContactService_GetByID_NotFound_Cov9(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
repo := repository.NewContactRepo(db)
|
|
svc := NewContactService(repo, nil, nil)
|
|
|
|
_, err := svc.GetByID(context.Background(), 99999)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// --- ArticleService tests ---
|
|
|
|
func TestArticleService_Struct_Cov9(t *testing.T) {
|
|
// Just test that the type exists
|
|
_ = &ArticleService{}
|
|
}
|
|
|
|
// --- CsatTemplateService tests ---
|
|
|
|
func TestCsatTemplateService_Struct_Cov9(t *testing.T) {
|
|
_ = &CsatTemplateService{}
|
|
}
|
|
|
|
// --- NotificationService tests ---
|
|
|
|
func TestNotificationService_Struct_Cov9(t *testing.T) {
|
|
_ = &NotificationService{}
|
|
}
|
|
|
|
// --- ProfileService tests ---
|
|
|
|
func TestProfileService_Struct_Cov9(t *testing.T) {
|
|
_ = &ProfileService{}
|
|
}
|
|
|
|
func uintPtrCov9(v uint) *uint { return &v }
|