Files
knowledge-governance/internal/releaseadapter/v071/library_question_index_test.go
T
姚章浩 2fc6dc068b
build / build (push) Successful in 1m11s
fix(snapshot): 按持久元数据核验生成问题索引
2026-10-28 01:24:32 +08:00

44 lines
1.7 KiB
Go

package v071
import (
"encoding/json"
"testing"
)
func TestLibraryCandidateGeneratedQuestionIndex(t *testing.T) {
for _, mode := range []string{"complete", "wrong-id", "wrong-content", "missing-question", "missing-body", "foreign-source", "question-as-body"} {
t.Run(mode, func(t *testing.T) {
pg, es, _ := libraryCandidateFixture()
pg.Tables[1] = newTableSnapshot("chunks", []json.RawMessage{json.RawMessage(`{"id":"chunk-safe","tenant_id":10000,"knowledge_base_id":"kb-safe","knowledge_id":"doc-safe","content":"chunk","chunk_type":"text","status":0,"metadata":{"generated_questions":[{"id":"q1","question":"How does it work?"}]}}`)})
questionID, content := "chunk-safe-q1", "How does it work?"
if mode == "wrong-id" {
questionID = "chunk-safe-q2"
}
if mode == "wrong-content" {
content = "Different question"
}
if mode == "foreign-source" {
questionID = "foreign-q1"
}
if mode == "question-as-body" {
content = "chunk"
}
raw, _ := json.Marshal(map[string]any{"chunk_id": "chunk-safe", "knowledge_id": "doc-safe", "knowledge_base_id": "kb-safe", "source_id": questionID, "content": content})
q := SearchDocument{ID: "question-index", Source: raw}
if mode == "missing-body" {
es.Documents = []SearchDocument{q}
} else if mode != "missing-question" {
es.Documents = append(es.Documents, q)
}
es.DocumentsSHA256, _ = searchDocumentsDigest(es.Documents)
err := validateLibraryCandidate(PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, pg, es)
if mode == "complete" && err != nil {
t.Fatalf("valid persisted question rejected: %v", err)
}
if mode != "complete" && err == nil {
t.Fatal("incomplete or inconsistent question index accepted")
}
})
}
}