ftui-harness 0.4.0

Test harness and reference fixtures for FrankenTUI.
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
#![forbid(unsafe_code)]

//! Hierarchical Delta Debugging (HDD) for structured inputs.
//!
//! Recursively removes parts of structured input while a failure predicate
//! still holds, producing a minimal failing test case. Applies to widget
//! trees, event sequences, state configurations, or any [`Decomposable`]
//! structure.
//!
//! # Algorithm
//!
//! 1. Try removing children in halves (binary search reduction).
//! 2. If removing a half still triggers the predicate, keep that reduction.
//! 3. Otherwise split into smaller groups and retry.
//! 4. When no more children can be removed at the current level, recurse
//!    into each remaining child.
//!
//! # Example
//!
//! ```rust
//! use ftui_harness::hdd::{Decomposable, hdd_minimize};
//!
//! #[derive(Clone, Debug, PartialEq)]
//! struct Tree {
//!     label: &'static str,
//!     children: Vec<Tree>,
//! }
//!
//! impl Decomposable for Tree {
//!     fn children(&self) -> Vec<Self> {
//!         self.children.clone()
//!     }
//!     fn remove_child(&mut self, idx: usize) {
//!         self.children.remove(idx);
//!     }
//!     fn replace_children(&mut self, new_children: Vec<Self>) {
//!         self.children = new_children;
//!     }
//! }
//!
//! let tree = Tree {
//!     label: "root",
//!     children: vec![
//!         Tree { label: "a", children: vec![] },
//!         Tree { label: "b", children: vec![] },  // ← causes failure
//!         Tree { label: "c", children: vec![] },
//!     ],
//! };
//!
//! // Predicate: fails when tree contains a child labeled "b"
//! let minimal = hdd_minimize(tree, |t| {
//!     t.children.iter().any(|c| c.label == "b")
//! });
//!
//! assert_eq!(minimal.children.len(), 1);
//! assert_eq!(minimal.children[0].label, "b");
//! ```

/// A structure that can be decomposed into children for delta debugging.
///
/// Implementors expose their child elements so the HDD algorithm can
/// try removing subsets to find a minimal failing configuration.
pub trait Decomposable: Clone {
    /// Return a snapshot of the current children.
    fn children(&self) -> Vec<Self>;

    /// Remove the child at position `idx`.
    ///
    /// # Panics
    ///
    /// May panic if `idx` is out of bounds.
    fn remove_child(&mut self, idx: usize);

    /// Replace all children with `new_children`.
    fn replace_children(&mut self, new_children: Vec<Self>);
}

/// Minimize a structured input using Hierarchical Delta Debugging.
///
/// Repeatedly removes subsets of children at each level of the tree
/// while `predicate` still returns `true` (i.e., the failure still
/// reproduces). Returns the smallest structure that still satisfies
/// the predicate.
///
/// The predicate should return `true` when the failure is present
/// (the "interesting" condition holds).
pub fn hdd_minimize<T, F>(input: T, predicate: F) -> T
where
    T: Decomposable,
    F: Fn(&T) -> bool,
{
    assert!(
        predicate(&input),
        "predicate must hold on the original input"
    );
    hdd_minimize_inner(input, &predicate)
}

fn hdd_minimize_inner<T, F>(mut input: T, predicate: &F) -> T
where
    T: Decomposable,
    F: Fn(&T) -> bool,
{
    // Phase 1: minimize the set of children at this level using ddmin.
    input = ddmin_children(input, predicate);

    // Phase 2: recurse into each remaining child.
    let mut children = input.children();
    for i in 0..children.len() {
        let minimized = hdd_minimize_inner(children[i].clone(), predicate);

        // Try replacing this child with its minimized version.
        let original = children[i].clone();
        children[i] = minimized;

        let mut candidate = input.clone();
        candidate.replace_children(children.clone());

        if predicate(&candidate) {
            input = candidate;
        } else {
            // Minimized child broke the predicate; restore original.
            children[i] = original;
        }
    }

    input
}

