use crate::common::harness::EditorTestHarness;
use crossterm::event::{KeyCode, KeyModifiers};
use std::time::Duration;
#[test]
fn test_plugin_command_executes_via_keybinding() {
let mut config = fresh::config::Config::default();
config.keybindings.push(fresh::config::Keybinding {
key: "m".to_string(),
modifiers: vec!["alt".to_string()],
keys: vec![],
action: "marker_insert_action".to_string(),
args: std::collections::HashMap::new(),
when: None,
});
let mut harness = EditorTestHarness::with_temp_project_and_config(120, 40, config).unwrap();
let plugin_source = r#"
const editor = getEditor();
globalThis.marker_insert_action = function(): void {
const bufferId = editor.getActiveBufferId();
if (bufferId !== null && bufferId !== undefined) {
editor.insertText(bufferId, 0, "XYZZY_KEYBIND_MARKER");
}
};
editor.registerCommand(
"Insert Marker",
"Insert a marker string at the start of the buffer",
"marker_insert_action",
null
);
editor.setStatus("marker-plugin-loaded");
"#;
let project_dir = harness.project_dir().unwrap();
let plugin_file = project_dir.join("marker_plugin.ts");
std::fs::write(&plugin_file, plugin_source).unwrap();
harness.open_file(&plugin_file).unwrap();
harness.render().unwrap();
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.render().unwrap();
harness.type_text("Load Plugin from Buffer").unwrap();
for _ in 0..3 {
harness.process_async_and_render().unwrap();
harness.sleep(Duration::from_millis(50));
}
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
for _ in 0..10 {
harness.process_async_and_render().unwrap();
harness.sleep(Duration::from_millis(50));
}
harness.assert_no_plugin_errors();
let test_file = project_dir.join("test_target.txt");
std::fs::write(&test_file, "hello world\n").unwrap();
harness.open_file(&test_file).unwrap();
harness.render().unwrap();
let screen = harness.screen_to_string();
assert!(
!screen.contains("XYZZY_KEYBIND_MARKER"),
"Marker should not be present before pressing keybinding. Screen:\n{}",
screen
);
harness
.send_key(KeyCode::Char('m'), KeyModifiers::ALT)
.unwrap();
for _ in 0..10 {
harness.process_async_and_render().unwrap();
harness.sleep(Duration::from_millis(50));
}
let screen = harness.screen_to_string();
assert!(
screen.contains("XYZZY_KEYBIND_MARKER"),
"Pressing Alt+M should have executed the plugin command via keybinding, \
inserting 'XYZZY_KEYBIND_MARKER' into the buffer. Screen:\n{}",
screen
);
}