GitCorner

Code

ossartcraftwordcraft🌐
6ef13b2cc8a6
use super::*;

fn ctx() -> egui::Context {
    let ctx = egui::Context::default();
    crate::theme::install_fonts(&ctx);
    crate::theme::apply(&ctx, crate::theme::Appearance::Light);
    ctx.run_ui(egui::RawInput::default(), |_| {}).drop_without_applying_deltas();
    ctx
}

fn frame(ctx: &egui::Context, n: &mut u32, events: Vec<egui::Event>, a: &mut WordApp) {
    *n += 1;
    let input = egui::RawInput { time: Some(f64::from(*n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() };
    ctx.run_ui(input, |ui| title_bar(a, ui)).drop_without_applying_deltas();
}

fn click(ctx: &egui::Context, n: &mut u32, pos: egui::Pos2, a: &mut WordApp) {
    let button = |pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
    frame(ctx, n, vec![egui::Event::PointerMoved(pos), button(true)], a);
    frame(ctx, n, vec![button(false)], a);
}

/// #701: the arrow beside Undo lists the session's undo steps, newest first; clicking the third
/// row undoes three steps at once.
#[test]
fn undo_list_matches_the_session_and_undoes_up_to_the_clicked_row() {
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    a.run("text.insert", json!({"text": "Hello"})).unwrap();
    a.run("select.all", json!({})).unwrap();
    a.run("format.bold", json!({})).unwrap();
    a.run("format.italic", json!({})).unwrap();
    a.run("caret.docEnd", json!({})).unwrap();
    a.run("text.insert", json!({"text": " world"})).unwrap();
    let session: Vec<String> = a.session.undo_history().into_iter().map(str::to_string).collect();
    assert_eq!(session, ["Typing", "Italic", "Bold", "Typing"]);
    assert_eq!(undo_list_rows(&a), session);
    assert_eq!(undo_list_footer(0), "Cancel");
    assert_eq!(undo_list_footer(1), "Undo 1 Action");
    assert_eq!(undo_list_footer(3), "Undo 3 Actions");

    let (ctx, mut n) = (ctx(), 0);
    frame(&ctx, &mut n, vec![], &mut a);
    let arrow = ctx.read_response(undo_list_button_id()).unwrap();
    let popup = egui::Popup::default_response_id(&arrow);
    click(&ctx, &mut n, arrow.rect.center(), &mut a);
    assert!(egui::Popup::is_id_open(&ctx, popup), "the arrow opens the Undo list");
    frame(&ctx, &mut n, vec![], &mut a);
    for i in 0..4 {
        assert!(ctx.read_response(undo_list_row_id(i)).is_some(), "row {i} is drawn");
    }
    assert!(ctx.read_response(undo_list_row_id(4)).is_none(), "one row per step");
    let third = ctx.read_response(undo_list_row_id(2)).unwrap();
    click(&ctx, &mut n, third.rect.center(), &mut a);
    assert_eq!(a.session.undo_history(), ["Typing"], "three steps undone in one go");
    assert_eq!(a.session.doc.plain_text(wordcraft_doc::StoryRef::Body), "Hello");
    frame(&ctx, &mut n, vec![], &mut a);
    assert!(!egui::Popup::is_id_open(&ctx, popup), "the list closes");
}

/// #1975: the zoom percentage gains and loses digits (9% β†’ 100% β†’ 500%); the controls to its
/// left must not shift with it.
#[test]
fn status_bar_controls_hold_still_across_zoom_levels() {
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    let ctx = ctx();
    ctx.enable_accesskit();
    let names = ["Focus", "Print Layout", "Web Layout"];
    let mut seen: Vec<(f32, Vec<f64>)> = Vec::new();
    for (n, zoom) in [10.0_f32, 9.0, 100.0, 500.0].into_iter().enumerate() {
        a.canvas.scale = zoom / 100.0 * crate::canvas::PX_PER_PT;
        let input = egui::RawInput {
            time: Some(n as f64 / 60.0),
            screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(1200.0, 800.0))),
            ..Default::default()
        };
        let mut out = ctx.run_ui(input, |ui| status_bar(&mut a, ui));
        out.textures_delta.clear();
        let update = out.platform_output.accesskit_update.take().unwrap();
        let xs: Vec<f64> = names
            .iter()
            .map(|name| {
                let node = update.nodes.iter().find(|(_, node)| node.label() == Some(*name)).unwrap_or_else(|| panic!("{name} is drawn")).1.clone();
                node.bounds().unwrap().x0
            })
            .collect();
        seen.push((zoom, xs));
    }
    let first = &seen[0].1;
    for (zoom, xs) in &seen {
        assert_eq!(xs, first, "status bar controls moved at {zoom}% zoom");
    }
}

/// #2797: the count line's wording per unit β€” words (the default), characters with spaces,
/// characters without spaces β€” whole document or "selected of total".
#[test]
fn the_count_line_words_each_unit() {
    for (unit, total, selected, expected) in [
        (StatusCount::Words, 12, None, "12 words"),
        (StatusCount::Words, 12, Some(3), "3 of 12 words"),
        (StatusCount::Characters, 23, None, "23 characters"),
        (StatusCount::Characters, 23, Some(6), "6 of 23 characters"),
        (StatusCount::CharactersNoSpaces, 21, None, "21 characters (no spaces)"),
        (StatusCount::CharactersNoSpaces, 21, Some(6), "6 of 21 characters (no spaces)"),
    ] {
        assert_eq!(status_count_text(unit, total, selected), expected, "{unit:?} with {selected:?} selected");
    }
}

