GitCorner

Code

ossartcraftlightcraft🌐
5a24289898fd
//! Issue #103: quitting with changes that couldn't be saved asks first (after one more try), and
//! settings-file problems found when the library opened are shown, not swallowed.

use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use lightcraft_catalog::{MemStore, Store};
use lightcraft_engine::library::LibraryStores;
use serde_json::json;

use crate::headless::Headless;
use crate::{LightcraftApp, Services};

const T: Duration = Duration::from_secs(20);

/// Every write fails while `fail` is set (an unplugged drive).
#[derive(Clone, Default)]
struct Unplugged {
    files: MemStore,
    fail: Arc<AtomicBool>,
}

impl Unplugged {
    fn check(&self) -> std::io::Result<()> {
        if self.fail.load(Ordering::SeqCst) { Err(std::io::Error::other("drive unplugged")) } else { Ok(()) }
    }
}

impl Store for Unplugged {
    fn read(&mut self, name: &str) -> std::io::Result<Option<Vec<u8>>> {
        self.files.read(name)
    }
    fn write_atomic(&mut self, name: &str, data: &[u8]) -> std::io::Result<()> {
        self.check()?;
        self.files.write_atomic(name, data)
    }
    fn append(&mut self, name: &str, data: &[u8]) -> std::io::Result<()> {
        self.check()?;
        self.files.append(name, data)
    }
    fn truncate(&mut self, name: &str, len: u64) -> std::io::Result<()> {
        self.files.truncate(name, len)
    }
    fn describe(&self) -> String {
        "unplugged".into()
    }
}

fn has(h: &Headless, id: &str) -> bool {
    h.app.widgets.iter().any(|(w, _)| w == id)
}

/// Click a widget and let the synthetic events (move, press, release) play out.
fn click(h: &mut Headless, id: &str) {
    // a window that just appeared is placed on its second frame
    h.step();
    h.step();
    assert_eq!(h.request("ui.clickWidget", json!({"id": id}), T)["ok"], true, "{id}");
    for _ in 0..4 {
        h.step();
    }
}

#[test]
fn quitting_with_unsaved_changes_asks_first() {
    let store = Unplugged::default();
    let mut s = lightcraft_engine::Session::new();
    let stores = LibraryStores { dir: "unplugged".into(), catalog: Box::new(store.clone()), files: Box::new(MemStore::new()), on_disk: false };
    s.open_library_in(stores, false).unwrap();
    let app = LightcraftApp::new(s, Services { png: None, ..Default::default() });
    let mut h = Headless::new(app, [1200.0, 800.0], 1.0);
    h.step();
    // nothing unsaved: closing is fine
    assert!(crate::panels::notices::may_close(&mut h.app));

    store.fail.store(true, Ordering::SeqCst);
    let r = h.request("engine.execute", json!({"command": "album.create", "params": {"name": "Kept"}}), T);
    assert_eq!(r["ok"], false, "{r}");
    assert!(!crate::panels::notices::may_close(&mut h.app), "retried, still failing: ask");
    h.step();
    let prompt = h.app.quit_prompt.clone().unwrap().text().to_string();
    assert!(prompt.contains("1 change couldn't be written to disk") && prompt.contains("drive unplugged"), "{prompt}");
    for b in ["button:quitRetry", "button:quitAnyway", "button:quitCancel"] {
        assert!(has(&h, b), "{b}");
    }
    assert_eq!(h.request("ui.inspect", json!({}), T)["result"]["quitPrompt"], json!(prompt));
    // Cancel: back to work, nothing quit
    click(&mut h, "button:quitCancel");
    assert!(h.app.quit_prompt.is_none() && !h.app.quit_confirmed);

    // the drive is back: Try Saving Again saves and quits
    assert!(!crate::panels::notices::may_close(&mut h.app));
    h.step();
    store.fail.store(false, Ordering::SeqCst);
    click(&mut h, "button:quitRetry");
    assert!(h.app.session.unsaved().is_none(), "saved");
    assert!(h.app.quit_prompt.is_none() && !h.app.quit_confirmed);

    // Quit Anyway is a deliberate choice
    store.fail.store(true, Ordering::SeqCst);
    let _ = h.request("engine.execute", json!({"command": "album.create", "params": {"name": "Lost"}}), T);
    assert!(!crate::panels::notices::may_close(&mut h.app));
    h.step();
    click(&mut h, "button:quitAnyway");
    assert!(h.app.quit_confirmed && crate::panels::notices::may_close(&mut h.app));
}