/// Delta-debugging minimization of children at a single level.
///
/// Implements the ddmin algorithm: try removing halves, then quarters,
/// etc. of the children list while the predicate holds.
fn ddmin_children<T, F>(mut input: T, predicate: &F) -> T
where
    T: Decomposable,
    F: Fn(&T) -> bool,
{
    let mut n = 2usize;

    loop {
        let children = input.children();
        let len = children.len();

        if len == 0 {
            break;
        }

        let chunk_size = len.div_ceil(n);
        let mut reduced = false;

        // Try removing each chunk.
        let mut i = 0;
        while i < n {
            let start = i * chunk_size;
            let end = (start + chunk_size).min(len);
            if start >= len {
                break;
            }

            // Build candidate with chunk [start..end) removed.
            let mut candidate = input.clone();
            let remaining: Vec<T> = children
                .iter()
                .enumerate()
                .filter(|(idx, _)| *idx < start || *idx >= end)
                .map(|(_, c)| c.clone())
                .collect();
            candidate.replace_children(remaining);

            if predicate(&candidate) {
                input = candidate;
                n = 2;
                reduced = true;
                break;
            }
            i += 1;
        }

        if reduced {
            continue;
        }

        // Try keeping each chunk (complement removal).
        // Only useful when there are at least 2 chunks.
        if n > 1 {
            let mut i = 0;
            while i < n {
                let start = i * chunk_size;
                let end = (start + chunk_size).min(len);
                if start >= len {
                    break;
                }

                let kept_len = end - start;
                // Skip if keeping this chunk doesn't reduce the size.
                if kept_len >= len {
                    i += 1;
                    continue;
                }

                // Build candidate keeping only chunk [start..end).
                let mut candidate = input.clone();
                let kept: Vec<T> = children[start..end].to_vec();
                candidate.replace_children(kept);

                if predicate(&candidate) {
                    input = candidate;
                    n = 2;
                    reduced = true;
                    break;
                }
                i += 1;
            }
        }

        if reduced {
            continue;
        }

        // Increase granularity.
        if n >= len {
            break;
        }
        n = (n * 2).min(len);
    }

    input
}

// =========================================================================
// Logged Minimization (proptest post-shrink integration)
// =========================================================================

use std::cell::RefCell;
use std::fmt;

/// A single reduction step recorded during logged minimization.
#[derive(Clone, Debug)]
pub struct ReductionStep {
    /// Step number (0-indexed).
    pub step: usize,
    /// What phase produced this step.
    pub phase: ReductionPhase,
    /// Number of children before this step.
    pub children_before: usize,
    /// Number of children after this step.
    pub children_after: usize,
    /// Whether the predicate held (and the reduction was accepted).
    pub accepted: bool,
}

/// The phase of HDD that produced a reduction step.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ReductionPhase {
    /// Removing a chunk of children (ddmin).
    ChunkRemoval,
    /// Keeping only a subset of children (ddmin complement).
    ChunkRetention,
    /// Recursive minimization of a child subtree.
    ChildRecursion,
}

impl fmt::Display for ReductionPhase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ChunkRemoval => write!(f, "chunk_removal"),
            Self::ChunkRetention => write!(f, "chunk_retention"),
            Self::ChildRecursion => write!(f, "child_recursion"),
        }
    }
}

/// Result of a logged HDD minimization.
#[derive(Clone, Debug)]
pub struct MinimizationResult<T> {
    /// The minimized input.
    pub minimized: T,
    /// Log of all reduction steps attempted.
    pub steps: Vec<ReductionStep>,
    /// Total predicate evaluations.
    pub predicate_calls: usize,
}

impl<T> MinimizationResult<T> {
    /// Serialize the reduction log as JSONL (one JSON object per line).
    pub fn steps_to_jsonl(&self) -> String {
        let mut out = String::new();
        for step in &self.steps {
            if !out.is_empty() {
                out.push('\n');
            }
            out.push_str(&format!(
                "{{\"step\":{},\"phase\":\"{}\",\"children_before\":{},\"children_after\":{},\"accepted\":{}}}",
                step.step, step.phase, step.children_before, step.children_after, step.accepted
            ));
        }
        out
    }
}

/// Minimize a structured input using HDD, logging every reduction step.
///
/// This is the proptest post-shrink integration point. After proptest finds
/// a counterexample and performs its own shrinking, pass the result to this
/// function for further structural minimization with a full audit trail.
///
/// Returns a [`MinimizationResult`] containing the minimized input, a log
/// of all reduction steps, and the total number of predicate evaluations.
pub fn hdd_minimize_logged<T, F>(input: T, predicate: F) -> MinimizationResult<T>
where
    T: Decomposable,
    F: Fn(&T) -> bool,
{
    let log = RefCell::new(Vec::new());
    let call_count = RefCell::new(0usize);

    let logging_predicate = |t: &T| -> bool {
        *call_count.borrow_mut() += 1;
        predicate(t)
    };

    assert!(
        logging_predicate(&input),
        "predicate must hold on the original input"
    );

    let minimized = hdd_logged_inner(input, &logging_predicate, &log);

    MinimizationResult {
        minimized,
        steps: log.into_inner(),
        predicate_calls: call_count.into_inner(),
    }
}

