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) <noreply@anthropic.com>
main
Victor Maznyuk 2026-09-26 11:06:30 +02:00 committed by lyingbug
parent a570dffcfa
commit 5bf2ae1cc2
2 changed files with 45 additions and 1 deletions

View File

@ -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("标准问不能为空")
}

View File

@ -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)
}