opencrabs 0.3.67

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
//! Tests for the ADR 0005 flow-message structure: the always-visible plan
//! chrome (title / checklist / goal), the merged footer (status → log summary →
//! ctx → clock), and the uncollapsed shell — the whole message is never wrapped
//! in one outer expandable; only the processing log collapses.

use crate::channels::telegram::flow::{
    FlowHeader, FlowLine, render_flow_details_chrome, render_flow_details_chrome_pref,
    render_flow_html_chrome, render_flow_html_chrome_pref,
};
use crate::channels::telegram::flow_chrome::{
    FlowSections, GoalSection, ProseSection, clock_glyph, split_plan_prose,
};

fn sections(title: Option<&str>, checklist: Option<Vec<&str>>, goal: Option<&str>) -> FlowSections {
    FlowSections {
        plan_state: None,
        plan_kb: Default::default(),
        plan_title: title.map(str::to_string),
        prose: None,
        checklist: checklist.map(|rows| rows.into_iter().map(str::to_string).collect()),
        goal: goal.map(|text| GoalSection {
            text: text.to_string(),
            completed: false,
        }),
        ctx: None,
    }
}

fn prose(heading: Option<&str>, body: &str) -> ProseSection {
    ProseSection {
        heading: heading.map(str::to_string),
        body: body.to_string(),
    }
}

fn tline(label: &str, context: &str) -> FlowLine {
    FlowLine::Tool {
        label: label.to_string(),
        context: context.to_string(),
        raw_context: String::new(),
    }
}

// ── clock glyph (Decision 13) ──

#[test]
fn clock_glyph_formats_minutes_and_hours() {
    assert_eq!(clock_glyph(0), "⏱ 0:00");
    assert_eq!(clock_glyph(9), "⏱ 0:09");
    assert_eq!(clock_glyph(83), "⏱ 1:23");
    assert_eq!(clock_glyph(3665), "⏱ 1:01:05");
}

// ── chrome assembly: title / prose / checklist / goal (Decision 3 / 12 / 13) ──
// plan_state and ctx moved to the merged footer (Decision 7 / Decision 12), so
// they must NOT appear in the chrome.

#[test]
fn chrome_classic_order_title_checklist_rows_goal_and_omit_state_and_ctx() {
    let mut s = sections(
        Some("Ship plan mode"),
        Some(vec!["☑ scope it", "☐ build it"]),
        Some("close B"),
    );
    s.plan_state = Some("✍️ Editing plan".to_string());
    s.ctx = Some("ctx 12.3k/200k".to_string());
    let out = s.chrome_classic(false);
    // Blank line between checklist and goal (the classic stand-in for the
    // rich <hr> — Decision 13); no blank line between title and checklist.
    // The goal is its own expandable with the bold Decision 10 prefix.
    assert_eq!(
        out,
        "📋 <b>Ship plan mode</b>\n☑ scope it\n☐ build it\n\n\
         <blockquote expandable><b>🎯 Goal:</b> close B</blockquote>"
    );
    assert!(
        !out.contains("Editing plan"),
        "plan_state stays in the footer"
    );
    assert!(!out.contains("ctx"), "ctx stays in the footer");
}

#[test]
fn chrome_empty_when_all_plan_sections_empty() {
    let mut s = sections(None, None, None);
    // plan_state / ctx set but no title/prose/checklist/goal → no chrome.
    s.plan_state = Some("✍️ Editing plan".to_string());
    s.ctx = Some("ctx 1k/200k".to_string());
    assert!(s.chrome_classic(false).is_empty());
    assert!(s.chrome_rich(false).is_empty());
}

#[test]
fn chrome_escapes_html_in_section_text() {
    let s = sections(Some("a <b> & c"), Some(vec!["☐ x < y"]), None);
    for out in [s.chrome_classic(false), s.chrome_rich(false)] {
        assert!(out.contains("a &lt;b&gt; &amp; c"), "title escaped: {out}");
        assert!(out.contains("☐ x &lt; y"), "checklist row escaped: {out}");
        assert!(!out.contains("a <b> & c"));
    }
}

