feat(automation): align attachment uploads

This commit is contained in:
2026-06-07 10:44:45 +08:00
parent f46bce7528
commit f7515b111d
6 changed files with 319 additions and 30 deletions
+68
View File
@@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"testing"
"github.com/gochat/gochat/internal/model"
)
func TestAutomationRuleService_Create(t *testing.T) {
@@ -34,6 +36,72 @@ func TestAutomationRuleService_Create(t *testing.T) {
}
}
func TestAutomationRuleService_CreateNormalizesAttachmentActionsAndHydratesFiles(t *testing.T) {
dbProvider := setupAutomationTestDBProvider(t)
db := dbProvider.DB()
accountID, _ := seedTestAccount(db, t)
upload := &model.DirectUpload{UploadUUID: "automation-service-upload", AccountID: accountID, Status: model.DirectUploadStatusPending, Source: model.DirectUploadSourceAccount, OriginalName: "rule.pdf", FileType: "file", MimeType: "application/pdf", FileSize: 789, FileURL: "/uploads/account/1/rule.pdf"}
if err := db.Create(upload).Error; err != nil {
t.Fatalf("create upload: %v", err)
}
svc := NewAutomationRuleService(dbProvider)
rule := &AutomationRule{
AccountID: accountID,
EventName: "conversation_created",
Name: "Send attachment",
Conditions: Conditions{},
Actions: Actions{{ActionName: "send_attachment", ActionParams: map[string]interface{}{"blob_id": "automation-service-upload", "attachment_url": "stale"}}},
Active: true,
}
if err := svc.Create(context.Background(), rule); err != nil {
t.Fatalf("create rule: %v", err)
}
if got := macroBlobIDAsUint(rule.Actions[0].ActionParams["blob_id"]); got != upload.ID {
t.Fatalf("expected blob id %d, got %d", upload.ID, got)
}
if _, ok := rule.Actions[0].ActionParams["attachment_url"]; ok {
t.Fatal("expected attachment_url to be removed")
}
if len(rule.Files) != 1 || rule.Files[0].BlobID != upload.ID || rule.Files[0].AutomationRuleID != rule.ID {
t.Fatalf("unexpected files payload: %#v", rule.Files)
}
}
func TestAutomationRuleService_MatchAndExecuteCreatesAttachmentMessages(t *testing.T) {
dbProvider := setupAutomationTestDBProvider(t)
db := dbProvider.DB()
accountID, _ := seedTestAccount(db, t)
inboxID := seedTestInbox(db, t, accountID)
contactID := seedTestContact(db, t, accountID)
conversationID := seedTestConversationWithDetails(db, t, accountID, inboxID, contactID, "open", "low", "web", 0)
upload := &model.DirectUpload{UploadUUID: "automation-exec-upload", AccountID: accountID, Status: model.DirectUploadStatusPending, Source: model.DirectUploadSourceAccount, OriginalName: "automation.pdf", FileType: "file", MimeType: "application/pdf", FileSize: 456, FileURL: "/uploads/account/1/automation.pdf"}
if err := db.Create(upload).Error; err != nil {
t.Fatalf("create upload: %v", err)
}
svc := NewAutomationRuleService(dbProvider)
rule := &AutomationRule{AccountID: accountID, EventName: "conversation_created", Name: "file rule", Conditions: Conditions{}, Actions: Actions{{ActionName: "send_attachment", ActionParams: map[string]interface{}{"blob_id": "automation-exec-upload"}}}, Active: true}
if err := svc.Create(context.Background(), rule); err != nil {
t.Fatalf("create rule: %v", err)
}
if err := svc.MatchAndExecute(context.Background(), accountID, "conversation_created", conversationID, map[string]interface{}{}); err != nil {
t.Fatalf("execute automation: %v", err)
}
var message model.Message
if err := db.Where("conversation_id = ? AND content_type = ?", conversationID, "file").First(&message).Error; err != nil {
t.Fatalf("expected file message: %v", err)
}
var attachment model.Attachment
if err := db.Where("message_id = ?", message.ID).First(&attachment).Error; err != nil {
t.Fatalf("expected attachment: %v", err)
}
if attachment.FileURL != upload.FileURL || attachment.FileName != upload.OriginalName {
t.Fatalf("unexpected attachment: %#v", attachment)
}
}
func TestAutomationRuleService_GetByID(t *testing.T) {
dbProvider := setupAutomationTestDBProvider(t)
accountID, _ := seedTestAccount(dbProvider.DB(), t)