package testweknora import ( "context" "strings" "sync/atomic" "testing" "time" "knowledge-governance/internal/platform/weknora" ) func TestVerifyLegacyClassifiesWithoutLeaking(t *testing.T) { client := &legacyClientStub{getResults: map[string]legacyGetResult{ "knowledge-completed-a": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "completed"}}, "knowledge-completed-b": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "completed"}}, "knowledge-pending": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "pending"}}, "knowledge-processing": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "processing"}}, "knowledge-finalizing": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "finalizing"}}, "knowledge-failed": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "failed"}}, "knowledge-wrong-kb": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-other", ParseStatus: "completed"}}, "knowledge-missing": {err: &weknora.HTTPError{StatusCode: 404}}, "knowledge-server-error": {err: &weknora.HTTPError{StatusCode: 500}}, }, chunkTotal: map[string]int64{ "knowledge-completed-a": 2, "knowledge-completed-b": 0, }} candidates := []LegacyCandidate{ {ID: "pub-1", KnowledgeID: "knowledge-completed-a", TestKBID: "kb-1"}, {ID: "pub-2", KnowledgeID: "knowledge-completed-b", TestKBID: "kb-1"}, {ID: "pub-3", KnowledgeID: "knowledge-pending", TestKBID: "kb-1"}, {ID: "pub-4", KnowledgeID: "knowledge-processing", TestKBID: "kb-1"}, {ID: "pub-5", KnowledgeID: "knowledge-finalizing", TestKBID: "kb-1"}, {ID: "pub-6", KnowledgeID: "knowledge-failed", TestKBID: "kb-1"}, {ID: "pub-7", KnowledgeID: "knowledge-wrong-kb", TestKBID: "kb-1"}, {ID: "pub-8", KnowledgeID: "knowledge-missing", TestKBID: "kb-1"}, {ID: "pub-9", KnowledgeID: "knowledge-server-error", TestKBID: "kb-1"}, } report, err := VerifyLegacy(context.Background(), client, candidates, 1) if err != nil { t.Fatalf("VerifyLegacy() error = %v", err) } want := LegacyVerificationReport{ Total: 9, Existing: 6, Missing: 1, WrongKB: 1, Pending: 1, Processing: 1, Finalizing: 1, Completed: 2, Failed: 1, ChunkChecked: 2, ChunkEmpty: 1, HTTPErrors: 1, } if report != want { t.Fatalf("report = %#v, want %#v", report, want) } } func TestVerifyLegacyChunkSampleEverySkipsUnsampledCompleted(t *testing.T) { client := &legacyClientStub{getResults: map[string]legacyGetResult{ "knowledge-a": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "completed"}}, "knowledge-b": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "completed"}}, }, chunkTotal: map[string]int64{ "knowledge-b": 5, }} candidates := []LegacyCandidate{ {ID: "pub-1", KnowledgeID: "knowledge-a", TestKBID: "kb-1"}, {ID: "pub-2", KnowledgeID: "knowledge-b", TestKBID: "kb-1"}, } report, err := VerifyLegacy(context.Background(), client, candidates, 2) if err != nil { t.Fatal(err) } if report.Completed != 2 || report.ChunkChecked != 1 { t.Fatalf("report = %#v", report) } if client.chunkQueries != 1 { t.Fatalf("chunk queries = %d, want 1", client.chunkQueries) } } func TestVerifyLegacyConcurrentBoundsConcurrencyAndAggregates(t *testing.T) { client := &concurrentLegacyClient{} candidates := make([]LegacyCandidate, 12) for index := range candidates { candidates[index] = LegacyCandidate{ID: "publication", KnowledgeID: "knowledge", TestKBID: "kb-1"} } report, err := VerifyLegacyConcurrent(context.Background(), client, candidates, 0, 3) if err != nil { t.Fatalf("VerifyLegacyConcurrent() error = %v", err) } if report.Total != 12 || report.Existing != 12 || report.Completed != 12 { t.Fatalf("report = %#v", report) } if maximum := client.maximum.Load(); maximum < 2 || maximum > 3 { t.Fatalf("maximum concurrency = %d, want 2..3", maximum) } } func TestVerifyLegacyConcurrentReturnsImmediatelyForEmptyInput(t *testing.T) { report, err := VerifyLegacyConcurrent(context.Background(), &concurrentLegacyClient{}, nil, 10, 4) if err != nil { t.Fatalf("VerifyLegacyConcurrent() error = %v", err) } if report != (LegacyVerificationReport{}) { t.Fatalf("report = %#v", report) } } func TestFormatDryRunReportAggregatesWithoutKnowledgeIDs(t *testing.T) { candidates := []LegacyCandidate{ {ID: "pub-1", KnowledgeID: "knowledge-1", TestKBID: "kb-1"}, {ID: "pub-2", KnowledgeID: "knowledge-2", TestKBID: "kb-1"}, {ID: "pub-3", KnowledgeID: "knowledge-3", TestKBID: "kb-2"}, } report := FormatDryRunReport(candidates, 4) if !strings.Contains(report, "total=3") || !strings.Contains(report, "concurrency=4") || !strings.Contains(report, "knowledge_base=kb-1 count=2") || !strings.Contains(report, "knowledge_base=kb-2 count=1") { t.Fatalf("report = %q", report) } if strings.Contains(report, "knowledge-1") || strings.Contains(report, "pub-1") { t.Fatalf("report leaked knowledge/publication ID: %q", report) } } func TestFormatLegacyVerificationReportContainsOnlyAggregateCounts(t *testing.T) { report := FormatLegacyVerificationReport(LegacyVerificationReport{ Total: 530, Existing: 500, Missing: 20, WrongKB: 10, Completed: 490, Failed: 10, ChunkChecked: 20, ChunkEmpty: 2, HTTPErrors: 3, }) for _, expected := range []string{"total=530", "existing=500", "missing=20", "wrong_kb=10", "completed=490", "failed=10", "chunk_checked=20", "chunk_empty=2", "http_errors=3"} { if !strings.Contains(report, expected) { t.Fatalf("report missing %q: %q", expected, report) } } for _, forbidden := range []string{"knowledge_id", "knowledge_base", "publication_id"} { if strings.Contains(report, forbidden) { t.Fatalf("report leaked identifier field %q: %q", forbidden, report) } } } type legacyClientStub struct { getResults map[string]legacyGetResult chunkTotal map[string]int64 chunkQueries int } type legacyGetResult struct { knowledge weknora.Knowledge err error } type concurrentLegacyClient struct { current atomic.Int64 maximum atomic.Int64 } func (client *concurrentLegacyClient) GetKnowledge(context.Context, string) (weknora.Knowledge, error) { current := client.current.Add(1) for { maximum := client.maximum.Load() if current <= maximum || client.maximum.CompareAndSwap(maximum, current) { break } } time.Sleep(10 * time.Millisecond) client.current.Add(-1) return weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "completed"}, nil } func (*concurrentLegacyClient) ListChunks(context.Context, string, int, int, []string) ([]weknora.Chunk, int64, error) { return nil, 1, nil } func (client *legacyClientStub) GetKnowledge(_ context.Context, knowledgeID string) (weknora.Knowledge, error) { result, ok := client.getResults[knowledgeID] if !ok { return weknora.Knowledge{}, &weknora.HTTPError{StatusCode: 404} } return result.knowledge, result.err } func (client *legacyClientStub) ListChunks(_ context.Context, knowledgeID string, _ int, _ int, _ []string) ([]weknora.Chunk, int64, error) { client.chunkQueries++ total, ok := client.chunkTotal[knowledgeID] if !ok { total = 0 } chunks := make([]weknora.Chunk, 0, total) for i := int64(0); i < total; i++ { chunks = append(chunks, weknora.Chunk{ID: "chunk", KnowledgeID: knowledgeID}) } return chunks, total, nil }