package automation import ( "testing" "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" "github.com/gochat/gochat/internal/model" ) // testDBProvider implements DBProvider for tests. type testDBProvider struct { db *gorm.DB } func (p *testDBProvider) DB() *gorm.DB { return p.db } // setupAutomationTestDB creates an SQLite in-memory database with all required migrations. func setupAutomationTestDB(t *testing.T) *gorm.DB { t.Helper() db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), }) if err != nil { t.Fatalf("failed to open SQLite test database: %v", err) } if err := db.AutoMigrate( &model.Account{}, &model.User{}, &model.AccountUser{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{}, &model.Conversation{}, &model.Message{}, &model.Team{}, &model.TeamMember{}, &AutomationRule{}, &Macro{}, &MacroExecution{}, &AutomationExecution{}, &CsatSurveyResponse{}, &ConversationLabel{}, &ConversationMute{}, &BotRule{}, &BotTriggerConfig{}, ); err != nil { t.Fatalf("failed to auto-migrate models: %v", err) } t.Cleanup(func() { sqlDB, _ := db.DB() sqlDB.Close() }) return db } // setupAutomationTestDBProvider creates a DBProvider-backed test database. func setupAutomationTestDBProvider(t *testing.T) DBProvider { t.Helper() db := setupAutomationTestDB(t) return &testDBProvider{db: db} } // seedTestAccount creates a minimal account + user for automation tests. func seedTestAccount(db *gorm.DB, t *testing.T) (accountID uint, userID uint) { t.Helper() account := &model.Account{Name: "Test Account"} if err := db.Create(account).Error; err != nil { t.Fatalf("failed to create test account: %v", err) } accountID = account.ID user := &model.User{ Name: "Test User", Email: "test@example.com", } if err := db.Create(user).Error; err != nil { t.Fatalf("failed to create test user: %v", err) } userID = user.ID au := &model.AccountUser{ AccountID: accountID, UserID: userID, Role: "administrator", } if err := db.Create(au).Error; err != nil { t.Fatalf("failed to create account_user: %v", err) } return accountID, userID } // seedTestConversation creates a test conversation for action execution tests. func seedTestConversation(db *gorm.DB, t *testing.T, accountID uint, inboxID uint, contactID uint) uint { t.Helper() conv := &model.Conversation{ AccountID: accountID, InboxID: inboxID, ContactID: contactID, Status: "open", ChannelType: "web", Channel: "web_widget", UUID: "test-uuid-conversation", } if err := db.Create(conv).Error; err != nil { t.Fatalf("failed to create test conversation: %v", err) } return conv.ID } // seedTestInbox creates a test inbox. func seedTestInbox(db *gorm.DB, t *testing.T, accountID uint) uint { t.Helper() inbox := &model.Inbox{ AccountID: accountID, Name: "Test Inbox", ChannelType: "web", } if err := db.Create(inbox).Error; err != nil { t.Fatalf("failed to create test inbox: %v", err) } return inbox.ID } // seedTestContact creates a test contact. func seedTestContact(db *gorm.DB, t *testing.T, accountID uint) uint { t.Helper() contact := &model.Contact{ AccountID: accountID, Name: "Test Contact", Email: "contact@example.com", } if err := db.Create(contact).Error; err != nil { t.Fatalf("failed to create test contact: %v", err) } return contact.ID } // seedTestConversationWithDetails creates a conversation with specific attributes for filter tests. func seedTestConversationWithDetails(db *gorm.DB, t *testing.T, accountID, inboxID, contactID uint, status, priority, channelType string, assigneeID uint) uint { t.Helper() conv := &model.Conversation{ AccountID: accountID, InboxID: inboxID, ContactID: contactID, Status: status, Priority: priority, ChannelType: channelType, AssigneeID: &assigneeID, Channel: channelType, UUID: "test-uuid-filter-" + status, } if err := db.Create(conv).Error; err != nil { t.Fatalf("failed to create test conversation: %v", err) } return conv.ID }