GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! File → Package…: the dialog, saving first, the folder or the downloaded zip, Show Package.

use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;

use serde_json::json;
use vectorcraft_engine::Session;
use vectorcraft_engine::file_access::{Access, AutomationRoots};

use super::*;
use crate::background;
use crate::tests_background::{Files, Gate, written};

/// A fresh folder for one test (removed when dropped).
struct Folder(std::path::PathBuf);

impl Folder {
    fn new(name: &str) -> Self {
        let dir = std::env::temp_dir().join(format!("vectorcraft-ui-package-{name}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        Self(dir)
    }
    fn path(&self, name: &str) -> String {
        self.0.join(name).to_string_lossy().into_owned()
    }
}

impl Drop for Folder {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

fn frame(app: &mut VectorcraftApp) -> String {
    crate::tests_labels::painted_text(app, |app, ui| show(app, ui.ctx()))
}

/// An app writing through `std::fs`, with a saved document holding a placed linked image.
fn saved(dir: &Folder) -> VectorcraftApp {
    let mut app = VectorcraftApp::new(Session::new(), Default::default());
    app.services.write = Some(Box::new(|p: &str, b: &[u8]| std::fs::write(p, b).map_err(|e| e.to_string())));
    app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
    let mut png = vec![];
    {
        let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(300, 300, image::Rgba([1, 2, 3, 255]))).into();
        let (__w, __h) = (__dyn.width(), __dyn.height());
        let (__layout, __sample, __raw) = match __dyn {
            image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
            image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
            image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
            image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
            image::DynamicImage::ImageLuma16(b) => (
                craft_image_codecs::ChannelLayout::Gray,
                craft_image_codecs::SampleType::U16,
                b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
            ),
            image::DynamicImage::ImageLumaA16(b) => (
                craft_image_codecs::ChannelLayout::GrayA,
                craft_image_codecs::SampleType::U16,
                b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
            ),
            image::DynamicImage::ImageRgb16(b) => (
                craft_image_codecs::ChannelLayout::Rgb,
                craft_image_codecs::SampleType::U16,
                b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
            ),
            image::DynamicImage::ImageRgba16(b) => (
                craft_image_codecs::ChannelLayout::Rgba,
                craft_image_codecs::SampleType::U16,
                b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
            ),
            other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
        };
        let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
        craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
            .map(|__b| png.extend_from_slice(&__b))
    }
    .unwrap();
    std::fs::write(dir.path("pic.png"), &png).unwrap();
    app.run("file.place", json!({"path": dir.path("pic.png")})).unwrap();
    app.run("document.save", json!({"path": dir.path("card.vectorcraft")})).unwrap();
    app
}

#[test]
fn an_unsaved_document_is_offered_save_as_first() {
    let mut app = VectorcraftApp::new(Session::new(), Default::default());
    assert!(!crate::menus::enabled(&app, "ui.packageDialog"), "no document");
    app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
    app.run("ui.packageDialog", json!({})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.kind.as_str(), d.str("__command")), (confirm::KIND, "file.saveAs".to_string()));
}

#[test]
fn package_writes_the_folder_then_offers_to_show_it() {
    let dir = Folder::new("folder");
    let mut app = saved(&dir);
    app.run("ui.packageDialog", json!({})).unwrap();
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.kind.as_str(), d.str("name"), d.bool("copyFonts")), ("package", "card Folder".to_string(), true));
    assert_eq!(std::path::Path::new(&d.str("folder")), dir.0.as_path(), "next to the document");
    let text = frame(&mut app);
    assert!(text.contains("Collect Links in a Separate Folder") && text.contains("Folder Name:"), "{text}");
    let out = dir.path("out");
    std::fs::create_dir_all(&out).unwrap();
    app.ui.dialog.as_mut().unwrap().fields.insert("folder".into(), json!(out));
    // Unsaved changes are saved first.
    app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
    let r = confirm(&mut app).unwrap();
    assert!(!app.session.active().unwrap().is_dirty());
    let root = std::path::Path::new(&out).join("card Folder");
    assert!(root.join("card.vectorcraft").is_file() && root.join("Links/pic.png").is_file(), "{r}");
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.kind.as_str(), d.str("__command")), (confirm::KIND, "file.showPackage".to_string()));
    let opened: Rc<RefCell<Vec<String>>> = Rc::default();
    let o = opened.clone();
    app.services.open_file = Some(Box::new(move |p: &str| {
        o.borrow_mut().push(p.to_string());
        Ok(())
    }));
    confirm(&mut app).unwrap();
    assert_eq!(opened.borrow().first().map(std::path::PathBuf::from), Some(root));
}

