deepdoc-core 0.3.0

Extract clean Markdown, JSON or text from documents — the DeepDoc engine.
Documentation
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
//! RAG chunking: `Document` → chunks that carry their heading context.
//!
//! Two ideas carry the whole module:
//!
//! * **Cut on block boundaries.** A paragraph, a table or a code block is never
//!   split down the middle — only a single block bigger than the target size is,
//!   and then at the friendliest boundary it offers (line, sentence, word).
//! * **Every chunk knows where it sits.** The chain of enclosing headings
//!   (`Handbook > Onboarding > Day one`) travels with the text, so an embedding
//!   of "it takes two days" still carries what "it" was.
//!
//! Sizes are counted in **characters**, not tokens: a tokenizer means a model
//! file or a large table inside a binary whose whole promise is that there is
//! nothing to install. Roughly four characters per token is close enough to aim
//! at an embedding model's window.
//!
//! `byte_range` indexes the Markdown rendering of the document — exactly what
//! `deepdoc file --format md` prints, without front matter — and slicing it with
//! the range gives the chunk's text back.
//!
//! Every chunk also carries a `hash` of its own content, so an index can tell
//! which chunks a parser upgrade actually changed instead of re-embedding a
//! whole corpus. See [`chunk_hash`] for what goes into it.

use std::fmt::Write as _;

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::model::{Block, Document};
use crate::render;

/// Default chunk size, in characters (see `docs/PRD/02-cli-spec.md`).
pub const DEFAULT_CHUNK_SIZE: usize = 800;

/// Options for [`chunk`].
#[derive(Debug, Clone)]
pub struct ChunkOpts {
    /// Target chunk size, in characters.
    pub size: usize,
    /// Overlap between neighbouring chunks, in characters.
    pub overlap: usize,
}

impl Default for ChunkOpts {
    fn default() -> Self {
        Self {
            size: DEFAULT_CHUNK_SIZE,
            overlap: 0,
        }
    }
}

/// One RAG chunk.
///
/// The field names are the public JSON schema (`docs/PRD/02-cli-spec.md`).
/// `source` repeats the document path on purpose: chunks are usually flattened
/// into a vector store one by one, and a chunk that cannot say where it came
/// from is a citation you cannot follow back.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Chunk {
    pub text: String,
    /// Enclosing headings, outermost first (`["Introduction", "Method"]`).
    pub heading_path: Vec<String>,
    /// Path the document was read from, when it is known.
    pub source: Option<String>,
    /// Byte range of the chunk inside the rendered Markdown.
    pub byte_range: (usize, usize),
    /// Content hash, `"sha256:<hex>"` — see [`chunk_hash`].
    pub hash: String,
}

/// Separator between the heading path's own entries.
const PATH_SEPARATOR: char = '\u{1F}';
/// Separator between the heading path and the text.
const FIELD_SEPARATOR: char = '\u{1E}';

/// The content hash of a chunk: `"sha256:<hex>"` over its heading path and its
/// text together.
///
/// The point of the hash is to answer "did this chunk change?" without
/// re-embedding a corpus. That question is about what the chunk *means* in an
/// index, and a chunk's meaning is not only its text: the enclosing headings
/// (`Handbook > Onboarding > Day one`) travel with it and sit **outside** `text`
/// for every chunk that does not open with its own heading. A document
/// reorganised under new headings gives the same text a different context — the
/// exact case the hash exists to catch — so the path is hashed too.
///
/// The two parts are joined with the ASCII unit and record separators, which do
/// not occur in extracted text, so no text can spell out a different split.
pub fn chunk_hash(heading_path: &[String], text: &str) -> String {
    let mut hasher = Sha256::new();
    for (index, heading) in heading_path.iter().enumerate() {
        if index > 0 {
            hasher.update([PATH_SEPARATOR as u8]);
        }
        hasher.update(heading.as_bytes());
    }
    hasher.update([FIELD_SEPARATOR as u8]);
    hasher.update(text.as_bytes());

    let mut out = String::with_capacity("sha256:".len() + 64);
    out.push_str("sha256:");
    for byte in hasher.finalize() {
        // Writing into a String cannot fail.
        let _ = write!(out, "{byte:02x}");
    }
    out
}

