171 lines
7.2 KiB
Rust
171 lines
7.2 KiB
Rust
//! Registry browser driven through real egui frames with cached engine-shaped replies.
|
|
|
|
use egui::{Event, Vec2};
|
|
use egui_kittest::{Harness, kittest::Queryable};
|
|
use notescraft_ui_egui::{
|
|
theme::Tokens,
|
|
views::registry_view::{Action, RegistryView},
|
|
};
|
|
use serde_json::json;
|
|
|
|
struct Page {
|
|
view: RegistryView,
|
|
actions: Vec<Action>,
|
|
tokens: Tokens,
|
|
warmed: bool,
|
|
}
|
|
|
|
fn rows() -> serde_json::Value {
|
|
json!([
|
|
{"id":"twig","kind":"grammar","title":"Twig","summary":"Syntax highlighting for Twig templates.","version":"1.2.0","size":3900,"state":"available"},
|
|
{"id":"dice","kind":"plugin","title":"Dice","summary":"Roll dice in your notes.","version":"1.2.0","installed_version":"1.0.0","previous":"0.9.0","size":4800,"state":"update","permissions":["vault.read","vault.write","editor"],"license":"MIT","source":"https://example.test/dice","key_id":"first-party","files":[{"path":"plugin.wasm","size":4800}]},
|
|
{"id":"theme","kind":"theme","title":"Frappe","version":"1.0.0","state":"installed","installed_version":"1.0.0"},
|
|
{"id":"old","kind":"plugin","title":"Revoked plugin","state":"revoked"},
|
|
{"id":"future","kind":"plugin","title":"Future plugin","state":"incompatible","min_app_version":"0.3"},
|
|
{"id":"font","kind":"font","title":"Math font","state":"unsupported"}
|
|
])
|
|
}
|
|
|
|
fn page(theme: &str, width: f32) -> Harness<'static, Page> {
|
|
let mut view = RegistryView::default();
|
|
view.apply_reply(&rows());
|
|
let page = Page { view, actions: Vec::new(), tokens: Tokens::new(notescraft_themes::builtin(theme).expect("theme")), warmed: false };
|
|
Harness::builder().with_size(Vec2::new(width, 1200.0)).build_ui_state(
|
|
|ui, page: &mut Page| {
|
|
if !page.warmed {
|
|
notescraft_ui_egui::fonts::install(ui.ctx());
|
|
notescraft_ui_egui::motion::set_enabled(ui.ctx(), false);
|
|
notescraft_ui_egui::style::apply(ui.ctx(), &page.tokens);
|
|
page.warmed = true;
|
|
return;
|
|
}
|
|
egui::Frame::new().fill(notescraft_ui_egui::theme::color(page.tokens.theme.colors.canvas)).inner_margin(24).show(ui, |ui| {
|
|
page.actions.extend(page.view.show(ui, &page.tokens));
|
|
});
|
|
},
|
|
page,
|
|
)
|
|
}
|
|
|
|
fn settle(h: &mut Harness<'_, Page>) {
|
|
h.run_steps(4);
|
|
}
|
|
|
|
#[test]
|
|
fn every_row_state_and_install_click() {
|
|
let mut h = page("catppuccin-mocha", 1000.0);
|
|
settle(&mut h);
|
|
for label in ["Install", "Update to 1.2.0", "Installed", "Revoked", "Needs NotesCraft 0.3", "Not available in this version"] {
|
|
assert!(h.query_by_label(label).is_some(), "missing {label}");
|
|
}
|
|
h.get_by_label("Install").click();
|
|
settle(&mut h);
|
|
assert_eq!(h.state().actions, vec![Action::Install("grammar".to_owned(), "twig".to_owned())]);
|
|
}
|
|
|
|
#[test]
|
|
fn typing_search_and_kind_filter_are_local() {
|
|
let mut h = page("catppuccin-latte", 1000.0);
|
|
settle(&mut h);
|
|
h.get_by_role(egui::accesskit::Role::TextInput).click();
|
|
h.step();
|
|
h.input_mut().events.push(Event::Text("Twig".to_owned()));
|
|
settle(&mut h);
|
|
assert!(h.query_by_label("Twig").is_some());
|
|
assert!(h.query_by_label("Dice").is_none());
|
|
assert_eq!(h.state().actions, Vec::<Action>::new());
|
|
h.get_by_role_and_label(egui::accesskit::Role::Button, "Plugins").click();
|
|
settle(&mut h);
|
|
assert!(h.query_by_label("Twig").is_none());
|
|
assert!(h.query_by_label("No add-ons found. Try another search or check for updates.").is_some());
|
|
assert_eq!(h.state().actions, Vec::<Action>::new());
|
|
}
|
|
|
|
#[test]
|
|
fn offline_error_and_empty_states_preserve_cache() {
|
|
let mut h = page("catppuccin-mocha", 1000.0);
|
|
h.state_mut().view.set_error("offline", "Connection refused");
|
|
settle(&mut h);
|
|
assert!(h.query_by_label("Offline — showing cached add-ons. Try checking again when connected.").is_some());
|
|
assert!(h.query_by_label("Connection refused").is_some());
|
|
assert!(h.query_by_label("Twig").is_some());
|
|
h.state_mut().view.rows.clear();
|
|
settle(&mut h);
|
|
assert!(h.query_by_label("No add-ons found. Try another search or check for updates.").is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_signing_key_error_stays_visible_without_discarding_cache() {
|
|
let mut h = page("catppuccin-mocha", 1000.0);
|
|
let message = notescraft_engine::Error::Addon("unknown signing key `first-party`".to_owned()).to_string();
|
|
h.state_mut().view.set_error("addon", &message);
|
|
settle(&mut h);
|
|
assert!(h.query_by_label(&message).is_some());
|
|
assert!(h.query_by_label("Twig").is_some());
|
|
assert!(!h.state().view.offline);
|
|
assert_eq!(h.state().actions, Vec::<Action>::new());
|
|
}
|
|
|
|
#[test]
|
|
fn permissions_are_sentences_and_detail_actions_are_explicit() {
|
|
let mut h = page("catppuccin-mocha", 1000.0);
|
|
h.state_mut().view.selected = Some(("plugin".to_owned(), "dice".to_owned()));
|
|
settle(&mut h);
|
|
for label in ["Read notes and search", "Create and change notes", "Edit the open note", "Remove", "Rollback"] {
|
|
assert!(h.query_by_label(label).is_some(), "missing {label}");
|
|
}
|
|
assert_eq!(h.state().actions, Vec::<Action>::new());
|
|
assert!(h.query_by_role_and_label(egui::accesskit::Role::CheckBox, "Enable in this vault").is_some());
|
|
h.get_by_label("Rollback").click();
|
|
settle(&mut h);
|
|
assert_eq!(h.state().actions, vec![Action::Rollback("plugin".to_owned(), "dice".to_owned())]);
|
|
}
|
|
|
|
#[test]
|
|
fn update_badge_requires_a_successful_check() {
|
|
let mut h = page("catppuccin-mocha", 1000.0);
|
|
settle(&mut h);
|
|
assert_eq!(h.state().view.updates(), 0);
|
|
assert!(h.query_by_label("1 updates").is_none());
|
|
h.get_by_label("Check for updates").click();
|
|
settle(&mut h);
|
|
assert_eq!(h.state().actions, vec![Action::Check]);
|
|
assert_eq!(h.state().view.updates(), 0);
|
|
h.state_mut().view.last_checked = Some("just now".to_owned());
|
|
settle(&mut h);
|
|
assert!(h.query_by_label("1 updates").is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_reply_does_not_discard_cache_and_info_keeps_install_state() {
|
|
let mut view = RegistryView::default();
|
|
view.apply_reply(&rows());
|
|
view.apply_reply(&json!({"entries":[{"permissions":12}]}));
|
|
assert_eq!(view.rows.len(), 6);
|
|
assert!(view.error.is_some());
|
|
view.apply_info(&json!({"kind":"plugin","id":"dice","summary":"Updated metadata"}));
|
|
let row = view.rows.iter().find(|row| row.id == "dice").expect("dice");
|
|
assert_eq!(row.summary, "Updated metadata");
|
|
assert_eq!(row.installed_version.as_deref(), Some("1.0.0"));
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "offscreen PNG visual review"]
|
|
fn render_registry_pngs() {
|
|
let output = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/agent-sol/png");
|
|
std::fs::create_dir_all(&output).expect("png folder");
|
|
for (theme, width, name) in [
|
|
("catppuccin-mocha", 1000.0, "registry-mocha.png"),
|
|
("catppuccin-latte", 1000.0, "registry-latte.png"),
|
|
("catppuccin-mocha", 390.0, "registry-compact.png"),
|
|
] {
|
|
let mut h = page(theme, width);
|
|
settle(&mut h);
|
|
h.render().expect("render").save(output.join(name)).expect("png");
|
|
}
|
|
let mut h = page("catppuccin-mocha", 1000.0);
|
|
h.state_mut().view.selected = Some(("plugin".to_owned(), "dice".to_owned()));
|
|
settle(&mut h);
|
|
h.render().expect("render detail").save(output.join("registry-detail.png")).expect("png");
|
|
}
|