/// #2797: the status bar's totals are the Word Count dialog's numbers (words, characters with
/// spaces, characters without spaces), and its selection line counts the selection the same way.
#[test]
fn the_status_bar_totals_are_the_word_count_dialogs_numbers() {
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    a.run("text.insert", json!({"text": "Hello world.\nSecond line"})).unwrap();
    // Straight from the engine: `a.run` would only open the Word Count dialog (#240).
    let stats = a.session.run("review.wordCount", &json!({})).unwrap();
    let counts = a.cached_status_counts();
    assert_eq!(counts.words, stats["words"].as_u64().unwrap_or_default() as usize, "words, as the dialog counts them");
    assert_eq!(counts.chars, stats["charactersWithSpaces"].as_u64().unwrap_or_default() as usize, "characters with spaces");
    assert_eq!(counts.chars_no_spaces, stats["characters"].as_u64().unwrap_or_default() as usize, "characters without spaces");
    // "world." selected: its own count of the chosen unit over the document's total.
    a.run("select.text", json!({"text": "world."})).unwrap();
    let line = |unit: StatusCount| {
        let selected = crate::count_in_unit(&a.session.selected_text(), unit);
        status_count_text(unit, counts.in_unit(unit), Some(selected))
    };
    assert_eq!(line(StatusCount::Words), "1 of 4 words");
    assert_eq!(line(StatusCount::Characters), "6 of 23 characters");
    assert_eq!(line(StatusCount::CharactersNoSpaces), "6 of 21 characters (no spaces)");
}

/// #2797: the status bar draws the unit the person chose, and right-clicking the count offers
/// the three units, the choice taking effect at once and kept between runs.
#[test]
fn the_status_bar_count_is_chosen_from_its_context_menu() {
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    a.run("text.insert", json!({"text": "Hello world.\nSecond line"})).unwrap();
    let ctx = ctx();
    ctx.enable_accesskit();
    let mut n = 0;
    // What the status bar drew last frame: each widget's label and where it sits.
    let drawn = |a: &mut WordApp, n: &mut u32, events: Vec<egui::Event>| -> Vec<(String, egui::Rect)> {
        *n += 1;
        let input = egui::RawInput {
            time: Some(f64::from(*n) / 60.0),
            screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(1200.0, 800.0))),
            events,
            ..Default::default()
        };
        let mut out = ctx.run_ui(input, |ui| status_bar(a, ui));
        out.textures_delta.clear();
        let update = out.platform_output.accesskit_update.take().unwrap();
        update
            .nodes
            .into_iter()
            .filter_map(|(_, node)| {
                let b = node.bounds()?;
                Some((node.label()?.to_string(), egui::Rect::from_min_max(pos2(b.x0 as f32, b.y0 as f32), pos2(b.x1 as f32, b.y1 as f32))))
            })
            .collect()
    };
    let at =
        |seen: &[(String, egui::Rect)], label: &str| seen.iter().find(|(l, _)| l == label).unwrap_or_else(|| panic!("{label} is drawn")).1.center();
    let button = |at: egui::Pos2, button: egui::PointerButton, pressed: bool| egui::Event::PointerButton {
        pos: at,
        button,
        pressed,
        modifiers: Default::default(),
    };
    // Words are counted: the default unit.
    let mut seen = drawn(&mut a, &mut n, vec![]);
    assert!(seen.iter().any(|(l, _)| l == "4 words"), "{seen:?}");
    // Right-click the count: the menu lists the three units.
    let count = at(&seen, "4 words");
    drawn(&mut a, &mut n, vec![egui::Event::PointerMoved(count), button(count, egui::PointerButton::Secondary, true)]);
    drawn(&mut a, &mut n, vec![button(count, egui::PointerButton::Secondary, false)]);
    seen = drawn(&mut a, &mut n, vec![]);
    for label in ["Words", "Characters (with spaces)", "Characters (no spaces)"] {
        assert!(seen.iter().any(|(l, _)| l == label), "the menu lists {label}: {seen:?}");
    }
    // Pick characters with spaces: the count changes at once, with no document change.
    let pick = at(&seen, "Characters (with spaces)");
    drawn(&mut a, &mut n, vec![egui::Event::PointerMoved(pick), button(pick, egui::PointerButton::Primary, true)]);
    drawn(&mut a, &mut n, vec![button(pick, egui::PointerButton::Primary, false)]);
    assert_eq!(a.ui.status_count, StatusCount::Characters, "the menu's choice is taken");
    assert_eq!(a.run("ui.statusCount", json!({})).unwrap()["statusCount"], "characters", "and answered to scripts");
    seen = drawn(&mut a, &mut n, vec![]);
    assert!(seen.iter().any(|(l, _)| l == "23 characters"), "{seen:?}");
    // With a selection: "selected of total".
    a.run("select.text", json!({"text": "world."})).unwrap();
    seen = drawn(&mut a, &mut n, vec![]);
    assert!(seen.iter().any(|(l, _)| l == "6 of 23 characters"), "{seen:?}");
    // The choice comes back after a restart.
    let mut b = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    let saved = serde_json::to_vec(&a.prefs()).unwrap();
    b.apply_prefs(crate::UiState::from_json(&saved).unwrap());
    assert_eq!(b.ui.status_count, StatusCount::Characters, "kept between runs");
}

