From d93b55f7ff2c30cc0722f2c1eb4416f9ce9782c5 Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Fri, 13 Nov 2026 18:47:51 -0600 Subject: [PATCH] fix(cloud): retain conflict-copy identity through retries --- crates/ui/src/cloud/app.rs | 3 + crates/ui/src/cloud/conflict_tests.rs | 79 +++++++++++++++++++++++++++ crates/ui/src/cloud/mod.rs | 19 ++++++- crates/ui/src/cloud/pages.rs | 4 +- 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/crates/ui/src/cloud/app.rs b/crates/ui/src/cloud/app.rs index b98e91ba5..cfb697935 100644 --- a/crates/ui/src/cloud/app.rs +++ b/crates/ui/src/cloud/app.rs @@ -222,6 +222,9 @@ pub trait CloudApp { /// Take the user to the application's own Save As page, with the name /// [`CloudFiles::save_name`](crate::cloud::CloudFiles::save_name) now /// holding. Called when a refused save is answered with Save as a copy. + /// Hosts call [`cancel_save_as`](crate::cloud::cancel_save_as) when leaving + /// that page without submitting, or before opening an ordinary Save As, + /// so the abandoned copy's document and suggested name cannot carry over. fn go_to_save_as(&mut self) {} // ---- its words ------------------------------------------------------- diff --git a/crates/ui/src/cloud/conflict_tests.rs b/crates/ui/src/cloud/conflict_tests.rs index c8e3d75cf..1e22e815f 100644 --- a/crates/ui/src/cloud/conflict_tests.rs +++ b/crates/ui/src/cloud/conflict_tests.rs @@ -234,3 +234,82 @@ fn a_late_reply_from_before_sign_out_is_dropped() { r["entries"] ); } + +/// Save as a copy, then the first click on the Save As page is refused (no name typed yet). The +/// second click, with a name, must still copy the conflicted document A, not the active tab B. +#[test] +fn conflict_copy_survives_a_refused_first_attempt() { + let (app, held, log) = conflict_on_a_while_b_is_active(); + let mut app = click_in_conflict_modal(app, "Save as a copy…"); + let refused = save_as(&mut app, "", ""); + assert!(refused.is_err(), "an empty name is refused: {refused:?}"); + let name = app.cloud.save_name.clone(); + save_as(&mut app, "", &name).unwrap(); + let (job, _) = held.take(|j| matches!(j, Job::Create { .. })).unwrap(); + let Job::Create { uid, bytes, .. } = job else { panic!("expected Job::Create") }; + assert_eq!( + (uid, String::from_utf8_lossy(&bytes).into_owned()), + (1, "bytes of 1".to_owned()), + "after one refused attempt the copy of A.note encodes uid {uid} (encoded uids {:?})", + log.lock().unwrap().encoded + ); +} + +/// Save as a copy, but the user leaves the Save As page without saving. Later, on tab B, an +/// ordinary Save As must save B, not the conflicted A from the abandoned copy. +#[test] +fn cancelled_conflict_copy_does_not_hijack_a_later_save_as() { + let (app, held, log) = conflict_on_a_while_b_is_active(); + let app = click_in_conflict_modal(app, "Save as a copy…"); + let mut app = app; + cancel_save_as(&mut app); // the host leaves the Save As page without submitting + assert!(app.cloud.save_name.is_empty()); + assert_eq!(app.active, 2); + save_as(&mut app, "", "B renamed").unwrap(); + let (job, _) = held.take(|j| matches!(j, Job::Create { .. })).unwrap(); + let Job::Create { uid, name, bytes, .. } = job else { panic!("expected Job::Create") }; + assert_eq!( + (uid, String::from_utf8_lossy(&bytes).into_owned()), + (2, "bytes of 2".to_owned()), + "an ordinary Save As of tab B as {name:?} encoded uid {uid} (encoded uids {:?})", + log.lock().unwrap().encoded + ); +} + +/// A successfully saved conflict copy must not redirect a later ordinary Save As. +#[test] +fn successful_conflict_copy_releases_its_document_target() { + let (app, held, _log) = conflict_on_a_while_b_is_active(); + let mut app = click_in_conflict_modal(app, "Save as a copy…"); + let name = app.cloud.save_name.clone(); + save_as(&mut app, "", &name).unwrap(); + let (job, post) = held.take(|job| matches!(job, Job::Create { .. })).unwrap(); + let Job::Create { uid, .. } = job else { panic!("expected Job::Create") }; + assert_eq!(uid, 1); + assert_eq!(app.cloud.save_as_target, Some(1)); + post.send(Event::Saved { uid, entry: Ok(entry("/A-copy.note", "a2")) }); + drain(&mut app, &egui::Context::default()); + assert_eq!(app.cloud.save_as_target, None); + save_as(&mut app, "", "B renamed").unwrap(); + let (job, _) = held.take(|job| matches!(job, Job::Create { .. })).unwrap(); + let Job::Create { uid, bytes, .. } = job else { panic!("expected Job::Create") }; + assert_eq!((uid, bytes), (2, b"bytes of 2".to_vec())); +} + +/// A refused create keeps the same document pinned while the user changes its name. +#[test] +fn conflict_copy_survives_a_backend_refusal() { + let (app, held, _log) = conflict_on_a_while_b_is_active(); + let mut app = click_in_conflict_modal(app, "Save as a copy…"); + let name = app.cloud.save_name.clone(); + save_as(&mut app, "", &name).unwrap(); + let (job, post) = held.take(|job| matches!(job, Job::Create { .. })).unwrap(); + let Job::Create { uid, .. } = job else { panic!("expected Job::Create") }; + assert_eq!(uid, 1); + post.send(Event::Saved { uid, entry: Err(Fault::Refused("exists".to_owned())) }); + drain(&mut app, &egui::Context::default()); + save_as(&mut app, "", "A another copy").unwrap(); + let (job, _) = held.take(|job| matches!(job, Job::Create { .. })).unwrap(); + let Job::Create { uid, bytes, .. } = job else { panic!("expected Job::Create") }; + assert_eq!((uid, bytes), (1, b"bytes of 1".to_vec())); +} diff --git a/crates/ui/src/cloud/mod.rs b/crates/ui/src/cloud/mod.rs index ca261d23d..3844c4f8f 100644 --- a/crates/ui/src/cloud/mod.rs +++ b/crates/ui/src/cloud/mod.rs @@ -222,7 +222,8 @@ pub struct CloudFiles { /// /// Set only by the conflict modal's "Save as a copy", so the copy it asks /// for is made from the conflicted document even if the user has since - /// switched to another tab. Consumed by the next [`save_as`] call. + /// switched to another tab. Cleared after a successful save or by + /// [`cancel_save_as`] when the interaction is abandoned. save_as_target: Option, } @@ -580,6 +581,17 @@ pub fn save(app: &mut A) -> Result { Ok(answer) } +/// Ends an unsubmitted Save As interaction. +/// +/// Hosts call this when leaving the Save As page without saving, and before +/// opening an unrelated Save As. It does not cancel a transfer already sent +/// to the backend. +pub fn cancel_save_as(app: &mut A) { + let cloud = app.cloud_mut(); + cloud.save_as_target = None; + cloud.save_name.clear(); +} + /// Save As: writes a new document into `folder` under `name`. /// /// Acts on [`CloudFiles::save_as_target`] when the conflict modal's "Save as @@ -588,7 +600,7 @@ pub fn save(app: &mut A) -> Result { /// active document, same as every other ordinary Save As. pub fn save_as(app: &mut A, folder: &str, name: &str) -> Result { let kind = app.kind(); - let uid = app.cloud_mut().save_as_target.take().or_else(|| app.active()).ok_or(format!("no {} open", kind.noun))?; + let uid = app.cloud().save_as_target.or_else(|| app.active()).ok_or(format!("no {} open", kind.noun))?; let name = document_name(name, kind); if name.is_empty() { return Err("type a file name".to_owned()); @@ -714,6 +726,9 @@ fn apply(app: &mut A, event: Event) { app.cloud_mut().busy = None; match entry { Ok(entry) => { + if app.cloud().save_as_target == Some(uid) { + app.cloud_mut().save_as_target = None; + } app.mark_saved(uid); stored(app, uid, entry); } diff --git a/crates/ui/src/cloud/pages.rs b/crates/ui/src/cloud/pages.rs index 665370952..86e71d1c8 100644 --- a/crates/ui/src/cloud/pages.rs +++ b/crates/ui/src/cloud/pages.rs @@ -351,7 +351,9 @@ fn shared_files(app: &mut A, ui: &mut Ui, pick: &mut Option<(Entry, /// File › Save As, the cloud destination. Pick a folder, name the file, save. /// Returns true when a save started, so a Backstage view can return to the -/// document. +/// document. Hosts call [`super::cancel_save_as`] when leaving without saving, +/// or before opening this page for an ordinary File › Save As, to discard any +/// abandoned conflict-copy target and suggested name. pub fn save_page(app: &mut A, ui: &mut Ui) -> bool { let style = app.style(ui); let kind = app.kind();