返回 CodeWhale
pending_input_preview.rs
根目录 / crates / tui / src / tui / widgets / pending_input_preview.rs
1 //! Pending-input preview widget for the composer area.
2 //!
3 //! Renders queued and in-turn follow-ups above the composer when a turn is
4 //! in flight, so typed input doesn't disappear silently. The backing state
5 //! still distinguishes queue vs send-now origins, but the UI renders one
6 //! coherent pending-input list.
7 //!
8 //! Empty state renders zero rows so the composer doesn't gain wasted height
9 //! when there's nothing to show.
10 //!
11 //! Wired into `ui.rs::render` between the chat area and the composer; the user
12 //! can see when typed input has been captured for later delivery.
13
14 use codewhale_ratatui::{
15 Paint, PendingCard, PendingCardContext, PendingCardStyles, PendingCardWords,
16 };
17 use ratatui::buffer::Buffer;
18 use ratatui::layout::Rect;
19 use ratatui::style::{Modifier, Style};
20
21 use crate::tui::widgets::Renderable;
22 use codewhale_localization::{Locale, MessageId, tr};
23 use codewhale_palette as palette;
24 use codewhale_tui_kit::menu_style;
25
26 /// Description of the keybinding the hint line at the bottom should advertise
27 /// for the "edit last queued message" action.
28 #[derive(Debug, Clone)]
29 pub struct EditBinding {
30 pub label: &'static str,
31 }
32
33 impl EditBinding {
34 pub const UP: EditBinding = EditBinding { label: "↑" };
35 }
36
37 /// Widget showing pending input while a turn is in progress.
38 #[derive(Debug, Clone)]
39 pub struct PendingInputPreview {
40 pub locale: Locale,
41 pub context_items: Vec<ContextPreviewItem>,
42 pub pending_steers: Vec<String>,
43 pub queued_messages: Vec<String>,
44 pub editing_queued_message: Option<String>,
45 pub edit_binding: EditBinding,
46 /// waiting on the person whose card is not on top (approvals C1).
47 pub pending_approvals: Vec<String>,
48 pub offline: bool,
49 }
50
51 /// Compact pre-send context row shown above the composer. `included=false`
52 /// marks unconfirmed, missing, or skipped context distinctly from files/media
53 /// already known to be sent or inlined.
54 #[derive(Debug, Clone, PartialEq, Eq)]
55 pub struct ContextPreviewItem {
56 pub kind: String,
57 pub label: String,
58 pub detail: Option<String>,
59 pub included: bool,
60 pub removable: bool,
61 pub selected: bool,
62 }
63
64 impl PendingInputPreview {
65 pub fn new() -> Self {
66 Self {
67 locale: Locale::En,
68 context_items: Vec::new(),
69 pending_steers: Vec::new(),
70 queued_messages: Vec::new(),
71 editing_queued_message: None,
72 edit_binding: EditBinding::UP,
73 pending_approvals: Vec::new(),
74 offline: false,
75 }
76 }
77
78 fn kit(&self) -> PendingCard<'_> {
79 let (queued_controls, compact_controls) = if self.offline {
80 let saved = tr(self.locale, MessageId::ToastOfflineQueuedCount)
81 .replace("{count}", &self.queued_messages.len().to_string());
82 (saved.clone(), saved)
83 } else {
84 (
85 tr(self.locale, MessageId::PendingSendNowControls)
86 .replace("{key}", self.edit_binding.label),
87 tr(self.locale, MessageId::PendingSendNowDropControls)
88 .replace("{key}", self.edit_binding.label),
89 )
90 };
91 let mut card = PendingCard::new(PendingCardWords {
92 context_header: tr(self.locale, MessageId::PendingContextHeader),
93 inputs_header: tr(self.locale, MessageId::PendingInputsHeader),
94 sending_prefix: tr(self.locale, MessageId::PendingSendingIntoTurnPrefix),
95 editing_prefix: tr(self.locale, MessageId::PendingEditingFollowUpPrefix),
96 editing_restore: tr(self.locale, MessageId::PendingEscRestore),
97 queued_prefix: tr(self.locale, MessageId::PendingQueuedFollowUpPrefix),
98 queued_one_prefix: tr(self.locale, MessageId::PendingQueuedOnePrefix),
99 queued_many_prefix: tr(self.locale, MessageId::PendingQueuedManyPrefix),
100 queued_controls: queued_controls.into(),
101 compact_controls: compact_controls.into(),
102 // These are the existing context suffixes; their copy has not
103 // acquired new localization authority in this rendering slice.
104 removable: "removable".into(),
105 selected_remove: "Backspace/Delete removes".into(),
106 });
107 card.context = self
108 .context_items
109 .iter()
110 .map(|item| PendingCardContext {
111 kind: item.kind.as_str().into(),
112 label: item.label.as_str().into(),
113 detail: item.detail.as_deref().map(Into::into),
114 included: item.included,
115 removable: item.removable,
116 selected: item.selected,
117 })
118 .collect();
119 card.sending = self
120 .pending_steers
121 .iter()
122 .map(|value| value.as_str().into())
123 .collect();
124 card.queued = self
125 .queued_messages
126 .iter()
127 .map(|value| value.as_str().into())
128 .collect();
129 card.editing = self.editing_queued_message.as_deref().map(Into::into);
130 card.priority_rows = self
131 .pending_approvals
132 .iter()
133 .map(|value| value.as_str().into())
134 .collect();
135 card.styles = Some(PendingCardStyles {
136 input: Style::default()
137 .fg(palette::TEXT_DIM)
138 .add_modifier(Modifier::DIM),
139 warning: Style::default().fg(palette::STATUS_WARNING),
140 context_muted: Style::default().fg(palette::TEXT_MUTED),
141 context_label: Style::default().fg(palette::TEXT_PRIMARY),
142 selected: menu_style::selected_row_bg_style().fg(palette::SELECTION_TEXT),
143 });
144 card
145 }
146 }
147
148 impl Default for PendingInputPreview {
149 fn default() -> Self {
150 Self::new()
151 }
152 }
153
154 impl Renderable for PendingInputPreview {
155 fn render(&self, area: Rect, buf: &mut Buffer) {
156 // The existing backend owns terminal punctuation/color projection.
157 // This pure card receives exact host styles and authors one row plan.
158 self.kit()
159 .paint(area, buf, &crate::tui::infoline::source_theme(false));
160 }
161 fn desired_height(&self, width: u16) -> u16 {
162 self.kit()
163 .height(width, &crate::tui::infoline::source_theme(false))
164 }
165 }
166
167 #[cfg(test)]
168 mod tests {
169 use super::*;
170
171 fn render_to_string(widget: &PendingInputPreview, width: u16) -> Vec<String> {
172 let height = widget.desired_height(width);
173 if height == 0 {
174 return Vec::new();
175 }
176 let mut buf = Buffer::empty(Rect::new(0, 0, width, height));
177 widget.render(Rect::new(0, 0, width, height), &mut buf);
178 (0..height)
179 .map(|y| {
180 (0..width)
181 .map(|x| buf[(x, y)].symbol().chars().next().unwrap_or(' '))
182 .collect::<String>()
183 .trim_end()
184 .to_string()
185 })
186 .collect()
187 }
188
189 fn render_in_area(widget: &PendingInputPreview, width: u16, height: u16) -> Vec<String> {
190 let mut buf = Buffer::empty(Rect::new(0, 0, width, height));
191 widget.render(Rect::new(0, 0, width, height), &mut buf);
192 (0..height)
193 .map(|y| {
194 (0..width)
195 .map(|x| buf[(x, y)].symbol().chars().next().unwrap_or(' '))
196 .collect::<String>()
197 .trim_end()
198 .to_string()
199 })
200 .collect()
201 }
202
203 #[test]
204 fn empty_widget_has_zero_height() {
205 let preview = PendingInputPreview::new();
206 assert_eq!(preview.desired_height(40), 0);
207 }
208
209 #[test]
210 fn single_queued_message_renders_header_item_and_hint() {
211 let mut preview = PendingInputPreview::new();
212 preview.queued_messages.push("Hello, world!".to_string());
213 let rows = render_to_string(&preview, 40);
214 assert_eq!(rows.len(), 2, "got rows: {rows:?}");
215 assert!(rows[0].contains("Queued #1: Hello, world!"));
216 assert!(rows[1].contains("Enter send now"));
217 assert!(rows[1].contains("↑ edit"));
218 assert!(rows[1].contains("/queue drop 1"));
219 }
220
221 #[test]
222 fn compact_queue_keeps_send_control_in_one_two_and_three_row_areas() {
223 let mut preview = PendingInputPreview::new();
224 preview
225 .queued_messages
226 .push("ship the compact fix".to_string());
227
228 for (width, height) in [(40, 1), (40, 2), (60, 3)] {
229 let rows = render_in_area(&preview, width, height);
230 assert!(
231 rows.iter().any(|row| row.contains("Enter send now")),
232 "send control clipped at {width}x{height}: {rows:?}"
233 );
234 }
235 }
236
237 #[test]
238 fn editing_queued_message_renders_explicit_state_and_restore_hint() {
239 let mut preview = PendingInputPreview::new();
240 preview.editing_queued_message = Some("revise before sending".to_string());
241
242 let rows = render_to_string(&preview, 80);
243
244 assert!(rows[0].contains("Pending inputs"));
245 assert!(
246 rows.iter()
247 .any(|row| row.contains("Editing follow-up: revise before sending")),
248 "missing editing label: {rows:?}"
249 );
250 assert!(
251 rows.iter()
252 .any(|row| row.contains("Esc restores the queued follow-up")),
253 "missing restore hint: {rows:?}"
254 );
255 assert!(
256 !rows.iter().any(|row| row.contains("edit last queued")),
257 "editing mode should not also advertise opening a queued edit: {rows:?}"
258 );
259 }
260
261 #[test]
262 fn context_items_render_before_queue_buckets() {
263 let mut preview = PendingInputPreview::new();
264 preview.context_items.push(ContextPreviewItem {
265 kind: "file".to_string(),
266 label: "src/main.rs".to_string(),
267 detail: Some("included".to_string()),
268 included: true,
269 removable: false,
270 selected: false,
271 });
272 preview.context_items.push(ContextPreviewItem {
273 kind: "missing".to_string(),
274 label: "nope.txt".to_string(),
275 detail: Some("not found".to_string()),
276 included: false,
277 removable: false,
278 selected: false,
279 });
280 let rows = render_to_string(&preview, 64);
281 assert!(rows[0].contains("Context for next send"));
282 assert!(rows[1].contains("[file] src/main.rs"));
283 assert!(rows[2].contains("[missing] nope.txt"));
284 }
285
286 #[test]
287 fn selected_removable_attachment_renders_delete_hint() {
288 let mut preview = PendingInputPreview::new();
289 preview.context_items.push(ContextPreviewItem {
290 kind: "image".to_string(),
291 label: "/tmp/pasted.png".to_string(),
292 detail: Some("attached media".to_string()),
293 included: true,
294 removable: true,
295 selected: true,
296 });
297
298 let rows = render_to_string(&preview, 96);
299
300 assert!(
301 rows.iter()
302 .any(|row| row.contains("Backspace/Delete removes"))
303 );
304 assert!(rows.iter().any(|row| row.contains("▸")));
305 }
306
307 #[test]
308 fn pending_steer_renders_without_queue_edit_hint() {
309 let mut preview = PendingInputPreview::new();
310 preview.pending_steers.push("Please continue.".to_string());
311 let rows = render_to_string(&preview, 80);
312 assert!(
313 rows.iter().any(|r| r.contains("Pending inputs")),
314 "missing pending input header: {rows:?}"
315 );
316 assert!(
317 !rows.iter().any(|r| r.contains("Esc")),
318 "unexpected Esc hint: {rows:?}"
319 );
320 assert!(
321 !rows.iter().any(|r| r.contains("edit last queued")),
322 "unexpected edit hint in pending-steer-only view: {rows:?}"
323 );
324 }
325
326 #[test]
327 fn all_pending_inputs_render_as_one_list() {
328 let mut preview = PendingInputPreview::new();
329 preview.pending_steers.push("steer".to_string());
330 preview.queued_messages.push("queued".to_string());
331 let rows = render_to_string(&preview, 60);
332 assert!(rows[0].contains("Pending inputs"));
333 assert_eq!(
334 rows.iter().filter(|r| r.contains("Pending inputs")).count(),
335 1
336 );
337 assert!(rows.iter().any(|r| r.contains("steer")));
338 assert!(rows.iter().any(|r| r.contains("queued")));
339 assert!(rows.iter().any(|r| r.contains("↑")));
340 assert!(rows.iter().any(|r| r.contains("Enter send now")));
341 }
342
343 #[test]
344 fn pending_input_copy_does_not_teach_steer() {
345 let mut preview = PendingInputPreview::new();
346 preview.pending_steers.push("please continue".to_string());
347 preview.queued_messages.push("next".to_string());
348 let joined = render_to_string(&preview, 80)
349 .join("\n")
350 .to_ascii_lowercase();
351 assert!(
352 !joined.contains("steer"),
353 "pending-input copy leaked internal vocabulary: {joined}"
354 );
355 assert!(joined.contains("sending into this turn"));
356 assert!(joined.contains("queued follow-up"));
357 }
358
359 #[test]
360 fn pending_input_rows_label_each_delivery_mode() {
361 let mut preview = PendingInputPreview::new();
362 preview.pending_steers.push("steer".to_string());
363 preview.queued_messages.push("queued".to_string());
364 preview.editing_queued_message = Some("editing".to_string());
365
366 let rows = render_to_string(&preview, 80);
367
368 assert!(
369 rows.iter()
370 .any(|row| row.contains("Sending into this turn: steer")),
371 "missing pending send-now label: {rows:?}"
372 );
373 assert!(
374 rows.iter()
375 .any(|row| row.contains("Queued follow-up #1: queued")),
376 "missing queued-follow-up label: {rows:?}"
377 );
378 assert!(
379 rows.iter()
380 .any(|row| row.contains("Editing follow-up: editing")),
381 "missing queued-edit label: {rows:?}"
382 );
383 }
384
385 #[test]
386 fn queued_only_preview_truncates_instead_of_hiding_controls() {
387 let mut preview = PendingInputPreview::new();
388 preview
389 .queued_messages
390 .push("alpha beta gamma delta epsilon zeta".to_string());
391
392 let rows = render_to_string(&preview, 34);
393
394 assert_eq!(rows.len(), 2, "got rows: {rows:?}");
395 assert!(rows[0].contains("Queued #1: alpha"));
396 assert!(rows[0].contains('…'));
397 assert!(rows[1].contains("Enter send now"));
398 }
399
400 #[test]
401 fn multiline_queued_message_collapses_to_one_truncated_summary() {
402 let mut preview = PendingInputPreview::new();
403 preview
404 .queued_messages
405 .push("line1\nline2\nline3\nline4\nline5\nline6\nline7".to_string());
406 let rows = render_to_string(&preview, 40);
407 assert_eq!(rows.len(), 2, "got rows: {rows:?}");
408 assert!(rows[0].contains("Queued #1: line1 line2"));
409 assert!(rows[0].contains('…'));
410 assert!(rows[1].contains("Enter send now"));
411 assert!(rows[1].contains("↑ edit"));
412 }
413
414 #[test]
415 fn long_url_does_not_explode_into_ellipsis_rows() {
416 let mut preview = PendingInputPreview::new();
417 preview.queued_messages.push(
418 "example.test/api/v1/projects/alpha/releases/2026-02-17/build/1234567890/artifacts/x"
419 .to_string(),
420 );
421 let rows = render_to_string(&preview, 36);
422 assert_eq!(rows.len(), 2, "got rows: {rows:?}");
423 assert!(rows[0].contains("Queued #1:"));
424 assert!(rows[1].contains("Enter send now"));
425 }
426
427 #[test]
428 fn narrow_width_renders_nothing() {
429 let mut preview = PendingInputPreview::new();
430 preview.queued_messages.push("hi".to_string());
431 assert_eq!(preview.desired_height(2), 0);
432 }
433
434 fn guarded_preview_buffer(area: Rect) -> Buffer {
435 // A nonzero buffer origin catches accidental use of local coordinates.
436 // A guard around the whole requested rectangle catches stray painting;
437 // cells inside are initially empty with a caller-owned background
438 // and modifier, including wide-character continuation cells.
439 let canvas = Rect::new(2, 3, area.width + 12, area.height + 8);
440 let mut buffer = Buffer::empty(canvas);
441 for y in canvas.y..canvas.bottom() {
442 for x in canvas.x..canvas.right() {
443 buffer[(x, y)].set_style(
444 Style::default()
445 .fg(ratatui::style::Color::Rgb(13, 41, 67))
446 .bg(ratatui::style::Color::Rgb(19, 47, 73))
447 .add_modifier(Modifier::UNDERLINED),
448 );
449 if x < area.x || x >= area.right() || y < area.y || y >= area.bottom() {
450 buffer[(x, y)].set_symbol("~");
451 }
452 }
453 }
454 buffer
455 }
456
457 fn assert_guard_cells_untouched(
458 actual: &Buffer,
459 initial: &Buffer,
460 area: Rect,
461 case: &str,
462 locale: Locale,
463 ) {
464 let canvas = initial.area;
465 for y in canvas.y..canvas.bottom() {
466 for x in canvas.x..canvas.right() {
467 if x < area.x || x >= area.right() || y < area.y || y >= area.bottom() {
468 assert_eq!(
469 actual[(x, y)],
470 initial[(x, y)],
471 "stray paint outside {area:?} at ({x},{y}) case={case} locale={locale:?}"
472 );
473 }
474 }
475 }
476 }
477
478 fn assert_bounded_render(preview: &PendingInputPreview, case: &str) {
479 assert_eq!(
480 preview.desired_height(0),
481 0,
482 "width 0 height case={case} locale={:?}",
483 preview.locale
484 );
485 assert_eq!(
486 preview.desired_height(2),
487 0,
488 "width 2 height case={case} locale={:?}",
489 preview.locale
490 );
491 for width in [0, 2, 4, 12, 40, 80] {
492 for height in [0, 1, 2, 3, 12] {
493 let area = Rect::new(7, 6, width, height);
494 let initial = guarded_preview_buffer(area);
495 let mut actual = initial.clone();
496 preview.render(area, &mut actual);
497 assert_guard_cells_untouched(&actual, &initial, area, case, preview.locale);
498 if area.is_empty() {
499 assert_eq!(
500 actual, initial,
501 "empty area mutated buffer case={case} locale={:?} area={area:?}",
502 preview.locale
503 );
504 }
505 }
506 }
507 }
508
509 fn parity_context(included: bool, removable: bool, selected: bool) -> ContextPreviewItem {
510 ContextPreviewItem {
511 kind: "file 文件".to_string(),
512 label: "資料/cafe\u{0301}-①-1\u{20e3}-👩‍💻.txt".to_string(),
513 detail: Some("CJK 你好 and cafe\u{0301} ① 1\u{20e3} attachment".to_string()),
514 included,
515 removable,
516 selected,
517 }
518 }
519
520 #[test]
521 fn kit_pending_preview_renders_bounded_output_in_every_shipped_locale_and_geometry() {
522 let long_url = "https://example.test/api/v1/projects/資料/releases/2026-10-02/build/1234567890/artifacts/abcdefghijklmnopqrstuvwxyz";
523 for &locale in Locale::shipped() {
524 let mut empty = PendingInputPreview::new();
525 empty.locale = locale;
526 let mut queued = empty.clone();
527 queued
528 .queued_messages
529 .push(format!("你好 cafe\u{0301} {long_url}"));
530 let mut queued_many = queued.clone();
531 queued_many
532 .queued_messages
533 .push("second follow-up".to_string());
534 let mut mixed = queued_many.clone();
535 mixed.context_items.push(parity_context(true, true, false));
536 mixed.context_items.push(parity_context(false, false, true));
537 mixed.pending_steers.push(format!(
538 "你好 cafe\u{0301} ① 1\u{20e3} 👩‍💻 first paragraph\nsecond paragraph {long_url}\nthird\nfourth"
539 ));
540 mixed.editing_queued_message =
541 Some("revise 你好 cafe\u{0301}\nnext paragraph".to_string());
542 mixed.edit_binding = EditBinding { label: "Alt+↑" };
543 let mut context_only = empty.clone();
544 context_only
545 .context_items
546 .push(parity_context(false, true, true));
547 context_only
548 .context_items
549 .push(parity_context(true, false, false));
550 let mut priority_only = empty.clone();
551 priority_only.pending_approvals = vec![
552 "Approval needed in 資料/cafe\u{0301} — /subagents".to_string(),
553 "Another child needs input — /agents".to_string(),
554 ];
555 let mut child_and_queued = queued.clone();
556 child_and_queued.pending_approvals = priority_only.pending_approvals.clone();
557
558 for width in [0, 2, 4, 12, 40, 80] {
559 assert_eq!(
560 empty.desired_height(width),
561 0,
562 "locale={locale:?} width={width}"
563 );
564 }
565 assert!(render_to_string(&empty, 120).is_empty());
566
567 assert_eq!(queued.desired_height(120), 2, "locale={locale:?}");
568 let queued_rows = render_to_string(&queued, 120);
569 assert!(
570 queued_rows[0].contains("cafe"),
571 "locale={locale:?} rows={queued_rows:?}"
572 );
573
574 assert_eq!(queued_many.desired_height(120), 2, "locale={locale:?}");
575 let queued_many_rows = render_to_string(&queued_many, 120);
576 assert!(
577 queued_many_rows[0].contains("cafe"),
578 "locale={locale:?} rows={queued_many_rows:?}"
579 );
580
581 assert_eq!(priority_only.desired_height(120), 2, "locale={locale:?}");
582 let priority_rows = render_to_string(&priority_only, 120);
583 assert!(
584 priority_rows[0].contains("Approval needed in"),
585 "locale={locale:?} rows={priority_rows:?}"
586 );
587 assert!(
588 priority_rows[1].contains("Another child needs input"),
589 "locale={locale:?} rows={priority_rows:?}"
590 );
591
592 assert_eq!(child_and_queued.desired_height(120), 4, "locale={locale:?}");
593 let child_and_queued_rows = render_to_string(&child_and_queued, 120);
594 assert!(
595 child_and_queued_rows[0].contains("Approval needed in"),
596 "locale={locale:?} rows={child_and_queued_rows:?}"
597 );
598 assert!(
599 child_and_queued_rows[2].contains("cafe"),
600 "locale={locale:?} rows={child_and_queued_rows:?}"
601 );
602
603 assert!(context_only.desired_height(120) >= 3, "locale={locale:?}");
604 let context_rows = render_to_string(&context_only, 120);
605 assert!(
606 context_rows.iter().any(|row| row.contains("[file ")),
607 "locale={locale:?} rows={context_rows:?}"
608 );
609
610 assert!(mixed.desired_height(120) >= 8, "locale={locale:?}");
611 let mixed_rows = render_to_string(&mixed, 120);
612 assert!(
613 mixed_rows.iter().any(|row| row.contains("Alt+↑")),
614 "locale={locale:?} rows={mixed_rows:?}"
615 );
616 assert!(
617 mixed_rows
618 .iter()
619 .any(|row| row.contains("second follow-up")),
620 "locale={locale:?} rows={mixed_rows:?}"
621 );
622
623 for (case, preview) in [
624 ("empty", empty),
625 ("queued-only", queued),
626 ("queued-many", queued_many),
627 ("mixed", mixed),
628 ("context-only", context_only),
629 ("priority-only", priority_only),
630 ("child-and-queued", child_and_queued),
631 ] {
632 assert_bounded_render(&preview, case);
633 }
634 }
635 }
636
637 #[test]
638 fn kit_pending_context_flags_remain_independent_of_each_other() {
639 for &locale in Locale::shipped() {
640 for included in [false, true] {
641 for removable in [false, true] {
642 for selected in [false, true] {
643 let mut preview = PendingInputPreview::new();
644 preview.locale = locale;
645 preview
646 .context_items
647 .push(parity_context(included, removable, selected));
648 let case = format!(
649 "included={included} removable={removable} selected={selected}"
650 );
651 assert_bounded_render(&preview, &case);
652
653 let height = preview.desired_height(160);
654 assert_eq!(height, 2, "locale={locale:?} {case}");
655 let mut buf = Buffer::empty(Rect::new(0, 0, 160, height));
656 preview.render(Rect::new(0, 0, 160, height), &mut buf);
657
658 let rows = render_to_string(&preview, 160);
659 assert_eq!(
660 rows[1].starts_with(" ▸ "),
661 selected,
662 "locale={locale:?} {case} rows={rows:?}"
663 );
664 assert_eq!(
665 rows[1].starts_with(" ↳ "),
666 !selected,
667 "locale={locale:?} {case} rows={rows:?}"
668 );
669 assert_eq!(
670 rows[1].contains("Backspace/Delete removes"),
671 selected,
672 "locale={locale:?} {case} rows={rows:?}"
673 );
674 assert_eq!(
675 rows[1].contains(" · removable"),
676 !selected && removable,
677 "locale={locale:?} {case} rows={rows:?}"
678 );
679
680 let expected_prefix_fg = if selected {
681 palette::SELECTION_TEXT
682 } else if included {
683 palette::TEXT_MUTED
684 } else {
685 palette::STATUS_WARNING
686 };
687 let expected_body_fg = if selected {
688 palette::SELECTION_TEXT
689 } else if included {
690 palette::TEXT_PRIMARY
691 } else {
692 palette::TEXT_MUTED
693 };
694 assert_eq!(
695 buf[(2, 1)].fg,
696 expected_prefix_fg,
697 "prefix fg locale={locale:?} {case}"
698 );
699 assert_eq!(
700 buf[(2, 1)].modifier.contains(Modifier::BOLD),
701 selected,
702 "prefix bold locale={locale:?} {case}"
703 );
704 assert_eq!(
705 buf[(4, 1)].fg,
706 expected_body_fg,
707 "body fg locale={locale:?} {case}"
708 );
709 }
710 }
711 }
712 }
713 }
714
715 #[test]
716 fn kit_pending_control_and_bidi_sanitization_precedes_measurement_and_paint() {
717 // Sanitization is an intentional safety change, so this uses explicit
718 // sanitized user text rather than comparing against the old renderer.
719 for &locale in Locale::shipped() {
720 let mut safe = PendingInputPreview::new();
721 safe.locale = locale;
722 safe.context_items.push(ContextPreviewItem {
723 kind: "ABCD".to_string(),
724 label: "ABCD".to_string(),
725 detail: Some("ABCD".to_string()),
726 included: false,
727 removable: true,
728 selected: true,
729 });
730 safe.pending_steers.push("ABCD".to_string());
731 safe.queued_messages.push("ABCD".to_string());
732 safe.editing_queued_message = Some("ABCD".to_string());
733 safe.pending_approvals.push("ABCD".to_string());
734 let mut unsafe_text = safe.clone();
735 let value = "A\u{202e}B\u{2066}C\u{001b}\0\t\rD".to_string();
736 unsafe_text.context_items[0].kind = value.clone();
737 unsafe_text.context_items[0].label = value.clone();
738 unsafe_text.context_items[0].detail = Some(value.clone());
739 unsafe_text.pending_steers[0] = value.clone();
740 unsafe_text.queued_messages[0] = value.clone();
741 unsafe_text.editing_queued_message = Some(value.clone());
742 unsafe_text.pending_approvals[0] = value;
743 for width in [4, 12, 40, 80] {
744 assert_eq!(
745 unsafe_text.desired_height(width),
746 safe.desired_height(width)
747 );
748 let area = Rect::new(7, 6, width, 12);
749 let mut actual = guarded_preview_buffer(area);
750 let mut expected = actual.clone();
751 unsafe_text.render(area, &mut actual);
752 safe.render(area, &mut expected);
753 assert_eq!(
754 actual, expected,
755 "sanitization locale={locale:?} width={width}"
756 );
757 }
758 }
759 }
760
761 #[test]
762 fn kit_pending_multiline_body_keeps_three_rows_and_one_overflow_marker() {
763 for &locale in Locale::shipped() {
764 let mut preview = PendingInputPreview::new();
765 preview.locale = locale;
766 preview
767 .pending_steers
768 .push("one\ntwo\nthree\nfour\nfive\nsix\nseven".to_string());
769 assert_eq!(preview.desired_height(160), 5, "locale={locale:?}");
770 let rows = render_to_string(&preview, 160);
771 assert_eq!(rows.len(), 5);
772 assert!(rows[1].ends_with("one"));
773 assert!(rows[2].ends_with("two"));
774 assert!(rows[3].ends_with("three"));
775 assert_eq!(rows[4].trim(), "…");
776 assert!(!rows.iter().any(|row| row.contains("four")));
777 }
778 }
779 }
780
780 lines RUST