168 lines
5.6 KiB
Go
168 lines
5.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func newWidgetSvcCov14(t *testing.T) *WidgetService {
|
|
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)
|
|
return NewWidgetService(repo, contactRepo, contactInboxRepo, convRepo, msgRepo, nil, nil, nil, nil, nil, nil, nil, nil)
|
|
}
|
|
|
|
// === WidgetService Public API tests (all return error for invalid inbox) ===
|
|
|
|
func TestWidgetService_PublicGetContact_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, err := svc.PublicGetContact(context.Background(), "invalid", "source")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicUpdateContact_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, err := svc.PublicUpdateContact(context.Background(), "invalid", "source", PublicContactRequest{})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicListConversations_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, err := svc.PublicListConversations(context.Background(), "invalid", "source")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicCreateConversation_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, err := svc.PublicCreateConversation(context.Background(), "invalid", "source", PublicConversationRequest{})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicGetConversation_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, err := svc.PublicGetConversation(context.Background(), "invalid", "source", 1)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicToggleStatus_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, err := svc.PublicToggleStatus(context.Background(), "invalid", "source", 1)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicUpdateLastSeen_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, err := svc.PublicUpdateLastSeen(context.Background(), "invalid", "source", 1)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicToggleTyping_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
err := svc.PublicToggleTyping(context.Background(), "invalid", "source", 1, true)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicListMessages_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, _, _, err := svc.PublicListMessages(context.Background(), "invalid", "source", 1, PublicMessageListOptions{})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicCreateMessage_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, _, _, err := svc.PublicCreateMessage(context.Background(), "invalid", "source", 1, PublicMessageRequest{})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_PublicUpdateMessage_InvalidInbox_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, _, err := svc.PublicUpdateMessage(context.Background(), "invalid", "source", 1, 1, PublicMessageRequest{})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_GetMessageAttachments_Empty_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
atts, err := svc.GetMessageAttachments(context.Background(), 99999)
|
|
_ = err
|
|
_ = atts
|
|
}
|
|
|
|
func TestWidgetService_GetContact_InvalidToken_Cov14(t *testing.T) {
|
|
svc := newWidgetSvcCov14(t)
|
|
_, err := svc.GetContact(context.Background(), "invalid-token")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// === reverseMessages helper test ===
|
|
|
|
func TestReverseMessages_Cov14(t *testing.T) {
|
|
msgs := []model.Message{{Content: "a"}, {Content: "b"}, {Content: "c"}}
|
|
reverseMessages(msgs)
|
|
assert.Equal(t, "c", msgs[0].Content)
|
|
assert.Equal(t, "a", msgs[2].Content)
|
|
}
|
|
|
|
func TestReverseMessages_Empty_Cov14(t *testing.T) {
|
|
msgs := []model.Message{}
|
|
reverseMessages(msgs)
|
|
assert.Empty(t, msgs)
|
|
}
|
|
|
|
func TestReverseMessages_Single_Cov14(t *testing.T) {
|
|
msgs := []model.Message{{Content: "a"}}
|
|
reverseMessages(msgs)
|
|
assert.Equal(t, "a", msgs[0].Content)
|
|
}
|
|
|
|
// === ConversationService tests ===
|
|
|
|
func TestConversationService_ListByStatus_Empty_Cov14(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.ListByStatus(context.Background(), 1, "open", 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, convs)
|
|
}
|
|
|
|
func TestConversationService_ListByAssignee_Empty_Cov14(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)
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, convs)
|
|
}
|
|
|
|
func TestConversationService_GetByID_NotFound_Cov14(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)
|
|
}
|
|
|
|
// === InboxService tests ===
|
|
|
|
func TestInboxService_Ready_NilSvc_Cov14(t *testing.T) {
|
|
var svc *InboxService
|
|
assert.False(t, svc.Ready())
|
|
}
|
|
|
|
func TestInboxService_Ready_WithRepo_Cov14(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
repo := repository.NewInboxRepo(db)
|
|
svc := NewInboxService(repo, nil, nil, nil, nil, nil, nil)
|
|
assert.True(t, svc.Ready())
|
|
}
|