termproof 0.4.3

Evidence-first verification for TUI and terminal applications: recipe model, schema, steps, assertions, orchestration, terminal sessions and the evidence pipeline
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
//! Compare two verification runs and report where they disagree.
//!
//! Useful whenever two things are supposed to produce the same verdicts and you
//! need that checked rather than assumed:
//!
//! - two implementations behind a switch — "switchable" is a claim until
//!   something compares them;
//! - a release candidate against the last known-good run;
//! - the same recipes before and after a refactor.
//!
//! It works on [`RunResult`] values, so it does not care what produced them.
//! Foreign payloads — output from another implementation, or from an older
//! version of this one — can be read with [`summarize_json`], which tolerates a
//! shape that has drifted rather than refusing it.
//!
//! # Verdict, score, detail
//!
//! Three grades of disagreement, reported separately because they mean
//! different things:
//!
//! | | meaning |
//! |---|---|
//! | [`Divergence::Verdict`] | the two disagree on pass/fail — always a real difference |
//! | [`Divergence::Score`] | same verdict, different partial credit — a pass/fail comparison would hide it |
//! | [`Divergence::Detail`] | same verdict, different wording — usually cosmetic, occasionally a clue |
//!
//! Separating detail from verdict matters when comparing implementations that
//! word their diagnostics independently: hundreds of harmless wording
//! differences would otherwise bury the handful of real ones.
//!
//! # It refuses rather than guesses
//!
//! An empty payload is an error, not an empty divergence list. A comparison
//! that reports "no differences" because it read nothing is worse than no
//! comparison, because it looks like success.
//!
//! ```
//! use termproof::parity::{compare, summarize, Divergence};
//! # use termproof::result::RunResult;
//! # use std::collections::BTreeMap;
//! # fn run(name: &str, passed: bool) -> RunResult {
//! #     RunResult { result_version: Some(1), recipe_name: name.into(), passed, exit_code: None,
//! #         duration_seconds: 0.0, priority: "P0".into(), execution: "scripted".into(),
//! #         renderer: "default".into(), score: if passed { 1.0 } else { 0.0 },
//! #         steps: vec![], assertions: vec![], artifacts: BTreeMap::new() }
//! # }
//! let before = summarize(&[run("login", true)]);
//! let after = summarize(&[run("login", false)]);
//!
//! let divergences = compare(&before, &after, "before", "after")?;
//! assert!(matches!(divergences[0], Divergence::Verdict { .. }));
//! # Ok::<(), termproof::parity::ParityError>(())
//! ```

use std::collections::BTreeMap;
use std::collections::BTreeSet;

use thiserror::Error;

use crate::result::RunResult;

/// One recipe's outcome, reduced to what a comparison needs.
#[derive(Debug, Clone, PartialEq)]
pub struct RecipeOutcome {
    /// Whether the recipe passed overall.
    pub passed: bool,
    /// Score in `0.0..=1.0`.
    pub score: f64,
    /// Assertion name → (passed, detail).
    pub assertions: BTreeMap<String, (bool, String)>,
}

/// A run reduced to the parts a comparison looks at.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct RunSummary {
    /// Recipe key → outcome. Keyed by `recipe_name:renderer` when a renderer is
    /// present, so one recipe run under two renderers stays two entries rather
    /// than one overwriting the other.
    pub recipes: BTreeMap<String, RecipeOutcome>,
}