/// #2402: a long message (the path of a file that isn't there) is shortened so that it stops
/// before the controls on the right, instead of being drawn over the Focus button.
#[test]
fn a_long_status_message_stops_before_the_controls() {
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    let long = "C:\\Test\\Documents\\Reports\\Final individual test folder\\Second level folder\\Project - Gadgets and sleep of teenagers.docx: The system cannot find the path specified. (os error 3)";
    a.status_msg = Some((long.to_string(), crate::now_ms()));
    let ctx = ctx();
    ctx.enable_accesskit();
    let input =
        egui::RawInput { time: Some(0.0), screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(1200.0, 800.0))), ..Default::default() };
    let mut whole = 0.0;
    let mut out = ctx.run_ui(input, |ui| {
        status_bar(&mut a, ui);
        // The width the whole message would take, unshortened.
        let t = Tokens::get(ui.ctx());
        let font = TypeRung::Caption.regular();
        let track = TypeRung::Caption.tracking();
        whole = text_width(ui.painter(), long, &font, track, t.accent_text);
    });
    out.textures_delta.clear();
    let update = out.platform_output.accesskit_update.take().unwrap();
    let focus = update.nodes.iter().find(|(_, node)| node.label() == Some("Focus")).unwrap_or_else(|| panic!("Focus is drawn")).1.bounds().unwrap();
    let msg = ctx.read_response(status_message_id()).expect("the message is drawn");
    assert!(msg.rect.width() < whole, "the message is shortened: {:?} of {whole}", msg.rect);
    assert!(f64::from(msg.rect.right()) <= focus.x0, "the message stops before the Focus button: {:?} vs {}", msg.rect, focus.x0);
}

/// The status bar's "editing a non-body story" line named every one of them "Editing
/// header/footer", footnotes and endnotes included β€” it only ever checked "not the body,
/// not a text box", with no regard for which kind of part it actually was.
#[test]
fn status_bar_names_the_actual_part_being_edited() {
    let label = |a: &mut WordApp| {
        let ctx = ctx();
        ctx.enable_accesskit();
        let input = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(1200.0, 800.0))), ..Default::default() };
        let mut out = ctx.run_ui(input, |ui| status_bar(a, ui));
        out.textures_delta.clear();
        let update = out.platform_output.accesskit_update.take().unwrap();
        for prefix in ["Editing footnote", "Editing endnote", "Editing header/footer"] {
            if update.nodes.iter().any(|(_, node)| node.label().is_some_and(|l| l == prefix)) {
                return prefix.to_string();
            }
        }
        "(none shown)".to_string()
    };
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    a.run("text.insert", json!({"text": "Body text"})).unwrap();
    a.run("references.footnote", json!({})).unwrap();
    assert_eq!(label(&mut a), "Editing footnote");
    a.run("caret.docEnd", json!({})).unwrap();
    a.run("references.endnote", json!({})).unwrap();
    assert_eq!(label(&mut a), "Editing endnote");
    a.run("insert.createHeader", json!({})).unwrap();
    assert_eq!(label(&mut a), "Editing header/footer");
}

/// #1903: the footer's Cancel row closes the Undo list without undoing anything.
#[test]
fn undo_list_cancel_closes_without_undoing() {
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    a.run("text.insert", json!({"text": "Hello"})).unwrap();
    let before: Vec<String> = a.session.undo_history().into_iter().map(str::to_string).collect();
    assert!(!before.is_empty());

    let (ctx, mut n) = (ctx(), 0);
    frame(&ctx, &mut n, vec![], &mut a);
    let arrow = ctx.read_response(undo_list_button_id()).unwrap();
    let popup = egui::Popup::default_response_id(&arrow);
    click(&ctx, &mut n, arrow.rect.center(), &mut a);
    assert!(egui::Popup::is_id_open(&ctx, popup), "the arrow opens the Undo list");
    frame(&ctx, &mut n, vec![], &mut a);
    let cancel = ctx.read_response(undo_list_cancel_id()).expect("Cancel is drawn");
    click(&ctx, &mut n, cancel.rect.center(), &mut a);
    let after: Vec<String> = a.session.undo_history().into_iter().map(str::to_string).collect();
    assert_eq!(after, before, "nothing undone");
    frame(&ctx, &mut n, vec![], &mut a);
    assert!(!egui::Popup::is_id_open(&ctx, popup), "Cancel closes the list");
}

#[test]
fn mac_title_bar_centres_and_clears_the_traffic_lights() {
    // The lights are centred in the bar, and content keeps the same gap after them.
    let margin = (MAC_TITLE_BAR - MAC_LIGHT) / 2.0;
    let lights_end = MAC_LIGHTS_LEFT + 2.0 * MAC_LIGHT_PITCH + MAC_LIGHT;
    assert_eq!(MAC_CONTENT_LEFT - lights_end, margin);
    // `egui::Margin` is in `i8`.
    assert!(MAC_CONTENT_LEFT <= f32::from(i8::MAX));
}

#[test]
fn mac_title_bar_follows_the_measured_lights() {
    // The standard lights give the standard bar.
    let lights_end = MAC_LIGHTS_LEFT + 2.0 * MAC_LIGHT_PITCH + MAC_LIGHT;
    assert_eq!(MacTitleBar::around_lights(lights_end, MAC_TITLE_BAR / 2.0, MAC_LIGHT), MacTitleBar::default());
    // Larger, lower lights (macOS 26): a taller bar, and content clear of the green button.
    let bar = MacTitleBar::around_lights(78.0, 18.0, 14.0);
    assert_eq!(bar.height, 36.0);
    assert_eq!(bar.content_left, 89.0);
    // Never tighter than the standard layout.
    assert_eq!(MacTitleBar::around_lights(40.0, 10.0, 12.0), MacTitleBar::default());
}