/// [`saved`] with a thread-safe writer, the background thread's, that records the files it is
/// given once `gate` opens.
fn saved_with_worker(dir: &Folder, gate: &Gate) -> (VectorcraftApp, Files) {
    let mut app = saved(dir);
    let (files, g) = (Files::default(), gate.clone());
    let recorded = files.clone();
    app.services.write_shared = Some(Arc::new(move |p: &str, b: &[u8]| {
        g.wait();
        recorded.lock().unwrap().push((p.to_string(), b.to_vec()));
        Ok(())
    }));
    (app, files)
}

/// Open the Package dialog for a package in `out`.
fn package_dialog(app: &mut VectorcraftApp, out: &str) {
    app.run("ui.packageDialog", json!({})).unwrap();
    app.ui.dialog.as_mut().unwrap().fields.insert("folder".into(), json!(out));
}

#[test]
fn package_is_written_in_the_background_as_it_is_at_once() {
    let dir = Folder::new("background");
    let gate = Gate::default();
    let (mut app, files) = saved_with_worker(&dir, &gate);
    let out = dir.path("out");
    let root = std::path::Path::new(&out).join("card Folder");
    // With Background Export off, the package is written at once through the UI thread's writer.
    app.session.execute("prefs.set", &json!({"values": {"backgroundExport": false}})).unwrap();
    package_dialog(&mut app, &out);
    let at_once = confirm(&mut app).unwrap();
    let want: Vec<(String, Vec<u8>)> = at_once["files"]
        .as_array()
        .unwrap()
        .iter()
        .map(|f| root.join(f.as_str().unwrap()))
        .map(|p| (p.to_string_lossy().into_owned(), std::fs::read(&p).unwrap()))
        .collect();
    assert_eq!(want.len(), 3, "the document, its linked file and the report: {at_once}");
    let (status, detail) = (app.ui.status.clone(), app.ui.dialog.as_ref().unwrap().str("detail"));
    std::fs::remove_dir_all(&root).unwrap();
    app.ui.dialog = None;
    // With Background Export on, the dialog closes at once, and the same files are written later.
    app.session.execute("prefs.set", &json!({"values": {"backgroundExport": true}})).unwrap();
    package_dialog(&mut app, &out);
    let r = confirm(&mut app).unwrap();
    assert_eq!(r["background"], true, "{r}");
    assert!(app.ui.dialog.is_none(), "closed");
    assert_eq!(app.ui.status, "Packaging card Folder…");
    assert!(written(&files).is_empty(), "nothing written yet");
    gate.open();
    background::wait_all(&mut app);
    assert_eq!(*files.lock().unwrap(), want);
    assert_eq!(app.ui.status, status);
    let d = app.ui.dialog.as_ref().unwrap();
    assert_eq!((d.str("__command"), d.str("detail")), ("file.showPackage".to_string(), detail));
}

#[test]
fn edits_after_ok_do_not_reach_the_package() {
    let dir = Folder::new("snapshot");
    let gate = Gate::default();
    let (mut app, files) = saved_with_worker(&dir, &gate);
    let nodes = app.session.active().unwrap().doc.node_count();
    package_dialog(&mut app, &dir.path("out"));
    assert_eq!(confirm(&mut app).unwrap()["background"], true);
    app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
    app.run("select.all", json!({})).unwrap();
    app.run("edit.clear", json!({})).unwrap();
    // A dialog opened meanwhile stays open; the status bar reports the package.
    app.run("ui.packageDialog", json!({})).unwrap();
    gate.open();
    background::wait_all(&mut app);
    assert_eq!(app.ui.dialog.as_ref().map(|d| d.kind.as_str()), Some("package"));
    assert!(app.ui.status.starts_with("Packaged 3 files in "), "{}", app.ui.status);
    let files = files.lock().unwrap();
    let (_, bytes) = files.iter().find(|(p, _)| p.ends_with("card.vectorcraft")).unwrap();
    assert_eq!(vectorcraft_format::load(bytes).unwrap().node_count(), nodes, "the document as it was at OK");
    assert!(files.iter().any(|(p, _)| p.ends_with("pic.png")), "its linked file");
}

