trusty-code-tui 0.1.1

Engine-agnostic terminal-UI seam (TuiEngine trait + ReplEvent) shared by trusty-code and trusty-agents interactive REPLs
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
//! Tests for `super::apply` (`crate::app::reduce`), split into its own file
//! to satisfy the 500-SLOC production-file cap (`scripts/check_line_cap.sh`)
//! — mirrors the precedent set by Slice 3 (splitting a widget module's
//! implementation from its test module across two files). Reachable via
//! `crate::app::reduce::tests` exactly as an inline `mod tests { ... }`
//! would have been; only the file boundary changed.

use super::*;
use crate::app::ReplApp;
use crate::event::{KeyModifiers, WorkstreamSummary};
use crate::model::StatuslineSegment;
use crate::run::TuiModel;

fn key(code: KeyCode) -> ReplEvent {
    ReplEvent::Key(KeyInput {
        code,
        modifiers: KeyModifiers::default(),
    })
}

fn ctrl_key(c: char) -> ReplEvent {
    ReplEvent::Key(KeyInput {
        code: KeyCode::Char(c),
        modifiers: KeyModifiers {
            ctrl: true,
            alt: false,
            shift: false,
        },
    })
}

#[test]
fn apply_char_inserts_at_cursor() {
    let mut app = ReplApp::new("demo", "u");
    apply(&mut app, key(KeyCode::Char('h')));
    apply(&mut app, key(KeyCode::Char('i')));
    assert_eq!(app.input_buf, "hi");
    assert_eq!(app.cursor_pos, 2);
}

#[test]
fn apply_backspace_removes_last_char() {
    let mut app = ReplApp::new("demo", "u");
    app.insert_char('h');
    app.insert_char('i');
    apply(&mut app, key(KeyCode::Backspace));
    assert_eq!(app.input_buf, "h");
}

#[test]
fn apply_enter_submits_and_echoes() {
    let mut app = ReplApp::new("demo", "u");
    for c in "hello".chars() {
        apply(&mut app, key(KeyCode::Char(c)));
    }
    apply(&mut app, key(KeyCode::Enter));
    assert!(app.input_buf.is_empty());
    assert_eq!(app.pending_submit.take(), Some("hello".to_string()));
    assert_eq!(app.chat.len(), 1);
    assert_eq!(app.chat[0].text, "hello");
    assert_eq!(app.history, vec!["hello".to_string()]);
}

#[test]
fn apply_enter_on_empty_input_is_noop() {
    let mut app = ReplApp::new("demo", "u");
    apply(&mut app, key(KeyCode::Enter));
    assert!(app.pending_submit.is_none());
    assert!(app.chat.is_empty());
}

#[test]
fn apply_submit_event_mirrors_enter() {
    let mut app = ReplApp::new("demo", "u");
    apply(&mut app, ReplEvent::Submit("/model opus-4".to_string()));
    assert_eq!(app.pending_submit.take(), Some("/model opus-4".to_string()));
    assert_eq!(app.chat[0].text, "/model opus-4");
}

/// DOC-50 §5 Slice 5's "blocks user input until cancel completes", made
/// literal: while a turn is in flight (`busy == true`), Enter must not start
/// a second turn — and must leave the typed text sitting in `input_buf`
/// rather than consuming it and dropping it via `submit_line`'s own guard.
/// This is the direct fix for the double-submit corruption a code-review
/// pass caught on PR #3477 (task B's chunks splicing into task A's orphaned
/// `streaming_idx` entry).
#[test]
fn apply_enter_is_noop_while_busy_and_preserves_buffer() {
    let mut app = ReplApp::new("demo", "u");
    app.busy = true;
    for c in "explain Y".chars() {
        apply(&mut app, key(KeyCode::Char(c)));
    }
    let chat_len_before = app.chat.len();
    apply(&mut app, key(KeyCode::Enter));
    assert_eq!(
        app.input_buf, "explain Y",
        "typed text must survive a blocked Enter, not be discarded"
    );
    assert!(app.pending_submit.is_none(), "must not stage a second turn");
    assert_eq!(
        app.chat.len(),
        chat_len_before,
        "must not echo a second user line while busy"
    );
}

