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

282 lines
8.5 KiB
Go

package repository
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/model"
)
// Helper: create prerequisite Account, AgentBot, and Inbox for AgentBotInbox tests.
func createAgentBotInboxPrereqs(t *testing.T, db *gorm.DB) (*model.Account, *model.AgentBot, *model.Inbox) {
t.Helper()
account := &model.Account{Name: "ABITestOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
bot := &model.AgentBot{
AccountID: &account.ID,
Name: "TestBot",
BotType: "default",
Secret: "secret-abitest",
AccessToken: "token-abitest",
}
require.NoError(t, db.Create(bot).Error)
inbox := &model.Inbox{
AccountID: account.ID,
Name: "TestInbox",
ChannelType: "web_widget",
ChannelID: 1,
}
require.NoError(t, db.Create(inbox).Error)
return account, bot, inbox
}
// Helper: create a second bot with unique secret/token for SQLite UNIQUE constraint.
func createSecondBot(t *testing.T, db *gorm.DB, accountID uint, suffix string) *model.AgentBot {
t.Helper()
bot := &model.AgentBot{
AccountID: &accountID,
Name: "Bot-" + suffix,
BotType: "default",
Secret: fmt.Sprintf("secret-%s", suffix),
AccessToken: fmt.Sprintf("token-%s", suffix),
}
require.NoError(t, db.Create(bot).Error)
return bot
}
// Helper: create a second inbox.
func createSecondInbox(t *testing.T, db *gorm.DB, accountID uint, channelID uint) *model.Inbox {
t.Helper()
inbox := &model.Inbox{
AccountID: accountID,
Name: fmt.Sprintf("Inbox-%d", channelID),
ChannelType: "web_widget",
ChannelID: channelID,
}
require.NoError(t, db.Create(inbox).Error)
return inbox
}
// Helper: create a test AgentBotInbox binding.
func createTestAgentBotInbox(t *testing.T, db *gorm.DB, botID, inboxID uint, accountID *uint, status model.AgentBotInboxStatus) *model.AgentBotInbox {
t.Helper()
abi := &model.AgentBotInbox{
AgentBotID: botID,
InboxID: inboxID,
AccountID: accountID,
Status: status,
}
require.NoError(t, db.Create(abi).Error)
return abi
}
// ========== 1. FindByID ==========
func TestAgentBotInboxRepo_FindByID(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
_, bot, inbox := createAgentBotInboxPrereqs(t, db)
abi := createTestAgentBotInbox(t, db, bot.ID, inbox.ID, &inbox.AccountID, model.AgentBotInboxActive)
found, err := repo.FindByID(context.Background(), abi.ID)
assert.NoError(t, err)
assert.Equal(t, abi.ID, found.ID)
assert.Equal(t, bot.ID, found.AgentBotID)
assert.Equal(t, inbox.ID, found.InboxID)
assert.Equal(t, model.AgentBotInboxActive, found.Status)
}
func TestAgentBotInboxRepo_FindByID_NotFound(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
found, err := repo.FindByID(context.Background(), 9999)
assert.Error(t, err)
assert.Nil(t, found)
}
// ========== 2. FindByAgentBotID ==========
func TestAgentBotInboxRepo_FindByAgentBotID(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
_, bot, inbox := createAgentBotInboxPrereqs(t, db)
createTestAgentBotInbox(t, db, bot.ID, inbox.ID, &inbox.AccountID, model.AgentBotInboxActive)
// Create a second inbox for the same bot
inbox2 := createSecondInbox(t, db, inbox.AccountID, 2)
createTestAgentBotInbox(t, db, bot.ID, inbox2.ID, &inbox2.AccountID, model.AgentBotInboxInactive)
abis, err := repo.FindByAgentBotID(context.Background(), bot.ID)
assert.NoError(t, err)
assert.Len(t, abis, 2)
}
func TestAgentBotInboxRepo_FindByAgentBotID_Empty(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
_, bot, _ := createAgentBotInboxPrereqs(t, db)
abis, err := repo.FindByAgentBotID(context.Background(), bot.ID)
assert.NoError(t, err)
assert.Len(t, abis, 0)
}
// ========== 3. FindByInboxID ==========
func TestAgentBotInboxRepo_FindByInboxID(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
_, bot, inbox := createAgentBotInboxPrereqs(t, db)
createTestAgentBotInbox(t, db, bot.ID, inbox.ID, &inbox.AccountID, model.AgentBotInboxActive)
// Create a second bot for the same inbox
bot2 := createSecondBot(t, db, inbox.AccountID, "byinbox")
createTestAgentBotInbox(t, db, bot2.ID, inbox.ID, &inbox.AccountID, model.AgentBotInboxInactive)
abis, err := repo.FindByInboxID(context.Background(), inbox.ID)
assert.NoError(t, err)
assert.Len(t, abis, 2)
}
// ========== 4. FindActiveByInboxID ==========
func TestAgentBotInboxRepo_FindActiveByInboxID(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
_, bot, inbox := createAgentBotInboxPrereqs(t, db)
createTestAgentBotInbox(t, db, bot.ID, inbox.ID, &inbox.AccountID, model.AgentBotInboxActive)
// Create a second bot binding that is inactive
bot2 := createSecondBot(t, db, inbox.AccountID, "activeinbox")
createTestAgentBotInbox(t, db, bot2.ID, inbox.ID, &inbox.AccountID, model.AgentBotInboxInactive)
abis, err := repo.FindActiveByInboxID(context.Background(), inbox.ID)
assert.NoError(t, err)
assert.Len(t, abis, 1)
assert.Equal(t, model.AgentBotInboxActive, abis[0].Status)
}
func TestAgentBotInboxRepo_FindActiveByInboxID_AllInactive(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
_, bot, inbox := createAgentBotInboxPrereqs(t, db)
createTestAgentBotInbox(t, db, bot.ID, inbox.ID, &inbox.AccountID, model.AgentBotInboxInactive)
abis, err := repo.FindActiveByInboxID(context.Background(), inbox.ID)
assert.NoError(t, err)
assert.Len(t, abis, 0)
}
// ========== 5. FindByAgentBotAndInbox ==========
func TestAgentBotInboxRepo_FindByAgentBotAndInbox(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
_, bot, inbox := createAgentBotInboxPrereqs(t, db)
abi := createTestAgentBotInbox(t, db, bot.ID, inbox.ID, &inbox.AccountID, model.AgentBotInboxActive)
found, err := repo.FindByAgentBotAndInbox(context.Background(), bot.ID, inbox.ID)
assert.NoError(t, err)
assert.Equal(t, abi.ID, found.ID)
assert.Equal(t, bot.ID, found.AgentBotID)
assert.Equal(t, inbox.ID, found.InboxID)
}
func TestAgentBotInboxRepo_FindByAgentBotAndInbox_NotFound(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
found, err := repo.FindByAgentBotAndInbox(context.Background(), 9999, 9999)
assert.Error(t, err)
assert.Nil(t, found)
}
// ========== 6. Create ==========
func TestAgentBotInboxRepo_Create(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
_, bot, inbox := createAgentBotInboxPrereqs(t, db)
abi := &model.AgentBotInbox{
AgentBotID: bot.ID,
InboxID: inbox.ID,
AccountID: &inbox.AccountID,
Status: model.AgentBotInboxActive,
}
err := repo.Create(context.Background(), abi)
assert.NoError(t, err)
assert.NotZero(t, abi.ID)
// Verify persisted
found, err := repo.FindByID(context.Background(), abi.ID)
assert.NoError(t, err)
assert.Equal(t, bot.ID, found.AgentBotID)
}
// ========== 7. Update ==========
func TestAgentBotInboxRepo_Update(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
_, bot, inbox := createAgentBotInboxPrereqs(t, db)
abi := createTestAgentBotInbox(t, db, bot.ID, inbox.ID, &inbox.AccountID, model.AgentBotInboxActive)
// Toggle status to inactive
abi.Status = model.AgentBotInboxInactive
err := repo.Update(context.Background(), abi)
assert.NoError(t, err)
// Verify persisted
found, err := repo.FindByID(context.Background(), abi.ID)
assert.NoError(t, err)
assert.Equal(t, model.AgentBotInboxInactive, found.Status)
}
// ========== 8. Delete ==========
func TestAgentBotInboxRepo_Delete(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
_, bot, inbox := createAgentBotInboxPrereqs(t, db)
abi := createTestAgentBotInbox(t, db, bot.ID, inbox.ID, &inbox.AccountID, model.AgentBotInboxActive)
err := repo.Delete(context.Background(), abi.ID)
assert.NoError(t, err)
// Verify deleted
found, err := repo.FindByID(context.Background(), abi.ID)
assert.Error(t, err)
assert.Nil(t, found)
}
func TestAgentBotInboxRepo_Delete_NotFound(t *testing.T) {
db := setupTestDB(t, &model.AgentBotInbox{})
repo := NewAgentBotInboxRepo(db)
err := repo.Delete(context.Background(), 9999)
assert.NoError(t, err) // GORM delete on non-existent row returns no error
}