/// #1945: Hide Quick Access Toolbar, the last item of the toolbar's menu, takes the toolbar's
/// buttons and its … button out of the title bar, which keeps its title; the command brings
/// them back.
#[test]
fn hide_quick_access_toolbar_takes_the_toolbar_out_of_the_title_bar() {
    use egui_kittest::kittest::Queryable;
    let a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(
        |ui, app: &mut WordApp| {
            let ctx = ui.ctx().clone();
            app.logic(&ctx);
            // The fonts `logic` installs take effect on the next frame, as `WordApp::ui` waits.
            if app.fonts_frames < 2 {
                app.fonts_frames += 1;
                return;
            }
            title_bar(app, ui);
        },
        a,
    );
    h.run_steps(4);
    let drawn = |h: &egui_kittest::Harness<'static, WordApp>, id: &str| h.ctx.read_response(qat_button_id(id)).is_some();
    for id in ["file.save", "edit.undo", "edit.redo"] {
        assert!(drawn(&h, id), "{id} is on the toolbar");
    }
    let at = h.ctx.read_response(qat_menu_button_id()).expect("the … button").rect.center();
    h.event(egui::Event::PointerMoved(at));
    for pressed in [true, false] {
        h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });
    }
    h.run_steps(2);
    h.get_by_label("Hide Quick Access Toolbar").click();
    // `read_response` also reads the frame before last, so run two frames past the change.
    h.run_steps(3);
    assert!(!h.state().ui.quick_access_toolbar, "the menu's last item hides the toolbar");
    assert!(h.query_by_label("Hide Quick Access Toolbar").is_none(), "and closes the menu");
    for id in ["file.save", "edit.undo", "edit.redo"] {
        assert!(!drawn(&h, id), "{id} is gone");
    }
    assert!(h.ctx.read_response(qat_menu_button_id()).is_none(), "so is the … button");
    assert!(h.query_by_label_contains("Not saved").is_some(), "the title is still drawn");
    h.state_mut().run("ui.quickAccessToolbar", json!({"value": true})).unwrap();
    h.run_steps(2);
    for id in ["file.save", "edit.undo", "edit.redo"] {
        assert!(drawn(&h, id), "{id} is back");
    }
    assert!(h.ctx.read_response(qat_menu_button_id()).is_some());
}

/// #3040: clicking a row in the Undo list hands the keyboard back to the document.
#[test]
fn undo_list_row_returns_focus_to_the_document() {
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    a.run("text.insert", json!({"text": "Hello"})).unwrap();
    assert_eq!(a.session.undo_history().len(), 1);

    let (ctx, mut n) = (ctx(), 0);
    frame(&ctx, &mut n, vec![], &mut a);
    let arrow = ctx.read_response(undo_list_button_id()).unwrap();
    let popup = egui::Popup::default_response_id(&arrow);
    click(&ctx, &mut n, arrow.rect.center(), &mut a);
    assert!(egui::Popup::is_id_open(&ctx, popup), "the arrow opens the Undo list");
    frame(&ctx, &mut n, vec![], &mut a);
    let row = ctx.read_response(undo_list_row_id(0)).unwrap();
    a.canvas.want_focus = false;
    click(&ctx, &mut n, row.rect.center(), &mut a);
    assert!(a.canvas.want_focus, "the row click returns focus to the document");
    assert!(a.session.undo_history().is_empty(), "the step was undone");
}