/// Same guard, exercised via the synthesized `ReplEvent::Submit` path
/// (e.g. a future picker confirmation) rather than the Enter key — proves
/// the guard lives in `submit_line` itself, not just the Enter call site.
#[test]
fn apply_submit_event_is_noop_while_busy() {
    let mut app = ReplApp::new("demo", "u");
    app.busy = true;
    apply(&mut app, ReplEvent::Submit("/model opus-4".to_string()));
    assert!(app.pending_submit.is_none());
    assert!(app.chat.is_empty(), "must not echo while busy");
}

/// Direct port of tagent's `repl_app_up_arrow_recalls_last_prompt`
/// (`crates/trusty-agents/src/repl/tui/tests_input.rs`).
#[test]
fn apply_up_recalls_last_prompt_when_idle() {
    let mut app = ReplApp::new("demo", "u");
    app.last_prompt = "hello world".to_string();
    app.busy = false;
    apply(&mut app, key(KeyCode::Up));
    assert_eq!(app.input_buf, "hello world");
    assert_eq!(app.cursor_pos, "hello world".len());
    assert!(!app.pending_cancel, "idle Up must NOT signal cancel");
}

/// Direct port of tagent's `repl_app_up_arrow_when_busy_signals_cancel`.
#[test]
fn apply_up_signals_cancel_and_recalls_when_busy() {
    let mut app = ReplApp::new("demo", "u");
    app.last_prompt = "long task".to_string();
    app.busy = true;
    apply(&mut app, key(KeyCode::Up));
    assert!(app.pending_cancel, "busy Up must signal cancel");
    assert_eq!(app.input_buf, "long task", "must restore last_prompt");
}

/// tagent's cancel signal fires unconditionally on `thinking`, ahead of
/// (and independent of) the `last_prompt` recall — pins that ordering
/// isn't accidentally coupled to `last_prompt` being non-empty.
#[test]
fn apply_up_signals_cancel_even_with_no_last_prompt() {
    let mut app = ReplApp::new("demo", "u");
    app.busy = true;
    assert!(app.last_prompt.is_empty());
    apply(&mut app, key(KeyCode::Up));
    assert!(app.pending_cancel);
    assert!(app.input_buf.is_empty());
}

/// Direct port of tagent's `repl_app_up_arrow_noop_when_no_last_prompt`.
#[test]
fn apply_up_is_noop_when_idle_and_no_last_prompt() {
    let mut app = ReplApp::new("demo", "u");
    app.insert_char('a');
    app.insert_char('b');
    app.last_prompt.clear();
    app.busy = false;
    apply(&mut app, key(KeyCode::Up));
    assert_eq!(app.input_buf, "ab");
    assert!(!app.pending_cancel);
}

/// Direct port of tagent's real `KeyCode::Down` arm — a functional
/// no-op today (nothing sets `history_idx`), kept for fidelity per
/// `crate::app`'s disclosure list.
#[test]
fn apply_down_calls_history_next_and_is_currently_inert() {
    let mut app = ReplApp::new("demo", "u");
    app.insert_char('x');
    apply(&mut app, key(KeyCode::Down));
    assert_eq!(app.input_buf, "x", "Down must not clobber input_buf");
    assert!(app.history_idx.is_none());
}

/// Direct port of tagent's `ctrl_e_pastes_last_bash_block_when_input_empty`.
#[test]
fn apply_ctrl_e_pastes_last_bash_block_when_input_empty() {
    let mut app = ReplApp::new("demo", "u");
    app.push_assistant("```bash\necho hi\n```", false);
    apply(&mut app, ctrl_key('e'));
    assert_eq!(app.input_buf, "echo hi");
    assert_eq!(app.cursor_pos, "echo hi".len());
}