/// Split a document into chunks. Pure.
pub fn chunk(doc: &Document, opts: &ChunkOpts) -> Vec<Chunk> {
    let size = opts.size.max(1);
    // The overlap must leave room for new text, or two neighbours would say
    // almost the same thing and the second one would carry the work forward
    // hardly at all.
    let overlap = opts.overlap.min(size / 2);

    let (markdown, ranges) = render::markdown_with_ranges(&doc.blocks);
    let segments = segments(&markdown, &doc.blocks, &ranges, size);
    let groups = pack(&markdown, &segments, size);

    let source = doc.meta.source_path.clone();
    let mut chunks = Vec::with_capacity(groups.len());

    for (position, &(first, end)) in groups.iter().enumerate() {
        let start = match position {
            0 => first,
            _ => overlap_start(&markdown, &segments, groups[position - 1].0, first, overlap),
        };
        let (from, to) = (segments[start].start, segments[end - 1].end);
        let text = markdown[from..to].to_string();
        // The path of the chunk's own first block: text pulled in as overlap
        // belongs to the chunk before it.
        let heading_path = segments[first].path.clone();

        chunks.push(Chunk {
            hash: chunk_hash(&heading_path, &text),
            text,
            heading_path,
            source: source.clone(),
            byte_range: (from, to),
        });
    }

    chunks
}

/// A unit that a chunk is built from: one block, or one piece of an oversized
/// block.
struct Segment {
    start: usize,
    end: usize,
    /// Length in characters — what `size` is measured in.
    chars: usize,
    /// Enclosing headings at this point in the document.
    path: Vec<String>,
    /// True when the segment is a heading, and so opens a section.
    heading: bool,
}

/// Walk the blocks, tracking the heading stack, and cut oversized blocks up.
fn segments(
    markdown: &str,
    blocks: &[Block],
    ranges: &[Option<(usize, usize)>],
    size: usize,
) -> Vec<Segment> {
    let mut out = Vec::new();
    let mut stack: Vec<(u8, String)> = Vec::new();

    for (block, range) in blocks.iter().zip(ranges) {
        let Some((start, end)) = *range else { continue };

        if let Block::Heading { level, text } = block {
            // Levels are clamped the same way the Markdown renderer clamps
            // them, so `#######` and `######` close the same sections.
            let level = (*level).clamp(1, 6);
            while stack.last().is_some_and(|(open, _)| *open >= level) {
                stack.pop();
            }
            stack.push((level, render::one_line(&text.plain_text())));
        }

        let path: Vec<String> = stack.iter().map(|(_, text)| text.clone()).collect();
        let heading = matches!(block, Block::Heading { .. });

        for (index, (start, end)) in split_block(&markdown[start..end], start, size)
            .into_iter()
            .enumerate()
        {
            out.push(Segment {
                start,
                end,
                chars: markdown[start..end].chars().count(),
                path: path.clone(),
                heading: heading && index == 0,
            });
        }
    }

    out
}

/// Cut one oversized block into pieces of at most `size` characters.
///
/// Returns absolute ranges into the rendered Markdown. A block that already
/// fits comes back whole — the common case, and the one the block-boundary
/// promise is about.
fn split_block(text: &str, base: usize, size: usize) -> Vec<(usize, usize)> {
    if text.chars().count() <= size {
        return vec![(base, base + text.len())];
    }

    let mut out = Vec::new();
    let mut cursor = 0;

    while cursor < text.len() {
        let rest = &text[cursor..];
        if rest.chars().count() <= size {
            out.push((base + cursor, base + text.len()));
            break;
        }

        let window = &rest[..byte_len(rest, size)];
        let cut = cut_point(window);
        let piece = rest[..cut].trim_end();
        if !piece.is_empty() {
            out.push((base + cursor, base + cursor + piece.len()));
        }

        // Step over the whitespace at the seam so the next piece starts on text.
        let skipped = rest[cut..].len() - rest[cut..].trim_start().len();
        cursor += cut + skipped;
    }

    out
}

