package chunking import ( "testing" "gitea.maximumdirect.net/eric/audita/internal/core/schema" ) func TestComputeSummary(t *testing.T) { config := ChunkingConfig{ MaxSectionTokens: 100, MinSectionTokens: 10, } sections := []Section{ {Index: 0, EstimatedTokens: 30, StartSegmentID: 1, EndSegmentID: 2}, {Index: 1, EstimatedTokens: 50, StartSegmentID: 3, EndSegmentID: 4}, {Index: 2, EstimatedTokens: 20, StartSegmentID: 5, EndSegmentID: 5}, } summary := ComputeSummary(sections, config) if summary.ChunkCount != 3 { t.Errorf("expected chunk_count=3, got %d", summary.ChunkCount) } if summary.MinEstimatedTokens != 20 { t.Errorf("expected min_estimated_tokens=20, got %d", summary.MinEstimatedTokens) } if summary.MaxEstimatedTokens != 50 { t.Errorf("expected max_estimated_tokens=50, got %d", summary.MaxEstimatedTokens) } if summary.TotalEstimatedTokens != 100 { t.Errorf("expected total_estimated_tokens=100, got %d", summary.TotalEstimatedTokens) } if summary.MaxSectionTokens != 100 { t.Errorf("expected max_section_tokens=100, got %d", summary.MaxSectionTokens) } if summary.MinSectionTokens != 10 { t.Errorf("expected min_section_tokens=10, got %d", summary.MinSectionTokens) } if summary.TargetSections != nil { t.Errorf("expected target_sections=nil, got %v", summary.TargetSections) } } func TestComputeSummaryWithTarget(t *testing.T) { target := 5 config := ChunkingConfig{ MaxSectionTokens: 100, MinSectionTokens: 10, TargetSections: &target, } sections := []Section{ {Index: 0, EstimatedTokens: 30, StartSegmentID: 1, EndSegmentID: 2}, } summary := ComputeSummary(sections, config) if summary.TargetSections == nil || *summary.TargetSections != 5 { t.Errorf("expected target_sections=5, got %v", summary.TargetSections) } } func TestComputeSummaryEmptySections(t *testing.T) { config := ChunkingConfig{ MaxSectionTokens: 100, MinSectionTokens: 10, } sections := []Section{} summary := ComputeSummary(sections, config) if summary.ChunkCount != 0 { t.Errorf("expected chunk_count=0, got %d", summary.ChunkCount) } if summary.MinEstimatedTokens != 0 { t.Errorf("expected min_estimated_tokens=0 for empty, got %d", summary.MinEstimatedTokens) } if summary.MaxSectionTokens != 100 { t.Errorf("expected max_section_tokens preserved, got %d", summary.MaxSectionTokens) } } func TestComputeDetailedSummary(t *testing.T) { config := ChunkingConfig{ MaxSectionTokens: 100, MinSectionTokens: 10, } sections := []Section{ { Index: 0, EstimatedTokens: 30, StartSegmentID: 1, EndSegmentID: 2, Segments: make([]schema.Segment, 2), // 2 segments }, { Index: 1, EstimatedTokens: 50, StartSegmentID: 3, EndSegmentID: 5, Segments: make([]schema.Segment, 3), // 3 segments }, } detailed := ComputeDetailedSummary(sections, config) if detailed.ChunkCount != 2 { t.Errorf("expected chunk_count=2, got %d", detailed.ChunkCount) } if len(detailed.Chunks) != 2 { t.Fatalf("expected 2 chunk entries, got %d", len(detailed.Chunks)) } // Check first chunk if detailed.Chunks[0].Index != 0 { t.Errorf("expected chunk[0].index=0, got %d", detailed.Chunks[0].Index) } if detailed.Chunks[0].StartSegmentID != 1 { t.Errorf("expected chunk[0].start_segment_id=1, got %d", detailed.Chunks[0].StartSegmentID) } if detailed.Chunks[0].EndSegmentID != 2 { t.Errorf("expected chunk[0].end_segment_id=2, got %d", detailed.Chunks[0].EndSegmentID) } if detailed.Chunks[0].EstimatedTokens != 30 { t.Errorf("expected chunk[0].estimated_tokens=30, got %d", detailed.Chunks[0].EstimatedTokens) } if detailed.Chunks[0].SegmentCount != 2 { t.Errorf("expected chunk[0].segment_count=2, got %d", detailed.Chunks[0].SegmentCount) } // Check second chunk if detailed.Chunks[1].Index != 1 { t.Errorf("expected chunk[1].index=1, got %d", detailed.Chunks[1].Index) } if detailed.Chunks[1].StartSegmentID != 3 { t.Errorf("expected chunk[1].start_segment_id=3, got %d", detailed.Chunks[1].StartSegmentID) } if detailed.Chunks[1].EndSegmentID != 5 { t.Errorf("expected chunk[1].end_segment_id=5, got %d", detailed.Chunks[1].EndSegmentID) } if detailed.Chunks[1].SegmentCount != 3 { t.Errorf("expected chunk[1].segment_count=3, got %d", detailed.Chunks[1].SegmentCount) } }