/// Direct port of tagent's
/// `ctrl_e_falls_back_to_end_of_line_when_input_nonempty`.
#[test]
fn apply_ctrl_e_falls_back_to_end_of_line_when_input_nonempty() {
    let mut app = ReplApp::new("demo", "u");
    app.push_assistant("```bash\necho hi\n```", false);
    app.set_input("typed text".into());
    app.cursor_pos = 0;
    apply(&mut app, ctrl_key('e'));
    assert_eq!(app.input_buf, "typed text");
    assert_eq!(app.cursor_pos, "typed text".len());
}

/// Direct port of tagent's `ctrl_e_no_op_when_no_block_and_input_empty`.
#[test]
fn apply_ctrl_e_noop_when_no_block_and_input_empty() {
    let mut app = ReplApp::new("demo", "u");
    apply(&mut app, ctrl_key('e'));
    assert_eq!(app.input_buf, "");
    assert_eq!(app.cursor_pos, 0);
}

/// Direct port of tagent's `ctrl_e_noop_when_python_block_but_no_shell_block`.
#[test]
fn apply_ctrl_e_noop_when_only_non_shell_block_present() {
    let mut app = ReplApp::new("demo", "u");
    app.push_assistant("Result:\n```python\nprint('hi')\n```", false);
    assert_eq!(app.last_bash_block, None);
    apply(&mut app, ctrl_key('e'));
    assert_eq!(app.input_buf, "");
    assert_eq!(app.cursor_pos, 0);
}

/// Multi-line block: only the first non-blank line pastes (single-line
/// input constraint) — pins the exact behavior a naive "paste the whole
/// block" port would get wrong.
#[test]
fn apply_ctrl_e_pastes_only_first_line_of_multiline_block() {
    let mut app = ReplApp::new("demo", "u");
    app.push_assistant("```bash\ngit add -A\ngit commit -m \"msg\"\n```", false);
    apply(&mut app, ctrl_key('e'));
    assert_eq!(app.input_buf, "git add -A");
}

/// Pins the pre-first-token latency window fix: `busy` must already be
/// `true` immediately after Submit, before any `AssistantOutput` chunk
/// has arrived — this is what makes the input composer's busy indicator
/// light up right away instead of staying on the idle placeholder while
/// waiting for the first token.
#[test]
fn apply_enter_sets_busy_before_any_assistant_output_arrives() {
    let mut app = ReplApp::new("demo", "u");
    for c in "hello".chars() {
        apply(&mut app, key(KeyCode::Char(c)));
    }
    assert!(!app.busy, "must not be busy before Submit");
    apply(&mut app, key(KeyCode::Enter));
    assert!(
        app.busy,
        "must be busy immediately at Submit, not only once streaming starts"
    );
}

#[test]
fn apply_ctrl_a_e_u_move_and_clear() {
    let mut app = ReplApp::new("demo", "u");
    app.set_input("hello".to_string());
    apply(&mut app, ctrl_key('a'));
    assert_eq!(app.cursor_pos, 0);
    apply(&mut app, ctrl_key('e'));
    assert_eq!(app.cursor_pos, 5);
    apply(&mut app, ctrl_key('u'));
    assert_eq!(app.input_buf, "");
    assert_eq!(app.cursor_pos, 0);
}

#[test]
fn apply_ctrl_c_signals_pending_cancel() {
    let mut app = ReplApp::new("demo", "u");
    apply(&mut app, ctrl_key('c'));
    assert!(app.pending_cancel);
}

/// Direct port of tagent's real `KeyCode::Char('d')` arm: Ctrl-D only quits
/// on an EMPTY input buffer (the readline EOF convention).
#[test]
fn apply_ctrl_d_signals_quit_when_input_empty() {
    let mut app = ReplApp::new("demo", "u");
    assert!(!app.should_quit());
    apply(&mut app, ctrl_key('d'));
    assert!(app.should_quit());
}

