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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
"""
Unit tests for Output Chunking and Streaming System.

Tests the ChunkedResult, ProgressiveProcessor, and ContentSummarizer
to ensure proper chunked output handling and streaming capabilities.
"""

import pytest
import time
import asyncio
from unittest.mock import Mock, AsyncMock, MagicMock
from typing import List, Dict, Any

import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))

from pipeline.chunking import (
    ChunkedResult, ProgressiveProcessor, ContentSummarizer
)


class TestChunkedResult:
    """Test ChunkedResult functionality."""
    
    def test_init(self):
        """Test ChunkedResult initialization."""
        result = ChunkedResult(
            chunk_id="chunk_1",
            task_id="task_123",
            agent_id="agent_456",
            content_chunk="This is a test chunk.",
            chunk_index=0,
            is_final=False,
            timestamp=time.time(),
            continuation_token="token_abc"
        )
        
        assert result.chunk_id == "chunk_1"
        assert result.task_id == "task_123"
        assert result.agent_id == "agent_456"
        assert result.content_chunk == "This is a test chunk."
        assert result.chunk_index == 0
        assert not result.is_final
        assert result.continuation_token == "token_abc"
        assert isinstance(result.timestamp, float)
        assert isinstance(result.metadata, dict)
    
    def test_init_with_defaults(self):
        """Test ChunkedResult with default values."""
        result = ChunkedResult(
            chunk_id="chunk_1",
            task_id="task_123", 
            agent_id="agent_456",
            content_chunk="Test content",
            chunk_index=0,
            is_final=True,
            timestamp=time.time()
        )
        
        assert result.metadata == {}
        assert result.continuation_token is None