#[test]
fn a_dirty_documents_save_is_written_before_its_package() {
    let dir = Folder::new("dirty");
    let gate = Gate::default();
    let (mut app, files) = saved_with_worker(&dir, &gate);
    let (doc, out) = (dir.path("card.vectorcraft"), dir.path("out"));
    // With Background Export on, the save and the package both run in the background, and the
    // package starts once the save is written.
    app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
    package_dialog(&mut app, &out);
    // A thread opens the gate after 10 s at the latest, so a confirm that waits for the save ends.
    let opener = gate.clone();
    std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_secs(10));
        opener.open();
    });
    assert_eq!(confirm(&mut app).unwrap()["background"], true);
    assert_eq!(app.background.jobs.len(), 1, "the save; the package waits for it");
    gate.open();
    background::wait_all(&mut app);
    let paths = written(&files);
    assert_eq!(paths.first(), Some(&doc), "{paths:?}");
    assert_eq!(paths.len(), 4, "{paths:?}");
    assert!(!app.session.active().unwrap().is_dirty());
    // With Background Export off, the package is made at once and waits for the background save.
    let gate = Gate::default();
    let (mut app, files) = saved_with_worker(&dir, &gate);
    app.session.execute("prefs.set", &json!({"values": {"backgroundExport": false}})).unwrap();
    app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
    package_dialog(&mut app, &out);
    let opener = gate.clone();
    let release = std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_millis(50));
        opener.open();
    });
    confirm(&mut app).unwrap();
    release.join().unwrap();
    assert_eq!(written(&files), [doc]);
    assert!(app.background.jobs.is_empty() && !app.session.active().unwrap().is_dirty());
    assert!(app.ui.status.starts_with("Packaged 3 files in "), "the package's status came last: {}", app.ui.status);
}

/// A save before packaging that fails writes no package, with Background Export on or off.
#[test]
fn a_failed_save_before_packaging_writes_no_package() {
    let dir = Folder::new("failed-save");
    let (doc, out) = (dir.path("card.vectorcraft"), dir.path("out"));
    let root = std::path::Path::new(&out).join("card Folder");
    for background_export in [true, false] {
        let mut app = saved(&dir);
        let (files, refused) = (Files::default(), doc.clone());
        let recorded = files.clone();
        app.services.write_shared = Some(Arc::new(move |p: &str, b: &[u8]| {
            if p == refused {
                return Err("Access is denied. (os error 5)".into());
            }
            recorded.lock().unwrap().push((p.to_string(), b.to_vec()));
            Ok(())
        }));
        app.session.execute("prefs.set", &json!({"values": {"backgroundExport": background_export}})).unwrap();
        app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
        package_dialog(&mut app, &out);
        let r = confirm(&mut app);
        background::wait_all(&mut app);
        assert!(written(&files).is_empty(), "Background Export {background_export}: {:?}", written(&files));
        assert!(!root.exists(), "Background Export {background_export}: no package folder");
        assert!(app.session.active().unwrap().is_dirty(), "the save failed");
        match r {
            Ok(r) => assert!(background_export && r["background"] == true, "{r}"),
            Err(e) => assert!(!background_export && e.contains("Access is denied"), "{e}"),
        }
    }
}