/// Direct port of tagent's real `KeyCode::Char('d')` arm: with text still in
/// the buffer, Ctrl-D is a no-op (tagent has no forward-delete fallback) —
/// pins the gap this slice's audit found and fixed (an earlier revision
/// quit unconditionally, losing unsaved input on a stray Ctrl-D).
#[test]
fn apply_ctrl_d_is_noop_when_input_nonempty() {
    let mut app = ReplApp::new("demo", "u");
    app.set_input("still typing".to_string());
    apply(&mut app, ctrl_key('d'));
    assert!(!app.should_quit());
    assert_eq!(app.input_buf, "still typing");
}

/// `ReplEvent::Quit` (synthesized by `crate::run::run`'s dispatch step when
/// `TuiEngine::handle_input` returns `Ok(false)`) must set `ReplApp::quit`
/// exactly like Ctrl-D does — the two are independent triggers for the same
/// state.
#[test]
fn apply_quit_event_signals_quit() {
    let mut app = ReplApp::new("demo", "u");
    assert!(!app.should_quit());
    apply(&mut app, ReplEvent::Quit);
    assert!(app.should_quit());
}

/// `ReplEvent::TurnFinished` (the `dispatch_pending` stuck-`busy` safety net)
/// must clear `busy`/`streaming_idx` and touch NOTHING else — in particular
/// it must never push a chat entry, which is exactly why it exists instead
/// of reusing an empty `AssistantOutput { done: true, .. }` — PROVIDED its
/// `generation` matches `app.current_generation` (the live turn).
#[test]
fn apply_turn_finished_clears_busy_and_streaming_idx_without_touching_chat() {
    let mut app = ReplApp::new("demo", "u");
    app.busy = true;
    app.streaming_idx = Some(0);
    app.current_generation = 1;
    app.push_status("unrelated"); // pre-existing chat content must survive
    let chat_before = app.chat.len();

    apply(&mut app, ReplEvent::TurnFinished { generation: 1 });

    assert!(!app.busy);
    assert!(app.streaming_idx.is_none());
    assert_eq!(
        app.chat.len(),
        chat_before,
        "must not push/alter any chat entry"
    );
}

/// The TOCTOU-race fix (PR #3477, final re-review round): a `TurnFinished`
/// whose `generation` is STALE relative to `app.current_generation` (a
/// newer turn is now live — e.g. the old turn was cancelled and the user
/// already resubmitted) must be a complete no-op. Without this guard, a
/// terminal signal that raced a cancel+resubmit could clear `busy`/
/// `streaming_idx` out from under a genuinely in-flight NEWER turn,
/// reintroducing the streaming-splice corruption class this whole PR
/// closes. This is a deterministic reducer-level test — the point of the
/// by-construction fix is that this comparison is the reducer's own logic,
/// not a live cross-thread race that would need reproducing.
#[test]
fn apply_turn_finished_with_stale_generation_is_ignored() {
    let mut app = ReplApp::new("demo", "u");
    app.busy = true;
    app.streaming_idx = Some(0);
    app.current_generation = 2; // a newer turn is now live

    apply(&mut app, ReplEvent::TurnFinished { generation: 1 }); // stale

    assert!(
        app.busy,
        "a stale TurnFinished must not clear busy for a newer in-flight turn"
    );
    assert_eq!(
        app.streaming_idx,
        Some(0),
        "a stale TurnFinished must not reset streaming_idx either"
    );
}

#[test]
fn apply_page_up_and_down_scroll_by_page() {
    let mut app = ReplApp::new("demo", "u");
    app.last_max_scroll
        .store(100, std::sync::atomic::Ordering::Relaxed);
    apply(&mut app, key(KeyCode::PageUp));
    assert_eq!(app.scroll_offset, PAGE_SCROLL as usize);
    apply(&mut app, key(KeyCode::PageDown));
    assert_eq!(app.scroll_offset, 0);
}

#[test]
fn apply_scroll_event_delegates_to_scroll() {
    let mut app = ReplApp::new("demo", "u");
    app.last_max_scroll
        .store(10, std::sync::atomic::Ordering::Relaxed);
    apply(&mut app, ReplEvent::Scroll(-5));
    assert_eq!(app.scroll_offset, 5);
}