class TestProgressiveProcessor:
    """Test ProgressiveProcessor functionality."""
    
    def test_init(self):
        """Test ProgressiveProcessor initialization."""
        content = "This is a test content for chunking. " * 10  # 400+ chars
        
        processor = ProgressiveProcessor(
            task_id="task_123",
            agent_id="agent_456",
            full_content=content,
            chunk_size=100
        )
        
        assert processor.task_id == "task_123"
        assert processor.agent_id == "agent_456"
        assert processor.full_content == content
        assert processor.chunk_size == 100
        assert processor._current_chunk_index == 0
        assert processor.total_chunks > 1  # Should need multiple chunks
        assert len(processor._continuation_tokens) == processor.total_chunks
    
    def test_post_init_calculations(self):
        """Test post-initialization calculations."""
        content = "A" * 250  # 250 characters
        processor = ProgressiveProcessor(
            task_id="task_1",
            agent_id="agent_1",
            full_content=content,
            chunk_size=100
        )
        
        # Should need 3 chunks: 100 + 100 + 50
        assert processor.total_chunks == 3
        assert len(processor._continuation_tokens) == 3
        
        # All tokens should be unique
        tokens = list(processor._continuation_tokens.values())
        assert len(tokens) == len(set(tokens))
    
    def test_get_first_chunk(self):
        """Test getting the first chunk."""
        content = "This is chunk one. This is chunk two. This is chunk three."
        processor = ProgressiveProcessor(
            task_id="task_1",
            agent_id="agent_1",
            full_content=content,
            chunk_size=20
        )
        
        first_chunk = processor.get_next_chunk()
        
        assert first_chunk is not None
        assert first_chunk.chunk_index == 0
        assert first_chunk.content_chunk == content[:20]
        assert not first_chunk.is_final  # Should have more chunks
        assert first_chunk.continuation_token is not None
        assert first_chunk.task_id == "task_1"
        assert first_chunk.agent_id == "agent_1"
    
    def test_get_chunk_sequence(self):
        """Test getting a sequence of chunks."""
        content = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"  # 26 characters
        processor = ProgressiveProcessor(
            task_id="task_1",
            agent_id="agent_1",
            full_content=content,
            chunk_size=10
        )
        
        chunks = []
        current_token = None
        
        # Get all chunks
        while True:
            chunk = processor.get_next_chunk(current_token)
            if chunk is None:
                break
            chunks.append(chunk)
            current_token = chunk.continuation_token
            
            if chunk.is_final:
                break
        
        assert len(chunks) == 3  # 10 + 10 + 6 characters
        assert chunks[0].content_chunk == "ABCDEFGHIJ"
        assert chunks[1].content_chunk == "KLMNOPQRST"
        assert chunks[2].content_chunk == "UVWXYZ"
        
        # Only last chunk should be final
        assert not chunks[0].is_final
        assert not chunks[1].is_final
        assert chunks[2].is_final
        
        # Final chunk should have no continuation token
        assert chunks[2].continuation_token is None
    
    def test_get_chunk_with_invalid_token(self):
        """Test getting chunk with invalid continuation token."""
        content = "Test content for invalid token testing."
        processor = ProgressiveProcessor(
            task_id="task_1",
            agent_id="agent_1", 
            full_content=content,
            chunk_size=15
        )
        
        # Try with invalid token
        chunk = processor.get_next_chunk("invalid_token_12345")
        assert chunk is None
    
    def test_get_chunk_by_index(self):
        """Test getting chunk by specific index."""
        content = "Index test: " + "ABCDEFGHIJ" * 5  # 62 characters total
        processor = ProgressiveProcessor(
            task_id="task_1",
            agent_id="agent_1",
            full_content=content,
            chunk_size=20
        )
        
        # Get chunk at index 1
        chunk = processor.get_chunk_by_index(1)
        
        assert chunk is not None
        assert chunk.chunk_index == 1
        assert chunk.content_chunk == content[20:40]
        assert chunk.task_id == "task_1"
        
        # Get chunk at index 0
        first_chunk = processor.get_chunk_by_index(0)
        assert first_chunk is not None
        assert first_chunk.chunk_index == 0
        assert first_chunk.content_chunk == content[:20]
        
        # Get final chunk
        last_index = processor.total_chunks - 1
        last_chunk = processor.get_chunk_by_index(last_index)
        assert last_chunk is not None
        assert last_chunk.is_final
        assert last_chunk.continuation_token is None
    
    def test_get_chunk_by_invalid_index(self):
        """Test getting chunk with invalid index."""
        content = "Short content"
        processor = ProgressiveProcessor(
            task_id="task_1",
            agent_id="agent_1",
            full_content=content,
            chunk_size=50
        )
        
        # Index out of bounds
        assert processor.get_chunk_by_index(-1) is None
        assert processor.get_chunk_by_index(10) is None
    
    def test_single_chunk_content(self):
        """Test content that fits in a single chunk."""
        content = "Short"
        processor = ProgressiveProcessor(
            task_id="task_1",
            agent_id="agent_1",
            full_content=content,
            chunk_size=100
        )
        
        assert processor.total_chunks == 1
        
        chunk = processor.get_next_chunk()
        assert chunk is not None
        assert chunk.chunk_index == 0
        assert chunk.content_chunk == content
        assert chunk.is_final
        assert chunk.continuation_token is None
    
    def test_empty_content(self):
        """Test processing empty content."""
        processor = ProgressiveProcessor(
            task_id="task_1",
            agent_id="agent_1",
            full_content="",
            chunk_size=100
        )
        
        assert processor.total_chunks == 1  # Empty content still creates one chunk
        
        chunk = processor.get_next_chunk()
        assert chunk is not None
        assert chunk.content_chunk == ""
        assert chunk.is_final
    
    def test_chunk_metadata_consistency(self):
        """Test that chunk metadata is consistent across requests."""
        content = "Metadata consistency test content for chunking."
        processor = ProgressiveProcessor(
            task_id="task_123",
            agent_id="agent_456",
            full_content=content,
            chunk_size=15
        )
        
        # Get same chunk multiple times
        chunk1 = processor.get_chunk_by_index(0)
        chunk2 = processor.get_chunk_by_index(0)
        
        assert chunk1.task_id == chunk2.task_id
        assert chunk1.agent_id == chunk2.agent_id
        assert chunk1.chunk_index == chunk2.chunk_index
        assert chunk1.content_chunk == chunk2.content_chunk
        assert chunk1.is_final == chunk2.is_final
        # Note: chunk_id and timestamp will be different as they're generated fresh
    
    def test_large_content_performance(self):
        """Test performance with large content."""
        # Create large content (10KB)
        content = "Large content test. " * 500  # ~10,000 characters
        
        start_time = time.time()
        processor = ProgressiveProcessor(
            task_id="perf_test",
            agent_id="agent_1",
            full_content=content,
            chunk_size=1000
        )
        
        # Should handle large content quickly
        assert time.time() - start_time < 1.0  # Less than 1 second
        
        # Should create reasonable number of chunks
        assert processor.total_chunks <= 15  # 10KB / 1KB + buffer
        
        # First chunk should work quickly
        start_time = time.time()
        first_chunk = processor.get_next_chunk()
        assert time.time() - start_time < 0.1  # Very fast
        
        assert first_chunk is not None
        assert len(first_chunk.content_chunk) == 1000