/// Where to cut a window of text: the last line break, else the last sentence
/// end, else the last word boundary, else the window itself.
fn cut_point(window: &str) -> usize {
    if let Some(index) = window.rfind('\n').filter(|index| *index > 0) {
        return index + 1;
    }

    let sentence_end = window.char_indices().rfind(|(index, ch)| {
        *index > 0
            && matches!(ch, '.' | '!' | '?' | '…')
            && window[index + ch.len_utf8()..]
                .chars()
                .next()
                .is_none_or(char::is_whitespace)
    });
    if let Some((index, ch)) = sentence_end {
        return index + ch.len_utf8();
    }

    window
        .char_indices()
        .rfind(|(index, ch)| *index > 0 && ch.is_whitespace())
        .map_or(window.len(), |(index, _)| index)
}

/// Byte length of the first `chars` characters.
fn byte_len(text: &str, chars: usize) -> usize {
    text.char_indices()
        .nth(chars)
        .map_or(text.len(), |(index, _)| index)
}

/// Greedily group segments into chunks; returns half-open index ranges.
///
/// A chunk grows until the next segment would overflow it. Headings also break
/// chunks, but not every heading: a document of many two-line sections would
/// otherwise chunk into a crowd of fragments too small to embed. So a heading
/// starts a new chunk when
///
/// * the current chunk is already worth keeping on its own (half the target), or
/// * the heading climbs back out of the section running just before it —
///   sibling and nested sections can share a chunk, `## Payroll` after
///   `### Tools` cannot, or the chunk's heading path would lie about half its
///   own text.
fn pack(markdown: &str, segments: &[Segment], size: usize) -> Vec<(usize, usize)> {
    let mut groups = Vec::new();
    let mut start = 0;
    let mut chars = 0;

    for (index, segment) in segments.iter().enumerate() {
        if index > start {
            let gap = gap_chars(markdown, &segments[index - 1], segment);
            let overflows = chars + gap + segment.chars > size;
            let new_section = segment.heading
                && (chars * 2 >= size || segment.path.len() < segments[index - 1].path.len());

            if overflows || new_section {
                groups.push((start, index));
                start = index;
                chars = 0;
            } else {
                chars += gap;
            }
        }
        chars += segment.chars;
    }

    if start < segments.len() {
        groups.push((start, segments.len()));
    }
    groups
}

/// Where a chunk starts once `overlap` characters of the previous one are
/// pulled in: whole segments only, and never the previous chunk entire.
fn overlap_start(
    markdown: &str,
    segments: &[Segment],
    previous: usize,
    first: usize,
    overlap: usize,
) -> usize {
    let mut start = first;
    let mut taken = 0;

    while start > previous + 1 {
        let segment = &segments[start - 1];
        let gap = gap_chars(markdown, segment, &segments[start]);
        if taken + gap + segment.chars > overlap {
            break;
        }
        taken += gap + segment.chars;
        start -= 1;
    }

    start
}