/// A saved .docx (so AutoSave can cover it) with the title bar drawn over it.
fn autosave_harness() -> (egui_kittest::Harness<'static, WordApp>, std::path::PathBuf) {
    let dir = std::env::temp_dir().join(format!("wordcraft-ui-autosave-label-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let path = dir.join("draft.docx");
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    a.run("text.insert", json!({"text": "Hello"})).unwrap();
    a.run("file.save", json!({"path": path.to_string_lossy()})).unwrap();
    assert!(a.autosaves(), "a saved .docx is covered by AutoSave");
    let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(
        |ui, app: &mut WordApp| {
            let ctx = ui.ctx().clone();
            app.logic(&ctx);
            // The fonts `logic` installs take effect on the next frame, as `WordApp::ui` waits.
            if app.fonts_frames < 2 {
                app.fonts_frames += 1;
                return;
            }
            title_bar(app, ui);
        },
        a,
    );
    h.run_steps(4);
    (h, dir)
}

/// #3370: the AutoSave text is part of the switch, not a caption beside it: clicking the text
/// toggles AutoSave, as clicking the switch does.
#[test]
fn clicking_the_autosave_label_toggles_autosave() {
    use egui_kittest::kittest::Queryable;
    let (mut h, dir) = autosave_harness();
    assert!(h.state().autosaves());
    h.get_by_label("AutoSave").click();
    h.run_steps(2);
    assert!(!h.state().autosaves(), "clicking the text turns AutoSave off");
    h.get_by_label("AutoSave").click();
    h.run_steps(2);
    assert!(h.state().autosaves(), "and clicking it again turns it back on");
    let _ = std::fs::remove_dir_all(dir);
}

#[test]
fn the_font_box_warns_where_the_status_bar_does_and_before_typing() {
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    let missing = "Wordcraft Nowhere Face";
    // An empty paragraph in a missing font: no letters show it yet, still worth a warning.
    a.session.run("select.all", &json!({})).unwrap();
    a.session.run("format.font", &json!({"name": missing})).unwrap();
    assert!(a.font_box_warning(missing).is_some(), "an empty paragraph warns");
    // Text in it: the status bar's list decides.
    a.session.run("text.insert", &json!({"text": "Hello"})).unwrap();
    let l = a.session.layout();
    let (list, _) = a.cached_missing_fonts(&l);
    assert!(list.iter().any(|f| f.font == missing), "{list:?}");
    assert!(a.font_box_warning(missing).is_some());
    // A missing font just picked at the end of text in an installed one: the next letter will be
    // drawn in a substitute, so the box warns before it is typed.
    a.session.run("select.all", &json!({})).unwrap();
    a.session.run("format.font", &json!({"name": "JetBrains Mono"})).unwrap();
    a.session.run("caret.docEnd", &json!({})).unwrap();
    a.session.run("format.font", &json!({"name": "Wordcraft Just Picked"})).unwrap();
    assert!(a.font_box_warning("Wordcraft Just Picked").is_some(), "picked at the caret");
    // Over a selection the box shows the selected text's font, which the list decides.
    a.session.run("select.all", &json!({})).unwrap();
    assert!(a.font_box_warning("Wordcraft Half Typed").is_none());
}

#[test]
fn the_east_asian_font_box_names_the_face_its_text_is_drawn_in() {
    // MS Mincho is roman in many font tables; the run's font is drawn in Cambria then, but CJK
    // text never is: it goes to the run's font or a fallback face.
    let mincho = "Wordcraft Mincho Gone";
    let mut doc = wordcraft_doc::Document::new();
    doc.settings.roman_fonts = vec![mincho.into()];
    let mut a = WordApp::new(wordcraft_engine::Session::new(doc), crate::Services::default());
    let want = wordcraft_engine::cmd::font::east_asian_shown_as(&a.session.doc, mincho, "JetBrains Mono");
    assert!(want.is_some(), "not installed");
    assert_eq!(a.east_asian_font_box_warning(mincho, "JetBrains Mono"), want, "an empty paragraph warns");
}

#[test]
fn a_screen_reader_hears_the_font_box_warning() {
    use egui_kittest::kittest::{NodeT, Queryable};
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    a.session.run("text.insert", &json!({"text": "Hello"})).unwrap();
    a.session.run("select.all", &json!({})).unwrap();
    a.session.run("format.font", &json!({"name": "Wordcraft Nowhere Face"})).unwrap();
    let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(
        |ui, app: &mut WordApp| {
            let ctx = ui.ctx().clone();
            app.logic(&ctx);
            app.ui(ui);
        },
        a,
    );
    h.run_steps(6);
    let described = h
        .query_all_by_role(egui::accesskit::Role::TextInput)
        .filter_map(|n| n.accesskit_node().description())
        .any(|d| d.contains("Wordcraft Nowhere Face") && d.contains("isn't installed"));
    assert!(described, "the font box describes its warning");
}

#[test]
fn the_font_dialog_warns_for_a_missing_font_over_a_selection() {
    // Selected Latin text: no East Asian text shows the Asian font, and none shows a font just
    // typed into the dialog, so the list has neither; OK would apply them all the same.
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    a.session.run("text.insert", &json!({"text": "Hello"})).unwrap();
    a.session.run("select.all", &json!({})).unwrap();
    a.session.run("format.font", &json!({"name": "JetBrains Mono"})).unwrap();
    assert!(a.east_asian_font_box_warning("Wordcraft Mincho Gone", "JetBrains Mono").is_some(), "the Asian box");
    assert!(a.font_dialog_warning("Wordcraft Dialog Gone").is_some(), "the Latin box");
    assert!(a.font_dialog_warning("JetBrains Mono").is_none(), "an installed font");
    // The ribbon's box over the selection shows the selected text's font: the list decides.
    assert!(a.font_box_warning("Wordcraft Dialog Gone").is_none());
}

#[test]
fn the_asian_box_names_the_face_cjk_text_gets_even_for_a_font_latin_text_uses() {
    // The status bar lists the font with the face its Latin text is drawn in; Chinese, Japanese
    // and Korean text in it would go to the Latin text font or a fallback face instead.
    let gone = "Wordcraft Both Ways Gone";
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
    a.session.run("text.insert", &json!({"text": "Hello"})).unwrap();
    a.session.run("select.all", &json!({})).unwrap();
    a.session.run("format.font", &json!({"name": gone})).unwrap();
    assert!(a.font_box_warning(gone).is_some(), "listed for its Latin text");
    let want = wordcraft_engine::cmd::font::east_asian_shown_as(&a.session.doc, gone, "JetBrains Mono");
    assert!(want.is_some());
    assert_eq!(a.east_asian_font_box_warning(gone, "JetBrains Mono"), want);
}

/// The AutoSave knob is at the right when the switch is on and at the left when it is off; a
/// right-to-left interface mirrors that.
#[test]
fn the_autosave_knob_sits_right_when_on_and_mirrors_right_to_left() {
    let track = Rect::from_min_max(egui::pos2(100.0, 0.0), egui::pos2(130.0, 16.0));
    assert_eq!(autosave_knob_x(false, true, track), 122.0, "left to right, on: right");
    assert_eq!(autosave_knob_x(false, false, track), 108.0, "left to right, off: left");
    assert_eq!(autosave_knob_x(true, true, track), 108.0, "right to left, on: left");
    assert_eq!(autosave_knob_x(true, false, track), 122.0, "right to left, off: right");
}

/// #5886: in a narrow window the title bar's search box gives up width first, then Discord its
/// label, then the search box its text; a wide window keeps them as they were.
#[test]
fn the_title_bar_controls_fit_a_narrow_window() {
    let fit = |room| TitleBarFit::new(room, true, 26.0);
    assert_eq!(fit(1000.0), TitleBarFit { search: 260.0, discord_label: true });
    assert_eq!(fit(300.0), TitleBarFit { search: 208.0, discord_label: true });
    assert_eq!(fit(200.0), TitleBarFit { search: 170.0, discord_label: false });
    assert_eq!(fit(120.0), TitleBarFit { search: 26.0, discord_label: false });
    assert_eq!(TitleBarFit::new(200.0, false, 26.0).search, 200.0, "without Discord the search box has its room");
}

/// The labelled controls drawn by `draw` in a window `width` wide at `scale`: (label, left, right).
fn labelled_scaled(a: &mut WordApp, width: f32, scale: f32, draw: fn(&mut WordApp, &mut Ui)) -> Vec<(String, f64, f64)> {
    let ctx = ctx();
    if (scale - 1.0).abs() > 0.001 {
        ctx.set_zoom_factor(scale);
    }
    ctx.enable_accesskit();
    let input = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(width, 700.0))), ..Default::default() };
    let mut out1 = ctx.run_ui(input.clone(), |ui| draw(a, ui));
    out1.textures_delta.clear();
    let mut out = ctx.run_ui(input, |ui| draw(a, ui));
    out.textures_delta.clear();
    let update = out.platform_output.accesskit_update.take().unwrap();
    update
        .nodes
        .iter()
        .filter_map(|(_, node)| Some((node.label()?.to_string(), node.bounds()?)))
        .filter(|(_, b)| b.width() > 0.0)
        .map(|(l, b)| (l, b.x0, b.x1))
        .collect()
}

