| 1 | //! Real-PTY regression for the active composer's painted `[↵]` submit |
| 2 | //! affordance (#5773, TUI-UX-01 active-work half). |
| 3 | //! |
| 4 | //! The session is driven through the real onboarding flow into deterministic |
| 5 | //! offline-explore mode, so bare submit resolves to Queue: no provider, no |
| 6 | //! credentials, no network. The user types a draft and clicks the painted |
| 7 | //! send cell with SGR mouse down/up. The click must reach the same queued |
| 8 | //! keyboard-submit path as Enter — proven by the offline queue receipt toast |
| 9 | //! and the durable `Queued #1:` pending-input preview, which renders directly |
| 10 | //! from `app.queued_messages` — rather than leaving the draft untouched as a |
| 11 | //! no-op click. |
| 12 | |
| 13 | #![cfg(all(unix, feature = "long-running-tests"))] |
| 14 | |
| 15 | use std::time::{Duration, Instant}; |
| 16 | |
| 17 | use super::qa_harness; |
| 18 | use qa_harness::Frame; |
| 19 | use qa_harness::harness::{Harness, make_sealed_workspace}; |
| 20 | use qa_harness::keys; |
| 21 | use qa_harness::modes::mode; |
| 22 | |
| 23 | /// (rows, cols): the release acceptance matrix for compact through wide terminals. |
| 24 | const SIZES: [(u16, u16); 5] = [(12, 40), (16, 60), (24, 80), (32, 100), (40, 140)]; |
| 25 | |
| 26 | const STARTUP_WAIT: Duration = Duration::from_secs(15); |
| 27 | const SETTLE_WAIT: Duration = Duration::from_secs(5); |
| 28 | |
| 29 | /// Offline-queue receipt toast (MessageId::ToastQueuedOffline, en). |
| 30 | const QUEUE_RECEIPT_TOAST: &str = "Saved for later. Connect a provider to send."; |
| 31 | /// Compact-width prefix of that toast: the footer truncates the tail when the |
| 32 | /// row cannot hold the full sentence next to the posture chips (deterministic |
| 33 | /// layout boundary, observed painted as "Saved for later." at 40 and 60 |
| 34 | /// cols). |
| 35 | const QUEUE_RECEIPT_TOAST_COMPACT: &str = "Saved for later."; |
| 36 | |
| 37 | /// The receipt needle this width can actually paint: full toast where the |
| 38 | /// footer row fits it, its unwrapped prefix on compact layouts. |
| 39 | fn queue_receipt_needle(cols: u16) -> &'static str { |
| 40 | if cols >= 100 { |
| 41 | QUEUE_RECEIPT_TOAST |
| 42 | } else { |
| 43 | QUEUE_RECEIPT_TOAST_COMPACT |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn active_composer_pointer_submit_queues_without_provider() { |
| 49 | for (rows, cols) in SIZES { |
| 50 | run_pointer_submit_case(rows, cols); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn run_pointer_submit_case(rows: u16, cols: u16) { |
| 55 | let size = format!("{cols}x{rows}"); |
| 56 | let workspace = make_sealed_workspace().expect("sealed workspace"); |
| 57 | std::fs::write(workspace.home().join(".codewhale/.onboarded"), "").expect("onboarded marker"); |
| 58 | workspace.trust_workspace().expect("trust workspace"); |
| 59 | |
| 60 | let mut tui = Harness::builder(Harness::codewhale_binary()) |
| 61 | .cwd(workspace.workspace()) |
| 62 | .clear_env() |
| 63 | .seal_home(workspace.home()) |
| 64 | .env("CODEWHALE_DISABLE_MODELS_DEV_FETCH", "1") |
| 65 | .env("CODEWHALE_NO_UPDATE_CHECK", "1") |
| 66 | .env("NO_ANIMATIONS", "1") |
| 67 | .env("RUST_LOG", "warn") |
| 68 | .args([ |
| 69 | "--workspace", |
| 70 | workspace.workspace().to_str().expect("workspace UTF-8"), |
| 71 | "--no-project-config", |
| 72 | "--fresh", |
| 73 | "--mouse-capture", |
| 74 | ]) |
| 75 | .size(rows, cols) |
| 76 | .spawn() |
| 77 | .expect("start distributed TUI binary"); |
| 78 | |
| 79 | // PTY reads can split a redraw: the launch header arrives before the |
| 80 | // composer, with onboarding rows still on screen (Buildkite #1861/#1867). |
| 81 | // Wait for the input surface as well as the header before asserting it. |
| 82 | tui.wait_for( |
| 83 | |frame| { |
| 84 | let text = frame.text(); |
| 85 | text.contains("New session") && text.contains('❯') && !text.contains("You're ready.") |
| 86 | }, |
| 87 | STARTUP_WAIT, |
| 88 | ) |
| 89 | .unwrap_or_else(|error| panic!("{size}: show the launch card and composer: {error}")); |
| 90 | tui.pump(); |
| 91 | assert_startup_contract(tui.frame(), rows, cols, &size); |
| 92 | super::launch_card_pty::explore_offline_from_composer(&mut tui); |
| 93 | tui.wait_for_idle(Duration::from_millis(300), SETTLE_WAIT) |
| 94 | .expect("session shell settles"); |
| 95 | // Prove the steady shell before clearing the onboarding seed. The clear |
| 96 | // action intentionally emits a transient `Ctrl+Z restores` receipt, and |
| 97 | // live receipts outrank the footer's key hint while present. |
| 98 | tui.pump(); |
| 99 | assert_live_shell_contract(tui.frame(), cols, &size); |
| 100 | |
| 101 | // Clear the onboarding seed so the unique pointer-test draft starts from |
| 102 | // an empty composer. |
| 103 | tui.send(keys::key::ctrl('u')).expect("clear seeded input"); |
| 104 | tui.wait_for_idle(Duration::from_millis(200), SETTLE_WAIT) |
| 105 | .expect("composer cleared"); |
| 106 | |
| 107 | // Mouse capture must be live before the click, or the SGR bytes would |
| 108 | // test nothing. Readiness comes from the control stream, not the frame. |
| 109 | wait_for_mouse_capture(&mut tui, &size); |
| 110 | |
| 111 | // Baseline the queue depth while the composer is empty: the pending |
| 112 | // preview row paints only then, and the later proof needs the exact |
| 113 | // pre-click depth. |
| 114 | let queued_before = queued_count(&normalized_text(tui.frame())); |
| 115 | |
| 116 | // Type a unique draft and let the composer settle before recording |
| 117 | // coordinates, so no late layout shift moves the cells under us. |
| 118 | let draft = format!("qa pointer draft {size}"); |
| 119 | tui.send(keys::key::text(&draft)).expect("type draft"); |
| 120 | wait_or_panic( |
| 121 | &mut tui, |
| 122 | &draft, |
| 123 | SETTLE_WAIT, |
| 124 | &format!("{size}: draft echo"), |
| 125 | ); |
| 126 | tui.wait_for_idle(Duration::from_millis(200), SETTLE_WAIT) |
| 127 | .expect("composer settles with draft"); |
| 128 | |
| 129 | // The paste-burst window treats input arriving within ~150ms of the |
| 130 | // keystrokes as paste; stay clearly outside it before clicking. |
| 131 | std::thread::sleep(Duration::from_millis(200)); |
| 132 | |
| 133 | tui.pump(); |
| 134 | let (send_row, send_col) = tui.frame().find_text("[↵]").unwrap_or_else(|| { |
| 135 | panic!( |
| 136 | "{size}: [↵] submit affordance not painted\n{}", |
| 137 | tui.diagnostics() |
| 138 | ) |
| 139 | }); |
| 140 | // Baseline: no queue receipt toast exists before the click, so the |
| 141 | // receipt below can only be produced by this gesture. (The onboarding |
| 142 | // seed may already sit in the offline queue; the unique draft cannot.) |
| 143 | let receipt = queue_receipt_needle(cols); |
| 144 | tui.pump(); |
| 145 | let before = normalized_text(tui.frame()); |
| 146 | assert!( |
| 147 | !before.contains(receipt), |
| 148 | "{size}: queue receipt already visible before any submit\n{}", |
| 149 | tui.diagnostics() |
| 150 | ); |
| 151 | |
| 152 | // Click the middle cell of the three-cell `[↵]` affordance: SGR down, |
| 153 | // settle, SGR up — the sequence a real terminal sends for one click. |
| 154 | tui.send(keys::mouse::down(send_row, send_col + 1)) |
| 155 | .expect("SGR mouse down on [↵]"); |
| 156 | tui.wait_for_idle(Duration::from_millis(150), Duration::from_secs(2)) |
| 157 | .expect("down settles"); |
| 158 | tui.send(keys::mouse::up(send_row, send_col + 1)) |
| 159 | .expect("SGR mouse up on [↵]"); |
| 160 | |
| 161 | // Distinguishing assertion: a real submit — the pointer click or keyboard |
| 162 | // Enter alike — consumes the draft into the deterministic offline queue |
| 163 | // and paints the queue receipt toast. A no-op click paints no receipt |
| 164 | // and never grows the queue. The receipt toast is transient, so either |
| 165 | // signal (toast seen, or the queue count grew) proves the dispatch. |
| 166 | let expected = queued_before.map(|n| n + 1); |
| 167 | let deadline = Instant::now() + qa_harness::harness::ci_scaled(SETTLE_WAIT); |
| 168 | let retry_at = Instant::now() + qa_harness::harness::ci_scaled(SETTLE_WAIT / 2); |
| 169 | let mut retried = false; |
| 170 | println!( |
| 171 | "POINTER DEBUG {size}: queued_before={queued_before:?} expected={expected:?} receipt={receipt:?}" |
| 172 | ); |
| 173 | loop { |
| 174 | tui.pump(); |
| 175 | let text = normalized_text(tui.frame()); |
| 176 | let seen = queued_count(&text); |
| 177 | // The click proves itself by either the receipt toast or the queue |
| 178 | // preview appearing (baseline absent -> something queued) or growing |
| 179 | // by exactly this draft (baseline visible -> baseline + 1). |
| 180 | let grew = match (queued_before, seen) { |
| 181 | (Some(before), Some(now)) => now == before + 1, |
| 182 | (None, Some(_)) => true, |
| 183 | _ => false, |
| 184 | }; |
| 185 | if text.contains(receipt) || grew { |
| 186 | println!( |
| 187 | "POINTER DEBUG {size}: broke with seen={seen:?} expected={expected:?} receipt_seen={}", |
| 188 | text.contains(receipt) |
| 189 | ); |
| 190 | break; |
| 191 | } |
| 192 | // One bounded retry at the half-way point: re-find the affordance |
| 193 | // (a redraw may have shifted cells between find and click under |
| 194 | // runner load) and click it again. Keep polling after it — the app |
| 195 | // may take a beat to process the second gesture. |
| 196 | if !retried && Instant::now() >= retry_at { |
| 197 | retried = true; |
| 198 | let (retry_row, retry_col) = tui.frame().find_text("[↵]").unwrap_or_else(|| { |
| 199 | panic!( |
| 200 | "{size}: [↵] submit affordance not painted on retry\n{}", |
| 201 | tui.diagnostics() |
| 202 | ) |
| 203 | }); |
| 204 | tui.send(keys::mouse::down(retry_row, retry_col + 1)) |
| 205 | .expect("SGR mouse down on [↵] retry"); |
| 206 | std::thread::sleep(Duration::from_millis(150)); |
| 207 | tui.send(keys::mouse::up(retry_row, retry_col + 1)) |
| 208 | .expect("SGR mouse up on [↵] retry"); |
| 209 | } |
| 210 | if Instant::now() >= deadline { |
| 211 | panic!( |
| 212 | "{size}: click on [↵] at ({send_row},{}) produced no queue receipt \ |
| 213 | {receipt:?} and no queue growth — pointer submit did not reach the \ |
| 214 | keyboard-submit dispatch path (seen={seen:?})\n{}", |
| 215 | send_col + 1, |
| 216 | tui.diagnostics() |
| 217 | ); |
| 218 | } |
| 219 | std::thread::sleep(Duration::from_millis(100)); |
| 220 | } |
| 221 | |
| 222 | // Durable queue proof at every size: the pending-input preview renders |
| 223 | // straight from `app.queued_messages` — the real queue state, no |
| 224 | // slash-menu navigation needed. The queue must still report at least |
| 225 | // the entry this gesture added (the 40-column floor truncates the |
| 226 | // preview before the draft text, so the entry count, not the text, is |
| 227 | // the signal at the floor). |
| 228 | tui.pump(); |
| 229 | let after = normalized_text(tui.frame()); |
| 230 | let grew = match (queued_count(&after), queued_before) { |
| 231 | (Some(after_n), Some(before_n)) => after_n == before_n + 1, |
| 232 | _ => after.contains("Queued "), |
| 233 | }; |
| 234 | assert!( |
| 235 | grew, |
| 236 | "{size}: the queue did not grow by exactly the pointer-submitted draft\n{}", |
| 237 | tui.diagnostics() |
| 238 | ); |
| 239 | |
| 240 | let modes = tui.terminal_modes(); |
| 241 | assert_eq!( |
| 242 | modes.state(mode::MOUSE_SGR), |
| 243 | Some(true), |
| 244 | "{size}: SGR mouse encoding must be enabled for the click to be meaningful\n{}", |
| 245 | modes.debug_dump() |
| 246 | ); |
| 247 | assert!( |
| 248 | modes.was_ever_enabled(mode::MOUSE_BUTTON), |
| 249 | "{size}: mouse button tracking was never enabled\n{}", |
| 250 | modes.debug_dump() |
| 251 | ); |
| 252 | |
| 253 | let _ = tui.shutdown(); |
| 254 | } |
| 255 | |
| 256 | /// Frame text with rows joined on single spaces, so assertions survive a |
| 257 | /// narrow-terminal wrap of the needle across two painted rows. |
| 258 | fn normalized_text(frame: &Frame) -> String { |
| 259 | frame |
| 260 | .text() |
| 261 | .lines() |
| 262 | .map(str::trim) |
| 263 | .filter(|line| !line.is_empty()) |
| 264 | .collect::<Vec<_>>() |
| 265 | .join(" ") |
| 266 | } |
| 267 | |
| 268 | fn assert_startup_contract(frame: &Frame, rows: u16, cols: u16, size: &str) { |
| 269 | let text = frame.text(); |
| 270 | // The launch card's own truth: the wordmark + version, the prominent |
| 271 | // new-session entry, and the focused composer. The posture bar and |
| 272 | // metrics line appear only once a session exists, so `context` is NOT |
| 273 | // asserted here any more (SHELL-DESIGN-20260901 Round 5). |
| 274 | for needle in ["codewhale", "❯"] { |
| 275 | assert!( |
| 276 | text.contains(needle), |
| 277 | "{size}: startup misses {needle:?}\n{}", |
| 278 | frame.debug_dump() |
| 279 | ); |
| 280 | } |
| 281 | // Empty workspaces keep the invitation without an empty history section. |
| 282 | assert!( |
| 283 | text.contains("New session"), |
| 284 | "{size}: startup misses invitation\n{}", |
| 285 | frame.debug_dump() |
| 286 | ); |
| 287 | assert!( |
| 288 | !text.contains("No recent sessions"), |
| 289 | "{size}: empty history adds noise\n{}", |
| 290 | frame.debug_dump() |
| 291 | ); |
| 292 | for retired_mark_row in ["▄▄▄▄██▌", "▜████▀▘"] { |
| 293 | assert!( |
| 294 | !text.contains(retired_mark_row), |
| 295 | "{size}: startup still paints the retired approximate mark\n{}", |
| 296 | frame.debug_dump() |
| 297 | ); |
| 298 | } |
| 299 | assert_eq!(frame.rows(), rows, "{size}: PTY row count drift"); |
| 300 | assert_eq!(frame.cols(), cols, "{size}: PTY column count drift"); |
| 301 | assert!( |
| 302 | frame.max_row_width() <= usize::from(cols), |
| 303 | "{size}: startup row overflow\n{}", |
| 304 | frame.debug_dump() |
| 305 | ); |
| 306 | } |
| 307 | |
| 308 | fn assert_live_shell_contract(frame: &Frame, cols: u16, size: &str) { |
| 309 | let text = frame.text(); |
| 310 | // The bottom metrics row owns the model; repository state belongs to |
| 311 | // the launch header and git view. This sealed offline session carries no |
| 312 | // key, so the route chip says the model is not connected instead of |
| 313 | // naming a default route that cannot answer (experience mark 8, U3). That |
| 314 | // chip must remain visible even at 40 columns. |
| 315 | let metrics = frame.row(frame.rows().saturating_sub(1)); |
| 316 | assert!( |
| 317 | metrics.contains("model not connected"), |
| 318 | "{size}: live shell misses the model in the metrics line\n{}", |
| 319 | frame.debug_dump() |
| 320 | ); |
| 321 | // Compact metrics omit the passive help hint; F1 still opens Help. |
| 322 | assert!( |
| 323 | !text.contains("RUNS") || cols < 100, |
| 324 | "{size}: passive duplicate Tideline rail is still visible\n{}", |
| 325 | frame.debug_dump() |
| 326 | ); |
| 327 | assert!( |
| 328 | frame.max_row_width() <= usize::from(cols), |
| 329 | "{size}: live-shell row overflow\n{}", |
| 330 | frame.debug_dump() |
| 331 | ); |
| 332 | } |
| 333 | |
| 334 | /// The queue depth the frame's pending-input preview reports |
| 335 | /// (`Queued N · next: …`), when one is painted. |
| 336 | fn queued_count(text: &str) -> Option<u32> { |
| 337 | let idx = text.find("Queued ")?; |
| 338 | let rest = &text[idx + "Queued ".len()..]; |
| 339 | let digits: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect(); |
| 340 | digits.parse().ok() |
| 341 | } |
| 342 | |
| 343 | fn wait_or_panic(tui: &mut Harness, needle: &str, timeout: Duration, label: &str) { |
| 344 | if tui.wait_for_text(needle, timeout).is_err() { |
| 345 | panic!("{label}: {needle:?} not visible\n{}", tui.diagnostics()); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | fn wait_for_mouse_capture(tui: &mut Harness, size: &str) { |
| 350 | let deadline = Instant::now() + qa_harness::harness::ci_scaled(STARTUP_WAIT); |
| 351 | loop { |
| 352 | tui.pump(); |
| 353 | let modes = tui.terminal_modes(); |
| 354 | if modes.state(mode::MOUSE_SGR) == Some(true) |
| 355 | && modes.state(mode::MOUSE_BUTTON) == Some(true) |
| 356 | { |
| 357 | return; |
| 358 | } |
| 359 | if Instant::now() >= deadline { |
| 360 | panic!( |
| 361 | "{size}: mouse capture never enabled in the control stream\n{}", |
| 362 | tui.diagnostics() |
| 363 | ); |
| 364 | } |
| 365 | std::thread::sleep(Duration::from_millis(40)); |
| 366 | } |
| 367 | } |
| 368 |