class TestContentSummarizer:
    """Test ContentSummarizer functionality."""
    
    def setup_method(self):
        """Set up test fixtures."""
        self.mock_llm_client = Mock()
        self.summarizer = ContentSummarizer(self.mock_llm_client)
    
    def test_init(self):
        """Test ContentSummarizer initialization."""
        assert self.summarizer.llm_client == self.mock_llm_client
    
    @pytest.mark.asyncio
    async def test_summarize_content_short_content(self):
        """Test summarization of short content that doesn't need summarization."""
        short_content = "This is a short text."
        
        result = await self.summarizer.summarize_content(
            content=short_content,
            target_tokens=100,
            agent_id="agent_1",
            task_id="task_1"
        )
        
        # Short content should be returned as-is
        assert result == short_content
        self.mock_llm_client.complete_async.assert_not_called()
    
    @pytest.mark.asyncio
    async def test_summarize_content_empty(self):
        """Test summarization of empty content."""
        result = await self.summarizer.summarize_content(
            content="",
            target_tokens=100,
            agent_id="agent_1",
            task_id="task_1"
        )
        
        assert result == ""
        self.mock_llm_client.complete_async.assert_not_called()
    
    @pytest.mark.asyncio
    async def test_summarize_content_needs_summarization(self):
        """Test summarization of long content."""
        # Create long content that needs summarization (>100 words)
        long_content = "This is a very long piece of content that needs to be summarized. " * 50
        
        # Mock LLM response
        mock_response = Mock()
        mock_response.content = "This is a concise summary of the long content."
        self.mock_llm_client.complete_async = AsyncMock(return_value=mock_response)
        
        result = await self.summarizer.summarize_content(
            content=long_content,
            target_tokens=50,
            agent_id="agent_1",
            task_id="task_1"
        )
        
        assert result == "This is a concise summary of the long content."
        
        # Check LLM was called with correct parameters
        self.mock_llm_client.complete_async.assert_called_once()
        call_args = self.mock_llm_client.complete_async.call_args
        
        assert call_args.kwargs["agent_id"] == "agent_1"
        assert call_args.kwargs["max_tokens"] == 50
        assert call_args.kwargs["temperature"] == 0.3
        assert "Summarize the following content" in call_args.kwargs["user_prompt"]
        assert long_content in call_args.kwargs["user_prompt"]
    
    @pytest.mark.asyncio
    async def test_summarize_content_llm_error(self):
        """Test summarization fallback when LLM fails."""
        long_content = "Content that needs summarization. " * 100
        
        # Mock LLM to raise an exception
        self.mock_llm_client.complete_async = AsyncMock(side_effect=Exception("LLM Error"))
        
        result = await self.summarizer.summarize_content(
            content=long_content,
            target_tokens=50,
            agent_id="agent_1",
            task_id="task_1"
        )
        
        # Should fallback to simple truncation
        assert result.endswith("...")
        assert len(result) <= 200 + 3  # 50 tokens * 4 chars + "..."
    
    def test_simple_truncate(self):
        """Test simple truncation fallback."""
        content = "This is a test content that will be truncated."
        
        result = self.summarizer._simple_truncate(content, target_tokens=5)
        
        # Should be truncated to ~20 characters (5 tokens * 4 chars)
        assert len(result) <= 23  # 20 + "..."
        assert result.endswith("...")
    
    def test_simple_truncate_short_content(self):
        """Test simple truncation with content shorter than limit."""
        short_content = "Short"
        
        result = self.summarizer._simple_truncate(short_content, target_tokens=10)
        
        # Should return content as-is
        assert result == short_content
    
    @pytest.mark.asyncio
    async def test_system_prompt_includes_target_tokens(self):
        """Test that system prompt includes target token limit."""
        long_content = "Long content for testing system prompt. " * 20
        target_tokens = 75
        
        mock_response = Mock()
        mock_response.content = "Summary"
        self.mock_llm_client.complete_async = AsyncMock(return_value=mock_response)
        
        await self.summarizer.summarize_content(
            content=long_content,
            target_tokens=target_tokens,
            agent_id="agent_1",
            task_id="task_1"
        )
        
        call_args = self.mock_llm_client.complete_async.call_args
        system_prompt = call_args.kwargs["system_prompt"]
        
        assert str(target_tokens) in system_prompt
        assert "Content Summarizer" in system_prompt
        assert "essential information" in system_prompt