/// How two runs differ on one recipe.
#[derive(Debug, Clone, PartialEq)]
pub enum Divergence {
    /// One side ran it and the other did not.
    OnlyIn {
        /// Recipe key.
        recipe: String,
        /// Name of the side that ran it.
        side: String,
    },
    /// Both ran it and disagreed on pass/fail.
    Verdict {
        /// Recipe key.
        recipe: String,
        /// Left side's verdict.
        left: bool,
        /// Right side's verdict.
        right: bool,
    },
    /// Same verdict, different score.
    Score {
        /// Recipe key.
        recipe: String,
        /// Left side's score.
        left: f64,
        /// Right side's score.
        right: f64,
    },
    /// Assertions that disagree on pass/fail, or exist on only one side.
    Assertions {
        /// Recipe key.
        recipe: String,
        /// Assertions present on both sides with different verdicts.
        differing: Vec<String>,
        /// Assertions only the left side reported.
        only_left: Vec<String>,
        /// Assertions only the right side reported.
        only_right: Vec<String>,
    },
    /// Assertions that agree on pass/fail but word their detail differently.
    ///
    /// Reported separately from [`Divergence::Assertions`] so a wording change
    /// cannot be mistaken for a behaviour change. See [`compare_detail`].
    Detail {
        /// Recipe key.
        recipe: String,
        /// Assertions whose `detail` differs.
        differing: Vec<String>,
    },
}

/// Why a comparison could not be made at all.
#[derive(Debug, Clone, PartialEq, Error)]
pub enum ParityError {
    /// A side had no recipes, which is never a legitimate run.
    #[error("`{side}` contained no recipes; refusing to report agreement")]
    Empty {
        /// Name of the empty side.
        side: String,
    },
}

/// Whether [`compare`] reports detail-only differences.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DetailMode {
    /// Ignore `detail`; compare verdicts only. The default, because two
    /// implementations that word diagnostics independently produce many
    /// harmless differences.
    #[default]
    Ignore,
    /// Report `detail` differences as [`Divergence::Detail`].
    Compare,
}

/// The key a recipe is compared under.
fn key(result: &RunResult) -> String {
    if result.renderer.is_empty() {
        result.recipe_name.clone()
    } else {
        format!("{}:{}", result.recipe_name, result.renderer)
    }
}

/// Reduce typed results to a comparable summary.
pub fn summarize(results: &[RunResult]) -> RunSummary {
    let mut recipes = BTreeMap::new();
    for r in results {
        recipes.insert(
            key(r),
            RecipeOutcome {
                passed: r.passed,
                score: r.score,
                assertions: r
                    .assertions
                    .iter()
                    .map(|a| (a.name.clone(), (a.passed, a.detail.clone())))
                    .collect(),
            },
        );
    }
    RunSummary { recipes }
}

/// Reduce a foreign JSON payload to a comparable summary.
///
/// Deliberately lenient about shape: a payload from another implementation, or
/// from a version that predates a field, should still be comparable. Anything
/// missing reads as absent rather than as a parse failure — an entry with no
/// recognisable `recipe_name` is skipped, and a run with no recognisable
/// entries yields an empty summary, which [`compare`] then rejects loudly.
///
/// It does **not** gate on [`crate::result::RESULT_SCHEMA_VERSION`], on purpose.
/// Every field it reads — `recipe_name`, `renderer`, `passed`, `score`,
/// `assertions` — is present in version 1 and is what a foreign writer emits
/// anyway, so a version check here would buy nothing and cost the exact failure
/// the version scheme exists to avoid: a comparison refusing to run because the
/// other side is not this build. Readers that need the whole typed payload go
/// through [`crate::result::RunResult::from_json_value`], which does check.
pub fn summarize_json(payload: &serde_json::Value) -> RunSummary {
    let mut recipes = BTreeMap::new();
    let Some(results) = payload.get("results").and_then(|r| r.as_array()) else {
        return RunSummary { recipes };
    };
    for r in results {
        let Some(name) = r.get("recipe_name").and_then(|n| n.as_str()) else {
            continue;
        };
        let renderer = r.get("renderer").and_then(|v| v.as_str()).unwrap_or("");
        let entry_key = if renderer.is_empty() {
            name.to_string()
        } else {
            format!("{name}:{renderer}")
        };
        recipes.insert(
            entry_key,
            RecipeOutcome {
                passed: r.get("passed").and_then(|p| p.as_bool()).unwrap_or(false),
                score: r.get("score").and_then(|s| s.as_f64()).unwrap_or(0.0),
                assertions: json_assertions(r.get("assertions")),
            },
        );
    }
    RunSummary { recipes }
}

