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