| 1 | //! Golden-buffer contract for the posture bar — the first row under the |
| 2 | //! composer. Goldens: `footer_{w}x{h}` — the one-row band at the bottom of |
| 3 | //! each blocker-size buffer. Re-bless by DELETING the golden and running |
| 4 | //! with `CODEWHALE_BLESS_GOLDENS=1`. |
| 5 | |
| 6 | use ratatui::layout::Rect; |
| 7 | use unicode_width::UnicodeWidthChar; |
| 8 | |
| 9 | use super::{ChromeInk, TidelineFooter, render_tideline_footer}; |
| 10 | use crate::tui::golden_harness::{BLOCKER_SIZES, assert_matches_golden, render_golden_text}; |
| 11 | use codewhale_palette::UI_THEME; |
| 12 | |
| 13 | struct Fixture { |
| 14 | permission: (&'static str, ChromeInk), |
| 15 | permission_key: Option<&'static str>, |
| 16 | turn_clock: Option<(&'static str, ChromeInk)>, |
| 17 | counts: Vec<(String, ChromeInk)>, |
| 18 | session_clock: Option<(&'static str, ChromeInk)>, |
| 19 | hint: Option<(&'static str, ChromeInk)>, |
| 20 | context_percent: u8, |
| 21 | right: Option<(&'static str, ChromeInk)>, |
| 22 | } |
| 23 | |
| 24 | /// A working turn with two sub-agents on the Ask dial stop. |
| 25 | fn working() -> Fixture { |
| 26 | Fixture { |
| 27 | permission: ("ask", ChromeInk::PermissionAsk), |
| 28 | permission_key: Some("Shift+Tab to change"), |
| 29 | turn_clock: Some(("working 1m 15s", ChromeInk::Active)), |
| 30 | counts: vec![("2 agents".to_string(), ChromeInk::Active)], |
| 31 | session_clock: Some(("worked 41m 12s", ChromeInk::Active)), |
| 32 | hint: Some(("Esc to interrupt", ChromeInk::MetadataHint)), |
| 33 | context_percent: 61, |
| 34 | right: None, |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl Fixture { |
| 39 | fn widget<'a>(&'a self, theme: &'a codewhale_palette::UiTheme) -> TidelineFooter<'a> { |
| 40 | TidelineFooter::new(theme, self.permission) |
| 41 | .permission_key(self.permission_key) |
| 42 | .turn_clock(self.turn_clock) |
| 43 | .counts(&self.counts) |
| 44 | .session_clock(self.session_clock) |
| 45 | .hint(self.hint) |
| 46 | .context_percent(self.context_percent) |
| 47 | .right(self.right) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn draw(width: u16, height: u16, footer: &TidelineFooter<'_>) -> String { |
| 52 | render_golden_text(width, height, |buf| { |
| 53 | // The shell reserves exactly one row for the bar. |
| 54 | render_tideline_footer( |
| 55 | Rect::new(0, height.saturating_sub(1), width, 1), |
| 56 | buf, |
| 57 | footer, |
| 58 | ); |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn footer_matches_goldens_at_blocker_sizes() { |
| 64 | let fixture = working(); |
| 65 | for (w, h) in BLOCKER_SIZES { |
| 66 | let footer = fixture.widget(&UI_THEME); |
| 67 | let rows = draw(w, h, &footer); |
| 68 | assert_matches_golden( |
| 69 | &format!("footer_{w}x{h}"), |
| 70 | &format!("{}\n", rows.trim_end()), |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// The dial chip with its key, the working clock, live counts, then the one |
| 76 | /// hint that applies now. |
| 77 | #[test] |
| 78 | fn posture_bar_reads_dial_clock_counts_hint() { |
| 79 | let text = draw(120, 30, &working().widget(&UI_THEME)); |
| 80 | let band = text.lines().last().unwrap_or_default().trim_end(); |
| 81 | assert_eq!( |
| 82 | band, |
| 83 | " ● ask Shift+Tab to change │ working 1m 15s 2 agents worked 41m 12s Esc to interrupt" |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | /// Experience mark 8: the current permission reads without color. With every |
| 88 | /// ink stripped the row still marks which chip is the permission in force |
| 89 | /// (`●`) and says what its key does, instead of printing two look-alike |
| 90 | /// `word (key)` chips that read as a menu of options. |
| 91 | #[test] |
| 92 | fn current_permission_is_identifiable_without_color() { |
| 93 | for (w, h) in BLOCKER_SIZES { |
| 94 | let text = draw(w, h, &working().widget(&UI_THEME)); |
| 95 | let band = text.lines().last().unwrap_or_default(); |
| 96 | assert!(band.starts_with(" ● ask"), "{w}x{h}: {band}"); |
| 97 | assert_eq!(band.matches('●').count(), 1, "{w}x{h}: {band}"); |
| 98 | assert!(band.contains("Shift+Tab to change"), "{w}x{h}: {band}"); |
| 99 | assert!(!band.contains("(Shift+Tab)"), "{w}x{h}: {band}"); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// The bar carries no cost and no context reading: the metrics line owns |
| 104 | /// both. The elapsed reading is this bar's again (#5914) — a fixed row is |
| 105 | /// the only place a glancing user can find it during a multi-hour session — |
| 106 | /// so it is asserted here rather than excluded. |
| 107 | #[test] |
| 108 | fn posture_bar_states_no_cost_or_context_reading() { |
| 109 | for pct in [0u8, 12, 61, 79] { |
| 110 | let mut fixture = working(); |
| 111 | fixture.context_percent = pct; |
| 112 | let text = draw(120, 32, &fixture.widget(&UI_THEME)); |
| 113 | assert!(!text.contains(&format!("{pct}%")), "{pct}: {text}"); |
| 114 | assert!(!text.contains("<·>"), "{text}"); |
| 115 | assert!(!text.contains('$'), "{text}"); |
| 116 | assert!(text.contains("working 1m 15s"), "{text}"); |
| 117 | assert!(text.contains("worked 41m 12s"), "{text}"); |
| 118 | assert!(text.contains("2 agents"), "{text}"); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /// At the cap the bar's hint slot says what to do, outranking the |
| 123 | /// interrupt hint; the reading itself still is not here. |
| 124 | #[test] |
| 125 | fn posture_bar_warns_at_eighty_percent_cap() { |
| 126 | let mut fixture = working(); |
| 127 | fixture.context_percent = 83; |
| 128 | let text = draw(100, 30, &fixture.widget(&UI_THEME)); |
| 129 | assert!(text.contains("▲ surface soon — /compact"), "{text}"); |
| 130 | assert!(!text.contains("Esc to interrupt"), "{text}"); |
| 131 | assert!(!text.contains("83%"), "{text}"); |
| 132 | } |
| 133 | |
| 134 | /// The cap warning outranks the clock: a working session that cannot start |
| 135 | /// its next turn needs the instruction more than it needs the stopwatch. |
| 136 | #[test] |
| 137 | fn cap_warning_outranks_the_clock_when_only_one_fits() { |
| 138 | let mut fixture = working(); |
| 139 | fixture.context_percent = 83; |
| 140 | let mut saw_warning_alone = false; |
| 141 | for width in 8..=160u16 { |
| 142 | let text = draw(width, 12, &fixture.widget(&UI_THEME)); |
| 143 | let warning = text.contains("surface soon"); |
| 144 | let clock = text.contains("worked 41m 12s"); |
| 145 | assert!( |
| 146 | !(clock && !warning), |
| 147 | "width {width} kept the clock and shed the cap warning: {text}" |
| 148 | ); |
| 149 | if warning && !clock { |
| 150 | saw_warning_alone = true; |
| 151 | } |
| 152 | } |
| 153 | assert!(saw_warning_alone, "no width shed the clock alone"); |
| 154 | } |
| 155 | |
| 156 | /// The right slot is the notice when one is owed, else the remote-control |
| 157 | /// state; it never covers the permission chip. |
| 158 | #[test] |
| 159 | fn posture_bar_pins_notice_or_remote_control_right() { |
| 160 | let mut fixture = working(); |
| 161 | fixture.right = Some(("/rc connected", ChromeInk::Info)); |
| 162 | let text = draw(100, 30, &fixture.widget(&UI_THEME)); |
| 163 | assert!(text.trim_end().ends_with("/rc connected"), "{text}"); |
| 164 | assert!(text.contains(" ● ask Shift+Tab to change"), "{text}"); |
| 165 | |
| 166 | fixture.right = Some(("Auto-denied exec_shell", ChromeInk::Attention)); |
| 167 | let text = draw(100, 30, &fixture.widget(&UI_THEME)); |
| 168 | assert!( |
| 169 | text.trim_end().ends_with("Auto-denied exec_shell"), |
| 170 | "{text}" |
| 171 | ); |
| 172 | |
| 173 | // Narrow: the notice truncates against the permission floor, never |
| 174 | // over it. |
| 175 | let narrow = draw(30, 12, &fixture.widget(&UI_THEME)); |
| 176 | assert!(narrow.contains(" ask"), "{narrow}"); |
| 177 | } |
| 178 | |
| 179 | /// Shed ladder, most expendable first: the session clock, the permission |
| 180 | /// key (a binding reminder, and the widest optional item), the hint, the |
| 181 | /// counts, the turn clock. The permission chip never sheds (#5796), and the |
| 182 | /// turn clock is the last live fact to go: a row that drops it for a static |
| 183 | /// hint leaves a working session looking dead (CW-8264). |
| 184 | #[test] |
| 185 | fn posture_bar_sheds_static_hints_before_the_turn_clock() { |
| 186 | let fixture = working(); |
| 187 | let narrowest_showing = |needle: &str| -> u16 { |
| 188 | (8..=160u16) |
| 189 | .filter(|w| draw(*w, 3, &fixture.widget(&UI_THEME)).contains(needle)) |
| 190 | .min() |
| 191 | .unwrap_or_else(|| panic!("{needle} never painted")) |
| 192 | }; |
| 193 | let turn_clock = narrowest_showing("working 1m 15s"); |
| 194 | let session_clock = narrowest_showing("worked 41m 12s"); |
| 195 | let hint = narrowest_showing("Esc to interrupt"); |
| 196 | let counts = narrowest_showing("2 agents"); |
| 197 | let permission_key = narrowest_showing("Shift+Tab to change"); |
| 198 | assert!( |
| 199 | session_clock > permission_key |
| 200 | && permission_key > hint |
| 201 | && hint > counts |
| 202 | && counts > turn_clock, |
| 203 | "turn_clock@{turn_clock} session_clock@{session_clock} hint@{hint} counts@{counts} permission_key@{permission_key}" |
| 204 | ); |
| 205 | for w in 8..=160u16 { |
| 206 | let text = draw(w, 3, &fixture.widget(&UI_THEME)); |
| 207 | assert!( |
| 208 | text.contains("ask"), |
| 209 | "{w}: the permission chip never sheds: {text}" |
| 210 | ); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /// `tui.posture_bar = "compact"` (#5950) starts the ladder past the clocks, |
| 215 | /// the hint and the counts at any width: the row states its posture — the |
| 216 | /// dial chip, and the cap warning when it is owed — and |
| 217 | /// nothing live. Width still sheds from there, and the right slot is |
| 218 | /// untouched. |
| 219 | #[test] |
| 220 | fn compact_posture_bar_states_posture_and_nothing_live() { |
| 221 | let mut fixture = working(); |
| 222 | fixture.right = Some(("/rc connected", ChromeInk::Info)); |
| 223 | let wide = draw(160, 3, &fixture.widget(&UI_THEME).compact(true)); |
| 224 | for kept in [" ● ask", "/rc connected"] { |
| 225 | assert!(wide.contains(kept), "compact keeps {kept}: {wide}"); |
| 226 | } |
| 227 | for gone in [ |
| 228 | "working 1m 15s", |
| 229 | "worked 41m 12s", |
| 230 | "Shift+Tab to change", |
| 231 | "2 agents", |
| 232 | "Esc to interrupt", |
| 233 | ] { |
| 234 | assert!(!wide.contains(gone), "compact drops {gone}: {wide}"); |
| 235 | } |
| 236 | // The full row at the same width is the row the user had before. |
| 237 | assert!(draw(160, 3, &fixture.widget(&UI_THEME)).contains("working 1m 15s")); |
| 238 | |
| 239 | // The cap warning is not a hint: a compact row still says what to do |
| 240 | // about a full context. |
| 241 | fixture.context_percent = 85; |
| 242 | let capped = draw(160, 3, &fixture.widget(&UI_THEME).compact(true)); |
| 243 | assert!(capped.contains("surface soon"), "{capped}"); |
| 244 | assert!(!capped.contains("Esc to interrupt"), "{capped}"); |
| 245 | |
| 246 | for w in 8..=160u16 { |
| 247 | let text = draw(w, 3, &fixture.widget(&UI_THEME).compact(true)); |
| 248 | assert!( |
| 249 | text.contains("ask"), |
| 250 | "{w}: the permission chip never sheds: {text}" |
| 251 | ); |
| 252 | assert!( |
| 253 | !text.contains("working") && !text.contains("agents"), |
| 254 | "{w}: nothing live in a compact row: {text}" |
| 255 | ); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /// The dial key prints only when the caller says the binding is live. |
| 260 | #[test] |
| 261 | fn posture_bar_prints_cycle_keys_only_when_live() { |
| 262 | let mut fixture = working(); |
| 263 | fixture.permission_key = None; |
| 264 | let text = draw(120, 30, &fixture.widget(&UI_THEME)); |
| 265 | assert!( |
| 266 | text.contains(" ● ask │ working 1m 15s 2 agents worked 41m 12s"), |
| 267 | "{text}" |
| 268 | ); |
| 269 | assert!(!text.contains('('), "{text}"); |
| 270 | } |
| 271 | |
| 272 | #[test] |
| 273 | fn posture_bar_ascii_safe_projects_glyphs() { |
| 274 | let mut fixture = working(); |
| 275 | fixture.context_percent = 90; |
| 276 | let text = draw(100, 30, &fixture.widget(&UI_THEME).ascii_safe(true)); |
| 277 | let band = text.lines().last().unwrap_or_default(); |
| 278 | assert!(band.starts_with(" . ask"), "inset preserved: {band}"); |
| 279 | assert!(text.contains("^ surface soon"), "{text}"); |
| 280 | for ch in text.chars() { |
| 281 | if ch != '\n' { |
| 282 | assert_eq!(ch.width(), Some(1), "ascii-safe single-width: {ch:?}"); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn posture_bar_degenerate_sizes_do_not_panic() { |
| 289 | for (w, h) in [(0u16, 0), (2, 1), (8, 1), (300, 2)] { |
| 290 | let fixture = working(); |
| 291 | let _ = draw(w, h, &fixture.widget(&UI_THEME)); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // --------------------------------------------------------------------------- |
| 296 | // Footer-hint retirement: each hint shows at 0 and 1 uses, and is gone at 2. |
| 297 | // These drive `tideline_footer_from_app` with a live session `App`, so the |
| 298 | // gating between the use counts and the facts is covered, not just the |
| 299 | // predicate in `footer_hints`. |
| 300 | // --------------------------------------------------------------------------- |
| 301 | |
| 302 | use super::tideline_footer_from_app; |
| 303 | use crate::tui::app::{App, OnboardingState}; |
| 304 | use crate::tui::footer_hints::{AGENT_ARROWS, ESC_INTERRUPT, PERMISSION_CYCLE}; |
| 305 | |
| 306 | fn session_app() -> App { |
| 307 | let mut app = crate::tui::test_support::test_app_with_options( |
| 308 | crate::tui::test_support::test_tui_options(std::path::PathBuf::from(".")), |
| 309 | ); |
| 310 | app.onboarding = OnboardingState::None; |
| 311 | app.launch.visible = false; |
| 312 | app.sandbox_backend = None; |
| 313 | app |
| 314 | } |
| 315 | |
| 316 | fn set_uses(app: &mut App, key: &str, uses: u8) { |
| 317 | if uses == 0 { |
| 318 | app.footer_hint_uses.remove(key); |
| 319 | } else { |
| 320 | app.footer_hint_uses.insert(key.to_string(), uses); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /// The dial chord prints while its binding is fresh, and the chip goes |
| 325 | /// bare — never away — once it has been used twice. The Plan stop is the |
| 326 | /// chip itself: there is no separate mode chip or Tab hint. |
| 327 | #[test] |
| 328 | fn cycle_keys_show_at_zero_and_one_use_and_go_bare_at_two() { |
| 329 | let mut app = session_app(); |
| 330 | app.approval_mode = codewhale_execpolicy::ApprovalMode::Suggest; |
| 331 | let facts = tideline_footer_from_app(&mut app, 120); |
| 332 | assert_eq!(facts.permission_key.as_deref(), Some("Shift+Tab to change")); |
| 333 | |
| 334 | set_uses(&mut app, PERMISSION_CYCLE, 1); |
| 335 | let facts = tideline_footer_from_app(&mut app, 120); |
| 336 | assert_eq!(facts.permission_key.as_deref(), Some("Shift+Tab to change")); |
| 337 | |
| 338 | set_uses(&mut app, PERMISSION_CYCLE, 2); |
| 339 | let facts = tideline_footer_from_app(&mut app, 120); |
| 340 | assert_eq!(facts.permission_key, None); |
| 341 | assert!( |
| 342 | !facts.permission_chip.0.is_empty(), |
| 343 | "the permission chip never retires with its key" |
| 344 | ); |
| 345 | |
| 346 | app.mode = codewhale_config::AppMode::Plan; |
| 347 | let facts = tideline_footer_from_app(&mut app, 120); |
| 348 | assert_eq!( |
| 349 | facts.permission_chip.0, |
| 350 | "Plan · files: read-only (unenforced)" |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | /// A running turn advertises the interrupt affordance until Esc has been |
| 355 | /// used to interrupt twice. |
| 356 | #[test] |
| 357 | fn interrupt_hint_shows_at_zero_and_one_use_and_clears_at_two() { |
| 358 | let mut app = session_app(); |
| 359 | app.is_loading = true; |
| 360 | let facts = tideline_footer_from_app(&mut app, 120); |
| 361 | let (text, _) = facts.hint.as_ref().expect("a running turn names Esc"); |
| 362 | assert!(text.contains("Esc"), "{text}"); |
| 363 | |
| 364 | set_uses(&mut app, ESC_INTERRUPT, 1); |
| 365 | assert!(tideline_footer_from_app(&mut app, 120).hint.is_some()); |
| 366 | |
| 367 | set_uses(&mut app, ESC_INTERRUPT, 2); |
| 368 | assert!(tideline_footer_from_app(&mut app, 120).hint.is_none()); |
| 369 | } |
| 370 | |
| 371 | /// #6502: while the workbar, vim Insert or agent focus owns Esc, Esc does |
| 372 | /// not interrupt, so the posture bar must not promise an interrupt. |
| 373 | #[test] |
| 374 | fn interrupt_hint_stands_down_while_the_workbar_owns_esc() { |
| 375 | let mut app = session_app(); |
| 376 | app.is_loading = true; |
| 377 | let names_esc = |app: &mut App| { |
| 378 | tideline_footer_from_app(app, 120) |
| 379 | .hint |
| 380 | .is_some_and(|(text, _)| text.contains("Esc")) |
| 381 | }; |
| 382 | assert!(names_esc(&mut app)); |
| 383 | app.work_surface.focused = true; |
| 384 | assert!(!names_esc(&mut app)); |
| 385 | app.work_surface.focused = false; |
| 386 | app.composer.vim_enabled = true; |
| 387 | app.composer.vim_mode = crate::tui::app::VimMode::Insert; |
| 388 | assert!(!names_esc(&mut app)); |
| 389 | app.composer.vim_mode = crate::tui::app::VimMode::Normal; |
| 390 | app.agent_focus = Some(crate::tui::agent_focus::AgentFocus::new( |
| 391 | "worker-1".to_string(), |
| 392 | "worker-1".to_string(), |
| 393 | )); |
| 394 | assert!(!names_esc(&mut app)); |
| 395 | } |
| 396 | |
| 397 | fn completed_subagent(id: &str) -> codewhale_engine::tools::subagent::SubAgentResult { |
| 398 | codewhale_engine::tools::subagent::SubAgentResult { |
| 399 | usage: None, |
| 400 | name: id.to_string(), |
| 401 | agent_id: id.to_string(), |
| 402 | context_mode: "fresh".to_string(), |
| 403 | fork_context: false, |
| 404 | workspace: None, |
| 405 | git_branch: None, |
| 406 | agent_type: codewhale_agent_contract::role::FleetRole::Worker, |
| 407 | assignment: codewhale_engine::tools::subagent::SubAgentAssignment { |
| 408 | native_preset: None, |
| 409 | objective: format!("objective-{id}"), |
| 410 | role: Some("worker".to_string()), |
| 411 | }, |
| 412 | model: String::new(), |
| 413 | nickname: None, |
| 414 | status: codewhale_engine::tools::subagent::SubAgentStatus::Completed, |
| 415 | worker_status: None, |
| 416 | runtime_permissions: None, |
| 417 | parent_run_id: None, |
| 418 | spawn_depth: 0, |
| 419 | child_route: None, |
| 420 | result: None, |
| 421 | steps_taken: 0, |
| 422 | checkpoint: None, |
| 423 | needs_input: None, |
| 424 | duration_ms: 0, |
| 425 | started_at: None, |
| 426 | from_prior_session: false, |
| 427 | idle_ms: None, |
| 428 | heartbeat_timeout_ms: None, |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /// The empty composer lends its arrows to the roster until those shortcuts |
| 433 | /// have been used twice. The roster entry is a finished agent: live progress |
| 434 | /// would put the phase back to Working, where the interrupt hint outranks. |
| 435 | #[test] |
| 436 | fn agent_arrow_hints_show_at_zero_and_one_use_and_clear_at_two() { |
| 437 | let mut app = session_app(); |
| 438 | app.subagent_cache.push(completed_subagent("agent-a")); |
| 439 | assert!( |
| 440 | tideline_footer_from_app(&mut app, 120).hint.is_some(), |
| 441 | "the empty composer lends its arrows to the roster" |
| 442 | ); |
| 443 | |
| 444 | set_uses(&mut app, AGENT_ARROWS, 1); |
| 445 | assert!(tideline_footer_from_app(&mut app, 120).hint.is_some()); |
| 446 | |
| 447 | set_uses(&mut app, AGENT_ARROWS, 2); |
| 448 | assert!(tideline_footer_from_app(&mut app, 120).hint.is_none()); |
| 449 | } |
| 450 | |
| 451 | /// Esc Esc rewinds the conversation and leaves the files as they are. The |
| 452 | /// notice gets half the row and loses whole trailing sentences that do not |
| 453 | /// fit, so the file fact comes first: it is still on screen at 80 columns, |
| 454 | /// and the pointer to `/undo` joins it once the row is wide enough. The |
| 455 | /// notice used to read `Rewound to previous user message` and said nothing |
| 456 | /// about files. |
| 457 | #[test] |
| 458 | fn rewind_notice_states_the_file_fact_first_and_names_restore_when_it_fits() { |
| 459 | use crate::tui::history::HistoryCell; |
| 460 | let mut app = session_app(); |
| 461 | app.add_message(HistoryCell::User { |
| 462 | content: "make whole hours read 2h".to_string(), |
| 463 | }); |
| 464 | app.add_message(HistoryCell::Assistant { |
| 465 | content: "Updated duration.mjs".to_string(), |
| 466 | streaming: false, |
| 467 | }); |
| 468 | |
| 469 | crate::tui::ui::apply_backtrack(&mut app, 0); |
| 470 | |
| 471 | assert_eq!(app.input, "make whole hours read 2h"); |
| 472 | let shown = |app: &mut App, width: u16| { |
| 473 | tideline_footer_from_app(app, width) |
| 474 | .right |
| 475 | .map(|(text, _)| text) |
| 476 | }; |
| 477 | assert_eq!(shown(&mut app, 80).as_deref(), Some("Files not changed.")); |
| 478 | assert_eq!( |
| 479 | shown(&mut app, 96).as_deref(), |
| 480 | Some("Files not changed. Use /restore.") |
| 481 | ); |
| 482 | assert_eq!( |
| 483 | shown(&mut app, 124).as_deref(), |
| 484 | Some("Files not changed. Use /restore. Conversation rewound.") |
| 485 | ); |
| 486 | } |
| 487 | |
| 488 | // --------------------------------------------------------------------------- |
| 489 | // The working clock (#5914). The founder watching a multi-hour operate |
| 490 | // session could not find how long the thing had been working: the classic |
| 491 | // footer's `worked` chip went with the legacy footer path (146ab7f756) and |
| 492 | // the phase band's `working_detail` went with the 0.9.12 merged shell |
| 493 | // (329960fcbf). These pin what came back — both readings, the state word |
| 494 | // that says what the clock is counting, and its place in the shed ladder. |
| 495 | // --------------------------------------------------------------------------- |
| 496 | |
| 497 | use std::time::{Duration, Instant}; |
| 498 | |
| 499 | use super::{ShellPhase, working_clock}; |
| 500 | |
| 501 | /// A live turn states both readings: what the session is doing now and for |
| 502 | /// how long, then how long it has worked in total. |
| 503 | #[test] |
| 504 | fn live_turn_clock_states_the_turn_and_the_session() { |
| 505 | let mut app = session_app(); |
| 506 | app.ui_locale = codewhale_localization::Locale::En; |
| 507 | app.is_loading = true; |
| 508 | app.turn_started_at = Some(Instant::now() - Duration::from_secs(75)); |
| 509 | app.cumulative_turn_duration = Duration::from_secs(2_400); |
| 510 | |
| 511 | let facts = tideline_footer_from_app(&mut app, 160); |
| 512 | let (turn, turn_ink) = facts.turn_clock.expect("a live turn states its elapsed"); |
| 513 | assert_eq!(turn, "working 1m 15s"); |
| 514 | assert_eq!(turn_ink, ChromeInk::Active); |
| 515 | let (session, session_ink) = facts |
| 516 | .session_clock |
| 517 | .expect("a working session states its total"); |
| 518 | assert_eq!(session, "worked 41m 15s"); |
| 519 | assert_eq!(session_ink, ChromeInk::Active); |
| 520 | } |
| 521 | |
| 522 | /// The session reading is model work, not wall clock: it is the sum of |
| 523 | /// finished turns plus the live one, so it never jumps at `TurnComplete`. |
| 524 | #[test] |
| 525 | fn session_reading_carries_finished_turns_plus_the_live_one() { |
| 526 | let mut app = session_app(); |
| 527 | app.ui_locale = codewhale_localization::Locale::En; |
| 528 | app.cumulative_turn_duration = Duration::from_secs(3_600); |
| 529 | |
| 530 | // Turn in flight: the finished total plus this turn. |
| 531 | app.is_loading = true; |
| 532 | app.turn_started_at = Some(Instant::now() - Duration::from_secs(30)); |
| 533 | let (_, live) = working_clock(&app, ShellPhase::Working, "working"); |
| 534 | assert_eq!(live.expect("live session clock").0, "worked 60m 30s"); |
| 535 | |
| 536 | // Turn done: the engine folded it into the cumulative total, so the |
| 537 | // reading is unchanged and the clock stops. |
| 538 | app.is_loading = false; |
| 539 | app.turn_started_at = None; |
| 540 | app.cumulative_turn_duration = Duration::from_secs(3_630); |
| 541 | let facts = tideline_footer_from_app(&mut app, 160); |
| 542 | assert!(facts.turn_clock.is_none(), "no turn, no turn clock"); |
| 543 | let (idle, ink) = facts |
| 544 | .session_clock |
| 545 | .expect("an idle session still states its total"); |
| 546 | assert_eq!(idle, "worked 60m 30s"); |
| 547 | assert_eq!(ink, ChromeInk::MetadataValue, "a stopped clock reads quiet"); |
| 548 | } |
| 549 | |
| 550 | /// #6041: a session's first long turn must not print one duration twice. |
| 551 | /// The turn half names the phase; the session half returns only once a |
| 552 | /// finished turn makes the totals different. |
| 553 | #[test] |
| 554 | fn first_turn_does_not_repeat_the_turn_duration_as_a_session_total() { |
| 555 | let mut app = session_app(); |
| 556 | app.ui_locale = codewhale_localization::Locale::En; |
| 557 | app.is_loading = true; |
| 558 | app.turn_started_at = Some(Instant::now() - Duration::from_secs(22 * 60 + 39)); |
| 559 | |
| 560 | let facts = tideline_footer_from_app(&mut app, 160); |
| 561 | assert_eq!( |
| 562 | facts.turn_clock.expect("the turn clock states the phase").0, |
| 563 | "working 22m 39s" |
| 564 | ); |
| 565 | assert!( |
| 566 | facts.session_clock.is_none(), |
| 567 | "a session total that repeats the turn reading is furniture (#6041)" |
| 568 | ); |
| 569 | |
| 570 | // One finished turn later the two readings differ, and both are honest. |
| 571 | app.cumulative_turn_duration = Duration::from_secs(14 * 60); |
| 572 | let facts = tideline_footer_from_app(&mut app, 160); |
| 573 | assert_eq!( |
| 574 | facts |
| 575 | .session_clock |
| 576 | .expect("a finished total earns the chip") |
| 577 | .0, |
| 578 | "worked 36m 39s" |
| 579 | ); |
| 580 | } |
| 581 | |
| 582 | /// Actively working, waiting on a sub-agent, and waiting on you are three |
| 583 | /// different readings — a bare duration cannot tell them apart. |
| 584 | #[test] |
| 585 | fn clock_distinguishes_working_from_waiting_on_something() { |
| 586 | let mut app = session_app(); |
| 587 | app.ui_locale = codewhale_localization::Locale::En; |
| 588 | app.is_loading = true; |
| 589 | app.turn_started_at = Some(Instant::now() - Duration::from_secs(75)); |
| 590 | app.cumulative_turn_duration = Duration::from_secs(2_400); |
| 591 | |
| 592 | let working = tideline_footer_from_app(&mut app, 160) |
| 593 | .turn_clock |
| 594 | .expect("working clock"); |
| 595 | assert_eq!(working.0, "working 1m 15s"); |
| 596 | assert_eq!(working.1, ChromeInk::Active); |
| 597 | |
| 598 | // A live sub-agent: the parent turn's clock keeps running, but the word |
| 599 | // says what it is waiting on (#5906 is the same founder session). |
| 600 | app.agent_progress |
| 601 | .insert("agent-a".to_string(), "reading".to_string()); |
| 602 | let subagents = tideline_footer_from_app(&mut app, 160) |
| 603 | .turn_clock |
| 604 | .expect("sub-agent clock"); |
| 605 | assert_eq!(subagents.0, "agents underway 1m 15s"); |
| 606 | app.agent_progress.clear(); |
| 607 | |
| 608 | // Waiting on the user parks the clock in the waiting ink. |
| 609 | app.pending_user_input_prompt = Some(( |
| 610 | "tool-call-1".to_string(), |
| 611 | codewhale_engine::tools::user_input::UserInputRequest { |
| 612 | require_typed_answers: false, |
| 613 | questions: Vec::new(), |
| 614 | }, |
| 615 | )); |
| 616 | let waiting = tideline_footer_from_app(&mut app, 160) |
| 617 | .turn_clock |
| 618 | .expect("waiting clock"); |
| 619 | assert_eq!(waiting.0, "needs you 1m 15s"); |
| 620 | assert_eq!(waiting.1, ChromeInk::Waiting); |
| 621 | assert_ne!(waiting.1, working.1, "waiting must not read as working"); |
| 622 | |
| 623 | app.turn_started_at = None; |
| 624 | let waiting = tideline_footer_from_app(&mut app, 40).turn_clock; |
| 625 | assert_eq!(waiting, Some(("needs you".into(), ChromeInk::Waiting))); |
| 626 | } |
| 627 | |
| 628 | /// A fresh session says nothing: no turn has run, so there is no clock to |
| 629 | /// state, and a sub-minute total stays quiet rather than rendering a noisy |
| 630 | /// `worked 4s` that immediately ticks. |
| 631 | #[test] |
| 632 | fn a_session_that_has_not_worked_states_no_clock() { |
| 633 | let mut app = session_app(); |
| 634 | app.ui_locale = codewhale_localization::Locale::En; |
| 635 | let facts = tideline_footer_from_app(&mut app, 160); |
| 636 | assert!(facts.turn_clock.is_none()); |
| 637 | assert!(facts.session_clock.is_none()); |
| 638 | |
| 639 | app.cumulative_turn_duration = Duration::from_secs(42); |
| 640 | assert!( |
| 641 | tideline_footer_from_app(&mut app, 160) |
| 642 | .session_clock |
| 643 | .is_none(), |
| 644 | "under a minute the session total stays quiet" |
| 645 | ); |
| 646 | |
| 647 | // A live turn states its own elapsed from the first second, though — the |
| 648 | // whole point is telling a stuck turn from a working one. |
| 649 | app.is_loading = true; |
| 650 | app.turn_started_at = Some(Instant::now() - Duration::from_secs(3)); |
| 651 | let facts = tideline_footer_from_app(&mut app, 160); |
| 652 | assert_eq!( |
| 653 | facts.turn_clock.expect("a live turn always times itself").0, |
| 654 | "working 3s" |
| 655 | ); |
| 656 | assert!( |
| 657 | facts.session_clock.is_none(), |
| 658 | "the sub-minute total is still quiet" |
| 659 | ); |
| 660 | } |
| 661 | |
| 662 | /// Narrow terminals shed the session clock, then the hint, then the counts, |
| 663 | /// and the turn clock only after all of them; neither reading is ever |
| 664 | /// painted half-truncated. |
| 665 | #[test] |
| 666 | fn narrow_widths_keep_the_turn_clock_past_the_hint_and_counts() { |
| 667 | let fixture = working(); |
| 668 | for w in 8..=160u16 { |
| 669 | let text = draw(w, 3, &fixture.widget(&UI_THEME)); |
| 670 | let turn = text.contains("working 1m 15s"); |
| 671 | let session = text.contains("worked 41m 12s"); |
| 672 | assert!( |
| 673 | !session || text.contains("Esc to interrupt"), |
| 674 | "{w} kept the session clock and dropped the hint: {text}" |
| 675 | ); |
| 676 | assert!( |
| 677 | !text.contains("Esc to interrupt") || text.contains("2 agents"), |
| 678 | "{w} kept the hint and dropped the counts: {text}" |
| 679 | ); |
| 680 | assert!( |
| 681 | !text.contains("2 agents") || turn, |
| 682 | "{w} kept the counts and dropped the turn clock: {text}" |
| 683 | ); |
| 684 | // Whole readings or none: a clipped duration is a lie. |
| 685 | assert!( |
| 686 | !text.contains("working 1m") || turn, |
| 687 | "{w} painted a half-truncated turn clock: {text}" |
| 688 | ); |
| 689 | assert!( |
| 690 | !text.contains("worked 41m") || session, |
| 691 | "{w} painted a half-truncated session clock: {text}" |
| 692 | ); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | #[test] |
| 697 | fn a_working_turn_keeps_its_clock_on_narrow_rows() { |
| 698 | use crate::tui::app::{StatusToast, StatusToastLevel}; |
| 699 | let footer = |width: u16, notice: bool| { |
| 700 | let mut app = session_app(); |
| 701 | app.ui_locale = codewhale_localization::Locale::En; |
| 702 | app.approval_mode = codewhale_execpolicy::ApprovalMode::Bypass; |
| 703 | app.is_loading = true; |
| 704 | app.turn_started_at = Some(Instant::now() - Duration::from_secs(3)); |
| 705 | if notice { |
| 706 | app.push_status_toast_record( |
| 707 | StatusToast::new( |
| 708 | "MCP · failed: launch-proof · /mcp", |
| 709 | StatusToastLevel::Warning, |
| 710 | Some(12_000), |
| 711 | ) |
| 712 | .with_fallbacks(vec!["MCP · 1 failed · /mcp".to_string()]), |
| 713 | ); |
| 714 | } |
| 715 | let facts = tideline_footer_from_app(&mut app, width); |
| 716 | draw(width, 3, &facts.widget(&app.ui_theme, false)) |
| 717 | }; |
| 718 | for (width, notice, kept) in [ |
| 719 | ( |
| 720 | 80u16, |
| 721 | true, |
| 722 | vec!["working 3s", "Esc to interrupt", "MCP · 1 failed · /mcp"], |
| 723 | ), |
| 724 | (60, true, vec!["working 3s", "MCP · 1 failed · /mcp"]), |
| 725 | (60, false, vec!["working 3s", "Esc to interrupt"]), |
| 726 | (40, true, vec!["working 3s"]), |
| 727 | ] { |
| 728 | let text = footer(width, notice); |
| 729 | for needle in kept { |
| 730 | assert!(text.contains(needle), "{width}: lost {needle}: {text}"); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | #[test] |
| 736 | fn scheduled_count_opens_automations_directly() { |
| 737 | let mut app = crate::tui::test_support::test_app_with_options( |
| 738 | crate::tui::test_support::test_tui_options("."), |
| 739 | ); |
| 740 | app.automation_panel.active_automations = 2; |
| 741 | let facts = super::tideline_footer_from_app(&mut app, 140); |
| 742 | assert_eq!( |
| 743 | facts.count_actions.last(), |
| 744 | Some(&crate::tui::tideline::InteractionAction::OpenAutomations) |
| 745 | ); |
| 746 | } |
| 747 | |
| 748 | #[test] |
| 749 | fn posture_shortcuts_recede_without_changing_count_click_targets() { |
| 750 | use ratatui::{buffer::Buffer, style::Modifier}; |
| 751 | let fixture = working(); |
| 752 | let mut buf = Buffer::empty(Rect::new(0, 0, 120, 1)); |
| 753 | let targets = render_tideline_footer(buf.area, &mut buf, &fixture.widget(&UI_THEME)); |
| 754 | let text: String = buf.content().iter().map(|cell| cell.symbol()).collect(); |
| 755 | let column = |needle: &str| text[..text.find(needle).unwrap()].chars().count() as u16; |
| 756 | let label = column("ask"); |
| 757 | let key = column("Shift+Tab to change"); |
| 758 | assert!(buf[(label, 0)].modifier.contains(Modifier::BOLD)); |
| 759 | assert!(!buf[(key, 0)].modifier.contains(Modifier::BOLD)); |
| 760 | assert_eq!(buf[(key, 0)].fg, UI_THEME.text_muted); |
| 761 | assert_ne!(buf[(label, 0)].fg, buf[(key, 0)].fg); |
| 762 | assert_eq!(targets.len(), 1); |
| 763 | let (index, target) = targets[0]; |
| 764 | assert_eq!(index, 0); |
| 765 | let painted: String = (target.x..target.right()) |
| 766 | .map(|x| buf[(x, 0)].symbol()) |
| 767 | .collect(); |
| 768 | assert_eq!(painted, "2 agents"); |
| 769 | } |
| 770 |