reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//! Document Chunking Module
//!
//! Provides multiple chunking strategies for splitting documents into smaller pieces
//! suitable for embedding and retrieval.

use crate::{Chunk, Document, DocumentType, EmbeddingIds};
use uuid::Uuid;

/// Chunking strategy configuration
#[derive(Debug, Clone)]
pub struct ChunkingConfig {
    /// Target chunk size in tokens (approximate)
    pub chunk_size: usize,
    /// Overlap between chunks in tokens
    pub chunk_overlap: usize,
    /// Minimum chunk size (don't create tiny chunks)
    pub min_chunk_size: usize,
    /// Strategy to use
    pub strategy: ChunkingStrategy,
    /// Preserve sentence boundaries
    pub respect_sentences: bool,
}

impl Default for ChunkingConfig {
    fn default() -> Self {
        Self {
            chunk_size: 512,
            chunk_overlap: 50,
            min_chunk_size: 100,
            strategy: ChunkingStrategy::Recursive,
            respect_sentences: true,
        }
    }
}

/// Chunking strategies
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChunkingStrategy {
    /// Fixed token count chunks (simple splitting)
    FixedSize,
    /// Split on semantic boundaries (paragraphs, sections)
    Semantic,
    /// Recursive character splitting (try different delimiters)
    Recursive,
    /// Document-type aware chunking
    DocumentAware,
}

/// Chunking error
#[derive(Debug, thiserror::Error)]
pub enum ChunkingError {
    #[error("Text too short for chunking: {0} characters")]
    TextTooShort(usize),
    #[error("Invalid chunk size: {0}")]
    InvalidChunkSize(usize),
    #[error("Chunking failed: {0}")]
    ChunkingFailed(String),
}

/// Chunk a document into smaller pieces
pub fn chunk_document(
    document: &Document,
    config: &ChunkingConfig,
) -> Result<Vec<Chunk>, ChunkingError> {
    let text = &document.content.raw;

    if text.is_empty() {
        return Ok(Vec::new());
    }

    if text.len() < config.min_chunk_size {
        return Err(ChunkingError::TextTooShort(text.len()));
    }

    if config.chunk_size < config.min_chunk_size {
        return Err(ChunkingError::InvalidChunkSize(config.chunk_size));
    }

    let chunks = match config.strategy {
        ChunkingStrategy::FixedSize => chunk_fixed_size(text, config, document.id),
        ChunkingStrategy::Semantic => chunk_semantic(text, config, document.id, &document.doc_type),
        ChunkingStrategy::Recursive => {
            chunk_recursive(text, config, document.id, &document.doc_type)
        }
        ChunkingStrategy::DocumentAware => {
            chunk_document_aware(text, config, document.id, &document.doc_type)
        }
    }?;

    Ok(chunks)
}

/// Fixed-size chunking (simple token-based splitting)
fn chunk_fixed_size(
    text: &str,
    config: &ChunkingConfig,
    _document_id: Uuid,
) -> Result<Vec<Chunk>, ChunkingError> {
    let mut chunks = Vec::new();
    let chunk_size_chars = estimate_chars_from_tokens(config.chunk_size);
    let overlap_chars = estimate_chars_from_tokens(config.chunk_overlap);

    let mut start = 0;
    let mut index = 0;

    while start < text.len() {
        let end = (start + chunk_size_chars).min(text.len());
        let chunk_text = &text[start..end];

        if chunk_text.trim().len() < config.min_chunk_size {
            break; // Don't create tiny chunks at the end
        }

        let token_count = super::estimate_tokens(chunk_text);

        chunks.push(Chunk {
            id: Uuid::new_v4(),
            text: chunk_text.to_string(),
            index,
            start_char: start,
            end_char: end,
            token_count: Some(token_count),
            section: None,
            page: None,
            embedding_ids: EmbeddingIds::default(),
        });

        // Move start position with overlap
        let next_start = end.saturating_sub(overlap_chars);
        if next_start > start {
            start = next_start;
        } else {
            // Ensure we always make progress
            start += 1;
        }
        index += 1;

        // Prevent infinite loop (should be covered by loop condition, but good safety)
        if start >= text.len() {
            break;
        }
    }

    Ok(chunks)
}

