fix: prevent repeated off-hours handoff notices

This commit is contained in:
mlogclub
2026-05-09 23:46:56 +08:00
parent af8ec71bb8
commit 59b3509ca9
4 changed files with 42 additions and 2 deletions
@@ -47,6 +47,32 @@ func TestConversationHumanDispatchAIHandoffOffHoursKeepsAIServingAndSendsNotice(
}
}
func TestConversationHumanDispatchAIHandoffOffHoursDoesNotDuplicateNotice(t *testing.T) {
db := setupConversationHumanDispatchTestDB(t)
aiAgent := createHumanDispatchAIAgent(t, db, enums.IMConversationServiceModeAIFirst, "1")
conversation := createHumanDispatchConversation(t, db, aiAgent.ID, enums.IMConversationStatusAIServing)
for i := 0; i < 3; i++ {
result, err := services.ConversationHumanDispatchService.HandoffByAI(conversation.ID, aiAgent, "用户要求转人工")
if err != nil {
t.Fatalf("HandoffByAI() round %d error = %v", i+1, err)
}
if result == nil || result.Decision != services.HandoffDecisionOffHours {
t.Fatalf("expected off_hours decision on round %d, got %+v", i+1, result)
}
}
var count int64
if err := db.Model(&models.Message{}).
Where("conversation_id = ? AND sender_type = ? AND content = ?", conversation.ID, enums.IMSenderTypeAI, services.HandoffOffHoursMessage).
Count(&count).Error; err != nil {
t.Fatalf("count off-hours messages error = %v", err)
}
if count != 1 {
t.Fatalf("expected one off-hours notice, got %d", count)
}
}
func TestConversationHumanDispatchAIHandoffAssignsAvailableAgent(t *testing.T) {
db := setupConversationHumanDispatchTestDB(t)
aiAgent := createHumanDispatchAIAgent(t, db, enums.IMConversationServiceModeAIFirst, "1")