package v1 import ( "net/http" "net/http/httptest" "strings" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/require" "gorm.io/driver/sqlite" "gorm.io/gorm" "github.com/gochat/gochat/internal/model" "github.com/gochat/gochat/internal/repository" "github.com/gochat/gochat/internal/service" ) func newTestDB_Cov18(t *testing.T) *gorm.DB { t.Helper() db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) require.NoError(t, err) models := []interface{}{ &model.Account{}, &model.User{}, &model.AccountUser{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{}, &model.Conversation{}, &model.Message{}, &model.Attachment{}, &model.Notification{}, &model.NotificationPreference{}, &model.NotificationSetting{}, &model.NotificationSubscription{}, &model.CustomRole{}, &model.PlatformApp{}, &model.Permissible{}, &model.AgentBot{}, &model.AgentBotInbox{}, &model.AgentCapacityPolicy{}, &model.AssignmentPolicy{}, &model.Article{}, &model.Banner{}, &model.Category{}, &model.Company{}, &model.ConversationLabel{}, &model.ConversationParticipant{}, &model.CsatTemplate{}, &model.CustomAttributeDefinition{}, &model.CustomFilter{}, &model.DashboardApp{}, &model.DeliveryStatus{}, &model.DraftMessage{}, &model.Folder{}, &model.InboxLimit{}, &model.InboxMember{}, &model.InstallationConfig{}, &model.IntegrationHook{}, &model.Note{}, &model.Portal{}, &model.PortalMember{}, &model.PushToken{}, &model.ReportingEvent{}, &model.SlaPolicy{}, &model.Tag{}, &model.Team{}, &model.TeamMember{}, &model.WorkingHour{}, &model.CaptainAssistant{}, &model.CaptainDocument{}, &model.CaptainPreference{}, } for _, m := range models { require.NoError(t, db.AutoMigrate(m)) } return db } func seedAccount_Cov18(t *testing.T, db *gorm.DB) uint { t.Helper() acc := model.Account{Name: "test-account"} require.NoError(t, db.Create(&acc).Error) return acc.ID } func seedUser_Cov18(t *testing.T, db *gorm.DB, accountID uint) uint { t.Helper() u := model.User{Name: "test-user", Email: "test@test.com", AccountID: accountID} require.NoError(t, db.Create(&u).Error) return u.ID } func seedInbox_Cov18(t *testing.T, db *gorm.DB, accountID uint) uint { t.Helper() inbox := model.Inbox{Name: "test-inbox", AccountID: accountID, ChannelType: "web_widget"} require.NoError(t, db.Create(&inbox).Error) return inbox.ID } func newCtx_Cov18(t *testing.T, method, path, body string) (*gin.Context, *httptest.ResponseRecorder) { t.Helper() w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) var req *http.Request if body != "" { req = httptest.NewRequest(method, path, strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") } else { req = httptest.NewRequest(method, path, nil) } c.Request = req return c, w } func TestConversationHandler_List_DB_Cov18(t *testing.T) { db := newTestDB_Cov18(t) accID := seedAccount_Cov18(t, db) convRepo := repository.NewConversationRepo(db) msgRepo := repository.NewMessageRepo(db) convSvc := service.NewConversationService(convRepo, msgRepo, nil, nil, nil, nil, nil) msgSvc := service.NewMessageService(msgRepo, nil, nil) h := NewConversationHandler(convSvc, msgSvc) c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/conversations", "") c.Set("account_id", accID) c.Params = gin.Params{{Key: "account_id", Value: "1"}} h.List(c) } func TestContactHandler_List_DB_Cov18(t *testing.T) { db := newTestDB_Cov18(t) accID := seedAccount_Cov18(t, db) contactRepo := repository.NewContactRepo(db) contactSvc := service.NewContactService(contactRepo, nil, nil) h := NewContactHandler(contactSvc, nil, nil, nil) c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/contacts", "") c.Set("account_id", accID) c.Params = gin.Params{{Key: "account_id", Value: "1"}} h.List(c) } func TestInboxHandler_List_DB_Cov18(t *testing.T) { db := newTestDB_Cov18(t) accID := seedAccount_Cov18(t, db) seedInbox_Cov18(t, db, accID) inboxRepo := repository.NewInboxRepo(db) inboxSvc := service.NewInboxService(inboxRepo, nil, nil, nil, nil, nil, nil) h := NewInboxHandler(inboxSvc) c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/inboxes", "") c.Set("account_id", accID) c.Params = gin.Params{{Key: "account_id", Value: "1"}} h.List(c) } func TestAccountHandler_Show_DB_Cov18(t *testing.T) { db := newTestDB_Cov18(t) accID := seedAccount_Cov18(t, db) accRepo := repository.NewAccountRepo(db) accSvc := service.NewAccountService(accRepo) h := NewAccountHandler(accSvc) c, _ := newCtx_Cov18(t, "GET", "/platform/api/v1/accounts/1", "") c.Params = gin.Params{{Key: "id", Value: "1"}} c.Set("account_id", accID) h.Get(c) } func TestBannerHandler_List_DB_Cov18(t *testing.T) { db := newTestDB_Cov18(t) accID := seedAccount_Cov18(t, db) bannerRepo := repository.NewBannerRepo(db) bannerSvc := service.NewBannerService(bannerRepo) h := NewBannerHandler(bannerSvc) c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/banners", "") c.Set("account_id", accID) c.Params = gin.Params{{Key: "account_id", Value: "1"}} h.List(c) } func TestNoteHandler_List_DB_Cov18(t *testing.T) { db := newTestDB_Cov18(t) accID := seedAccount_Cov18(t, db) noteRepo := repository.NewNoteRepo(db) noteSvc := service.NewNoteService(noteRepo) h := NewNoteHandler(noteSvc) c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/notes", "") c.Set("account_id", accID) c.Params = gin.Params{{Key: "account_id", Value: "1"}} h.List(c) } func TestAgentBotHandler_List_DB_Cov18(t *testing.T) { db := newTestDB_Cov18(t) accID := seedAccount_Cov18(t, db) botRepo := repository.NewAgentBotRepo(db) botSvc := service.NewAgentBotService(botRepo) h := NewAgentBotHandler(botSvc) c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/agent_bots", "") c.Set("account_id", accID) c.Params = gin.Params{{Key: "account_id", Value: "1"}} h.List(c) } func TestDashboardAppHandler_List_DB_Cov18(t *testing.T) { db := newTestDB_Cov18(t) accID := seedAccount_Cov18(t, db) dashRepo := repository.NewDashboardAppRepo(db) dashSvc := service.NewDashboardAppService(dashRepo) h := NewDashboardAppHandler(dashSvc) c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/dashboard_apps", "") c.Set("account_id", accID) c.Params = gin.Params{{Key: "account_id", Value: "1"}} h.List(c) } func TestFolderHandler_List_DB_Cov18(t *testing.T) { db := newTestDB_Cov18(t) accID := seedAccount_Cov18(t, db) folderRepo := repository.NewFolderRepo(db) folderSvc := service.NewFolderService(folderRepo) h := NewFolderHandler(folderSvc) c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/portals/1/folders", "") c.Set("account_id", accID) c.Params = gin.Params{{Key: "account_id", Value: "1"}, {Key: "portal_id", Value: "1"}} h.List(c) } func TestInstallationConfigHandler_List_DB_Cov18(t *testing.T) { db := newTestDB_Cov18(t) icRepo := repository.NewInstallationConfigRepo(db) icSvc := service.NewInstallationConfigService(icRepo) h := NewInstallationConfigHandler(icSvc) c, _ := newCtx_Cov18(t, "GET", "/platform/api/v1/installation_configs", "") h.List(c) }