#[test]
fn apply_cancel_event_signals_pending_cancel() {
    let mut app = ReplApp::new("demo", "u");
    apply(&mut app, ReplEvent::Cancel);
    assert!(app.pending_cancel);
}

#[test]
fn apply_assistant_output_streams_into_one_entry() {
    let mut app = ReplApp::new("demo", "u");
    apply(
        &mut app,
        ReplEvent::AssistantOutput {
            chunk: "Hel".into(),
            done: false,
            is_error: false,
        },
    );
    assert!(app.busy);
    assert_eq!(app.chat.len(), 1);
    apply(
        &mut app,
        ReplEvent::AssistantOutput {
            chunk: "lo".into(),
            done: true,
            is_error: false,
        },
    );
    assert!(!app.busy);
    assert_eq!(app.chat.len(), 1, "must accumulate into one entry");
    assert_eq!(app.chat[0].text, "Hello");
    assert!(app.streaming_idx.is_none());
}

#[test]
fn apply_assistant_output_finalizes_as_error_role() {
    use crate::app::ChatRole;
    let mut app = ReplApp::new("demo", "u");
    apply(
        &mut app,
        ReplEvent::AssistantOutput {
            chunk: "boom".into(),
            done: true,
            is_error: true,
        },
    );
    assert_eq!(app.chat[0].role, ChatRole::Error);
}

#[test]
fn apply_tool_invocation_renders_start_and_result_as_status() {
    let mut app = ReplApp::new("demo", "u");
    apply(
        &mut app,
        ReplEvent::ToolInvocation {
            id: "call-1".into(),
            tool_name: "git.checkout".into(),
            args: serde_json::json!("main"),
            result: None,
        },
    );
    assert_eq!(app.chat[0].text, "[TOOL] git.checkout: \"main\"");
    apply(
        &mut app,
        ReplEvent::ToolInvocation {
            id: "call-1".into(),
            tool_name: "git.checkout".into(),
            args: serde_json::json!("main"),
            result: Some("switched to main".into()),
        },
    );
    assert_eq!(app.chat[1].text, "[RESULT] switched to main");
}

#[test]
fn apply_status_message_and_clear_scrollback() {
    let mut app = ReplApp::new("demo", "u");
    apply(&mut app, ReplEvent::StatusMessage("hi".into()));
    assert_eq!(app.chat.len(), 1);
    apply(&mut app, ReplEvent::ClearScrollback);
    assert!(app.chat.is_empty());
}

#[test]
fn apply_connection_lost_pushes_status() {
    let mut app = ReplApp::new("demo", "u");
    apply(
        &mut app,
        ReplEvent::ConnectionLost {
            reason: "timeout".into(),
        },
    );
    assert_eq!(app.chat[0].text, "Connection lost: timeout");
}

#[test]
fn apply_statusline_update_replaces_segments() {
    let mut app = ReplApp::new("demo", "u");
    apply(
        &mut app,
        ReplEvent::StatuslineUpdate(vec![StatuslineSegment::SessionId("s1".into())]),
    );
    assert_eq!(app.statusline.len(), 1);
}

#[test]
fn apply_workstream_updated_sets_active_workstream() {
    let mut app = ReplApp::new("demo", "u");
    apply(
        &mut app,
        ReplEvent::WorkstreamUpdated(WorkstreamSummary {
            id: "a1".into(),
            name: "Token rotation".into(),
        }),
    );
    assert_eq!(app.active_workstream.unwrap().name, "Token rotation");
}

#[test]
fn apply_workstream_activation_changed_some_is_a_deliberate_noop() {
    // `Some(new_id)` alone names an id with no display name — the engine's
    // subsequent `WorkstreamUpdated` (asserted separately above) is what
    // actually sets the displayed workstream in this case, not this event.
    let mut app = ReplApp::new("demo", "u");
    apply(
        &mut app,
        ReplEvent::WorkstreamActivationChanged {
            new_active_id: Some("a1".into()),
            prior_id: None,
        },
    );
    assert!(app.active_workstream.is_none());
    assert!(app.chat.is_empty());
}