/// The text strings painted by `draw` in a window `width` wide after settling controls width.
fn painted_texts(a: &mut WordApp, width: f32, draw: fn(&mut WordApp, &mut Ui)) -> Vec<String> {
    /// Recursively collects painted text strings from egui shapes.
    fn extract(shape: &egui::Shape, out: &mut Vec<String>) {
        match shape {
            egui::Shape::Text(t) => out.push(t.galley.text().to_string()),
            egui::Shape::Vec(v) => {
                for s in v {
                    extract(s, out);
                }
            }
            _ => {}
        }
    }
    let ctx = ctx();
    let input = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(width, 700.0))), ..Default::default() };
    let mut out1 = ctx.run_ui(input.clone(), |ui| draw(a, ui));
    out1.textures_delta.clear();
    let mut out = ctx.run_ui(input, |ui| draw(a, ui));
    out.textures_delta.clear();
    let mut texts = Vec::new();
    for clipped in &out.shapes {
        extract(&clipped.shape, &mut texts);
    }
    texts
}

/// #5886: down to the new minimum width the title bar's controls don't overlap: the search box
/// narrows instead of covering the Quick Access Toolbar. Parametrized at the exact 600 pt minimum,
/// half-screen split widths (640 pt at 150% scale, 683 pt), and across 1.0x and 1.5x scale factors.
#[test]
fn title_bar_controls_never_overlap_in_a_narrow_window() {
    for scale in [1.0, 1.5] {
        for width in [crate::window_geometry::MIN_SIZE.x, 640.0, 680.0, 760.0, 1440.0] {
            let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
            a.ui.show_discord = true;
            let nodes = labelled_scaled(&mut a, width, scale, title_bar);
            assert!(nodes.iter().any(|(l, ..)| l == "Search commands and help"), "{scale}x {width}: {nodes:?}");
            for (i, (la, a0, a1)) in nodes.iter().enumerate() {
                assert!(*a1 <= f64::from(width) + 0.5, "{scale}x {width}: {la} runs off the window");
                for (lb, b0, b1) in nodes.iter().skip(i + 1) {
                    assert!(a1 <= b0 || b1 <= a0, "{scale}x {width}: {la} ({a0}–{a1}) overlaps {lb} ({b0}–{b1})");
                }
            }
        }
    }
}

/// #5886 (review): when search is active in the title bar at narrow widths down to the minimum
/// width (and at icon-only search widths), the active text editor stays within the fitted width
/// without expanding or overlapping surrounding controls (Quick Access Toolbar or Discord).
#[test]
fn title_bar_controls_never_overlap_when_search_is_active_in_a_narrow_window() {
    for scale in [1.0f32, 1.5f32] {
        // Test narrow icon-only widths (e.g. 400 pt where TitleBarFit returns control_h) as well
        // as the 600 pt split minimum and wider split widths.
        for width in [400.0, 450.0, crate::window_geometry::MIN_SIZE.x, 640.0, 680.0, 760.0, 1440.0] {
            let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default());
            a.ui.show_discord = true;
            let ctx = ctx();
            if (scale - 1.0).abs() > 0.001 {
                ctx.set_zoom_factor(scale);
            }
            ctx.enable_accesskit();
            a.ctx = Some(ctx.clone());
            let input = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(width, 700.0))), ..Default::default() };
            // Frame 1: draw title bar to record search box presence.
            let mut out1 = ctx.run_ui(input.clone(), |ui| title_bar(&mut a, ui));
            out1.textures_delta.clear();
            assert!(open_title_search(&mut a), "search opens in the title bar box");
            // Frame 2: draw title bar with search active.
            let mut out = ctx.run_ui(input, |ui| title_bar(&mut a, ui));
            out.textures_delta.clear();
            assert!(title_search_open(&a, &ctx), "search is active in frame 2");
            let update = out.platform_output.accesskit_update.take().unwrap();
            let nodes: Vec<(String, f64, f64)> = update
                .nodes
                .iter()
                .filter_map(|(_, node)| Some((node.label()?.to_string(), node.bounds()?)))
                .filter(|(_, b)| b.width() > 0.0)
                .map(|(l, b)| (l, b.x0, b.x1))
                .collect();
            assert!(nodes.iter().any(|(l, ..)| l == "Search commands and help"), "{scale}x {width}: {nodes:?}");
            for (i, (la, a0, a1)) in nodes.iter().enumerate() {
                assert!(*a1 <= f64::from(width) + 0.5, "{scale}x {width}: {la} runs off the window");
                for (lb, b0, b1) in nodes.iter().skip(i + 1) {
                    assert!(a1 <= b0 || b1 <= a0, "{scale}x {width}: active search {la} ({a0}–{a1}) overlaps {lb} ({b0}–{b1})");
                }
            }
        }
    }
}