/// A save before packaging that Cancel Save stops writes no package either: the document keeps
/// its unsaved changes, so the package would not hold it as saved.
#[test]
fn a_canceled_save_before_packaging_writes_no_package() {
    let dir = Folder::new("canceled-save");
    let gate = Gate::default();
    let (mut app, files) = saved_with_worker(&dir, &gate);
    let out = dir.path("out");
    // An export holds the worker, so the save waits behind it and Cancel comes before it starts.
    crate::io::export(&mut app, Some("svg"), Some(dir.path("first.svg")), &json!({})).unwrap();
    app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
    package_dialog(&mut app, &out);
    assert_eq!(confirm(&mut app).unwrap()["background"], true);
    assert_eq!(app.run("file.cancelSave", json!({})).unwrap()["cancelled"], 1);
    gate.open();
    background::wait_all(&mut app);
    assert_eq!(written(&files), [dir.path("first.svg")], "only the export");
    assert!(!std::path::Path::new(&out).join("card Folder").exists(), "no package folder");
    assert!(app.session.active().unwrap().is_dirty(), "the save was canceled");
}

/// A package that an automation request confined to its roots asks for, started once the
/// document's background save is written, is still confined to those roots, though the save's
/// result arrives outside the request.
#[test]
fn a_package_started_after_its_save_keeps_the_automation_roots() {
    let (dir, elsewhere) = (Folder::new("scope"), Folder::new("scope-elsewhere"));
    let (doc, out) = (dir.path("card.vectorcraft"), elsewhere.path("out"));
    let root = std::path::Path::new(&out).join("card Folder");
    let mut app = saved(&dir);
    let files = Files::default();
    let recorded = files.clone();
    app.services.write_shared = Some(Arc::new(move |p: &str, b: &[u8]| {
        vectorcraft_engine::file_access::check_write(p)?;
        recorded.lock().unwrap().push((p.to_string(), b.to_vec()));
        Ok(())
    }));
    let roots = AutomationRoots::new(&[dir.0.as_path()], &[dir.0.as_path()]).unwrap();
    app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
    package_dialog(&mut app, &out);
    let r = vectorcraft_engine::file_access::confine(roots.as_ref(), || confirm(&mut app)).unwrap();
    assert_eq!(r["background"], true, "{r}");
    // The save's result is applied outside the request, as the next frame does.
    background::wait_all(&mut app);
    assert_eq!(written(&files), [doc], "only the save, inside the roots");
    assert!(!root.exists(), "no package folder outside the roots");
    assert!(!app.session.active().unwrap().is_dirty(), "the save was written");
    assert!(app.ui.status.contains("failed"), "the package was refused: {}", app.ui.status);
}

#[test]
fn a_package_made_at_once_waits_for_one_in_the_background() {
    let dir = Folder::new("twice");
    let gate = Gate::default();
    let (mut app, files) = saved_with_worker(&dir, &gate);
    let out = dir.path("out");
    package_dialog(&mut app, &out);
    assert_eq!(confirm(&mut app).unwrap()["background"], true);
    // With Background Export turned off, the same package made again is written after the one in
    // the background.
    app.session.execute("prefs.set", &json!({"values": {"backgroundExport": false}})).unwrap();
    package_dialog(&mut app, &out);
    let opener = gate.clone();
    let release = std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_millis(500));
        opener.open();
    });
    confirm(&mut app).unwrap();
    assert!(app.background.jobs.is_empty(), "the package in the background was written first");
    release.join().unwrap();
    assert_eq!(written(&files).len(), 3);
    assert!(std::path::Path::new(&out).join("card Folder/card.vectorcraft").is_file());
}

#[test]
fn the_web_downloads_a_zip() {
    let dir = Folder::new("web");
    let mut app = saved(&dir);
    let got: Rc<RefCell<Vec<(String, usize)>>> = Rc::default();
    let g = got.clone();
    app.services.download = Some(Box::new(move |name: &str, b: &[u8]| {
        g.borrow_mut().push((name.to_string(), b.len()));
        Ok(())
    }));
    app.run("ui.packageDialog", json!({})).unwrap();
    assert!(!frame(&mut app).contains("Location:"), "no folder to choose");
    confirm(&mut app).unwrap();
    assert!(app.ui.dialog.is_none());
    assert!(app.ui.status.contains("Packaged") && app.ui.status.contains("files"), "{}", app.ui.status);
    let got = got.borrow();
    assert_eq!(got.len(), 1);
    assert_eq!(got[0].0, "card Folder.zip");
    assert!(got[0].1 > 100);
}

