Files
gochat/backend/internal/automation/automation_test_helper.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

177 lines
4.2 KiB
Go

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.Attachment{},
&model.DirectUpload{},
&model.Team{},
&model.TeamMember{},
&model.SlaPolicy{},
&model.AppliedSLA{},
&AutomationRule{},
&Macro{},
&MacroExecution{},
&AutomationExecution{},
&model.BackgroundJob{},
&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
}