/// Read assertions from either shape a payload might use.
///
/// A list of `{name, passed, detail}` is this crate's own format. A bare
/// `{name: bool}` map is what a simpler implementation tends to emit, and
/// dropping those on the floor would make the comparison silently shallower
/// rather than visibly unsupported.
fn json_assertions(value: Option<&serde_json::Value>) -> BTreeMap<String, (bool, String)> {
    let mut out = BTreeMap::new();
    match value {
        Some(serde_json::Value::Array(items)) => {
            for a in items {
                if let Some(name) = a.get("name").and_then(|n| n.as_str()) {
                    out.insert(
                        name.to_string(),
                        (
                            a.get("passed").and_then(|p| p.as_bool()).unwrap_or(false),
                            a.get("detail")
                                .and_then(|d| d.as_str())
                                .unwrap_or_default()
                                .to_string(),
                        ),
                    );
                }
            }
        }
        Some(serde_json::Value::Object(map)) => {
            for (name, v) in map {
                if let Some(passed) = v.as_bool() {
                    out.insert(name.clone(), (passed, String::new()));
                }
            }
        }
        _ => {}
    }
    out
}

/// Every way `left` and `right` disagree, ignoring diagnostic wording.
pub fn compare(
    left: &RunSummary,
    right: &RunSummary,
    left_name: &str,
    right_name: &str,
) -> Result<Vec<Divergence>, ParityError> {
    compare_with(left, right, left_name, right_name, DetailMode::Ignore)
}

/// [`compare`], also reporting assertions whose `detail` differs.
pub fn compare_detail(
    left: &RunSummary,
    right: &RunSummary,
    left_name: &str,
    right_name: &str,
) -> Result<Vec<Divergence>, ParityError> {
    compare_with(left, right, left_name, right_name, DetailMode::Compare)
}

/// Every way `left` and `right` disagree.
///
/// Refuses rather than guesses: an empty payload is an error, not an empty
/// divergence list.
pub fn compare_with(
    left: &RunSummary,
    right: &RunSummary,
    left_name: &str,
    right_name: &str,
    detail: DetailMode,
) -> Result<Vec<Divergence>, ParityError> {
    for (side, run) in [(left_name, left), (right_name, right)] {
        if run.recipes.is_empty() {
            return Err(ParityError::Empty {
                side: side.to_string(),
            });
        }
    }

    let mut out = Vec::new();
    let names: BTreeSet<&String> = left.recipes.keys().chain(right.recipes.keys()).collect();
    for name in names {
        match (left.recipes.get(name), right.recipes.get(name)) {
            (Some(l), Some(r)) => compare_one(name, l, r, detail, &mut out),
            (Some(_), None) => out.push(Divergence::OnlyIn {
                recipe: name.clone(),
                side: left_name.to_string(),
            }),
            (None, Some(_)) => out.push(Divergence::OnlyIn {
                recipe: name.clone(),
                side: right_name.to_string(),
            }),
            (None, None) => unreachable!("name came from one of the two maps"),
        }
    }
    Ok(out)
}