class TestIntegrationScenarios:
    """Test realistic integration scenarios."""
    
    def test_chunked_blog_post_simulation(self):
        """Test chunking a realistic blog post."""
        # Simulate a blog post
        blog_content = """
        # The Future of Artificial Intelligence

        Artificial intelligence is rapidly transforming our world. In this comprehensive analysis, 
        we explore the key trends and implications for the future.

        ## Current State of AI

        Today's AI systems demonstrate remarkable capabilities in various domains including 
        natural language processing, computer vision, and decision-making. These systems are 
        being deployed across industries from healthcare to finance.

        ## Emerging Trends

        Several key trends are shaping the future of AI:
        1. Increased model sophistication
        2. Better human-AI collaboration
        3. Improved ethical frameworks
        4. Enhanced accessibility

        ## Challenges Ahead

        Despite progress, significant challenges remain including bias in algorithms, 
        privacy concerns, and the need for better interpretability.

        ## Conclusion

        The future of AI holds great promise, but requires careful consideration of ethical 
        implications and societal impact.
        """
        
        processor = ProgressiveProcessor(
            task_id="blog_post",
            agent_id="blog_writer",
            full_content=blog_content.strip(),
            chunk_size=300
        )
        
        chunks = []
        current_token = None
        
        while True:
            chunk = processor.get_next_chunk(current_token)
            if chunk is None:
                break
            chunks.append(chunk)
            current_token = chunk.continuation_token
            if chunk.is_final:
                break
        
        # Should create multiple chunks
        assert len(chunks) >= 3
        
        # Reconstruct content from chunks
        reconstructed = "".join(chunk.content_chunk for chunk in chunks)
        assert reconstructed == blog_content.strip()
        
        # Check chunk properties
        for i, chunk in enumerate(chunks):
            assert chunk.chunk_index == i
            assert chunk.task_id == "blog_post"
            assert chunk.agent_id == "blog_writer"
        
        # Only last chunk should be final
        for chunk in chunks[:-1]:
            assert not chunk.is_final
            assert chunk.continuation_token is not None
        
        assert chunks[-1].is_final
        assert chunks[-1].continuation_token is None
    
    @pytest.mark.asyncio
    async def test_summarization_fallback_scenario(self):
        """Test realistic summarization scenario with fallback."""
        # Long research content that exceeds token limit
        research_content = """
        Research Analysis: Climate Change Impact on Agricultural Systems
        
        Executive Summary: This comprehensive study examines the multifaceted impacts 
        of climate change on global agricultural systems, analyzing temperature variations,
        precipitation patterns, and extreme weather events across multiple geographic regions.
        
        Methodology: We employed a mixed-methods approach combining quantitative climate data
        analysis with qualitative assessments from agricultural stakeholders across 15 countries.
        
        Key Findings:
        1. Temperature increases of 2-3°C significantly reduce crop yields in tropical regions
        2. Changing precipitation patterns affect irrigation-dependent systems most severely  
        3. Extreme weather events cause both immediate and long-term agricultural disruption
        4. Adaptation strategies show varying effectiveness across different crop types
        
        Regional Analysis: Sub-Saharan Africa shows greatest vulnerability while Northern
        European regions may experience some agricultural benefits from moderate warming.
        
        Recommendations: Immediate implementation of climate-resilient farming practices,
        investment in drought-resistant crop varieties, and improved early warning systems.
        """ * 3  # Triple the content to ensure it needs summarization
        
        mock_llm_client = Mock()
        mock_response = Mock()
        mock_response.content = "Climate change significantly impacts global agriculture through temperature increases, changing precipitation, and extreme weather. Key recommendations include climate-resilient farming and drought-resistant crops."
        
        mock_llm_client.complete_async = AsyncMock(return_value=mock_response)
        
        summarizer = ContentSummarizer(mock_llm_client)
        
        summary = await summarizer.summarize_content(
            content=research_content,
            target_tokens=100,
            agent_id="research_agent",
            task_id="climate_research"
        )
        
        # Should get summarized version
        assert summary != research_content
        assert "climate change" in summary.lower()
        assert "agriculture" in summary.lower()
        assert len(summary) < len(research_content)
        
        # Verify LLM was called with research content
        mock_llm_client.complete_async.assert_called_once()
        call_args = mock_llm_client.complete_async.call_args
        assert "climate change" in call_args.kwargs["user_prompt"].lower()
    
    def test_progressive_synthesis_workflow(self):
        """Test a progressive synthesis workflow with multiple agents."""
        # Simulate multiple agent contributions
        contributions = [
            "Research findings on quantum computing fundamentals and current capabilities.",
            "Analysis of market trends and commercial applications in the quantum computing sector.",
            "Technical review of hardware approaches: superconducting, trapped ion, and photonic systems.",
            "Risk assessment and timeline projections for quantum computing milestones."
        ]
        
        # Each contribution gets processed into chunks
        processors = []
        all_chunks = []
        
        for i, contribution in enumerate(contributions):
            processor = ProgressiveProcessor(
                task_id=f"synthesis_task",
                agent_id=f"agent_{i+1}",
                full_content=contribution,
                chunk_size=50
            )
            processors.append(processor)
            
            # Get first chunk from each processor
            first_chunk = processor.get_next_chunk()
            if first_chunk:
                all_chunks.append(first_chunk)
        
        # Should have chunks from multiple agents
        assert len(all_chunks) == 4
        
        # Each chunk should have different agent_id but same task_id
        agent_ids = {chunk.agent_id for chunk in all_chunks}
        task_ids = {chunk.task_id for chunk in all_chunks}
        
        assert len(agent_ids) == 4  # Four different agents
        assert len(task_ids) == 1   # Same task
        assert "synthesis_task" in task_ids


if __name__ == "__main__":
    pytest.main([__file__, "-v"])