1
//! Break tokens — the value-type "resume here" state for true fragmentation.
2
//!
3
//! The layout contract is LayoutNG-shaped and PURE:
4
//!
5
//! ```text
6
//! (node, constraint_space { remaining_extent }, break_token?) -> (fragment, break_token?)
7
//! ```
8
//!
9
//! Tokens are OWNED, comparable value types — nothing about pagination is
10
//! ever written into the node tree, the layout tree, or any cache during a
11
//! pass (the fossil at `paged_layout.rs:1-11` documents why the mutable
12
//! alternative failed). Two laws every consumer may rely on:
13
//!
14
//! 1. **Determinism**: identical input (content + constraints + incoming token) produces an
15
//!    identical outgoing token, comparable with `==`. Equality is structural; float fields inherit
16
//!    text3's rounding-tolerant `Rect` comparison, which is safe in the conservative direction — a
17
//!    false *inequality* merely re-lays one extra page, a false *equality* cannot arise from
18
//!    tolerant comparison of identical-bits passes.
19
//! 2. **Progress**: an outgoing token never equals the incoming token of the same fragmentainer
20
//!    (the page loop asserts this; violating it is the NG infinite-loop class).
21
//!
22
//! `token_fingerprint` is a FAST-PATH REJECTOR for K34 convergence checks:
23
//! `a == b ⇒ fingerprint(a) == fingerprint(b)` (it hashes a subset of the
24
//! compared fields). Convergence must NEVER be decided on fingerprints
25
//! alone — equal fingerprints require the full `==` before stopping
26
//! repagination (a collision that stopped early would ship stale pages).
27
//!
28
//! Provenance note: the token SHAPE follows public architecture prose
29
//! (css-break-3, the `RenderingNG` fragmentation article, the `LayoutNG`
30
//! README); no engine implementation source was consulted. See the design
31
//! doc §9.
32

            
33
use alloc::{boxed::Box, vec::Vec};
34

            
35
use crate::text3::cache::{BreakCursor, Hyphens, LineBreakStrictness, ShapedItem, WordBreak};
36

            
37
/// The resume state a fragmentainer boundary produced. `None` anywhere a
38
/// token could appear means "finished — nothing to resume".
39
#[derive(Debug, Clone, PartialEq)]
40
pub enum BreakToken {
41
    /// Resume a block-level box (its unfinished/unstarted children carry
42
    /// their own tokens).
43
    Block(BlockBreakToken),
44
    /// Resume an inline formatting context mid-flow.
45
    Inline(InlineBreakToken),
46
}
47

            
48
/// Resume state for one BLOCK box. Invariant (asserted by consumers, not
49
/// trusted): every sibling BEFORE the first entry in `children` is FINISHED
50
/// — `children` is the unfinished tail, in document order.
51
#[derive(Debug, Clone, PartialEq)]
52
pub struct BlockBreakToken {
53
    /// Layout-tree index of the box this token resumes. Tokens never
54
    /// outlive their layout generation (they are regenerated per pass), so
55
    /// the index is same-generation by construction; `generation` exists to
56
    /// assert that in debug builds.
57
    pub node: usize,
58
    /// Block-size of this box already consumed by previous fragmentainers.
59
    /// Drives `box-decoration-break: slice` (default: no re-emitted top
60
    /// decoration on resume) and monolith overflow resumption.
61
    pub consumed_block_size: f32,
62
    /// The unfinished tail of this box's children, document order.
63
    pub children: Vec<ChildBreakEntry>,
64
    /// Layout-generation stamp for debug assertions (see `node`).
65
    pub generation: u64,
66
}
67

            
68
/// One unfinished child in a [`BlockBreakToken`].
69
#[derive(Debug, Clone, PartialEq)]
70
pub enum ChildBreakEntry {
71
    /// The child started in an earlier fragmentainer; resume it with this
72
    /// token.
73
    ResumeIn {
74
        child: usize,
75
        token: Box<BreakToken>,
76
    },
77
    /// The child has not started yet — a break landed before it.
78
    /// `forced` = the break came from `break-before: page` (or a
79
    /// `<pagebreak/>` node): css-break-3 §5.2 truncates margins adjoining
80
    /// UNFORCED breaks only, so the resume side keeps this child's top
81
    /// margin iff the break was forced.
82
    BreakBefore { child: usize, forced: bool },
83
}
84

            
85
/// Owned snapshot of text3's [`BreakCursor`] — the inline resume state.
86
///
87
/// `BreakCursor` borrows `&'a [ShapedItem]` and therefore cannot be stored
88
/// across passes or compared as a value; this snapshot owns exactly the
89
/// STATE (resume index + hyphenation remainder). The style knobs on the
90
/// cursor (`word_break` / `hyphens` / `line_break`) are deliberately NOT
91
/// part of the token: they derive from style, not from layout progress —
92
/// [`InlineBreakToken::resume`] takes them from the caller, who reads them
93
/// from the same style the original cursor did.
94
#[derive(Debug, Clone, PartialEq)]
95
pub struct InlineBreakToken {
96
    /// Index of the next *full* item to process in the IFC's shaped-item
97
    /// sequence.
98
    pub next_item_index: usize,
99
    /// The remainder of an item split by hyphenation on the boundary line —
100
    /// the very first content of the resumed fragment.
101
    pub partial_remainder: Vec<ShapedItem>,
102
}
103

            
104
impl InlineBreakToken {
105
    /// Snapshot a live cursor's resume state (pure; the cursor is untouched).
106
    #[must_use]
107
2
    pub fn from_cursor(cursor: &BreakCursor<'_>) -> Self {
108
2
        Self {
109
2
            next_item_index: cursor.next_item_index,
110
2
            partial_remainder: cursor.partial_remainder.clone(),
111
2
        }
112
2
    }
113

            
114
    /// Reconstruct a cursor over `items` positioned exactly where the
115
    /// snapshotted one stopped. The style knobs come from the caller (they
116
    /// are style-derived, not layout state — see the type docs).
117
    #[must_use]
118
1
    pub fn resume<'a>(
119
1
        &self,
120
1
        items: &'a [ShapedItem],
121
1
        word_break: WordBreak,
122
1
        hyphens: Hyphens,
123
1
        line_break: LineBreakStrictness,
124
1
    ) -> BreakCursor<'a> {