#[test]
fn fonts_left_out_of_the_package_are_named() {
    let dir = Folder::new("fonts");
    let mut app = saved(&dir);
    app.run("text.create", json!({"x": 10, "y": 40, "text": "Kept"})).unwrap();
    app.run("text.create", json!({"x": 10, "y": 80, "text": "Gone", "font": "No Such Font Family"})).unwrap();
    app.run("ui.packageDialog", json!({})).unwrap();
    let out = dir.path("out");
    std::fs::create_dir_all(&out).unwrap();
    app.ui.dialog.as_mut().unwrap().fields.insert("folder".into(), json!(out));
    let r = confirm(&mut app).unwrap();
    assert_eq!(r["skippedFonts"], json!([{"font": "No Such Font Family Regular", "reason": "not available on this computer"}]));
    let gone = "fonts not copied: No Such Font Family Regular";
    assert!(app.ui.status.contains(gone), "{}", app.ui.status);
    let detail = app.ui.dialog.as_ref().unwrap().str("detail");
    assert!(detail.contains(gone) && !detail.contains("Source Sans 3"), "{detail}");
    // The web's download says so too.
    app.ui.dialog = None;
    app.services.download = Some(Box::new(|_: &str, _: &[u8]| Ok(())));
    app.run("ui.packageDialog", json!({})).unwrap();
    confirm(&mut app).unwrap();
    assert!(app.ui.status.contains(gone), "{}", app.ui.status);
}

/// A package made while the interface is in another language keeps its status line in English,
/// note included, so the status bar can show it in the language of the moment. The dialog shown
/// at once is in the interface language.
#[test]
fn a_package_status_is_stored_in_english() {
    let dir = Folder::new("english");
    let mut app = saved(&dir);
    app.run("text.create", json!({"x": 10, "y": 80, "text": "Gone", "font": "No Such Font Family"})).unwrap();
    app.run("ui.packageDialog", json!({})).unwrap();
    let out = dir.path("out");
    std::fs::create_dir_all(&out).unwrap();
    app.ui.dialog.as_mut().unwrap().fields.insert("folder".into(), json!(out));
    crate::i18n::set_current(crate::i18n::Lang::from_code("cs").unwrap());
    let r = confirm(&mut app);
    crate::i18n::set_current(crate::i18n::Lang::EN);
    r.unwrap();
    let status = &app.ui.status;
    assert!(status.starts_with("Packaged "), "{status}");
    assert!(status.contains(&format!(" (fonts not copied: No Such Font Family Regular) in {out}")), "{status}");
    let detail = app.ui.dialog.as_ref().unwrap().str("detail");
    assert!(detail.contains("(nezkopírovaná písma: No Such Font Family Regular)"), "{detail}");
    assert!(detail.ends_with("Zobrazit složku balíčku?"), "{detail}");
}

/// The dialog's note on linked files left out of a package uses the form the language's plural
/// rule picks for the count. The status bar's note stays English.
#[test]
fn the_missing_links_note_uses_each_count_form() {
    let note = |code: &str, missing: usize| {
        crate::i18n::set_current(crate::i18n::Lang::from_code(code).unwrap());
        let notes = (package::package_dialog_note(missing, ""), package::package_status_note(missing, ""));
        crate::i18n::set_current(crate::i18n::Lang::EN);
        notes
    };
    assert_eq!(note("en", 1).0, "(1 linked file not found)");
    assert_eq!(note("en", 2).0, "(2 linked files not found)");
    assert_eq!(note("cs", 1).0, "(Nenalezen 1 propojený soubor)");
    assert_eq!(note("cs", 2).0, "(Nenalezeny 2 propojené soubory)");
    assert_eq!(note("cs", 5).0, "(Nenalezeno 5 propojených souborů)");
    assert_eq!(note("cs", 1).1, "(1 linked file not found)");
    assert_eq!(note("cs", 5).1, "(5 linked files not found)");
}