fn compare_one(
    name: &str,
    l: &RecipeOutcome,
    r: &RecipeOutcome,
    detail: DetailMode,
    out: &mut Vec<Divergence>,
) {
    if l.passed != r.passed {
        out.push(Divergence::Verdict {
            recipe: name.to_string(),
            left: l.passed,
            right: r.passed,
        });
    } else if (l.score - r.score).abs() > f64::EPSILON {
        out.push(Divergence::Score {
            recipe: name.to_string(),
            left: l.score,
            right: r.score,
        });
    }

    let lk: BTreeSet<&String> = l.assertions.keys().collect();
    let rk: BTreeSet<&String> = r.assertions.keys().collect();
    let shared: Vec<String> = lk.intersection(&rk).map(|k| (*k).clone()).collect();

    let differing: Vec<String> = shared
        .iter()
        .filter(|k| l.assertions[*k].0 != r.assertions[*k].0)
        .cloned()
        .collect();
    let only_left: Vec<String> = lk.difference(&rk).map(|k| (*k).clone()).collect();
    let only_right: Vec<String> = rk.difference(&lk).map(|k| (*k).clone()).collect();

    if !differing.is_empty() || !only_left.is_empty() || !only_right.is_empty() {
        out.push(Divergence::Assertions {
            recipe: name.to_string(),
            differing,
            only_left,
            only_right,
        });
    }

    if detail == DetailMode::Compare {
        let worded: Vec<String> = shared
            .iter()
            .filter(|k| {
                let (lp, ld) = &l.assertions[*k];
                let (rp, rd) = &r.assertions[*k];
                lp == rp && ld != rd
            })
            .cloned()
            .collect();
        if !worded.is_empty() {
            out.push(Divergence::Detail {
                recipe: name.to_string(),
                differing: worded,
            });
        }
    }
}

/// Render divergences as markdown.
pub fn render(divergences: &[Divergence], left_name: &str, right_name: &str) -> String {
    let mut out = vec!["# Parity report".to_string(), String::new()];
    if divergences.is_empty() {
        out.push(format!(
            "`{left_name}` and `{right_name}` agree on every recipe."
        ));
        out.push(String::new());
        return out.join("\n");
    }
    out.push(format!(
        "**{} divergence(s)** between `{left_name}` and `{right_name}`.",
        divergences.len()
    ));
    out.push(String::new());
    for d in divergences {
        out.push(render_one(d, left_name, right_name));
    }
    out.push(String::new());
    out.join("\n")
}

fn render_one(d: &Divergence, left_name: &str, right_name: &str) -> String {
    match d {
        Divergence::OnlyIn { recipe, side } => format!("- `{recipe}` — ran only in `{side}`"),
        Divergence::Verdict {
            recipe,
            left,
            right,
        } => format!(
            "- `{recipe}` — **verdict differs**: `{left_name}` {}, `{right_name}` {}",
            verdict(*left),
            verdict(*right)
        ),
        Divergence::Score {
            recipe,
            left,
            right,
        } => format!("- `{recipe}` — same verdict, score {left:.2} vs {right:.2}"),
        Divergence::Assertions {
            recipe,
            differing,
            only_left,
            only_right,
        } => {
            let mut s = format!("- `{recipe}` — assertion differences:\n");
            if !differing.is_empty() {
                s.push_str(&format!("  - disagree: {}\n", backticked(differing)));
            }
            if !only_left.is_empty() {
                s.push_str(&format!(
                    "  - only in `{left_name}`: {}\n",
                    backticked(only_left)
                ));
            }
            if !only_right.is_empty() {
                s.push_str(&format!(
                    "  - only in `{right_name}`: {}\n",
                    backticked(only_right)
                ));
            }
            s.trim_end().to_string()
        }
        Divergence::Detail { recipe, differing } => format!(
            "- `{recipe}` — same verdicts, different wording: {}",
            backticked(differing)
        ),
    }
}

fn backticked(names: &[String]) -> String {
    names
        .iter()
        .map(|n| format!("`{n}`"))
        .collect::<Vec<_>>()
        .join(", ")
}