// ── per-heading prose split (Decision 12) ──

#[test]
fn split_plan_prose_strips_h1_and_cuts_on_top_level_headings() {
    let md = "# The Plan\n\nintro paragraph\n\n## Context\nwhy we do it\n\n## Steps\n1. first\n2. second\n";
    let secs = split_plan_prose(md);
    assert_eq!(
        secs,
        vec![
            prose(None, "intro paragraph"),
            prose(Some("Context"), "why we do it"),
            prose(Some("Steps"), "1. first\n2. second"),
        ]
    );
}

#[test]
fn split_plan_prose_keeps_nested_headings_in_the_body() {
    let secs = split_plan_prose("## Design\n### Option A\ntext a\n### Option B\ntext b");
    assert_eq!(
        secs,
        vec![prose(
            Some("Design"),
            "### Option A\ntext a\n### Option B\ntext b"
        )]
    );
}

#[test]
fn split_plan_prose_ignores_headings_inside_code_fences() {
    let md = "## Setup\n```\n## not a heading\n# neither\n```\ndone";
    let secs = split_plan_prose(md);
    assert_eq!(secs.len(), 1);
    assert_eq!(secs[0].heading.as_deref(), Some("Setup"));
    assert!(secs[0].body.contains("## not a heading"));
}

#[test]
fn split_plan_prose_drops_empty_sections_and_empty_input() {
    assert!(split_plan_prose("").is_empty());
    assert!(split_plan_prose("# Title Only\n\n").is_empty());
    // A heading with nothing under it has nothing to disclose.
    assert_eq!(
        split_plan_prose("## Empty\n\n## Real\ncontent"),
        vec![prose(Some("Real"), "content")]
    );
}

// ── prose rendering: rich details per heading / classic expandables ──

#[test]
fn rich_prose_renders_one_details_per_heading_flush_after_title() {
    let mut s = sections(Some("The Plan"), None, None);
    s.prose = Some(vec![
        prose(None, "intro line"),
        prose(Some("Context"), "why\n\nand how"),
    ]);
    let out = s.chrome_rich(false);
    // Title flush against prose (no spacer), orphan preamble always visible,
    // heading as inline summary, blank body lines dropped on rich.
    assert_eq!(
        out,
        "<p>📋 <b>The Plan</b></p><p>intro line</p>\
         <details><summary>Context</summary><p>why</p><p>and how</p></details>"
    );
}

#[test]
fn rich_prose_then_checklist_then_goal_use_hr_boundaries() {
    let mut s = sections(Some("P"), Some(vec!["☐ a"]), Some("g"));
    s.prose = Some(vec![prose(Some("Ctx"), "body")]);
    let out = s.chrome_rich(false);
    assert_eq!(
        out,
        "<p>📋 <b>P</b></p><details><summary>Ctx</summary><p>body</p></details>\
         <hr><p>☐ a</p><hr><p><b>🎯 Goal:</b> g</p>"
    );
}

#[test]
fn rich_title_and_checklist_without_prose_have_no_hr_between_them() {
    let s = sections(Some("P"), Some(vec!["☐ a", "☐ b"]), None);
    assert_eq!(
        s.chrome_rich(false),
        "<p>📋 <b>P</b></p><p>☐ a</p><p>☐ b</p>"
    );
}

#[test]
fn classic_prose_puts_bold_heading_inside_the_expandable() {
    let mut s = sections(Some("The Plan"), None, None);
    s.prose = Some(vec![prose(Some("Context"), "why we do it")]);
    let out = s.chrome_classic(false);
    // The bold heading is the FIRST line inside the blockquote so the
    // collapsed peek shows it (Decision 12) — never a visible line above it.
    assert_eq!(
        out,
        "📋 <b>The Plan</b>\n<blockquote expandable><b>Context</b>\nwhy we do it</blockquote>"
    );
}

