Files
gochat/internal/repository/inbox_repo_test.go
T
2026-06-04 15:44:48 +08:00

296 lines
8.3 KiB
Go

package repository
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/model"
)
func createTestInbox(t *testing.T, db *gorm.DB, accountID uint, name string) *model.Inbox {
t.Helper()
inbox := &model.Inbox{
AccountID: accountID,
Name: name,
ChannelType: "web_widget",
ChannelID: 1,
}
require.NoError(t, db.Create(inbox).Error)
return inbox
}
func TestInboxRepo_FindByID(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxTestOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
inbox := createTestInbox(t, db, account.ID, "TestInbox")
found, err := repo.FindByID(context.Background(), inbox.ID)
assert.NoError(t, err)
assert.Equal(t, inbox.ID, found.ID)
assert.Equal(t, "TestInbox", found.Name)
}
func TestInboxRepo_FindByID_NotFound(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
found, err := repo.FindByID(context.Background(), 9999)
assert.Error(t, err)
assert.Nil(t, found)
}
func TestInboxRepo_FindByAccountAndID(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxScopedOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
inbox := createTestInbox(t, db, account.ID, "ScopedInbox")
found, err := repo.FindByAccountAndID(context.Background(), account.ID, inbox.ID)
assert.NoError(t, err)
assert.Equal(t, inbox.ID, found.ID)
}
func TestInboxRepo_FindByAccountAndID_WrongAccount(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxScopedWrongOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
inbox := createTestInbox(t, db, account.ID, "ScopedInbox")
found, err := repo.FindByAccountAndID(context.Background(), 9999, inbox.ID)
assert.Error(t, err)
assert.Nil(t, found)
}
func TestInboxRepo_FindByAccount(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxListOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
for i := 0; i < 5; i++ {
createTestInbox(t, db, account.ID, "Inbox"+string(rune('A'+i)))
}
inboxes, total, err := repo.FindByAccount(context.Background(), account.ID, 0, 10)
assert.NoError(t, err)
assert.Equal(t, int64(5), total)
assert.Len(t, inboxes, 5)
}
func TestInboxRepo_FindByAccount_Empty(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
inboxes, total, err := repo.FindByAccount(context.Background(), 1, 0, 10)
assert.NoError(t, err)
assert.Equal(t, int64(0), total)
assert.Len(t, inboxes, 0)
}
func TestInboxRepo_FindByAccount_Pagination(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxPagOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
for i := 0; i < 10; i++ {
createTestInbox(t, db, account.ID, "PagInbox")
}
inboxes, total, err := repo.FindByAccount(context.Background(), account.ID, 5, 3)
assert.NoError(t, err)
assert.Equal(t, int64(10), total)
assert.Len(t, inboxes, 3)
}
func TestInboxRepo_Create(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
inbox := &model.Inbox{
AccountID: 1,
Name: "NewInbox",
ChannelType: "web_widget",
ChannelID: 1,
}
err := repo.Create(context.Background(), inbox)
assert.NoError(t, err)
assert.NotZero(t, inbox.ID)
}
func TestInboxRepo_Update(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxUpdateOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
inbox := createTestInbox(t, db, account.ID, "OriginalName")
inbox.Name = "UpdatedName"
err := repo.Update(context.Background(), inbox)
assert.NoError(t, err)
found, err := repo.FindByID(context.Background(), inbox.ID)
assert.NoError(t, err)
assert.Equal(t, "UpdatedName", found.Name)
}
func TestInboxRepo_Delete(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxDeleteOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
inbox := createTestInbox(t, db, account.ID, "DeleteInbox")
err := repo.Delete(context.Background(), inbox.ID)
assert.NoError(t, err)
found, err := repo.FindByID(context.Background(), inbox.ID)
assert.Error(t, err)
assert.Nil(t, found)
}
func TestInboxRepo_CountByAccount(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxCountOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
for i := 0; i < 3; i++ {
createTestInbox(t, db, account.ID, "CountInbox")
}
total, err := repo.CountByAccount(context.Background(), account.ID)
assert.NoError(t, err)
assert.Equal(t, int64(3), total)
}
func TestInboxRepo_CountByAccount_Empty(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
total, err := repo.CountByAccount(context.Background(), 1)
assert.NoError(t, err)
assert.Equal(t, int64(0), total)
}
// ========================================
// UpdateFields tests (G9: partial field updates)
// ========================================
func TestInboxRepo_UpdateFields_SingleField(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxFieldsOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
inbox := createTestInbox(t, db, account.ID, "FieldsTestInbox")
err := repo.UpdateFields(context.Background(), inbox.ID, map[string]interface{}{
"webhook_url": "https://example.com/hook",
})
assert.NoError(t, err)
found, err := repo.FindByID(context.Background(), inbox.ID)
assert.NoError(t, err)
assert.Equal(t, "https://example.com/hook", found.WebhookURL)
// Other fields should remain unchanged
assert.Equal(t, "FieldsTestInbox", found.Name)
}
func TestInboxRepo_UpdateFields_MultipleFields(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxMultiFieldsOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
inbox := createTestInbox(t, db, account.ID, "MultiFieldsInbox")
err := repo.UpdateFields(context.Background(), inbox.ID, map[string]interface{}{
"webhook_url": "https://example.com/hook2",
"secret": "my-hmac-secret",
"name": "MultiFieldsUpdated",
})
assert.NoError(t, err)
found, err := repo.FindByID(context.Background(), inbox.ID)
assert.NoError(t, err)
assert.Equal(t, "https://example.com/hook2", found.WebhookURL)
assert.Equal(t, "my-hmac-secret", found.Secret)
assert.Equal(t, "MultiFieldsUpdated", found.Name)
}
func TestInboxRepo_UpdateFields_BooleanFields(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxBoolFieldsOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
inbox := createTestInbox(t, db, account.ID, "BoolFieldsInbox")
assert.False(t, inbox.GreetingEnabled)
err := repo.UpdateFields(context.Background(), inbox.ID, map[string]interface{}{
"greeting_enabled": true,
"csat_survey_enabled": true,
})
assert.NoError(t, err)
found, err := repo.FindByID(context.Background(), inbox.ID)
assert.NoError(t, err)
assert.True(t, found.GreetingEnabled)
assert.True(t, found.CsatSurveyEnabled)
}
func TestInboxRepo_UpdateFields_NonexistentInbox(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
// UpdateFields on a nonexistent ID should not error (GORM Updates with 0 rows is not an error)
err := repo.UpdateFields(context.Background(), 9999, map[string]interface{}{
"webhook_url": "https://example.com/ghost",
})
// GORM .Updates() on a record that doesn't match any rows returns nil error
assert.NoError(t, err)
}
func TestInboxRepo_UpdateFields_EmptyMap(t *testing.T) {
db := setupTestDB(t)
repo := NewInboxRepo(db)
account := &model.Account{Name: "InboxEmptyMapOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
inbox := createTestInbox(t, db, account.ID, "EmptyMapInbox")
err := repo.UpdateFields(context.Background(), inbox.ID, map[string]interface{}{})
// GORM Updates with empty map skips the update
assert.NoError(t, err)
found, err := repo.FindByID(context.Background(), inbox.ID)
assert.NoError(t, err)
assert.Equal(t, "EmptyMapInbox", found.Name)
}