/// #5886: the status bar's texts stop before its controls on the right; in a narrow window the
/// ones that don't fit are left out or shortened instead of being drawn over Focus, the views and the zoom.
/// Parametrized across exact 600 pt minimum, split widths, and 1.0x and 1.5x scale factors.
#[test]
fn status_bar_texts_stop_before_the_controls_in_a_narrow_window() {
    let controls = ["Focus", "Read Mode", "Print Layout", "Web Layout", "Zoom Out", "Zoom", "Zoom In"];
    for scale in [1.0, 1.5] {
        for width in [crate::window_geometry::MIN_SIZE.x, 640.0, 680.0, 760.0, 1440.0] {
            let mut a =
                WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("Several words here")), crate::Services::default());
            a.run("review.trackChanges", json!({"value": true})).unwrap();
            a.run("select.all", json!({})).unwrap();
            let nodes = labelled_scaled(&mut a, width, scale, status_bar);
            let focus = nodes.iter().find(|(l, ..)| l == "Focus").unwrap_or_else(|| panic!("{scale}x {width}: Focus is drawn")).1;
            assert!(nodes.iter().any(|(l, ..)| l == "Page 1 of 1"), "{scale}x {width}: the page is always shown: {nodes:?}");
            for (label, _, right) in nodes.iter().filter(|(l, ..)| !controls.contains(&l.as_str()) && !l.ends_with('%')) {
                assert!(*right <= focus, "{scale}x {width}: {label} ends at {right}, past the controls at {focus}");
            }
            if width >= 1000.0 && scale <= 1.0 {
                assert!(nodes.iter().any(|(l, ..)| l == "Track Changes: On"), "a wide window shows everything: {nodes:?}");
            }
        }
    }
}

/// #5886: when a language label is too long to fit before the status bar controls in a narrow
/// window, it falls back to "…" while preserving clickability to open the language menu,
/// and its tooltip and accessibility label retain the full language name.
#[test]
fn status_bar_language_falls_back_to_ellipsis_in_a_narrow_window() {
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("Several words here")), crate::Services::default());
    a.run("review.trackChanges", json!({"value": true})).unwrap();
    a.run("select.all", json!({})).unwrap();
    std::sync::Arc::make_mut(&mut a.session.doc.styles).default_chr.lang = Some("en-US".into());

    let narrow = painted_texts(&mut a, crate::window_geometry::MIN_SIZE.x, status_bar);
    assert!(narrow.iter().any(|t| t == "…"), "narrow window paints ellipsis for language: {narrow:?}");
    assert!(!narrow.iter().any(|t| t == "English (United States)"), "narrow window does not paint full language name: {narrow:?}");

    let wide = painted_texts(&mut a, 1440.0, status_bar);
    assert!(wide.iter().any(|t| t == "English (United States)"), "wide window paints full language name: {wide:?}");
    assert!(!wide.iter().any(|t| t == "…"), "wide window does not paint ellipsis: {wide:?}");

    let ctx = ctx();
    ctx.enable_accesskit();
    let mut n = 0;
    let mut drawn = |a: &mut WordApp, events: Vec<egui::Event>| -> Vec<(String, egui::Rect)> {
        n += 1;
        let input = egui::RawInput {
            time: Some(f64::from(n) / 60.0),
            screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(crate::window_geometry::MIN_SIZE.x, 700.0))),
            events,
            ..Default::default()
        };
        let mut out = ctx.run_ui(input, |ui| status_bar(a, ui));
        out.textures_delta.clear();
        let update = out.platform_output.accesskit_update.take().unwrap();
        update
            .nodes
            .into_iter()
            .filter_map(|(_, node)| {
                let b = node.bounds()?;
                Some((node.label()?.to_string(), egui::Rect::from_min_max(pos2(b.x0 as f32, b.y0 as f32), pos2(b.x1 as f32, b.y1 as f32))))
            })
            .collect()
    };

    // Frame 1: Accessibility label retains the full language name.
    let seen = drawn(&mut a, vec![]);
    let lang_node = seen.iter().find(|(l, _)| l == "English (United States)");
    assert!(lang_node.is_some(), "narrow window maintains full language name in accessibility label: {seen:?}");
    let lang_at = lang_node.unwrap().1.center();

    // Hover over the button: tooltip contains the full language name.
    let hovered = drawn(&mut a, vec![egui::Event::PointerMoved(lang_at)]);
    assert!(
        hovered.iter().any(|(l, _)| l.contains("English (United States)")),
        "hovering the ellipsized language button shows tooltip with the full language name: {hovered:?}"
    );

    // Click the button: the menu opens with proofing options.
    let click_down = egui::Event::PointerButton { pos: lang_at, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() };
    let click_up = egui::Event::PointerButton { pos: lang_at, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() };
    drawn(&mut a, vec![click_down]);
    drawn(&mut a, vec![click_up]);
    let clicked = drawn(&mut a, vec![]);
    assert!(
        clicked.iter().any(|(l, _)| l == "Set Proofing Language…"),
        "clicking the ellipsized language button opens the language menu: {clicked:?}"
    );
}

