Files
libredesk/internal/conversation/transcript_test.go
Abhinav Raut 170fcd026d add help article citations to AI replies
Link cited public help articles in chat replies and emails. Batch reference lookups and keep source metadata out of customer replies.
2026-10-10 20:57:12 +05:30

101 lines
3.3 KiB
Go

package conversation
import (
"encoding/json"
"strings"
"testing"
"time"
"github.com/abhinavxd/libredesk/internal/attachment"
"github.com/abhinavxd/libredesk/internal/conversation/models"
hcmodels "github.com/abhinavxd/libredesk/internal/helpcenter/models"
"github.com/abhinavxd/libredesk/internal/testutil"
"github.com/volatiletech/null/v9"
)
func TestBuildTranscript(t *testing.T) {
m := &Manager{i18n: testutil.NewI18n(t)}
created := time.Date(2026, time.May, 11, 10, 0, 0, 0, time.UTC)
downloaded := time.Date(2026, time.June, 2, 16, 10, 0, 0, time.UTC)
conversation := models.Conversation{
ReferenceNumber: "1234",
Subject: null.StringFrom("Refund request"),
CreatedAt: created,
Contact: models.ConversationContact{FirstName: "John", LastName: "Doe"},
}
messages := []models.Message{
{
Type: models.MessageIncoming,
SenderType: models.SenderTypeContact,
CreatedAt: created.Add(time.Minute),
TextContent: "Hi, I need a refund for order #555",
Author: models.MessageAuthor{FirstName: "John", LastName: "Doe"},
},
{
Type: models.MessageOutgoing,
SenderType: models.SenderTypeAgent,
CreatedAt: created.Add(5 * time.Minute),
TextContent: "Sure, processing it now.",
Author: models.MessageAuthor{FirstName: "Priya"},
Attachments: attachment.Attachments{{Name: "invoice.pdf"}},
},
{
Type: models.MessageOutgoing,
SenderType: models.SenderTypeAgent,
CreatedAt: created.Add(6 * time.Minute),
Content: "<p>Falls back to <b>HTML</b></p>",
ContentType: models.ContentTypeHTML,
Author: models.MessageAuthor{FirstName: "Priya"},
},
}
out := string(m.BuildTranscript(conversation, messages, downloaded))
wantContains := []string{
"Conversation #1234",
"Subject: Refund request",
"Contact: John Doe",
"Created at: May 11, 2026 10:00 AM UTC",
"Downloaded at: Jun 02, 2026 04:10 PM UTC",
"[May 11, 2026 10:01 AM UTC] John Doe (Contact):",
"Hi, I need a refund for order #555",
"[May 11, 2026 10:05 AM UTC] Priya (Agent):",
"Attachment: invoice.pdf",
"Falls back to HTML",
}
for _, want := range wantContains {
if !strings.Contains(out, want) {
t.Errorf("transcript missing %q\n---\n%s", want, out)
}
}
}
func TestBuildTranscriptArticleReferences(t *testing.T) {
m := Manager{
articleReferenceStore: citationArticleStore{references: []hcmodels.ArticleReference{
{ID: 12, Title: "Refund policy", HelpCenterSlug: "support", Locale: "en", Slug: "refunds"},
}},
settingsStore: citationSettingsStore{rootURL: "https://desk.example.com"},
i18n: testutil.NewI18n(t),
}
for _, text := range []string{"Answer.", ""} {
messages := []models.Message{{
Content: "<p>Answer.</p>",
TextContent: text,
Meta: json.RawMessage(`{"ai_assistant_id":1,"ai_article_ids":[12]}`),
}}
out := string(m.BuildTranscript(models.Conversation{}, messages, time.Now() /* downloadedAt */))
for _, want := range []string{"Answer.", "Article references", "Refund policy", "https://desk.example.com/hc/support/en/articles/refunds"} {
if !strings.Contains(out, want) {
t.Errorf("transcript missing %q: %s", want, out)
}
}
if strings.Contains(messages[0].Content, "https://") {
t.Fatal("transcript rendering changed the saved reply")
}
}
}