125
1
        BreakCursor {
126
1
            items,
127
1
            next_item_index: self.next_item_index,
128
1
            partial_remainder: self.partial_remainder.clone(),
129
1
            word_break,
130
1
            hyphens,
131
1
            line_break,
132
1
        }
133
1
    }
134

            
135
    /// True when resuming would start from the very beginning — such a
136
    /// token should not exist (it encodes "no progress"); the page loop's
137
    /// progress guard treats it as a hard stop.
138
    #[must_use]
139
3
    pub const fn is_degenerate_start(&self) -> bool {
140
3
        self.next_item_index == 0 && self.partial_remainder.is_empty()
141
3
    }
142
}
143

            
144
// ---------------------------------------------------------------------------
145
// K30b decision helpers — pure, exhaustively unit-tested; `layout_bfc` only
146
// wires them (design §4.4)
147
// ---------------------------------------------------------------------------
148

            
149
/// Verdict for one normal-flow child against the fragmentainer.
150
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
151
pub enum FitDecision {
152
    /// The child ends within the remaining extent: place it here.
153
    Fits,
154
    /// Break BEFORE this child — it (and its later siblings) resume in the
155
    /// next fragmentainer.
156
    BreakBeforeHere,
157
    /// The child is the first content of this fragmentainer and no
158
    /// fragmentainer can ever hold it: place it OVERFLOWING (css-break
159
    /// monolith rule — never tear, never loop).
160
    MonolithOverflow,
161
}
162

            
163
/// Geometry tolerance for fit checks: sub-1/100-px overshoot is float noise,
164
/// not a page break.
165
const FIT_EPS: f32 = 0.01;
166

            
167
/// Decide whether a child whose border-box starts at `pen` (margins already
168
/// resolved) and spans `child_block_size` fits the remaining extent.
169
///
170
/// `placed_any_content`: whether this fragmentainer already holds content
171
/// from this box — if it does, breaking before the child always makes
172
/// progress; if it does not, breaking only helps when a FRESH fragmentainer
173
/// is actually bigger than what remains here (otherwise the child is a
174
/// monolith and overflows).
175
#[must_use]
176
807
pub fn fragment_fit(
177
807
    pen: f32,
178
807
    child_block_size: f32,
179
807
    remaining_block_extent: f32,
180
807
    next_fragmentainer_extent: f32,
181
807
    placed_any_content: bool,
182
807
) -> FitDecision {
183
807
    if pen + child_block_size <= remaining_block_extent + FIT_EPS {
184
662
        return FitDecision::Fits;
185
145
    }
186
145
    if placed_any_content {
187
131
        return FitDecision::BreakBeforeHere;
188
14
    }
189
    // First content of the fragmentainer overflows on its own. Progress
190
    // guarantee: only defer to the next fragmentainer if it is genuinely
191
    // roomier than what is left here AND can hold the child.
192
14
    if child_block_size <= next_fragmentainer_extent + FIT_EPS
193
1
        && next_fragmentainer_extent > remaining_block_extent + FIT_EPS
194
    {
195
1
        FitDecision::BreakBeforeHere
196
    } else {
197
13
        FitDecision::MonolithOverflow
198
    }
199
807
}
200

            
201
/// Build the outgoing token when a break lands before `breaking_child`:
202
/// the unfinished tail is that child plus every later in-flow sibling, all
203
/// as `BreakBefore` entries (block-granular v1 — `ResumeIn` entries appear
204
/// when nested resume lands, K30b part 2).
205
#[must_use]
206
16
pub fn tail_token(
207
16
    node: usize,
208
16
    consumed_block_size: f32,
209
16
    breaking_child: usize,
210
16
    later_in_flow_siblings: impl Iterator<Item = usize>,
211
16
) -> BreakToken {
212
16
    let mut children = alloc::vec![ChildBreakEntry::BreakBefore {
213
16
        child: breaking_child,
214
16
        forced: false,
215
16
    }];
216
16
    children.extend(
217
16
        later_in_flow_siblings.map(|child| ChildBreakEntry::BreakBefore {
218
26
            child,
219
            forced: false,
220
26
        }),
221
    );
222
16
    BreakToken::Block(BlockBreakToken {
223
16
        node,
224
16
        consumed_block_size,
225
16
        children,
226
16
        generation: 0,
227
16
    })
228
16
}
229

            
230
/// The consumer-side resume plan for a block token: which child is the
231
/// FIRST unfinished one (everything before it is finished and must be
232
/// skipped with zero side effects). `None` for a childless token —
233
/// defensive: such a token encodes no work and resuming from it is a
234
/// no-op, which the page loop's progress guard turns into a stop.
235
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
236
pub struct ResumePlan {
237
    pub first_unfinished: usize,
238
}
239

            
240
#[must_use]
241
362
pub fn resume_plan(token: &BlockBreakToken) -> Option<ResumePlan> {
242
362
    token.children.first().map(|entry| ResumePlan {
243
37
        first_unfinished: match entry {
244
20
            ChildBreakEntry::ResumeIn { child, .. }
245
37
            | ChildBreakEntry::BreakBefore { child, .. } => *child,
246
        },
247
37
    })
248
362
}
249

            
250
// ---------------------------------------------------------------------------
251
// Fingerprints — fast-path rejector for K34 convergence
252
// ---------------------------------------------------------------------------
253

            
254
const FNV_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
255
const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
256

            
257
#[inline]
258
11714
fn fnv(hash: u64, byte: u8) -> u64 {
259
11714
    (hash ^ u64::from(byte)).wrapping_mul(FNV_PRIME)
260
11714
}
261

            
262
#[inline]
263
1367
fn fnv_u64(mut hash: u64, value: u64) -> u64 {
264
10936
    for b in value.to_le_bytes() {
265
10936
        hash = fnv(hash, b);
266
10936
    }
267
1367
    hash
268
1367
}
269

            
270
/// 64-bit structural fingerprint. Law: `a == b ⇒ fingerprint(a) ==
271
/// fingerprint(b)` — guaranteed because it hashes a SUBSET of the fields
272
/// `PartialEq` compares (float geometry inside `partial_remainder` items is
273
/// summarized by count + source indices, never by the tolerant-compared
274
/// floats themselves, so the law survives the rounding tolerance).
275
/// Convergence checks use it to reject fast and MUST confirm with `==`.
276
#[must_use]
277
134
pub fn token_fingerprint(token: &BreakToken) -> u64 {
278
134
    fingerprint_into(FNV_OFFSET, token)
279
134
}
280

            
281
296
fn fingerprint_into(mut h: u64, token: &BreakToken) -> u64 {
282
296
    match token {
283
285
        BreakToken::Block(b) => {
284
285
            h = fnv(h, 0x01);
285
285
            h = fnv_u64(h, b.node as u64);
286
            // consumed_block_size participates in PartialEq as an exact
287
            // f32 compare, so its bits are a valid fingerprint component.
288
285
            h = fnv_u64(h, u64::from(b.consumed_block_size.to_bits()));
289
285
            h = fnv_u64(h, b.children.len() as u64);
290
767
            for entry in &b.children {
291
482
                match entry {
292
162
                    ChildBreakEntry::ResumeIn { child, token } => {
293
162
                        h = fnv(h, 0x02);
294
162
                        h = fnv_u64(h, *child as u64);
295
162
                        h = fingerprint_into(h, token);
296
162
                    }
297
320
                    ChildBreakEntry::BreakBefore { child, .. } => {
298
320
                        h = fnv(h, 0x03);
299
320
                        h = fnv_u64(h, *child as u64);
300
320
                    }
301
                }
302
            }
303
285
            h
304
        }
305
11
        BreakToken::Inline(t) => {
306
11
            h = fnv(h, 0x04);
307
11
            h = fnv_u64(h, t.next_item_index as u64);
308
11
            h = fnv_u64(h, t.partial_remainder.len() as u64);
309
            // Source indices are integer-exact fields of the compared items
310
            // (Rect floats are deliberately excluded — they compare with a
311
            // rounding tolerance, hashing them would break the law).
312
15
            for item in &t.partial_remainder {
313
4
                if let Some(src) = shaped_item_source(item) {
314
4
                    h = fnv_u64(h, u64::from(src.run_index));
315
4
                    h = fnv_u64(h, u64::from(src.item_index));
316
4
                }
317
            }
318
11
            h
319
        }
320
    }
321
296
}
322

            
323
// Returns Option to mirror the other `*_source` accessors and to stay
324
// source-compatible if a future `ShapedItem` variant has no content index.
325
#[allow(clippy::unnecessary_wraps)]
326
4
const fn shaped_item_source(item: &ShapedItem) -> Option<azul_core::selection::ContentIndex> {
327
4
    match item {
328
        ShapedItem::Cluster(c) => Some(c.source_content_index),
329
        ShapedItem::CombinedBlock { source, .. }
330
        | ShapedItem::Object { source, .. }
331
4
        | ShapedItem::Tab { source, .. }
332
4
        | ShapedItem::Break { source, .. } => Some(*source),
333
    }
334
4
}
335

            
336
// ---------------------------------------------------------------------------
337
// Property tests (K30a exit gate; see design doc §6.2)
338
// ---------------------------------------------------------------------------
339

            
340
#[cfg(test)]
341
mod break_token_laws {
342
    use azul_core::selection::ContentIndex;
343

            
344
    use super::*;
345
    use crate::text3::cache::Rect;
346

            
347
11
    fn tab(run: u32, item: u32, w: f32) -> ShapedItem {
348
11
        ShapedItem::Tab {
349
11
            source: ContentIndex {
350
11
                run_index: run,
351
11
                item_index: item,
352
11
            },
353
11
            bounds: Rect {
354
11
                x: 0.0,
355
11
                y: 0.0,
356
11
                width: w,
357
11
                height: 16.0,
358
11
            },
359
11
        }
360
11
    }
361

            
362
12
    fn inline(next: usize, remainder: Vec<ShapedItem>) -> BreakToken {
363
12
        BreakToken::Inline(InlineBreakToken {
364
12
            next_item_index: next,
365
12
            partial_remainder: remainder,
366
12
        })
367
12
    }
368

            
369
6
    fn block(node: usize, consumed: f32, children: Vec<ChildBreakEntry>) -> BreakToken {
370
6
        BreakToken::Block(BlockBreakToken {
371
6
            node,
372
6
            consumed_block_size: consumed,
373
6
            children,
374
6
            generation: 1,
375
6
        })
376
6
    }
377

            
378
    // -- Eq laws ----------------------------------------------------------
379

            
380
    #[test]
381
1
    fn equality_is_structural_and_reflexive() {
382
1
        let t = block(
383
            7,
384
            120.5,
385
1
            vec![
386
1
                ChildBreakEntry::BreakBefore {
387
1
                    child: 3,
388
1
                    forced: false,
389
1
                },
390
1
                ChildBreakEntry::ResumeIn {
391
1
                    child: 2,
392
1
                    token: Box::new(inline(4, vec![tab(0, 9, 12.0)])),
393
1
                },
394
            ],
395
        );
396
1
        assert_eq!(t, t.clone());
397
        // Any structural difference breaks equality: node…
398
1
        let mut o = t.clone();
399
1
        if let BreakToken::Block(b) = &mut o {
400
1
            b.node = 8;
401
1
        }
402
1
        assert_ne!(t, o);
403
        // …consumed size…
404
1
        let mut o = t.clone();
405
1
        if let BreakToken::Block(b) = &mut o {
406
1
            b.consumed_block_size += 0.5;
407
1
        }
408
1
        assert_ne!(t, o);
409
        // …child order (document order is semantic)…
410
1
        let mut o = t.clone();
411
1
        if let BreakToken::Block(b) = &mut o {
412
1
            b.children.reverse();
413
1
        }
414
1
        assert_ne!(t, o);
415
        // …and nested inline state.
416
1
        let mut o = t.clone();
417
1
        if let BreakToken::Block(b) = &mut o {
418
1
            if let ChildBreakEntry::ResumeIn { token, .. } = &mut b.children[1] {
419
1
                **token = inline(5, vec![tab(0, 9, 12.0)]);
420
1
            }
421
        }
422
1
        assert_ne!(t, o);
423
1
    }
424

            
425
    #[test]
426
1
    fn fingerprint_law_equal_tokens_have_equal_fingerprints() {
427
1
        let cases = [
428
1
            inline(0, vec![]),
429
1
            inline(3, vec![tab(1, 2, 8.0)]),
430
1
            block(0, 0.0, vec![]),
431
1
            block(
432
1
                5,
433
1
                33.25,
434
1
                vec![ChildBreakEntry::ResumeIn {
435
1
                    child: 1,
436
1
                    token: Box::new(inline(2, vec![])),
437
1
                }],
438
1
            ),
439
1
        ];
440
5
        for t in &cases {
441
4
            assert_eq!(
442
4
                token_fingerprint(t),
443
4
                token_fingerprint(&t.clone()),
444
                "fingerprint must be a pure function of compared fields: {t:?}"
445
            );
446
        }
447
        // And it actually discriminates the obvious cases (not a constant).
448
1
        assert_ne!(
449
1
            token_fingerprint(&cases[0]),
450
1
            token_fingerprint(&cases[2]),
451
            "inline(0) vs block(0) must not collide on the variant tag"
452
        );
453
1
        assert_ne!(
454
1
            token_fingerprint(&inline(1, vec![])),
455
1
            token_fingerprint(&inline(2, vec![]))
456
        );
457
1
    }
458

            
459
    #[test]
460
1
    fn fingerprint_survives_the_tolerant_rect_compare() {
461
        // text3's Rect PartialEq is rounding-tolerant: two tokens whose
462
        // remainder Rects differ inside the tolerance are EQUAL — the
463
        // fingerprint must agree (law: a == b ⇒ fp(a) == fp(b)). This is
464
        // exactly why Rect floats are excluded from the fingerprint.
465
1
        let a = inline(3, vec![tab(1, 2, 8.0)]);
466
1
        let b = inline(3, vec![tab(1, 2, 8.000001)]);
467
1
        if a == b {
468
1
            assert_eq!(token_fingerprint(&a), token_fingerprint(&b));
469
        } else {
470
            // If the tolerance ever tightens to bit-exact this branch keeps
471
            // the test meaningful instead of vacuous.
472
            assert_ne!(a, b);
473
        }
474
1
    }
475

            
476
    // -- Cursor bridge ----------------------------------------------------
477

            
478
    #[test]
479
1
    fn cursor_snapshot_resume_round_trips() {
480
1
        let items = vec![tab(0, 0, 10.0), tab(0, 1, 10.0), tab(0, 2, 10.0)];
481
1
        let mut cursor = BreakCursor::new(&items);
482
1
        cursor.next_item_index = 2;
483
1
        cursor.partial_remainder = vec![tab(0, 1, 4.0)];
484

            
485
1
        let token = InlineBreakToken::from_cursor(&cursor);
486
1
        let resumed = token.resume(&items, cursor.word_break, cursor.hyphens, cursor.line_break);
487

            
488
1
        assert_eq!(resumed.next_item_index, cursor.next_item_index);
489
1
        assert_eq!(resumed.partial_remainder, cursor.partial_remainder);
490
1
        assert!(!resumed.is_at_start());
491
        // And the snapshot round-trips through the snapshot again.
492
1
        assert_eq!(InlineBreakToken::from_cursor(&resumed), token);
493
1
    }
494

            
495
    #[test]
496
1
    fn degenerate_start_token_is_detected() {
497
1
        assert!(InlineBreakToken {
498
1
            next_item_index: 0,
499
1
            partial_remainder: vec![],
500
1
        }
501
1
        .is_degenerate_start());
502
1
        assert!(!InlineBreakToken {
503
1
            next_item_index: 0,
504
1
            partial_remainder: vec![tab(0, 0, 1.0)],
505
1
        }
506
1
        .is_degenerate_start());
507
1
        assert!(!InlineBreakToken {
508
1
            next_item_index: 1,
509
1
            partial_remainder: vec![],
510
1
        }
511
1
        .is_degenerate_start());
512
1
    }
513

            
514
    // -- K30b decision helpers ---------------------------------------------
515

            
516
    #[test]
517
1
    fn fragment_fit_truth_table() {
518
        use FitDecision::*;
519
        // Fits exactly / with epsilon slack.
520
1
        assert_eq!(fragment_fit(0.0, 100.0, 100.0, 100.0, false), Fits);
521
1
        assert_eq!(fragment_fit(50.0, 50.005, 100.0, 100.0, true), Fits);
522
        // Mid-page overflow with content already placed: break before.
523
1
        assert_eq!(
524
1
            fragment_fit(80.0, 40.0, 100.0, 100.0, true),
525
            BreakBeforeHere
526
        );
527
        // First content, uniform pages, taller than a page: monolith.
528
1
        assert_eq!(
529
1
            fragment_fit(0.0, 250.0, 100.0, 100.0, false),
530
            MonolithOverflow
531
        );
532
        // First content, but the NEXT page is roomier and holds it: defer.
533
1
        assert_eq!(
534
1
            fragment_fit(0.0, 250.0, 100.0, 300.0, false),
535
            BreakBeforeHere
536
        );
537
        // First content, next page roomier but STILL too small: monolith
538
        // (deferring would just move the overflow, not fix it).
539
1
        assert_eq!(
540
1
            fragment_fit(0.0, 400.0, 100.0, 300.0, false),
541
            MonolithOverflow
542
        );
543
        // Progress guarantee: never break-before on first content when the
544
        // next fragmentainer is the same size (would loop forever).
545
1
        assert_eq!(
546
1
            fragment_fit(0.0, 100.02, 100.0, 100.0, false),
547
            MonolithOverflow
548
        );
549
1
    }
550

            
551
    #[test]
552
1
    fn tail_token_lists_the_breaking_child_then_later_siblings_in_order() {
553
1
        let t = tail_token(4, 320.0, 7, [9, 12].into_iter());
554
1
        let BreakToken::Block(b) = &t else {
555
            panic!("block token expected")
556
        };
557
1
        assert_eq!(b.node, 4);
558
1
        assert_eq!(b.consumed_block_size, 320.0);
559
1
        assert_eq!(
560
            b.children,
561
1
            vec![
562
1
                ChildBreakEntry::BreakBefore {
563
1
                    child: 7,
564
1
                    forced: false
565
1
                },
566
1
                ChildBreakEntry::BreakBefore {
567
1
                    child: 9,
568
1
                    forced: false
569
1
                },
570
1
                ChildBreakEntry::BreakBefore {
571
1
                    child: 12,
572
1
                    forced: false
573
1
                },
574
            ]
575
        );
576
        // Consumer side: resume starts exactly at the breaking child.
577
1
        assert_eq!(resume_plan(b).unwrap().first_unfinished, 7);
578
1
    }
579

            
580
    #[test]
581
1
    fn resume_plan_is_none_for_a_childless_token() {
582
1
        let empty = BlockBreakToken {
583
1
            node: 1,
584
1
            consumed_block_size: 0.0,
585
1
            children: vec![],
586
1
            generation: 0,
587
1
        };
588
1
        assert!(resume_plan(&empty).is_none());
589
1
    }
590

            
591
    // -- Progress guard shape ----------------------------------------------
592

            
593
    #[test]
594
1
    fn progress_is_observable_via_equality() {
595
        // The page loop's no-progress guard is `outgoing == incoming`; pin
596
        // that "one more child finished" and "one more item consumed" are
597
        // both visible to it.
598
1
        let before = block(
599
            0,
600
            0.0,
601
1
            vec![
602
1
                ChildBreakEntry::ResumeIn {
603
1
                    child: 1,
604
1
                    token: Box::new(inline(2, vec![])),
605
1
                },
606
1
                ChildBreakEntry::BreakBefore {
607
1
                    child: 2,
608
1
                    forced: false,
609
1
                },
610
            ],
611
        );
612
1
        let after_child_finished = block(
613
            0,
614
            0.0,
615
1
            vec![ChildBreakEntry::ResumeIn {
616
1
                child: 2,
617
1
                token: Box::new(inline(0, vec![tab(0, 0, 1.0)])),
618
1
            }],
619
        );
620
1
        assert_ne!(before, after_child_finished);
621

            
622
1
        let after_items_consumed = block(
623
            0,
624
            0.0,
625
1
            vec![
626
1
                ChildBreakEntry::ResumeIn {
627
1
                    child: 1,
628
1
                    token: Box::new(inline(3, vec![])),
629
1
                },
630
1
                ChildBreakEntry::BreakBefore {
631
1
                    child: 2,
632
1
                    forced: false,
633
1
                },
634
            ],
635
        );
636
1
        assert_ne!(before, after_items_consumed);
637
1
    }
638
}