308 lines
9.3 KiB
Go
308 lines
9.3 KiB
Go
package automation
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestCsatSurveyService_Create(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
db := dbProvider.DB()
|
|
inboxID := seedTestInbox(db, t, accountID)
|
|
contactID := seedTestContact(db, t, accountID)
|
|
convID := seedTestConversation(db, t, accountID, inboxID, contactID)
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
|
|
resp := &CsatSurveyResponse{
|
|
AccountID: accountID,
|
|
ConversationID: convID,
|
|
ContactID: contactID,
|
|
AssignedAgentID: &userID,
|
|
Rating: 4,
|
|
FeedbackMessage: "Good service",
|
|
}
|
|
|
|
err := svc.Create(context.Background(), resp)
|
|
if err != nil {
|
|
t.Fatalf("expected no error creating CSAT response, got: %v", err)
|
|
}
|
|
if resp.ID == 0 {
|
|
t.Fatal("expected ID to be set after creation")
|
|
}
|
|
}
|
|
|
|
func TestCsatSurveyService_Create_InvalidRating(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, _ := seedTestAccount(dbProvider.DB(), t)
|
|
db := dbProvider.DB()
|
|
inboxID := seedTestInbox(db, t, accountID)
|
|
contactID := seedTestContact(db, t, accountID)
|
|
convID := seedTestConversation(db, t, accountID, inboxID, contactID)
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
|
|
// Rating too low
|
|
respLow := &CsatSurveyResponse{
|
|
AccountID: accountID,
|
|
ConversationID: convID,
|
|
Rating: 0,
|
|
}
|
|
err := svc.Create(context.Background(), respLow)
|
|
if err == nil {
|
|
t.Fatal("expected error for rating < 1, got nil")
|
|
}
|
|
|
|
// Rating too high
|
|
respHigh := &CsatSurveyResponse{
|
|
AccountID: accountID,
|
|
ConversationID: convID,
|
|
Rating: 6,
|
|
}
|
|
err = svc.Create(context.Background(), respHigh)
|
|
if err == nil {
|
|
t.Fatal("expected error for rating > 5, got nil")
|
|
}
|
|
}
|
|
|
|
func TestCsatSurveyService_GetByID(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
db := dbProvider.DB()
|
|
inboxID := seedTestInbox(db, t, accountID)
|
|
contactID := seedTestContact(db, t, accountID)
|
|
convID := seedTestConversation(db, t, accountID, inboxID, contactID)
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
|
|
resp := &CsatSurveyResponse{
|
|
AccountID: accountID,
|
|
ConversationID: convID,
|
|
ContactID: contactID,
|
|
AssignedAgentID: &userID,
|
|
Rating: 5,
|
|
FeedbackMessage: "Excellent!",
|
|
}
|
|
if err := svc.Create(context.Background(), resp); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
found, err := svc.GetByID(context.Background(), resp.ID)
|
|
if err != nil {
|
|
t.Fatalf("expected no error getting CSAT response, got: %v", err)
|
|
}
|
|
if found.Rating != 5 {
|
|
t.Fatalf("expected rating 5, got %d", found.Rating)
|
|
}
|
|
if found.FeedbackMessage != "Excellent!" {
|
|
t.Fatalf("expected feedback 'Excellent!', got %q", found.FeedbackMessage)
|
|
}
|
|
}
|
|
|
|
func TestCsatSurveyService_GetByID_NotFound(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
|
|
_, err := svc.GetByID(context.Background(), 9999)
|
|
if err == nil {
|
|
t.Fatal("expected error for non-existent CSAT response, got nil")
|
|
}
|
|
}
|
|
|
|
func TestCsatSurveyService_ListByAccount(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
db := dbProvider.DB()
|
|
inboxID := seedTestInbox(db, t, accountID)
|
|
contactID := seedTestContact(db, t, accountID)
|
|
convID := seedTestConversation(db, t, accountID, inboxID, contactID)
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
|
|
// Create 3 CSAT responses
|
|
for i := 1; i <= 3; i++ {
|
|
resp := &CsatSurveyResponse{
|
|
AccountID: accountID,
|
|
ConversationID: convID,
|
|
ContactID: contactID,
|
|
AssignedAgentID: &userID,
|
|
Rating: i,
|
|
}
|
|
if err := svc.Create(context.Background(), resp); err != nil {
|
|
t.Fatalf("setup: failed to create CSAT response %d: %v", i, err)
|
|
}
|
|
}
|
|
|
|
responses, total, err := svc.ListByAccount(context.Background(), accountID, CsatListFilter{Page: 1, PageSize: 25})
|
|
if err != nil {
|
|
t.Fatalf("expected no error listing CSAT responses, got: %v", err)
|
|
}
|
|
if total != 3 {
|
|
t.Fatalf("expected total 3, got %d", total)
|
|
}
|
|
if len(responses) != 3 {
|
|
t.Fatalf("expected 3 responses, got %d", len(responses))
|
|
}
|
|
}
|
|
|
|
func TestCsatSurveyService_ListByAccount_WithFilters(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
db := dbProvider.DB()
|
|
inboxID := seedTestInbox(db, t, accountID)
|
|
contactID := seedTestContact(db, t, accountID)
|
|
convID := seedTestConversation(db, t, accountID, inboxID, contactID)
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
|
|
resp := &CsatSurveyResponse{
|
|
AccountID: accountID,
|
|
ConversationID: convID,
|
|
ContactID: contactID,
|
|
AssignedAgentID: &userID,
|
|
Rating: 3,
|
|
}
|
|
if err := svc.Create(context.Background(), resp); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
// Filter by agent ID
|
|
responses, total, err := svc.ListByAccount(context.Background(), accountID, CsatListFilter{
|
|
AgentID: &userID,
|
|
Page: 1,
|
|
PageSize: 25,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
if total != 1 {
|
|
t.Fatalf("expected 1 response with agent filter, got %d", total)
|
|
}
|
|
if len(responses) != 1 {
|
|
t.Fatalf("expected 1 response, got %d", len(responses))
|
|
}
|
|
}
|
|
|
|
func TestCsatSurveyService_UpdateResponse(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
db := dbProvider.DB()
|
|
inboxID := seedTestInbox(db, t, accountID)
|
|
contactID := seedTestContact(db, t, accountID)
|
|
convID := seedTestConversation(db, t, accountID, inboxID, contactID)
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
|
|
resp := &CsatSurveyResponse{
|
|
AccountID: accountID,
|
|
ConversationID: convID,
|
|
ContactID: contactID,
|
|
AssignedAgentID: &userID,
|
|
Rating: 3,
|
|
FeedbackMessage: "Okay",
|
|
}
|
|
if err := svc.Create(context.Background(), resp); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
err := svc.UpdateResponse(context.Background(), resp.ID, 5, "Actually great!")
|
|
if err != nil {
|
|
t.Fatalf("expected no error updating response, got: %v", err)
|
|
}
|
|
|
|
found, _ := svc.GetByID(context.Background(), resp.ID)
|
|
if found.Rating != 5 {
|
|
t.Fatalf("expected updated rating 5, got %d", found.Rating)
|
|
}
|
|
if found.FeedbackMessage != "Actually great!" {
|
|
t.Fatalf("expected updated feedback, got %q", found.FeedbackMessage)
|
|
}
|
|
}
|
|
|
|
func TestCsatSurveyService_UpdateResponse_InvalidRating(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, _ := seedTestAccount(dbProvider.DB(), t)
|
|
db := dbProvider.DB()
|
|
inboxID := seedTestInbox(db, t, accountID)
|
|
contactID := seedTestContact(db, t, accountID)
|
|
convID := seedTestConversation(db, t, accountID, inboxID, contactID)
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
|
|
resp := &CsatSurveyResponse{
|
|
AccountID: accountID,
|
|
ConversationID: convID,
|
|
Rating: 3,
|
|
}
|
|
if err := svc.Create(context.Background(), resp); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
err := svc.UpdateResponse(context.Background(), resp.ID, 0, "bad rating")
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid rating, got nil")
|
|
}
|
|
}
|
|
|
|
func TestCsatSurveyService_UpdateReviewNotes(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
db := dbProvider.DB()
|
|
inboxID := seedTestInbox(db, t, accountID)
|
|
contactID := seedTestContact(db, t, accountID)
|
|
convID := seedTestConversation(db, t, accountID, inboxID, contactID)
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
|
|
resp := &CsatSurveyResponse{
|
|
AccountID: accountID,
|
|
ConversationID: convID,
|
|
ContactID: contactID,
|
|
AssignedAgentID: &userID,
|
|
Rating: 4,
|
|
}
|
|
if err := svc.Create(context.Background(), resp); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
err := svc.UpdateReviewNotes(context.Background(), resp.ID, "Agent review notes", userID)
|
|
if err != nil {
|
|
t.Fatalf("expected no error updating review notes, got: %v", err)
|
|
}
|
|
|
|
found, _ := svc.GetByID(context.Background(), resp.ID)
|
|
if found.CsatReviewNotes != "Agent review notes" {
|
|
t.Fatalf("expected review notes, got %q", found.CsatReviewNotes)
|
|
}
|
|
}
|
|
|
|
func TestCsatSurveyService_Metrics(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
db := dbProvider.DB()
|
|
inboxID := seedTestInbox(db, t, accountID)
|
|
contactID := seedTestContact(db, t, accountID)
|
|
convID := seedTestConversation(db, t, accountID, inboxID, contactID)
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
|
|
// Create responses with ratings 1,3,5
|
|
for _, rating := range []int{1, 3, 5} {
|
|
resp := &CsatSurveyResponse{
|
|
AccountID: accountID,
|
|
ConversationID: convID,
|
|
AssignedAgentID: &userID,
|
|
Rating: rating,
|
|
}
|
|
if err := svc.Create(context.Background(), resp); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
}
|
|
|
|
metrics, err := svc.Metrics(context.Background(), accountID, CsatListFilter{})
|
|
if err != nil {
|
|
t.Fatalf("expected no error computing metrics, got: %v", err)
|
|
}
|
|
if metrics.TotalCount != 3 {
|
|
t.Fatalf("expected 3 total responses, got %d", metrics.TotalCount)
|
|
}
|
|
if metrics.TotalSentMessagesCount != 0 {
|
|
t.Fatalf("expected 0 sent CSAT messages, got %d", metrics.TotalSentMessagesCount)
|
|
}
|
|
if metrics.RatingsCount[1] != 1 || metrics.RatingsCount[3] != 1 || metrics.RatingsCount[5] != 1 {
|
|
t.Fatalf("expected distribution {1:1, 3:1, 5:1}, got %v", metrics.RatingsCount)
|
|
}
|
|
}
|