5e8020aa38
Editor previews (colour swatches, arrows, embeds), the 18 obsidian-basics tasks (find and replace with regex, back/forward, auto-pair, footnotes, folding, search operators, split view, PDF/audio/ video embeds, spelling, drag and drop, pinned tabs, settings) and round 2: ```query blocks, stacked tabs, pop-out windows, tasks ticked inside embeds, text around an embed, embeds that follow edits, block menu shortcut. New crates: notescraft-regex (linear-time engine) and notescraft-syntax. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
151 lines
6.1 KiB
Rust
151 lines
6.1 KiB
Rust
//! Headless sidebar rendering, tag navigation and ranked-search keyboard tests.
|
|
|
|
use egui::{Context, Event, Key, RawInput};
|
|
use notescraft_engine::Session;
|
|
use notescraft_ui_egui::{app::NotesApp, sidebar, sidebar_tabs::SidebarTab, source_panel::Repository};
|
|
|
|
fn app(name: &str) -> Option<(NotesApp, std::path::PathBuf)> {
|
|
let root = std::env::temp_dir().join(format!("notescraft-sidebar-views-{name}-{}", std::process::id()));
|
|
assert!(std::fs::create_dir_all(&root).is_ok());
|
|
assert!(std::fs::write(root.join("Alpha.md"), "# Alpha\nalpha #work/project\n").is_ok());
|
|
assert!(std::fs::write(root.join("Beta.md"), "alpha body\n").is_ok());
|
|
let session = Session::open_read_only(&root);
|
|
assert!(session.is_ok());
|
|
let Ok(mut session) = session else { return None };
|
|
session.use_settings_dir(root.join(".notescraft/test-config"));
|
|
let theme = notescraft_themes::builtin("catppuccin-mocha")?;
|
|
let mut app = NotesApp::new(session, theme);
|
|
app.open_note("Alpha.md");
|
|
app.state.animations = false;
|
|
Some((app, root))
|
|
}
|
|
|
|
fn context() -> Context {
|
|
let ctx = Context::default();
|
|
notescraft_ui_egui::fonts::install(&ctx);
|
|
egui_extras::install_image_loaders(&ctx);
|
|
let mut warm = ctx.run_ui(RawInput::default(), |_| {});
|
|
warm.textures_delta.clear();
|
|
ctx
|
|
}
|
|
|
|
fn frame(ctx: &Context, app: &mut NotesApp, events: Vec<Event>) -> (sidebar::SidebarResponse, Vec<(String, egui::Pos2)>) {
|
|
let data = sidebar::data_for(&app.session, None);
|
|
let mut response = sidebar::SidebarResponse::default();
|
|
let mut output =
|
|
ctx.run_ui(RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(280.0, 900.0))), events, ..Default::default() }, |ui| {
|
|
notescraft_ui_egui::motion::set_enabled(ui.ctx(), false);
|
|
notescraft_ui_egui::style::apply(ui.ctx(), &app.tokens);
|
|
let mut inputs = sidebar::SidebarInputs {
|
|
data: &data,
|
|
tree: &app.tree,
|
|
state: &mut app.state,
|
|
selected: app.selected.as_deref(),
|
|
session: &app.session,
|
|
tabs: &app.tabs,
|
|
search: &mut app.search,
|
|
source: &mut app.source,
|
|
tag_collapsed: &mut app.tag_collapsed,
|
|
index: app.session.index_progress(),
|
|
};
|
|
response = sidebar::draw_with_tabs(ui, &app.tokens, &mut inputs);
|
|
});
|
|
let labels = output
|
|
.shapes
|
|
.iter()
|
|
.filter_map(|shape| {
|
|
let egui::Shape::Text(text) = &shape.shape else { return None };
|
|
Some((text.galley.text().to_owned(), text.pos + text.galley.size() / 2.0))
|
|
})
|
|
.collect();
|
|
output.textures_delta.clear();
|
|
(response, labels)
|
|
}
|
|
|
|
fn key(key: Key) -> Event {
|
|
Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }
|
|
}
|
|
|
|
#[test]
|
|
fn every_sidebar_tab_draws_with_warm_fonts() {
|
|
let Some((mut app, root)) = app("all") else { return };
|
|
let ctx = context();
|
|
app.source.repository = Repository::Ready(notescraft_git::Status::default());
|
|
app.source.busy = false;
|
|
for (tab, expected) in [
|
|
(SidebarTab::Explorer, "Search notes"),
|
|
(SidebarTab::Search, "Search your vault"),
|
|
(SidebarTab::Tags, "work"),
|
|
(SidebarTab::Buffers, "Alpha"),
|
|
(SidebarTab::Source, "Working tree clean"),
|
|
] {
|
|
app.state.sidebar_tab = tab;
|
|
let _ = frame(&ctx, &mut app, Vec::new());
|
|
let (_, labels) = frame(&ctx, &mut app, Vec::new());
|
|
assert!(labels.iter().any(|(label, _)| label == expected), "{tab:?}: {labels:?}");
|
|
}
|
|
drop(app);
|
|
let _ = std::fs::remove_dir_all(root);
|
|
}
|
|
|
|
#[test]
|
|
fn clicking_nested_tag_switches_to_search_with_full_query() {
|
|
let Some((mut app, root)) = app("tag") else { return };
|
|
let ctx = context();
|
|
app.state.sidebar_tab = SidebarTab::Tags;
|
|
let (_, labels) = frame(&ctx, &mut app, Vec::new());
|
|
let position = labels.iter().find(|(label, _)| label == "work/project").map(|(_, position)| *position);
|
|
assert!(position.is_some());
|
|
let Some(position) = position else { return };
|
|
let click = vec![
|
|
Event::PointerMoved(position),
|
|
Event::PointerButton { pos: position, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE },
|
|
Event::PointerButton { pos: position, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE },
|
|
];
|
|
let _ = frame(&ctx, &mut app, click);
|
|
assert_eq!(app.state.sidebar_tab, SidebarTab::Search);
|
|
assert_eq!(app.search.query, "tag:#work/project");
|
|
assert!(app.search.focus);
|
|
drop(app);
|
|
let _ = std::fs::remove_dir_all(root);
|
|
}
|
|
|
|
#[test]
|
|
fn search_arrow_and_enter_open_selected_result() {
|
|
let Some((mut app, root)) = app("keys") else { return };
|
|
let ctx = context();
|
|
app.state.sidebar_tab = SidebarTab::Search;
|
|
app.search.set_query("alpha".into(), 0.0);
|
|
app.search.refresh(&app.session, 0.2);
|
|
app.search.focus = true;
|
|
let _ = frame(&ctx, &mut app, Vec::new());
|
|
let _ = frame(&ctx, &mut app, vec![key(Key::ArrowDown)]);
|
|
assert_eq!(app.search.selected, 1);
|
|
let (response, _) = frame(&ctx, &mut app, vec![key(Key::Enter)]);
|
|
assert_eq!(
|
|
response.open_note,
|
|
Some(("Beta.md".into(), false)),
|
|
"selected={} hits={:?}",
|
|
app.search.selected,
|
|
app.search.hits.iter().map(|hit| &hit.path).collect::<Vec<_>>()
|
|
);
|
|
drop(app);
|
|
let _ = std::fs::remove_dir_all(root);
|
|
}
|
|
|
|
#[test]
|
|
fn source_explains_missing_git_and_offers_initialization() {
|
|
let Some((mut app, root)) = app("states") else { return };
|
|
let ctx = context();
|
|
app.state.sidebar_tab = SidebarTab::Source;
|
|
app.source.busy = false;
|
|
app.source.repository = Repository::MissingGit;
|
|
let (_, labels) = frame(&ctx, &mut app, Vec::new());
|
|
assert!(labels.iter().any(|(label, _)| label.contains("Install git")));
|
|
app.source.repository = Repository::NotInitialized;
|
|
let (_, labels) = frame(&ctx, &mut app, Vec::new());
|
|
assert!(labels.iter().any(|(label, _)| label == "Initialize repository"));
|
|
drop(app);
|
|
let _ = std::fs::remove_dir_all(root);
|
|
}
|