feat(automation): align rule trigger coverage

This commit is contained in:
2026-06-05 09:24:10 +08:00
parent 0866ea5429
commit f4c56bf109
6 changed files with 599 additions and 37 deletions
@@ -17,6 +17,7 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/automation"
"github.com/gochat/gochat/internal/channel"
linechannel "github.com/gochat/gochat/internal/channel/line"
channelprovider "github.com/gochat/gochat/internal/channel/provider"
@@ -33,6 +34,12 @@ type recordingListener struct {
events []*channel.ChannelEvent
}
type webhookAutomationDBProvider struct {
db *gorm.DB
}
func (p webhookAutomationDBProvider) DB() *gorm.DB { return p.db }
func (l *recordingListener) Name() string { return "recording-listener" }
func (l *recordingListener) OnEvent(ctx context.Context, event *channel.ChannelEvent) error {
@@ -55,6 +62,8 @@ func newWebhookLookupTestDB(t *testing.T) *gorm.DB {
&model.Conversation{},
&model.Message{},
&model.DeliveryStatus{},
&automation.AutomationRule{},
&automation.AutomationExecution{},
&channelmodel.ChannelTelegram{},
&channelmodel.ChannelLINE{},
&channelmodel.ChannelTwilioSMS{},
@@ -68,6 +77,50 @@ func newWebhookLookupTestDB(t *testing.T) *gorm.DB {
return db
}
func TestIncomingPersisterTriggersAutomationRuleFromMessageCreated(t *testing.T) {
db := newWebhookLookupTestDB(t)
inbox := seedWebhookInbox(t, db, "telegram")
dispatcher := channel.NewDispatcher()
automation.RegisterAutomationRuleListener(dispatcher, webhookAutomationDBProvider{db: db})
persister := NewIncomingPersister(db, dispatcher)
rule := &automation.AutomationRule{
AccountID: inbox.AccountID,
EventName: "message_created",
Name: "provider message created",
Conditions: automation.Conditions{},
Actions: automation.Actions{},
Active: true,
}
if err := automation.NewAutomationRuleService(webhookAutomationDBProvider{db: db}).Create(t.Context(), rule); err != nil {
t.Fatalf("create automation rule: %v", err)
}
msg := &channel.IncomingMessage{
ChannelType: channel.ChannelTelegram,
SourceID: "tg-automation-1",
SenderID: "tg-automation-user",
SenderName: "Automation User",
SenderType: channel.SenderContact,
Content: "trigger automation",
ContentType: channel.ContentText,
InboxID: inbox.ID,
AccountID: inbox.AccountID,
}
result, err := persister.PersistIncoming(t.Context(), &inbox, msg)
if err != nil {
t.Fatalf("persist incoming: %v", err)
}
var exec automation.AutomationExecution
if err := db.Where("rule_id = ? AND conversation_id = ?", rule.ID, result.Conversation.ID).First(&exec).Error; err != nil {
t.Fatalf("expected automation execution from provider message_created: %v", err)
}
if exec.Status != automation.ExecutionStatusSuccess {
t.Fatalf("expected success execution, got %s", exec.Status)
}
}
func TestIncomingPersisterUpdatesMessageStatus(t *testing.T) {
db := newWebhookLookupTestDB(t)
inbox := seedWebhookInbox(t, db, "telegram")