#[test]
fn classic_prose_keeps_blank_lines_and_formats_markdown_body() {
    let mut s = sections(None, None, None);
    s.prose = Some(vec![prose(
        Some("Steps"),
        "### Sub\n- item **bold**\n\nplain `code`",
    )]);
    let out = s.chrome_classic(false);
    assert!(out.contains("<b>Sub</b>"), "nested heading bolded: {out}");
    assert!(out.contains("• item <b>bold</b>"), "list bulleted: {out}");
    assert!(out.contains("\n\n"), "paragraph break kept on classic");
    assert!(
        out.contains("plain <code>code</code>"),
        "inline code: {out}"
    );
}

#[test]
fn prose_body_escapes_html_and_keeps_fenced_code_as_code_lines() {
    let mut s = sections(None, None, None);
    s.prose = Some(vec![prose(
        Some("Danger"),
        "a <b> tag\n```\nlet x = 1;\n```",
    )]);
    let out = s.chrome_rich(false);
    assert!(out.contains("a &lt;b&gt; tag"), "body escaped: {out}");
    assert!(
        out.contains("<code>let x = 1;</code>"),
        "fence as code: {out}"
    );
    assert!(!out.contains("```"), "fence markers stripped: {out}");
}

// ── goal chrome: Decision 10 prefixes + Decision 12 collapse ──

#[test]
fn rich_multi_paragraph_goal_collapses_with_first_paragraph_summary() {
    let s = sections(
        None,
        None,
        Some("ship the release\n\nthen tag it\n\nthen announce"),
    );
    let out = s.chrome_rich(false);
    assert_eq!(
        out,
        "<details><summary><b>🎯 Goal:</b> ship the release</summary>\
         <p>then tag it</p><p>then announce</p></details>"
    );
}

#[test]
fn rich_one_paragraph_goal_stays_plain_always_visible() {
    let s = sections(None, None, Some("ship the release"));
    let out = s.chrome_rich(false);
    assert_eq!(out, "<p><b>🎯 Goal:</b> ship the release</p>");
    assert!(!out.contains("<details"), "one paragraph never collapses");
}

#[test]
fn completed_goal_keeps_target_icon_live_and_swaps_to_check_at_settle() {
    let mut s = sections(None, None, Some("close the audit"));
    s.goal.as_mut().expect("goal set").completed = true;
    // While the turn is still running a completed goal keeps 🎯 (Decision 10).
    assert_eq!(
        s.chrome_rich(false),
        "<p><b>🎯 Goal:</b> close the audit</p>"
    );
    // At settle only the icon swaps; the Goal: word never changes.
    assert_eq!(
        s.chrome_rich(true),
        "<p><b>✅ Goal:</b> close the audit</p>"
    );
    assert_eq!(
        s.chrome_classic(true),
        "<blockquote expandable><b>✅ Goal:</b> close the audit</blockquote>"
    );
}

#[test]
fn active_goal_never_shows_check_even_at_settle() {
    // Settle with the goal still active → 🎯 (Decision 10 rule 5).
    let s = sections(None, None, Some("still going"));
    assert_eq!(s.chrome_rich(true), "<p><b>🎯 Goal:</b> still going</p>");
}

// ── plan-state copy (Decision 7): Editing chrome carries no slash hints ──

