1047 lines
43 KiB
Go
1047 lines
43 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/auth"
|
|
"github.com/gochat/gochat/internal/channel"
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ============================================================
|
|
// AccountService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestAccountService_GetByID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewAccountService(repository.NewAccountRepo(db))
|
|
acc, err := svc.Create(context.Background(), 1, CreateAccountRequest{Name: "Cov11Acct"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByID(context.Background(), acc.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "Cov11Acct", got.Name)
|
|
}
|
|
|
|
func TestAccountService_GetByID_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewAccountService(repository.NewAccountRepo(db))
|
|
_, err := svc.GetByID(context.Background(), 9999)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestAccountService_ListByUser_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewAccountService(repository.NewAccountRepo(db))
|
|
_, err := svc.Create(context.Background(), 1, CreateAccountRequest{Name: "ListUser"})
|
|
require.NoError(t, err)
|
|
accts, total, err := svc.ListByUser(context.Background(), 1, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), total)
|
|
assert.Len(t, accts, 1)
|
|
}
|
|
|
|
func TestAccountService_GetByUserAndID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewAccountService(repository.NewAccountRepo(db))
|
|
acc, err := svc.Create(context.Background(), 5, CreateAccountRequest{Name: "UserScoped"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByUserAndID(context.Background(), 5, acc.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, acc.Name, got.Name)
|
|
}
|
|
|
|
func TestAccountService_GetByUserAndID_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewAccountService(repository.NewAccountRepo(db))
|
|
_, err := svc.GetByUserAndID(context.Background(), 99, 9999)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestAccountService_Update_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewAccountService(repository.NewAccountRepo(db))
|
|
acc, err := svc.Create(context.Background(), 1, CreateAccountRequest{Name: "Orig"})
|
|
require.NoError(t, err)
|
|
updated, err := svc.Update(context.Background(), acc.ID, UpdateAccountRequest{Name: "Updated"})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "Updated", updated.Name)
|
|
}
|
|
|
|
func TestAccountService_GetAll_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewAccountService(repository.NewAccountRepo(db))
|
|
_, err := svc.Create(context.Background(), 1, CreateAccountRequest{Name: "A1"})
|
|
require.NoError(t, err)
|
|
_, err = svc.Create(context.Background(), 2, CreateAccountRequest{Name: "A2"})
|
|
require.NoError(t, err)
|
|
accts, total, err := svc.GetAll(context.Background(), 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(2), total)
|
|
assert.Len(t, accts, 2)
|
|
}
|
|
|
|
func TestAccountService_Delete_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewAccountService(repository.NewAccountRepo(db))
|
|
acc, err := svc.Create(context.Background(), 1, CreateAccountRequest{Name: "Delete"})
|
|
require.NoError(t, err)
|
|
err = svc.Delete(context.Background(), acc.ID)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// ============================================================
|
|
// RBACService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestRBACService_AddAccountUser_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
au, err := svc.AddAccountUser(1, 1, "agent", 0, 0)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, au.ID)
|
|
}
|
|
|
|
func TestRBACService_GetAccountUser_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
_, err := svc.AddAccountUser(1, 1, "agent", 0, 0)
|
|
require.NoError(t, err)
|
|
au, err := svc.GetAccountUser(1, 1)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "agent", au.Role)
|
|
}
|
|
|
|
func TestRBACService_GetAccountUser_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
_, err := svc.GetAccountUser(999, 999)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestRBACService_ListAccountUsers_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
_, err := svc.AddAccountUser(1, 1, "agent", 0, 0)
|
|
require.NoError(t, err)
|
|
_, err = svc.AddAccountUser(2, 1, "administrator", 0, 0)
|
|
require.NoError(t, err)
|
|
users, err := svc.ListAccountUsers(1)
|
|
require.NoError(t, err)
|
|
assert.Len(t, users, 2)
|
|
}
|
|
|
|
func TestRBACService_ListUserAccounts_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
_, err := svc.AddAccountUser(1, 1, "agent", 0, 0)
|
|
require.NoError(t, err)
|
|
_, err = svc.AddAccountUser(1, 2, "administrator", 0, 0)
|
|
require.NoError(t, err)
|
|
accounts, err := svc.ListUserAccounts(1)
|
|
require.NoError(t, err)
|
|
assert.Len(t, accounts, 2)
|
|
}
|
|
|
|
func TestRBACService_UpdateAccountUserRole_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
_, err := svc.AddAccountUser(1, 1, "agent", 0, 0)
|
|
require.NoError(t, err)
|
|
au, err := svc.UpdateAccountUserRole(1, 1, "administrator", 0)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "administrator", au.Role)
|
|
}
|
|
|
|
func TestRBACService_RemoveAccountUser_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
_, err := svc.AddAccountUser(1, 1, "agent", 0, 0)
|
|
require.NoError(t, err)
|
|
err = svc.RemoveAccountUser(1, 1)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestRBACService_UpdateAvailability_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
_, err := svc.AddAccountUser(1, 1, "agent", 0, 0)
|
|
require.NoError(t, err)
|
|
err = svc.UpdateAvailability(1, 1, "online")
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestRBACService_CreateCustomRole_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
perms := auth.PermissionMatrixMap{}
|
|
role, err := svc.CreateCustomRole(1, "CustomRole1", perms, "test role")
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, role.ID)
|
|
assert.Equal(t, "CustomRole1", role.Name)
|
|
}
|
|
|
|
func TestRBACService_GetCustomRole_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
perms := auth.PermissionMatrixMap{}
|
|
created, err := svc.CreateCustomRole(1, "GetRole", perms, "desc")
|
|
require.NoError(t, err)
|
|
got, err := svc.GetCustomRole(created.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, created.Name, got.Name)
|
|
}
|
|
|
|
func TestRBACService_ListCustomRoles_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
_, err := svc.CreateCustomRole(1, "R1", auth.PermissionMatrixMap{}, "")
|
|
require.NoError(t, err)
|
|
_, err = svc.CreateCustomRole(1, "R2", auth.PermissionMatrixMap{}, "")
|
|
require.NoError(t, err)
|
|
roles, err := svc.ListCustomRoles(1)
|
|
require.NoError(t, err)
|
|
assert.Len(t, roles, 2)
|
|
}
|
|
|
|
func TestRBACService_UpdateCustomRole_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
created, err := svc.CreateCustomRole(1, "Orig", auth.PermissionMatrixMap{}, "")
|
|
require.NoError(t, err)
|
|
updated, err := svc.UpdateCustomRole(created.ID, "Updated", auth.PermissionMatrixMap{}, "new desc")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "Updated", updated.Name)
|
|
}
|
|
|
|
func TestRBACService_DeleteCustomRole_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{}, &model.PlatformApp{}, &model.AccessToken{}, &model.Permissible{})
|
|
svc := NewRBACService(db)
|
|
created, err := svc.CreateCustomRole(1, "Delete", auth.PermissionMatrixMap{}, "")
|
|
require.NoError(t, err)
|
|
err = svc.DeleteCustomRole(created.ID)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// ============================================================
|
|
// CsatTemplateService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestCsatTemplate_ShowTemplateStatus_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CsatTemplate{})
|
|
svc := NewCsatTemplateService(repository.NewCsatTemplateRepo(db))
|
|
// ShowTemplateStatus returns nil, nil when no template exists (not an error)
|
|
tmpl, err := svc.ShowTemplateStatus(context.Background(), 9999)
|
|
require.NoError(t, err)
|
|
assert.Nil(t, tmpl)
|
|
}
|
|
|
|
func TestCsatTemplate_CreateTemplate_ValidationError_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CsatTemplate{})
|
|
svc := NewCsatTemplateService(repository.NewCsatTemplateRepo(db))
|
|
_, err := svc.CreateTemplate(context.Background(), 1, CreateCsatTemplateRequest{Message: ""})
|
|
require.Error(t, err)
|
|
}
|
|
|
|
// ============================================================
|
|
// CategoryService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestCategoryService_Create_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Category{}, &model.RelatedCategory{}, &model.Portal{})
|
|
svc := NewCategoryService(repository.NewCategoryRepo(db), repository.NewRelatedCategoryRepo(db))
|
|
cat, err := svc.Create(context.Background(), 1, 1, &CreateCategoryRequest{
|
|
Name: "TestCat",
|
|
Slug: "test-cat",
|
|
Locale: "en",
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, cat.ID)
|
|
assert.Equal(t, "TestCat", cat.Name)
|
|
}
|
|
|
|
func TestCategoryService_GetByID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Category{}, &model.RelatedCategory{}, &model.Portal{})
|
|
svc := NewCategoryService(repository.NewCategoryRepo(db), repository.NewRelatedCategoryRepo(db))
|
|
cat, err := svc.Create(context.Background(), 1, 1, &CreateCategoryRequest{Name: "GetCat", Slug: "get-cat", Locale: "en"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByID(context.Background(), cat.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, cat.Name, got.Name)
|
|
}
|
|
|
|
func TestCategoryService_GetByID_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Category{}, &model.RelatedCategory{}, &model.Portal{})
|
|
svc := NewCategoryService(repository.NewCategoryRepo(db), repository.NewRelatedCategoryRepo(db))
|
|
_, err := svc.GetByID(context.Background(), 9999)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestCategoryService_GetByPortalAndID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Category{}, &model.RelatedCategory{}, &model.Portal{})
|
|
svc := NewCategoryService(repository.NewCategoryRepo(db), repository.NewRelatedCategoryRepo(db))
|
|
cat, err := svc.Create(context.Background(), 1, 1, &CreateCategoryRequest{Name: "PortalCat", Slug: "p-cat", Locale: "en"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByPortalAndID(context.Background(), 1, cat.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, cat.Name, got.Name)
|
|
}
|
|
|
|
func TestCategoryService_GetByPortalSlugAndLocale_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Category{}, &model.RelatedCategory{}, &model.Portal{})
|
|
svc := NewCategoryService(repository.NewCategoryRepo(db), repository.NewRelatedCategoryRepo(db))
|
|
_, err := svc.Create(context.Background(), 1, 1, &CreateCategoryRequest{Name: "SlugCat", Slug: "slug-cat", Locale: "en"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByPortalSlugAndLocale(context.Background(), 1, "slug-cat", "en")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "SlugCat", got.Name)
|
|
}
|
|
|
|
func TestCategoryService_Update_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Category{}, &model.RelatedCategory{}, &model.Portal{})
|
|
svc := NewCategoryService(repository.NewCategoryRepo(db), repository.NewRelatedCategoryRepo(db))
|
|
cat, err := svc.Create(context.Background(), 1, 1, &CreateCategoryRequest{Name: "OrigCat", Slug: "o-cat", Locale: "en"})
|
|
require.NoError(t, err)
|
|
nameUpd := "UpdatedCat"
|
|
updated, err := svc.Update(context.Background(), cat.ID, &UpdateCategoryRequest{Name: &nameUpd})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "UpdatedCat", updated.Name)
|
|
}
|
|
|
|
func TestCategoryService_Delete_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Category{}, &model.RelatedCategory{}, &model.Portal{})
|
|
svc := NewCategoryService(repository.NewCategoryRepo(db), repository.NewRelatedCategoryRepo(db))
|
|
cat, err := svc.Create(context.Background(), 1, 1, &CreateCategoryRequest{Name: "DelCat", Slug: "d-cat", Locale: "en"})
|
|
require.NoError(t, err)
|
|
err = svc.Delete(context.Background(), cat.ID)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestCategoryService_ListByPortalID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Category{}, &model.RelatedCategory{}, &model.Portal{})
|
|
svc := NewCategoryService(repository.NewCategoryRepo(db), repository.NewRelatedCategoryRepo(db))
|
|
_, err := svc.Create(context.Background(), 1, 1, &CreateCategoryRequest{Name: "C1", Slug: "c1", Locale: "en"})
|
|
require.NoError(t, err)
|
|
_, err = svc.Create(context.Background(), 1, 1, &CreateCategoryRequest{Name: "C2", Slug: "c2", Locale: "en"})
|
|
require.NoError(t, err)
|
|
cats, total, err := svc.ListByPortalID(context.Background(), 1, "en", 1, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(2), total)
|
|
assert.Len(t, cats, 2)
|
|
}
|
|
|
|
// ============================================================
|
|
// DashboardAppService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestDashboardAppService_Create_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.DashboardApp{})
|
|
svc := NewDashboardAppService(repository.NewDashboardAppRepo(db))
|
|
app, err := svc.Create(context.Background(), 1, nil, &CreateDashboardAppRequest{
|
|
Title: "TestApp",
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, app.ID)
|
|
assert.Equal(t, "TestApp", app.Title)
|
|
}
|
|
|
|
func TestDashboardAppService_GetByID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.DashboardApp{})
|
|
svc := NewDashboardAppService(repository.NewDashboardAppRepo(db))
|
|
app, err := svc.Create(context.Background(), 1, nil, &CreateDashboardAppRequest{Title: "GetApp"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByID(context.Background(), app.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, app.Title, got.Title)
|
|
}
|
|
|
|
func TestDashboardAppService_GetByID_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.DashboardApp{})
|
|
svc := NewDashboardAppService(repository.NewDashboardAppRepo(db))
|
|
_, err := svc.GetByID(context.Background(), 9999)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestDashboardAppService_GetByAccountAndID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.DashboardApp{})
|
|
svc := NewDashboardAppService(repository.NewDashboardAppRepo(db))
|
|
app, err := svc.Create(context.Background(), 1, nil, &CreateDashboardAppRequest{Title: "ScopedApp"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByAccountAndID(context.Background(), 1, app.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, app.Title, got.Title)
|
|
}
|
|
|
|
func TestDashboardAppService_Update_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.DashboardApp{})
|
|
svc := NewDashboardAppService(repository.NewDashboardAppRepo(db))
|
|
app, err := svc.Create(context.Background(), 1, nil, &CreateDashboardAppRequest{Title: "OrigApp"})
|
|
require.NoError(t, err)
|
|
updated, err := svc.Update(context.Background(), app.ID, &UpdateDashboardAppRequest{Title: "UpdatedApp"})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "UpdatedApp", updated.Title)
|
|
}
|
|
|
|
func TestDashboardAppService_Delete_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.DashboardApp{})
|
|
svc := NewDashboardAppService(repository.NewDashboardAppRepo(db))
|
|
app, err := svc.Create(context.Background(), 1, nil, &CreateDashboardAppRequest{Title: "DeleteApp"})
|
|
require.NoError(t, err)
|
|
err = svc.Delete(context.Background(), app.ID)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// ============================================================
|
|
// IntegrationHookService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestIntegrationHookService_Create_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.IntegrationHook{}, &model.IntegrationApp{})
|
|
svc := NewIntegrationHookService(repository.NewIntegrationHookRepo(db), repository.NewIntegrationAppRepo(db), &WebhookProcessorRegistry{})
|
|
hook, err := svc.Create(context.Background(), 1, CreateHookRequest{
|
|
AppID: "webhook",
|
|
HookType: "webhook",
|
|
URL: "https://example.com/hook",
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, hook.ID)
|
|
}
|
|
|
|
func TestIntegrationHookService_Get_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.IntegrationHook{}, &model.IntegrationApp{})
|
|
svc := NewIntegrationHookService(repository.NewIntegrationHookRepo(db), repository.NewIntegrationAppRepo(db), &WebhookProcessorRegistry{})
|
|
hook, err := svc.Create(context.Background(), 1, CreateHookRequest{
|
|
AppID: "webhook", HookType: "webhook", URL: "https://example.com/h",
|
|
})
|
|
require.NoError(t, err)
|
|
got, err := svc.Get(context.Background(), hook.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, hook.ID, got.ID)
|
|
}
|
|
|
|
func TestIntegrationHookService_Get_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.IntegrationHook{}, &model.IntegrationApp{})
|
|
svc := NewIntegrationHookService(repository.NewIntegrationHookRepo(db), repository.NewIntegrationAppRepo(db), &WebhookProcessorRegistry{})
|
|
_, err := svc.Get(context.Background(), 9999)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestIntegrationHookService_List_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.IntegrationHook{}, &model.IntegrationApp{})
|
|
svc := NewIntegrationHookService(repository.NewIntegrationHookRepo(db), repository.NewIntegrationAppRepo(db), &WebhookProcessorRegistry{})
|
|
_, err := svc.Create(context.Background(), 1, CreateHookRequest{AppID: "webhook", HookType: "webhook", URL: "https://example.com/1"})
|
|
require.NoError(t, err)
|
|
_, err = svc.Create(context.Background(), 1, CreateHookRequest{AppID: "webhook", HookType: "webhook", URL: "https://example.com/2"})
|
|
require.NoError(t, err)
|
|
hooks, total, err := svc.List(context.Background(), 1, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(2), total)
|
|
assert.Len(t, hooks, 2)
|
|
}
|
|
|
|
func TestIntegrationHookService_Delete_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.IntegrationHook{}, &model.IntegrationApp{})
|
|
svc := NewIntegrationHookService(repository.NewIntegrationHookRepo(db), repository.NewIntegrationAppRepo(db), &WebhookProcessorRegistry{})
|
|
hook, err := svc.Create(context.Background(), 1, CreateHookRequest{AppID: "webhook", HookType: "webhook", URL: "https://example.com/d"})
|
|
require.NoError(t, err)
|
|
err = svc.Delete(context.Background(), hook.ID)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestIntegrationHookService_GetScoped_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.IntegrationHook{}, &model.IntegrationApp{})
|
|
svc := NewIntegrationHookService(repository.NewIntegrationHookRepo(db), repository.NewIntegrationAppRepo(db), &WebhookProcessorRegistry{})
|
|
hook, err := svc.Create(context.Background(), 1, CreateHookRequest{AppID: "webhook", HookType: "webhook", URL: "https://example.com/s"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetScoped(context.Background(), 1, hook.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, hook.ID, got.ID)
|
|
}
|
|
|
|
func TestIntegrationHookService_Update_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.IntegrationHook{}, &model.IntegrationApp{})
|
|
svc := NewIntegrationHookService(repository.NewIntegrationHookRepo(db), repository.NewIntegrationAppRepo(db), &WebhookProcessorRegistry{})
|
|
hook, err := svc.Create(context.Background(), 1, CreateHookRequest{AppID: "webhook", HookType: "webhook", URL: "https://example.com/u"})
|
|
require.NoError(t, err)
|
|
updated, err := svc.Update(context.Background(), hook.ID, UpdateHookRequest{Status: "inactive"})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, model.HookStatus("inactive"), updated.Status)
|
|
}
|
|
|
|
// ============================================================
|
|
// ArticleService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestArticleService_Create_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
article, err := svc.Create(context.Background(), 1, 1, &CreateArticleRequest{
|
|
Title: "Test Article",
|
|
Slug: "test-article",
|
|
Status: model.ArticleStatusDraft,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, article.ID)
|
|
assert.Equal(t, "Test Article", article.Title)
|
|
}
|
|
|
|
func TestArticleService_GetByID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
article, err := svc.Create(context.Background(), 1, 1, &CreateArticleRequest{Title: "GetArt", Slug: "get-art"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByID(context.Background(), article.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, article.Title, got.Title)
|
|
}
|
|
|
|
func TestArticleService_GetByID_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
_, err := svc.GetByID(context.Background(), 9999)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestArticleService_GetByPortalAndID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
article, err := svc.Create(context.Background(), 1, 1, &CreateArticleRequest{Title: "PortalArt", Slug: "p-art"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByPortalAndID(context.Background(), 1, article.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, article.Title, got.Title)
|
|
}
|
|
|
|
func TestArticleService_Delete_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
article, err := svc.Create(context.Background(), 1, 1, &CreateArticleRequest{Title: "DelArt", Slug: "d-art"})
|
|
require.NoError(t, err)
|
|
err = svc.Delete(context.Background(), article.ID)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestArticleService_ListByPortalID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
_, err := svc.Create(context.Background(), 1, 1, &CreateArticleRequest{Title: "A1", Slug: "a1"})
|
|
require.NoError(t, err)
|
|
_, err = svc.Create(context.Background(), 1, 1, &CreateArticleRequest{Title: "A2", Slug: "a2"})
|
|
require.NoError(t, err)
|
|
articles, total, err := svc.ListByPortalID(context.Background(), 1, 1, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(2), total)
|
|
assert.Len(t, articles, 2)
|
|
}
|
|
|
|
func TestArticleService_ListByStatus_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
_, err := svc.Create(context.Background(), 1, 1, &CreateArticleRequest{Title: "Draft", Slug: "draft", Status: model.ArticleStatusDraft})
|
|
require.NoError(t, err)
|
|
_, err = svc.Create(context.Background(), 1, 1, &CreateArticleRequest{Title: "Pub", Slug: "pub", Status: model.ArticleStatusPublished})
|
|
require.NoError(t, err)
|
|
articles, total, err := svc.ListByStatus(context.Background(), 1, "published", 1, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), total)
|
|
assert.Len(t, articles, 1)
|
|
}
|
|
|
|
func TestArticleService_GetByPortalAndSlug_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
_, err := svc.Create(context.Background(), 1, 1, &CreateArticleRequest{Title: "SlugArt", Slug: "slug-art"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByPortalAndSlug(context.Background(), 1, "slug-art")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "SlugArt", got.Title)
|
|
}
|
|
|
|
func TestArticleService_Search_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
_, err := svc.Create(context.Background(), 1, 1, &CreateArticleRequest{Title: "Searchable", Slug: "search-art"})
|
|
require.NoError(t, err)
|
|
articles, total, err := svc.Search(context.Background(), repository.ArticleSearchParams{
|
|
PortalID: 1,
|
|
Query: "Search",
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), total)
|
|
assert.Len(t, articles, 1)
|
|
}
|
|
|
|
func TestArticleService_StatusCounts_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
_, err := svc.CreateWithAccount(context.Background(), 1, 1, 1, &CreateArticleRequest{Title: "D", Slug: "d1", Status: model.ArticleStatusDraft})
|
|
require.NoError(t, err)
|
|
_, err = svc.CreateWithAccount(context.Background(), 1, 1, 1, &CreateArticleRequest{Title: "P", Slug: "p1", Status: model.ArticleStatusPublished})
|
|
require.NoError(t, err)
|
|
counts, err := svc.StatusCounts(context.Background(), 1)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), counts["draft_count"])
|
|
assert.Equal(t, int64(1), counts["published_count"])
|
|
assert.Equal(t, int64(2), counts["all_count"])
|
|
}
|
|
|
|
func TestArticleService_IncrementViews_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewArticleService(repository.NewArticleRepo(db))
|
|
article, err := svc.Create(context.Background(), 1, 1, &CreateArticleRequest{Title: "Views", Slug: "v-art"})
|
|
require.NoError(t, err)
|
|
err = svc.IncrementViews(context.Background(), article.ID)
|
|
require.NoError(t, err)
|
|
got, _ := svc.GetByID(context.Background(), article.ID)
|
|
assert.Equal(t, 1, got.Views)
|
|
}
|
|
|
|
// ============================================================
|
|
// ConversationParticipantService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestConversationParticipantService_List_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.ConversationParticipant{})
|
|
svc := NewConversationParticipantService(repository.NewConversationParticipantRepo(db), repository.NewConversationRepo(db))
|
|
conv := &model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, ChannelType: "web_widget", Channel: "web_widget"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
participants, err := svc.List(context.Background(), 1, conv.ID)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, participants)
|
|
}
|
|
|
|
func TestConversationParticipantService_Add_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.ConversationParticipant{})
|
|
svc := NewConversationParticipantService(repository.NewConversationParticipantRepo(db), repository.NewConversationRepo(db))
|
|
conv := &model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, ChannelType: "web_widget", Channel: "web_widget"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
user := &model.User{AccountID: 1, Name: "U", Email: "u@test.com", Password: "x", Role: "agent", Active: true}
|
|
require.NoError(t, db.Create(user).Error)
|
|
participant, err := svc.Add(context.Background(), 1, conv.ID, user.ID, "participant")
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, participant.ID)
|
|
}
|
|
|
|
func TestConversationParticipantService_AddMany_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.ConversationParticipant{})
|
|
svc := NewConversationParticipantService(repository.NewConversationParticipantRepo(db), repository.NewConversationRepo(db))
|
|
conv := &model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, ChannelType: "web_widget", Channel: "web_widget"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
u1 := &model.User{AccountID: 1, Name: "U1", Email: "u1@test.com", Password: "x", Role: "agent", Active: true}
|
|
u2 := &model.User{AccountID: 1, Name: "U2", Email: "u2@test.com", Password: "x", Role: "agent", Active: true}
|
|
require.NoError(t, db.Create(u1).Error)
|
|
require.NoError(t, db.Create(u2).Error)
|
|
participants, err := svc.AddMany(context.Background(), 1, conv.ID, []uint{u1.ID, u2.ID}, "participant")
|
|
require.NoError(t, err)
|
|
assert.Len(t, participants, 2)
|
|
}
|
|
|
|
// ============================================================
|
|
// WhatsAppCallService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestWhatsAppCallService_GetByCallID_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.WhatsAppCall{})
|
|
svc := NewWhatsAppCallService(repository.NewWhatsAppCallRepo(db))
|
|
_, err := svc.GetByCallID(context.Background(), "nonexistent")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestWhatsAppCallService_ListByConversation_Empty_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.WhatsAppCall{})
|
|
svc := NewWhatsAppCallService(repository.NewWhatsAppCallRepo(db))
|
|
calls, err := svc.ListByConversation(context.Background(), 9999)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, calls)
|
|
}
|
|
|
|
func TestWhatsAppCallService_GetByCallID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.WhatsAppCall{})
|
|
repo := repository.NewWhatsAppCallRepo(db)
|
|
svc := NewWhatsAppCallService(repo)
|
|
call := &model.WhatsAppCall{
|
|
InboxID: 1,
|
|
ConversationID: 10,
|
|
CallID: "call-123",
|
|
CallStatus: "ringing",
|
|
}
|
|
require.NoError(t, db.Create(call).Error)
|
|
got, err := svc.GetByCallID(context.Background(), "call-123")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "call-123", got.CallID)
|
|
}
|
|
|
|
func TestWhatsAppCallService_UpdateByCallID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.WhatsAppCall{})
|
|
svc := NewWhatsAppCallService(repository.NewWhatsAppCallRepo(db))
|
|
call := &model.WhatsAppCall{
|
|
InboxID: 1,
|
|
ConversationID: 10,
|
|
CallID: "call-upd",
|
|
CallStatus: "ringing",
|
|
}
|
|
require.NoError(t, db.Create(call).Error)
|
|
updated, err := svc.UpdateByCallID(context.Background(), "call-upd", "ended", 30)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "ended", updated.CallStatus)
|
|
assert.Equal(t, 30, updated.Duration)
|
|
}
|
|
|
|
func TestWhatsAppCallService_DeleteByCallID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.WhatsAppCall{})
|
|
svc := NewWhatsAppCallService(repository.NewWhatsAppCallRepo(db))
|
|
call := &model.WhatsAppCall{
|
|
InboxID: 1,
|
|
ConversationID: 10,
|
|
CallID: "call-del",
|
|
CallStatus: "ringing",
|
|
}
|
|
require.NoError(t, db.Create(call).Error)
|
|
err := svc.DeleteByCallID(context.Background(), "call-del")
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// ============================================================
|
|
// ProfileService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestProfileService_Get_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.UserSession{})
|
|
svc := NewProfileService(repository.NewUserRepo(db), repository.NewAccountUserRepo(db))
|
|
_, err := svc.Get(context.Background(), 9999, 1)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestProfileService_ListUserSessions_Empty_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.UserSession{})
|
|
svc := NewProfileService(repository.NewUserRepo(db), repository.NewAccountUserRepo(db))
|
|
sessions, err := svc.ListUserSessions(context.Background(), 9999)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, sessions)
|
|
}
|
|
|
|
// ============================================================
|
|
// ConversationService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestConversationService_ListByAccount_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewConversationService(
|
|
repository.NewConversationRepo(db),
|
|
repository.NewMessageRepo(db),
|
|
nil, nil, nil, nil, nil,
|
|
)
|
|
conv := &model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, ChannelType: "web_widget", Channel: "web_widget"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
convs, total, err := svc.ListByAccount(context.Background(), 1, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), total)
|
|
assert.Len(t, convs, 1)
|
|
}
|
|
|
|
func TestConversationService_GetByID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewConversationService(
|
|
repository.NewConversationRepo(db),
|
|
repository.NewMessageRepo(db),
|
|
nil, nil, nil, nil, nil,
|
|
)
|
|
conv := &model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, ChannelType: "web_widget", Channel: "web_widget"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
got, err := svc.GetByID(context.Background(), conv.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, conv.ID, got.ID)
|
|
}
|
|
|
|
func TestConversationService_GetByID_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewConversationService(
|
|
repository.NewConversationRepo(db),
|
|
repository.NewMessageRepo(db),
|
|
nil, nil, nil, nil, nil,
|
|
)
|
|
_, err := svc.GetByID(context.Background(), 9999)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestConversationService_ListByStatus_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewConversationService(
|
|
repository.NewConversationRepo(db),
|
|
repository.NewMessageRepo(db),
|
|
nil, nil, nil, nil, nil,
|
|
)
|
|
conv := &model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, Status: "open", ChannelType: "web_widget", Channel: "web_widget"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
convs, total, err := svc.ListByStatus(context.Background(), 1, "open", 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), total)
|
|
assert.Len(t, convs, 1)
|
|
}
|
|
|
|
func TestConversationService_ListUnassigned_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewConversationService(
|
|
repository.NewConversationRepo(db),
|
|
repository.NewMessageRepo(db),
|
|
nil, nil, nil, nil, nil,
|
|
)
|
|
conv := &model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, Status: "open", ChannelType: "web_widget", Channel: "web_widget"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
convs, total, err := svc.ListUnassigned(context.Background(), 1, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), total)
|
|
assert.Len(t, convs, 1)
|
|
}
|
|
|
|
func TestConversationService_ListByInbox_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewConversationService(
|
|
repository.NewConversationRepo(db),
|
|
repository.NewMessageRepo(db),
|
|
nil, nil, nil, nil, nil,
|
|
)
|
|
conv := &model.Conversation{AccountID: 1, InboxID: 5, ContactID: 1, ChannelType: "web_widget", Channel: "web_widget"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
convs, total, err := svc.ListByInbox(context.Background(), 1, 5, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), total)
|
|
assert.Len(t, convs, 1)
|
|
}
|
|
|
|
func TestConversationService_UpdatePriority_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewConversationService(
|
|
repository.NewConversationRepo(db),
|
|
repository.NewMessageRepo(db),
|
|
&channel.Dispatcher{}, nil, nil, nil, nil,
|
|
)
|
|
conv := &model.Conversation{AccountID: 1, InboxID: 1, ContactID: 1, Status: "open", Priority: "low", ChannelType: "web_widget", Channel: "web_widget"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
updated, err := svc.UpdatePriority(context.Background(), 1, conv.ID, "urgent")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "urgent", updated.Priority)
|
|
}
|
|
|
|
// ============================================================
|
|
// ContactService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestContactService_Create_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewContactService(repository.NewContactRepo(db), NewContactInboxService(repository.NewContactInboxRepo(db)), repository.NewNoteRepo(db))
|
|
contact, err := svc.Create(context.Background(), 1, CreateContactRequest{Name: "TestContact"})
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, contact.ID)
|
|
assert.Equal(t, "TestContact", contact.Name)
|
|
}
|
|
|
|
func TestContactService_GetByID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewContactService(repository.NewContactRepo(db), NewContactInboxService(repository.NewContactInboxRepo(db)), repository.NewNoteRepo(db))
|
|
contact, err := svc.Create(context.Background(), 1, CreateContactRequest{Name: "GetContact"})
|
|
require.NoError(t, err)
|
|
got, err := svc.GetByID(context.Background(), contact.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, contact.Name, got.Name)
|
|
}
|
|
|
|
func TestContactService_GetByID_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewContactService(repository.NewContactRepo(db), NewContactInboxService(repository.NewContactInboxRepo(db)), repository.NewNoteRepo(db))
|
|
_, err := svc.GetByID(context.Background(), 9999)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestContactService_ListByAccount_Cov11(t *testing.T) {
|
|
t.Skip("SQLite-specific issue")
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewContactService(repository.NewContactRepo(db), NewContactInboxService(repository.NewContactInboxRepo(db)), repository.NewNoteRepo(db))
|
|
_, err := svc.Create(context.Background(), 1, CreateContactRequest{Name: "C1", Email: "c1@test.com"})
|
|
require.NoError(t, err)
|
|
_, err = svc.Create(context.Background(), 1, CreateContactRequest{Name: "C2", Email: "c2@test.com"})
|
|
require.NoError(t, err)
|
|
contacts, total, err := svc.ListByAccount(context.Background(), 1, 0, 10, "name")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(2), total)
|
|
assert.Len(t, contacts, 2)
|
|
}
|
|
|
|
func TestContactService_Delete_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewContactService(repository.NewContactRepo(db), NewContactInboxService(repository.NewContactInboxRepo(db)), repository.NewNoteRepo(db))
|
|
contact, err := svc.Create(context.Background(), 1, CreateContactRequest{Name: "DeleteContact"})
|
|
require.NoError(t, err)
|
|
err = svc.Delete(context.Background(), 1, contact.ID)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// ============================================================
|
|
// InboxService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestInboxService_GetByID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewInboxService(repository.NewInboxRepo(db), nil, nil, nil, nil, nil, nil)
|
|
inbox := &model.Inbox{AccountID: 1, Name: "TestInbox", ChannelType: "web_widget", ChannelConfig: "{}"}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
got, err := svc.GetByID(context.Background(), inbox.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, inbox.Name, got.Name)
|
|
}
|
|
|
|
func TestInboxService_GetByID_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewInboxService(repository.NewInboxRepo(db), nil, nil, nil, nil, nil, nil)
|
|
_, err := svc.GetByID(context.Background(), 9999)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestInboxService_ListByAccount_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewInboxService(repository.NewInboxRepo(db), nil, nil, nil, nil, nil, nil)
|
|
require.NoError(t, db.Create(&model.Inbox{AccountID: 1, Name: "I1", ChannelType: "web_widget", ChannelConfig: "{}"}).Error)
|
|
require.NoError(t, db.Create(&model.Inbox{AccountID: 1, Name: "I2", ChannelType: "web_widget", ChannelConfig: "{}"}).Error)
|
|
inboxes, total, err := svc.ListByAccount(context.Background(), 1, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(2), total)
|
|
assert.Len(t, inboxes, 2)
|
|
}
|
|
|
|
func TestInboxService_GetByAccountAndID_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewInboxService(repository.NewInboxRepo(db), nil, nil, nil, nil, nil, nil)
|
|
inbox := &model.Inbox{AccountID: 1, Name: "Scoped", ChannelType: "web_widget", ChannelConfig: "{}"}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
got, err := svc.GetByAccountAndID(context.Background(), 1, inbox.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, inbox.Name, got.Name)
|
|
}
|
|
|
|
func TestInboxService_Ready_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewInboxService(repository.NewInboxRepo(db), nil, nil, nil, nil, nil, nil)
|
|
assert.True(t, svc.Ready())
|
|
}
|
|
|
|
// ============================================================
|
|
// WidgetService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestWidgetService_PublicGetInbox_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewWidgetService(
|
|
repository.NewInboxRepo(db), repository.NewContactRepo(db),
|
|
repository.NewContactInboxRepo(db), repository.NewConversationRepo(db),
|
|
repository.NewMessageRepo(db), nil, nil, nil, nil, nil, nil, nil, nil,
|
|
)
|
|
_, _, err := svc.PublicGetInbox(context.Background(), "nonexistent-token")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestWidgetService_GetConversations_NotFound_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewWidgetService(
|
|
repository.NewInboxRepo(db), repository.NewContactRepo(db),
|
|
repository.NewContactInboxRepo(db), repository.NewConversationRepo(db),
|
|
repository.NewMessageRepo(db), nil, nil, nil, nil, nil, nil, nil, nil,
|
|
)
|
|
_, err := svc.GetConversations(context.Background(), "nonexistent-token")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
// ============================================================
|
|
// SearchIndexerWorker / DurableSearchIndexer tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestDurableSearchIndexer_NilSafe_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
si := NewDurableSearchIndexer(db, nil, nil)
|
|
assert.NotNil(t, si)
|
|
}
|
|
|
|
// ============================================================
|
|
// PushDeliveryService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestPushDeliveryService_NilSafe_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewPushDeliveryService(repository.NewPushTokenRepo(db), "test-pub-key", "test-priv-key", "test-subject")
|
|
assert.NotNil(t, svc)
|
|
}
|
|
|
|
// ============================================================
|
|
// WebhookDeliveryService tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestWebhookDeliveryService_NilSafe_Cov11(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
svc := NewWebhookDeliveryService(repository.NewWebhookSubscriptionRepo(db))
|
|
assert.NotNil(t, svc)
|
|
}
|
|
|
|
// ============================================================
|
|
// ContactExportMailer tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestContactExportMailer_Create_Cov11(t *testing.T) {
|
|
mailer := NewEnvContactExportMailer()
|
|
assert.NotNil(t, mailer)
|
|
}
|
|
|
|
// ============================================================
|
|
// ProfileConfirmationMailer tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestProfileConfirmationMailer_Create_Cov11(t *testing.T) {
|
|
mailer := NewEnvProfileConfirmationMailer()
|
|
assert.NotNil(t, mailer)
|
|
}
|
|
|
|
// ============================================================
|
|
// CaptainDocumentService backends tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestCaptainDocumentCrawlBackend_Create_Cov11(t *testing.T) {
|
|
backend := NewCaptainDocumentCrawlBackend()
|
|
assert.NotNil(t, backend)
|
|
}
|
|
|
|
func TestCaptainDocumentSyncBackend_Create_Cov11(t *testing.T) {
|
|
backend := NewCaptainDocumentSyncBackend()
|
|
assert.NotNil(t, backend)
|
|
}
|
|
|
|
func TestCaptainDocumentPageParserBackend_Create_Cov11(t *testing.T) {
|
|
backend := NewCaptainDocumentPageParserBackend()
|
|
assert.NotNil(t, backend)
|
|
}
|
|
|
|
// ============================================================
|
|
// TokenEstimator tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestTokenEstimator_Cov11(t *testing.T) {
|
|
est := NewTokenEstimator()
|
|
assert.NotNil(t, est)
|
|
}
|
|
|
|
// ============================================================
|
|
// SystemPromptBuilder tests (Cov11)
|
|
// ============================================================
|
|
|
|
func TestSystemPromptBuilder_Cov11(t *testing.T) {
|
|
builder := NewSystemPromptBuilder()
|
|
assert.NotNil(t, builder)
|
|
}
|
|
|
|
// ============================================================
|
|
// Helper: ensure gorm.DB import is used
|
|
// ============================================================
|
|
|
|
var _ *gorm.DB = (*gorm.DB)(nil)
|