/// Semantic chunking (split on paragraph/section boundaries)
fn chunk_semantic(
    text: &str,
    config: &ChunkingConfig,
    document_id: Uuid,
    doc_type: &DocumentType,
) -> Result<Vec<Chunk>, ChunkingError> {
    // First, split into paragraphs
    let paragraphs = super::split_paragraphs(text);

    if paragraphs.is_empty() {
        return chunk_fixed_size(text, config, document_id);
    }

    let mut chunks = Vec::new();
    let mut current_chunk = String::new();
    let mut current_start = 0;
    let mut chunk_index = 0;
    let chunk_size_chars = estimate_chars_from_tokens(config.chunk_size);
    let overlap_chars = estimate_chars_from_tokens(config.chunk_overlap);

    for paragraph in paragraphs.iter() {
        let para_text = paragraph.trim();
        if para_text.is_empty() {
            continue;
        }

        // If adding this paragraph would exceed chunk size, finalize current chunk
        if !current_chunk.is_empty()
            && (current_chunk.len() + para_text.len() + 1) > chunk_size_chars
        {
            // Create chunk from accumulated text
            let end_pos = current_start + current_chunk.len();
            let token_count = super::estimate_tokens(&current_chunk);

            chunks.push(Chunk {
                id: Uuid::new_v4(),
                text: current_chunk.clone(),
                index: chunk_index,
                start_char: current_start,
                end_char: end_pos,
                token_count: Some(token_count),
                section: extract_section_header(paragraph, doc_type),
                page: None,
                embedding_ids: EmbeddingIds::default(),
            });

            // Start new chunk with overlap
            let overlap_text = extract_overlap(&current_chunk, overlap_chars);
            current_chunk = format!("{}{}", overlap_text, para_text);
            current_start = end_pos.saturating_sub(overlap_chars);
            chunk_index += 1;
        } else {
            // Add paragraph to current chunk
            if current_chunk.is_empty() {
                current_start = text.find(para_text).unwrap_or(current_start);
            } else {
                current_chunk.push_str("\n\n");
            }
            current_chunk.push_str(para_text);
        }
    }

    // Add final chunk if there's remaining text
    if !current_chunk.trim().is_empty() {
        let end_pos = current_start + current_chunk.len();
        let token_count = super::estimate_tokens(&current_chunk);

        chunks.push(Chunk {
            id: Uuid::new_v4(),
            text: current_chunk,
            index: chunk_index,
            start_char: current_start,
            end_char: end_pos,
            token_count: Some(token_count),
            section: None,
            page: None,
            embedding_ids: EmbeddingIds::default(),
        });
    }

    Ok(chunks)
}

/// Recursive chunking (try different delimiters in order)
fn chunk_recursive(
    text: &str,
    config: &ChunkingConfig,
    document_id: Uuid,
    doc_type: &DocumentType,
) -> Result<Vec<Chunk>, ChunkingError> {
    let _chunk_size_chars = estimate_chars_from_tokens(config.chunk_size);

    // Try different delimiters in order of preference
    let delimiters = if matches!(doc_type, DocumentType::Code) {
        vec!["\n\n\n", "\n\n", "\n", ". ", " "]
    } else {
        // Documentation and other types use the same delimiters
        vec!["\n\n", "\n", ". ", " "]
    };

    chunk_recursive_internal(text, config, document_id, &delimiters, 0)
}

