package main import ( "context" "encoding/json" "testing" "knowledge-governance/internal/governance" postgresstore "knowledge-governance/internal/platform/postgres" "knowledge-governance/internal/platform/weknora" ) func TestVerifyPublicationReturnsSanitizedEvidence(t *testing.T) { store := &reportStoreStub{ publication: postgresstore.PublicationForValidation{ID: "tp-1", TestKBID: "kb-1", KnowledgeID: "knowledge-secret-id"}, acceptance: []governance.AcceptanceRecord{ {CheckType: "knowledge_exists", State: string(governance.AcceptancePassed)}, {CheckType: "chunks_present", State: string(governance.AcceptancePassed)}, {CheckType: "retrieval_hit", State: string(governance.AcceptanceManualRequired)}, }, } client := &reportClientStub{ knowledge: weknora.Knowledge{ID: "knowledge-secret-id", KnowledgeBaseID: "kb-1", ParseStatus: "completed"}, chunks: []weknora.Chunk{ {ID: "chunk-1", KnowledgeID: "knowledge-secret-id", ChunkType: "text"}, {ID: "chunk-2", KnowledgeID: "knowledge-secret-id", ChunkType: "text"}, }, search: []weknora.SearchResult{{KnowledgeID: "knowledge-secret-id", Content: "answer contains MIKU-GOV-P3-5927", Score: 0.02}}, } report, err := verifyPublication(context.Background(), store, client, "tp-1", "unique marker", "MIKU-GOV-P3-5927") if err != nil { t.Fatalf("verifyPublication() error = %v", err) } if report.PublicationID != "tp-1" || report.RemoteStatus != "completed" || report.ChunkTotal != 2 || !report.AllChunksMatch || report.SearchResultCount != 1 || report.TargetSearchHits != 1 || !report.MarkerFound || !report.StructuralPassed || report.SemanticState != "manual_required" { t.Fatalf("report = %#v", report) } encoded, err := json.Marshal(report) if err != nil { t.Fatal(err) } for _, forbidden := range []string{"knowledge-secret-id", "chunk-1", "unique marker", "MIKU-GOV-P3-5927"} { if string(encoded) == forbidden || contains(string(encoded), forbidden) { t.Fatalf("report leaked %q: %s", forbidden, encoded) } } } func contains(value, fragment string) bool { for index := 0; index+len(fragment) <= len(value); index++ { if value[index:index+len(fragment)] == fragment { return true } } return false } type reportStoreStub struct { publication postgresstore.PublicationForValidation acceptance []governance.AcceptanceRecord } func (store *reportStoreStub) LoadPublicationForValidation(context.Context, string) (postgresstore.PublicationForValidation, error) { return store.publication, nil } func (store *reportStoreStub) ListAcceptanceResults(context.Context, string) ([]governance.AcceptanceRecord, error) { return store.acceptance, nil } type reportClientStub struct { knowledge weknora.Knowledge chunks []weknora.Chunk search []weknora.SearchResult } func (client *reportClientStub) GetKnowledge(context.Context, string) (weknora.Knowledge, error) { return client.knowledge, nil } func (client *reportClientStub) ListChunks(context.Context, string, int, int, []string) ([]weknora.Chunk, int64, error) { return client.chunks, int64(len(client.chunks)), nil } func (client *reportClientStub) SearchKnowledge(context.Context, weknora.SearchRequest) ([]weknora.SearchResult, error) { return client.search, nil }