/// A long list of fonts left out is cut short in the desktop's status line, so the status bar can
/// still translate the line. The dialog names every font.
#[test]
fn a_long_list_of_fonts_left_out_is_cut_short_in_the_status() {
    use crate::i18n::{Lang, message};
    let fonts = (0..200).map(|i| format!("Family {i} Regular")).collect::<Vec<_>>().join(", ");
    assert_eq!(package::status_fonts("Mono, Sans"), "Mono, Sans");
    let note = package::package_status_note(0, &package::status_fonts(&fonts));
    assert!(note.starts_with("(fonts not copied: Family 0 Regular, ") && note.ends_with(", …)"), "{note}");
    let shown = message(Lang::from_code("de").unwrap(), &package::package_status(3, Some("/out/card Folder"), &note));
    assert!(shown.starts_with("3 Dateien in /out/card Folder verpackt (nicht kopierte Schriften: Family 0 Regular, "), "{shown}");
    assert!(package::package_dialog_note(0, &fonts).contains("Family 199 Regular"));
}

/// A package written in the background while another dialog is open shows no dialog, so its
/// status line names every font left out, however long the list.
#[test]
fn a_background_package_behind_another_dialog_names_every_font_in_the_status() {
    let dir = Folder::new("fonts-behind");
    let gate = Gate::default();
    let (mut app, _files) = saved_with_worker(&dir, &gate);
    for i in 0..30 {
        app.run("text.create", json!({"x": 10, "y": 40 + i, "text": "Gone", "font": format!("No Such Font Family {i}")})).unwrap();
    }
    package_dialog(&mut app, &dir.path("out"));
    assert_eq!(confirm(&mut app).unwrap()["background"], true);
    app.run("ui.packageDialog", json!({})).unwrap();
    gate.open();
    background::wait_all(&mut app);
    assert_eq!(app.ui.dialog.as_ref().map(|d| d.kind.as_str()), Some("package"), "the other dialog stays");
    let status = &app.ui.status;
    assert!(status.contains("No Such Font Family 0 Regular") && status.contains("No Such Font Family 29 Regular"), "{status}");
    assert!(!status.contains('…'), "{status}");
}

/// A package status written in English, for one file or several, with or without a folder and a
/// note, is translated by every catalog when the status bar shows it after a language switch, in
/// the form the language's plural rule picks for the count, and a folder whose name has a space or
/// parentheses keeps its whole name, next to one note or two.
#[test]
fn an_english_package_status_is_translated_after_a_language_switch() {
    use crate::i18n::{Lang, message};
    let folders = [None, Some("/out/card Folder"), Some("/out/card (draft) v2")];
    let notes = ["", "(fonts not copied: Mono)", "(1 linked file not found) (fonts not copied: Mono)"];
    let mut statuses = vec![];
    for n in [1, 3] {
        for folder in folders {
            for note in notes {
                statuses.push((package::package_status(n, folder, note), folder, note));
            }
        }
    }
    let english: Vec<&str> = statuses.iter().map(|(s, ..)| s.as_str()).collect();
    assert!(english.contains(&"Packaged 1 file in /out/card Folder"), "{english:?}");
    assert!(english.contains(&"Packaged 1 file (fonts not copied: Mono) in /out/card (draft) v2"), "{english:?}");
    assert!(english.contains(&"Packaged 3 files"), "{english:?}");
    for lang in Lang::all().filter(|l| l.code() != "en") {
        for (s, folder, note) in &statuses {
            let shown = message(lang, s);
            assert!(!shown.starts_with("Packaged"), "{}: {shown}", lang.code());
            // The folder stays whole in the translated line, and the note's font is still named.
            assert!(folder.is_none_or(|f| shown.contains(f)), "{}: {shown}", lang.code());
            assert_eq!(note.contains("Mono"), shown.contains("Mono"), "{}: {shown}", lang.code());
            // The notes are translated too.
            assert!(!shown.contains("linked file") && !shown.contains("not copied"), "{}: {shown}", lang.code());
        }
    }
    let de = Lang::from_code("de").unwrap();
    assert_eq!(
        message(de, &package::package_status(1, Some("/out/card (draft)"), "(fonts not copied: Mono)")),
        "1 Datei in /out/card (draft) verpackt (nicht kopierte Schriften: Mono)"
    );
    assert_eq!(
        message(de, &package::package_status(3, Some("/out/card (draft)"), "(1 linked file not found) (fonts not copied: Mono)")),
        "3 Dateien in /out/card (draft) verpackt (1 verknüpfte Datei nicht gefunden) (nicht kopierte Schriften: Mono)"
    );
    assert_eq!(
        message(de, &package::package_status(3, None, "(1 linked file not found) (fonts not copied: Mono)")),
        "3 Dateien verpackt (1 verknüpfte Datei nicht gefunden) (nicht kopierte Schriften: Mono)"
    );
    assert_eq!(
        message(de, &package::package_status(3, None, "(2 linked files not found)")),
        "3 Dateien verpackt (2 verknüpfte Dateien nicht gefunden)"
    );
    let cs = Lang::from_code("cs").unwrap();
    assert_eq!(message(cs, "Packaged 1 file in /out"), "Zabalen 1 soubor do /out");
    assert_eq!(message(cs, "Packaged 3 files in /out"), "Zabaleny 3 soubory do /out");
    assert_eq!(message(cs, "Packaged 5 files in /out"), "Zabaleno 5 souborů do /out");
    let de = Lang::from_code("de").unwrap();
    assert_eq!(message(de, "Packaged 1 file"), "1 Datei verpackt");
    assert_eq!(message(de, "Packaged 3 files in /out"), "3 Dateien in /out verpackt");
}

