216 lines
6.7 KiB
Rust
216 lines
6.7 KiB
Rust
mod common;
|
|
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
|
use insta::{assert_snapshot, with_settings};
|
|
use owlen_core::types::{Message, ToolCall};
|
|
use owlen_tui::ChatApp;
|
|
use owlen_tui::events::Event;
|
|
use owlen_tui::ui::render_chat;
|
|
use ratatui::{Terminal, backend::TestBackend};
|
|
|
|
use common::build_chat_app;
|
|
|
|
fn buffer_to_string(buffer: &ratatui::buffer::Buffer) -> String {
|
|
let mut output = String::new();
|
|
|
|
for y in 0..buffer.area.height {
|
|
output.push('"');
|
|
for x in 0..buffer.area.width {
|
|
output.push_str(buffer[(x, y)].symbol());
|
|
}
|
|
output.push('"');
|
|
output.push('\n');
|
|
}
|
|
|
|
output
|
|
}
|
|
|
|
fn render_snapshot(app: &mut ChatApp, width: u16, height: u16) -> String {
|
|
let backend = TestBackend::new(width, height);
|
|
let mut terminal = Terminal::new(backend).expect("terminal");
|
|
|
|
terminal
|
|
.draw(|frame| render_chat(frame, app))
|
|
.expect("render chat");
|
|
|
|
let buffer = terminal.backend().buffer();
|
|
buffer_to_string(buffer)
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn render_chat_idle_snapshot() {
|
|
let mut app_80 = build_chat_app(|_| {}, |_| {}).await;
|
|
with_settings!({ snapshot_suffix => "80x35" }, {
|
|
let snapshot = render_snapshot(&mut app_80, 80, 35);
|
|
assert_snapshot!("chat_idle_snapshot", snapshot);
|
|
});
|
|
|
|
let mut app_100 = build_chat_app(|_| {}, |_| {}).await;
|
|
with_settings!({ snapshot_suffix => "100x35" }, {
|
|
let snapshot = render_snapshot(&mut app_100, 100, 35);
|
|
assert_snapshot!("chat_idle_snapshot", snapshot);
|
|
});
|
|
|
|
let mut app_140 = build_chat_app(|_| {}, |_| {}).await;
|
|
with_settings!({ snapshot_suffix => "140x35" }, {
|
|
let snapshot = render_snapshot(&mut app_140, 140, 35);
|
|
assert_snapshot!("chat_idle_snapshot", snapshot);
|
|
});
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn render_chat_tool_call_snapshot() {
|
|
let mut app = build_chat_app(
|
|
|_| {},
|
|
|session| {
|
|
let conversation = session.conversation_mut();
|
|
conversation.push_user_message("What happened in the Rust ecosystem today?");
|
|
|
|
let stream_id = conversation.start_streaming_response();
|
|
conversation
|
|
.set_stream_placeholder(stream_id, "Consulting the knowledge base…")
|
|
.expect("placeholder");
|
|
|
|
let tool_call = ToolCall {
|
|
id: "call-search-1".into(),
|
|
name: "web_search".into(),
|
|
arguments: serde_json::json!({ "query": "Rust language news" }),
|
|
};
|
|
conversation
|
|
.set_tool_calls_on_message(stream_id, vec![tool_call.clone()])
|
|
.expect("tool call metadata");
|
|
conversation
|
|
.append_stream_chunk(stream_id, "Found multiple articles…", false)
|
|
.expect("stream chunk");
|
|
|
|
let tool_message = Message::tool(
|
|
tool_call.id.clone(),
|
|
"Rust 1.85 released with generics cleanups and faster async compilation."
|
|
.to_string(),
|
|
);
|
|
conversation.push_message(tool_message);
|
|
|
|
let assistant_summary = Message::assistant(
|
|
"Summarising the latest Rust release and the async runtime updates.".into(),
|
|
);
|
|
conversation.push_message(assistant_summary);
|
|
},
|
|
)
|
|
.await;
|
|
|
|
// Surface quota toast to exercise header/status rendering.
|
|
app.push_toast(
|
|
owlen_tui::toast::ToastLevel::Warning,
|
|
"Cloud usage is at 82% of the hourly quota.",
|
|
);
|
|
|
|
with_settings!({
|
|
snapshot_suffix => "80x24"
|
|
}, {
|
|
let snapshot = render_snapshot(&mut app, 80, 24);
|
|
assert_snapshot!("chat_tool_call_snapshot", snapshot);
|
|
});
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn render_chat_idle_no_animation_snapshot() {
|
|
let mut app = build_chat_app(
|
|
|cfg| {
|
|
cfg.ui.animations.micro = false;
|
|
},
|
|
|_| {},
|
|
)
|
|
.await;
|
|
|
|
with_settings!({ snapshot_suffix => "no-anim-100x35" }, {
|
|
let snapshot = render_snapshot(&mut app, 100, 35);
|
|
assert_snapshot!("chat_idle_snapshot_no_anim", snapshot);
|
|
});
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn render_command_palette_focus_snapshot() {
|
|
let mut app = build_chat_app(|_| {}, |_| {}).await;
|
|
|
|
app.handle_event(Event::Key(KeyEvent::new(
|
|
KeyCode::Char(':'),
|
|
KeyModifiers::NONE,
|
|
)))
|
|
.await
|
|
.expect("enter command mode");
|
|
|
|
for ch in ['f', 'o', 'c', 'u', 's'] {
|
|
app.handle_event(Event::Key(KeyEvent::new(
|
|
KeyCode::Char(ch),
|
|
KeyModifiers::NONE,
|
|
)))
|
|
.await
|
|
.expect("type query");
|
|
}
|
|
|
|
// Highlight the second suggestion (typically the model picker preview).
|
|
app.handle_event(Event::Key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)))
|
|
.await
|
|
.expect("move selection");
|
|
|
|
with_settings!({ snapshot_suffix => "80x20" }, {
|
|
let snapshot = render_snapshot(&mut app, 80, 20);
|
|
assert_snapshot!("command_palette_focus", snapshot);
|
|
});
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn render_guidance_onboarding_snapshot() {
|
|
let mut app = build_chat_app(
|
|
|cfg| {
|
|
cfg.ui.show_onboarding = true;
|
|
cfg.ui.guidance.coach_marks_complete = false;
|
|
},
|
|
|_| {},
|
|
)
|
|
.await;
|
|
|
|
with_settings!({ snapshot_suffix => "step1-80x24" }, {
|
|
let snapshot = render_snapshot(&mut app, 80, 24);
|
|
assert_snapshot!("guidance_onboarding", snapshot);
|
|
});
|
|
|
|
app.handle_event(Event::Key(KeyEvent::new(
|
|
KeyCode::Enter,
|
|
KeyModifiers::NONE,
|
|
)))
|
|
.await
|
|
.expect("advance onboarding to step 2");
|
|
|
|
with_settings!({ snapshot_suffix => "step2-100x24" }, {
|
|
let snapshot = render_snapshot(&mut app, 100, 24);
|
|
assert_snapshot!("guidance_onboarding", snapshot);
|
|
});
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn render_guidance_cheatsheet_snapshot() {
|
|
let mut app = build_chat_app(|cfg| cfg.ui.guidance.coach_marks_complete = true, |_| {}).await;
|
|
|
|
app.handle_event(Event::Key(KeyEvent::new(
|
|
KeyCode::Char('?'),
|
|
KeyModifiers::NONE,
|
|
)))
|
|
.await
|
|
.expect("open guidance overlay");
|
|
|
|
with_settings!({ snapshot_suffix => "tab1-100x24" }, {
|
|
let snapshot = render_snapshot(&mut app, 100, 24);
|
|
assert_snapshot!("guidance_cheatsheet", snapshot);
|
|
});
|
|
|
|
app.handle_event(Event::Key(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE)))
|
|
.await
|
|
.expect("advance guidance tab");
|
|
|
|
with_settings!({ snapshot_suffix => "tab2-100x24" }, {
|
|
let snapshot = render_snapshot(&mut app, 100, 24);
|
|
assert_snapshot!("guidance_cheatsheet", snapshot);
|
|
});
|
|
}
|