fn chunk_recursive_internal(
    text: &str,
    config: &ChunkingConfig,
    document_id: Uuid,
    delimiters: &[&str],
    delimiter_idx: usize,
) -> Result<Vec<Chunk>, ChunkingError> {
    if delimiter_idx >= delimiters.len() {
        // Fallback: character-level splitting
        return chunk_fixed_size(text, config, document_id);
    }

    let delimiter = delimiters[delimiter_idx];
    let chunk_size_chars = estimate_chars_from_tokens(config.chunk_size);
    let overlap_chars = estimate_chars_from_tokens(config.chunk_overlap);

    // Split by delimiter
    let parts: Vec<&str> = text.split(delimiter).collect();

    if parts.len() <= 1 {
        // Delimiter not found, try next one
        return chunk_recursive_internal(text, config, document_id, delimiters, delimiter_idx + 1);
    }

    let mut chunks = Vec::new();
    let mut current_chunk = String::new();
    let mut current_start = 0;
    let mut chunk_index = 0;

    for part in parts {
        let part_trimmed = part.trim();
        if part_trimmed.is_empty() {
            continue;
        }

        let part_with_delim = if current_chunk.is_empty() {
            part_trimmed.to_string()
        } else {
            format!("{}{}", delimiter, part_trimmed)
        };

        if (current_chunk.len() + part_with_delim.len()) > chunk_size_chars
            && !current_chunk.is_empty()
        {
            // Finalize current chunk
            let end_pos = current_start + current_chunk.len();
            let token_count = super::estimate_tokens(&current_chunk);

            chunks.push(Chunk {
                id: Uuid::new_v4(),
                text: current_chunk.clone(),
                index: chunk_index,
                start_char: current_start,
                end_char: end_pos,
                token_count: Some(token_count),
                section: None,
                page: None,
                embedding_ids: EmbeddingIds::default(),
            });

            // Start new chunk with overlap
            let overlap_text = extract_overlap(&current_chunk, overlap_chars);
            current_chunk = format!("{}{}", overlap_text, part_with_delim);
            current_start = end_pos.saturating_sub(overlap_chars);
            chunk_index += 1;
        } else {
            if current_chunk.is_empty() {
                current_start = text.find(part_trimmed).unwrap_or(current_start);
            }
            current_chunk.push_str(&part_with_delim);
        }
    }

    // Add final chunk
    if !current_chunk.trim().is_empty() {
        let end_pos = current_start + current_chunk.len();
        let token_count = super::estimate_tokens(&current_chunk);

        chunks.push(Chunk {
            id: Uuid::new_v4(),
            text: current_chunk,
            index: chunk_index,
            start_char: current_start,
            end_char: end_pos,
            token_count: Some(token_count),
            section: None,
            page: None,
            embedding_ids: EmbeddingIds::default(),
        });
    }

    // If chunks are still too large, recursively chunk them
    let mut final_chunks = Vec::new();
    for chunk in chunks {
        if chunk.text.len() > chunk_size_chars * 2 {
            // Chunk is too large, recursively split it
            let sub_chunks = chunk_recursive_internal(
                &chunk.text,
                config,
                document_id,
                delimiters,
                delimiter_idx + 1,
            )?;
            final_chunks.extend(sub_chunks);
        } else {
            final_chunks.push(chunk);
        }
    }

    Ok(final_chunks)
}

/// Document-aware chunking (uses document type to choose best strategy)
fn chunk_document_aware(
    text: &str,
    config: &ChunkingConfig,
    document_id: Uuid,
    doc_type: &DocumentType,
) -> Result<Vec<Chunk>, ChunkingError> {
    match doc_type {
        DocumentType::Code => {
            // For code: split on function/class boundaries
            chunk_code_aware(text, config, document_id)
        }
        DocumentType::Documentation | DocumentType::Paper => {
            // For docs/papers: try markdown-aware first, fall back to semantic
            if text.contains('#') {
                chunk_markdown_aware(text, config, document_id)
            } else {
                chunk_semantic(text, config, document_id, doc_type)
            }
        }
        _ => {
            // Default: recursive chunking
            chunk_recursive(text, config, document_id, doc_type)
        }
    }
}

/// Code-aware chunking (split on function/class boundaries)
fn chunk_code_aware(
    text: &str,
    config: &ChunkingConfig,
    document_id: Uuid,
) -> Result<Vec<Chunk>, ChunkingError> {
    // Simple approach: split on double newlines (common in code formatting)
    // In production, could use AST parsing for better boundaries
    let parts: Vec<&str> = text.split("\n\n\n").collect();

    if parts.len() <= 1 {
        // Fall back to recursive chunking
        return chunk_recursive(text, config, document_id, &DocumentType::Code);
    }

    let mut chunks = Vec::new();
    let mut current_pos = 0;

    for (idx, part) in parts.iter().enumerate() {
        let part_trimmed = part.trim();
        if part_trimmed.is_empty() {
            continue;
        }

        let start_pos = text[current_pos..]
            .find(part_trimmed)
            .map(|p| current_pos + p)
            .unwrap_or(current_pos);
        let end_pos = start_pos + part_trimmed.len();
        let token_count = super::estimate_tokens(part_trimmed);

        chunks.push(Chunk {
            id: Uuid::new_v4(),
            text: part_trimmed.to_string(),
            index: idx,
            start_char: start_pos,
            end_char: end_pos,
            token_count: Some(token_count),
            section: extract_function_name(part_trimmed),
            page: None,
            embedding_ids: EmbeddingIds::default(),
        });

        current_pos = end_pos;
    }

    Ok(chunks)
}

