PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Test idle autosave after a second edit
FredApps10b768ff42c4
diff --git a/crates/ui-egui/src/recovery.rs b/crates/ui-egui/src/recovery.rsindex a691bcdfe..b0b3761f5 100644--- a/crates/ui-egui/src/recovery.rs+++ b/crates/ui-egui/src/recovery.rs@@ -453,6 +453,53 @@ mod idle_tests {assert_eq!(delay, Duration::MAX, "unchanged snapshot needs no periodic wake");}+ #[test]+ fn second_edit_after_autosave_schedules_next_deadline_and_updates_snapshot() {+ let dir = TestDir::new();+ let store = RecoveryStore::new(dir.path());+ let mut app = PdfCraftApp::new();+ app.enable_recovery(store.clone());+ let bytes = app.session.create_blank(200.0, 300.0, 1).unwrap();+ let id = app.session.open_new("Untitled.pdf", bytes).unwrap();+ let ctx = egui::Context::default();+ frame(&mut app, &ctx, 60.0);+ let entries = store.list();+ assert_eq!(entries.len(), 1);+ let key = entries[0].key.clone();+ let first = store.read(&key).unwrap();+ assert!(!app.session.has_pending_autosave());+ let mut delay = Duration::ZERO;+ for _ in 0..4 {+ delay = frame(&mut app, &ctx, 61.0);+ }+ assert_eq!(delay, Duration::MAX);++ app.session.apply(id, pdfcraft_engine::Edit::SetInfo { key: "Title".into(), value: "Second edit".into() }).unwrap();+ assert!(app.session.has_pending_autosave());+ for _ in 0..4 {+ delay = frame(&mut app, &ctx, 75.0);+ }+ assert!(delay > Duration::ZERO && delay <= Duration::from_secs(45), "second edit wakes by 120 seconds: {delay:?}");+ assert_eq!(store.read(&key).unwrap(), first, "editing does not overwrite the snapshot before its deadline");+ frame(&mut app, &ctx, 119.0);+ assert_eq!(store.read(&key).unwrap(), first);+ frame(&mut app, &ctx, 120.0);+ let entries = store.list();+ assert_eq!(entries.len(), 1, "the same document replaces its existing snapshot");+ assert_eq!(entries[0].key, key);+ let second = store.read(&key).unwrap();+ assert_ne!(second, first);+ let mut recovered = pdfcraft_engine::Session::new();+ let restored = recovered.open("Snapshot.pdf", None, second.into(), None).unwrap();+ assert_eq!(recovered.get(restored).unwrap().info_value("Title"), Some("Second edit".into()));+ assert!(app.session.get(id).unwrap().dirty);+ assert!(!app.session.has_pending_autosave());+ for _ in 0..4 {+ delay = frame(&mut app, &ctx, 121.0);+ }+ assert_eq!(delay, Duration::MAX, "the second snapshot also lets the window sleep");+ }+#[test]fn clean_documents_and_disabled_recovery_do_not_schedule_autosave() {for recovery_enabled in [false, true] {