#[tokio::test]
async fn plan_state_editing_copy_locked_to_decision_7() {
    use crate::channels::telegram::flow_chrome::load_plan_state_section;
    use crate::config::profile::{home_for_profile, with_profile_home_async};
    use crate::utils::plan_files::{create_design_md, save_plan, set_pre_init_editing};
    use uuid::Uuid;

    let profile = format!("flow-chrome-test-{}", Uuid::new_v4());
    with_profile_home_async(Some(&profile), async {
        let sid = Uuid::new_v4();
        // NoPlan → no state line.
        assert_eq!(load_plan_state_section(sid, true).await.0, None);

        // Pre-init Editing: 📝 Discussing plan, no keyboard, no hints.
        set_pre_init_editing(sid).await.unwrap();
        let (state, _) = load_plan_state_section(sid, true).await;
        assert_eq!(state.as_deref(), Some("📝 Discussing plan"));

        // Post-init Editing: ✍️ Editing plan only — the design prose reads on
        // the flow message and Approve rides the keyboard, so the chrome never
        // teaches /show-plan or /execute (Decision 14).
        let plan = crate::tui::plan::PlanDocument::new(sid, "T".to_string(), String::new());
        save_plan(&plan).await.unwrap();
        create_design_md(sid, "T").await.unwrap();
        let (state, _) = load_plan_state_section(sid, true).await;
        assert_eq!(state.as_deref(), Some("✍️ Editing plan"));
        let state = state.unwrap();
        assert!(!state.contains("/show-plan"), "no view hint: {state}");
        assert!(!state.contains("/execute"), "no approve hint: {state}");
    })
    .await;
    let _ = std::fs::remove_dir_all(home_for_profile(Some(&profile)));
}

// ── header-only renders (empty flow_entries): plain merged footer ──

#[test]
fn header_only_html_is_plain_footer_line() {
    // Pre-tool phase, non-plan: no log, so a plain footer line with the
    // Working-on status and the clock — no blockquote, no <sub> on classic.
    let out = render_flow_html_chrome(
        &[],
        &FlowHeader::Live(Some("10s")),
        Some("Working on: fix the tests"),
        &FlowSections::default(),
        10,
    );
    assert_eq!(out, "Working on: fix the tests • ⏱ 0:10");
    assert!(!out.contains("<blockquote"), "no outer expandable");
    assert!(
        !out.contains("<details"),
        "no log details before first entry"
    );
}

#[test]
fn header_only_html_leads_with_chrome_then_footer() {
    let out = render_flow_html_chrome(
        &[],
        &FlowHeader::Live(None),
        None,
        &sections(Some("Ship it"), Some(vec!["☐ wire it", "☐ test it"]), None),
        0,
    );
    // Chrome leads (always visible): title then one line per checklist task; a
    // blank line separates it from the plain footer clock.
    assert_eq!(out, "📋 <b>Ship it</b>\n☐ wire it\n☐ test it\n\n⏱ 0:00");
}

#[test]
fn header_only_settled_no_tool_turn_puts_ctx_before_clock() {
    // Settled no-tool turn: footer = outcome → ctx → clock, ctx BEFORE the clock.
    let secs = FlowSections {
        ctx: Some("ctx 9.1k/200k".to_string()),
        ..Default::default()
    };
    let out = render_flow_html_chrome(
        &[],
        &FlowHeader::Settled {
            icon: "",
            verb: "Finished",
            duration: "3s",
        },
        None,
        &secs,
        3,
    );
    assert_eq!(out, "✅ Finished • ctx 9.1k/200k • ⏱ 0:03");
}

#[test]
fn header_only_details_is_plain_sub_footer_line() {
    let out = render_flow_details_chrome(
        &[],
        &FlowHeader::Live(Some("5s")),
        Some("🧠 reading the diff"),
        &FlowSections::default(),
        5,
    );
    assert_eq!(out, "<sub>🧠 reading the diff • ⏱ 0:05</sub>");
    assert!(
        !out.contains("<details>"),
        "no log details before first entry"
    );
}

// ── populated flows: uncollapsed shell, log in its own block, footer last ──

