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

161 lines
5.5 KiB
Rust

//! The picker of the icon: the search finds the rocket, a click writes the icon, a colour
//! writes the tint and Remove takes the icon and the tint away.
//!
//! The picker never writes the vault: every choice is one `decor.set` command, posted for
//! the app to run.
use egui_kittest::kittest::Queryable;
use notescraft_themes::builtin;
use notescraft_ui_egui::theme::Tokens;
use notescraft_ui_egui::views::icon_picker::{Choice, IconPicker, SET_COMMAND, take_choice};
use serde_json::Value;
/// The note the picker writes to.
const NOTE: &str = "note.md";
/// What the picker draws and what it posted.
struct State {
/// The picker under test.
picker: IconPicker,
/// The choices the picker posted, in order.
choices: Vec<Choice>,
/// The tokens of the theme.
tokens: Tokens,
/// Whether the fonts are in.
warmed: bool,
}
/// The picker as the app draws it, with the choices it posted.
fn picker() -> egui_kittest::Harness<'static, State> {
let tokens = Tokens::new(builtin("catppuccin-mocha").expect("the theme is there"));
let state = State { picker: IconPicker::open(NOTE), choices: Vec::new(), tokens, warmed: false };
egui_kittest::Harness::builder().with_size(egui::Vec2::new(900.0, 700.0)).with_max_steps(200).build_ui_state(
|ui, state: &mut State| {
if !state.warmed {
notescraft_ui_egui::fonts::install(ui.ctx());
notescraft_ui_egui::motion::set_enabled(ui.ctx(), false);
state.warmed = true;
return;
}
state.picker.show(ui.ctx(), &state.tokens);
if let Some(choice) = take_choice(ui.ctx()) {
state.choices.push(choice);
}
},
state,
)
}
/// Types `text` into the one field of the picker.
fn type_in_search(harness: &mut egui_kittest::Harness<'_, State>, text: &str) {
harness.get_by_role(egui::accesskit::Role::TextInput).click();
harness.run();
harness.event(egui::Event::Text(text.to_owned()));
harness.run();
harness.run();
}
#[test]
fn the_search_finds_the_rocket_and_a_click_writes_it_once() {
let mut harness = picker();
harness.run();
harness.run();
type_in_search(&mut harness, "rock");
assert!(harness.query_by_label("rocket").is_some(), "the search shows the rocket");
assert!(harness.query_by_label("folder").is_none(), "the search hides the other icons");
harness.get_by_label("rocket").click();
harness.run();
harness.run();
assert_eq!(harness.state().choices.len(), 1, "the click wrote one command");
let choice = &harness.state().choices[0];
assert_eq!(choice.id, SET_COMMAND);
assert_eq!(choice.params["path"], NOTE);
assert_eq!(choice.params["icon"], "rocket");
}
#[test]
fn a_colour_writes_the_tint_and_the_plain_one_takes_it_away() {
let mut harness = picker();
harness.run();
harness.run();
harness.get_by_label("accent").click();
harness.run();
harness.run();
assert_eq!(harness.state().choices.len(), 1);
assert_eq!(harness.state().choices[0].id, SET_COMMAND);
assert_eq!(harness.state().choices[0].params["path"], NOTE);
assert_eq!(harness.state().choices[0].params["color"], "accent");
harness.get_by_label("No colour").click();
harness.run();
harness.run();
assert_eq!(harness.state().choices.len(), 2);
assert_eq!(harness.state().choices[1].params["color"], Value::Null);
}
#[test]
fn remove_takes_the_icon_and_the_tint_away() {
let mut harness = picker();
harness.run();
harness.run();
harness.get_by_label("Remove icon").click();
harness.run();
harness.run();
assert_eq!(harness.state().choices.len(), 1);
let params = &harness.state().choices[0].params;
assert_eq!(params["path"], NOTE);
assert_eq!(params["icon"], Value::Null);
assert_eq!(params["color"], Value::Null);
}
#[test]
fn a_picture_of_the_vault_is_written_as_a_link() {
let mut harness = picker();
harness.state_mut().picker.set_files(vec!["cover.png".to_owned(), "note.md".to_owned()]);
harness.run();
harness.run();
harness.get_by_label("File").click();
harness.run();
harness.run();
harness.get_by_label("cover.png").click();
harness.run();
harness.run();
assert_eq!(harness.state().choices.len(), 1);
assert_eq!(harness.state().choices[0].params["icon"], "[[cover.png]]");
}
#[test]
fn the_banner_part_writes_the_picture_and_takes_it_away() {
let mut harness = picker();
harness.state_mut().picker.set_files(vec!["cover.png".to_owned()]);
harness.run();
harness.run();
harness.get_by_label("Banner").click();
harness.run();
harness.run();
harness.get_by_label("cover.png").click();
harness.run();
harness.run();
assert_eq!(harness.state().choices.len(), 1);
assert_eq!(harness.state().choices[0].params["banner"], "[[cover.png]]");
harness.get_by_label("Remove banner").click();
harness.run();
harness.run();
assert_eq!(harness.state().choices.len(), 2);
assert_eq!(harness.state().choices[1].params["banner"], Value::Null);
}
/// Renders the popover to `NOTESCRAFT_ICON_PICKER_PNG`.
#[test]
#[ignore = "needs a GPU adapter; run by hand with NOTESCRAFT_ICON_PICKER_PNG=/path/out"]
fn the_picker_renders() {
let Ok(out) = std::env::var("NOTESCRAFT_ICON_PICKER_PNG") else { return };
let mut harness = picker();
harness.state_mut().picker.set_files(vec!["cover.png".to_owned(), "icons/star.svg".to_owned()]);
harness.run();
harness.run();
harness.render().expect("the frame renders").save(out).expect("the png is written");
}