use pretty_assertions::assert_eq;
use super::DaemonCompactTurn;
use crate::tui::{
daemon_events::{Rest, TurnState},
rewind_actions::RewindConfirmation,
tests::test_app,
ComposerMode, Entry, PickerItem, QueuedPrompt, StreamKind, UiPicker,
};
#[test]
fn a_replayed_continuation_keeps_the_stream_in_step_with_the_message() {
let mut app = test_app();
app.streams.assistant_stream.push_delta("Here:\n");
app.finish_streams();
app.continue_stream_with_replayed(StreamKind::Assistant, "```rust\nlet a = 1;\n");
let shown = match app.history.entries() {
[Entry::Assistant(reply)] => reply.text.clone(),
_ => String::new(),
};
let said = "Here:\n```rust\nlet a = 1;\n";
assert_eq!(
(
app.streams.code_fence(StreamKind::Assistant).is_open(),
app.streams.assistant_stream.emitted_text(),
shown.as_str(),
),
(true, said, said)
);
}
#[tokio::test]
async fn reset_replay_during_a_turn_places_its_start_after_the_rebuilt_rows() {
let mut app = test_app();
app.insert_entry(&Entry::User("prompt".into()));
app.show_daemon_turn(TurnState::Running).await;
app.clear_daemon_transcript();
app.insert_entry(&Entry::User("earlier".into()));
app.insert_entry(&Entry::Assistant("earlier reply".into()));
app.insert_entry(&Entry::User("prompt".into()));
app.show_daemon_turn(TurnState::Running).await;
app.show_daemon_turn(TurnState::Resting(Rest::Ready)).await;
assert!(matches!(
app.history.entries(),
[
Entry::User(_),
Entry::Assistant(earlier),
Entry::User(_),
Entry::Assistant(receipt),
] if earlier.worked_for.is_none() && receipt.worked_for.is_some()
));
}
#[tokio::test]
async fn a_failed_compaction_sends_the_follow_up_that_waited_for_it() {
let failed = || Rest::Failed {
error: "compaction failed".into(),
};
let cases = [
("failed compaction", true, failed(), true),
("cancelled compaction", true, Rest::Interrupted, false),
("failed model turn", false, failed(), false),
];
for (name, compaction, rest, sends) in cases {
let mut app = test_app();
app.show_daemon_turn(TurnState::Running).await;
if compaction {
app.daemon_compact_turn = Some(DaemonCompactTurn::default());
}
app.pending.push_follow_up(QueuedPrompt {
prompt: "parked".into(),
display_prompt: "parked".into(),
paste_segments: Vec::new(),
media: Vec::new(),
});
app.show_daemon_turn(TurnState::Resting(rest)).await;
assert_eq!(app.start_follow_ups, sends, "{name}");
}
}
fn linked_paths_after_checks(app: &mut crate::tui::App) -> usize {
let settings = crate::tui::history_cache::HistoryRenderSettings {
width: 80,
max_tool_output_lines: 10,
zen_mode: false,
show_reasoning_output: true,
theme_generation: 0,
max_image_height: crate::tui::feed_image::DEFAULT_IMAGE_HEIGHT,
path_icons: false,
};
let all = crate::tui::history_cache::HistoryLineSlice {
start: 0,
count: usize::MAX,
};
app.history.with_lines_and_images_mut(|cache, entries, _| {
let mut links = cache.visible_links(entries, settings, all, &|_, _| Vec::new());
while let Some(check) = cache.take_path_check() {
cache.apply_path_check(check.run());
links = cache.visible_links(entries, settings, all, &|_, _| Vec::new());
}
links.len()
})
}
#[tokio::test]
async fn every_turn_end_rechecks_paths_found_missing() {
let cases = [
("completed", false, Rest::Ready),
("compact", true, Rest::Ready),
("interrupted", false, Rest::Interrupted),
(
"failed",
false,
Rest::Failed {
error: "boom".into(),
},
),
];
for (name, compact, rest) in cases {
let workspace = tempfile::tempdir().expect("tempdir");
let mut app = test_app();
app.history.lines_mut().set_path_workspace(workspace.path());
app.show_daemon_turn(TurnState::Running).await;
if compact {
app.daemon_compact_turn = Some(DaemonCompactTurn::default());
}
app.insert_entry(&Entry::Assistant("I will write src/new.rs next.".into()));
assert_eq!(linked_paths_after_checks(&mut app), 0, "{name}: before");
std::fs::create_dir_all(workspace.path().join("src")).expect("mkdir");
std::fs::write(workspace.path().join("src/new.rs"), "").expect("write");
app.show_daemon_turn(TurnState::Resting(rest)).await;
assert_eq!(linked_paths_after_checks(&mut app), 1, "{name}: after");
}
}
#[tokio::test]
async fn a_turn_end_leaves_no_rewind_confirmation_from_before_it() {
let mut app = test_app();
let stale = RewindConfirmation {
checkpoint: rho_protocol::TreeNodeId::new("checkpoint"),
tip: rho_protocol::TreeNodeId::new("tip"),
};
app.input_ui
.set_composer(ComposerMode::Picker(UiPicker::confirm_rewind(
"Confirm workspace rewind",
vec![PickerItem::new(
"Confirm workspace rewind".into(),
stale.value(),
)],
)));
let open_before = app.open_rewind_confirmation();
app.show_daemon_turn(TurnState::Resting(Rest::Ready)).await;
assert_eq!(
(open_before, app.open_rewind_confirmation()),
(Some(stale), None)
);
}