Files
2026-10-15 14:36:16 -03:00

84 lines
3.7 KiB
Rust

//! Assistant docking, command routing and ordinary composer typing regressions.
use egui::{Context, Event, Key, Modifiers, RawInput, Rect, Vec2};
use notescraft_ui_egui::{app::NotesApp, right_view::RightTab};
fn app(name: &str) -> Option<NotesApp> {
let root = std::env::temp_dir().join(format!("notescraft-assistant-dock-{name}-{}", std::process::id()));
assert!(std::fs::create_dir_all(&root).is_ok());
assert!(std::fs::write(root.join("note.md"), "# Note\n\nContext.").is_ok());
let mut session = notescraft_engine::Session::open(&root).ok()?;
session.use_settings_dir(root.join(".notescraft/test-config"));
let theme = notescraft_themes::builtin("catppuccin-mocha")?;
let mut app = NotesApp::new(session, theme);
app.state.animations = false;
app.open_note("note.md");
Some(app)
}
fn frame(ctx: &Context, app: &mut NotesApp, events: Vec<Event>) -> Vec<String> {
let input = RawInput { screen_rect: Some(Rect::from_min_size(egui::Pos2::ZERO, Vec2::new(1440.0, 900.0))), events, ..Default::default() };
let mut output = ctx.run_ui(input, |ui| app.ui(ui.ctx()));
let labels = output
.shapes
.iter()
.filter_map(|shape| match &shape.shape {
egui::epaint::Shape::Text(text) => Some(text.galley.text().to_owned()),
_ => None,
})
.collect();
output.textures_delta.clear();
labels
}
#[test]
fn assistant_command_opens_its_tab_and_preserves_other_tabs() {
let candidate = app("command");
assert!(candidate.is_some());
let Some(mut app) = candidate else { return };
app.state.right_visible = false;
app.run_command("ui.toggle_assistant", &serde_json::Value::Null);
assert!(app.state.right_visible);
assert_eq!(app.right_tab, RightTab::Assistant);
app.right_tab = RightTab::Outline;
let ctx = Context::default();
frame(&ctx, &mut app, Vec::new());
frame(&ctx, &mut app, Vec::new());
let labels = frame(&ctx, &mut app, Vec::new());
assert!(labels.iter().any(|label| label == "No headings" || label == "Note"), "outline labels: {labels:?}");
assert_eq!(app.right_tab, RightTab::Outline);
}
#[test]
fn shortcut_opens_empty_state_without_starting_an_agent() {
let candidate = app("shortcut");
assert!(candidate.is_some());
let Some(mut app) = candidate else { return };
let ctx = Context::default();
frame(&ctx, &mut app, Vec::new());
let event =
Event::Key { key: Key::A, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers { command: true, shift: true, ..Modifiers::NONE } };
frame(&ctx, &mut app, vec![event]);
let labels = frame(&ctx, &mut app, Vec::new());
assert_eq!(app.right_tab, RightTab::Assistant);
assert!(labels.iter().any(|label| label == "No agent configured"));
assert!(labels.iter().any(|label| label == "Open settings"));
}
#[test]
fn vim_navigation_leaves_composer_text_untouched() {
let candidate = app("vim-composer");
assert!(candidate.is_some());
let Some(mut app) = candidate else { return };
app.run_command("settings.set", &serde_json::json!({"key":"editor.vim","value":true}));
app.session.set_assistant_settings(notescraft_engine::AssistantSettings { command: "stub-agent".into(), ..Default::default() });
app.run_command("ui.toggle_assistant", &serde_json::Value::Null);
let ctx = Context::default();
frame(&ctx, &mut app, Vec::new());
frame(&ctx, &mut app, Vec::new());
ctx.memory_mut(|memory| memory.request_focus(egui::Id::new("assistant-composer")));
frame(&ctx, &mut app, vec![Event::Text("hjkl :hello".into())]);
assert_eq!(app.assistant.composer(), "hjkl :hello");
assert_eq!(app.right_tab, RightTab::Assistant);
}