216 lines
7.6 KiB
Go
216 lines
7.6 KiB
Go
package automation
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestBotTriggerConfigService_Create(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, _ := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewBotTriggerConfigService(dbProvider)
|
|
|
|
config := &BotTriggerConfig{
|
|
AccountID: accountID,
|
|
AgentBotID: 1,
|
|
Name: "Trigger on new conversations",
|
|
EventName: BotRuleEventConversationCreated,
|
|
Conditions: TriggerConditions{
|
|
{Attribute: "status", FilterOperator: "equal", Values: []string{"open"}, QueryOperator: "and"},
|
|
},
|
|
QueryOperator: "and",
|
|
Active: true,
|
|
}
|
|
|
|
err := svc.Create(context.Background(), config)
|
|
if err != nil {
|
|
t.Fatalf("expected no error creating trigger config, got: %v", err)
|
|
}
|
|
if config.ID == 0 {
|
|
t.Fatal("expected trigger config ID to be set after creation")
|
|
}
|
|
}
|
|
|
|
func TestBotTriggerConfigService_GetByID(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, _ := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewBotTriggerConfigService(dbProvider)
|
|
|
|
config := &BotTriggerConfig{
|
|
AccountID: accountID,
|
|
AgentBotID: 1,
|
|
Name: "Trigger on messages",
|
|
EventName: BotRuleEventMessageCreated,
|
|
Conditions: TriggerConditions{
|
|
{Attribute: "content", FilterOperator: "contains", Values: []string{"help"}, QueryOperator: "and"},
|
|
},
|
|
Active: true,
|
|
}
|
|
|
|
svc.Create(context.Background(), config)
|
|
|
|
found, err := svc.GetByID(context.Background(), config.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetByID failed: %v", err)
|
|
}
|
|
if found.EventName != BotRuleEventMessageCreated {
|
|
t.Fatalf("expected event_name message_created, got %s", found.EventName)
|
|
}
|
|
}
|
|
|
|
func TestBotTriggerConfigService_GetByID_NotFound(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
svc := NewBotTriggerConfigService(dbProvider)
|
|
|
|
_, err := svc.GetByID(context.Background(), 9999)
|
|
if err == nil {
|
|
t.Fatal("expected error for non-existent trigger config, got nil")
|
|
}
|
|
}
|
|
|
|
func TestBotTriggerConfigService_ListByAccount(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, _ := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewBotTriggerConfigService(dbProvider)
|
|
|
|
c1 := &BotTriggerConfig{AccountID: accountID, AgentBotID: 1, Name: "tc1", EventName: BotRuleEventConversationCreated, Conditions: TriggerConditions{}, Active: true}
|
|
c2 := &BotTriggerConfig{AccountID: accountID, AgentBotID: 2, Name: "tc2", EventName: BotRuleEventMessageCreated, Conditions: TriggerConditions{}, Active: true}
|
|
svc.Create(context.Background(), c1)
|
|
svc.Create(context.Background(), c2)
|
|
|
|
configs, err := svc.ListByAccount(context.Background(), accountID)
|
|
if err != nil {
|
|
t.Fatalf("ListByAccount failed: %v", err)
|
|
}
|
|
if len(configs) != 2 {
|
|
t.Fatalf("expected 2 configs, got %d", len(configs))
|
|
}
|
|
}
|
|
|
|
func TestBotTriggerConfigService_ListByAgentBot(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, _ := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewBotTriggerConfigService(dbProvider)
|
|
|
|
c1 := &BotTriggerConfig{AccountID: accountID, AgentBotID: 1, Name: "tc1", EventName: BotRuleEventConversationCreated, Conditions: TriggerConditions{}, Active: true}
|
|
c2 := &BotTriggerConfig{AccountID: accountID, AgentBotID: 1, Name: "tc2", EventName: BotRuleEventMessageCreated, Conditions: TriggerConditions{}, Active: true}
|
|
c3 := &BotTriggerConfig{AccountID: accountID, AgentBotID: 2, Name: "tc3", EventName: BotRuleEventConversationUpdated, Conditions: TriggerConditions{}, Active: true}
|
|
svc.Create(context.Background(), c1)
|
|
svc.Create(context.Background(), c2)
|
|
svc.Create(context.Background(), c3)
|
|
|
|
configs, err := svc.ListByAgentBot(context.Background(), accountID, 1)
|
|
if err != nil {
|
|
t.Fatalf("ListByAgentBot failed: %v", err)
|
|
}
|
|
if len(configs) != 2 {
|
|
t.Fatalf("expected 2 configs for agent_bot_id=1, got %d", len(configs))
|
|
}
|
|
}
|
|
|
|
func TestBotTriggerConfigService_ListActiveByAccountAndEvent(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, _ := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewBotTriggerConfigService(dbProvider)
|
|
|
|
c1 := &BotTriggerConfig{AccountID: accountID, AgentBotID: 1, Name: "tc1", EventName: BotRuleEventConversationCreated, Conditions: TriggerConditions{}, Active: true}
|
|
c2 := &BotTriggerConfig{AccountID: accountID, AgentBotID: 2, Name: "tc2", EventName: BotRuleEventConversationCreated, Conditions: TriggerConditions{}, Active: true}
|
|
c3 := &BotTriggerConfig{AccountID: accountID, AgentBotID: 1, Name: "tc3", EventName: BotRuleEventMessageCreated, Conditions: TriggerConditions{}, Active: false}
|
|
svc.Create(context.Background(), c1)
|
|
svc.Create(context.Background(), c2)
|
|
svc.Create(context.Background(), c3)
|
|
|
|
configs, err := svc.ListActiveByAccountAndEvent(context.Background(), accountID, BotRuleEventConversationCreated)
|
|
if err != nil {
|
|
t.Fatalf("ListActiveByAccountAndEvent failed: %v", err)
|
|
}
|
|
if len(configs) != 2 {
|
|
t.Fatalf("expected 2 active configs for conversation_created, got %d", len(configs))
|
|
}
|
|
}
|
|
|
|
func TestBotTriggerConfigService_Update(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, _ := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewBotTriggerConfigService(dbProvider)
|
|
|
|
config := &BotTriggerConfig{
|
|
AccountID: accountID,
|
|
AgentBotID: 1,
|
|
Name: "tc1",
|
|
EventName: BotRuleEventConversationCreated,
|
|
Conditions: TriggerConditions{{Attribute: "status", FilterOperator: "equal", Values: []string{"open"}, QueryOperator: "and"}},
|
|
Active: true,
|
|
}
|
|
svc.Create(context.Background(), config)
|
|
|
|
// Update
|
|
config.EventName = BotRuleEventMessageCreated
|
|
config.Active = false
|
|
err := svc.Update(context.Background(), config)
|
|
if err != nil {
|
|
t.Fatalf("Update failed: %v", err)
|
|
}
|
|
|
|
found, err := svc.GetByID(context.Background(), config.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetByID after update failed: %v", err)
|
|
}
|
|
if found.EventName != BotRuleEventMessageCreated {
|
|
t.Fatalf("expected event_name message_created, got %s", found.EventName)
|
|
}
|
|
if found.Active != false {
|
|
t.Fatal("expected active=false after update")
|
|
}
|
|
}
|
|
|
|
func TestBotTriggerConfigService_Delete(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, _ := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewBotTriggerConfigService(dbProvider)
|
|
|
|
config := &BotTriggerConfig{AccountID: accountID, AgentBotID: 1, Name: "tc1", EventName: BotRuleEventConversationCreated, Conditions: TriggerConditions{}, Active: true}
|
|
svc.Create(context.Background(), config)
|
|
|
|
err := svc.Delete(context.Background(), config.ID)
|
|
if err != nil {
|
|
t.Fatalf("Delete failed: %v", err)
|
|
}
|
|
|
|
_, err = svc.GetByID(context.Background(), config.ID)
|
|
if err == nil {
|
|
t.Fatal("expected error after deletion, got nil")
|
|
}
|
|
}
|
|
|
|
func TestBotTriggerConfigService_ToggleActive(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, _ := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewBotTriggerConfigService(dbProvider)
|
|
|
|
config := &BotTriggerConfig{AccountID: accountID, AgentBotID: 1, Name: "tc1", EventName: BotRuleEventConversationCreated, Conditions: TriggerConditions{}, Active: true}
|
|
svc.Create(context.Background(), config)
|
|
|
|
err := svc.ToggleActive(context.Background(), config.ID, false)
|
|
if err != nil {
|
|
t.Fatalf("ToggleActive failed: %v", err)
|
|
}
|
|
|
|
found, err := svc.GetByID(context.Background(), config.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetByID after toggle failed: %v", err)
|
|
}
|
|
if found.Active != false {
|
|
t.Fatal("expected active=false after toggle")
|
|
}
|
|
|
|
// Toggle back to active
|
|
svc.ToggleActive(context.Background(), config.ID, true)
|
|
found2, err := svc.GetByID(context.Background(), config.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetByID after second toggle failed: %v", err)
|
|
}
|
|
if found2.Active != true {
|
|
t.Fatal("expected active=true after second toggle")
|
|
}
|
|
} |