fn hdd_logged_inner<T, F>(mut input: T, predicate: &F, log: &RefCell<Vec<ReductionStep>>) -> T
where
    T: Decomposable,
    F: Fn(&T) -> bool,
{
    // Phase 1: ddmin with logging.
    input = ddmin_children_logged(input, predicate, log);

    // Phase 2: recurse into each remaining child.
    let mut children = input.children();
    for i in 0..children.len() {
        let before_count = count_children_recursive(&children[i]);
        let minimized = hdd_logged_inner(children[i].clone(), predicate, log);
        let after_count = count_children_recursive(&minimized);

        let original = children[i].clone();
        children[i] = minimized;

        let mut candidate = input.clone();
        candidate.replace_children(children.clone());

        let accepted = predicate(&candidate);

        let step_num = log.borrow().len();
        log.borrow_mut().push(ReductionStep {
            step: step_num,
            phase: ReductionPhase::ChildRecursion,
            children_before: before_count,
            children_after: if accepted { after_count } else { before_count },
            accepted,
        });

        if accepted {
            input = candidate;
        } else {
            children[i] = original;
        }
    }

    input
}

fn ddmin_children_logged<T, F>(mut input: T, predicate: &F, log: &RefCell<Vec<ReductionStep>>) -> T
where
    T: Decomposable,
    F: Fn(&T) -> bool,
{
    let mut n = 2usize;

    loop {
        let children = input.children();
        let len = children.len();

        if len == 0 {
            break;
        }

        let chunk_size = len.div_ceil(n);
        let mut reduced = false;

        // Try removing each chunk.
        let mut i = 0;
        while i < n {
            let start = i * chunk_size;
            let end = (start + chunk_size).min(len);
            if start >= len {
                break;
            }

            let mut candidate = input.clone();
            let remaining: Vec<T> = children
                .iter()
                .enumerate()
                .filter(|(idx, _)| *idx < start || *idx >= end)
                .map(|(_, c)| c.clone())
                .collect();
            let new_len = remaining.len();
            candidate.replace_children(remaining);

            let accepted = predicate(&candidate);

            let step_num = log.borrow().len();
            log.borrow_mut().push(ReductionStep {
                step: step_num,
                phase: ReductionPhase::ChunkRemoval,
                children_before: len,
                children_after: if accepted { new_len } else { len },
                accepted,
            });

            if accepted {
                input = candidate;
                n = 2;
                reduced = true;
                break;
            }
            i += 1;
        }

        if reduced {
            continue;
        }

        // Try keeping each chunk.
        if n > 1 {
            let mut i = 0;
            while i < n {
                let start = i * chunk_size;
                let end = (start + chunk_size).min(len);
                if start >= len {
                    break;
                }

                let kept_len = end - start;
                if kept_len >= len {
                    i += 1;
                    continue;
                }

                let mut candidate = input.clone();
                let kept: Vec<T> = children[start..end].to_vec();
                candidate.replace_children(kept);

                let accepted = predicate(&candidate);

                let step_num = log.borrow().len();
                log.borrow_mut().push(ReductionStep {
                    step: step_num,
                    phase: ReductionPhase::ChunkRetention,
                    children_before: len,
                    children_after: if accepted { kept_len } else { len },
                    accepted,
                });

                if accepted {
                    input = candidate;
                    n = 2;
                    reduced = true;
                    break;
                }
                i += 1;
            }
        }

        if reduced {
            continue;
        }

        if n >= len {
            break;
        }
        n = (n * 2).min(len);
    }

    input
}

/// Count total children recursively (used for step logging).
fn count_children_recursive<T: Decomposable>(node: &T) -> usize {
    let children = node.children();
    children.len() + children.iter().map(count_children_recursive).sum::<usize>()
}