fn verdict(passed: bool) -> &'static str {
    if passed {
        "PASS"
    } else {
        "FAIL"
    }
}

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

    fn assertion(name: &str, passed: bool, detail: &str) -> AssertionResult {
        AssertionResult {
            name: name.to_string(),
            passed,
            detail: detail.to_string(),
        }
    }

    fn run(name: &str, passed: bool, score: f64, assertions: Vec<AssertionResult>) -> RunResult {
        RunResult {
            result_version: Some(crate::result::RESULT_SCHEMA_VERSION),
            recipe_name: name.to_string(),
            passed,
            exit_code: None,
            duration_seconds: 0.0,
            priority: "P0".to_string(),
            execution: "scripted".to_string(),
            renderer: String::new(),
            score,
            steps: vec![],
            assertions,
            artifacts: BTreeMap::new(),
        }
    }

    #[test]
    fn identical_runs_agree() {
        let a = summarize(&[run("login", true, 1.0, vec![assertion("ok", true, "fine")])]);
        assert!(compare(&a, &a, "left", "right").unwrap().is_empty());
    }

    #[test]
    fn an_empty_side_is_an_error_not_agreement() {
        // The failure this exists to prevent: a gate that goes green because it
        // compared nothing looks exactly like a gate that passed.
        let full = summarize(&[run("login", true, 1.0, vec![])]);
        let empty = RunSummary::default();
        assert_eq!(
            compare(&full, &empty, "left", "right"),
            Err(ParityError::Empty {
                side: "right".to_string()
            })
        );
        assert_eq!(
            compare(&empty, &full, "left", "right"),
            Err(ParityError::Empty {
                side: "left".to_string()
            })
        );
    }

    #[test]
    fn a_differing_verdict_is_reported() {
        let l = summarize(&[run("login", true, 1.0, vec![])]);
        let r = summarize(&[run("login", false, 0.0, vec![])]);
        assert_eq!(
            compare(&l, &r, "a", "b").unwrap(),
            vec![Divergence::Verdict {
                recipe: "login".to_string(),
                left: true,
                right: false
            }]
        );
    }

    #[test]
    fn a_score_difference_survives_an_agreeing_verdict() {
        // Both failed, but one got half credit. A pass/fail comparison would
        // call this agreement.
        let l = summarize(&[run("login", false, 0.5, vec![])]);
        let r = summarize(&[run("login", false, 0.0, vec![])]);
        assert!(matches!(
            compare(&l, &r, "a", "b").unwrap()[0],
            Divergence::Score { .. }
        ));
    }

    #[test]
    fn assertion_level_differences_are_named() {
        let l = summarize(&[run(
            "login",
            true,
            1.0,
            vec![assertion("shared", true, ""), assertion("only_l", true, "")],
        )]);
        let r = summarize(&[run(
            "login",
            true,
            1.0,
            vec![
                assertion("shared", false, ""),
                assertion("only_r", true, ""),
            ],
        )]);
        let d = compare(&l, &r, "a", "b").unwrap();
        let found = d
            .iter()
            .find_map(|x| match x {
                Divergence::Assertions {
                    differing,
                    only_left,
                    only_right,
                    ..
                } => Some((differing.clone(), only_left.clone(), only_right.clone())),
                _ => None,
            })
            .expect("assertion divergence");
        assert_eq!(found.0, vec!["shared".to_string()]);
        assert_eq!(found.1, vec!["only_l".to_string()]);
        assert_eq!(found.2, vec!["only_r".to_string()]);
    }

    #[test]
    fn wording_is_ignored_by_default() {
        // Two implementations word diagnostics independently. If every wording
        // difference counted, the real divergences would be buried.
        let l = summarize(&[run(
            "login",
            true,
            1.0,
            vec![assertion("ok", true, "matched at line 3")],
        )]);
        let r = summarize(&[run(
            "login",
            true,
            1.0,
            vec![assertion("ok", true, "found on row 3")],
        )]);
        assert!(compare(&l, &r, "a", "b").unwrap().is_empty());
    }

    #[test]
    fn wording_is_reported_when_asked_for() {
        let l = summarize(&[run("login", true, 1.0, vec![assertion("ok", true, "one")])]);
        let r = summarize(&[run("login", true, 1.0, vec![assertion("ok", true, "two")])]);
        assert_eq!(
            compare_detail(&l, &r, "a", "b").unwrap(),
            vec![Divergence::Detail {
                recipe: "login".to_string(),
                differing: vec!["ok".to_string()]
            }]
        );
    }

    #[test]
    fn one_recipe_under_two_renderers_stays_two_entries() {
        // Keyed by name alone, the second would overwrite the first and a whole
        // renderer's results would vanish without a word.
        let mut a = run("login", true, 1.0, vec![]);
        a.renderer = "alpha".to_string();
        let mut b = run("login", false, 0.0, vec![]);
        b.renderer = "beta".to_string();
        let s = summarize(&[a, b]);
        assert_eq!(s.recipes.len(), 2);
        assert!(s.recipes.contains_key("login:alpha"));
        assert!(s.recipes.contains_key("login:beta"));
    }

    #[test]
    fn a_recipe_only_one_side_ran_is_reported() {
        let l = summarize(&[
            run("login", true, 1.0, vec![]),
            run("logout", true, 1.0, vec![]),
        ]);
        let r = summarize(&[run("login", true, 1.0, vec![])]);
        assert_eq!(
            compare(&l, &r, "a", "b").unwrap(),
            vec![Divergence::OnlyIn {
                recipe: "logout".to_string(),
                side: "a".to_string()
            }]
        );
    }

    #[test]
    fn a_foreign_payload_with_list_assertions_is_readable() {
        let payload = serde_json::json!({
            "results": [{
                "recipe_name": "login", "renderer": "default",
                "passed": true, "score": 1.0,
                "assertions": [{"name": "ok", "passed": true, "detail": "d"}]
            }]
        });
        let s = summarize_json(&payload);
        let o = &s.recipes["login:default"];
        assert!(o.passed);
        assert_eq!(o.assertions["ok"], (true, "d".to_string()));
    }

    #[test]
    fn a_foreign_payload_with_map_assertions_is_readable() {
        // A simpler implementation emits `{name: bool}`. Dropping those would
        // make the comparison silently shallower rather than visibly
        // unsupported.
        let payload = serde_json::json!({
            "results": [{
                "recipe_name": "login",
                "passed": false, "score": 0.5,
                "assertions": {"a": true, "b": false}
            }]
        });
        let s = summarize_json(&payload);
        let o = &s.recipes["login"];
        assert_eq!(o.assertions["a"], (true, String::new()));
        assert_eq!(o.assertions["b"], (false, String::new()));
    }

    #[test]
    fn an_unrecognisable_payload_summarises_to_nothing_and_compare_refuses() {
        // Leniency in the reader must not become leniency in the verdict.
        let s = summarize_json(&serde_json::json!({"unexpected": true}));
        assert!(s.recipes.is_empty());
        let full = summarize(&[run("login", true, 1.0, vec![])]);
        assert!(compare(&full, &s, "a", "b").is_err());
    }

    #[test]
    fn agreement_renders_as_a_sentence_not_an_empty_list() {
        let out = render(&[], "left", "right");
        assert!(out.contains("agree on every recipe"), "{out}");
    }

    #[test]
    fn every_divergence_kind_renders() {
        let all = vec![
            Divergence::OnlyIn {
                recipe: "a".into(),
                side: "left".into(),
            },
            Divergence::Verdict {
                recipe: "b".into(),
                left: true,
                right: false,
            },
            Divergence::Score {
                recipe: "c".into(),
                left: 1.0,
                right: 0.5,
            },
            Divergence::Assertions {
                recipe: "d".into(),
                differing: vec!["x".into()],
                only_left: vec!["y".into()],
                only_right: vec!["z".into()],
            },
            Divergence::Detail {
                recipe: "e".into(),
                differing: vec!["w".into()],
            },
        ];
        let out = render(&all, "left", "right");
        for expected in [
            "ran only in",
            "verdict differs",
            "score",
            "disagree",
            "wording",
        ] {
            assert!(out.contains(expected), "{expected} missing from:\n{out}");
        }
        assert!(out.contains("**5 divergence(s)**"), "{out}");
    }
}