From 5bf2ae1cc27aa555957dc1c258968c5b36246b26 Mon Sep 17 00:00:00 2001 From: Victor Maznyuk Date: Sat, 26 Sep 2026 11:06:30 +0200 Subject: [PATCH] fix(faq): actually clean FAQ entry payloads before validation and storage sanitizeFAQEntryPayload called meta.Normalize() and discarded the result. Normalize returns a normalized copy for hashing and indexing and does not touch its receiver, so no cleaning happened: blank, whitespace-only and duplicate similar/negative questions and answers were stored as sent, two empty similar questions were reported as a duplicate, and answers: [" "] passed the "at least one answer" check. Call meta.Sanitize(), the in-place method meant for storage (trim, drop empties, de-duplicate, keep the original text). Co-Authored-By: Claude Opus 5.5 (1M context) --- internal/application/service/knowledge_faq.go | 2 +- .../service/knowledge_faq_sanitize_test.go | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 internal/application/service/knowledge_faq_sanitize_test.go diff --git a/internal/application/service/knowledge_faq.go b/internal/application/service/knowledge_faq.go index 01e288fe..49aa52ec 100644 --- a/internal/application/service/knowledge_faq.go +++ b/internal/application/service/knowledge_faq.go @@ -1862,7 +1862,7 @@ func sanitizeFAQEntryPayload(payload *types.FAQEntryPayload) (*types.FAQChunkMet Version: 1, Source: "faq", } - meta.Normalize() + meta.Sanitize() if meta.StandardQuestion == "" { return nil, werrors.NewBadRequestError("标准问不能为空") } diff --git a/internal/application/service/knowledge_faq_sanitize_test.go b/internal/application/service/knowledge_faq_sanitize_test.go new file mode 100644 index 00000000..8357d354 --- /dev/null +++ b/internal/application/service/knowledge_faq_sanitize_test.go @@ -0,0 +1,44 @@ +package service + +import ( + "testing" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/require" +) + +func TestSanitizeFAQEntryPayloadCleansQuestionLists(t *testing.T) { + meta, err := sanitizeFAQEntryPayload(&types.FAQEntryPayload{ + StandardQuestion: " How do I reset my password? ", + SimilarQuestions: []string{"", " Forgot password ", "Forgot password", " "}, + NegativeQuestions: []string{" ", "Reset username", " Reset username "}, + Answers: []string{" Use the reset link. ", "", "Use the reset link."}, + }) + require.NoError(t, err) + require.Equal(t, "How do I reset my password?", meta.StandardQuestion) + require.Equal(t, []string{"Forgot password"}, meta.SimilarQuestions) + require.Equal(t, []string{"Reset username"}, meta.NegativeQuestions) + require.Equal(t, []string{"Use the reset link."}, meta.Answers) +} + +func TestSanitizeFAQEntryPayloadKeepsOriginalText(t *testing.T) { + // Storage keeps what the user wrote: only whitespace and duplicates are + // dropped, no lower-casing or punctuation stripping (that is Normalize, + // which is for hashing and indexing). + meta, err := sanitizeFAQEntryPayload(&types.FAQEntryPayload{ + StandardQuestion: "What is WeKnora?", + SimilarQuestions: []string{"Tell me about WeKnora!"}, + Answers: []string{"A RAG framework."}, + }) + require.NoError(t, err) + require.Equal(t, "What is WeKnora?", meta.StandardQuestion) + require.Equal(t, []string{"Tell me about WeKnora!"}, meta.SimilarQuestions) +} + +func TestSanitizeFAQEntryPayloadRejectsBlankAnswers(t *testing.T) { + _, err := sanitizeFAQEntryPayload(&types.FAQEntryPayload{ + StandardQuestion: "How do I reset my password?", + Answers: []string{" ", ""}, + }) + require.Error(t, err) +}