/// Regression test (DOC-50 §5.3, Slice 6): a `WorkstreamActivationChanged`
/// with `new_active_id: None` (the workstream was deactivated with no
/// replacement, DOC-48 §4.2/§4.3) must clear `app.active_workstream`
/// immediately. `ReplEvent::WorkstreamUpdated`'s payload is a concrete
/// `WorkstreamSummary`, so it can never represent "no active workstream" —
/// this event, and only this event, is what clears the indicator. Before
/// this fix, `WorkstreamActivationChanged` was treated as an unconditional
/// no-op for EVERY `new_active_id` value, so a deactivation left the status
/// line showing the stale, no-longer-active workstream forever.
#[test]
fn apply_workstream_activation_changed_none_clears_active_workstream() {
    let mut app = ReplApp::new("demo", "u");
    app.active_workstream = Some(WorkstreamSummary {
        id: "a1".into(),
        name: "Token rotation".into(),
    });

    apply(
        &mut app,
        ReplEvent::WorkstreamActivationChanged {
            new_active_id: None,
            prior_id: Some("a1".into()),
        },
    );

    assert!(
        app.active_workstream.is_none(),
        "deactivation must clear active_workstream, not leave the stale summary in place"
    );
}

#[test]
fn apply_resize_is_a_noop() {
    let mut app = ReplApp::new("demo", "u");
    let before = app.clone();
    apply(&mut app, ReplEvent::Resize(80, 24));
    assert_eq!(app.chat.len(), before.chat.len());
    assert_eq!(app.input_buf, before.input_buf);
}

/// Behavior-preserving port of tagent's
/// `push_assistant_trims_surrounding_blanks`
/// (`crates/trusty-agents/src/repl/tui/tests_state.rs`).
#[test]
fn push_assistant_trims_surrounding_blanks() {
    let mut app = ReplApp::new("demo", "u");
    app.push_assistant("\n\n2 + 2 = 4.\n\n   No tools needed.\n\n\n", false);
    assert_eq!(app.chat.len(), 1);
    assert_eq!(app.chat[0].text, "2 + 2 = 4.\n   No tools needed.");
}

/// Direct port of tagent's `repl_app_last_bash_block_updates_on_push`.
#[test]
fn push_assistant_updates_last_bash_block() {
    let mut app = ReplApp::new("demo", "u");
    assert_eq!(app.last_bash_block, None);
    app.push_assistant("Try `ls`:\n```bash\nls -la\n```", false);
    assert_eq!(app.last_bash_block, Some("ls -la".into()));
    app.push_assistant("Here:\n```sh\npwd\n```", false);
    assert_eq!(app.last_bash_block, Some("pwd".into()));
}

/// Direct port of tagent's `repl_app_last_bash_block_skips_errors`.
#[test]
fn push_assistant_skips_error_entries_for_bash_block() {
    let mut app = ReplApp::new("demo", "u");
    app.push_assistant("```bash\nls\n```", false);
    app.push_assistant("```bash\nrm -rf /\n```", true);
    assert_eq!(app.last_bash_block, Some("ls".into()));
}

/// A streamed (not single-push) assistant response must refresh
/// `last_bash_block` on finalize exactly like `push_assistant` does —
/// otherwise Ctrl-E's paste buffer goes stale for every streaming
/// engine (the MVP-required "streaming input/output" case).
#[test]
fn apply_assistant_output_refreshes_last_bash_block_on_finalize() {
    let mut app = ReplApp::new("demo", "u");
    apply(
        &mut app,
        ReplEvent::AssistantOutput {
            chunk: "```bash\n".into(),
            done: false,
            is_error: false,
        },
    );
    assert_eq!(
        app.last_bash_block, None,
        "must not update mid-stream, only on finalize"
    );
    apply(
        &mut app,
        ReplEvent::AssistantOutput {
            chunk: "echo hi\n```".into(),
            done: true,
            is_error: false,
        },
    );
    assert_eq!(app.last_bash_block, Some("echo hi".into()));
}