/// "Try Saving Again" can recreate the identical unsaved prompt. It is still a new prompt: its
/// first frame is shielded and focus goes back to the safe Cancel, never the Retry button that was
/// just used.
#[test]
fn retrying_an_unsaved_prompt_resets_focus_to_safe_cancel() {
    let store = Unplugged::default();
    let mut s = lightcraft_engine::Session::new();
    let stores = LibraryStores { dir: "unplugged".into(), catalog: Box::new(store.clone()), files: Box::new(MemStore::new()), on_disk: false };
    s.open_library_in(stores, false).unwrap();
    let app = LightcraftApp::new(s, Services { png: None, ..Default::default() });
    let mut h = Headless::new(app, [1200.0, 800.0], 1.0);
    h.step();
    store.fail.store(true, Ordering::SeqCst);
    let r = h.request("engine.execute", json!({"command": "album.create", "params": {"name": "Kept"}}), T);
    assert_eq!(r["ok"], false, "{r}");
    assert!(!crate::panels::notices::may_close(&mut h.app), "retried, still failing: ask");
    h.step();
    h.step();
    let focused = |h: &Headless| h.view.ctx.memory(|m| m.focused());
    let cancel = h.app.widget_hits.iter().rev().find(|(w, _)| w == "button:quitCancel").map(|(_, hit)| hit.id).expect("Cancel has a real response");
    let retry = h.app.widget_hits.iter().rev().find(|(w, _)| w == "button:quitRetry").map(|(_, hit)| hit.id).expect("Retry has a real response");
    assert_eq!(focused(&h), Some(cancel), "the safe Cancel starts focused");
    assert_eq!(h.request("ui.key", json!({"key": "Tab"}), T)["ok"], true);
    assert_eq!(focused(&h), Some(retry), "Tab moved focus to Retry");
    // Activate Retry; the drive is still unplugged, so the same prompt is recreated.
    assert_eq!(h.request("ui.key", json!({"key": "Space"}), T)["ok"], true);
    h.step();
    h.step();
    assert!(matches!(h.app.quit_prompt, Some(crate::QuitPrompt::Unsaved(_))), "the retried save still asks");
    let cancel_after =
        h.app.widget_hits.iter().rev().find(|(w, _)| w == "button:quitCancel").map(|(_, hit)| hit.id).expect("the new Cancel has a real response");
    let retry_after =
        h.app.widget_hits.iter().rev().find(|(w, _)| w == "button:quitRetry").map(|(_, hit)| hit.id).expect("the new Retry has a real response");
    assert_ne!(cancel_after, retry_after);
    assert_eq!(focused(&h), Some(cancel_after), "the recreated prompt reset focus to safe Cancel");
    assert!(!h.app.quit_confirmed, "nothing quit");
}

#[test]
fn damaged_settings_file_is_shown() {
    let files = MemStore::new();
    files.set("presets.json", b"{\"user\": [ {\"id\": ".to_vec());
    let mut s = lightcraft_engine::Session::new();
    let stores = LibraryStores { dir: "lib".into(), catalog: Box::new(MemStore::new()), files: Box::new(files.clone()), on_disk: false };
    s.open_library_in(stores, false).unwrap();
    let app = LightcraftApp::new(s, Services { png: None, ..Default::default() });
    let mut h = Headless::new(app, [1200.0, 800.0], 1.0);
    h.step();
    h.step();
    assert_eq!(h.app.notices.len(), 1);
    assert!(h.app.notices[0].contains("presets.json is damaged"), "{:?}", h.app.notices);
    assert!(has(&h, "button:noticeOk"));
    assert!(files.files.lock().unwrap().keys().any(|k| k.starts_with("presets.json.corrupt-")));
    click(&mut h, "button:noticeOk");
    assert!(h.app.notices.is_empty(), "{:?}", h.app.notices);
    h.step();
    assert!(!has(&h, "button:noticeOk"));
}

/// A change the catalog log's thread accepted but hadn't written yet (a drive that takes a moment,
/// then fails) is not taken for saved: the quit check waits for the thread, finds the failure and
/// saves everything with the closing snapshot before the window may close (review of #3557). It
/// used to read only the failure the thread had already reported, and close with the change in
/// no file.
#[test]
fn quitting_waits_for_what_the_log_thread_has_not_written_yet() {
    use lightcraft_catalog::fault::{self, Step};
    let dir = std::env::temp_dir().join(format!("lc-ui-quit-log-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    let mut s = lightcraft_engine::Session::new();
    s.open_library(&dir, true).unwrap();
    let id = s.catalog.photos().map(|p| p.id).min().unwrap();
    fault::install(
        &dir,
        Arc::new(|step| {
            if step == Step::Write {
                std::thread::sleep(Duration::from_millis(400));
                return Err(std::io::Error::other("the drive is gone (test)"));
            }
            Ok(())
        }),
    );
    let app = LightcraftApp::new(s, Services { png: None, ..Default::default() });
    let mut h = Headless::new(app, [1200.0, 800.0], 1.0);
    let r = h.request("engine.execute", json!({"command": "photo.flag", "params": {"flag": "reject", "ids": [id.0]}}), T);
    assert_eq!(r["ok"], true, "{r}");
    let closes = crate::panels::notices::may_close(&mut h.app);
    let prompt = h.app.quit_prompt.as_ref().map(|p| p.text().to_string());
    // the files alone, as the next start reads them, hold the change
    let copy = dir.with_extension("copy");
    let _ = std::fs::remove_dir_all(&copy);
    std::fs::create_dir_all(&copy).unwrap();
    for f in [lightcraft_catalog::journal::SNAPSHOT, lightcraft_catalog::journal::LOG] {
        if dir.join(f).exists() {
            std::fs::copy(dir.join(f), copy.join(f)).unwrap();
        }
    }
    let (_, c, _) = lightcraft_catalog::Journal::open(Box::new(lightcraft_catalog::store::FsStore::open(&copy).unwrap())).unwrap();
    let saved = c.photo(id).map(|p| p.flag);
    fault::remove(&dir);
    drop(h);
    let _ = std::fs::remove_dir_all(&copy);
    let _ = std::fs::remove_dir_all(&dir);
    assert!(!closes || saved == Some(lightcraft_catalog::Flag::Reject), "closed with the flag in no file: {saved:?}");
    assert!(closes, "the closing snapshot saved it, so the window may close: {:?} {saved:?}", prompt);
}