/// Markdown-aware chunking (split on headers)
fn chunk_markdown_aware(
    text: &str,
    config: &ChunkingConfig,
    document_id: Uuid,
) -> Result<Vec<Chunk>, ChunkingError> {
    // Split on markdown headers (# ## ###)
    // Use Lazy to compile regex once, or handle error if creating dynamically
    // Since this is a static pattern, expect is acceptable for the compilation itself if we trust the pattern
    let header_pattern = regex::Regex::new(r"(?m)^#{1,6}\s+.+$").expect("Invalid regex pattern");
    let mut chunks = Vec::new();
    let mut last_header_end = 0;
    let mut chunk_index = 0;
    let chunk_size_chars = estimate_chars_from_tokens(config.chunk_size);

    for mat in header_pattern.find_iter(text) {
        let header_start = mat.start();

        // If there's content between headers, create a chunk
        if header_start > last_header_end {
            let section_text = &text[last_header_end..header_start].trim();
            if !section_text.is_empty() && section_text.len() >= config.min_chunk_size {
                let token_count = super::estimate_tokens(section_text);
                let header_text = extract_previous_header(&text[..last_header_end]);

                chunks.push(Chunk {
                    id: Uuid::new_v4(),
                    text: section_text.to_string(),
                    index: chunk_index,
                    start_char: last_header_end,
                    end_char: header_start,
                    token_count: Some(token_count),
                    section: header_text,
                    page: None,
                    embedding_ids: EmbeddingIds::default(),
                });
                chunk_index += 1;
            }
        }

        last_header_end = header_start;
    }

    // Add final section
    if last_header_end < text.len() {
        let section_text = &text[last_header_end..].trim();
        if !section_text.is_empty() && section_text.len() >= config.min_chunk_size {
            let token_count = super::estimate_tokens(section_text);
            let header_text = extract_previous_header(&text[..last_header_end]);

            chunks.push(Chunk {
                id: Uuid::new_v4(),
                text: section_text.to_string(),
                index: chunk_index,
                start_char: last_header_end,
                end_char: text.len(),
                token_count: Some(token_count),
                section: header_text,
                page: None,
                embedding_ids: EmbeddingIds::default(),
            });
        }
    }

    // If no headers found or chunks are too large, fall back to semantic chunking
    if chunks.is_empty() || chunks.iter().any(|c| c.text.len() > chunk_size_chars * 2) {
        return chunk_semantic(text, config, document_id, &DocumentType::Documentation);
    }

    Ok(chunks)
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Estimate character count from token count (rough: ~4 chars per token)
fn estimate_chars_from_tokens(tokens: usize) -> usize {
    tokens * 4
}

/// Extract overlap text from end of chunk
fn extract_overlap(text: &str, overlap_chars: usize) -> String {
    if text.len() <= overlap_chars {
        return text.to_string();
    }

    // Calculate where to start searching for a sentence boundary
    let start_search = text.len().saturating_sub(overlap_chars);
    let overlap_region = &text[start_search..];

    // Try to find a sentence boundary in the overlap region
    // Prefer finding a boundary that gives us roughly overlap_chars
    if let Some(sentence_start) = overlap_region.find(|c: char| c.is_uppercase()) {
        // Check if preceded by punctuation
        if start_search + sentence_start >= 2 {
            let prev_chars =
                &text[start_search + sentence_start - 2..start_search + sentence_start];
            if prev_chars.ends_with(". ")
                || prev_chars.ends_with("! ")
                || prev_chars.ends_with("? ")
            {
                return text[start_search + sentence_start..].to_string();
            }
        }
    }

    // Fallback: just return the last overlap_chars
    text[start_search..].to_string()
}

/// Extract section header from paragraph (for semantic chunking)
fn extract_section_header(paragraph: &str, _doc_type: &DocumentType) -> Option<String> {
    // Look for markdown headers
    if let Some(header_match) = regex::Regex::new(r"^#{1,6}\s+(.+)$")
        .ok()
        .and_then(|re| re.captures(paragraph.lines().next().unwrap_or("")))
    {
        return header_match.get(1).map(|m| m.as_str().trim().to_string());
    }

    // Look for all-caps lines (common in papers)
    if let Some(first_line) = paragraph.lines().next() {
        if first_line.len() > 5
            && first_line
                .chars()
                .all(|c| c.is_uppercase() || c.is_whitespace() || c.is_ascii_punctuation())
        {
            return Some(first_line.trim().to_string());
        }
    }

    None
}

/// Extract function name from code (simple heuristic)
fn extract_function_name(code: &str) -> Option<String> {
    // Look for common function patterns
    let patterns = vec![
        r"fn\s+(\w+)",
        r"function\s+(\w+)",
        r"def\s+(\w+)",
        r"pub\s+fn\s+(\w+)",
    ];

    for pattern in patterns {
        if let Some(captures) = regex::Regex::new(pattern)
            .ok()
            .and_then(|re| re.captures(code))
        {
            return captures.get(1).map(|m| m.as_str().to_string());
        }
    }

    None
}

/// Extract previous header from text
fn extract_previous_header(text: &str) -> Option<String> {
    regex::Regex::new(r"(?m)^#{1,6}\s+(.+)$")
        .ok()
        .and_then(|re| {
            re.captures_iter(text)
                .last()
                .and_then(|cap| cap.get(1))
                .map(|m| m.as_str().trim().to_string())
        })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{DocumentType, Source, SourceType};
    use chrono::Utc;

    fn create_test_document(text: &str, doc_type: DocumentType) -> Document {
        let source = Source {
            source_type: SourceType::Local,
            url: None,
            path: Some("/test/doc.txt".to_string()),
            arxiv_id: None,
            github_repo: None,
            retrieved_at: Utc::now(),
            version: None,
        };

        Document::new(doc_type, source).with_content(text.to_string())
    }

    #[test]
    fn test_fixed_size_chunking() {
        let text = "This is a test document. ".repeat(100); // ~2800 chars
        let doc = create_test_document(&text, DocumentType::Note);
        let config = ChunkingConfig {
            chunk_size: 512,
            chunk_overlap: 50,
            min_chunk_size: 100,
            strategy: ChunkingStrategy::FixedSize,
            respect_sentences: true,
        };

        let chunks = chunk_document(&doc, &config).unwrap();
        assert!(!chunks.is_empty());
        assert!(chunks.len() > 1); // Should create multiple chunks

        // Verify chunk properties
        for chunk in &chunks {
            assert!(!chunk.text.is_empty());
            assert!(chunk.start_char < chunk.end_char);
            assert!(chunk.token_count.is_some());
        }
    }

    #[test]
    fn test_semantic_chunking() {
        let text = "First paragraph.\n\nSecond paragraph.\n\nThird paragraph.";
        let doc = create_test_document(text, DocumentType::Documentation);
        let config = ChunkingConfig {
            chunk_size: 100, // Small to force multiple chunks
            chunk_overlap: 10,
            min_chunk_size: 10,
            strategy: ChunkingStrategy::Semantic,
            respect_sentences: true,
        };

        let chunks = chunk_document(&doc, &config).unwrap();
        assert!(!chunks.is_empty());
    }

    #[test]
    fn test_recursive_chunking() {
        let text = "Sentence one. Sentence two. Sentence three. ".repeat(20);
        let doc = create_test_document(&text, DocumentType::Note);
        let config = ChunkingConfig {
            chunk_size: 200,
            chunk_overlap: 20,
            min_chunk_size: 50,
            strategy: ChunkingStrategy::Recursive,
            respect_sentences: true,
        };

        let chunks = chunk_document(&doc, &config).unwrap();
        assert!(!chunks.is_empty());
    }

    #[test]
    fn test_markdown_aware_chunking() {
        let text =
            "# Header 1\n\nContent under header 1.\n\n## Header 2\n\nContent under header 2.";
        let doc = create_test_document(text, DocumentType::Documentation);
        let config = ChunkingConfig {
            chunk_size: 200,
            chunk_overlap: 10,
            min_chunk_size: 10,
            strategy: ChunkingStrategy::DocumentAware,
            respect_sentences: true,
        };

        let chunks = chunk_document(&doc, &config).unwrap();
        assert!(!chunks.is_empty());

        // Should have section headers
        assert!(chunks.iter().any(|c| c.section.is_some()));
    }

    #[test]
    fn test_empty_text() {
        let doc = create_test_document("", DocumentType::Note);
        let config = ChunkingConfig::default();
        let chunks = chunk_document(&doc, &config).unwrap();
        assert!(chunks.is_empty());
    }

    #[test]
    fn test_text_too_short() {
        let doc = create_test_document("Short", DocumentType::Note);
        let config = ChunkingConfig {
            min_chunk_size: 100,
            ..Default::default()
        };
        let result = chunk_document(&doc, &config);
        assert!(result.is_err());
    }

    #[test]
    fn test_overlap_extraction() {
        let text = "This is a long sentence. This is another sentence. Final sentence.";
        let overlap = extract_overlap(text, 20);
        assert!(!overlap.is_empty());
        assert!(overlap.len() <= 20);
    }
}