// =========================================================================
// Tests
// =========================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Clone, Debug, PartialEq)]
    struct Tree {
        label: String,
        children: Vec<Tree>,
    }

    impl Tree {
        fn leaf(label: &str) -> Self {
            Self {
                label: label.to_string(),
                children: vec![],
            }
        }

        fn node(label: &str, children: Vec<Tree>) -> Self {
            Self {
                label: label.to_string(),
                children,
            }
        }
    }

    impl Decomposable for Tree {
        fn children(&self) -> Vec<Self> {
            self.children.clone()
        }

        fn remove_child(&mut self, idx: usize) {
            self.children.remove(idx);
        }

        fn replace_children(&mut self, new_children: Vec<Self>) {
            self.children = new_children;
        }
    }

    // Vec<T> is also decomposable (for event sequences).
    // A Vec with 0 or 1 elements is a leaf (no further decomposition).
    impl<T: Clone> Decomposable for Vec<T> {
        fn children(&self) -> Vec<Self> {
            if self.len() <= 1 {
                return vec![];
            }
            self.iter().map(|item| vec![item.clone()]).collect()
        }

        fn remove_child(&mut self, idx: usize) {
            self.remove(idx);
        }

        fn replace_children(&mut self, new_children: Vec<Self>) {
            *self = new_children.into_iter().flatten().collect();
        }
    }

    #[test]
    fn single_child_preserved() {
        let tree = Tree::node("root", vec![Tree::leaf("only")]);
        let result = hdd_minimize(tree, |t| t.children.iter().any(|c| c.label == "only"));
        assert_eq!(result.children.len(), 1);
        assert_eq!(result.children[0].label, "only");
    }

    #[test]
    fn removes_irrelevant_children() {
        let tree = Tree::node(
            "root",
            vec![
                Tree::leaf("a"),
                Tree::leaf("b"),
                Tree::leaf("trigger"),
                Tree::leaf("c"),
                Tree::leaf("d"),
            ],
        );

        let result = hdd_minimize(tree, |t| t.children.iter().any(|c| c.label == "trigger"));

        assert_eq!(result.children.len(), 1);
        assert_eq!(result.children[0].label, "trigger");
    }

    #[test]
    fn preserves_two_required_children() {
        let tree = Tree::node(
            "root",
            vec![
                Tree::leaf("a"),
                Tree::leaf("needed1"),
                Tree::leaf("b"),
                Tree::leaf("needed2"),
                Tree::leaf("c"),
            ],
        );

        let result = hdd_minimize(tree, |t| {
            let labels: Vec<&str> = t.children.iter().map(|c| c.label.as_str()).collect();
            labels.contains(&"needed1") && labels.contains(&"needed2")
        });

        assert_eq!(result.children.len(), 2);
        let labels: Vec<&str> = result.children.iter().map(|c| c.label.as_str()).collect();
        assert!(labels.contains(&"needed1"));
        assert!(labels.contains(&"needed2"));
    }

    #[test]
    fn recurses_into_children() {
        let tree = Tree::node(
            "root",
            vec![Tree::node(
                "parent",
                vec![Tree::leaf("x"), Tree::leaf("culprit"), Tree::leaf("y")],
            )],
        );

        let result = hdd_minimize(tree, |t| {
            fn has_culprit(t: &Tree) -> bool {
                if t.label == "culprit" {
                    return true;
                }
                t.children.iter().any(has_culprit)
            }
            has_culprit(t)
        });

        // Root should have 1 child ("parent"), which has 1 child ("culprit").
        assert_eq!(result.children.len(), 1);
        assert_eq!(result.children[0].label, "parent");
        assert_eq!(result.children[0].children.len(), 1);
        assert_eq!(result.children[0].children[0].label, "culprit");
    }

    #[test]
    fn empty_children_is_fixpoint() {
        let tree = Tree::leaf("root");
        let result = hdd_minimize(tree.clone(), |_| true);
        assert_eq!(result, tree);
    }

    #[test]
    fn event_sequence_minimization() {
        let events: Vec<i32> = vec![1, 2, 3, 4, 5, 6, 7, 8];

        // Predicate: sequence must contain 3 and 7.
        let result = hdd_minimize(events, |seq| seq.contains(&3) && seq.contains(&7));

        assert!(result.contains(&3));
        assert!(result.contains(&7));
        assert!(result.len() <= 3); // Should be close to minimal.
    }

    #[test]
    fn deep_nested_minimization() {
        let tree = Tree::node(
            "root",
            vec![
                Tree::node(
                    "branch1",
                    vec![
                        Tree::leaf("noise1"),
                        Tree::node(
                            "sub",
                            vec![Tree::leaf("noise2"), Tree::leaf("deep_trigger")],
                        ),
                    ],
                ),
                Tree::leaf("noise3"),
            ],
        );

        let result = hdd_minimize(tree, |t| {
            fn find_label(t: &Tree, label: &str) -> bool {
                if t.label == label {
                    return true;
                }
                t.children.iter().any(|c| find_label(c, label))
            }
            find_label(t, "deep_trigger")
        });

        // Should drill down to the minimal path.
        fn find_label(t: &Tree, label: &str) -> bool {
            if t.label == label {
                return true;
            }
            t.children.iter().any(|c| find_label(c, label))
        }
        assert!(find_label(&result, "deep_trigger"));

        // Count total nodes — should be much less than original 7.
        fn count_nodes(t: &Tree) -> usize {
            1 + t.children.iter().map(count_nodes).sum::<usize>()
        }
        assert!(count_nodes(&result) <= 4);
    }

    #[test]
    #[should_panic(expected = "predicate must hold")]
    fn panics_if_predicate_fails_on_input() {
        let tree = Tree::leaf("root");
        hdd_minimize(tree, |_| false);
    }

    #[test]
    fn all_children_needed() {
        let tree = Tree::node(
            "root",
            vec![Tree::leaf("a"), Tree::leaf("b"), Tree::leaf("c")],
        );

        let result = hdd_minimize(tree.clone(), |t| t.children.len() == 3);

        assert_eq!(result.children.len(), 3);
    }

    #[test]
    fn large_input_binary_search_efficiency() {
        // With 100 children, ddmin should not need O(n) predicate calls.
        let children: Vec<Tree> = (0..100).map(|i| Tree::leaf(&format!("n{i}"))).collect();
        let tree = Tree::node("root", children);

        let call_count = std::cell::Cell::new(0u64);
        let result = hdd_minimize(tree, |t| {
            call_count.set(call_count.get() + 1);
            t.children.iter().any(|c| c.label == "n42")
        });

        assert_eq!(result.children.len(), 1);
        assert_eq!(result.children[0].label, "n42");

        // ddmin on n=100 with 1 target should need O(log n) calls, not O(n).
        // Generous bound: should be well under 100.
        assert!(
            call_count.get() < 50,
            "too many predicate calls: {}",
            call_count.get()
        );
    }

    // =====================================================================
    // Logged minimization tests
    // =====================================================================

    #[test]
    fn logged_minimization_produces_steps() {
        let tree = Tree::node(
            "root",
            vec![
                Tree::leaf("a"),
                Tree::leaf("trigger"),
                Tree::leaf("b"),
                Tree::leaf("c"),
            ],
        );

        let result = hdd_minimize_logged(tree, |t| t.children.iter().any(|c| c.label == "trigger"));

        assert_eq!(result.minimized.children.len(), 1);
        assert_eq!(result.minimized.children[0].label, "trigger");
        assert!(!result.steps.is_empty(), "should have logged steps");
        assert!(result.predicate_calls > 0);
    }

    #[test]
    fn logged_steps_contain_accepted_reductions() {
        let tree = Tree::node(
            "root",
            vec![
                Tree::leaf("a"),
                Tree::leaf("b"),
                Tree::leaf("trigger"),
                Tree::leaf("c"),
                Tree::leaf("d"),
            ],
        );

        let result = hdd_minimize_logged(tree, |t| t.children.iter().any(|c| c.label == "trigger"));

        // At least one step should have been accepted.
        let accepted_count = result.steps.iter().filter(|s| s.accepted).count();
        assert!(
            accepted_count > 0,
            "at least one reduction must be accepted"
        );

        // Accepted steps should show actual reduction.
        for step in result.steps.iter().filter(|s| s.accepted) {
            assert!(
                step.children_after <= step.children_before,
                "accepted step should not increase children"
            );
        }
    }

    #[test]
    fn jsonl_output_is_valid() {
        let tree = Tree::node(
            "root",
            vec![Tree::leaf("a"), Tree::leaf("trigger"), Tree::leaf("b")],
        );

        let result = hdd_minimize_logged(tree, |t| t.children.iter().any(|c| c.label == "trigger"));

        let jsonl = result.steps_to_jsonl();
        assert!(!jsonl.is_empty());

        // Each line should be valid JSON.
        for line in jsonl.lines() {
            let parsed: serde_json::Value =
                serde_json::from_str(line).expect("each JSONL line must be valid JSON");
            assert!(parsed.get("step").is_some());
            assert!(parsed.get("phase").is_some());
            assert!(parsed.get("accepted").is_some());
        }
    }

    #[test]
    fn logged_predicate_count_matches() {
        let tree = Tree::node(
            "root",
            vec![Tree::leaf("a"), Tree::leaf("trigger"), Tree::leaf("b")],
        );

        let manual_count = std::cell::Cell::new(0u64);
        let result = hdd_minimize_logged(tree, |t| {
            manual_count.set(manual_count.get() + 1);
            t.children.iter().any(|c| c.label == "trigger")
        });

        // The logged predicate_calls should match our manual count.
        assert_eq!(result.predicate_calls, manual_count.get() as usize);
    }
}