Files
agent-desk-ng/internal/ai/rag/index_retrieve_helpers_test.go
mono 2f7a491666 feat: harden support, localize knowledge and add operator availability [skip ci]
Source-only publication of reviewed commit 64e96a7f423673dc29ad56485be9fc79bce8af1c.

Passed in isolation: focused frontend/backend tests, race checks, TypeScript typecheck, lint, Go compilation, Flowgram build, and deterministic SDK build/tests.

Not accepted as a production release: Next.js export/native assembly remains blocked by environment memory limits; browser interaction and MySQL staging acceptance remain unverified. No production server, database/schema, runtime configuration, deployment, release tag, or workflow execution was performed.

Expected source tree: 0a4a7528e9.
2026-10-16 15:21:42 +00:00

96 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Modified in the independent AgentDesk fork: localized knowledge workflows and safe display errors.
package rag
import (
"context"
"strings"
"testing"
"agent-desk/internal/pkg/i18nx"
"agent-desk/internal/models"
)
func TestBuildFAQChunkContent(t *testing.T) {
faq := models.KnowledgeFAQ{
Question: "如何退款",
SimilarQuestions: `["退款怎么申请","申请售后"]`,
Answer: "在订单页发起退款。",
}
content := sourceFAQContent(faq).render(i18nx.LocaleZhCN)
if !strings.Contains(content, "问题:如何退款") {
t.Fatalf("expected question in content, got %q", content)
}
if !strings.Contains(content, "相似问:退款怎么申请;申请售后") {
t.Fatalf("expected similar questions in content, got %q", content)
}
if !strings.Contains(content, "回答:在订单页发起退款。") {
t.Fatalf("expected answer in content, got %q", content)
}
}
func TestBuildFAQChunkModel(t *testing.T) {
knowledgeBase := models.KnowledgeBase{ID: 11}
faq := models.KnowledgeFAQ{ID: 22, Question: "如何退款"}
chunk, chunkID := buildFAQChunkModel(knowledgeBase, faq, "问题:如何退款\n回答:在订单页发起退款。")
if chunkID == "" {
t.Fatalf("expected chunk id")
}
if chunk.KnowledgeBaseID != 11 || chunk.FaqID != 22 {
t.Fatalf("unexpected chunk identity: %#v", chunk)
}
if chunk.VectorID != chunkID {
t.Fatalf("expected vector id to match chunk id")
}
if chunk.Title != "如何退款" {
t.Fatalf("unexpected title: %q", chunk.Title)
}
}
func TestNormalizeContextResultsMergesAndDedupes(t *testing.T) {
results := normalizeContextResults([]RetrieveResult{
{DocumentID: 1, ChunkNo: 1, SectionPath: "A", Content: "第一段", Score: 0.7},
{DocumentID: 1, ChunkNo: 2, SectionPath: "A", Content: "第二段", Score: 0.9},
{DocumentID: 1, ChunkNo: 3, SectionPath: "A", Content: "第三段", Score: 0.6},
{DocumentID: 2, ChunkNo: 1, Title: "标题", Content: "独立段", Score: 0.5},
{FaqID: 9, FaqQuestion: "FAQ", Content: "FAQ内容", Score: 0.8},
{FaqID: 9, FaqQuestion: "FAQ", Content: "FAQ重复", Score: 0.7},
})
if len(results) != 3 {
t.Fatalf("expected 3 normalized results, got %d", len(results))
}
if !strings.Contains(results[0].Content, "第一段\n第二段") {
t.Fatalf("expected adjacent document chunks to merge, got %q", results[0].Content)
}
if results[0].Score != 0.9 {
t.Fatalf("expected merged score to keep max score, got %v", results[0].Score)
}
if results[1].Content != "独立段" {
t.Fatalf("expected section duplicates to be removed after merge, got %q", results[1].Content)
}
}
func TestBuildContextChunkText(t *testing.T) {
faqText := buildContextChunkText(i18nx.WithLocale(context.Background(), i18nx.LocaleZhCN), RetrieveResult{
FaqID: 1,
FaqQuestion: "如何退款",
Content: "在订单页发起退款。",
})
if !strings.Contains(faqText, "【FAQ:如何退款】") {
t.Fatalf("unexpected faq context text: %q", faqText)
}
docText := buildContextChunkText(i18nx.WithLocale(context.Background(), i18nx.LocaleZhCN), RetrieveResult{
DocumentID: 2,
DocumentTitle: "退款文档",
SectionPath: "售后/退款",
Content: "文档内容",
})
if !strings.Contains(docText, "【文档:退款文档|章节:售后/退款】") {
t.Fatalf("unexpected document context text: %q", docText)
}
}