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>
401 lines
16 KiB
Rust
401 lines
16 KiB
Rust
//! Integration tests for the view widgets, run headless over two frames.
|
|
|
|
use std::collections::{BTreeMap, HashMap};
|
|
|
|
use egui::{Context, Event, Key, RawInput};
|
|
|
|
use notescraft_backlinks::{LinkedMention, UnlinkedMention};
|
|
use notescraft_engine::CommandInfo;
|
|
use notescraft_themes::{CalloutKind as ThemeCallout, Colors, Fonts, GraphColors, Radii, Rgba, SyntaxColors, Theme, Variant};
|
|
use notescraft_ui_egui::theme::Tokens;
|
|
use notescraft_ui_egui::{
|
|
backlinks_panel::{self, BacklinksData},
|
|
note_view::CalloutFolds,
|
|
note_view::{self},
|
|
outline::{self},
|
|
palette::{self},
|
|
};
|
|
|
|
/// A compact test theme: tokens are arbitrary but distinct.
|
|
fn theme() -> Theme {
|
|
let c = Rgba::new(64, 64, 64, 255);
|
|
let mut callouts = BTreeMap::new();
|
|
for kind in ThemeCallout::ALL {
|
|
callouts.insert(kind, Rgba::new(120, 80, 200, 255));
|
|
}
|
|
Theme {
|
|
id: String::from("test"),
|
|
name: String::from("Test"),
|
|
variant: Variant::Dark,
|
|
colors: Colors {
|
|
background: c,
|
|
surface: Rgba::new(40, 40, 40, 255),
|
|
surface_2: Rgba::new(56, 56, 56, 255),
|
|
canvas: Rgba::new(24, 24, 24, 255),
|
|
border: c,
|
|
text: Rgba::new(230, 230, 230, 255),
|
|
text_muted: Rgba::new(150, 150, 150, 255),
|
|
accent: Rgba::new(180, 120, 250, 255),
|
|
on_accent: Rgba::new(20, 20, 20, 255),
|
|
link: Rgba::new(120, 160, 250, 255),
|
|
link_unresolved: Rgba::new(250, 120, 130, 255),
|
|
selection: Rgba::new(80, 80, 90, 255),
|
|
code_background: Rgba::new(20, 20, 26, 255),
|
|
highlight: Rgba::new(240, 220, 160, 100),
|
|
success: Rgba::new(120, 200, 120, 255),
|
|
warning: Rgba::new(220, 180, 100, 255),
|
|
danger: Rgba::new(230, 110, 110, 255),
|
|
},
|
|
callouts,
|
|
graph: GraphColors { node: c, node_active: c, edge: c, unresolved: c },
|
|
syntax: SyntaxColors::uniform(c),
|
|
radii: Radii { small: 4.0, medium: 6.0 },
|
|
fonts: Fonts { ui: String::new(), editor: String::new(), mono: String::new() },
|
|
}
|
|
}
|
|
|
|
fn tokens() -> Tokens {
|
|
Tokens::new(theme())
|
|
}
|
|
|
|
/// A context with the app fonts installed and applied (named families panic otherwise).
|
|
fn fresh_ctx() -> Context {
|
|
let ctx = Context::default();
|
|
notescraft_ui_egui::fonts::install(&ctx);
|
|
let mut warm = ctx.run_ui(RawInput::default(), |_| {});
|
|
warm.textures_delta.clear();
|
|
ctx
|
|
}
|
|
|
|
/// Runs `frame` twice on a 900x700 screen and returns the second output.
|
|
fn two_frames<F: FnMut(&mut egui::Ui)>(mut frame: F) -> egui::FullOutput {
|
|
let ctx = fresh_ctx();
|
|
let input = |events: Vec<Event>| RawInput {
|
|
screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(900.0, 700.0))),
|
|
events,
|
|
..RawInput::default()
|
|
};
|
|
let mut first = ctx.run_ui(input(vec![]), |ui| frame(ui));
|
|
first.textures_delta.clear();
|
|
let mut second = ctx.run_ui(input(vec![]), |ui| frame(ui));
|
|
second.textures_delta.clear();
|
|
second
|
|
}
|
|
|
|
/// Applies nothing but clears texture deltas so the context can drop safely.
|
|
fn drop_output(mut out: egui::FullOutput) {
|
|
out.textures_delta.clear();
|
|
}
|
|
|
|
/// `Enter` selects; `Escape` closes; `ArrowDown` moves and the next `Enter` picks.
|
|
#[test]
|
|
fn palette_keyboard_selection() {
|
|
let commands = vec![
|
|
CommandInfo {
|
|
id: String::from("a.first"),
|
|
label: String::from("Cut"),
|
|
menu_path: vec![String::from("Edit")],
|
|
shortcut: None,
|
|
params: vec![],
|
|
doc: String::new(),
|
|
enabled: true,
|
|
},
|
|
CommandInfo {
|
|
id: String::from("b.second"),
|
|
label: String::from("Copy"),
|
|
menu_path: vec![],
|
|
shortcut: None,
|
|
params: vec![],
|
|
doc: String::new(),
|
|
enabled: true,
|
|
},
|
|
];
|
|
let t = tokens();
|
|
let input = |events: Vec<Event>| RawInput {
|
|
screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(900.0, 700.0))),
|
|
events,
|
|
..RawInput::default()
|
|
};
|
|
let key = |k: Key| vec![Event::Key { key: k, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::default() }];
|
|
|
|
let picked = std::cell::RefCell::new(None);
|
|
let ctx = fresh_ctx();
|
|
let mut state = palette::PaletteState { open: true, query: String::new(), selected: 0, kind: palette::PaletteKind::Commands };
|
|
drop_output(ctx.run_ui(input(key(Key::Enter)), |ui| {
|
|
*picked.borrow_mut() = palette::show_palette(ui.ctx(), &t, &mut state, &commands);
|
|
}));
|
|
assert_eq!(picked.borrow().as_deref(), Some("a.first"));
|
|
assert!(!state.open, "Enter closes the palette");
|
|
picked.borrow_mut().take();
|
|
|
|
let mut state = palette::PaletteState { open: true, query: String::new(), selected: 0, kind: palette::PaletteKind::Commands };
|
|
drop_output(ctx.run_ui(input(key(Key::Escape)), |ui| {
|
|
*picked.borrow_mut() = palette::show_palette(ui.ctx(), &t, &mut state, &commands);
|
|
}));
|
|
assert!(picked.borrow().is_none());
|
|
assert!(!state.open, "Escape closes the palette");
|
|
|
|
let mut state = palette::PaletteState { open: true, query: String::new(), selected: 0, kind: palette::PaletteKind::Commands };
|
|
drop_output(ctx.run_ui(input(key(Key::ArrowDown)), |ui| {
|
|
*picked.borrow_mut() = palette::show_palette(ui.ctx(), &t, &mut state, &commands);
|
|
}));
|
|
drop_output(ctx.run_ui(input(key(Key::Enter)), |ui| {
|
|
*picked.borrow_mut() = palette::show_palette(ui.ctx(), &t, &mut state, &commands);
|
|
}));
|
|
assert_eq!(picked.into_inner(), Some(String::from("b.second")));
|
|
}
|
|
|
|
/// The open palette draws rows; a closed one draws nothing.
|
|
#[test]
|
|
fn palette_draws_rows_when_open() {
|
|
let commands = vec![CommandInfo {
|
|
id: String::from("open.file"),
|
|
label: String::from("Open file"),
|
|
menu_path: vec![String::from("File")],
|
|
shortcut: Some(String::from("Mod-o")),
|
|
params: vec![],
|
|
doc: String::new(),
|
|
enabled: true,
|
|
}];
|
|
let mut state = palette::PaletteState { open: true, query: String::from("op"), selected: 0, kind: palette::PaletteKind::Commands };
|
|
let out = two_frames(|ui| {
|
|
show_in_ctx(ui, &mut state, &commands);
|
|
});
|
|
assert!(out.shapes.len() > 4, "expected rows, backdrop and query field, got {} shapes", out.shapes.len());
|
|
}
|
|
|
|
/// Query field needs the context-level palette call (helper for `two_frames`).
|
|
fn show_in_ctx(ui: &mut egui::Ui, state: &mut palette::PaletteState, commands: &[CommandInfo]) {
|
|
let tokens = tokens();
|
|
let _ = palette::show_palette(ui.ctx(), &tokens, state, commands);
|
|
}
|
|
|
|
#[test]
|
|
fn fuzzy_score_table() {
|
|
assert_eq!(palette::fuzzy_score("", "New note"), Some(0));
|
|
assert!(palette::fuzzy_score("New note", "New note").is_some());
|
|
assert!(palette::fuzzy_score("nt", "New note").is_some(), "subsequence");
|
|
assert_eq!(palette::fuzzy_score("nt", "Save"), None);
|
|
assert!(palette::fuzzy_score("NT", "new note").is_some(), "case-insensitive");
|
|
assert!(palette::fuzzy_score("é", "café menu").is_some(), "unicode");
|
|
// Word starts outrank plain runs; both are subsequence hits.
|
|
let start = palette::fuzzy_score("n", "new note");
|
|
let inner = palette::fuzzy_score("o", "note");
|
|
assert!(start.is_some_and(|s| s >= 3), "word start scores {start:?}");
|
|
assert!(inner.is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn ranked_commands_hides_disabled_unless_exact() {
|
|
let commands = vec![
|
|
CommandInfo {
|
|
id: String::from("x.on"),
|
|
label: String::from("Cut"),
|
|
menu_path: vec![],
|
|
shortcut: None,
|
|
params: vec![],
|
|
doc: String::new(),
|
|
enabled: true,
|
|
},
|
|
CommandInfo {
|
|
id: String::from("y.off"),
|
|
label: String::from("Cut off"),
|
|
menu_path: vec![],
|
|
shortcut: None,
|
|
params: vec![],
|
|
doc: String::new(),
|
|
enabled: false,
|
|
},
|
|
];
|
|
assert_eq!(palette::ranked_commands(&commands, ""), vec![0], "empty query shows only enabled");
|
|
assert_eq!(palette::ranked_commands(&commands, "Cut"), vec![0], "a prefix hit is not an exact match for the disabled one");
|
|
assert_eq!(palette::ranked_commands(&commands, "Cut off"), vec![1], "exact match reveals the disabled one");
|
|
assert_eq!(palette::ranked_commands(&commands, "nothing"), Vec::<usize>::new());
|
|
}
|
|
|
|
#[test]
|
|
fn move_selection_clamps() {
|
|
assert_eq!(palette::move_selection(0, -1, 3), 0);
|
|
assert_eq!(palette::move_selection(2, 1, 3), 2);
|
|
assert_eq!(palette::move_selection(0, 2, 10), 2);
|
|
assert_eq!(palette::move_selection(5, -3, 2), 1);
|
|
assert_eq!(palette::move_selection(0, 1, 0), 0);
|
|
}
|
|
/// One fixture line per markdown construct; all 13 callout kinds included.
|
|
fn fixture() -> String {
|
|
let kinds = ["note", "abstract", "info", "todo", "tip", "success", "question", "warning", "failure", "danger", "bug", "example", "quote"];
|
|
let callouts: String = kinds.iter().fold(String::new(), |acc, k| acc + "> [!" + k + "] Title\n> Body of " + k + "\n\n");
|
|
format!(
|
|
"---\ntitle: Fixture\ntags: [alpha, beta]\n---\n\
|
|
# Title\n## Section\n### Sub\n#### H4\n##### H5\n###### H6\n\
|
|
Body with **bold**, *italic*, ~~strike~~, `code`, ==highlight==, $inline$ and [[Known|alias]].\n\
|
|
See [markdown link](Known.md#Section) and [[Missing]]. #tag/nested\n\
|
|
- bullet one\n- bullet two\n1. first\n2. second\n- [ ] to do\n- [x] done\n\
|
|
> quoted line\n\n\
|
|
![[img.png|300x200]]\n\n\
|
|
$$\na + b = c\n$$\n\n```rust\nlet x = 1;\n```\n\n{callouts}"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn note_view_renders_every_construct() {
|
|
let doc = notescraft_markdown::parse(&fixture());
|
|
let t = tokens();
|
|
let resolves = |target: &str| target == "Known";
|
|
let mut folds: CalloutFolds = HashMap::new();
|
|
let out = two_frames(|ui| {
|
|
let _ = note_view::show_note(ui, &t, &doc, &resolves, &mut folds);
|
|
});
|
|
assert!(out.shapes.len() > 40, "expected many painted rows, got {}", out.shapes.len());
|
|
}
|
|
|
|
/// A `Fold::Open` callout collapses to its title when `folds` says so.
|
|
#[test]
|
|
fn note_view_callouts_honour_the_fold_state() {
|
|
let doc = notescraft_markdown::parse("> [!tip]+ Open\n> body line\n");
|
|
let header = doc.callouts().last().map_or(0, |i| i.range.start);
|
|
let t = tokens();
|
|
let resolves = |_: &str| true;
|
|
let mut expanded: CalloutFolds = HashMap::new();
|
|
expanded.insert(header, true);
|
|
let with_body = two_frames(|ui| {
|
|
let mut folds = expanded.clone();
|
|
let _ = note_view::show_note(ui, &t, &doc, &resolves, &mut folds);
|
|
});
|
|
let mut collapsed: CalloutFolds = HashMap::new();
|
|
collapsed.insert(header, false);
|
|
let title_only = two_frames(|ui| {
|
|
let mut folds = collapsed.clone();
|
|
let _ = note_view::show_note(ui, &t, &doc, &resolves, &mut folds);
|
|
});
|
|
assert!(with_body.shapes.len() > title_only.shapes.len(), "{} vs {}", with_body.shapes.len(), title_only.shapes.len());
|
|
}
|
|
|
|
#[test]
|
|
fn note_view_survives_deep_nesting() {
|
|
let mut src = String::new();
|
|
for depth in 0..500 {
|
|
src.push_str(&"> ".repeat(depth + 1));
|
|
src.push_str("level\n");
|
|
}
|
|
let doc = notescraft_markdown::parse(&src);
|
|
let t = tokens();
|
|
let out = two_frames(|ui| {
|
|
let mut folds: CalloutFolds = HashMap::new();
|
|
let _ = note_view::show_note(ui, &t, &doc, &|_: &str| false, &mut folds);
|
|
});
|
|
assert!(out.shapes.len() > 2, "nested quotes painted, got {} shapes", out.shapes.len());
|
|
}
|
|
|
|
#[test]
|
|
fn note_view_truncates_huge_note_with_notice() {
|
|
let line = "one two three four five six seven eight nine ten\n";
|
|
let mut src = String::from("# Big\n");
|
|
while src.len() < 3 * 1024 * 1024 {
|
|
src.push_str(line);
|
|
}
|
|
assert!(src.len() > note_view::MAX_RENDER_BYTES);
|
|
let doc = notescraft_markdown::parse(&src);
|
|
let t = tokens();
|
|
let out = two_frames(|ui| {
|
|
let mut folds: CalloutFolds = HashMap::new();
|
|
let _ = note_view::show_note(ui, &t, &doc, &|_: &str| true, &mut folds);
|
|
});
|
|
assert!(out.shapes.len() > 2, "prefix plus notice rendered, got {}", out.shapes.len());
|
|
}
|
|
|
|
#[test]
|
|
fn note_view_survives_malformed_and_control_chars() {
|
|
for src in [
|
|
"```\nunclosed fence\n==unclosed ==nope\n[[unclosed\n![[\n$math\n%%%open comment\n",
|
|
"a\x07b\u{8}c\n\t\x00? \u{1F510}\n",
|
|
"== a ==\n> \n> [!tip]\n> \n",
|
|
"- [x]\n> [!broken\n***\n",
|
|
] {
|
|
let doc = notescraft_markdown::parse(src);
|
|
let t = tokens();
|
|
let out = two_frames(|ui| {
|
|
let mut folds: CalloutFolds = HashMap::new();
|
|
let _ = note_view::show_note(ui, &t, &doc, &|_: &str| false, &mut folds);
|
|
});
|
|
let _ = out.shapes.len();
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn backlinks_sections_counts_and_cards() {
|
|
let empty = BacklinksData { linked: vec![], unlinked: vec![], skipped_large: 2, ..Default::default() };
|
|
let t = tokens();
|
|
let out = two_frames(|ui| {
|
|
let _ = backlinks_panel::show_backlinks(ui, &t, &empty);
|
|
});
|
|
assert!(out.shapes.len() > 2, "headers and skipped notice drawn");
|
|
|
|
let linked = LinkedMention { from: String::from("a.md"), range: 0..8, line: 3, snippet: String::from("see the Plan today"), matched: String::from("Plan") };
|
|
let unlinked = UnlinkedMention {
|
|
from: String::from("b.md"),
|
|
range: 4..8,
|
|
line: 7,
|
|
snippet: String::from("the Plan is due"),
|
|
matched: String::from("Plan"),
|
|
name: String::from("Plan"),
|
|
};
|
|
let data = BacklinksData { linked: vec![linked], unlinked: vec![unlinked], skipped_large: 0, ..Default::default() };
|
|
let out = two_frames(|ui| {
|
|
let _ = backlinks_panel::show_backlinks(ui, &t, &data);
|
|
});
|
|
assert!(out.shapes.len() > 6, "two sections and two cards drawn");
|
|
}
|
|
|
|
#[test]
|
|
fn backlinks_capped_at_many_mentions() {
|
|
let unlinked: Vec<UnlinkedMention> = (0..10_000)
|
|
.map(|i| UnlinkedMention {
|
|
from: format!("note-{i}.md"),
|
|
range: 0..4,
|
|
line: 1,
|
|
snippet: String::from("Plan mention"),
|
|
matched: String::from("Plan"),
|
|
name: String::from("Plan"),
|
|
})
|
|
.collect();
|
|
let data = BacklinksData { linked: vec![], unlinked, skipped_large: 0, ..Default::default() };
|
|
let t = tokens();
|
|
let out = two_frames(|ui| {
|
|
let _ = backlinks_panel::show_backlinks(ui, &t, &data);
|
|
});
|
|
assert!(out.shapes.len() > backlinks_panel::MAX_CARDS_VISIBLE, "capped rendering still paints many cards");
|
|
}
|
|
|
|
#[test]
|
|
fn outline_renders_and_reports_clicks() {
|
|
let doc = notescraft_markdown::parse("# One\n## Two\nText\n-----\n===\n");
|
|
let entries = outline::outline_from(&doc);
|
|
assert!(entries.len() >= 3, "{entries:?}");
|
|
let t = tokens();
|
|
let out = two_frames(|ui| {
|
|
let _ = outline::show_outline(ui, &t, &entries, Some(1));
|
|
});
|
|
assert!(out.shapes.len() >= entries.len());
|
|
let nothing = outline::outline_from(¬escraft_markdown::parse("no headings\n"));
|
|
assert_eq!(nothing, []);
|
|
}
|
|
|
|
#[test]
|
|
fn mermaid_hook_renders_only_mermaid_and_preserves_plain_fences() {
|
|
let source = "é\n```mermaid\nflowchart LR\nA-->B\n```\n```rust\nlet a = 1;\n```\n";
|
|
let doc = notescraft_markdown::parse(source);
|
|
let mut calls = 0;
|
|
let mut folds = CalloutFolds::default();
|
|
let out = two_frames(|ui| {
|
|
note_view::show_note_with_fences(ui, &tokens(), &doc, &|_| true, &mut folds, &mut |ui, range, text| {
|
|
calls += 1;
|
|
assert_eq!(source.get(range), Some(text));
|
|
let mut view = notescraft_ui_egui::diagram_view::DiagramView::default();
|
|
let _ = notescraft_ui_egui::diagram_view::show_diagram(ui, &tokens(), &mut view, text);
|
|
});
|
|
});
|
|
assert_eq!(calls, 2);
|
|
drop_output(out);
|
|
}
|