/// #5886: each native window has its own cached status bar controls width in egui's temporary data,
/// so drawing multiple windows with different widths does not overwrite each other or trigger
/// `request_discard` redraw loops.
#[test]
fn status_bar_controls_width_cache_is_per_window_and_avoids_discard_loops() {
    let ctx = ctx();
    let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("Hello")), crate::Services::default());

    let win1 = a.windows.shown();
    a.new_window();
    let win2 = a.windows.shown();
    assert_ne!(win1, win2, "two distinct windows open");

    let id2 = a.window_id("status_controls_width");
    assert!(a.enter_window(win1));
    let id1 = a.window_id("status_controls_width");
    assert_ne!(id1, id2, "per-window cache keys are distinct");

    // Frame 1: Window 1 at 600 pt (narrow), Window 2 at 1200 pt (wide).
    // The first pass populates each window's cache.
    assert!(a.enter_window(win1));
    ctx.run_ui(egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(600.0, 700.0))), ..Default::default() }, |ui| {
        status_bar(&mut a, ui);
    })
    .drop_without_applying_deltas();

    assert!(a.enter_window(win2));
    ctx.run_ui(egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(1200.0, 700.0))), ..Default::default() }, |ui| {
        status_bar(&mut a, ui);
    })
    .drop_without_applying_deltas();

    let cached1: Option<f32> = ctx.data(|d| d.get_temp(id1));
    let cached2: Option<f32> = ctx.data(|d| d.get_temp(id2));
    assert!(cached1.is_some(), "window 1 controls width is cached");
    assert!(cached2.is_some(), "window 2 controls width is cached");

    // Frame 2 and subsequent frames: rendering both windows alternating does NOT trigger discard loops.
    // Each run_ui must complete in exactly 1 pass (0 discards requested).
    for frame in 2..=4 {
        assert!(a.enter_window(win1));
        let mut passes1 = 0;
        ctx.run_ui(
            egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(600.0, 700.0))), ..Default::default() },
            |ui| {
                status_bar(&mut a, ui);
                passes1 += 1;
            },
        )
        .drop_without_applying_deltas();
        assert_eq!(passes1, 1, "frame {frame} window 1 settled without discard loop");

        assert!(a.enter_window(win2));
        let mut passes2 = 0;
        ctx.run_ui(
            egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(1200.0, 700.0))), ..Default::default() },
            |ui| {
                status_bar(&mut a, ui);
                passes2 += 1;
            },
        )
        .drop_without_applying_deltas();
        assert_eq!(passes2, 1, "frame {frame} window 2 settled without discard loop");
    }

    // Both window caches remain intact and separate.
    assert_eq!(ctx.data(|d| d.get_temp(id1)), cached1);
    assert_eq!(ctx.data(|d| d.get_temp(id2)), cached2);
}

/// #5886: when rendering the first pass with an empty cache and max_passes = 1 (where egui declines
/// request_discard), status bar texts never overlap the right-hand controls across narrow window widths.
#[test]
fn status_bar_texts_never_overlap_controls_on_cache_miss_with_single_pass() {
    let controls = ["Focus", "Read Mode", "Print Layout", "Web Layout", "Zoom Out", "Zoom", "Zoom In"];
    for scale in [1.0f32, 1.5f32] {
        for width in [crate::window_geometry::MIN_SIZE.x, 640.0, 680.0, 760.0, 1440.0] {
            let mut a =
                WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("Several words here")), crate::Services::default());
            a.run("review.trackChanges", json!({"value": true})).unwrap();
            a.run("select.all", json!({})).unwrap();

            let ctx = ctx();
            if (scale - 1.0f32).abs() > 0.001 {
                ctx.set_zoom_factor(scale);
            }
            ctx.options_mut(|o| o.max_passes = std::num::NonZeroUsize::new(1).unwrap());
            ctx.enable_accesskit();

            let input = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(width, 700.0))), ..Default::default() };
            let mut out = ctx.run_ui(input, |ui| status_bar(&mut a, ui));
            out.textures_delta.clear();
            let update = out.platform_output.accesskit_update.take().unwrap();
            let nodes: Vec<(String, f64, f64)> = update
                .nodes
                .iter()
                .filter_map(|(_, node)| Some((node.label()?.to_string(), node.bounds()?)))
                .filter(|(_, b)| b.width() > 0.0)
                .map(|(l, b)| (l, b.x0, b.x1))
                .collect();

            let focus = nodes.iter().find(|(l, ..)| l == "Focus").unwrap_or_else(|| panic!("{scale}x {width}: Focus is drawn")).1;
            assert!(nodes.iter().any(|(l, ..)| l == "Page 1 of 1"), "{scale}x {width}: the page is always shown: {nodes:?}");
            for (label, _, right) in nodes.iter().filter(|(l, ..)| !controls.contains(&l.as_str()) && !l.ends_with('%')) {
                assert!(*right <= focus, "{scale}x {width} (pass 1): {label} ends at {right}, past the controls at {focus}");
            }
        }
    }
}