use super::ChatInput;
use crate::ChatInputEvent;
use gpui_kit::test::TestWindowExt as _;
use gpui_kit::{Entity, Subscription, TestAppContext, VisualTestContext};
use std::{cell::RefCell, rc::Rc};
fn submissions(
input: &Entity<ChatInput>,
cx: &mut VisualTestContext,
) -> (Rc<RefCell<Vec<String>>>, Subscription) {
let submitted = Rc::new(RefCell::new(Vec::new()));
let recorded = submitted.clone();
let subscription = cx.update(|_, cx| {
cx.subscribe(input, move |_, event, _| {
if let ChatInputEvent::Submit(text) = event {
recorded.borrow_mut().push(text.clone());
}
})
});
(submitted, subscription)
}
#[gpui_kit::test]
fn message_enter_trims_rejects_empty_and_busy_and_error_preserves_draft(cx: &mut TestAppContext) {
cx.update(gpui_kit::init);
let (input, cx) = cx.add_window_view(ChatInput::message);
let (submitted, _subscription) = submissions(&input, cx);
cx.update(|window, cx| {
input.update(cx, |input, cx| input.focus(window, cx));
window.render_frame(cx);
window.press("enter", cx);
});
cx.run_until_parked();
assert!(submitted.borrow().is_empty());
cx.update(|window, cx| {
window.input(" retained message ", cx);
window.press("enter", cx);
});
cx.run_until_parked();
assert_eq!(&*submitted.borrow(), &["retained message"]);
cx.update(|window, cx| {
input.update(cx, |input, cx| input.set_busy(true, cx));
window.render_frame(cx);
window.press("enter", cx);
window.click("message-send", cx);
});
cx.run_until_parked();
assert_eq!(submitted.borrow().len(), 1);
cx.update(|window, cx| {
input.update(cx, |input, cx| {
input.set_error("Fixture send failure".into(), cx);
assert!(!input.disabled);
assert!(!input.message.as_ref().unwrap().busy);
assert_eq!(
input.message.as_ref().unwrap().error.as_deref(),
Some("Fixture send failure")
);
assert_eq!(input.text(cx), " retained message ");
input.focus(window, cx);
});
window.render_frame(cx);
window.press("enter", cx);
});
cx.run_until_parked();
assert_eq!(
&*submitted.borrow(),
&["retained message", "retained message"]
);
}
fn recall_events(
input: &Entity<ChatInput>,
cx: &mut VisualTestContext,
) -> (Rc<RefCell<Vec<&'static str>>>, Subscription) {
let events = Rc::new(RefCell::new(Vec::new()));
let recorded = events.clone();
let subscription = cx.update(|_, cx| {
cx.subscribe(input, move |_, event, _| {
let name = match event {
ChatInputEvent::Previous => "previous",
ChatInputEvent::Next => "next",
ChatInputEvent::Complete => "complete",
_ => return,
};
recorded.borrow_mut().push(name);
})
});
(events, subscription)
}
#[gpui_kit::test]
fn plain_recall_input_hides_markdown_and_routes_arrows_and_tab(cx: &mut TestAppContext) {
cx.update(gpui_kit::init);
cx.update(crate::bind_keys);
let (input, cx) = cx.add_window_view(ChatInput::message);
let (events, _subscription) = recall_events(&input, cx);
cx.update(|window, cx| {
input.update(cx, |input, cx| input.focus(window, cx));
window.render_frame(cx);
assert!(window.try_find("message-write").is_some());
window.press("up", cx);
});
cx.run_until_parked();
assert!(
events.borrow().is_empty(),
"arrows move the cursor by default"
);
cx.update(|window, cx| {
input.update(cx, |input, cx| {
input.set_plain_text(cx);
input.set_recall_keys(true, cx);
});
window.render_frame(cx);
assert!(window.try_find("message-write").is_none());
assert!(window.try_find("message-preview").is_none());
window.press("up", cx);
window.press("down", cx);
window.press("tab", cx);
});
cx.run_until_parked();
assert_eq!(&*events.borrow(), &["previous", "next", "complete"]);
}
#[gpui_kit::test]
fn a_multi_line_draft_keeps_its_arrows_unless_it_was_recalled(cx: &mut TestAppContext) {
cx.update(gpui_kit::init);
cx.update(crate::bind_keys);
let (input, cx) = cx.add_window_view(ChatInput::message);
let (events, _subscription) = recall_events(&input, cx);
cx.update(|window, cx| {
input.update(cx, |input, cx| {
input.set_plain_text(cx);
input.set_recall_keys(true, cx);
input.focus(window, cx);
input.set_text("one\ntwo", window, cx);
});
window.render_frame(cx);
window.press("up", cx);
window.press("down", cx);
window.press("tab", cx);
});
cx.run_until_parked();
assert_eq!(
&*events.borrow(),
&["complete"],
"in a multi-line draft the arrows move between lines; Tab still completes"
);
cx.update(|window, cx| {
input.update(cx, |input, cx| input.set_recalled(true, cx));
window.render_frame(cx);
window.press("up", cx);
});
cx.run_until_parked();
assert_eq!(
events.borrow().last(),
Some(&"previous"),
"a recalled multi-line line keeps recalling"
);
}
#[gpui_kit::test]
fn shift_enter_breaks_the_line_without_sending(cx: &mut TestAppContext) {
cx.update(gpui_kit::init);
cx.update(crate::bind_keys);
let (input, cx) = cx.add_window_view(ChatInput::message);
let (submitted, _subscription) = submissions(&input, cx);
cx.update(|window, cx| {
input.update(cx, |input, cx| input.focus(window, cx));
window.render_frame(cx);
window.input("first", cx);
window.press("shift-enter", cx);
window.input("second", cx);
});
cx.run_until_parked();
assert!(
submitted.borrow().is_empty(),
"Shift+Enter only breaks the line"
);
assert_eq!(cx.update(|_, cx| input.read(cx).text(cx)), "first\nsecond");
cx.update(|window, cx| window.press("enter", cx));
cx.run_until_parked();
assert_eq!(&*submitted.borrow(), &["first\nsecond"]);
}
#[test]
fn the_hint_names_the_platform_s_send_and_new_line_keys() {
use super::send_hint;
assert_eq!(
send_hint(false, true),
"Return to send · Shift+Return for a new line"
);
assert_eq!(
send_hint(false, false),
"Enter to send · Shift+Enter for a new line"
);
assert_eq!(
send_hint(true, true),
"Markdown · Return to send · Shift+Return for a new line"
);
}