Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
360 lines
11 KiB
Go
360 lines
11 KiB
Go
package automation
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/datatypes"
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestCsatSurveyService_SubmitPublicSurveyIndexesUpdatedMessage(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
db := dbProvider.DB()
|
|
accountID, _ := seedTestAccount(db, t)
|
|
conversation := seedCsatListenerConversation(t, db, accountID, true)
|
|
conversation.UUID = "public-csat-search-index"
|
|
if err := db.Save(conversation).Error; err != nil {
|
|
t.Fatalf("failed to set conversation uuid: %v", err)
|
|
}
|
|
message := &model.Message{
|
|
ConversationID: conversation.ID,
|
|
AccountID: conversation.AccountID,
|
|
InboxID: conversation.InboxID,
|
|
Content: "Rate this chat",
|
|
ContentType: "input_csat",
|
|
MessageType: "template",
|
|
Status: "sent",
|
|
ContentAttributes: datatypes.JSON(`{}`),
|
|
}
|
|
if err := db.Create(message).Error; err != nil {
|
|
t.Fatalf("failed to create CSAT message: %v", err)
|
|
}
|
|
indexer := &recordingActionSearchIndexer{}
|
|
svc := NewCsatSurveyService(dbProvider)
|
|
svc.SetSearchIndexer(indexer)
|
|
|
|
_, err := svc.SubmitPublicSurveyByConversationUUID(context.Background(), conversation.UUID, map[string]any{
|
|
"csat_survey_response": map[string]any{"rating": 4, "feedback_message": "Helpful"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("submit public survey: %v", err)
|
|
}
|
|
|
|
if len(indexer.messages) != 1 || indexer.messages[0].ID != message.ID {
|
|
t.Fatalf("expected submitted CSAT message %d to be indexed, got %#v", message.ID, indexer.messages)
|
|
}
|
|
var attrs map[string]any
|
|
if err := json.Unmarshal(indexer.messages[0].ContentAttributes, &attrs); err != nil {
|
|
t.Fatalf("decode indexed message attributes: %v", err)
|
|
}
|
|
if _, ok := attrs["submitted_values"]; !ok {
|
|
t.Fatalf("expected indexed CSAT message to include submitted_values, got %#v", attrs)
|
|
}
|
|
if len(indexer.conversations) != 1 || indexer.conversations[0].ID != conversation.ID {
|
|
t.Fatalf("expected submitted CSAT conversation %d to be indexed, got %#v", conversation.ID, indexer.conversations)
|
|
}
|
|
}
|