/// Characters of separator (usually the blank line) between two segments.
fn gap_chars(markdown: &str, left: &Segment, right: &Segment) -> usize {
    markdown[left.end..right.start].chars().count()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{Block, Inline, Metadata, Row};
    use crate::render::{RenderOpts, to_markdown};

    fn doc(blocks: Vec<Block>) -> Document {
        Document {
            meta: Metadata::default(),
            blocks,
        }
    }

    fn heading(level: u8, text: &str) -> Block {
        Block::Heading {
            level,
            text: Inline::text(text),
        }
    }

    fn opts(size: usize, overlap: usize) -> ChunkOpts {
        ChunkOpts { size, overlap }
    }

    /// Every chunk must be a verbatim slice of the Markdown the CLI prints —
    /// that is what `byte_range` promises.
    fn check_ranges(doc: &Document, chunks: &[Chunk]) {
        let markdown = to_markdown(doc, &RenderOpts::default());
        for chunk in chunks {
            let (start, end) = chunk.byte_range;
            assert_eq!(
                &markdown[start..end],
                chunk.text,
                "byte_range does not point at the chunk's text"
            );
        }
    }

    #[test]
    fn empty_document_yields_no_chunks() {
        let doc = Document::new(Metadata::default());
        assert!(chunk(&doc, &ChunkOpts::default()).is_empty());
    }

    #[test]
    fn a_short_document_is_one_chunk() {
        let doc = doc(vec![Block::paragraph("hello")]);
        let chunks = chunk(&doc, &ChunkOpts::default());
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].text, "hello");
        assert_eq!(chunks[0].byte_range, (0, 5));
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn chunks_cut_on_block_boundaries() {
        let doc = doc(vec![
            Block::paragraph("a".repeat(40)),
            Block::paragraph("b".repeat(40)),
            Block::paragraph("c".repeat(40)),
        ]);
        let chunks = chunk(&doc, &opts(90, 0));

        // Two paragraphs fit (40 + 2 + 40); the third opens the next chunk
        // rather than being cut in half.
        assert_eq!(chunks.len(), 2);
        assert_eq!(
            chunks[0].text,
            format!("{}\n\n{}", "a".repeat(40), "b".repeat(40))
        );
        assert_eq!(chunks[1].text, "c".repeat(40));
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn a_table_is_never_split_when_it_fits() {
        let doc = doc(vec![
            Block::paragraph("intro"),
            Block::Table {
                header: Some(Row::from_texts(["part", "qty"])),
                rows: vec![Row::from_texts(["bolt", "4"])],
            },
        ]);
        // Room for the table (44 characters) but not for the paragraph too.
        let chunks = chunk(&doc, &opts(46, 0));
        assert_eq!(chunks.len(), 2);
        assert_eq!(chunks[0].text, "intro");
        assert!(chunks[1].text.starts_with("| part | qty |"));
        assert!(chunks[1].text.ends_with("| bolt | 4   |"));
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn a_table_too_big_for_a_chunk_is_cut_between_rows() {
        let rows: Vec<Row> = (0..20)
            .map(|n| Row::from_texts([format!("row {n}"), n.to_string()]))
            .collect();
        let doc = doc(vec![Block::Table {
            header: Some(Row::from_texts(["part", "qty"])),
            rows,
        }]);
        let chunks = chunk(&doc, &opts(80, 0));

        assert!(chunks.len() > 1);
        for chunk in &chunks {
            for line in chunk.text.lines() {
                assert!(line.starts_with('|') && line.ends_with('|'), "{line:?}");
            }
        }
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn heading_path_follows_the_outline() {
        let doc = doc(vec![
            heading(1, "Handbook"),
            Block::paragraph("intro"),
            heading(2, "Onboarding"),
            Block::paragraph("day one"),
            heading(3, "Tools"),
            Block::paragraph("laptop"),
            heading(2, "Payroll"),
            Block::paragraph("monthly"),
        ]);
        // One block per chunk, so every path is easy to read off.
        let chunks = chunk(&doc, &opts(12, 0));
        let paths: Vec<Vec<String>> = chunks.iter().map(|c| c.heading_path.clone()).collect();

        assert_eq!(paths[0], ["Handbook"]);
        assert_eq!(paths[1], ["Handbook"]);
        assert_eq!(paths[2], ["Handbook", "Onboarding"]);
        assert_eq!(paths[3], ["Handbook", "Onboarding"]);
        assert_eq!(paths[4], ["Handbook", "Onboarding", "Tools"]);
        assert_eq!(paths[5], ["Handbook", "Onboarding", "Tools"]);
        // A second `##` closes both `## Onboarding` and its `### Tools`.
        assert_eq!(paths[6], ["Handbook", "Payroll"]);
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn a_heading_starts_a_new_chunk_once_the_current_one_is_worth_keeping() {
        let doc = doc(vec![
            heading(2, "One"),
            Block::paragraph("a".repeat(60)),
            heading(2, "Two"),
            Block::paragraph("b".repeat(20)),
        ]);
        let chunks = chunk(&doc, &opts(100, 0));

        assert_eq!(chunks.len(), 2);
        assert!(chunks[0].text.starts_with("## One"));
        assert!(chunks[1].text.starts_with("## Two"));
        assert_eq!(chunks[1].heading_path, ["Two"]);
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn short_sections_are_merged_rather_than_left_as_fragments() {
        let doc = doc(vec![
            heading(2, "One"),
            Block::paragraph("first"),
            heading(2, "Two"),
            Block::paragraph("second"),
        ]);
        let chunks = chunk(&doc, &opts(200, 0));
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].heading_path, ["One"]);
    }

    #[test]
    fn a_heading_that_leaves_the_section_breaks_the_chunk() {
        let doc = doc(vec![
            heading(1, "Handbook"),
            heading(2, "Onboarding"),
            heading(3, "Tools"),
            Block::paragraph("laptop"),
            heading(2, "Payroll"),
            Block::paragraph("monthly"),
        ]);
        // Everything would fit in one chunk, but `## Payroll` climbs back out
        // of `### Tools`: keeping them together would leave half the chunk
        // filed under a heading path it has nothing to do with.
        let chunks = chunk(&doc, &opts(500, 0));
        assert_eq!(chunks.len(), 2);
        assert_eq!(chunks[0].heading_path, ["Handbook"]);
        assert!(chunks[0].text.ends_with("laptop"));
        assert_eq!(chunks[1].heading_path, ["Handbook", "Payroll"]);
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn overlap_repeats_whole_blocks() {
        let doc = doc(vec![
            Block::paragraph("a".repeat(30)),
            Block::paragraph("b".repeat(30)),
            Block::paragraph("c".repeat(30)),
        ]);
        let chunks = chunk(&doc, &opts(64, 32));

        assert_eq!(chunks.len(), 2);
        assert_eq!(
            chunks[0].text,
            format!("{}\n\n{}", "a".repeat(30), "b".repeat(30))
        );
        // The second chunk opens with the block that closed the first one.
        assert_eq!(
            chunks[1].text,
            format!("{}\n\n{}", "b".repeat(30), "c".repeat(30))
        );
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn overlap_never_swallows_the_previous_chunk_whole() {
        let doc = doc(vec![
            Block::paragraph("a".repeat(20)),
            Block::paragraph("b".repeat(20)),
            Block::paragraph("c".repeat(20)),
            Block::paragraph("d".repeat(20)),
        ]);
        // An overlap as large as the chunk itself is clamped, and at least one
        // block always stays behind.
        let chunks = chunk(&doc, &opts(44, 1000));
        assert_eq!(chunks.len(), 2);
        assert!(chunks[1].text.starts_with(&"b".repeat(20)));
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn an_oversized_paragraph_is_cut_at_a_sentence_end() {
        let sentence = format!("{}. ", "word ".repeat(9).trim_end());
        let doc = doc(vec![Block::paragraph(sentence.repeat(4).trim_end())]);
        let chunks = chunk(&doc, &opts(120, 0));

        assert!(chunks.len() > 1);
        for chunk in &chunks {
            assert!(chunk.text.chars().count() <= 120, "{}", chunk.text);
            assert!(
                chunk.text.ends_with('.'),
                "cut mid-sentence: {}",
                chunk.text
            );
        }
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn an_oversized_block_without_sentences_is_cut_between_words() {
        let doc = doc(vec![Block::paragraph(
            "alpha bravo charlie delta echo foxtrot",
        )]);
        let chunks = chunk(&doc, &opts(20, 0));

        assert!(chunks.len() > 1);
        for chunk in &chunks {
            assert!(!chunk.text.starts_with(' ') && !chunk.text.ends_with(' '));
            assert!(chunk.text.split_whitespace().all(|word| {
                ["alpha", "bravo", "charlie", "delta", "echo", "foxtrot"].contains(&word)
            }));
        }
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn a_single_unbreakable_run_is_cut_on_a_character_boundary() {
        let doc = doc(vec![Block::paragraph("é".repeat(50))]);
        let chunks = chunk(&doc, &opts(20, 0));
        assert_eq!(chunks.len(), 3);
        assert!(chunks.iter().all(|c| c.text.chars().count() <= 20));
        check_ranges(&doc, &chunks);
    }

    #[test]
    fn chunks_carry_the_source_path() {
        let doc = Document {
            meta: Metadata {
                source_path: Some("handbook.pdf".into()),
                ..Metadata::default()
            },
            blocks: vec![Block::paragraph("hello")],
        };
        let chunks = chunk(&doc, &ChunkOpts::default());
        assert_eq!(chunks[0].source.as_deref(), Some("handbook.pdf"));
    }

    #[test]
    fn every_chunk_carries_a_sha256_hash() {
        let doc = doc(vec![
            heading(1, "Handbook"),
            Block::paragraph("a".repeat(60)),
            heading(2, "Payroll"),
            Block::paragraph("b".repeat(60)),
        ]);
        let chunks = chunk(&doc, &opts(80, 0));

        assert!(chunks.len() > 1);
        for chunk in &chunks {
            let hex = chunk.hash.strip_prefix("sha256:").expect("prefixed hash");
            assert_eq!(hex.len(), 64, "{}", chunk.hash);
            assert!(
                hex.chars()
                    .all(|c| c.is_ascii_hexdigit() && !c.is_uppercase())
            );
        }
    }

    /// The whole point of the hash: the same input must give the same one, run
    /// after run and process after process.
    #[test]
    fn the_hash_is_deterministic() {
        let doc = doc(vec![
            heading(2, "Payroll"),
            Block::paragraph("Salaries are paid monthly."),
        ]);
        let first = chunk(&doc, &ChunkOpts::default());
        let second = chunk(&doc, &ChunkOpts::default());

        let hashes: Vec<&str> = first.iter().map(|c| c.hash.as_str()).collect();
        let again: Vec<&str> = second.iter().map(|c| c.hash.as_str()).collect();
        assert_eq!(hashes, again);
        // And it is the hash of the fields, not of some run-local state.
        assert_eq!(
            first[0].hash,
            chunk_hash(&first[0].heading_path, &first[0].text)
        );
    }

    /// The heading path is part of a chunk's identity: reorganising a document
    /// changes what the same sentence means in an index, and that has to show.
    #[test]
    fn moving_a_chunk_under_a_new_heading_changes_the_hash() {
        let text = "Salaries are paid monthly.";
        let under_payroll = chunk_hash(&["Handbook".into(), "Payroll".into()], text);
        let under_benefits = chunk_hash(&["Handbook".into(), "Benefits".into()], text);
        assert_ne!(under_payroll, under_benefits);

        // And the path is hashed as a path, not as a run of concatenated words:
        // splitting one heading in two is a different chunk.
        assert_ne!(
            chunk_hash(&["Handbook".into(), "Payroll".into()], text),
            chunk_hash(&["HandbookPayroll".into()], text)
        );
    }

    #[test]
    fn the_same_text_under_the_same_path_hashes_the_same() {
        let path = vec!["Handbook".to_string()];
        assert_eq!(chunk_hash(&path, "hello"), chunk_hash(&path, "hello"));
        assert_ne!(chunk_hash(&path, "hello"), chunk_hash(&path, "hello "));
    }

    /// The chunk JSON schema is a public contract — this is what breaks when a
    /// field is renamed.
    #[test]
    fn json_schema_snapshot() {
        let chunk = Chunk {
            text: "Body.".into(),
            heading_path: vec!["Introduction".into()],
            source: Some("paper.pdf".into()),
            byte_range: (0, 5),
            hash: chunk_hash(&["Introduction".to_string()], "Body."),
        };
        assert_eq!(
            serde_json::to_value(&chunk).unwrap(),
            serde_json::json!({
                "text": "Body.",
                "heading_path": ["Introduction"],
                "source": "paper.pdf",
                "byte_range": [0, 5],
                // sha256 of "Introduction\u{1E}Body." — the path, then the text.
                "hash": "sha256:4e9b39d64994e392fa7a19bc795b4272591d5442c2fc6b90ec432ff2bbf9efe9",
            })
        );
    }
}