Files
Marcos Paulo eb1245bd20
ci / ci (macos-latest) (push) Has been cancelled
ci / ci (ubuntu-latest) (push) Has been cancelled
ci / ci (windows-latest) (push) Has been cancelled
fix: tab switch freeze (unlinked scan off the UI thread); assistant: vault MCP wiring, preamble, wrapping choices, presets; 23 grammars incl. Twig; Tasks query, Templater include, icon picker
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-21 13:36:33 -03:00

72 lines
2.9 KiB
Rust

//! Switching and closing tabs in a big vault must not stall a frame: the unlinked-mention scan of the
//! active note reads every note of the vault, so it runs on a thread and the frame only starts it.
use egui::{Context, Pos2, RawInput};
use notescraft_engine::Session;
use notescraft_ui_egui::app::NotesApp;
use std::fmt::Write as _;
use std::time::{Duration, Instant};
/// Notes in the generated vault.
const NOTES: usize = 2000;
/// Fenced code blocks in each note.
const FENCES: usize = 50;
/// Tabs opened, switched between and closed.
const TABS: usize = 8;
/// The slowest frame allowed, generous for a debug build on a busy CI machine.
const FRAME_BUDGET: Duration = Duration::from_millis(250);
fn note(index: usize) -> String {
let mut text = format!(
"---\ntitle: Topic {index}\n---\n# Topic {index}\n\nSee the widget notes; widget is a plain word here.\n\n> [!note] Callout\n> widget callout\n\n"
);
for fence in 0..FENCES {
let _ = write!(text, "```rust\nfn widget_{fence}() -> u32 {{ {fence} }}\n```\n\nprose widget {fence} ![[Topic {}]]\n\n", (index + 1) % NOTES);
}
text
}
fn frame(app: &mut NotesApp, ctx: &Context, time: &mut f64, slowest: &mut Duration) {
*time += 0.05;
let input = RawInput { screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, egui::vec2(1440.0, 900.0))), time: Some(*time), ..Default::default() };
let started = Instant::now();
let mut out = ctx.run_ui(input, |ui| app.ui(ui.ctx()));
*slowest = (*slowest).max(started.elapsed());
out.textures_delta.clear();
}
#[test]
fn switching_and_closing_tabs_in_a_big_vault_never_stalls_a_frame() {
let root = std::env::temp_dir().join(format!("notescraft-freeze-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).expect("root");
for index in 0..NOTES {
std::fs::write(root.join(format!("Widget {index}.md")), note(index)).expect("seed");
}
let mut session = Session::open(&root).expect("session");
session.use_settings_dir(root.join("config"));
let mut app = NotesApp::new(session, notescraft_themes::builtin("catppuccin-mocha").expect("theme"));
app.state.animations = false;
let ctx = Context::default();
let mut time = 1.0;
let mut warm = Duration::ZERO;
for _ in 0..20 {
frame(&mut app, &ctx, &mut time, &mut warm);
}
let paths: Vec<String> = (0..TABS).map(|index| format!("Widget {index}.md")).collect();
let mut slowest = Duration::ZERO;
for path in &paths {
app.open_note(path);
frame(&mut app, &ctx, &mut time, &mut slowest);
}
for path in &paths {
app.open_note(path);
frame(&mut app, &ctx, &mut time, &mut slowest);
}
for path in &paths {
app.close_note(path);
frame(&mut app, &ctx, &mut time, &mut slowest);
}
assert!(slowest < FRAME_BUDGET, "slowest frame {slowest:?} over {FRAME_BUDGET:?}");
let _ = std::fs::remove_dir_all(&root);
}