#[test]
fn package_status_without_notes_has_no_extra_spaces() {
    let dir = Folder::new("spaces");
    let mut app = saved(&dir);
    app.run("ui.packageDialog", json!({})).unwrap();
    let out = dir.path("out");
    std::fs::create_dir_all(&out).unwrap();
    app.ui.dialog.as_mut().unwrap().fields.insert("folder".into(), json!(out));
    confirm(&mut app).unwrap();
    assert!(!app.ui.status.ends_with(' '), "{}", app.ui.status);
    let detail = app.ui.dialog.as_ref().unwrap().str("detail");
    assert!(!detail.contains(" ."), "{detail}");
}

/// A file the writer refuses is named once in the status bar: a refusal from the automation-root
/// check already names the path, and an error without it gets the path in front.
#[test]
fn a_refused_file_is_named_once() {
    let dir = Folder::new("refused");
    let mut app = saved(&dir);
    app.session.execute("prefs.set", &json!({"values": {"backgroundExport": false}})).unwrap();
    let (out, elsewhere, other) = (dir.path("out"), dir.path("elsewhere"), dir.path("other"));
    std::fs::create_dir_all(&elsewhere).unwrap();
    std::fs::create_dir_all(&other).unwrap();
    let doc = std::path::Path::new(&out).join("card Folder").join("card.vectorcraft").to_string_lossy().into_owned();
    let (elsewhere, other) = (std::path::Path::new(&elsewhere), std::path::Path::new(&other));
    for write_roots in [vec![elsewhere], vec![elsewhere, other]] {
        let roots = AutomationRoots::new(&[], &write_roots).unwrap().unwrap();
        let refused = roots.check(&doc, Access::Write).unwrap_err();
        assert!(refused.ends_with(&doc), "{refused}");
        app.services.write = Some(Box::new(move |p: &str, _: &[u8]| roots.check(p, Access::Write)));
        package_dialog(&mut app, &out);
        assert_eq!(confirm(&mut app), Err(refused.clone()));
        assert_eq!(app.ui.status, refused);
    }
    app.services.write = Some(Box::new(|_: &str, _: &[u8]| Err("Access is denied. (os error 5)".into())));
    package_dialog(&mut app, &out);
    assert!(confirm(&mut app).is_err());
    assert_eq!(app.ui.status, format!("{doc}: Access is denied. (os error 5)"));
}

#[test]
fn package_without_a_location_says_so() {
    let dir = Folder::new("empty");
    let mut app = saved(&dir);
    app.run("ui.packageDialog", json!({})).unwrap();
    app.ui.dialog.as_mut().unwrap().fields.insert("folder".into(), json!(" "));
    assert!(confirm(&mut app).is_err());
    assert!(app.ui.dialog.is_some(), "stays open");
}