Files
codex/codex-rs/tui/src/chatwidget/tests/status_surface_previews.rs
T
Eric Traut 789b7e39dc Split ChatWidget state into focused modules (#21866)
## Summary

`ChatWidget` has been carrying several independent domains in one large
state bag: transcript bookkeeping, turn lifecycle, queued input, status
surfaces, connectors, review mode, and protocol dispatch. That makes
otherwise-local changes hard to reason about because unrelated fields
and side effects live beside each other in `chatwidget.rs`.

This is the first cleanup PR in a larger decomposition effort. It does
not try to make `chatwidget.rs` small in one sweep; instead, it
establishes focused state boundaries that later handler, popup,
rendering, and effect-synchronization extractions can build on.

This PR keeps `ChatWidget` as the composition layer while moving focused
state into smaller `codex-tui` modules. The widget still owns effects
that touch the bottom pane, app events, command submission, redraw
scheduling, and terminal-title updates.

## Changes

- Add focused state modules under `codex-rs/tui/src/chatwidget/` for
input queues, turn lifecycle, transcript bookkeeping, status state,
connectors, review mode, and app-server protocol dispatch.
- Update `ChatWidget` to hold grouped state structs and route
input/lifecycle/status operations through those focused helpers.
- Move app-server notification dispatch into `chatwidget/protocol.rs`
while leaving feature handlers and side effects on `ChatWidget`.
- Replace the large manual `ChatWidget` test literal with the normal
constructor plus narrow test overrides, so future state moves do not
require every field to be restated in test setup.
- Update existing tests to access the new grouped state or narrower
helpers without changing snapshot behavior.

## Longer-term direction

Follow-up PRs can continue shrinking `chatwidget.rs` by moving behavior,
not just state, into focused modules:

- Extract input/submission flow, turn/stream handling, and tool-cell
lifecycles into domain modules that call the new state reducers.
- Move popup/settings builders and rendering helpers out of the main
widget file so `ChatWidget` stays focused on composition.
- Reduce direct `BottomPane` mutation by applying domain-specific sync
outputs at clearer boundaries.
2026-05-09 15:16:01 -07:00

280 lines
9.4 KiB
Rust

use super::*;
use crate::bottom_pane::preview_line_for_title_items;
use pretty_assertions::assert_eq;
use ratatui::text::Line;
fn line_text(line: Line<'static>) -> String {
line.spans
.into_iter()
.map(|span| span.content.into_owned())
.collect()
}
fn status_preview_line(chat: &mut ChatWidget, items: &[StatusLineItem]) -> String {
let preview_data = chat.status_surface_preview_data();
let preview = preview_data
.status_line_for_items(items.iter().copied(), /*use_theme_colors*/ true)
.expect("status preview line");
line_text(preview)
}
fn title_preview_line(chat: &mut ChatWidget, items: &[TerminalTitleItem]) -> String {
let preview_data = chat.terminal_title_preview_data();
let preview =
preview_line_for_title_items(items, &preview_data).expect("terminal title preview line");
line_text(preview)
}
fn combined_preview_snapshot(
chat: &mut ChatWidget,
status_items: &[StatusLineItem],
title_items: &[TerminalTitleItem],
) -> String {
normalize_snapshot_paths(format!(
"status line: {}\nterminal title: {}",
status_preview_line(chat, status_items),
title_preview_line(chat, title_items),
))
}
fn status_line_popup_snapshot(chat: &mut ChatWidget) -> String {
chat.open_status_line_setup();
normalize_snapshot_paths(strip_osc8_for_snapshot(&render_bottom_popup(
chat, /*width*/ 100,
)))
}
fn terminal_title_popup_snapshot(chat: &mut ChatWidget) -> String {
chat.open_terminal_title_setup();
normalize_snapshot_paths(strip_osc8_for_snapshot(&render_bottom_popup(
chat, /*width*/ 100,
)))
}
fn cache_project_root(chat: &mut ChatWidget, root_name: &str) {
chat.status_line_project_root_name_cache = Some(CachedProjectRootName {
cwd: chat.config.cwd.to_path_buf(),
root_name: Some(root_name.to_string()),
});
}
#[tokio::test]
async fn status_surface_preview_lines_live_only_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
cache_project_root(&mut chat, "preview-live-root");
chat.status_line_branch = Some("feature/live-preview-branch".to_string());
chat.thread_name = Some("Live preview thread".to_string());
chat.transcript.last_plan_progress = Some((2, 5));
let snapshot = combined_preview_snapshot(
&mut chat,
&[
StatusLineItem::ProjectRoot,
StatusLineItem::GitBranch,
StatusLineItem::ThreadTitle,
],
&[
TerminalTitleItem::Project,
TerminalTitleItem::Thread,
TerminalTitleItem::GitBranch,
TerminalTitleItem::TaskProgress,
],
);
assert_chatwidget_snapshot!("status_surface_previews_live_only", snapshot);
}
#[tokio::test]
async fn status_line_setup_popup_live_only_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
cache_project_root(&mut chat, "preview-live-root");
chat.status_line_branch = Some("feature/live-preview-branch".to_string());
chat.thread_name = Some("Live preview thread".to_string());
chat.config.tui_status_line = Some(vec![
"project-name".to_string(),
"git-branch".to_string(),
"thread-title".to_string(),
]);
assert_chatwidget_snapshot!(
"status_line_setup_popup_live_only",
status_line_popup_snapshot(&mut chat)
);
}
#[tokio::test]
async fn status_surface_preview_lines_hardcoded_only_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let snapshot = combined_preview_snapshot(
&mut chat,
&[
StatusLineItem::ProjectRoot,
StatusLineItem::GitBranch,
StatusLineItem::ThreadTitle,
StatusLineItem::Permissions,
StatusLineItem::ApprovalMode,
],
&[
TerminalTitleItem::Thread,
TerminalTitleItem::GitBranch,
TerminalTitleItem::TaskProgress,
],
);
assert_chatwidget_snapshot!("status_surface_previews_hardcoded_only", snapshot);
}
#[tokio::test]
async fn thread_title_falls_back_to_thread_id_when_unnamed() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let thread_id = ThreadId::new();
chat.thread_id = Some(thread_id);
assert_eq!(
status_preview_line(&mut chat, &[StatusLineItem::ThreadTitle]),
thread_id.to_string()
);
assert_eq!(
title_preview_line(&mut chat, &[TerminalTitleItem::Thread]),
thread_id.to_string()
);
}
#[tokio::test]
async fn status_line_setup_popup_hardcoded_only_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.config.tui_status_line = Some(vec![
"project-name".to_string(),
"git-branch".to_string(),
"thread-title".to_string(),
]);
assert_chatwidget_snapshot!(
"status_line_setup_popup_hardcoded_only",
status_line_popup_snapshot(&mut chat)
);
}
#[tokio::test]
async fn status_surface_preview_lines_mixed_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.status_line_branch = Some("feature/mixed-preview".to_string());
chat.thread_name = Some("Mixed preview thread".to_string());
let snapshot = combined_preview_snapshot(
&mut chat,
&[
StatusLineItem::ProjectRoot,
StatusLineItem::GitBranch,
StatusLineItem::ThreadTitle,
],
&[
TerminalTitleItem::Project,
TerminalTitleItem::Thread,
TerminalTitleItem::TaskProgress,
],
);
assert_chatwidget_snapshot!("status_surface_previews_mixed", snapshot);
}
#[tokio::test]
async fn status_line_setup_popup_mixed_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.status_line_branch = Some("feature/mixed-preview".to_string());
chat.thread_name = Some("Mixed preview thread".to_string());
chat.config.tui_status_line = Some(vec![
"project-name".to_string(),
"git-branch".to_string(),
"thread-title".to_string(),
]);
assert_chatwidget_snapshot!(
"status_line_setup_popup_mixed",
status_line_popup_snapshot(&mut chat)
);
}
#[tokio::test]
async fn terminal_title_setup_popup_live_only_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
cache_project_root(&mut chat, "preview-live-root");
chat.status_line_branch = Some("feature/live-preview-branch".to_string());
chat.thread_name = Some("Live preview thread".to_string());
chat.transcript.last_plan_progress = Some((2, 5));
chat.config.tui_terminal_title = Some(vec![
"project-name".to_string(),
"thread-title".to_string(),
"git-branch".to_string(),
"task-progress".to_string(),
]);
assert_chatwidget_snapshot!(
"terminal_title_setup_popup_live_only",
terminal_title_popup_snapshot(&mut chat)
);
}
#[tokio::test]
async fn terminal_title_setup_popup_hardcoded_only_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.config.tui_terminal_title = Some(vec![
"thread-title".to_string(),
"git-branch".to_string(),
"task-progress".to_string(),
]);
assert_chatwidget_snapshot!(
"terminal_title_setup_popup_hardcoded_only",
terminal_title_popup_snapshot(&mut chat)
);
}
#[tokio::test]
async fn terminal_title_setup_popup_mixed_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.thread_name = Some("Mixed preview thread".to_string());
chat.config.tui_terminal_title = Some(vec![
"project-name".to_string(),
"thread-title".to_string(),
"task-progress".to_string(),
]);
assert_chatwidget_snapshot!(
"terminal_title_setup_popup_mixed",
terminal_title_popup_snapshot(&mut chat)
);
}
#[tokio::test]
async fn missing_project_root_uses_different_status_and_title_preview_sources() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let status_preview = status_preview_line(&mut chat, &[StatusLineItem::ProjectRoot]);
let title_preview = title_preview_line(&mut chat, &[TerminalTitleItem::Project]);
assert_eq!(status_preview, "my-project");
assert_eq!(title_preview, "project");
}
#[tokio::test]
async fn terminal_title_preview_uses_title_truncation_for_live_values() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let long_thread = "This thread title is intentionally much longer than forty-eight characters";
let long_branch = "feature/this-branch-name-is-intentionally-longer-than-thirty-two";
chat.thread_name = Some(long_thread.to_string());
chat.status_line_branch = Some(long_branch.to_string());
let preview = title_preview_line(
&mut chat,
&[TerminalTitleItem::Thread, TerminalTitleItem::GitBranch],
);
let truncated_thread =
ChatWidget::truncate_terminal_title_part(long_thread.to_string(), /*max_chars*/ 48);
let truncated_branch =
ChatWidget::truncate_terminal_title_part(long_branch.to_string(), /*max_chars*/ 32);
assert_eq!(preview, format!("{truncated_thread} | {truncated_branch}"));
}