#[test]
fn html_populated_flow_has_no_outer_expandable() {
    let lines = [
        tline("✅ bash", "git status"),
        tline("⚙️ read_file", "a.rs"),
    ];
    let out = render_flow_html_chrome(
        &lines,
        &FlowHeader::Live(Some("20s")),
        None,
        &sections(
            Some("Plan"),
            Some(vec!["☑ first", "☐ second", "☐ third", "☐ fourth"]),
            None,
        ),
        20,
    );
    // Chrome leads and is always visible (title then full ☐/☑ list), not inside
    // any expandable.
    assert!(out.starts_with("📋 <b>Plan</b>\n☑ first\n☐ second\n☐ third\n☐ fourth\n\n"));
    assert!(
        !out.starts_with("<blockquote"),
        "the whole message must not be one outer expandable"
    );
    // The processing log lives in its OWN expandable, chrome outside it.
    assert!(out.contains("<blockquote expandable><b>✅ bash</b> <code>git status</code>"));
    assert!(
        out.contains("</blockquote>\n"),
        "footer is a plain line under the log"
    );
    // In-flight footer: cog on the log summary, clock last.
    assert!(out.contains("⚙️"), "in-flight log summary carries the cog");
    assert!(out.contains("2 tool calls"));
    assert!(out.ends_with("⏱ 0:20"), "clock is the last footer segment");
}

#[test]
fn details_populated_flow_keeps_chrome_outside_the_details() {
    let lines = [tline("✅ bash", "ls"), tline("✅ grep", "todo")];
    let out = render_flow_details_chrome(
        &lines,
        &FlowHeader::Live(Some("8s")),
        None,
        &sections(None, None, Some("finish the audit")),
        8,
    );
    // Chrome is an always-visible <p> block BEFORE the collapsed log, with a
    // kept spacer, not inside the summary.
    assert!(out.starts_with(
        "<p><b>🎯 Goal:</b> finish the audit</p><p>&nbsp;</p><details><summary><sub>"
    ));
    assert!(out.ends_with("</details>"));
    assert!(out.contains("⏱ 0:08"));
}

#[test]
fn footer_shows_both_working_on_status_and_activity_summary() {
    // ADR 0005 footer merge: Working-on is segment 1, the live activity is the
    // segment-2 log summary — both visible (the old "activity beats fallback"
    // collapsed-preview rule is gone).
    let lines = [
        tline("✅ bash", "ls"),
        FlowLine::Text("Now checking the config.".to_string()),
        tline("⚙️ read_file", "config.toml"),
    ];
    let out = render_flow_html_chrome(
        &lines,
        &FlowHeader::Live(Some("30s")),
        Some("Working on: ship it"),
        &FlowSections::default(),
        30,
    );
    assert!(
        out.contains("Working on: ship it"),
        "status segment present"
    );
    assert!(
        out.contains("Now checking the config."),
        "activity summary present"
    );
}

#[test]
fn single_tool_gets_its_own_log_block_and_footer() {
    // The lone-tool-plain shortcut is gone under ADR 0005: even one entry sits
    // in its own expandable with the footer below.
    let out = render_flow_html_chrome(
        &[tline("✅ bash", "git status")],
        &FlowHeader::Live(None),
        None,
        &sections(Some("Plan"), None, None),
        0,
    );
    assert!(out.starts_with("📋 <b>Plan</b>\n\n"));
    assert!(
        out.contains(
            "<blockquote expandable><b>✅ bash</b> <code>git status</code></blockquote>\n"
        )
    );
    assert!(out.contains("1 tool calls"));
    assert!(out.ends_with("⏱ 0:00"));
}

#[test]
fn settled_footer_drops_the_cog() {
    // Settled footer: outcome carries ✅/❌, the log summary is a bare tool
    // count with NO cog (Decision 4 / 12).
    let lines = [tline("✅ bash", "ls"), tline("✅ grep", "todo")];
    let out = render_flow_html_chrome(
        &lines,
        &FlowHeader::Settled {
            icon: "",
            verb: "Finished",
            duration: "2m",
        },
        None,
        &FlowSections::default(),
        124,
    );
    // Footer is the plain final line under the log block.
    let footer = out
        .rsplit("</blockquote>\n")
        .next()
        .expect("footer present");
    assert!(footer.starts_with("✅ Finished • 2 tool calls • ⏱ 2:04"));
    assert!(
        !footer.contains("⚙️"),
        "settled footer never carries the cog"
    );
}

