mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-30 02:04:54 +01:00
Mutate the document directly via ds_mut() instead of execute() so live_edit() does not run, assert pending() == 0, and verify saved_doc stays unchanged across live_poll().
208 lines
9.8 KiB
Rust
208 lines
9.8 KiB
Rust
//! A file joined on a server that keeps it has nothing to save on this computer once the server
|
|
//! confirmed all of it: no "Unsaved changes" question when its tab closes or the app quits.
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use figcraft_collab::message::{ClientMsg, HostInfo, ServerMsg};
|
|
use figcraft_collab::{Client, Limits, Outgoing, Server};
|
|
use figcraft_doc::Document;
|
|
use serde_json::json;
|
|
|
|
use crate::Session;
|
|
use crate::live::{Event, Role, Status};
|
|
use crate::live_tests::{Script, info, peer, push, relay, session};
|
|
|
|
/// You joined "Team file" on a server as a guest: the session, its server and your client id.
|
|
fn joined() -> (Session, Arc<Mutex<Script>>, Server, u32) {
|
|
let (mut s, script) = session();
|
|
let mut doc = Document::new("Team file");
|
|
doc.repair();
|
|
let mut server = Server::new(doc, "", Limits::default());
|
|
let ClientMsg::Hello(h) = Client::hello("f", "", "Me", "") else { panic!("no hello") };
|
|
let (me, outs) = server.join(h).unwrap();
|
|
let Some(Outgoing::To(_, w)) = outs.into_iter().find(|o| matches!(o, Outgoing::To(_, ServerMsg::Welcome { .. }))) else { panic!("no welcome") };
|
|
s.execute("collab.join", &json!({"link": "figcraft://join?host=a:1&file=f&token=t"})).unwrap();
|
|
push(&script, vec![Event::Welcome { info: info(Role::Guest, me), peers: vec![peer(1, "Hana")] }, Event::Sync(w)]);
|
|
s.live_poll();
|
|
assert_eq!(s.doc().unwrap().name, "Team file");
|
|
(s, script, server, me)
|
|
}
|
|
|
|
/// Draw a rectangle, confirm it stays dirty across a poll while pending, then let the server confirm it.
|
|
fn edit_and_confirm(s: &mut Session, script: &Arc<Mutex<Script>>, server: &mut Server, me: u32) {
|
|
s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
|
|
s.live_sync();
|
|
// An unrelated event while the batch is still pending must not clear the unsaved mark.
|
|
push(script, vec![Event::PeerJoined(peer(1, "Hana"))]);
|
|
s.live_poll();
|
|
assert!(s.ds().unwrap().is_dirty(), "an edit the server hasn't confirmed is unsaved");
|
|
push(script, relay(script, server, me));
|
|
s.live_poll();
|
|
}
|
|
|
|
#[test]
|
|
fn a_file_the_server_keeps_has_nothing_to_save_once_confirmed() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
|
s.live_poll();
|
|
assert!(!s.ds().unwrap().is_dirty(), "joining a kept file asks for no save");
|
|
edit_and_confirm(&mut s, &script, &mut server, me);
|
|
assert!(!s.ds().unwrap().is_dirty(), "the server has every edit");
|
|
// Someone else's edit is on the server too.
|
|
let ClientMsg::Hello(h) = Client::hello("f", "", "Ada", "") else { panic!("no hello") };
|
|
let (ada, outs) = server.join(h).unwrap();
|
|
let Some(Outgoing::To(_, w)) = outs.into_iter().find(|o| matches!(o, Outgoing::To(_, ServerMsg::Welcome { .. }))) else { panic!("no welcome") };
|
|
let mut other = Client::join(w).unwrap();
|
|
let mut doc = other.document().clone();
|
|
doc.name = "Renamed".into();
|
|
let batch = other.edit(&doc).unwrap().unwrap();
|
|
let remote: Vec<Event> =
|
|
server.receive(ada, batch).into_iter().filter_map(|o| if let Outgoing::All(m) = o { Some(Event::Sync(m)) } else { None }).collect();
|
|
push(&script, remote);
|
|
s.live_poll();
|
|
assert_eq!(s.doc().unwrap().name, "Renamed");
|
|
assert!(!s.ds().unwrap().is_dirty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_session_an_app_hosts_still_asks() {
|
|
// No hosting info: an app hosts the session, and nothing keeps the file once it leaves.
|
|
let (mut s, script, mut server, me) = joined();
|
|
edit_and_confirm(&mut s, &script, &mut server, me);
|
|
assert!(s.ds().unwrap().is_dirty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_file_with_an_expiry_time_still_asks() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
push(&script, vec![Event::Hosting(HostInfo { open: true, expires: Some(4_102_444_800), ..HostInfo::default() })]);
|
|
s.live_poll();
|
|
edit_and_confirm(&mut s, &script, &mut server, me);
|
|
assert!(s.ds().unwrap().is_dirty(), "a server that expires the file keeps no backups");
|
|
}
|
|
|
|
#[test]
|
|
fn a_tab_with_a_file_of_its_own_still_asks() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
|
s.live_poll();
|
|
s.ds_mut().unwrap().path = Some("team.figcraft".into());
|
|
edit_and_confirm(&mut s, &script, &mut server, me);
|
|
assert!(s.ds().unwrap().is_dirty(), "the file on this computer is behind");
|
|
}
|
|
|
|
#[test]
|
|
fn a_tab_with_changed_versions_still_asks() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
|
s.live_poll();
|
|
s.execute("file.saveVersion", &json!({"name": "Local snapshot"})).unwrap();
|
|
edit_and_confirm(&mut s, &script, &mut server, me);
|
|
assert!(s.ds().unwrap().is_dirty(), "versions live only in the local file");
|
|
assert!(s.ds().unwrap().versions_changed);
|
|
}
|
|
|
|
#[test]
|
|
fn a_branch_tab_still_asks() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
|
s.live_poll();
|
|
s.ds_mut().unwrap().branch_of = Some((999, "b1".into()));
|
|
edit_and_confirm(&mut s, &script, &mut server, me);
|
|
assert!(s.ds().unwrap().is_dirty(), "branch tabs are excluded from live_kept");
|
|
}
|
|
|
|
#[test]
|
|
fn an_unsynced_local_edit_stays_unsaved_when_polling() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
// Confirm an edit before hosting arrives so saved_doc differs from synced.
|
|
edit_and_confirm(&mut s, &script, &mut server, me);
|
|
let saved_before = s.ds().unwrap().saved_doc.clone();
|
|
// Mutate the document directly without running live_edit(): pending() stays 0, but document_itself() != synced.
|
|
s.ds_mut().unwrap().doc_mut().name = "Local draft".into();
|
|
assert_eq!(s.live().client().unwrap().pending(), 0);
|
|
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
|
s.live_poll();
|
|
assert!(s.ds().unwrap().is_dirty(), "local edit not yet sent to the sync client must stay unsaved");
|
|
assert!(Arc::ptr_eq(&s.ds().unwrap().saved_doc, &saved_before), "unsynced local edit must not advance saved_doc");
|
|
}
|
|
|
|
#[test]
|
|
fn confirming_during_a_version_preview_marks_the_document_saved() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
s.execute("file.saveVersion", &json!({"name": "Earlier"})).unwrap();
|
|
// Simulate a version already saved before this edit so only the preview baseline is tested.
|
|
s.ds_mut().unwrap().versions_changed = false;
|
|
// Confirm the edit before hosting info arrives so the tab is still dirty entering the preview.
|
|
edit_and_confirm(&mut s, &script, &mut server, me);
|
|
assert!(s.ds().unwrap().is_dirty());
|
|
s.execute("version.preview", &json!({"index": 0})).unwrap();
|
|
assert!(s.ds().unwrap().preview.is_some());
|
|
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
|
s.live_poll();
|
|
assert!(!s.ds().unwrap().is_dirty(), "confirmed underlying document is saved during a version preview");
|
|
s.execute("version.exitPreview", &json!({})).unwrap();
|
|
assert!(!s.ds().unwrap().is_dirty(), "document stays saved after ending a version preview");
|
|
}
|
|
|
|
#[test]
|
|
fn confirming_during_a_text_preview_marks_the_document_saved() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
let text = s.execute("text.create", &json!({"text": "Preview"})).unwrap()["id"].clone();
|
|
s.live_sync();
|
|
push(&script, relay(&script, &mut server, me));
|
|
s.live_poll();
|
|
assert!(s.ds().unwrap().is_dirty());
|
|
s.execute("text.preview", &json!({"command": "text.style", "params": {"ids": [text], "fontSize": 32}})).unwrap();
|
|
assert!(s.ds().unwrap().text_preview.is_some());
|
|
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
|
s.live_poll();
|
|
assert!(!s.ds().unwrap().is_dirty(), "confirmed underlying document is saved during a text preview");
|
|
s.execute("text.endPreview", &json!({})).unwrap();
|
|
assert!(!s.ds().unwrap().is_dirty(), "document stays saved after ending a text preview");
|
|
}
|
|
|
|
#[test]
|
|
fn a_deferred_confirmation_stays_unsaved_until_applied_after_preview() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
|
s.live_poll();
|
|
let text = s.execute("text.create", &json!({"text": "Deferred"})).unwrap()["id"].clone();
|
|
s.live_sync();
|
|
// Start a text preview before the server echoes the batch so the confirmation is deferred.
|
|
s.execute("text.preview", &json!({"command": "text.style", "params": {"ids": [text], "fontSize": 28}})).unwrap();
|
|
push(&script, relay(&script, &mut server, me));
|
|
s.live_poll();
|
|
assert!(s.ds().unwrap().is_dirty(), "deferred confirmation must not clear the unsaved mark yet");
|
|
// Ending the preview lets live_sync() apply the deferred batch with no further network events.
|
|
s.execute("text.endPreview", &json!({})).unwrap();
|
|
s.live_sync();
|
|
assert!(!s.ds().unwrap().is_dirty(), "applying the deferred confirmation clears the unsaved mark");
|
|
}
|
|
|
|
#[test]
|
|
fn a_held_session_stays_unsaved_until_released() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
let uid = s.ds().unwrap().uid;
|
|
edit_and_confirm(&mut s, &script, &mut server, me);
|
|
assert!(s.ds().unwrap().is_dirty());
|
|
s.live_hold(uid, true);
|
|
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
|
s.live_poll();
|
|
assert!(s.ds().unwrap().is_dirty(), "a held session must stay unsaved");
|
|
s.live_hold(uid, false);
|
|
s.live_sync();
|
|
assert!(!s.ds().unwrap().is_dirty(), "releasing the hold allows the confirmed tab to become saved");
|
|
}
|
|
|
|
#[test]
|
|
fn a_disconnected_session_stays_unsaved_even_with_hosting_info() {
|
|
let (mut s, script, mut server, me) = joined();
|
|
edit_and_confirm(&mut s, &script, &mut server, me);
|
|
assert!(s.ds().unwrap().is_dirty());
|
|
// Hosting info arrives while reconnecting (not Status::Live).
|
|
push(&script, vec![Event::Status(Status::Reconnecting { attempt: 1 }), Event::Hosting(HostInfo::default())]);
|
|
s.live_poll();
|
|
assert!(s.ds().unwrap().is_dirty(), "a non-live session must stay unsaved");
|
|
}
|