#[test]
fn checklist_rows_render_as_separate_rich_paragraphs() {
    let out = render_flow_details_chrome(
        &[],
        &FlowHeader::Live(Some("2s")),
        None,
        &sections(Some("Plan"), Some(vec!["☑ done one", "☐ next"]), None),
        2,
    );
    // Rich: title and each checklist row are their own <p> block before the
    // kept spacer and the <sub> footer (rich HTML ignores raw newlines).
    assert!(
        out.starts_with("<p>📋 <b>Plan</b></p><p>☑ done one</p><p>☐ next</p><p>&nbsp;</p><sub>")
    );
}

#[test]
fn full_checklist_kept_when_all_tasks_done() {
    // Decision 9: the full list stays through settle even when every task is
    // ticked (the old N/M count hid a fully-done checklist).
    let out = render_flow_html_chrome(
        &[],
        &FlowHeader::Live(None),
        None,
        &sections(Some("Done plan"), Some(vec!["☑ a", "☑ b"]), None),
        0,
    );
    assert!(out.starts_with("📋 <b>Done plan</b>\n☑ a\n☑ b\n\n"));
}

// ── provider-aware folded-narration cap (#532 / upstream #531) ──────
// CLI providers fold the whole model turn into the block, so folded narration
// is capped (300); API providers pass uncapped (usize::MAX) and keep full
// reasoning. cap_narration is private, so these exercise it through the render
// path: a long narration line is truncated at the CLI cap and kept whole at the
// API cap.

fn long_narration(n: usize) -> String {
    "x".repeat(n)
}

fn narration_then_tool() -> [FlowLine; 2] {
    [
        FlowLine::Text(long_narration(1000)),
        tline("⚙️ read_file", "config.toml"),
    ]
}

#[test]
fn cli_cap_truncates_body_api_keeps_it_full_html() {
    let lines = narration_then_tool();
    let cli = render_flow_html_chrome_pref(
        &lines,
        &FlowHeader::Live(Some("2s")),
        None,
        &FlowSections::default(),
        300,
        2,
    );
    let api = render_flow_html_chrome_pref(
        &lines,
        &FlowHeader::Live(Some("2s")),
        None,
        &FlowSections::default(),
        usize::MAX,
        2,
    );
    assert!(
        cli.contains(''),
        "CLI cap truncates the folded body with an ellipsis: {cli}"
    );
    assert!(
        api.chars().count() > cli.chars().count(),
        "API keeps the full folded body, CLI truncates it (cli={} api={})",
        cli.chars().count(),
        api.chars().count()
    );
    assert!(
        api.contains(&long_narration(1000)),
        "the uncapped API render keeps the whole 1000-char body entry"
    );
}

#[test]
fn cli_cap_truncates_body_api_keeps_it_full_details() {
    let lines = narration_then_tool();
    let cli = render_flow_details_chrome_pref(
        &lines,
        &FlowHeader::Live(Some("2s")),
        None,
        &FlowSections::default(),
        300,
        2,
    );
    let api = render_flow_details_chrome_pref(
        &lines,
        &FlowHeader::Live(Some("2s")),
        None,
        &FlowSections::default(),
        usize::MAX,
        2,
    );
    assert!(
        cli.contains(''),
        "CLI cap truncates in the details path too"
    );
    assert!(
        api.chars().count() > cli.chars().count(),
        "API keeps the full folded body in the details path (cli={} api={})",
        cli.chars().count(),
        api.chars().count()
    );
}

#[test]
fn short_narration_untouched_by_either_cap() {
    let lines = [
        FlowLine::Text("brief note".to_string()),
        tline("⚙️ read_file", "config.toml"),
    ];
    for cap in [300usize, usize::MAX] {
        let out = render_flow_html_chrome_pref(
            &lines,
            &FlowHeader::Live(Some("2s")),
            None,
            &FlowSections::default(),
            cap,
            2,
        );
        assert!(out.contains("brief note"));
        assert!(
            !out.contains(''),
            "short narration must never be truncated (cap={cap})"
        );
    }
}